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

Fallback to stdlib distutils if setuptools._distutils is missing #4441

Closed
Closed
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
46 changes: 34 additions & 12 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,43 @@ def clear_distutils():
del sys.modules[name]


def get_local_distutils():
import importlib

try:
return importlib.import_module('setuptools._distutils')
except Exception:
# There are a couple of cases where setuptools._distutils
# may not be present:
# - An older Setuptools without a local distutils is
# taking precedence. Ref #2957.
# - Path manipulation during sitecustomize removes
# setuptools from the path but only after the hook
# has been loaded. Ref #2980.
# - setuptools._distutils has been removed, either by the user
# or by packaging / distribution.
# In either case, fall back to stdlib behavior.
return None


def enabled():
"""
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
return which == 'local'
if which != 'local':
return False

if sys.version_info < (3, 12) and not get_local_distutils():
import warnings

warnings.warn(
"environment variable SETUPTOOLS_USE_DISTUTILS is set to 'local' or None, "
+ "but `setuptools._distutils` could not be imported. Falling back to stdlib "
+ "distutils. This will lead to an error in Python 3.12 and above."
)
return False
return True


def ensure_local_distutils():
Expand Down Expand Up @@ -97,17 +128,8 @@ def spec_for_distutils(self):
import importlib.abc
import importlib.util

try:
mod = importlib.import_module('setuptools._distutils')
except Exception:
# There are a couple of cases where setuptools._distutils
# may not be present:
# - An older Setuptools without a local distutils is
# taking precedence. Ref #2957.
# - Path manipulation during sitecustomize removes
# setuptools from the path but only after the hook
# has been loaded. Ref #2980.
# In either case, fall back to stdlib behavior.
mod = get_local_distutils()
if not mod:
return None

class DistutilsLoader(importlib.abc.Loader):
Expand Down
1 change: 1 addition & 0 deletions newsfragments/4441.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a fallback to stdlib `distutils` if ``setuptools._distutils`` is missing on Python 3.11 or below, even with ``SETUPTOOLS_USE_DISTUTILS=local``. -- by :user:`Avasam`
Loading