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