Commit 89e9d3c8 authored by Paul Ganssle's avatar Paul Ganssle

Adjust distutils shim when removing _distutils_importer

parent 03b36b5d
import sys
here = os.path.dirname(__file__)
NEW_DISTUTILS_LOCATION = os.path.join(here, 'distutils-shim-package')
_HERE = os.path.dirname(__file__)
NEW_DISTUTILS_LOCATION = os.path.join(_HERE, 'distutils-shim-package')
sys.path.insert(0, NEW_DISTUTILS_LOCATION)
def add_shim():
if NEW_DISTUTILS_LOCATION not in sys.path:
sys.path.insert(0, NEW_DISTUTILS_LOCATION)
def remove_shim():
try:
sys.path.remove(NEW_DISTUTILS_LOCATION)
except ValueError:
pass
add_shim()
......@@ -185,8 +185,8 @@ def setup_context(setup_dir):
temp_dir = os.path.join(setup_dir, 'temp')
with save_pkg_resources_state():
with save_modules():
hide_setuptools()
with save_path():
hide_setuptools()
with save_argv():
with override_temp(temp_dir):
with pushd(setup_dir):
......@@ -195,6 +195,15 @@ def setup_context(setup_dir):
yield
_MODULES_TO_HIDE = {
'setuptools',
'distutils',
'pkg_resources',
'Cython',
'_distutils_importer',
}
def _needs_hiding(mod_name):
"""
>>> _needs_hiding('setuptools')
......@@ -212,8 +221,8 @@ def _needs_hiding(mod_name):
>>> _needs_hiding('Cython')
True
"""
pattern = re.compile(r'(setuptools|pkg_resources|distutils|Cython)(\.|$)')
return bool(pattern.match(mod_name))
base_module = mod_name.split('.', 1)[0]
return base_module in _MODULES_TO_HIDE
def hide_setuptools():
......@@ -223,6 +232,10 @@ def hide_setuptools():
necessary to avoid issues such as #315 where setuptools upgrading itself
would fail to find a function declared in the metadata.
"""
_distutils_importer = sys.modules.get('_distutils_importer', None)
if _distutils_importer is not None:
_distutils_importer.remove_shim()
modules = filter(_needs_hiding, sys.modules)
_clear_modules(modules)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment