Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down Expand Up @@ -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):
Expand All @@ -84,6 +86,14 @@ def exec_module(self, module):

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()

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2232.breaking.rst
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '; ')

Expand Down
9 changes: 4 additions & 5 deletions setuptools/tests/test_distutils_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)