m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB

6 months ago
  1. import os
  2. import sys
  3. import platform
  4. from typing import Union
  5. __all__ = ['install', 'NullFinder', 'Protocol']
  6. try:
  7. from typing import Protocol
  8. except ImportError: # pragma: no cover
  9. # Python 3.7 compatibility
  10. from typing_extensions import Protocol # type: ignore
  11. def install(cls):
  12. """
  13. Class decorator for installation on sys.meta_path.
  14. Adds the backport DistributionFinder to sys.meta_path and
  15. attempts to disable the finder functionality of the stdlib
  16. DistributionFinder.
  17. """
  18. sys.meta_path.append(cls())
  19. disable_stdlib_finder()
  20. return cls
  21. def disable_stdlib_finder():
  22. """
  23. Give the backport primacy for discovering path-based distributions
  24. by monkey-patching the stdlib O_O.
  25. See #91 for more background for rationale on this sketchy
  26. behavior.
  27. """
  28. def matches(finder):
  29. return getattr(
  30. finder, '__module__', None
  31. ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
  32. for finder in filter(matches, sys.meta_path): # pragma: nocover
  33. del finder.find_distributions
  34. class NullFinder:
  35. """
  36. A "Finder" (aka "MetaClassFinder") that never finds any modules,
  37. but may find distributions.
  38. """
  39. @staticmethod
  40. def find_spec(*args, **kwargs):
  41. return None
  42. def pypy_partial(val):
  43. """
  44. Adjust for variable stacklevel on partial under PyPy.
  45. Workaround for #327.
  46. """
  47. is_pypy = platform.python_implementation() == 'PyPy'
  48. return val + is_pypy
  49. if sys.version_info >= (3, 9):
  50. StrPath = Union[str, os.PathLike[str]]
  51. else:
  52. # PathLike is only subscriptable at runtime in 3.9+
  53. StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover