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