This commit is contained in:
lz_db
2026-10-16 22:48:30 +08:00
parent 190de72571
commit 0b36fd3383
3161 changed files with 12787 additions and 77274 deletions
@@ -1,15 +1,18 @@
# don't import any costly modules
import os
import sys
import os
report_url = (
"https://github.com/pypa/setuptools/issues/new?template=distutils-deprecation.yml"
)
is_pypy = '__pypy__' in sys.builtin_module_names
def warn_distutils_present():
if 'distutils' not in sys.modules:
return
if is_pypy and sys.version_info < (3, 7):
# PyPy for 3.6 unconditionally imports distutils, so bypass the warning
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
return
import warnings
warnings.warn(
@@ -27,12 +30,7 @@ def clear_distutils():
return
import warnings
warnings.warn(
"Setuptools is replacing distutils. Support for replacing "
"an already imported distutils is deprecated. In the future, "
"this condition will fail. "
f"Register concerns at {report_url}"
)
warnings.warn("Setuptools is replacing distutils.")
mods = [
name
for name in sys.modules
@@ -47,16 +45,6 @@ def enabled():
Allow selection of distutils by environment variable.
"""
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
if which == 'stdlib':
import warnings
warnings.warn(
"Reliance on distutils from stdlib is deprecated. Users "
"must rely on setuptools to provide the distutils module. "
"Avoid importing distutils or import setuptools first, "
"and avoid setting SETUPTOOLS_USE_DISTUTILS=stdlib. "
f"Register concerns at {report_url}"
)
return which == 'local'
@@ -90,7 +78,7 @@ def do_override():
class _TrivialRe:
def __init__(self, *patterns) -> None:
def __init__(self, *patterns):
self._patterns = patterns
def match(self, string):
@@ -102,7 +90,7 @@ class DistutilsMetaFinder:
# optimization: only consider top level modules and those
# found in the CPython test suite.
if path is not None and not fullname.startswith('test.'):
return None
return
method_name = 'spec_for_{fullname}'.format(**locals())
method = getattr(self, method_name, lambda: None)
@@ -110,7 +98,7 @@ class DistutilsMetaFinder:
def spec_for_distutils(self):
if self.is_cpython():
return None
return
import importlib
import importlib.abc
@@ -127,7 +115,7 @@ class DistutilsMetaFinder:
# setuptools from the path but only after the hook
# has been loaded. Ref #2980.
# In either case, fall back to stdlib behavior.
return None
return
class DistutilsLoader(importlib.abc.Loader):
def create_module(self, spec):
@@ -154,7 +142,7 @@ class DistutilsMetaFinder:
Ensure stdlib distutils when running under pip.
See pypa/pip#8761 for rationale.
"""
if sys.version_info >= (3, 12) or self.pip_imported_during_build():
if self.pip_imported_during_build():
return
clear_distutils()
self.spec_for_distutils = lambda: None
@@ -216,24 +204,19 @@ def add_shim():
class shim:
def __enter__(self) -> None:
def __enter__(self):
insert_shim()
def __exit__(self, exc: object, value: object, tb: object) -> None:
_remove_shim()
def __exit__(self, exc, value, tb):
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