Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e7cb24

Browse files
committed
Add minimal interface to symtable: _symtable module.
1 parent 4b38da6 commit 5e7cb24

4 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lib/test/output/test_symtable

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_symtable

Lib/test/test_symtable.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from test_support import verify
2+
3+
import _symtable
4+
5+
symbols, scopes = _symtable.symtable("def f(x): return x", "?", "exec")
6+
7+
verify(symbols.has_key(0))
8+
verify(scopes.has_key(0))

Modules/symtablemodule.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
34+
t = Py_BuildValue("OO", st->st_symbols, st->st_scopes);
35+
PySymtable_Free(st);
36+
return t;
37+
}
38+
39+
static PyMethodDef symtable_methods[] = {
40+
{"symtable", symtable_symtable, METH_VARARGS,
41+
"Return symbol and scope dictionaries used internally by compiler."},
42+
{NULL, NULL} /* sentinel */
43+
};
44+
45+
DL_EXPORT(void)
46+
init_symtable(void)
47+
{
48+
PyObject *m;
49+
50+
m = Py_InitModule("_symtable", symtable_methods);
51+
PyModule_AddIntConstant(m, "USE", USE);
52+
PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
53+
PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
54+
PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM);
55+
PyModule_AddIntConstant(m, "DEF_STAR", DEF_STAR);
56+
PyModule_AddIntConstant(m, "DEF_DOUBLESTAR", DEF_DOUBLESTAR);
57+
PyModule_AddIntConstant(m, "DEF_INTUPLE", DEF_INTUPLE);
58+
PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE);
59+
PyModule_AddIntConstant(m, "DEF_FREE_GLOBAL", DEF_FREE_GLOBAL);
60+
PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS);
61+
PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT);
62+
63+
PyModule_AddIntConstant(m, "TYPE_FUNCTION", TYPE_FUNCTION);
64+
PyModule_AddIntConstant(m, "TYPE_CLASS", TYPE_CLASS);
65+
PyModule_AddIntConstant(m, "TYPE_MODULE", TYPE_MODULE);
66+
67+
PyModule_AddIntConstant(m, "LOCAL", LOCAL);
68+
PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT);
69+
PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT);
70+
PyModule_AddIntConstant(m, "FREE", FREE);
71+
PyModule_AddIntConstant(m, "CELL", CELL);
72+
}

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def detect_modules(self):
154154
exts.append( Extension('pcre', ['pcremodule.c', 'pypcre.c']) )
155155

156156
exts.append( Extension('_weakref', ['_weakref.c']) )
157+
exts.append( Extension('_symtable', ['symtablemodule.c']) )
157158
exts.append( Extension('xreadlines', ['xreadlinesmodule.c']) )
158159

159160
# array objects

0 commit comments

Comments
 (0)