Skip to content

Create additional workarounds for failing tests in CPython #3386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2022
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
47 changes: 41 additions & 6 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ def warn_distutils_present():
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
return
import warnings

warnings.warn(
"Distutils was imported before Setuptools, but importing Setuptools "
"also replaces the `distutils` module in `sys.modules`. This may lead "
"to undesirable behaviors or errors. To avoid these issues, avoid "
"using distutils directly, ensure that setuptools is installed in the "
"traditional way (e.g. not an editable install), and/or make sure "
"that setuptools is always imported before distutils.")
"that setuptools is always imported before distutils."
)


def clear_distutils():
if 'distutils' not in sys.modules:
return
import warnings

warnings.warn("Setuptools is replacing distutils.")
mods = [
name for name in sys.modules
name
for name in sys.modules
if name == "distutils" or name.startswith("distutils.")
]
for name in mods:
Expand All @@ -46,6 +50,7 @@ def enabled():

def ensure_local_distutils():
import importlib

clear_distutils()

# With the DistutilsMetaFinder in place,
Expand Down Expand Up @@ -82,7 +87,9 @@ def match(self, string):

class DistutilsMetaFinder:
def find_spec(self, fullname, path, target=None):
if path is not None:
# optimization: only consider top level modules and those
# found in the CPython test suite.
if path is not None and not fullname.startswith('test.'):
return

method_name = 'spec_for_{fullname}'.format(**locals())
Expand Down Expand Up @@ -111,7 +118,6 @@ def spec_for_distutils(self):
return

class DistutilsLoader(importlib.abc.Loader):

def create_module(self, spec):
mod.__name__ = 'distutils'
return mod
Expand Down Expand Up @@ -147,9 +153,9 @@ def pip_imported_during_build(cls):
Detect if pip is being imported in a build script. Ref #2355.
"""
import traceback

return any(
cls.frame_file_is_setup(frame)
for frame, line in traceback.walk_stack(None)
cls.frame_file_is_setup(frame) for frame, line in traceback.walk_stack(None)
)

@staticmethod
Expand All @@ -160,6 +166,35 @@ def frame_file_is_setup(frame):
# some frames may not have __file__ (#2940)
return frame.f_globals.get('__file__', '').endswith('setup.py')

def spec_for_sensitive_tests(self):
"""
Ensure stdlib distutils when running select tests under CPython.

python/cpython#91169
"""
clear_distutils()
self.spec_for_distutils = lambda: None

sensitive_tests = (
[
'test.test_distutils',
'test.test_peg_generator',
'test.test_importlib',
]
if sys.version_info < (3, 10)
else [
'test.test_distutils',
]
)


for name in DistutilsMetaFinder.sensitive_tests:
setattr(
DistutilsMetaFinder,
f'spec_for_{name}',
DistutilsMetaFinder.spec_for_sensitive_tests,
)


DISTUTILS_FINDER = DistutilsMetaFinder()

Expand Down
1 change: 1 addition & 0 deletions changelog.d/3383.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In _distutils_hack, suppress/undo the use of local distutils when select tests are imported in CPython.