blob: e3c9e510da0c688056227696ff17006b2fb22145 [file] [log] [blame]
Jeremy Hylton5e7cb242001-02-02 18:24:26 +00001#include "Python.h"
2
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003#include "code.h"
Jeremy Hylton5e7cb242001-02-02 18:24:26 +00004#include "compile.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "Python-ast.h"
Jeremy Hylton5e7cb242001-02-02 18:24:26 +00006#include "symtable.h"
7
8static PyObject *
9symtable_symtable(PyObject *self, PyObject *args)
10{
11 struct symtable *st;
12 PyObject *t;
13
14 char *str;
15 char *filename;
16 char *startstr;
17 int start;
18
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;
Nick Coghlan650f0d02007-04-15 12:05:43 +000036 t = st->st_blocks;
Raymond Hettinger8ae46892003-10-12 19:09:37 +000037 Py_INCREF(t);
Jeremy Hylton2554dd92001-12-06 14:34:58 +000038 PyMem_Free((void *)st->st_future);
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000039 PySymtable_Free(st);
40 return t;
41}
42
43static PyMethodDef symtable_methods[] = {
44 {"symtable", symtable_symtable, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +000045 PyDoc_STR("Return symbol and scope dictionaries"
46 " used internally by compiler.")},
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000047 {NULL, NULL} /* sentinel */
48};
49
Martin v. Löwis1a214512008-06-11 05:26:20 +000050static struct PyModuleDef symtablemodule = {
51 PyModuleDef_HEAD_INIT,
52 "_symtable",
53 NULL,
54 -1,
55 symtable_methods,
56 NULL,
57 NULL,
58 NULL,
59 NULL
60};
61
Mark Hammondfe51c6d2002-08-02 02:27:13 +000062PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +000063PyInit__symtable(void)
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000064{
65 PyObject *m;
66
Martin v. Löwis1a214512008-06-11 05:26:20 +000067 m = PyModule_Create(&symtablemodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +000068 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +000069 return NULL;
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000070 PyModule_AddIntConstant(m, "USE", USE);
71 PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
72 PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
73 PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM);
74 PyModule_AddIntConstant(m, "DEF_STAR", DEF_STAR);
75 PyModule_AddIntConstant(m, "DEF_DOUBLESTAR", DEF_DOUBLESTAR);
76 PyModule_AddIntConstant(m, "DEF_INTUPLE", DEF_INTUPLE);
77 PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE);
78 PyModule_AddIntConstant(m, "DEF_FREE_GLOBAL", DEF_FREE_GLOBAL);
79 PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS);
80 PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT);
Jeremy Hylton8bf395f2001-03-22 23:10:44 +000081 PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND);
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083 PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
84 PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
85 PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000086
Jeremy Hylton43454762001-04-16 18:42:13 +000087 PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR);
Georg Brandl7cae87c2006-09-06 06:51:57 +000088 PyModule_AddIntConstant(m, "OPT_TOPLEVEL", OPT_TOPLEVEL);
Jeremy Hylton43454762001-04-16 18:42:13 +000089
Jeremy Hylton5e7cb242001-02-02 18:24:26 +000090 PyModule_AddIntConstant(m, "LOCAL", LOCAL);
91 PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT);
92 PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT);
93 PyModule_AddIntConstant(m, "FREE", FREE);
94 PyModule_AddIntConstant(m, "CELL", CELL);
Martin v. Löwis1a214512008-06-11 05:26:20 +000095 if (PyErr_Occurred()) {
96 Py_DECREF(m);
97 m = 0;
98 }
99 return m;
Jeremy Hylton5e7cb242001-02-02 18:24:26 +0000100}