PageRenderTime 464ms CodeModel.GetById 241ms app.highlight 5ms RepoModel.GetById 217ms app.codeStats 0ms

/Modules/make_cpickle_opcodes.py

http://unladen-swallow.googlecode.com/
Python | 41 lines | 39 code | 0 blank | 2 comment | 0 complexity | 876f017c163edf4c2f89a98b697058dd MD5 | raw file
 1#! /usr/bin/env python
 2"""Generate C code for the jump table of the cPickle unpickling interpreter
 3(for compilers supporting computed gotos or "labels-as-values", such as gcc).
 4
 5This needs to be run when you change the pickle protocol, which you shouldn't
 6be doing.
 7"""
 8
 9import imp
10import os
11import sys
12
13
14def find_module(modname):
15    """Finds and returns a module in the local dist/checkout."""
16    modpath = os.path.join(
17        os.path.dirname(os.path.dirname(__file__)), "Lib")
18    return imp.load_module(modname, *imp.find_module(modname, [modpath]))
19
20def write_contents(f):
21    """Write C code contents to the target file object."""
22    pickletools = find_module("pickletools")
23    targets = ['TARGET_NULLBYTE'] + (['_unknown_opcode'] * 255)
24    for opcode in pickletools.opcodes:
25        targets[ord(opcode.code)] = "TARGET_%s" % opcode.name
26    f.write("static void *opcode_targets[256] = {\n")
27    f.write(",\n".join("\t&&%s" % s for s in targets))
28    f.write("\n};\n")
29
30
31if __name__ == "__main__":
32    assert len(sys.argv) < 3, "Too many arguments"
33    if len(sys.argv) == 2:
34        target = sys.argv[1]
35    else:
36        target = "Modules/cPickle_opcodes.h"
37    f = open(target, "w")
38    try:
39        write_contents(f)
40    finally:
41        f.close()