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 15 : _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 15 : PyCompilerFlags cf = _PyCompilerFlags_INIT;
31 15 : PyObject *source_copy = NULL;
32 :
33 15 : cf.cf_flags = PyCF_SOURCE_IS_UTF8;
34 :
35 15 : const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
36 15 : if (str == NULL) {
37 0 : return NULL;
38 : }
39 :
40 15 : if (strcmp(startstr, "exec") == 0)
41 13 : start = Py_file_input;
42 2 : else if (strcmp(startstr, "eval") == 0)
43 1 : start = Py_eval_input;
44 1 : else if (strcmp(startstr, "single") == 0)
45 1 : 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 15 : st = _Py_SymtableStringObjectFlags(str, filename, start, &cf);
54 15 : Py_DECREF(filename);
55 15 : Py_XDECREF(source_copy);
56 15 : if (st == NULL) {
57 2 : return NULL;
58 : }
59 13 : t = (PyObject *)st->st_top;
60 13 : Py_INCREF(t);
61 13 : _PySymtable_Free(st);
62 13 : return t;
63 : }
64 :
65 : static PyMethodDef symtable_methods[] = {
66 : _SYMTABLE_SYMTABLE_METHODDEF
67 : {NULL, NULL} /* sentinel */
68 : };
69 :
70 : static int
71 3 : symtable_init_stentry_type(PyObject *m)
72 : {
73 3 : return PyType_Ready(&PySTEntry_Type);
74 : }
75 :
76 : static int
77 3 : symtable_init_constants(PyObject *m)
78 : {
79 3 : if (PyModule_AddIntMacro(m, USE) < 0) return -1;
80 3 : if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1;
81 3 : if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
82 3 : if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
83 3 : if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
84 3 : if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
85 3 : if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
86 3 : if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
87 3 : if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1;
88 3 : if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1;
89 :
90 3 : if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
91 0 : return -1;
92 3 : if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
93 3 : if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
94 0 : return -1;
95 :
96 3 : if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
97 3 : if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1;
98 3 : if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1;
99 3 : if (PyModule_AddIntMacro(m, FREE) < 0) return -1;
100 3 : if (PyModule_AddIntMacro(m, CELL) < 0) return -1;
101 :
102 3 : if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1;
103 3 : if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1;
104 :
105 3 : return 0;
106 : }
107 :
108 : static PyModuleDef_Slot symtable_slots[] = {
109 : {Py_mod_exec, symtable_init_stentry_type},
110 : {Py_mod_exec, symtable_init_constants},
111 : {0, NULL}
112 : };
113 :
114 : static struct PyModuleDef symtablemodule = {
115 : PyModuleDef_HEAD_INIT,
116 : .m_name = "_symtable",
117 : .m_size = 0,
118 : .m_methods = symtable_methods,
119 : .m_slots = symtable_slots,
120 : };
121 :
122 : PyMODINIT_FUNC
123 3 : PyInit__symtable(void)
124 : {
125 3 : return PyModuleDef_Init(&symtablemodule);
126 : }
|