Skip to content
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

Avoid _distutils_hack.remove_shim on Python >= 3.12 #3952

Merged
merged 5 commits into from
Jun 19, 2023
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
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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole module is private, so it's probably not necessary to mark it private, but also no problem if it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Jason. The renaming of the function was inspired by this piece of code in pip:

try:
    __import__("_distutils_hack").remove_shim()
except (ImportError, AttributeError):
    pass

Due to the rename the call would fail with an AttributeError that would be simply ignored. Without the renaming, there is some change of the importer being disable on Python >= 3.12.

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