]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/_imp.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / setuptools / _imp.py
1 """
2 Re-implementation of find_module and get_frozen_object
3 from the deprecated imp module.
4 """
5
6 import os
7 import importlib.util
8 import importlib.machinery
9
10 from importlib.util import module_from_spec
11
12
13 PY_SOURCE = 1
14 PY_COMPILED = 2
15 C_EXTENSION = 3
16 C_BUILTIN = 6
17 PY_FROZEN = 7
18
19
20 def find_spec(module, paths):
21 finder = (
22 importlib.machinery.PathFinder().find_spec
23 if isinstance(paths, list)
24 else importlib.util.find_spec
25 )
26 return finder(module, paths)
27
28
29 def find_module(module, paths=None):
30 """Just like 'imp.find_module()', but with package support"""
31 spec = find_spec(module, paths)
32 if spec is None:
33 raise ImportError("Can't find %s" % module)
34 if not spec.has_location and hasattr(spec, 'submodule_search_locations'):
35 spec = importlib.util.spec_from_loader('__init__.py', spec.loader)
36
37 kind = -1
38 file = None
39 static = isinstance(spec.loader, type)
40 if (
41 spec.origin == 'frozen'
42 or static
43 and issubclass(spec.loader, importlib.machinery.FrozenImporter)
44 ):
45 kind = PY_FROZEN
46 path = None # imp compabilty
47 suffix = mode = '' # imp compatibility
48 elif (
49 spec.origin == 'built-in'
50 or static
51 and issubclass(spec.loader, importlib.machinery.BuiltinImporter)
52 ):
53 kind = C_BUILTIN
54 path = None # imp compabilty
55 suffix = mode = '' # imp compatibility
56 elif spec.has_location:
57 path = spec.origin
58 suffix = os.path.splitext(path)[1]
59 mode = 'r' if suffix in importlib.machinery.SOURCE_SUFFIXES else 'rb'
60
61 if suffix in importlib.machinery.SOURCE_SUFFIXES:
62 kind = PY_SOURCE
63 elif suffix in importlib.machinery.BYTECODE_SUFFIXES:
64 kind = PY_COMPILED
65 elif suffix in importlib.machinery.EXTENSION_SUFFIXES:
66 kind = C_EXTENSION
67
68 if kind in {PY_SOURCE, PY_COMPILED}:
69 file = open(path, mode)
70 else:
71 path = None
72 suffix = mode = ''
73
74 return file, path, (suffix, mode, kind)
75
76
77 def get_frozen_object(module, paths=None):
78 spec = find_spec(module, paths)
79 if not spec:
80 raise ImportError("Can't find %s" % module)
81 return spec.loader.get_code(module)
82
83
84 def get_module(module, paths, info):
85 spec = find_spec(module, paths)
86 if not spec:
87 raise ImportError("Can't find %s" % module)
88 return module_from_spec(spec)