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.0.1, created at 2022-12-25 06:11 +0000

1import sys 

2import platform 

3 

4 

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

6 

7 

8try: 

9 from typing import Protocol 

10except ImportError: # pragma: no cover 

11 # Python 3.7 compatibility 

12 from typing_extensions import Protocol # type: ignore 

13 

14 

15def install(cls): 

16 """ 

17 Class decorator for installation on sys.meta_path. 

18 

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 

26 

27 

28def disable_stdlib_finder(): 

29 """ 

30 Give the backport primacy for discovering path-based distributions 

31 by monkey-patching the stdlib O_O. 

32 

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

34 behavior. 

35 """ 

36 

37 def matches(finder): 

38 return getattr( 

39 finder, '__module__', None 

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

41 

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

43 del finder.find_distributions 

44 

45 

46class NullFinder: 

47 """ 

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

49 but may find distributions. 

50 """ 

51 

52 @staticmethod 

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

54 return None 

55 

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 

63 

64 

65def pypy_partial(val): 

66 """ 

67 Adjust for variable stacklevel on partial under PyPy. 

68 

69 Workaround for #327. 

70 """ 

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

72 return val + is_pypy