_compat.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import platform
  2. import sys
  3. __all__ = ['install', 'NullFinder']
  4. def install(cls):
  5. """
  6. Class decorator for installation on sys.meta_path.
  7. Adds the backport DistributionFinder to sys.meta_path and
  8. attempts to disable the finder functionality of the stdlib
  9. DistributionFinder.
  10. """
  11. sys.meta_path.append(cls())
  12. disable_stdlib_finder()
  13. return cls
  14. def disable_stdlib_finder():
  15. """
  16. Give the backport primacy for discovering path-based distributions
  17. by monkey-patching the stdlib O_O.
  18. See #91 for more background for rationale on this sketchy
  19. behavior.
  20. """
  21. def matches(finder):
  22. return getattr(
  23. finder, '__module__', None
  24. ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
  25. for finder in filter(matches, sys.meta_path): # pragma: nocover
  26. del finder.find_distributions
  27. class NullFinder:
  28. """
  29. A "Finder" (aka "MetaPathFinder") that never finds any modules,
  30. but may find distributions.
  31. """
  32. @staticmethod
  33. def find_spec(*args, **kwargs):
  34. return None
  35. def pypy_partial(val):
  36. """
  37. Adjust for variable stacklevel on partial under PyPy.
  38. Workaround for #327.
  39. """
  40. is_pypy = platform.python_implementation() == 'PyPy'
  41. return val + is_pypy