Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/importlib_metadata/_compat.py: 96%
23 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1import os
2import sys
3import platform
5from typing import Union
8__all__ = ['install', 'NullFinder', 'Protocol']
11try:
12 from typing import Protocol
13except ImportError: # pragma: no cover
14 # Python 3.7 compatibility
15 from typing_extensions import Protocol # type: ignore
18def install(cls):
19 """
20 Class decorator for installation on sys.meta_path.
22 Adds the backport DistributionFinder to sys.meta_path and
23 attempts to disable the finder functionality of the stdlib
24 DistributionFinder.
25 """
26 sys.meta_path.append(cls())
27 disable_stdlib_finder()
28 return cls
31def disable_stdlib_finder():
32 """
33 Give the backport primacy for discovering path-based distributions
34 by monkey-patching the stdlib O_O.
36 See #91 for more background for rationale on this sketchy
37 behavior.
38 """
40 def matches(finder):
41 return getattr(
42 finder, '__module__', None
43 ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
45 for finder in filter(matches, sys.meta_path): # pragma: nocover
46 del finder.find_distributions
49class NullFinder:
50 """
51 A "Finder" (aka "MetaClassFinder") that never finds any modules,
52 but may find distributions.
53 """
55 @staticmethod
56 def find_spec(*args, **kwargs):
57 return None
60def pypy_partial(val):
61 """
62 Adjust for variable stacklevel on partial under PyPy.
64 Workaround for #327.
65 """
66 is_pypy = platform.python_implementation() == 'PyPy'
67 return val + is_pypy
70if sys.version_info >= (3, 9):
71 StrPath = Union[str, os.PathLike[str]]
72else:
73 # PathLike is only subscriptable at runtime in 3.9+
74 StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover