5
# Write a file containing frozen code for the modules in the dictionary.
10
static struct _frozen _PyImport_FrozenModules[] = {
13
{0, 0, 0} /* sentinel */
17
# if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app.
18
default_entry_point = """
20
main(int argc, char **argv)
22
extern int Py_FrozenMain(int, char **);
23
""" + ((not __debug__ and """
26
PyImport_FrozenModules = _PyImport_FrozenModules;
27
return Py_FrozenMain(argc, argv);
32
def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
33
if entry_point is None: entry_point = default_entry_point
36
mods = sorted(dict.keys())
39
mangled = "__".join(mod.split("."))
41
file = 'M_' + mangled + '.c'
42
outfp = bkfile.open(base + file, 'w')
45
print("freezing", mod, "...")
46
str = marshal.dumps(m.__code__)
49
# Indicate package by negative size
51
done.append((mod, mangled, size))
52
writecode(outfp, mangled, str)
55
print("generating table of frozen modules")
56
outfp = bkfile.open(base + 'frozen.c', 'w')
57
for mod, mangled, size in done:
58
outfp.write('extern unsigned char M_%s[];\n' % mangled)
60
for mod, mangled, size in done:
61
outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
63
# The following modules have a NULL code pointer, indicating
64
# that the frozen program should not search for them on the host
65
# system. Importing them will *always* raise an ImportError.
66
# The zero value size is never used.
67
for mod in fail_import:
68
outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
70
outfp.write(entry_point)
76
# Write a C initializer for a module containing the frozen python code.
77
# The array is called M_<mod>.
79
def writecode(outfp, mod, str):
80
outfp.write('unsigned char M_%s[] = {' % mod)
81
for i in range(0, len(str), 16):
83
for c in bytes(str[i:i+16]):
84
outfp.write('%d,' % c)
87
## def writecode(outfp, mod, str):
88
## outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str),
89
## '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"')))))