]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/_distutils_hack/__init__.py
1 # don't import any costly modules
6 is_pypy
= '__pypy__' in sys
.builtin_module_names
9 def warn_distutils_present():
10 if 'distutils' not in sys
.modules
:
12 if is_pypy
and sys
.version_info
< (3, 7):
13 # PyPy for 3.6 unconditionally imports distutils, so bypass the warning
14 # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
19 "Distutils was imported before Setuptools, but importing Setuptools "
20 "also replaces the `distutils` module in `sys.modules`. This may lead "
21 "to undesirable behaviors or errors. To avoid these issues, avoid "
22 "using distutils directly, ensure that setuptools is installed in the "
23 "traditional way (e.g. not an editable install), and/or make sure "
24 "that setuptools is always imported before distutils."
28 def clear_distutils():
29 if 'distutils' not in sys
.modules
:
33 warnings
.warn("Setuptools is replacing distutils.")
36 for name
in sys
.modules
37 if name
== "distutils" or name
.startswith("distutils.")
45 Allow selection of distutils by environment variable.
47 which
= os
.environ
.get('SETUPTOOLS_USE_DISTUTILS', 'local')
48 return which
== 'local'
51 def ensure_local_distutils():
56 # With the DistutilsMetaFinder in place,
57 # perform an import to cause distutils to be
58 # loaded from setuptools._distutils. Ref #2906.
60 importlib
.import_module('distutils')
62 # check that submodules load as expected
63 core
= importlib
.import_module('distutils.core')
64 assert '_distutils' in core
.__file
__, core
.__file
__
65 assert 'setuptools._distutils.log' not in sys
.modules
70 Ensure that the local copy of distutils is preferred over stdlib.
72 See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
76 warn_distutils_present()
77 ensure_local_distutils()
81 def __init__(self
, *patterns
):
82 self
._patterns
= patterns
84 def match(self
, string
):
85 return all(pat
in string
for pat
in self
._patterns
)
88 class DistutilsMetaFinder
:
89 def find_spec(self
, fullname
, path
, target
=None):
90 # optimization: only consider top level modules and those
91 # found in the CPython test suite.
92 if path
is not None and not fullname
.startswith('test.'):
95 method_name
= 'spec_for_{fullname}'.format(**locals())
96 method
= getattr(self
, method_name
, lambda: None)
99 def spec_for_distutils(self
):
100 if self
.is_cpython():
105 import importlib
.util
108 mod
= importlib
.import_module('setuptools._distutils')
110 # There are a couple of cases where setuptools._distutils
111 # may not be present:
112 # - An older Setuptools without a local distutils is
113 # taking precedence. Ref #2957.
114 # - Path manipulation during sitecustomize removes
115 # setuptools from the path but only after the hook
116 # has been loaded. Ref #2980.
117 # In either case, fall back to stdlib behavior.
120 class DistutilsLoader(importlib
.abc
.Loader
):
121 def create_module(self
, spec
):
122 mod
.__name
__ = 'distutils'
125 def exec_module(self
, module
):
128 return importlib
.util
.spec_from_loader(
129 'distutils', DistutilsLoader(), origin
=mod
.__file
__
135 Suppress supplying distutils for CPython (build and tests).
138 return os
.path
.isfile('pybuilddir.txt')
140 def spec_for_pip(self
):
142 Ensure stdlib distutils when running under pip.
143 See pypa/pip#8761 for rationale.
145 if sys
.version_info
>= (3, 12) or self
.pip_imported_during_build():
148 self
.spec_for_distutils
= lambda: None
151 def pip_imported_during_build(cls
):
153 Detect if pip is being imported in a build script. Ref #2355.
158 cls
.frame_file_is_setup(frame
) for frame
, line
in traceback
.walk_stack(None)
162 def frame_file_is_setup(frame
):
164 Return True if the indicated frame suggests a setup.py file.
166 # some frames may not have __file__ (#2940)
167 return frame
.f_globals
.get('__file__', '').endswith('setup.py')
169 def spec_for_sensitive_tests(self
):
171 Ensure stdlib distutils when running select tests under CPython.
176 self
.spec_for_distutils
= lambda: None
180 'test.test_distutils',
181 'test.test_peg_generator',
182 'test.test_importlib',
184 if sys
.version_info
< (3, 10)
186 'test.test_distutils',
191 for name
in DistutilsMetaFinder
.sensitive_tests
:
195 DistutilsMetaFinder
.spec_for_sensitive_tests
,
199 DISTUTILS_FINDER
= DistutilsMetaFinder()
203 DISTUTILS_FINDER
in sys
.meta_path
or insert_shim()
210 def __exit__(self
, exc
, value
, tb
):
215 sys
.meta_path
.insert(0, DISTUTILS_FINDER
)
220 sys
.meta_path
.remove(DISTUTILS_FINDER
)
225 if sys
.version_info
< (3, 12):
226 # DistutilsMetaFinder can only be disabled in Python < 3.12 (PEP 632)
227 remove_shim
= _remove_shim