Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/setuptools/_imp.py: 24%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

51 statements  

1""" 

2Re-implementation of find_module and get_frozen_object 

3from the deprecated imp module. 

4""" 

5 

6import os 

7import importlib.util 

8import importlib.machinery 

9 

10from .py34compat import module_from_spec 

11 

12 

13PY_SOURCE = 1 

14PY_COMPILED = 2 

15C_EXTENSION = 3 

16C_BUILTIN = 6 

17PY_FROZEN = 7 

18 

19 

20def find_module(module, paths=None): 

21 """Just like 'imp.find_module()', but with package support""" 

22 spec = importlib.util.find_spec(module, paths) 

23 if spec is None: 

24 raise ImportError("Can't find %s" % module) 

25 if not spec.has_location and hasattr(spec, 'submodule_search_locations'): 

26 spec = importlib.util.spec_from_loader('__init__.py', spec.loader) 

27 

28 kind = -1 

29 file = None 

30 static = isinstance(spec.loader, type) 

31 if spec.origin == 'frozen' or static and issubclass( 

32 spec.loader, importlib.machinery.FrozenImporter): 

33 kind = PY_FROZEN 

34 path = None # imp compabilty 

35 suffix = mode = '' # imp compability 

36 elif spec.origin == 'built-in' or static and issubclass( 

37 spec.loader, importlib.machinery.BuiltinImporter): 

38 kind = C_BUILTIN 

39 path = None # imp compabilty 

40 suffix = mode = '' # imp compability 

41 elif spec.has_location: 

42 path = spec.origin 

43 suffix = os.path.splitext(path)[1] 

44 mode = 'r' if suffix in importlib.machinery.SOURCE_SUFFIXES else 'rb' 

45 

46 if suffix in importlib.machinery.SOURCE_SUFFIXES: 

47 kind = PY_SOURCE 

48 elif suffix in importlib.machinery.BYTECODE_SUFFIXES: 

49 kind = PY_COMPILED 

50 elif suffix in importlib.machinery.EXTENSION_SUFFIXES: 

51 kind = C_EXTENSION 

52 

53 if kind in {PY_SOURCE, PY_COMPILED}: 

54 file = open(path, mode) 

55 else: 

56 path = None 

57 suffix = mode = '' 

58 

59 return file, path, (suffix, mode, kind) 

60 

61 

62def get_frozen_object(module, paths=None): 

63 spec = importlib.util.find_spec(module, paths) 

64 if not spec: 

65 raise ImportError("Can't find %s" % module) 

66 return spec.loader.get_code(module) 

67 

68 

69def get_module(module, paths, info): 

70 spec = importlib.util.find_spec(module, paths) 

71 if not spec: 

72 raise ImportError("Can't find %s" % module) 

73 return module_from_spec(spec)