From 3913e338f0ec1cdd570e419a8add114ac8aafe20 Mon Sep 17 00:00:00 2001 From: DongHun Kwak Date: Wed, 30 Dec 2020 07:08:59 +0900 Subject: [PATCH] Imported Upstream version 50.3.0 --- .bumpversion.cfg | 2 +- CHANGES.rst | 6 +++++ setup.cfg | 2 +- setuptools/_distutils/_msvccompiler.py | 26 ++++++++++++++++++- .../_distutils/tests/test_msvccompiler.py | 20 ++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 0bb91b4..7a0aef4 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 50.2.0 +current_version = 50.3.0 commit = True tag = True diff --git a/CHANGES.rst b/CHANGES.rst index 9962f63..83b56b6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +v50.3.0 +------- + +* #2368: In distutils, restore support for monkeypatched CCompiler.spawn per pypa/distutils#15. + + v50.2.0 ------- diff --git a/setup.cfg b/setup.cfg index d35e5fa..7851845 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,7 +16,7 @@ formats = zip [metadata] name = setuptools -version = 50.2.0 +version = 50.3.0 description = Easily download, build, install, upgrade, and uninstall Python packages author = Python Packaging Authority author_email = distutils-sig@python.org diff --git a/setuptools/_distutils/_msvccompiler.py b/setuptools/_distutils/_msvccompiler.py index 2d56ee0..e9af4cf 100644 --- a/setuptools/_distutils/_msvccompiler.py +++ b/setuptools/_distutils/_msvccompiler.py @@ -16,6 +16,8 @@ for older versions in distutils.msvc9compiler and distutils.msvccompiler. import os import subprocess import contextlib +import warnings +import unittest.mock with contextlib.suppress(ImportError): import winreg @@ -504,7 +506,29 @@ class MSVCCompiler(CCompiler) : def spawn(self, cmd): env = dict(os.environ, PATH=self._paths) - return super().spawn(cmd, env=env) + with self._fallback_spawn(cmd, env) as fallback: + return super().spawn(cmd, env=env) + return fallback.value + + @contextlib.contextmanager + def _fallback_spawn(self, cmd, env): + """ + Discovered in pypa/distutils#15, some tools monkeypatch the compiler, + so the 'env' kwarg causes a TypeError. Detect this condition and + restore the legacy, unsafe behavior. + """ + bag = type('Bag', (), {})() + try: + yield bag + except TypeError as exc: + if "unexpected keyword argument 'env'" not in str(exc): + raise + else: + return + warnings.warn( + "Fallback spawn triggered. Please update distutils monkeypatch.") + with unittest.mock.patch('os.environ', env): + bag.value = super().spawn(cmd) # -- Miscellaneous methods ----------------------------------------- # These are all used by the 'gen_lib_options() function, in diff --git a/setuptools/_distutils/tests/test_msvccompiler.py b/setuptools/_distutils/tests/test_msvccompiler.py index 88d912b..46a51cd 100644 --- a/setuptools/_distutils/tests/test_msvccompiler.py +++ b/setuptools/_distutils/tests/test_msvccompiler.py @@ -110,6 +110,26 @@ class TestSpawn(unittest.TestCase): thread.join() assert all(threads) + def test_concurrent_safe_fallback(self): + """ + If CCompiler.spawn has been monkey-patched without support + for an env, it should still execute. + """ + import distutils._msvccompiler as _msvccompiler + from distutils import ccompiler + compiler = _msvccompiler.MSVCCompiler() + compiler._paths = "expected" + + def CCompiler_spawn(self, cmd): + "A spawn without an env argument." + assert os.environ["PATH"] == "expected" + + with unittest.mock.patch.object( + ccompiler.CCompiler, 'spawn', CCompiler_spawn): + compiler.spawn(["n/a"]) + + assert os.environ.get("PATH") != "expected" + def test_suite(): return unittest.makeSuite(msvccompilerTestCase) -- 2.34.1