Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/importlib_metadata/_compat.py: 90%
21 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:42 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:42 +0000
1import os
2import sys
3import platform
5from typing import Union
8__all__ = ['install', 'NullFinder']
11def install(cls):
12 """
13 Class decorator for installation on sys.meta_path.
15 Adds the backport DistributionFinder to sys.meta_path and
16 attempts to disable the finder functionality of the stdlib
17 DistributionFinder.
18 """
19 sys.meta_path.append(cls())
20 disable_stdlib_finder()
21 return cls
24def disable_stdlib_finder():
25 """
26 Give the backport primacy for discovering path-based distributions
27 by monkey-patching the stdlib O_O.
29 See #91 for more background for rationale on this sketchy
30 behavior.
31 """
33 def matches(finder):
34 return getattr(
35 finder, '__module__', None
36 ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions')
38 for finder in filter(matches, sys.meta_path): # pragma: nocover
39 del finder.find_distributions
42class NullFinder:
43 """
44 A "Finder" (aka "MetaClassFinder") that never finds any modules,
45 but may find distributions.
46 """
48 @staticmethod
49 def find_spec(*args, **kwargs):
50 return None
53def pypy_partial(val):
54 """
55 Adjust for variable stacklevel on partial under PyPy.
57 Workaround for #327.
58 """
59 is_pypy = platform.python_implementation() == 'PyPy'
60 return val + is_pypy
63if sys.version_info >= (3, 9):
64 StrPath = Union[str, os.PathLike[str]]
65else:
66 # PathLike is only subscriptable at runtime in 3.9+
67 StrPath = Union[str, "os.PathLike[str]"] # pragma: no cover