ยปCore Development>Code coverage>Tools/scripts/generate_opcode_h.py

Python code coverage for Tools/scripts/generate_opcode_h.py

#countcontent
1n/a# This script generates the opcode.h header file.
2n/a
3n/aimport sys
4n/aimport tokenize
5n/a
6n/aheader = """/* Auto-generated by Tools/scripts/generate_opcode_h.py */
7n/a#ifndef Py_OPCODE_H
8n/a#define Py_OPCODE_H
9n/a#ifdef __cplusplus
10n/aextern "C" {
11n/a#endif
12n/a
13n/a
14n/a /* Instruction opcodes for compiled code */
15n/a"""
16n/a
17n/afooter = """
18n/a/* EXCEPT_HANDLER is a special, implicit block type which is created when
19n/a entering an except handler. It is not an opcode but we define it here
20n/a as we want it to be available to both frameobject.c and ceval.c, while
21n/a remaining private.*/
22n/a#define EXCEPT_HANDLER 257
23n/a
24n/a
25n/aenum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE,
26n/a PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, PyCmp_IN, PyCmp_NOT_IN,
27n/a PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
28n/a
29n/a#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
30n/a
31n/a#ifdef __cplusplus
32n/a}
33n/a#endif
34n/a#endif /* !Py_OPCODE_H */
35n/a"""
36n/a
37n/a
38n/adef main(opcode_py, outfile='Include/opcode.h'):
39n/a opcode = {}
40n/a if hasattr(tokenize, 'open'):
41n/a fp = tokenize.open(opcode_py) # Python 3.2+
42n/a else:
43n/a fp = open(opcode_py) # Python 2.7
44n/a with fp:
45n/a code = fp.read()
46n/a exec(code, opcode)
47n/a opmap = opcode['opmap']
48n/a with open(outfile, 'w') as fobj:
49n/a fobj.write(header)
50n/a for name in opcode['opname']:
51n/a if name in opmap:
52n/a fobj.write("#define %-23s %3s\n" % (name, opmap[name]))
53n/a if name == 'POP_EXCEPT': # Special entry for HAVE_ARGUMENT
54n/a fobj.write("#define %-23s %3d\n" %
55n/a ('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT']))
56n/a fobj.write(footer)
57n/a
58n/a print("%s regenerated from %s" % (outfile, opcode_py))
59n/a
60n/a
61n/aif __name__ == '__main__':
62n/a main(sys.argv[1], sys.argv[2])