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