/src/cpython/Modules/symtablemodule.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "pycore_pythonrun.h" // _Py_SourceAsString() |
3 | | #include "pycore_symtable.h" // struct symtable |
4 | | |
5 | | #include "clinic/symtablemodule.c.h" |
6 | | /*[clinic input] |
7 | | module _symtable |
8 | | [clinic start generated code]*/ |
9 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f4685845a7100605]*/ |
10 | | |
11 | | |
12 | | /*[clinic input] |
13 | | _symtable.symtable |
14 | | |
15 | | source: object |
16 | | filename: object(converter='PyUnicode_FSDecoder') |
17 | | startstr: str |
18 | | / |
19 | | |
20 | | Return symbol and scope dictionaries used internally by compiler. |
21 | | [clinic start generated code]*/ |
22 | | |
23 | | static PyObject * |
24 | | _symtable_symtable_impl(PyObject *module, PyObject *source, |
25 | | PyObject *filename, const char *startstr) |
26 | | /*[clinic end generated code: output=59eb0d5fc7285ac4 input=9dd8a50c0c36a4d7]*/ |
27 | 0 | { |
28 | 0 | struct symtable *st; |
29 | 0 | PyObject *t; |
30 | 0 | int start; |
31 | 0 | PyCompilerFlags cf = _PyCompilerFlags_INIT; |
32 | 0 | PyObject *source_copy = NULL; |
33 | |
|
34 | 0 | cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
35 | |
|
36 | 0 | const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy); |
37 | 0 | if (str == NULL) { |
38 | 0 | return NULL; |
39 | 0 | } |
40 | | |
41 | 0 | if (strcmp(startstr, "exec") == 0) |
42 | 0 | start = Py_file_input; |
43 | 0 | else if (strcmp(startstr, "eval") == 0) |
44 | 0 | start = Py_eval_input; |
45 | 0 | else if (strcmp(startstr, "single") == 0) |
46 | 0 | start = Py_single_input; |
47 | 0 | else { |
48 | 0 | PyErr_SetString(PyExc_ValueError, |
49 | 0 | "symtable() arg 3 must be 'exec' or 'eval' or 'single'"); |
50 | 0 | Py_DECREF(filename); |
51 | 0 | Py_XDECREF(source_copy); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | 0 | st = _Py_SymtableStringObjectFlags(str, filename, start, &cf); |
55 | 0 | Py_DECREF(filename); |
56 | 0 | Py_XDECREF(source_copy); |
57 | 0 | if (st == NULL) { |
58 | 0 | return NULL; |
59 | 0 | } |
60 | 0 | t = Py_NewRef(st->st_top); |
61 | 0 | _PySymtable_Free(st); |
62 | 0 | return t; |
63 | 0 | } |
64 | | |
65 | | static PyMethodDef symtable_methods[] = { |
66 | | _SYMTABLE_SYMTABLE_METHODDEF |
67 | | {NULL, NULL} /* sentinel */ |
68 | | }; |
69 | | |
70 | | static int |
71 | | symtable_init_constants(PyObject *m) |
72 | 0 | { |
73 | 0 | if (PyModule_AddIntMacro(m, USE) < 0) return -1; |
74 | 0 | if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1; |
75 | 0 | if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1; |
76 | 0 | if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1; |
77 | 0 | if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1; |
78 | 0 | if (PyModule_AddIntMacro(m, DEF_TYPE_PARAM) < 0) return -1; |
79 | 0 | if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1; |
80 | 0 | if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1; |
81 | 0 | if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1; |
82 | 0 | if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1; |
83 | 0 | if (PyModule_AddIntMacro(m, DEF_COMP_ITER) < 0) return -1; |
84 | 0 | if (PyModule_AddIntMacro(m, DEF_COMP_CELL) < 0) return -1; |
85 | | |
86 | 0 | if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0) |
87 | 0 | return -1; |
88 | 0 | if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) |
89 | 0 | return -1; |
90 | 0 | if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0) |
91 | 0 | return -1; |
92 | 0 | if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0) |
93 | 0 | return -1; |
94 | 0 | if (PyModule_AddIntConstant(m, "TYPE_TYPE_ALIAS", TypeAliasBlock) < 0) |
95 | 0 | return -1; |
96 | 0 | if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAMETERS", TypeParametersBlock) < 0) |
97 | 0 | return -1; |
98 | 0 | if (PyModule_AddIntConstant(m, "TYPE_TYPE_VARIABLE", TypeVariableBlock) < 0) |
99 | 0 | return -1; |
100 | | |
101 | 0 | if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1; |
102 | 0 | if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1; |
103 | 0 | if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1; |
104 | 0 | if (PyModule_AddIntMacro(m, FREE) < 0) return -1; |
105 | 0 | if (PyModule_AddIntMacro(m, CELL) < 0) return -1; |
106 | | |
107 | 0 | if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1; |
108 | 0 | if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1; |
109 | | |
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | | static PyModuleDef_Slot symtable_slots[] = { |
114 | | {Py_mod_exec, symtable_init_constants}, |
115 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
116 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
117 | | {0, NULL} |
118 | | }; |
119 | | |
120 | | static struct PyModuleDef symtablemodule = { |
121 | | PyModuleDef_HEAD_INIT, |
122 | | .m_name = "_symtable", |
123 | | .m_size = 0, |
124 | | .m_methods = symtable_methods, |
125 | | .m_slots = symtable_slots, |
126 | | }; |
127 | | |
128 | | PyMODINIT_FUNC |
129 | | PyInit__symtable(void) |
130 | 0 | { |
131 | 0 | return PyModuleDef_Init(&symtablemodule); |
132 | 0 | } |