Imported Upstream version 50.3.0 upstream/50.3.0
authorDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 29 Dec 2020 22:08:59 +0000 (07:08 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 29 Dec 2020 22:08:59 +0000 (07:08 +0900)
.bumpversion.cfg
CHANGES.rst
setup.cfg
setuptools/_distutils/_msvccompiler.py
setuptools/_distutils/tests/test_msvccompiler.py

index 0bb91b48fa59ca85519b2541934379f2cc7a6dc5..7a0aef412a903fb0430ecbd332701c365fccce55 100644 (file)
@@ -1,5 +1,5 @@
 [bumpversion]
-current_version = 50.2.0
+current_version = 50.3.0
 commit = True
 tag = True
 
index 9962f63d08512b6b6f6092b2bbd3eded49a9b0f5..83b56b65cc5dd491e30256b5d01f4fdf75c76793 100644 (file)
@@ -1,3 +1,9 @@
+v50.3.0
+-------
+
+* #2368: In distutils, restore support for monkeypatched CCompiler.spawn per pypa/distutils#15.
+
+
 v50.2.0
 -------
 
index d35e5fa2fdb0e290bcf7e0c6aa6124fd9ecb898a..78518457901dff78a22d3b916cf923dbf91d6a31 100644 (file)
--- 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
index 2d56ee0aada4b1073e4e814e44cf1bd5fbe9e3cf..e9af4cf52b163dae7a3657f8b30dc87a48d9ef64 100644 (file)
@@ -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
index 88d912b17b874eece6b079b4a0a6472bd01cb268..46a51cd0a7b85cfae328fa50c5d916db5dc57a13 100644 (file)
@@ -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)