Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/importlib_metadata/_compat.py: 100%
20 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1import sys
2import platform
5__all__ = ['install', 'NullFinder', 'Protocol']
8try:
9 from typing import Protocol
10except ImportError: # pragma: no cover
11 # Python 3.7 compatibility
12 from typing_extensions import Protocol # type: ignore
15def install(cls):
16 """
17 Class decorator for installation on sys.meta_path.
19 Adds the backport DistributionFinder to sys.meta_path and
20 attempts to disable the finder functionality of the stdlib
21 DistributionFinder.
22 """
23 sys.meta_path.append(cls())
24 disable_stdlib_finder()
25 return cls
28def disable_stdlib_finder():
29 """
30 Give the backport primacy for discovering path-based distributions
31 by monkey-patching the stdlib O_O.
33 See #91 for more background for rationale on this sketchy
34 behavior.
35 """
37 def matches(finder):
38 return getattr(
39 finder, '__module__', None
40 ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
42 for finder in filter(matches, sys.meta_path): # pragma: nocover
43 del finder.find_distributions
46class NullFinder:
47 """
48 A "Finder" (aka "MetaClassFinder") that never finds any modules,
49 but may find distributions.
50 """
52 @staticmethod
53 def find_spec(*args, **kwargs):
54 return None
56 # In Python 2, the import system requires finders
57 # to have a find_module() method, but this usage
58 # is deprecated in Python 3 in favor of find_spec().
59 # For the purposes of this finder (i.e. being present
60 # on sys.meta_path but having no other import
61 # system functionality), the two methods are identical.
62 find_module = find_spec
65def pypy_partial(val):
66 """
67 Adjust for variable stacklevel on partial under PyPy.
69 Workaround for #327.
70 """
71 is_pypy = platform.python_implementation() == 'PyPy'
72 return val + is_pypy