aboutsummaryrefslogtreecommitdiffstats
path: root/setuptools
diff options
context:
space:
mode:
Diffstat (limited to 'setuptools')
-rw-r--r--setuptools/__init__.py10
-rw-r--r--setuptools/distutils_patch.py61
-rw-r--r--setuptools/sandbox.py19
3 files changed, 20 insertions, 70 deletions
diff --git a/setuptools/__init__.py b/setuptools/__init__.py
index 83882511..99094230 100644
--- a/setuptools/__init__.py
+++ b/setuptools/__init__.py
@@ -1,17 +1,15 @@
"""Extensions to the 'distutils' for large or complex distributions"""
-import os
+from fnmatch import fnmatchcase
import functools
+import os
+import re
-# Disabled for now due to: #2228, #2230
-import setuptools.distutils_patch # noqa: F401
+import _distutils_hack.override # noqa: F401
import distutils.core
-import distutils.filelist
-import re
from distutils.errors import DistutilsOptionError
from distutils.util import convert_path
-from fnmatch import fnmatchcase
from ._deprecation_warning import SetuptoolsDeprecationWarning
diff --git a/setuptools/distutils_patch.py b/setuptools/distutils_patch.py
deleted file mode 100644
index 33f1e7f9..00000000
--- a/setuptools/distutils_patch.py
+++ /dev/null
@@ -1,61 +0,0 @@
-"""
-Ensure that the local copy of distutils is preferred over stdlib.
-
-See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
-for more motivation.
-"""
-
-import sys
-import re
-import os
-import importlib
-import warnings
-
-
-is_pypy = '__pypy__' in sys.builtin_module_names
-
-
-def warn_distutils_present():
- if 'distutils' not in sys.modules:
- return
- if is_pypy and sys.version_info < (3, 7):
- # PyPy for 3.6 unconditionally imports distutils, so bypass the warning
- # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
- return
- warnings.warn(
- "Distutils was imported before Setuptools. This usage is discouraged "
- "and may exhibit undesirable behaviors or errors. Please use "
- "Setuptools' objects directly or at least import Setuptools first.")
-
-
-def clear_distutils():
- if 'distutils' not in sys.modules:
- return
- warnings.warn("Setuptools is replacing distutils.")
- mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
- for name in mods:
- del sys.modules[name]
-
-
-def enabled():
- """
- Allow selection of distutils by environment variable.
- """
- which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
- return which == 'local'
-
-
-def ensure_local_distutils():
- clear_distutils()
- distutils = importlib.import_module('setuptools._distutils')
- distutils.__name__ = 'distutils'
- sys.modules['distutils'] = distutils
-
- # sanity check that submodules load as expected
- core = importlib.import_module('distutils.core')
- assert '_distutils' in core.__file__, core.__file__
-
-
-warn_distutils_present()
-if enabled():
- ensure_local_distutils()
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py
index 93ae8eb4..24a36080 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_hack',
+}
+
+
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_hack = sys.modules.get('_distutils_hack', None)
+ if _distutils_hack is not None:
+ _distutils_hack.remove_shim()
+
modules = filter(_needs_hiding, sys.modules)
_clear_modules(modules)