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