ยปCore Development>Code coverage>Python/makeopcodetargets.py

Python code coverage for Python/makeopcodetargets.py

#countcontent
1n/a#! /usr/bin/env python
2n/a"""Generate C code for the jump table of the threaded code interpreter
3n/a(for compilers supporting computed gotos or "labels-as-values", such as gcc).
4n/a"""
5n/a
6n/aimport os
7n/aimport sys
8n/a
9n/a
10n/atry:
11n/a from importlib.machinery import SourceFileLoader
12n/aexcept ImportError:
13n/a import imp
14n/a
15n/a def find_module(modname):
16n/a """Finds and returns a module in the local dist/checkout.
17n/a """
18n/a modpath = os.path.join(
19n/a os.path.dirname(os.path.dirname(__file__)), "Lib")
20n/a return imp.load_module(modname, *imp.find_module(modname, [modpath]))
21n/aelse:
22n/a def find_module(modname):
23n/a """Finds and returns a module in the local dist/checkout.
24n/a """
25n/a modpath = os.path.join(
26n/a os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
27n/a return SourceFileLoader(modname, modpath).load_module()
28n/a
29n/a
30n/adef write_contents(f):
31n/a """Write C code contents to the target file object.
32n/a """
33n/a opcode = find_module('opcode')
34n/a targets = ['_unknown_opcode'] * 256
35n/a for opname, op in opcode.opmap.items():
36n/a targets[op] = "TARGET_%s" % opname
37n/a f.write("static void *opcode_targets[256] = {\n")
38n/a f.write(",\n".join([" &&%s" % s for s in targets]))
39n/a f.write("\n};\n")
40n/a
41n/a
42n/adef main():
43n/a if len(sys.argv) >= 3:
44n/a sys.exit("Too many arguments")
45n/a if len(sys.argv) == 2:
46n/a target = sys.argv[1]
47n/a else:
48n/a target = "Python/opcode_targets.h"
49n/a with open(target, "w") as f:
50n/a write_contents(f)
51n/a print("Jump table written into %s" % target)
52n/a
53n/a
54n/aif __name__ == "__main__":
55n/a main()