diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2014-12-28 15:39:46 -0500 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-12-28 15:39:46 -0500 |
commit | 824cfa0bc32d500da6fd045147f752e8bf02f7b3 (patch) | |
tree | b09ccf678b4e577607018eae639068245667224d /setuptools/sandbox.py | |
parent | beef07b0dd8d538b638d8f604efd83aee14097e3 (diff) | |
download | external_python_setuptools-824cfa0bc32d500da6fd045147f752e8bf02f7b3.tar.gz external_python_setuptools-824cfa0bc32d500da6fd045147f752e8bf02f7b3.tar.bz2 external_python_setuptools-824cfa0bc32d500da6fd045147f752e8bf02f7b3.zip |
Remove setuptools modules from sys.modules before invoking setup script. Fixes #315.
Diffstat (limited to 'setuptools/sandbox.py')
-rwxr-xr-x | setuptools/sandbox.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 131b7048..e958f912 100755 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -121,6 +121,7 @@ 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(): with save_argv(): with override_temp(temp_dir): @@ -128,6 +129,32 @@ def setup_context(setup_dir): yield +def _is_setuptools_module(mod_name): + """ + >>> is_setuptools_module('setuptools') + True + >>> is_setuptools_module('pkg_resources') + True + >>> is_setuptools_module('setuptools_plugin') + False + >>> is_setuptools_module('setuptools.__init__') + True + """ + pattern = re.compile('(setuptools|pkg_resources)(\.|$)') + return bool(pattern.match(mod_name)) + + +def hide_setuptools(): + """ + Remove references to setuptools' modules from sys.modules to allow the + invocation to import the most appropriate setuptools. This technique is + necessary to avoid issues such as #315 where setuptools upgrading itself + would fail to find a function declared in the metadata. + """ + modules = list(filter(_is_setuptools_module, sys.modules)) + list(map(sys.modules.__delitem__, modules)) + + def run_setup(setup_script, args): """Run a distutils setup script, sandboxed in its directory""" setup_dir = os.path.abspath(os.path.dirname(setup_script)) |