1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include "Python.h"
#include "symtable.h"
#include "graminit.h"
#include "structmember.h"
PyObject *
PySymtableEntry_New(struct symtable *st, char *name, int type, int lineno)
{
PySymtableEntryObject *ste = NULL;
PyObject *k, *v;
k = PyInt_FromLong(st->st_nscopes++);
if (k == NULL)
goto fail;
v = PyDict_GetItem(st->st_symbols, k);
if (v) /* XXX could check that name, type, lineno match */
return v;
ste = (PySymtableEntryObject *)PyObject_New(PySymtableEntryObject,
&PySymtableEntry_Type);
ste->ste_table = st;
ste->ste_id = k;
v = PyString_FromString(name);
if (v == NULL)
goto fail;
ste->ste_name = v;
v = PyDict_New();
if (v == NULL)
goto fail;
ste->ste_symbols = v;
v = PyList_New(0);
if (v == NULL)
goto fail;
ste->ste_varnames = v;
v = PyList_New(0);
if (v == NULL)
goto fail;
ste->ste_children = v;
ste->ste_optimized = 1;
ste->ste_lineno = lineno;
switch (type) {
case funcdef:
case lambdef:
ste->ste_type = TYPE_FUNCTION;
break;
case classdef:
ste->ste_type = TYPE_CLASS;
break;
case single_input:
case eval_input:
case file_input:
ste->ste_type = TYPE_MODULE;
break;
}
if (st->st_cur == NULL)
ste->ste_nested = 0;
else if (st->st_cur->ste_nested
|| st->st_cur->ste_type == TYPE_FUNCTION)
ste->ste_nested = 1;
else
ste->ste_nested = 0;
ste->ste_child_free = 0;
if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
goto fail;
return (PyObject *)ste;
fail:
Py_XDECREF(ste);
return NULL;
}
static PyObject *
ste_repr(PySymtableEntryObject *ste)
{
char buf[256];
sprintf(buf, "<symtable entry %.100s(%ld), line %d>",
PyString_AS_STRING(ste->ste_name),
PyInt_AS_LONG(ste->ste_id),
ste->ste_lineno);
return PyString_FromString(buf);
}
static void
ste_dealloc(PySymtableEntryObject *ste)
{
ste->ste_table = NULL;
Py_XDECREF(ste->ste_id);
Py_XDECREF(ste->ste_name);
Py_XDECREF(ste->ste_symbols);
Py_XDECREF(ste->ste_varnames);
Py_XDECREF(ste->ste_children);
PyObject_Del(ste);
}
#define OFF(x) offsetof(PySymtableEntryObject, x)
static struct memberlist ste_memberlist[] = {
{"id", T_OBJECT, OFF(ste_id), READONLY},
{"name", T_OBJECT, OFF(ste_name), READONLY},
{"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
{"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
{"children", T_OBJECT, OFF(ste_children), READONLY},
{"type", T_INT, OFF(ste_type), READONLY},
{"lineno", T_INT, OFF(ste_lineno), READONLY},
{"optimized",T_INT, OFF(ste_optimized), READONLY},
{"nested", T_INT, OFF(ste_nested), READONLY},
{NULL}
};
static PyObject *
ste_getattr(PySymtableEntryObject *ste, char *name)
{
return PyMember_Get((char *)ste, ste_memberlist, name);
}
PyTypeObject PySymtableEntry_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"symtable entry",
sizeof(PySymtableEntryObject),
0,
(destructor)ste_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc)ste_getattr, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)ste_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
};
|