__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import sys
  2. import os
  3. import re
  4. import importlib
  5. import warnings
  6. is_pypy = '__pypy__' in sys.builtin_module_names
  7. def warn_distutils_present():
  8. if 'distutils' not in sys.modules:
  9. return
  10. if is_pypy and sys.version_info < (3, 7):
  11. # PyPy for 3.6 unconditionally imports distutils, so bypass the warning
  12. # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
  13. return
  14. warnings.warn(
  15. "Distutils was imported before Setuptools, but importing Setuptools "
  16. "also replaces the `distutils` module in `sys.modules`. This may lead "
  17. "to undesirable behaviors or errors. To avoid these issues, avoid "
  18. "using distutils directly, ensure that setuptools is installed in the "
  19. "traditional way (e.g. not an editable install), and/or make sure "
  20. "that setuptools is always imported before distutils.")
  21. def clear_distutils():
  22. if 'distutils' not in sys.modules:
  23. return
  24. warnings.warn("Setuptools is replacing distutils.")
  25. mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
  26. for name in mods:
  27. del sys.modules[name]
  28. def enabled():
  29. """
  30. Allow selection of distutils by environment variable.
  31. """
  32. which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
  33. return which == 'local'
  34. def ensure_local_distutils():
  35. clear_distutils()
  36. distutils = importlib.import_module('setuptools._distutils')
  37. distutils.__name__ = 'distutils'
  38. sys.modules['distutils'] = distutils
  39. # sanity check that submodules load as expected
  40. core = importlib.import_module('distutils.core')
  41. assert '_distutils' in core.__file__, core.__file__
  42. def do_override():
  43. """
  44. Ensure that the local copy of distutils is preferred over stdlib.
  45. See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
  46. for more motivation.
  47. """
  48. if enabled():
  49. warn_distutils_present()
  50. ensure_local_distutils()
  51. class DistutilsMetaFinder:
  52. def find_spec(self, fullname, path, target=None):
  53. if path is not None:
  54. return
  55. method_name = 'spec_for_{fullname}'.format(**locals())
  56. method = getattr(self, method_name, lambda: None)
  57. return method()
  58. def spec_for_distutils(self):
  59. import importlib.abc
  60. import importlib.util
  61. class DistutilsLoader(importlib.abc.Loader):
  62. def create_module(self, spec):
  63. return importlib.import_module('setuptools._distutils')
  64. def exec_module(self, module):
  65. pass
  66. return importlib.util.spec_from_loader('distutils', DistutilsLoader())
  67. def spec_for_pip(self):
  68. """
  69. Ensure stdlib distutils when running under pip.
  70. See pypa/pip#8761 for rationale.
  71. """
  72. if self.pip_imported_during_build():
  73. return
  74. clear_distutils()
  75. self.spec_for_distutils = lambda: None
  76. @staticmethod
  77. def pip_imported_during_build():
  78. """
  79. Detect if pip is being imported in a build script. Ref #2355.
  80. """
  81. import traceback
  82. return any(
  83. frame.f_globals['__file__'].endswith('setup.py')
  84. for frame, line in traceback.walk_stack(None)
  85. )
  86. DISTUTILS_FINDER = DistutilsMetaFinder()
  87. def add_shim():
  88. sys.meta_path.insert(0, DISTUTILS_FINDER)
  89. def remove_shim():
  90. try:
  91. sys.meta_path.remove(DISTUTILS_FINDER)
  92. except ValueError:
  93. pass