1import marshal 2import bkfile 3 4 5# Write a file containing frozen code for the modules in the dictionary. 6 7header = """ 8#include "Python.h" 9 10static struct _frozen _PyImport_FrozenModules[] = { 11""" 12trailer = """\ 13 {0, 0, 0} /* sentinel */ 14}; 15""" 16 17# if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app. 18default_entry_point = """ 19int 20main(int argc, char **argv) 21{ 22 extern int Py_FrozenMain(int, char **); 23""" + ((not __debug__ and """ 24 Py_OptimizeFlag++; 25""") or "") + """ 26 PyImport_FrozenModules = _PyImport_FrozenModules; 27 return Py_FrozenMain(argc, argv); 28} 29 30""" 31 32def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()): 33 if entry_point is None: entry_point = default_entry_point 34 done = [] 35 files = [] 36 mods = sorted(dict.keys()) 37 for mod in mods: 38 m = dict[mod] 39 mangled = "__".join(mod.split(".")) 40 if m.__code__: 41 file = 'M_' + mangled + '.c' 42 with bkfile.open(base + file, 'w') as outfp: 43 files.append(file) 44 if debug: 45 print("freezing", mod, "...") 46 str = marshal.dumps(m.__code__) 47 size = len(str) 48 if m.__path__: 49 # Indicate package by negative size 50 size = -size 51 done.append((mod, mangled, size)) 52 writecode(outfp, mangled, str) 53 if debug: 54 print("generating table of frozen modules") 55 with bkfile.open(base + 'frozen.c', 'w') as outfp: 56 for mod, mangled, size in done: 57 outfp.write('extern unsigned char M_%s[];\n' % mangled) 58 outfp.write(header) 59 for mod, mangled, size in done: 60 outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) 61 outfp.write('\n') 62 # The following modules have a NULL code pointer, indicating 63 # that the frozen program should not search for them on the host 64 # system. Importing them will *always* raise an ImportError. 65 # The zero value size is never used. 66 for mod in fail_import: 67 outfp.write('\t{"%s", NULL, 0},\n' % (mod,)) 68 outfp.write(trailer) 69 outfp.write(entry_point) 70 return files 71 72 73 74# Write a C initializer for a module containing the frozen python code. 75# The array is called M_<mod>. 76 77def writecode(outfp, mod, str): 78 outfp.write('unsigned char M_%s[] = {' % mod) 79 for i in range(0, len(str), 16): 80 outfp.write('\n\t') 81 for c in bytes(str[i:i+16]): 82 outfp.write('%d,' % c) 83 outfp.write('\n};\n') 84 85## def writecode(outfp, mod, str): 86## outfp.write('unsigned char M_%s[%d] = "%s";\n' % (mod, len(str), 87## '\\"'.join(map(lambda s: repr(s)[1:-1], str.split('"'))))) 88