Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/importlib_metadata/_compat.py: 96%
24 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 06:10 +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
59 # In Python 2, the import system requires finders
60 # to have a find_module() method, but this usage
61 # is deprecated in Python 3 in favor of find_spec().
62 # For the purposes of this finder (i.e. being present
63 # on sys.meta_path but having no other import
64 # system functionality), the two methods are identical.
65 find_module = find_spec
68def pypy_partial(val):
69 """
70 Adjust for variable stacklevel on partial under PyPy.
72 Workaround for #327.
73 """
74 is_pypy = platform.python_implementation() == 'PyPy'
75 return val + is_pypy
78if sys.version_info >= (3, 9):
79 StrPath = Union[str, os.PathLike[str]]
80else:
81 # PathLike is only subscriptable at runtime in 3.9+
82 StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover