diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index 834a062ca86..1f8daf498b7 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -89,6 +89,9 @@ def find_spec(self, fullname, path, target=None): return method() def spec_for_distutils(self): + if self.is_cpython(): + return + import importlib import importlib.abc import importlib.util @@ -118,6 +121,14 @@ def exec_module(self, module): 'distutils', DistutilsLoader(), origin=mod.__file__ ) + @staticmethod + def is_cpython(): + """ + Suppress supplying distutils for CPython (build and tests). + Ref #2965 and #3007. + """ + return os.path.isfile('pybuilddir.txt') + def spec_for_pip(self): """ Ensure stdlib distutils when running under pip. @@ -135,9 +146,9 @@ def spec_for_setuptools(self): a stubbed spec to represent setuptools being present without invoking any behavior. - Workaround for pypa/get-pip#137. + Workaround for pypa/get-pip#137. Ref #2993. """ - if not self.is_get_pip(): + if not self.is_script('get-pip'): return import importlib @@ -166,14 +177,11 @@ def pip_imported_during_build(cls): for frame, line in traceback.walk_stack(None) ) - @classmethod - def is_get_pip(cls): - """ - Detect if get-pip is being invoked. Ref #2993. - """ + @staticmethod + def is_script(name): try: import __main__ - return os.path.basename(__main__.__file__) == 'get-pip.py' + return os.path.basename(__main__.__file__) == f'{name}.py' except AttributeError: pass diff --git a/changelog.d/3031.misc.rst b/changelog.d/3031.misc.rst new file mode 100644 index 00000000000..b5af0169fc2 --- /dev/null +++ b/changelog.d/3031.misc.rst @@ -0,0 +1 @@ +Suppress distutils replacement when building or testing CPython. diff --git a/changelog.d/NNN.misc.rst b/changelog.d/NNN.misc.rst new file mode 100644 index 00000000000..e69de29bb2d