blob: 1e13b790de3d01e7d8702b579620cca02a6a954d [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Neal Norwitz090b3dd2006-02-28 22:36:46 +000022static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000023ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000024 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020027 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 k = PyLong_FromVoidPtr(key);
30 if (k == NULL)
31 goto fail;
32 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020033 if (ste == NULL) {
34 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020038 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 ste->ste_name = name;
41 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_symbols = NULL;
44 ste->ste_varnames = NULL;
45 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_symbols = PyDict_New();
48 if (ste->ste_symbols == NULL)
49 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 ste->ste_varnames = PyList_New(0);
52 if (ste->ste_varnames == NULL)
53 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_children = PyList_New(0);
56 if (ste->ste_children == NULL)
57 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_type = block;
60 ste->ste_unoptimized = 0;
61 ste->ste_nested = 0;
62 ste->ste_free = 0;
63 ste->ste_varargs = 0;
64 ste->ste_varkeywords = 0;
65 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000066 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000067 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000069 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
74 ste->ste_nested = 1;
75 ste->ste_child_free = 0;
76 ste->ste_generator = 0;
77 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
80 goto fail;
81
82 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_XDECREF(ste);
85 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
92 ste->ste_name,
93 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_reserved */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000165static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 _Py_block_ty block, void *ast, int lineno,
168 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500186static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
191 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000197"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199static struct symtable *
200symtable_new(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
205 if (st == NULL)
206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if ((st->st_stack = PyList_New(0)) == NULL)
212 goto fail;
213 if ((st->st_blocks = PyDict_New()) == NULL)
214 goto fail;
215 st->st_cur = NULL;
216 st->st_private = NULL;
217 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PySymtable_Free(st);
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221}
222
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000223/* When compiling the use of C stack is probably going to be a lot
224 lighter than when executing Python code but still can overflow
225 and causing a Python crash if not checked (e.g. eval("()"*300000)).
226 Using the current recursion limit for the compiler seems too
227 restrictive (it caused at least one test to fail) so a factor is
228 used to allow deeper recursion when compiling an expression.
229
230 Using a scaling factor means this should automatically adjust when
231 the recursion limit is adjusted for small or large C stack allocations.
232*/
233#define COMPILER_STACK_FRAME_SCALE 3
234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235struct symtable *
236PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
237{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000238 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 asdl_seq *seq;
240 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000241 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400242 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (st == NULL)
245 return st;
246 st->st_filename = filename;
247 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000248
249 /* Setup recursion depth check counters */
250 tstate = PyThreadState_GET();
251 if (!tstate) {
252 PySymtable_Free(st);
253 return NULL;
254 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400255 /* Be careful here to prevent overflow. */
256 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
257 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
258 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
259 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* Make the initial symbol information gathering pass */
262 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000263 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PySymtable_Free(st);
265 return NULL;
266 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 st->st_top = st->st_cur;
269 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
270 switch (mod->kind) {
271 case Module_kind:
272 seq = mod->v.Module.body;
273 for (i = 0; i < asdl_seq_LEN(seq); i++)
274 if (!symtable_visit_stmt(st,
275 (stmt_ty)asdl_seq_GET(seq, i)))
276 goto error;
277 break;
278 case Expression_kind:
279 if (!symtable_visit_expr(st, mod->v.Expression.body))
280 goto error;
281 break;
282 case Interactive_kind:
283 seq = mod->v.Interactive.body;
284 for (i = 0; i < asdl_seq_LEN(seq); i++)
285 if (!symtable_visit_stmt(st,
286 (stmt_ty)asdl_seq_GET(seq, i)))
287 goto error;
288 break;
289 case Suite_kind:
290 PyErr_SetString(PyExc_RuntimeError,
291 "this compiler does not handle Suites");
292 goto error;
293 }
294 if (!symtable_exit_block(st, (void *)mod)) {
295 PySymtable_Free(st);
296 return NULL;
297 }
298 /* Make the second symbol analysis pass */
299 if (symtable_analyze(st))
300 return st;
301 PySymtable_Free(st);
302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 (void) symtable_exit_block(st, (void *)mod);
305 PySymtable_Free(st);
306 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309void
310PySymtable_Free(struct symtable *st)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_XDECREF(st->st_blocks);
313 Py_XDECREF(st->st_stack);
314 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
317PySTEntryObject *
318PySymtable_Lookup(struct symtable *st, void *key)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 k = PyLong_FromVoidPtr(key);
323 if (k == NULL)
324 return NULL;
325 v = PyDict_GetItem(st->st_blocks, k);
326 if (v) {
327 assert(PySTEntry_Check(v));
328 Py_INCREF(v);
329 }
330 else {
331 PyErr_SetString(PyExc_KeyError,
332 "unknown symbol table entry");
333 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_DECREF(k);
336 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337}
338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340PyST_GetScope(PySTEntryObject *ste, PyObject *name)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
343 if (!v)
344 return 0;
345 assert(PyLong_Check(v));
346 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347}
348
349
350/* Analyze raw symbol information to determine scope of each name.
351
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000352 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 explicit global is declared with the global statement. An implicit
359 global is a free variable for which the compiler has found no binding
360 in an enclosing function scope. The implicit global is either a global
361 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
362 to handle these names to implement slightly odd semantics. In such a
363 block, the name is treated as global until it is assigned to; then it
364 is treated as a local.
365
366 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000367 The first pass collects raw facts from the AST via the symtable_visit_*
368 functions: the name is a parameter here, the name is used but not defined
369 here, etc. The second pass analyzes these facts during a pass over the
370 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371
372 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000374 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000375 Names which are explicitly declared nonlocal must exist in this set of
376 visible names - if they do not, a syntax error is raised. After doing
377 the local analysis, it analyzes each of its child blocks using an
378 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379
Nick Coghlan650f0d02007-04-15 12:05:43 +0000380 The children update the free variable set. If a local variable is added to
381 the free variable set by the child, the variable is marked as a cell. The
382 function object being defined must provide runtime storage for the variable
383 that may outlive the function's frame. Cell variables are removed from the
384 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000385
Nick Coghlan650f0d02007-04-15 12:05:43 +0000386 During analysis, the names are:
387 symbols: dict mapping from symbol names to flag values (including offset scope values)
388 scopes: dict mapping from symbol names to scope values (no offset)
389 local: set of all symbol names local to the current scope
390 bound: set of all symbol names local to a containing function scope
391 free: set of all symbol names referenced but not bound in child scopes
392 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393*/
394
395#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyObject *o = PyLong_FromLong(I); \
397 if (!o) \
398 return 0; \
399 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
400 Py_DECREF(o); \
401 return 0; \
402 } \
403 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404}
405
406/* Decide on scope of name, given flags.
407
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000408 The namespace dictionaries may be modified to record information
409 about the new name. For example, a new global will add an entry to
410 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411*/
412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000414analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyObject *bound, PyObject *local, PyObject *free,
416 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (flags & DEF_GLOBAL) {
419 if (flags & DEF_PARAM) {
420 PyErr_Format(PyExc_SyntaxError,
421 "name '%U' is parameter and global",
422 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000423 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
424 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425
426 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000427 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (flags & DEF_NONLOCAL) {
429 PyErr_Format(PyExc_SyntaxError,
430 "name '%U' is nonlocal and global",
431 name);
432 return 0;
433 }
434 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
435 if (PySet_Add(global, name) < 0)
436 return 0;
437 if (bound && (PySet_Discard(bound, name) < 0))
438 return 0;
439 return 1;
440 }
441 if (flags & DEF_NONLOCAL) {
442 if (flags & DEF_PARAM) {
443 PyErr_Format(PyExc_SyntaxError,
444 "name '%U' is parameter and nonlocal",
445 name);
446 return 0;
447 }
448 if (!bound) {
449 PyErr_Format(PyExc_SyntaxError,
450 "nonlocal declaration not allowed at module level");
451 return 0;
452 }
453 if (!PySet_Contains(bound, name)) {
454 PyErr_Format(PyExc_SyntaxError,
455 "no binding for nonlocal '%U' found",
456 name);
457
458 return 0;
459 }
460 SET_SCOPE(scopes, name, FREE);
461 ste->ste_free = 1;
462 return PySet_Add(free, name) >= 0;
463 }
464 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000465 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (PySet_Add(local, name) < 0)
467 return 0;
468 if (PySet_Discard(global, name) < 0)
469 return 0;
470 return 1;
471 }
472 /* If an enclosing block has a binding for this name, it
473 is a free variable rather than a global variable.
474 Note that having a non-NULL bound implies that the block
475 is nested.
476 */
477 if (bound && PySet_Contains(bound, name)) {
478 SET_SCOPE(scopes, name, FREE);
479 ste->ste_free = 1;
480 return PySet_Add(free, name) >= 0;
481 }
482 /* If a parent has a global statement, then call it global
483 explicit? It could also be global implicit.
484 */
485 if (global && PySet_Contains(global, name)) {
486 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
487 return 1;
488 }
489 if (ste->ste_nested)
490 ste->ste_free = 1;
491 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
492 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493}
494
495#undef SET_SCOPE
496
497/* If a name is defined in free and also in locals, then this block
498 provides the binding for the free variable. The name should be
499 marked CELL in this block and removed from the free list.
500
501 Note that the current block's free variables are included in free.
502 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000503
Martin v. Löwis2673a572007-10-29 19:54:24 +0000504 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000505 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506*/
507
508static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000509analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *name, *v, *v_cell;
512 int success = 0;
513 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 v_cell = PyLong_FromLong(CELL);
516 if (!v_cell)
517 return 0;
518 while (PyDict_Next(scopes, &pos, &name, &v)) {
519 long scope;
520 assert(PyLong_Check(v));
521 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000522 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 continue;
524 if (!PySet_Contains(free, name))
525 continue;
526 if (restricted != NULL &&
527 PyUnicode_CompareWithASCIIString(name, restricted))
528 continue;
529 /* Replace LOCAL with CELL for this name, and remove
530 from free. It is safe to replace the value of name
531 in the dict, because it will not cause a resize.
532 */
533 if (PyDict_SetItem(scopes, name, v_cell) < 0)
534 goto error;
535 if (PySet_Discard(free, name) < 0)
536 goto error;
537 }
538 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 Py_DECREF(v_cell);
541 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542}
543
544/* Check for illegal statements in unoptimized namespaces */
545static int
546check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
550 || !(ste->ste_free || ste->ste_child_free))
551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 trailer = (ste->ste_child_free ?
554 "contains a nested function with free variables" :
555 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 switch (ste->ste_unoptimized) {
558 case OPT_TOPLEVEL: /* import * at top-level is fine */
559 return 1;
560 case OPT_IMPORT_STAR:
561 PyErr_Format(PyExc_SyntaxError,
562 "import * is not allowed in function '%U' because it %s",
563 ste->ste_name, trailer);
564 break;
565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000567 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
568 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570}
571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572/* Enter the final scope information into the ste_symbols dict.
573 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 * All arguments are dicts. Modifies symbols, others are read-only.
575*/
576static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 PyObject *name = NULL, *itr = NULL;
581 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
582 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 /* Update scope information for all symbols in this scope */
585 while (PyDict_Next(symbols, &pos, &name, &v)) {
586 long scope, flags;
587 assert(PyLong_Check(v));
588 flags = PyLong_AS_LONG(v);
589 v_scope = PyDict_GetItem(scopes, name);
590 assert(v_scope && PyLong_Check(v_scope));
591 scope = PyLong_AS_LONG(v_scope);
592 flags |= (scope << SCOPE_OFFSET);
593 v_new = PyLong_FromLong(flags);
594 if (!v_new)
595 return 0;
596 if (PyDict_SetItem(symbols, name, v_new) < 0) {
597 Py_DECREF(v_new);
598 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_DECREF(v_new);
601 }
602
603 /* Record not yet resolved free variables from children (if any) */
604 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
605 if (!v_free)
606 return 0;
607
608 itr = PyObject_GetIter(free);
609 if (!itr)
610 goto error;
611
612 while ((name = PyIter_Next(itr))) {
613 v = PyDict_GetItem(symbols, name);
614
615 /* Handle symbol that already exists in this scope */
616 if (v) {
617 /* Handle a free variable in a method of
618 the class that has the same name as a local
619 or global in the class scope.
620 */
621 if (classflag &&
622 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
623 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
624 v_new = PyLong_FromLong(flags);
625 if (!v_new) {
626 goto error;
627 }
628 if (PyDict_SetItem(symbols, name, v_new) < 0) {
629 Py_DECREF(v_new);
630 goto error;
631 }
632 Py_DECREF(v_new);
633 }
634 /* It's a cell, or already free in this scope */
635 Py_DECREF(name);
636 continue;
637 }
638 /* Handle global symbol */
639 if (!PySet_Contains(bound, name)) {
640 Py_DECREF(name);
641 continue; /* it's a global */
642 }
643 /* Propagate new free symbol up the lexical stack */
644 if (PyDict_SetItem(symbols, name, v_free) < 0) {
645 goto error;
646 }
647 Py_DECREF(name);
648 }
649 Py_DECREF(itr);
650 Py_DECREF(v_free);
651 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000652error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 Py_XDECREF(v_free);
654 Py_XDECREF(itr);
655 Py_XDECREF(name);
656 return 0;
657}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
659/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 Arguments:
662 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000663 bound -- set of variables bound in enclosing scopes (input). bound
664 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 free -- set of free variables in enclosed scopes (output)
666 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000667
668 The implementation uses two mutually recursive functions,
669 analyze_block() and analyze_child_block(). analyze_block() is
670 responsible for analyzing the individual names defined in a block.
671 analyze_child_block() prepares temporary namespace dictionaries
672 used to evaluated nested blocks.
673
674 The two functions exist because a child block should see the name
675 bindings of its enclosing blocks, but those bindings should not
676 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677*/
678
679static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
681 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000682
683static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
685 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
688 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
689 PyObject *temp;
690 int i, success = 0;
691 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 local = PySet_New(NULL); /* collect new names bound in block */
694 if (!local)
695 goto error;
696 scopes = PyDict_New(); /* collect scopes defined for each name */
697 if (!scopes)
698 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 /* Allocate new global and bound variable dictionaries. These
701 dictionaries hold the names visible in nested blocks. For
702 ClassBlocks, the bound and global names are initialized
703 before analyzing names, because class bindings aren't
704 visible in methods. For other blocks, they are initialized
705 after names are analyzed.
706 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 /* TODO(jhylton): Package these dicts in a struct so that we
709 can write reasonable helper functions?
710 */
711 newglobal = PySet_New(NULL);
712 if (!newglobal)
713 goto error;
714 newfree = PySet_New(NULL);
715 if (!newfree)
716 goto error;
717 newbound = PySet_New(NULL);
718 if (!newbound)
719 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 /* Class namespace has no effect on names visible in
722 nested functions, so populate the global and bound
723 sets to be passed to child blocks before analyzing
724 this one.
725 */
726 if (ste->ste_type == ClassBlock) {
727 /* Pass down known globals */
728 temp = PyNumber_InPlaceOr(newglobal, global);
729 if (!temp)
730 goto error;
731 Py_DECREF(temp);
732 /* Pass down previously bound symbols */
733 if (bound) {
734 temp = PyNumber_InPlaceOr(newbound, bound);
735 if (!temp)
736 goto error;
737 Py_DECREF(temp);
738 }
739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
742 long flags = PyLong_AS_LONG(v);
743 if (!analyze_name(ste, scopes, name, flags,
744 bound, local, free, global))
745 goto error;
746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 /* Populate global and bound sets to be passed to children. */
749 if (ste->ste_type != ClassBlock) {
750 /* Add function locals to bound set */
751 if (ste->ste_type == FunctionBlock) {
752 temp = PyNumber_InPlaceOr(newbound, local);
753 if (!temp)
754 goto error;
755 Py_DECREF(temp);
756 }
757 /* Pass down previously bound symbols */
758 if (bound) {
759 temp = PyNumber_InPlaceOr(newbound, bound);
760 if (!temp)
761 goto error;
762 Py_DECREF(temp);
763 }
764 /* Pass down known globals */
765 temp = PyNumber_InPlaceOr(newglobal, global);
766 if (!temp)
767 goto error;
768 Py_DECREF(temp);
769 }
770 else {
771 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000772 if (!GET_IDENTIFIER(__class__))
773 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 assert(PySet_Contains(local, __class__) == 1);
775 if (PySet_Add(newbound, __class__) < 0)
776 goto error;
777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300779 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 newbound, newglobal now contain the names visible in
782 nested blocks. The free variables in the children will
783 be collected in allfree.
784 */
785 allfree = PySet_New(NULL);
786 if (!allfree)
787 goto error;
788 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
789 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
790 PySTEntryObject* entry;
791 assert(c && PySTEntry_Check(c));
792 entry = (PySTEntryObject*)c;
793 if (!analyze_child_block(entry, newbound, newfree, newglobal,
794 allfree))
795 goto error;
796 /* Check if any children have free variables */
797 if (entry->ste_free || entry->ste_child_free)
798 ste->ste_child_free = 1;
799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 temp = PyNumber_InPlaceOr(newfree, allfree);
802 if (!temp)
803 goto error;
804 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 /* Check if any local variables must be converted to cell variables */
807 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
808 NULL))
809 goto error;
810 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000811 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 goto error;
813 /* Records the results of the analysis in the symbol table entry */
814 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
815 ste->ste_type == ClassBlock))
816 goto error;
817 if (!check_unoptimized(ste))
818 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 temp = PyNumber_InPlaceOr(free, newfree);
821 if (!temp)
822 goto error;
823 Py_DECREF(temp);
824 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 Py_XDECREF(scopes);
827 Py_XDECREF(local);
828 Py_XDECREF(newbound);
829 Py_XDECREF(newglobal);
830 Py_XDECREF(newfree);
831 Py_XDECREF(allfree);
832 if (!success)
833 assert(PyErr_Occurred());
834 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835}
836
837static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
839 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
842 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 These dictionary are used by all blocks enclosed by the
847 current block. The analyze_block() call modifies these
848 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 */
851 temp_bound = PySet_New(bound);
852 if (!temp_bound)
853 goto error;
854 temp_free = PySet_New(free);
855 if (!temp_free)
856 goto error;
857 temp_global = PySet_New(global);
858 if (!temp_global)
859 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
862 goto error;
863 temp = PyNumber_InPlaceOr(child_free, temp_free);
864 if (!temp)
865 goto error;
866 Py_DECREF(temp);
867 Py_DECREF(temp_bound);
868 Py_DECREF(temp_free);
869 Py_DECREF(temp_global);
870 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000871 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_XDECREF(temp_bound);
873 Py_XDECREF(temp_free);
874 Py_XDECREF(temp_global);
875 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000876}
877
878static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879symtable_analyze(struct symtable *st)
880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *free, *global;
882 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 free = PySet_New(NULL);
885 if (!free)
886 return 0;
887 global = PySet_New(NULL);
888 if (!global) {
889 Py_DECREF(free);
890 return 0;
891 }
892 r = analyze_block(st->st_top, NULL, free, global);
893 Py_DECREF(free);
894 Py_DECREF(global);
895 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896}
897
898
899static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000900symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
903 lineno, NULL, NULL) < 0) {
904 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
905 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000906 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
907 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
909 return 0;
910 }
911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000914/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 This reference is released when the block is exited, via the DECREF
916 in symtable_exit_block().
917*/
918
919static int
920symtable_exit_block(struct symtable *st, void *ast)
921{
Benjamin Peterson609da582011-06-29 22:52:39 -0500922 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
Benjamin Peterson609da582011-06-29 22:52:39 -0500924 st->st_cur = NULL;
925 size = PyList_GET_SIZE(st->st_stack);
926 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500927 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500929 if (--size)
930 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 }
932 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933}
934
935static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000937 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938{
Benjamin Peterson609da582011-06-29 22:52:39 -0500939 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 ste = ste_new(st, name, block, ast, lineno, col_offset);
942 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
945 Py_DECREF(ste);
946 return 0;
947 }
948 prev = st->st_cur;
949 /* The entry is owned by the stack. Borrow it for st_cur. */
950 Py_DECREF(ste);
951 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000952 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 st->st_global = st->st_cur->ste_symbols;
954 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return 0;
957 }
958 }
959 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960}
961
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000962static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963symtable_lookup(struct symtable *st, PyObject *name)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject *o;
966 PyObject *mangled = _Py_Mangle(st->st_private, name);
967 if (!mangled)
968 return 0;
969 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
970 Py_DECREF(mangled);
971 if (!o)
972 return 0;
973 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
976static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *o;
980 PyObject *dict;
981 long val;
982 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Jeremy Hylton81e95022007-02-27 06:50:52 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (!mangled)
986 return 0;
987 dict = st->st_cur->ste_symbols;
988 if ((o = PyDict_GetItem(dict, mangled))) {
989 val = PyLong_AS_LONG(o);
990 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
991 /* Is it better to use 'mangled' or 'name' here? */
992 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000993 PyErr_SyntaxLocationEx(st->st_filename,
994 st->st_cur->ste_lineno,
995 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 goto error;
997 }
998 val |= flag;
999 } else
1000 val = flag;
1001 o = PyLong_FromLong(val);
1002 if (o == NULL)
1003 goto error;
1004 if (PyDict_SetItem(dict, mangled, o) < 0) {
1005 Py_DECREF(o);
1006 goto error;
1007 }
1008 Py_DECREF(o);
1009
1010 if (flag & DEF_PARAM) {
1011 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1012 goto error;
1013 } else if (flag & DEF_GLOBAL) {
1014 /* XXX need to update DEF_GLOBAL for other flags too;
1015 perhaps only DEF_FREE_GLOBAL */
1016 val = flag;
1017 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1018 val |= PyLong_AS_LONG(o);
1019 }
1020 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 goto error;
1023 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1024 Py_DECREF(o);
1025 goto error;
1026 }
1027 Py_DECREF(o);
1028 }
1029 Py_DECREF(mangled);
1030 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001031
1032error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Py_DECREF(mangled);
1034 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035}
1036
1037/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1038 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 function.
1040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1042 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001043
1044 VISIT_QUIT macro returns the specified value exiting from the function but
1045 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046*/
1047
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001048#define VISIT_QUIT(ST, X) \
1049 return --(ST)->recursion_depth,(X)
1050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001053 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 int i; \
1057 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1058 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1059 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1060 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001061 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 int i; \
1067 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1068 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1069 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1070 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001071 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074
Guido van Rossum4f72a782006-10-27 23:31:49 +00001075#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 int i = 0; \
1077 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1078 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1079 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1080 if (!elt) continue; /* can be NULL */ \
1081 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001082 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001084}
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001087symtable_new_tmpname(struct symtable *st)
1088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 char tmpname[256];
1090 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1093 ++st->st_cur->ste_tmpname);
1094 tmp = PyUnicode_InternFromString(tmpname);
1095 if (!tmp)
1096 return 0;
1097 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1098 return 0;
1099 Py_DECREF(tmp);
1100 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101}
1102
Guido van Rossum4f72a782006-10-27 23:31:49 +00001103
Guido van Rossumc2e20742006-02-27 22:32:47 +00001104static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105symtable_visit_stmt(struct symtable *st, stmt_ty s)
1106{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001107 if (++st->recursion_depth > st->recursion_limit) {
1108 PyErr_SetString(PyExc_RuntimeError,
1109 "maximum recursion depth exceeded during compilation");
1110 VISIT_QUIT(st, 0);
1111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 switch (s->kind) {
1113 case FunctionDef_kind:
1114 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001115 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (s->v.FunctionDef.args->defaults)
1117 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1118 if (s->v.FunctionDef.args->kw_defaults)
1119 VISIT_KWONLYDEFAULTS(st,
1120 s->v.FunctionDef.args->kw_defaults);
1121 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001122 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (s->v.FunctionDef.decorator_list)
1124 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1125 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001126 FunctionBlock, (void *)s, s->lineno,
1127 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001128 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001129 VISIT(st, arguments, s->v.FunctionDef.args);
1130 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001132 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 break;
1134 case ClassDef_kind: {
1135 PyObject *tmp;
1136 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1139 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1140 if (s->v.ClassDef.starargs)
1141 VISIT(st, expr, s->v.ClassDef.starargs);
1142 if (s->v.ClassDef.kwargs)
1143 VISIT(st, expr, s->v.ClassDef.kwargs);
1144 if (s->v.ClassDef.decorator_list)
1145 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1146 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001147 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001148 VISIT_QUIT(st, 0);
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001149 if (!GET_IDENTIFIER(__class__) ||
1150 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 !GET_IDENTIFIER(__locals__) ||
1152 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1153 symtable_exit_block(st, s);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001154 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 }
1156 tmp = st->st_private;
1157 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001158 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 st->st_private = tmp;
1160 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 break;
1163 }
1164 case Return_kind:
1165 if (s->v.Return.value) {
1166 VISIT(st, expr, s->v.Return.value);
1167 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
1169 break;
1170 case Delete_kind:
1171 VISIT_SEQ(st, expr, s->v.Delete.targets);
1172 break;
1173 case Assign_kind:
1174 VISIT_SEQ(st, expr, s->v.Assign.targets);
1175 VISIT(st, expr, s->v.Assign.value);
1176 break;
1177 case AugAssign_kind:
1178 VISIT(st, expr, s->v.AugAssign.target);
1179 VISIT(st, expr, s->v.AugAssign.value);
1180 break;
1181 case For_kind:
1182 VISIT(st, expr, s->v.For.target);
1183 VISIT(st, expr, s->v.For.iter);
1184 VISIT_SEQ(st, stmt, s->v.For.body);
1185 if (s->v.For.orelse)
1186 VISIT_SEQ(st, stmt, s->v.For.orelse);
1187 break;
1188 case While_kind:
1189 VISIT(st, expr, s->v.While.test);
1190 VISIT_SEQ(st, stmt, s->v.While.body);
1191 if (s->v.While.orelse)
1192 VISIT_SEQ(st, stmt, s->v.While.orelse);
1193 break;
1194 case If_kind:
1195 /* XXX if 0: and lookup_yield() hacks */
1196 VISIT(st, expr, s->v.If.test);
1197 VISIT_SEQ(st, stmt, s->v.If.body);
1198 if (s->v.If.orelse)
1199 VISIT_SEQ(st, stmt, s->v.If.orelse);
1200 break;
1201 case Raise_kind:
1202 if (s->v.Raise.exc) {
1203 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001204 if (s->v.Raise.cause) {
1205 VISIT(st, expr, s->v.Raise.cause);
1206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 }
1208 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001209 case Try_kind:
1210 VISIT_SEQ(st, stmt, s->v.Try.body);
1211 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1212 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1213 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 break;
1215 case Assert_kind:
1216 VISIT(st, expr, s->v.Assert.test);
1217 if (s->v.Assert.msg)
1218 VISIT(st, expr, s->v.Assert.msg);
1219 break;
1220 case Import_kind:
1221 VISIT_SEQ(st, alias, s->v.Import.names);
1222 /* XXX Don't have the lineno available inside
1223 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001224 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001226 st->st_cur->ste_opt_col_offset = s->col_offset;
1227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 break;
1229 case ImportFrom_kind:
1230 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1231 /* XXX Don't have the lineno available inside
1232 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001233 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001235 st->st_cur->ste_opt_col_offset = s->col_offset;
1236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 break;
1238 case Global_kind: {
1239 int i;
1240 asdl_seq *seq = s->v.Global.names;
1241 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1242 identifier name = (identifier)asdl_seq_GET(seq, i);
Benjamin Petersone2135c62013-05-16 19:38:22 -05001243 long cur;
Benjamin Peterson1e93b062013-05-15 16:17:25 -05001244 if (st->st_cur->ste_type == ClassBlock &&
1245 !PyUnicode_CompareWithASCIIString(name, "__class__")) {
1246 PyErr_SetString(PyExc_SyntaxError, "cannot make __class__ global");
1247 PyErr_SyntaxLocationEx(st->st_filename, s->lineno, s->col_offset);
1248 return 0;
1249 }
Benjamin Petersone2135c62013-05-16 19:38:22 -05001250 cur = symtable_lookup(st, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001252 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (cur & (DEF_LOCAL | USE)) {
1254 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001255 char *c_name = _PyUnicode_AsString(name);
1256 if (!c_name)
1257 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (cur & DEF_LOCAL)
1259 PyOS_snprintf(buf, sizeof(buf),
1260 GLOBAL_AFTER_ASSIGN,
1261 c_name);
1262 else
1263 PyOS_snprintf(buf, sizeof(buf),
1264 GLOBAL_AFTER_USE,
1265 c_name);
1266 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001267 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 }
1269 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001270 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
1272 break;
1273 }
1274 case Nonlocal_kind: {
1275 int i;
1276 asdl_seq *seq = s->v.Nonlocal.names;
1277 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1278 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 long cur = symtable_lookup(st, name);
1280 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001281 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (cur & (DEF_LOCAL | USE)) {
1283 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001284 char *c_name = _PyUnicode_AsString(name);
1285 if (!c_name)
1286 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (cur & DEF_LOCAL)
1288 PyOS_snprintf(buf, sizeof(buf),
1289 NONLOCAL_AFTER_ASSIGN,
1290 c_name);
1291 else
1292 PyOS_snprintf(buf, sizeof(buf),
1293 NONLOCAL_AFTER_USE,
1294 c_name);
1295 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001296 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 }
1298 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001299 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 }
1301 break;
1302 }
1303 case Expr_kind:
1304 VISIT(st, expr, s->v.Expr.value);
1305 break;
1306 case Pass_kind:
1307 case Break_kind:
1308 case Continue_kind:
1309 /* nothing to do here */
1310 break;
1311 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001312 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 VISIT_SEQ(st, stmt, s->v.With.body);
1314 break;
1315 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001316 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320symtable_visit_expr(struct symtable *st, expr_ty e)
1321{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001322 if (++st->recursion_depth > st->recursion_limit) {
1323 PyErr_SetString(PyExc_RuntimeError,
1324 "maximum recursion depth exceeded during compilation");
1325 VISIT_QUIT(st, 0);
1326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 switch (e->kind) {
1328 case BoolOp_kind:
1329 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1330 break;
1331 case BinOp_kind:
1332 VISIT(st, expr, e->v.BinOp.left);
1333 VISIT(st, expr, e->v.BinOp.right);
1334 break;
1335 case UnaryOp_kind:
1336 VISIT(st, expr, e->v.UnaryOp.operand);
1337 break;
1338 case Lambda_kind: {
1339 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001340 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (e->v.Lambda.args->defaults)
1342 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001343 if (e->v.Lambda.args->kw_defaults)
1344 VISIT_KWONLYDEFAULTS(st,
1345 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001347 FunctionBlock, (void *)e, e->lineno,
1348 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001349 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001350 VISIT(st, arguments, e->v.Lambda.args);
1351 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001353 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 break;
1355 }
1356 case IfExp_kind:
1357 VISIT(st, expr, e->v.IfExp.test);
1358 VISIT(st, expr, e->v.IfExp.body);
1359 VISIT(st, expr, e->v.IfExp.orelse);
1360 break;
1361 case Dict_kind:
1362 VISIT_SEQ(st, expr, e->v.Dict.keys);
1363 VISIT_SEQ(st, expr, e->v.Dict.values);
1364 break;
1365 case Set_kind:
1366 VISIT_SEQ(st, expr, e->v.Set.elts);
1367 break;
1368 case GeneratorExp_kind:
1369 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001370 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 break;
1372 case ListComp_kind:
1373 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001374 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 break;
1376 case SetComp_kind:
1377 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001378 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 break;
1380 case DictComp_kind:
1381 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001382 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 break;
1384 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001385 if (e->v.Yield.value)
1386 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001389 case YieldFrom_kind:
1390 VISIT(st, expr, e->v.YieldFrom.value);
1391 st->st_cur->ste_generator = 1;
1392 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 case Compare_kind:
1394 VISIT(st, expr, e->v.Compare.left);
1395 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1396 break;
1397 case Call_kind:
1398 VISIT(st, expr, e->v.Call.func);
1399 VISIT_SEQ(st, expr, e->v.Call.args);
1400 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1401 if (e->v.Call.starargs)
1402 VISIT(st, expr, e->v.Call.starargs);
1403 if (e->v.Call.kwargs)
1404 VISIT(st, expr, e->v.Call.kwargs);
1405 break;
1406 case Num_kind:
1407 case Str_kind:
1408 case Bytes_kind:
1409 case Ellipsis_kind:
1410 /* Nothing to do here. */
1411 break;
1412 /* The following exprs can be assignment targets. */
1413 case Attribute_kind:
1414 VISIT(st, expr, e->v.Attribute.value);
1415 break;
1416 case Subscript_kind:
1417 VISIT(st, expr, e->v.Subscript.value);
1418 VISIT(st, slice, e->v.Subscript.slice);
1419 break;
1420 case Starred_kind:
1421 VISIT(st, expr, e->v.Starred.value);
1422 break;
1423 case Name_kind:
1424 if (!symtable_add_def(st, e->v.Name.id,
1425 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001426 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 /* Special-case super: it counts as a use of __class__ */
1428 if (e->v.Name.ctx == Load &&
1429 st->st_cur->ste_type == FunctionBlock &&
1430 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001431 if (!GET_IDENTIFIER(__class__) ||
1432 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001433 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 }
1435 break;
1436 /* child nodes of List and Tuple will have expr_context set */
1437 case List_kind:
1438 VISIT_SEQ(st, expr, e->v.List.elts);
1439 break;
1440 case Tuple_kind:
1441 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1442 break;
1443 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001444 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445}
1446
1447static int
1448symtable_implicit_arg(struct symtable *st, int pos)
1449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1451 if (id == NULL)
1452 return 0;
1453 if (!symtable_add_def(st, id, DEF_PARAM)) {
1454 Py_DECREF(id);
1455 return 0;
1456 }
1457 Py_DECREF(id);
1458 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459}
1460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001462symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (!args)
1467 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 for (i = 0; i < asdl_seq_LEN(args); i++) {
1470 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1471 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1472 return 0;
1473 }
1474
1475 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476}
1477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001479symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (!args)
1484 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 for (i = 0; i < asdl_seq_LEN(args); i++) {
1487 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1488 if (arg->annotation)
1489 VISIT(st, expr, arg->annotation);
1490 }
1491
1492 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001493}
1494
Neal Norwitzc1505362006-12-28 06:47:50 +00001495static int
1496symtable_visit_annotations(struct symtable *st, stmt_ty s)
1497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 arguments_ty a = s->v.FunctionDef.args;
1499
1500 if (a->args && !symtable_visit_argannotations(st, a->args))
1501 return 0;
1502 if (a->varargannotation)
1503 VISIT(st, expr, a->varargannotation);
1504 if (a->kwargannotation)
1505 VISIT(st, expr, a->kwargannotation);
1506 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1507 return 0;
1508 if (s->v.FunctionDef.returns)
1509 VISIT(st, expr, s->v.FunctionDef.returns);
1510 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514symtable_visit_arguments(struct symtable *st, arguments_ty a)
1515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 /* skip default arguments inside function block
1517 XXX should ast be different?
1518 */
1519 if (a->args && !symtable_visit_params(st, a->args))
1520 return 0;
1521 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1522 return 0;
1523 if (a->vararg) {
1524 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1525 return 0;
1526 st->st_cur->ste_varargs = 1;
1527 }
1528 if (a->kwarg) {
1529 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1530 return 0;
1531 st->st_cur->ste_varkeywords = 1;
1532 }
1533 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
1536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (eh->v.ExceptHandler.type)
1541 VISIT(st, expr, eh->v.ExceptHandler.type);
1542 if (eh->v.ExceptHandler.name)
1543 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1544 return 0;
1545 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1546 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001549static int
1550symtable_visit_withitem(struct symtable *st, withitem_ty item)
1551{
1552 VISIT(st, expr, item->context_expr);
1553 if (item->optional_vars) {
1554 VISIT(st, expr, item->optional_vars);
1555 }
1556 return 1;
1557}
1558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561symtable_visit_alias(struct symtable *st, alias_ty a)
1562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001564 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 dotted package name (e.g. spam.eggs)
1566 */
1567 PyObject *store_name;
1568 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001569 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1570 PyUnicode_GET_LENGTH(name), 1);
1571 if (dot != -1) {
1572 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (!store_name)
1574 return 0;
1575 }
1576 else {
1577 store_name = name;
1578 Py_INCREF(store_name);
1579 }
1580 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1581 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1582 Py_DECREF(store_name);
1583 return r;
1584 }
1585 else {
1586 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001587 int lineno = st->st_cur->ste_lineno;
1588 int col_offset = st->st_cur->ste_col_offset;
1589 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1590 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1591 Py_DECREF(store_name);
1592 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 }
1594 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1595 Py_DECREF(store_name);
1596 return 1;
1597 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
1600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 VISIT(st, expr, lc->target);
1605 VISIT(st, expr, lc->iter);
1606 VISIT_SEQ(st, expr, lc->ifs);
1607 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608}
1609
1610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612symtable_visit_keyword(struct symtable *st, keyword_ty k)
1613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 VISIT(st, expr, k->value);
1615 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616}
1617
1618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620symtable_visit_slice(struct symtable *st, slice_ty s)
1621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 switch (s->kind) {
1623 case Slice_kind:
1624 if (s->v.Slice.lower)
1625 VISIT(st, expr, s->v.Slice.lower)
1626 if (s->v.Slice.upper)
1627 VISIT(st, expr, s->v.Slice.upper)
1628 if (s->v.Slice.step)
1629 VISIT(st, expr, s->v.Slice.step)
1630 break;
1631 case ExtSlice_kind:
1632 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1633 break;
1634 case Index_kind:
1635 VISIT(st, expr, s->v.Index.value)
1636 break;
1637 }
1638 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639}
1640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001642symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001643 identifier scope_name, asdl_seq *generators,
1644 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 int is_generator = (e->kind == GeneratorExp_kind);
1647 int needs_tmp = !is_generator;
1648 comprehension_ty outermost = ((comprehension_ty)
1649 asdl_seq_GET(generators, 0));
1650 /* Outermost iterator is evaluated in current scope */
1651 VISIT(st, expr, outermost->iter);
1652 /* Create comprehension scope for the rest */
1653 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001654 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1655 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 return 0;
1657 }
1658 st->st_cur->ste_generator = is_generator;
1659 /* Outermost iter is received as an argument */
1660 if (!symtable_implicit_arg(st, 0)) {
1661 symtable_exit_block(st, (void *)e);
1662 return 0;
1663 }
1664 /* Allocate temporary name if needed */
1665 if (needs_tmp && !symtable_new_tmpname(st)) {
1666 symtable_exit_block(st, (void *)e);
1667 return 0;
1668 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001669 VISIT(st, expr, outermost->target);
1670 VISIT_SEQ(st, expr, outermost->ifs);
1671 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001673 VISIT(st, expr, value);
1674 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001679symtable_visit_genexp(struct symtable *st, expr_ty e)
1680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1682 e->v.GeneratorExp.generators,
1683 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001684}
1685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001687symtable_visit_listcomp(struct symtable *st, expr_ty e)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1690 e->v.ListComp.generators,
1691 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001692}
1693
1694static int
1695symtable_visit_setcomp(struct symtable *st, expr_ty e)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1698 e->v.SetComp.generators,
1699 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001700}
1701
1702static int
1703symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1706 e->v.DictComp.generators,
1707 e->v.DictComp.key,
1708 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001709}