Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3 | #include "code.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4 | #include "Python-ast.h" |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 5 | #include "symtable.h" |
| 6 | |
| 7 | static PyObject * |
| 8 | symtable_symtable(PyObject *self, PyObject *args) |
| 9 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | struct symtable *st; |
| 11 | PyObject *t; |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 12 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 13 | char *str; |
| 14 | char *filename; |
| 15 | char *startstr; |
| 16 | int start; |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | if (!PyArg_ParseTuple(args, "sss:symtable", &str, &filename, |
| 19 | &startstr)) |
| 20 | return NULL; |
| 21 | if (strcmp(startstr, "exec") == 0) |
| 22 | start = Py_file_input; |
| 23 | else if (strcmp(startstr, "eval") == 0) |
| 24 | start = Py_eval_input; |
| 25 | else if (strcmp(startstr, "single") == 0) |
| 26 | start = Py_single_input; |
| 27 | else { |
| 28 | PyErr_SetString(PyExc_ValueError, |
| 29 | "symtable() arg 3 must be 'exec' or 'eval' or 'single'"); |
| 30 | return NULL; |
| 31 | } |
| 32 | st = Py_SymtableString(str, filename, start); |
| 33 | if (st == NULL) |
| 34 | return NULL; |
| 35 | t = st->st_blocks; |
| 36 | Py_INCREF(t); |
| 37 | PyMem_Free((void *)st->st_future); |
| 38 | PySymtable_Free(st); |
| 39 | return t; |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static PyMethodDef symtable_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | {"symtable", symtable_symtable, METH_VARARGS, |
| 44 | PyDoc_STR("Return symbol and scope dictionaries" |
| 45 | " used internally by compiler.")}, |
| 46 | {NULL, NULL} /* sentinel */ |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 49 | static struct PyModuleDef symtablemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | PyModuleDef_HEAD_INIT, |
| 51 | "_symtable", |
| 52 | NULL, |
| 53 | -1, |
| 54 | symtable_methods, |
| 55 | NULL, |
| 56 | NULL, |
| 57 | NULL, |
| 58 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 61 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 62 | PyInit__symtable(void) |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 63 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | PyObject *m; |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 65 | |
Benjamin Peterson | c431128 | 2012-10-30 23:21:10 -0400 | [diff] [blame] | 66 | if (PyType_Ready(&PySTEntry_Type) < 0) |
| 67 | return NULL; |
| 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | m = PyModule_Create(&symtablemodule); |
| 70 | if (m == NULL) |
| 71 | return NULL; |
| 72 | PyModule_AddIntConstant(m, "USE", USE); |
| 73 | PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL); |
| 74 | PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL); |
| 75 | PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM); |
| 76 | PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE); |
| 77 | PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS); |
| 78 | PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT); |
| 79 | PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND); |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock); |
| 82 | PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock); |
| 83 | PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock); |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR); |
| 86 | PyModule_AddIntConstant(m, "OPT_TOPLEVEL", OPT_TOPLEVEL); |
Jeremy Hylton | 4345476 | 2001-04-16 18:42:13 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | PyModule_AddIntConstant(m, "LOCAL", LOCAL); |
| 89 | PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT); |
| 90 | PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT); |
| 91 | PyModule_AddIntConstant(m, "FREE", FREE); |
| 92 | PyModule_AddIntConstant(m, "CELL", CELL); |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET); |
| 95 | PyModule_AddIntConstant(m, "SCOPE_MASK", SCOPE_MASK); |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (PyErr_Occurred()) { |
| 98 | Py_DECREF(m); |
| 99 | m = 0; |
| 100 | } |
| 101 | return m; |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 102 | } |