-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.py
96 lines (73 loc) · 3.28 KB
/
bootstrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
import marshal
import _imp
import _frozen_importlib_external as _bootstrap_external
import _frozen_importlib as _bootstrap
from onefile_python import pyzip_has_pyd, pyzip_load_pyd, pyzip_stdlib_has, pyzip_stdlib_read
_bootstrap._install_external_importers()
_bootstrap_external._set_bootstrap_module(_bootstrap)
DEBUG = globals().get('DEBUG', False)
def debug(s: str):
if DEBUG:
print(s, file=sys.stderr)
class InMemoryExtensionLoader:
def __init__(self, filename: str):
self.filename = filename
def create_module(self, spec):
debug(f'bootstrap.py InMemoryExtensionLoader.load_module({self.filename=!r}, {spec=!r})')
module = pyzip_load_pyd(self.filename, spec.name, spec)
debug(f'bootstrap.py {module=}')
return module
def exec_module(self, module):
_imp.exec_dynamic(module)
class InMemoryBytecodeLoader(_bootstrap_external._LoaderBasics):
def __init__(self, filename: str):
self.filename = filename
def get_code(self, fullname):
debug(f'bootstrap.py InMemoryBytecodeLoader.get_code({self.filename=!r}, {fullname=!r})')
pyc = pyzip_stdlib_read(self.filename)
code = marshal.loads(pyc[16:])
debug(f' {code=}')
return code
class InMemoryFinder:
def find_spec(self, fullname, path, target=None):
debug(f'bootstrap.py InMemoryFinder.find_spec(fullname={fullname!r}, path={path!r}, target={target!r})')
loader = None
submodule_search_locations = None
if '.' in fullname:
package_name, module_name = fullname.split('.', 1)
else:
package_name = fullname
module_name = ''
module_path = module_name.replace('.', '/')
pyd = package_name + '.pyd'
if module_path:
init = '/'.join([package_name, module_path, '__init__.pyc'])
else:
init = '/'.join([package_name, '__init__.pyc'])
module = '/'.join([package_name, module_path + '.pyc'])
single_file = package_name + '.pyc'
debug(f' checking {[pyd, init, module, single_file]}')
if pyzip_has_pyd(pyd):
debug(f' found pyd: {pyd!r} in pyzip')
loader = InMemoryExtensionLoader(pyd)
elif pyzip_stdlib_has(init):
debug(f' found init: {init!r} in stdlib (setting submodule_search_locations)')
loader = InMemoryBytecodeLoader(init)
submodule_search_locations = [f'<{fullname}>']
elif pyzip_stdlib_has(module):
debug(f' found module: {module!r} in stdlib')
loader = InMemoryBytecodeLoader(module)
elif pyzip_stdlib_has(single_file):
debug(f' found single file: {single_file!r} in stdlib')
loader = InMemoryBytecodeLoader(single_file)
if loader:
return _bootstrap_external.spec_from_file_location(
fullname,
loader=loader,
submodule_search_locations=submodule_search_locations
)
else:
debug(f' not found')
sys.meta_path.insert(1, InMemoryFinder())
debug(sys.meta_path)