Skip to content

Commit

Permalink
Add fallback for the situation where distutils.ccompiler.CCompiler.sp…
Browse files Browse the repository at this point in the history
…awn has been patched. Fixes pypa/distutils#15.
  • Loading branch information
jaraco committed Sep 5, 2020
1 parent 37fa0e7 commit 6e92cda
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import subprocess
import contextlib
import unittest.mock
with contextlib.suppress(ImportError):
import winreg

Expand Down Expand Up @@ -504,7 +505,27 @@ def link(self,

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
Expand Down

0 comments on commit 6e92cda

Please sign in to comment.