__init__.py 6.6 KB

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