• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *
_symtable_symtable_impl(PyObject * module,PyObject * source,PyObject * filename,const char * startstr)24 _symtable_symtable_impl(PyObject *module, PyObject *source,
25                         PyObject *filename, const char *startstr)
26 /*[clinic end generated code: output=59eb0d5fc7285ac4 input=9dd8a50c0c36a4d7]*/
27 {
28     struct symtable *st;
29     PyObject *t;
30     int start;
31     PyCompilerFlags cf = _PyCompilerFlags_INIT;
32     PyObject *source_copy = NULL;
33 
34     cf.cf_flags = PyCF_SOURCE_IS_UTF8;
35 
36     const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
37     if (str == NULL) {
38         return NULL;
39     }
40 
41     if (strcmp(startstr, "exec") == 0)
42         start = Py_file_input;
43     else if (strcmp(startstr, "eval") == 0)
44         start = Py_eval_input;
45     else if (strcmp(startstr, "single") == 0)
46         start = Py_single_input;
47     else {
48         PyErr_SetString(PyExc_ValueError,
49            "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
50         Py_DECREF(filename);
51         Py_XDECREF(source_copy);
52         return NULL;
53     }
54     st = _Py_SymtableStringObjectFlags(str, filename, start, &cf);
55     Py_DECREF(filename);
56     Py_XDECREF(source_copy);
57     if (st == NULL) {
58         return NULL;
59     }
60     t = Py_NewRef(st->st_top);
61     _PySymtable_Free(st);
62     return t;
63 }
64 
65 static PyMethodDef symtable_methods[] = {
66     _SYMTABLE_SYMTABLE_METHODDEF
67     {NULL,              NULL}           /* sentinel */
68 };
69 
70 static int
symtable_init_constants(PyObject * m)71 symtable_init_constants(PyObject *m)
72 {
73     if (PyModule_AddIntMacro(m, USE) < 0) return -1;
74     if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1;
75     if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
76     if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
77     if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
78     if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
79     if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
80     if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
81     if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1;
82     if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1;
83 
84     if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
85         return -1;
86     if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
87     if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
88         return -1;
89     if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)
90         return -1;
91     if (PyModule_AddIntConstant(m, "TYPE_TYPE_ALIAS", TypeAliasBlock) < 0)
92         return -1;
93     if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAMETERS", TypeParametersBlock) < 0)
94         return -1;
95     if (PyModule_AddIntConstant(m, "TYPE_TYPE_VARIABLE", TypeVariableBlock) < 0)
96         return -1;
97 
98     if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
99     if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1;
100     if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1;
101     if (PyModule_AddIntMacro(m, FREE) < 0) return -1;
102     if (PyModule_AddIntMacro(m, CELL) < 0) return -1;
103 
104     if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1;
105     if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1;
106 
107     return 0;
108 }
109 
110 static PyModuleDef_Slot symtable_slots[] = {
111     {Py_mod_exec, symtable_init_constants},
112     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
113     {Py_mod_gil, Py_MOD_GIL_NOT_USED},
114     {0, NULL}
115 };
116 
117 static struct PyModuleDef symtablemodule = {
118     PyModuleDef_HEAD_INIT,
119     .m_name = "_symtable",
120     .m_size = 0,
121     .m_methods = symtable_methods,
122     .m_slots = symtable_slots,
123 };
124 
125 PyMODINIT_FUNC
PyInit__symtable(void)126 PyInit__symtable(void)
127 {
128     return PyModuleDef_Init(&symtablemodule);
129 }
130