~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Tools/freeze/makefreeze.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import marshal
 
2
import bkfile
 
3
 
 
4
 
 
5
# Write a file containing frozen code for the modules in the dictionary.
 
6
 
 
7
header = """
 
8
#include "Python.h"
 
9
 
 
10
static struct _frozen _PyImport_FrozenModules[] = {
 
11
"""
 
12
trailer = """\
 
13
    {0, 0, 0} /* sentinel */
 
14
};
 
15
"""
 
16
 
 
17
# if __debug__ == 0 (i.e. -O option given), set Py_OptimizeFlag in frozen app.
 
18
default_entry_point = """
 
19
int
 
20
main(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
 
 
32
def 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
            outfp = bkfile.open(base + file, 'w')
 
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
            outfp.close()
 
54
    if debug:
 
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)
 
59
    outfp.write(header)
 
60
    for mod, mangled, size in done:
 
61
        outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
 
62
    outfp.write('\n')
 
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,))
 
69
    outfp.write(trailer)
 
70
    outfp.write(entry_point)
 
71
    outfp.close()
 
72
    return files
 
73
 
 
74
 
 
75
 
 
76
# Write a C initializer for a module containing the frozen python code.
 
77
# The array is called M_<mod>.
 
78
 
 
79
def writecode(outfp, mod, str):
 
80
    outfp.write('unsigned char M_%s[] = {' % mod)
 
81
    for i in range(0, len(str), 16):
 
82
        outfp.write('\n\t')
 
83
        for c in bytes(str[i:i+16]):
 
84
            outfp.write('%d,' % c)
 
85
    outfp.write('\n};\n')
 
86
 
 
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('"')))))