Skip to content

Commit

Permalink
Avoid _distutils_hack.remove_shim on Python >= 3.12 (#3952)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jun 19, 2023
2 parents 2d7bf2b + e391a09 commit 718493c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 sys.version_info >= (3, 12) or self.pip_imported_during_build():
return
clear_distutils()
self.spec_for_distutils = lambda: None
Expand Down Expand Up @@ -208,15 +208,20 @@ 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():
def _remove_shim():
try:
sys.meta_path.remove(DISTUTILS_FINDER)
except ValueError:
pass


if sys.version_info < (3, 12):
# DistutilsMetaFinder can only be disabled in Python < 3.12 (PEP 632)
remove_shim = _remove_shim
1 change: 1 addition & 0 deletions changelog.d/3952.change.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``DistutilsMetaFinder`` to skip ``spec_for_pip`` on Python >= 3.12.
3 changes: 3 additions & 0 deletions changelog.d/3952.change.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removed ``_distutils_hack.remove_shim`` on Python >= 3.12
(since ``distutils`` was removed from the standard library,
``DistutilsMetaFinder`` cannot be disabled on Python >= 3.12).
2 changes: 1 addition & 1 deletion setuptools/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 718493c

Please sign in to comment.