1 | #include "Python.h" |
2 | #include "opcode.h" |
3 | |
4 | /*[clinic input] |
5 | module _opcode |
6 | [clinic start generated code]*/ |
7 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/ |
8 | |
9 | #include "clinic/_opcode.c.h" |
10 | |
11 | /*[clinic input] |
12 | |
13 | _opcode.stack_effect -> int |
14 | |
15 | opcode: int |
16 | oparg: object = None |
17 | / |
18 | * |
19 | jump: object = None |
20 | |
21 | Compute the stack effect of the opcode. |
22 | [clinic start generated code]*/ |
23 | |
24 | static int |
25 | _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg, |
26 | PyObject *jump) |
27 | /*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/ |
28 | { |
29 | int effect; |
30 | int oparg_int = 0; |
31 | int jump_int; |
32 | if (HAS_ARG(opcode)) { |
33 | if (oparg == Py_None) { |
34 | PyErr_SetString(PyExc_ValueError, |
35 | "stack_effect: opcode requires oparg but oparg was not specified" ); |
36 | return -1; |
37 | } |
38 | oparg_int = (int)PyLong_AsLong(oparg); |
39 | if ((oparg_int == -1) && PyErr_Occurred()) { |
40 | return -1; |
41 | } |
42 | } |
43 | else if (oparg != Py_None) { |
44 | PyErr_SetString(PyExc_ValueError, |
45 | "stack_effect: opcode does not permit oparg but oparg was specified" ); |
46 | return -1; |
47 | } |
48 | if (jump == Py_None) { |
49 | jump_int = -1; |
50 | } |
51 | else if (jump == Py_True) { |
52 | jump_int = 1; |
53 | } |
54 | else if (jump == Py_False) { |
55 | jump_int = 0; |
56 | } |
57 | else { |
58 | PyErr_SetString(PyExc_ValueError, |
59 | "stack_effect: jump must be False, True or None" ); |
60 | return -1; |
61 | } |
62 | effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int); |
63 | if (effect == PY_INVALID_STACK_EFFECT) { |
64 | PyErr_SetString(PyExc_ValueError, |
65 | "invalid opcode or oparg" ); |
66 | return -1; |
67 | } |
68 | return effect; |
69 | } |
70 | |
71 | static PyMethodDef |
72 | opcode_functions[] = { |
73 | _OPCODE_STACK_EFFECT_METHODDEF |
74 | {NULL, NULL, 0, NULL} |
75 | }; |
76 | |
77 | static struct PyModuleDef opcodemodule = { |
78 | PyModuleDef_HEAD_INIT, |
79 | .m_name = "_opcode" , |
80 | .m_doc = "Opcode support module." , |
81 | .m_size = 0, |
82 | .m_methods = opcode_functions |
83 | }; |
84 | |
85 | PyMODINIT_FUNC |
86 | PyInit__opcode(void) |
87 | { |
88 | return PyModuleDef_Init(&opcodemodule); |
89 | } |
90 | |