Skip to content

Commit e20e85e

Browse files
authored
Merge pull request #2255 from pypa/bugfix/2232-adopt-distutils-default
Re-enable distutils patch by default.
2 parents 8d50896 + 3d404fd commit e20e85e

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

_distutils_hack/__init__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def enabled():
3737
"""
3838
Allow selection of distutils by environment variable.
3939
"""
40-
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
40+
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
4141
return which == 'local'
4242

4343

@@ -66,12 +66,14 @@ def do_override():
6666

6767
class DistutilsMetaFinder:
6868
def find_spec(self, fullname, path, target=None):
69-
if path is not None or fullname != "distutils":
70-
return None
69+
if path is not None:
70+
return
7171

72-
return self.get_distutils_spec()
72+
method_name = 'spec_for_{fullname}'.format(**locals())
73+
method = getattr(self, method_name, lambda: None)
74+
return method()
7375

74-
def get_distutils_spec(self):
76+
def spec_for_distutils(self):
7577
import importlib.util
7678

7779
class DistutilsLoader(importlib.util.abc.Loader):
@@ -84,6 +86,14 @@ def exec_module(self, module):
8486

8587
return importlib.util.spec_from_loader('distutils', DistutilsLoader())
8688

89+
def spec_for_pip(self):
90+
"""
91+
Ensure stdlib distutils when running under pip.
92+
See pypa/pip#8761 for rationale.
93+
"""
94+
clear_distutils()
95+
self.spec_for_distutils = lambda: None
96+
8797

8898
DISTUTILS_FINDER = DistutilsMetaFinder()
8999

changelog.d/2232.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ class install_with_pth(install):
9999
_pth_name = 'distutils-precedence'
100100
_pth_contents = textwrap.dedent("""
101101
import os
102-
enabled = os.environ.get('SETUPTOOLS_USE_DISTUTILS') == 'local'
102+
var = 'SETUPTOOLS_USE_DISTUTILS'
103+
enabled = os.environ.get(var, 'local') == 'local'
103104
enabled and __import__('_distutils_hack').add_shim()
104105
""").lstrip().replace('\n', '; ')
105106

setuptools/tests/test_distutils_adoption.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def test_distutils_stdlib(venv):
4848
"""
4949
Ensure stdlib distutils is used when appropriate.
5050
"""
51-
assert venv.name not in find_distutils(venv, env=dict()).split(os.sep)
51+
env = dict(SETUPTOOLS_USE_DISTUTILS='stdlib')
52+
assert venv.name not in find_distutils(venv, env=env).split(os.sep)
5253

5354

5455
def test_distutils_local_with_setuptools(venv):
5556
"""
5657
Ensure local distutils is used when appropriate.
5758
"""
58-
env = dict(SETUPTOOLS_USE_DISTUTILS='local')
59-
loc = find_distutils(venv, imports='setuptools, distutils', env=env)
59+
loc = find_distutils(venv, imports='setuptools, distutils', env=dict())
6060
assert venv.name in loc.split(os.sep)
6161

6262

@@ -66,5 +66,4 @@ def test_distutils_local(venv):
6666
Even without importing, the setuptools-local copy of distutils is
6767
preferred.
6868
"""
69-
env = dict(SETUPTOOLS_USE_DISTUTILS='local')
70-
assert venv.name in find_distutils(venv, env=env).split(os.sep)
69+
assert venv.name in find_distutils(venv, env=dict()).split(os.sep)

0 commit comments

Comments
 (0)