diff options
-rw-r--r-- | _distutils_importer/__init__.py | 17 | ||||
-rw-r--r-- | setuptools/sandbox.py | 19 |
2 files changed, 30 insertions, 6 deletions
diff --git a/_distutils_importer/__init__.py b/_distutils_importer/__init__.py index 54a825fe..498c4ac1 100644 --- a/_distutils_importer/__init__.py +++ b/_distutils_importer/__init__.py @@ -1,6 +1,17 @@ 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() diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 93ae8eb4..342a713f 100644 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -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) |