__init__.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # don't import any costly modules
  2. import sys
  3. import os
  4. report_url = (
  5. "https://github.com/pypa/setuptools/issues/new?"
  6. "template=distutils-deprecation.yml"
  7. )
  8. def warn_distutils_present():
  9. if 'distutils' not in sys.modules:
  10. return
  11. import warnings
  12. warnings.warn(
  13. "Distutils was imported before Setuptools, but importing Setuptools "
  14. "also replaces the `distutils` module in `sys.modules`. This may lead "
  15. "to undesirable behaviors or errors. To avoid these issues, avoid "
  16. "using distutils directly, ensure that setuptools is installed in the "
  17. "traditional way (e.g. not an editable install), and/or make sure "
  18. "that setuptools is always imported before distutils."
  19. )
  20. def clear_distutils():
  21. if 'distutils' not in sys.modules:
  22. return
  23. import warnings
  24. warnings.warn(
  25. "Setuptools is replacing distutils. Support for replacing "
  26. "an already imported distutils is deprecated. In the future, "
  27. "this condition will fail. "
  28. f"Register concerns at {report_url}"
  29. )
  30. mods = [
  31. name
  32. for name in sys.modules
  33. if name == "distutils" or name.startswith("distutils.")
  34. ]
  35. for name in mods:
  36. del sys.modules[name]
  37. def enabled():
  38. """
  39. Allow selection of distutils by environment variable.
  40. """
  41. which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local')
  42. if which == 'stdlib':
  43. import warnings
  44. warnings.warn(
  45. "Reliance on distutils from stdlib is deprecated. Users "
  46. "must rely on setuptools to provide the distutils module. "
  47. "Avoid importing distutils or import setuptools first, "
  48. "and avoid setting SETUPTOOLS_USE_DISTUTILS=stdlib. "
  49. f"Register concerns at {report_url}"
  50. )
  51. return which == 'local'
  52. def ensure_local_distutils():
  53. import importlib
  54. clear_distutils()
  55. # With the DistutilsMetaFinder in place,
  56. # perform an import to cause distutils to be
  57. # loaded from setuptools._distutils. Ref #2906.
  58. with shim():
  59. importlib.import_module('distutils')
  60. # check that submodules load as expected
  61. core = importlib.import_module('distutils.core')
  62. assert '_distutils' in core.__file__, core.__file__
  63. assert 'setuptools._distutils.log' not in sys.modules
  64. def do_override():
  65. """
  66. Ensure that the local copy of distutils is preferred over stdlib.
  67. See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
  68. for more motivation.
  69. """
  70. if enabled():
  71. warn_distutils_present()
  72. ensure_local_distutils()
  73. class _TrivialRe:
  74. def __init__(self, *patterns):
  75. self._patterns = patterns
  76. def match(self, string):
  77. return all(pat in string for pat in self._patterns)
  78. class DistutilsMetaFinder:
  79. def find_spec(self, fullname, path, target=None):
  80. # optimization: only consider top level modules and those
  81. # found in the CPython test suite.
  82. if path is not None and not fullname.startswith('test.'):
  83. return None
  84. method_name = 'spec_for_{fullname}'.format(**locals())
  85. method = getattr(self, method_name, lambda: None)
  86. return method()
  87. def spec_for_distutils(self):
  88. if self.is_cpython():
  89. return None
  90. import importlib
  91. import importlib.abc
  92. import importlib.util
  93. try:
  94. mod = importlib.import_module('setuptools._distutils')
  95. except Exception:
  96. # There are a couple of cases where setuptools._distutils
  97. # may not be present:
  98. # - An older Setuptools without a local distutils is
  99. # taking precedence. Ref #2957.
  100. # - Path manipulation during sitecustomize removes
  101. # setuptools from the path but only after the hook
  102. # has been loaded. Ref #2980.
  103. # In either case, fall back to stdlib behavior.
  104. return None
  105. class DistutilsLoader(importlib.abc.Loader):
  106. def create_module(self, spec):
  107. mod.__name__ = 'distutils'
  108. return mod
  109. def exec_module(self, module):
  110. pass
  111. return importlib.util.spec_from_loader(
  112. 'distutils', DistutilsLoader(), origin=mod.__file__
  113. )
  114. @staticmethod
  115. def is_cpython():
  116. """
  117. Suppress supplying distutils for CPython (build and tests).
  118. Ref #2965 and #3007.
  119. """
  120. return os.path.isfile('pybuilddir.txt')
  121. def spec_for_pip(self):
  122. """
  123. Ensure stdlib distutils when running under pip.
  124. See pypa/pip#8761 for rationale.
  125. """
  126. if sys.version_info >= (3, 12) or self.pip_imported_during_build():
  127. return
  128. clear_distutils()
  129. self.spec_for_distutils = lambda: None
  130. @classmethod
  131. def pip_imported_during_build(cls):
  132. """
  133. Detect if pip is being imported in a build script. Ref #2355.
  134. """
  135. import traceback
  136. return any(
  137. cls.frame_file_is_setup(frame) for frame, line in traceback.walk_stack(None)
  138. )
  139. @staticmethod
  140. def frame_file_is_setup(frame):
  141. """
  142. Return True if the indicated frame suggests a setup.py file.
  143. """
  144. # some frames may not have __file__ (#2940)
  145. return frame.f_globals.get('__file__', '').endswith('setup.py')
  146. def spec_for_sensitive_tests(self):
  147. """
  148. Ensure stdlib distutils when running select tests under CPython.
  149. python/cpython#91169
  150. """
  151. clear_distutils()
  152. self.spec_for_distutils = lambda: None
  153. sensitive_tests = (
  154. [
  155. 'test.test_distutils',
  156. 'test.test_peg_generator',
  157. 'test.test_importlib',
  158. ]
  159. if sys.version_info < (3, 10)
  160. else [
  161. 'test.test_distutils',
  162. ]
  163. )
  164. for name in DistutilsMetaFinder.sensitive_tests:
  165. setattr(
  166. DistutilsMetaFinder,
  167. f'spec_for_{name}',
  168. DistutilsMetaFinder.spec_for_sensitive_tests,
  169. )
  170. DISTUTILS_FINDER = DistutilsMetaFinder()
  171. def add_shim():
  172. DISTUTILS_FINDER in sys.meta_path or insert_shim()
  173. class shim:
  174. def __enter__(self):
  175. insert_shim()
  176. def __exit__(self, exc, value, tb):
  177. _remove_shim()
  178. def insert_shim():
  179. sys.meta_path.insert(0, DISTUTILS_FINDER)
  180. def _remove_shim():
  181. try:
  182. sys.meta_path.remove(DISTUTILS_FINDER)
  183. except ValueError:
  184. pass
  185. if sys.version_info < (3, 12):
  186. # DistutilsMetaFinder can only be disabled in Python < 3.12 (PEP 632)
  187. remove_shim = _remove_shim