Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
| 3 | #include "compile.h" |
| 4 | #include "symtable.h" |
| 5 | |
| 6 | static PyObject * |
| 7 | symtable_symtable(PyObject *self, PyObject *args) |
| 8 | { |
| 9 | struct symtable *st; |
| 10 | PyObject *t; |
| 11 | |
| 12 | char *str; |
| 13 | char *filename; |
| 14 | char *startstr; |
| 15 | int start; |
| 16 | |
| 17 | if (!PyArg_ParseTuple(args, "sss:symtable", &str, &filename, |
| 18 | &startstr)) |
| 19 | return NULL; |
| 20 | if (strcmp(startstr, "exec") == 0) |
| 21 | start = Py_file_input; |
| 22 | else if (strcmp(startstr, "eval") == 0) |
| 23 | start = Py_eval_input; |
| 24 | else if (strcmp(startstr, "single") == 0) |
| 25 | start = Py_single_input; |
| 26 | else { |
| 27 | PyErr_SetString(PyExc_ValueError, |
| 28 | "symtable() arg 3 must be 'exec' or 'eval' or 'single'"); |
| 29 | return NULL; |
| 30 | } |
| 31 | st = Py_SymtableString(str, filename, start); |
| 32 | if (st == NULL) |
| 33 | return NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 34 | t = Py_BuildValue("O", st->st_symbols); |
Jeremy Hylton | 2554dd9 | 2001-12-06 14:34:58 +0000 | [diff] [blame] | 35 | PyMem_Free((void *)st->st_future); |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 36 | PySymtable_Free(st); |
| 37 | return t; |
| 38 | } |
| 39 | |
| 40 | static PyMethodDef symtable_methods[] = { |
| 41 | {"symtable", symtable_symtable, METH_VARARGS, |
Neal Norwitz | 200788c | 2002-08-13 22:20:41 +0000 | [diff] [blame] | 42 | PyDoc_STR("Return symbol and scope dictionaries" |
| 43 | " used internally by compiler.")}, |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 44 | {NULL, NULL} /* sentinel */ |
| 45 | }; |
| 46 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 47 | PyMODINIT_FUNC |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 48 | init_symtable(void) |
| 49 | { |
| 50 | PyObject *m; |
| 51 | |
| 52 | m = Py_InitModule("_symtable", symtable_methods); |
| 53 | PyModule_AddIntConstant(m, "USE", USE); |
| 54 | PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL); |
| 55 | PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL); |
| 56 | PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM); |
| 57 | PyModule_AddIntConstant(m, "DEF_STAR", DEF_STAR); |
| 58 | PyModule_AddIntConstant(m, "DEF_DOUBLESTAR", DEF_DOUBLESTAR); |
| 59 | PyModule_AddIntConstant(m, "DEF_INTUPLE", DEF_INTUPLE); |
| 60 | PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE); |
| 61 | PyModule_AddIntConstant(m, "DEF_FREE_GLOBAL", DEF_FREE_GLOBAL); |
| 62 | PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS); |
| 63 | PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT); |
Jeremy Hylton | 8bf395f | 2001-03-22 23:10:44 +0000 | [diff] [blame] | 64 | PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND); |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 65 | |
| 66 | PyModule_AddIntConstant(m, "TYPE_FUNCTION", TYPE_FUNCTION); |
| 67 | PyModule_AddIntConstant(m, "TYPE_CLASS", TYPE_CLASS); |
| 68 | PyModule_AddIntConstant(m, "TYPE_MODULE", TYPE_MODULE); |
| 69 | |
Jeremy Hylton | 4345476 | 2001-04-16 18:42:13 +0000 | [diff] [blame] | 70 | PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR); |
| 71 | PyModule_AddIntConstant(m, "OPT_EXEC", OPT_EXEC); |
| 72 | PyModule_AddIntConstant(m, "OPT_BARE_EXEC", OPT_BARE_EXEC); |
| 73 | |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 74 | PyModule_AddIntConstant(m, "LOCAL", LOCAL); |
| 75 | PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT); |
| 76 | PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT); |
| 77 | PyModule_AddIntConstant(m, "FREE", FREE); |
| 78 | PyModule_AddIntConstant(m, "CELL", CELL); |
| 79 | } |