From 5ae8b5b30fb2de8837d86eb5c675b2565cb05608 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Mon, 12 Jun 2023 16:57:52 +0100 Subject: [PATCH] Avoid _distutils_hack.remove_shim on Python >= 3.12 --- _distutils_hack/__init__.py | 13 ++++++++++--- setuptools/sandbox.py | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index f987a5367fd..acfefc2ea97 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -142,7 +142,7 @@ def spec_for_pip(self): Ensure stdlib distutils when running under pip. See pypa/pip#8761 for rationale. """ - if self.pip_imported_during_build(): + if self.pip_imported_during_build() or sys.version_info >= (3, 12): return clear_distutils() self.spec_for_distutils = lambda: None @@ -208,14 +208,21 @@ def __enter__(self): insert_shim() def __exit__(self, exc, value, tb): - remove_shim() + _remove_shim() def insert_shim(): sys.meta_path.insert(0, DISTUTILS_FINDER) -def remove_shim(): +if sys.version_info < (3, 12): + # DistutilsMetaFinder should not be disabled in Python >= 3.12 + + def remove_shim(): + _remove_shim() + + +def _remove_shim(): try: sys.meta_path.remove(DISTUTILS_FINDER) except ValueError: diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 034fc80d20e..017c897b86a 100644 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -237,7 +237,7 @@ def hide_setuptools(): """ _distutils_hack = sys.modules.get('_distutils_hack', None) if _distutils_hack is not None: - _distutils_hack.remove_shim() + _distutils_hack._remove_shim() modules = filter(_needs_hiding, sys.modules) _clear_modules(modules)