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