From 08bb8a41cc976343795bd0e241cd7388e9f44ad5 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Fri, 17 Jun 2016 09:32:38 -0700 Subject: [PATCH] Issue #27048: Prevents distutils failing on Windows when environment variables contain non-ASCII characters --- Lib/distutils/_msvccompiler.py | 6 ++---- Lib/distutils/tests/test_msvccompiler.py | 18 ++++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Lib/distutils/_msvccompiler.py b/Lib/distutils/_msvccompiler.py index d0ba7d6d1ef..b120273fe9e 100644 --- a/Lib/distutils/_msvccompiler.py +++ b/Lib/distutils/_msvccompiler.py @@ -86,11 +86,9 @@ def _get_vc_env(plat_spec): try: out = subprocess.check_output( - '"{}" {} && set'.format(vcvarsall, plat_spec), - shell=True, + 'cmd /u /c "{}" {} && set'.format(vcvarsall, plat_spec), stderr=subprocess.STDOUT, - universal_newlines=True, - ) + ).decode('utf-16le', errors='replace') except subprocess.CalledProcessError as exc: log.error(exc.output) raise DistutilsPlatformError("Error executing {}" diff --git a/Lib/distutils/tests/test_msvccompiler.py b/Lib/distutils/tests/test_msvccompiler.py index c4d911ff838..4dc24886c82 100644 --- a/Lib/distutils/tests/test_msvccompiler.py +++ b/Lib/distutils/tests/test_msvccompiler.py @@ -83,6 +83,24 @@ class msvccompilerTestCase(support.TempdirManager, self.assertFalse(os.path.isfile(os.path.join( tempdir, os.path.basename(dll)))) + def test_get_vc_env_unicode(self): + import distutils._msvccompiler as _msvccompiler + + test_var = 'ṰḖṤṪ┅ṼẨṜ' + test_value = '₃⁴₅' + + # Ensure we don't early exit from _get_vc_env + old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None) + os.environ[test_var] = test_value + try: + env = _msvccompiler._get_vc_env('x86') + self.assertIn(test_var.lower(), env) + self.assertEqual(test_value, env[test_var.lower()]) + finally: + os.environ.pop(test_var) + if old_distutils_use_sdk: + os.environ['DISTUTILS_USE_SDK'] = old_distutils_use_sdk + def test_suite(): return unittest.makeSuite(msvccompilerTestCase) diff --git a/Misc/NEWS b/Misc/NEWS index 66a95f73088..5dff4dbdf5c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,6 +13,9 @@ Core and Builtins Library ------- +- Issue #27048: Prevents distutils failing on Windows when environment + variables contain non-ASCII characters + - Issue #27330: Fixed possible leaks in the ctypes module. - Issue #27238: Got rid of bare excepts in the turtle module. Original patch