distutils_patch.py 872 B

12345678910111213141516171819202122232425262728293031323334
  1. """
  2. Ensure that the local copy of distutils is preferred over stdlib.
  3. See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
  4. for more motivation.
  5. """
  6. import sys
  7. import re
  8. import importlib
  9. import warnings
  10. def clear_distutils():
  11. if 'distutils' not in sys.modules:
  12. return
  13. warnings.warn("Setuptools is replacing distutils")
  14. mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
  15. for name in mods:
  16. del sys.modules[name]
  17. def ensure_local_distutils():
  18. clear_distutils()
  19. distutils = importlib.import_module('setuptools._distutils')
  20. distutils.__name__ = 'distutils'
  21. sys.modules['distutils'] = distutils
  22. # sanity check that submodules load as expected
  23. core = importlib.import_module('distutils.core')
  24. assert '_distutils' in core.__file__, core.__file__
  25. ensure_local_distutils()