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

1import os 

2import sys 

3import platform 

4 

5from typing import Union 

6 

7 

8__all__ = ['install', 'NullFinder', 'Protocol'] 

9 

10 

11try: 

12 from typing import Protocol 

13except ImportError: # pragma: no cover 

14 # Python 3.7 compatibility 

15 from typing_extensions import Protocol # type: ignore 

16 

17 

18def install(cls): 

19 """ 

20 Class decorator for installation on sys.meta_path. 

21 

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 

29 

30 

31def disable_stdlib_finder(): 

32 """ 

33 Give the backport primacy for discovering path-based distributions 

34 by monkey-patching the stdlib O_O. 

35 

36 See #91 for more background for rationale on this sketchy 

37 behavior. 

38 """ 

39 

40 def matches(finder): 

41 return getattr( 

42 finder, '__module__', None 

43 ) == '_frozen_importlib_external' and hasattr(finder, 'find_distributions') 

44 

45 for finder in filter(matches, sys.meta_path): # pragma: nocover 

46 del finder.find_distributions 

47 

48 

49class NullFinder: 

50 """ 

51 A "Finder" (aka "MetaClassFinder") that never finds any modules, 

52 but may find distributions. 

53 """ 

54 

55 @staticmethod 

56 def find_spec(*args, **kwargs): 

57 return None 

58 

59 

60def pypy_partial(val): 

61 """ 

62 Adjust for variable stacklevel on partial under PyPy. 

63 

64 Workaround for #327. 

65 """ 

66 is_pypy = platform.python_implementation() == 'PyPy' 

67 return val + is_pypy 

68 

69 

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