diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2020-09-05 10:58:55 -0400 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2020-09-05 10:58:55 -0400 |
| commit | 6e92cdabaff71ab2bab3300763101fb8f84c367f (patch) | |
| tree | 54a6ef1bcd6ce53801eb452115f1cdbf9ff58bab | |
| parent | 37fa0e7531fbd240131d498f43436aafd4393149 (diff) | |
| download | python-setuptools-git-6e92cdabaff71ab2bab3300763101fb8f84c367f.tar.gz | |
Add fallback for the situation where distutils.ccompiler.CCompiler.spawn has been patched. Fixes pypa/distutils#15.
| -rw-r--r-- | distutils/_msvccompiler.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/distutils/_msvccompiler.py b/distutils/_msvccompiler.py index 2d56ee0a..f7efd4eb 100644 --- a/distutils/_msvccompiler.py +++ b/distutils/_msvccompiler.py @@ -16,6 +16,7 @@ for older versions in distutils.msvc9compiler and distutils.msvccompiler. import os import subprocess import contextlib +import unittest.mock with contextlib.suppress(ImportError): import winreg @@ -504,7 +505,27 @@ 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 + 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 |
