summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--_distutils_hack/__init__.py20
-rw-r--r--changelog.d/2232.breaking.rst1
-rwxr-xr-xsetup.py3
-rw-r--r--setuptools/tests/test_distutils_adoption.py9
4 files changed, 22 insertions, 11 deletions
diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py
index 1e7b294b..074bd5e9 100644
--- a/_distutils_hack/__init__.py
+++ b/_distutils_hack/__init__.py
@@ -37,7 +37,7 @@ def enabled():
"""
Allow selection of distutils by environment variable.
"""
- which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
+ which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
return which == 'local'
@@ -66,12 +66,14 @@ def do_override():
class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
- if path is not None or fullname != "distutils":
- return None
+ if path is not None:
+ return
- return self.get_distutils_spec()
+ method_name = 'spec_for_{fullname}'.format(**locals())
+ method = getattr(self, method_name, lambda: None)
+ return method()
- def get_distutils_spec(self):
+ def spec_for_distutils(self):
import importlib.util
class DistutilsLoader(importlib.util.abc.Loader):
@@ -84,6 +86,14 @@ class DistutilsMetaFinder:
return importlib.util.spec_from_loader('distutils', DistutilsLoader())
+ def spec_for_pip(self):
+ """
+ Ensure stdlib distutils when running under pip.
+ See pypa/pip#8761 for rationale.
+ """
+ clear_distutils()
+ self.spec_for_distutils = lambda: None
+
DISTUTILS_FINDER = DistutilsMetaFinder()
diff --git a/changelog.d/2232.breaking.rst b/changelog.d/2232.breaking.rst
new file mode 100644
index 00000000..b2fd926f
--- /dev/null
+++ b/changelog.d/2232.breaking.rst
@@ -0,0 +1 @@
+Once again, Setuptools overrides the stdlib distutils on import. For environments or invocations where this behavior is undesirable, users are provided with a temporary escape hatch. If the environment variable ``SETUPTOOLS_USE_DISTUTILS`` is set to ``stdlib``, Setuptools will fall back to the legacy behavior. Use of this escape hatch is discouraged, but it is provided to ease the transition while proper fixes for edge cases can be addressed.
diff --git a/setup.py b/setup.py
index 5d98c029..34654e19 100755
--- a/setup.py
+++ b/setup.py
@@ -99,7 +99,8 @@ class install_with_pth(install):
_pth_name = 'distutils-precedence'
_pth_contents = textwrap.dedent("""
import os
- enabled = os.environ.get('SETUPTOOLS_USE_DISTUTILS') == 'local'
+ var = 'SETUPTOOLS_USE_DISTUTILS'
+ enabled = os.environ.get(var, 'local') == 'local'
enabled and __import__('_distutils_hack').add_shim()
""").lstrip().replace('\n', '; ')
diff --git a/setuptools/tests/test_distutils_adoption.py b/setuptools/tests/test_distutils_adoption.py
index daccc473..8edd3f9b 100644
--- a/setuptools/tests/test_distutils_adoption.py
+++ b/setuptools/tests/test_distutils_adoption.py
@@ -48,15 +48,15 @@ def test_distutils_stdlib(venv):
"""
Ensure stdlib distutils is used when appropriate.
"""
- assert venv.name not in find_distutils(venv, env=dict()).split(os.sep)
+ env = dict(SETUPTOOLS_USE_DISTUTILS='stdlib')
+ assert venv.name not in find_distutils(venv, env=env).split(os.sep)
def test_distutils_local_with_setuptools(venv):
"""
Ensure local distutils is used when appropriate.
"""
- env = dict(SETUPTOOLS_USE_DISTUTILS='local')
- loc = find_distutils(venv, imports='setuptools, distutils', env=env)
+ loc = find_distutils(venv, imports='setuptools, distutils', env=dict())
assert venv.name in loc.split(os.sep)
@@ -66,5 +66,4 @@ def test_distutils_local(venv):
Even without importing, the setuptools-local copy of distutils is
preferred.
"""
- env = dict(SETUPTOOLS_USE_DISTUTILS='local')
- assert venv.name in find_distutils(venv, env=env).split(os.sep)
+ assert venv.name in find_distutils(venv, env=dict()).split(os.sep)