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     str:       str
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 *
_symtable_symtable_impl(PyObject * module,const char * str,PyObject * filename,const char * startstr)26 _symtable_symtable_impl(PyObject *module, const char *str,
27                         PyObject *filename, const char *startstr)
28 /*[clinic end generated code: output=914b369c9b785956 input=6c615e84d5f408e3]*/
29 {
30     struct symtable *st;
31     PyObject *t;
32     int start;
33 
34     if (strcmp(startstr, "exec") == 0)
35         start = Py_file_input;
36     else if (strcmp(startstr, "eval") == 0)
37         start = Py_eval_input;
38     else if (strcmp(startstr, "single") == 0)
39         start = Py_single_input;
40     else {
41         PyErr_SetString(PyExc_ValueError,
42            "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
43         Py_DECREF(filename);
44         return NULL;
45     }
46     st = Py_SymtableStringObject(str, filename, start);
47     Py_DECREF(filename);
48     if (st == NULL)
49         return NULL;
50     t = (PyObject *)st->st_top;
51     Py_INCREF(t);
52     PyMem_Free((void *)st->st_future);
53     PySymtable_Free(st);
54     return t;
55 }
56 
57 static PyMethodDef symtable_methods[] = {
58     _SYMTABLE_SYMTABLE_METHODDEF
59     {NULL,              NULL}           /* sentinel */
60 };
61 
62 static struct PyModuleDef symtablemodule = {
63     PyModuleDef_HEAD_INIT,
64     "_symtable",
65     NULL,
66     -1,
67     symtable_methods,
68     NULL,
69     NULL,
70     NULL,
71     NULL
72 };
73 
74 PyMODINIT_FUNC
PyInit__symtable(void)75 PyInit__symtable(void)
76 {
77     PyObject *m;
78 
79     if (PyType_Ready(&PySTEntry_Type) < 0)
80         return NULL;
81 
82     m = PyModule_Create(&symtablemodule);
83     if (m == NULL)
84         return NULL;
85     PyModule_AddIntMacro(m, USE);
86     PyModule_AddIntMacro(m, DEF_GLOBAL);
87     PyModule_AddIntMacro(m, DEF_LOCAL);
88     PyModule_AddIntMacro(m, DEF_PARAM);
89     PyModule_AddIntMacro(m, DEF_FREE);
90     PyModule_AddIntMacro(m, DEF_FREE_CLASS);
91     PyModule_AddIntMacro(m, DEF_IMPORT);
92     PyModule_AddIntMacro(m, DEF_BOUND);
93     PyModule_AddIntMacro(m, DEF_ANNOT);
94 
95     PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
96     PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
97     PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
98 
99     PyModule_AddIntMacro(m, LOCAL);
100     PyModule_AddIntMacro(m, GLOBAL_EXPLICIT);
101     PyModule_AddIntMacro(m, GLOBAL_IMPLICIT);
102     PyModule_AddIntMacro(m, FREE);
103     PyModule_AddIntMacro(m, CELL);
104 
105     PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
106     PyModule_AddIntMacro(m, SCOPE_MASK);
107 
108     if (PyErr_Occurred()) {
109         Py_DECREF(m);
110         m = 0;
111     }
112     return m;
113 }
114