1 #include "Python.h"
2 #include "pycore_pystate.h" // _PyThreadState_GET()
3 #include "symtable.h"
4 #undef Yield /* undefine macro conflicting with <winbase.h> */
5 #include "structmember.h" // PyMemberDef
6
7 /* error strings used for warnings */
8 #define GLOBAL_PARAM \
9 "name '%U' is parameter and global"
10
11 #define NONLOCAL_PARAM \
12 "name '%U' is parameter and nonlocal"
13
14 #define GLOBAL_AFTER_ASSIGN \
15 "name '%U' is assigned to before global declaration"
16
17 #define NONLOCAL_AFTER_ASSIGN \
18 "name '%U' is assigned to before nonlocal declaration"
19
20 #define GLOBAL_AFTER_USE \
21 "name '%U' is used prior to global declaration"
22
23 #define NONLOCAL_AFTER_USE \
24 "name '%U' is used prior to nonlocal declaration"
25
26 #define GLOBAL_ANNOT \
27 "annotated name '%U' can't be global"
28
29 #define NONLOCAL_ANNOT \
30 "annotated name '%U' can't be nonlocal"
31
32 #define IMPORT_STAR_WARNING "import * only allowed at module level"
33
34 #define NAMED_EXPR_COMP_IN_CLASS \
35 "assignment expression within a comprehension cannot be used in a class body"
36
37 #define NAMED_EXPR_COMP_CONFLICT \
38 "assignment expression cannot rebind comprehension iteration variable '%U'"
39
40 #define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
41 "comprehension inner loop cannot rebind assignment expression target '%U'"
42
43 #define NAMED_EXPR_COMP_ITER_EXPR \
44 "assignment expression cannot be used in a comprehension iterable expression"
45
46 static PySTEntryObject *
ste_new(struct symtable * st,identifier name,_Py_block_ty block,void * key,int lineno,int col_offset)47 ste_new(struct symtable *st, identifier name, _Py_block_ty block,
48 void *key, int lineno, int col_offset)
49 {
50 PySTEntryObject *ste = NULL;
51 PyObject *k = NULL;
52
53 k = PyLong_FromVoidPtr(key);
54 if (k == NULL)
55 goto fail;
56 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
57 if (ste == NULL) {
58 Py_DECREF(k);
59 goto fail;
60 }
61 ste->ste_table = st;
62 ste->ste_id = k; /* ste owns reference to k */
63
64 Py_INCREF(name);
65 ste->ste_name = name;
66
67 ste->ste_symbols = NULL;
68 ste->ste_varnames = NULL;
69 ste->ste_children = NULL;
70
71 ste->ste_directives = NULL;
72
73 ste->ste_type = block;
74 ste->ste_nested = 0;
75 ste->ste_free = 0;
76 ste->ste_varargs = 0;
77 ste->ste_varkeywords = 0;
78 ste->ste_opt_lineno = 0;
79 ste->ste_opt_col_offset = 0;
80 ste->ste_lineno = lineno;
81 ste->ste_col_offset = col_offset;
82
83 if (st->st_cur != NULL &&
84 (st->st_cur->ste_nested ||
85 st->st_cur->ste_type == FunctionBlock))
86 ste->ste_nested = 1;
87 ste->ste_child_free = 0;
88 ste->ste_generator = 0;
89 ste->ste_coroutine = 0;
90 ste->ste_comprehension = 0;
91 ste->ste_returns_value = 0;
92 ste->ste_needs_class_closure = 0;
93 ste->ste_comp_iter_target = 0;
94 ste->ste_comp_iter_expr = 0;
95
96 ste->ste_symbols = PyDict_New();
97 ste->ste_varnames = PyList_New(0);
98 ste->ste_children = PyList_New(0);
99 if (ste->ste_symbols == NULL
100 || ste->ste_varnames == NULL
101 || ste->ste_children == NULL)
102 goto fail;
103
104 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
105 goto fail;
106
107 return ste;
108 fail:
109 Py_XDECREF(ste);
110 return NULL;
111 }
112
113 static PyObject *
ste_repr(PySTEntryObject * ste)114 ste_repr(PySTEntryObject *ste)
115 {
116 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
117 ste->ste_name,
118 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
119 }
120
121 static void
ste_dealloc(PySTEntryObject * ste)122 ste_dealloc(PySTEntryObject *ste)
123 {
124 ste->ste_table = NULL;
125 Py_XDECREF(ste->ste_id);
126 Py_XDECREF(ste->ste_name);
127 Py_XDECREF(ste->ste_symbols);
128 Py_XDECREF(ste->ste_varnames);
129 Py_XDECREF(ste->ste_children);
130 Py_XDECREF(ste->ste_directives);
131 PyObject_Del(ste);
132 }
133
134 #define OFF(x) offsetof(PySTEntryObject, x)
135
136 static PyMemberDef ste_memberlist[] = {
137 {"id", T_OBJECT, OFF(ste_id), READONLY},
138 {"name", T_OBJECT, OFF(ste_name), READONLY},
139 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
140 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
141 {"children", T_OBJECT, OFF(ste_children), READONLY},
142 {"nested", T_INT, OFF(ste_nested), READONLY},
143 {"type", T_INT, OFF(ste_type), READONLY},
144 {"lineno", T_INT, OFF(ste_lineno), READONLY},
145 {NULL}
146 };
147
148 PyTypeObject PySTEntry_Type = {
149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
150 "symtable entry",
151 sizeof(PySTEntryObject),
152 0,
153 (destructor)ste_dealloc, /* tp_dealloc */
154 0, /* tp_vectorcall_offset */
155 0, /* tp_getattr */
156 0, /* tp_setattr */
157 0, /* tp_as_async */
158 (reprfunc)ste_repr, /* tp_repr */
159 0, /* tp_as_number */
160 0, /* tp_as_sequence */
161 0, /* tp_as_mapping */
162 0, /* tp_hash */
163 0, /* tp_call */
164 0, /* tp_str */
165 PyObject_GenericGetAttr, /* tp_getattro */
166 0, /* tp_setattro */
167 0, /* tp_as_buffer */
168 Py_TPFLAGS_DEFAULT, /* tp_flags */
169 0, /* tp_doc */
170 0, /* tp_traverse */
171 0, /* tp_clear */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
174 0, /* tp_iter */
175 0, /* tp_iternext */
176 0, /* tp_methods */
177 ste_memberlist, /* tp_members */
178 0, /* tp_getset */
179 0, /* tp_base */
180 0, /* tp_dict */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 0, /* tp_init */
185 0, /* tp_alloc */
186 0, /* tp_new */
187 };
188
189 static int symtable_analyze(struct symtable *st);
190 static int symtable_enter_block(struct symtable *st, identifier name,
191 _Py_block_ty block, void *ast, int lineno,
192 int col_offset);
193 static int symtable_exit_block(struct symtable *st);
194 static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
195 static int symtable_visit_expr(struct symtable *st, expr_ty s);
196 static int symtable_visit_genexp(struct symtable *st, expr_ty s);
197 static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
198 static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
199 static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
200 static int symtable_visit_arguments(struct symtable *st, arguments_ty);
201 static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
202 static int symtable_visit_alias(struct symtable *st, alias_ty);
203 static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
204 static int symtable_visit_keyword(struct symtable *st, keyword_ty);
205 static int symtable_visit_params(struct symtable *st, asdl_seq *args);
206 static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
207 static int symtable_implicit_arg(struct symtable *st, int pos);
208 static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
209 static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
210
211
212 static identifier top = NULL, lambda = NULL, genexpr = NULL,
213 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
214 __class__ = NULL;
215
216 #define GET_IDENTIFIER(VAR) \
217 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
218
219 #define DUPLICATE_ARGUMENT \
220 "duplicate argument '%U' in function definition"
221
222 static struct symtable *
symtable_new(void)223 symtable_new(void)
224 {
225 struct symtable *st;
226
227 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
228 if (st == NULL) {
229 PyErr_NoMemory();
230 return NULL;
231 }
232
233 st->st_filename = NULL;
234 st->st_blocks = NULL;
235
236 if ((st->st_stack = PyList_New(0)) == NULL)
237 goto fail;
238 if ((st->st_blocks = PyDict_New()) == NULL)
239 goto fail;
240 st->st_cur = NULL;
241 st->st_private = NULL;
242 return st;
243 fail:
244 PySymtable_Free(st);
245 return NULL;
246 }
247
248 /* When compiling the use of C stack is probably going to be a lot
249 lighter than when executing Python code but still can overflow
250 and causing a Python crash if not checked (e.g. eval("()"*300000)).
251 Using the current recursion limit for the compiler seems too
252 restrictive (it caused at least one test to fail) so a factor is
253 used to allow deeper recursion when compiling an expression.
254
255 Using a scaling factor means this should automatically adjust when
256 the recursion limit is adjusted for small or large C stack allocations.
257 */
258 #define COMPILER_STACK_FRAME_SCALE 3
259
260 struct symtable *
PySymtable_BuildObject(mod_ty mod,PyObject * filename,PyFutureFeatures * future)261 PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
262 {
263 struct symtable *st = symtable_new();
264 asdl_seq *seq;
265 int i;
266 PyThreadState *tstate;
267 int recursion_limit = Py_GetRecursionLimit();
268 int starting_recursion_depth;
269
270 if (st == NULL)
271 return NULL;
272 if (filename == NULL) {
273 PySymtable_Free(st);
274 return NULL;
275 }
276 Py_INCREF(filename);
277 st->st_filename = filename;
278 st->st_future = future;
279
280 /* Setup recursion depth check counters */
281 tstate = _PyThreadState_GET();
282 if (!tstate) {
283 PySymtable_Free(st);
284 return NULL;
285 }
286 /* Be careful here to prevent overflow. */
287 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
288 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
289 st->recursion_depth = starting_recursion_depth;
290 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
291 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
292
293 /* Make the initial symbol information gathering pass */
294 if (!GET_IDENTIFIER(top) ||
295 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
296 PySymtable_Free(st);
297 return NULL;
298 }
299
300 st->st_top = st->st_cur;
301 switch (mod->kind) {
302 case Module_kind:
303 seq = mod->v.Module.body;
304 for (i = 0; i < asdl_seq_LEN(seq); i++)
305 if (!symtable_visit_stmt(st,
306 (stmt_ty)asdl_seq_GET(seq, i)))
307 goto error;
308 break;
309 case Expression_kind:
310 if (!symtable_visit_expr(st, mod->v.Expression.body))
311 goto error;
312 break;
313 case Interactive_kind:
314 seq = mod->v.Interactive.body;
315 for (i = 0; i < asdl_seq_LEN(seq); i++)
316 if (!symtable_visit_stmt(st,
317 (stmt_ty)asdl_seq_GET(seq, i)))
318 goto error;
319 break;
320 case FunctionType_kind:
321 PyErr_SetString(PyExc_RuntimeError,
322 "this compiler does not handle FunctionTypes");
323 goto error;
324 }
325 if (!symtable_exit_block(st)) {
326 PySymtable_Free(st);
327 return NULL;
328 }
329 /* Check that the recursion depth counting balanced correctly */
330 if (st->recursion_depth != starting_recursion_depth) {
331 PyErr_Format(PyExc_SystemError,
332 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
333 starting_recursion_depth, st->recursion_depth);
334 PySymtable_Free(st);
335 return NULL;
336 }
337 /* Make the second symbol analysis pass */
338 if (symtable_analyze(st))
339 return st;
340 PySymtable_Free(st);
341 return NULL;
342 error:
343 (void) symtable_exit_block(st);
344 PySymtable_Free(st);
345 return NULL;
346 }
347
348 struct symtable *
PySymtable_Build(mod_ty mod,const char * filename_str,PyFutureFeatures * future)349 PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
350 {
351 PyObject *filename;
352 struct symtable *st;
353 filename = PyUnicode_DecodeFSDefault(filename_str);
354 if (filename == NULL)
355 return NULL;
356 st = PySymtable_BuildObject(mod, filename, future);
357 Py_DECREF(filename);
358 return st;
359 }
360
361 void
PySymtable_Free(struct symtable * st)362 PySymtable_Free(struct symtable *st)
363 {
364 Py_XDECREF(st->st_filename);
365 Py_XDECREF(st->st_blocks);
366 Py_XDECREF(st->st_stack);
367 PyMem_Free((void *)st);
368 }
369
370 PySTEntryObject *
PySymtable_Lookup(struct symtable * st,void * key)371 PySymtable_Lookup(struct symtable *st, void *key)
372 {
373 PyObject *k, *v;
374
375 k = PyLong_FromVoidPtr(key);
376 if (k == NULL)
377 return NULL;
378 v = PyDict_GetItemWithError(st->st_blocks, k);
379 if (v) {
380 assert(PySTEntry_Check(v));
381 Py_INCREF(v);
382 }
383 else if (!PyErr_Occurred()) {
384 PyErr_SetString(PyExc_KeyError,
385 "unknown symbol table entry");
386 }
387
388 Py_DECREF(k);
389 return (PySTEntryObject *)v;
390 }
391
392 static long
_PyST_GetSymbol(PySTEntryObject * ste,PyObject * name)393 _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
394 {
395 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
396 if (!v)
397 return 0;
398 assert(PyLong_Check(v));
399 return PyLong_AS_LONG(v);
400 }
401
402 int
PyST_GetScope(PySTEntryObject * ste,PyObject * name)403 PyST_GetScope(PySTEntryObject *ste, PyObject *name)
404 {
405 long symbol = _PyST_GetSymbol(ste, name);
406 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
407 }
408
409 static int
error_at_directive(PySTEntryObject * ste,PyObject * name)410 error_at_directive(PySTEntryObject *ste, PyObject *name)
411 {
412 Py_ssize_t i;
413 PyObject *data;
414 assert(ste->ste_directives);
415 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
416 data = PyList_GET_ITEM(ste->ste_directives, i);
417 assert(PyTuple_CheckExact(data));
418 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
419 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
420 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
421 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
422 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
423
424 return 0;
425 }
426 }
427 PyErr_SetString(PyExc_RuntimeError,
428 "BUG: internal directive bookkeeping broken");
429 return 0;
430 }
431
432
433 /* Analyze raw symbol information to determine scope of each name.
434
435 The next several functions are helpers for symtable_analyze(),
436 which determines whether a name is local, global, or free. In addition,
437 it determines which local variables are cell variables; they provide
438 bindings that are used for free variables in enclosed blocks.
439
440 There are also two kinds of global variables, implicit and explicit. An
441 explicit global is declared with the global statement. An implicit
442 global is a free variable for which the compiler has found no binding
443 in an enclosing function scope. The implicit global is either a global
444 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
445 to handle these names to implement slightly odd semantics. In such a
446 block, the name is treated as global until it is assigned to; then it
447 is treated as a local.
448
449 The symbol table requires two passes to determine the scope of each name.
450 The first pass collects raw facts from the AST via the symtable_visit_*
451 functions: the name is a parameter here, the name is used but not defined
452 here, etc. The second pass analyzes these facts during a pass over the
453 PySTEntryObjects created during pass 1.
454
455 When a function is entered during the second pass, the parent passes
456 the set of all name bindings visible to its children. These bindings
457 are used to determine if non-local variables are free or implicit globals.
458 Names which are explicitly declared nonlocal must exist in this set of
459 visible names - if they do not, a syntax error is raised. After doing
460 the local analysis, it analyzes each of its child blocks using an
461 updated set of name bindings.
462
463 The children update the free variable set. If a local variable is added to
464 the free variable set by the child, the variable is marked as a cell. The
465 function object being defined must provide runtime storage for the variable
466 that may outlive the function's frame. Cell variables are removed from the
467 free set before the analyze function returns to its parent.
468
469 During analysis, the names are:
470 symbols: dict mapping from symbol names to flag values (including offset scope values)
471 scopes: dict mapping from symbol names to scope values (no offset)
472 local: set of all symbol names local to the current scope
473 bound: set of all symbol names local to a containing function scope
474 free: set of all symbol names referenced but not bound in child scopes
475 global: set of all symbol names explicitly declared as global
476 */
477
478 #define SET_SCOPE(DICT, NAME, I) { \
479 PyObject *o = PyLong_FromLong(I); \
480 if (!o) \
481 return 0; \
482 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
483 Py_DECREF(o); \
484 return 0; \
485 } \
486 Py_DECREF(o); \
487 }
488
489 /* Decide on scope of name, given flags.
490
491 The namespace dictionaries may be modified to record information
492 about the new name. For example, a new global will add an entry to
493 global. A name that was global can be changed to local.
494 */
495
496 static int
analyze_name(PySTEntryObject * ste,PyObject * scopes,PyObject * name,long flags,PyObject * bound,PyObject * local,PyObject * free,PyObject * global)497 analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
498 PyObject *bound, PyObject *local, PyObject *free,
499 PyObject *global)
500 {
501 if (flags & DEF_GLOBAL) {
502 if (flags & DEF_NONLOCAL) {
503 PyErr_Format(PyExc_SyntaxError,
504 "name '%U' is nonlocal and global",
505 name);
506 return error_at_directive(ste, name);
507 }
508 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
509 if (PySet_Add(global, name) < 0)
510 return 0;
511 if (bound && (PySet_Discard(bound, name) < 0))
512 return 0;
513 return 1;
514 }
515 if (flags & DEF_NONLOCAL) {
516 if (!bound) {
517 PyErr_Format(PyExc_SyntaxError,
518 "nonlocal declaration not allowed at module level");
519 return error_at_directive(ste, name);
520 }
521 if (!PySet_Contains(bound, name)) {
522 PyErr_Format(PyExc_SyntaxError,
523 "no binding for nonlocal '%U' found",
524 name);
525
526 return error_at_directive(ste, name);
527 }
528 SET_SCOPE(scopes, name, FREE);
529 ste->ste_free = 1;
530 return PySet_Add(free, name) >= 0;
531 }
532 if (flags & DEF_BOUND) {
533 SET_SCOPE(scopes, name, LOCAL);
534 if (PySet_Add(local, name) < 0)
535 return 0;
536 if (PySet_Discard(global, name) < 0)
537 return 0;
538 return 1;
539 }
540 /* If an enclosing block has a binding for this name, it
541 is a free variable rather than a global variable.
542 Note that having a non-NULL bound implies that the block
543 is nested.
544 */
545 if (bound && PySet_Contains(bound, name)) {
546 SET_SCOPE(scopes, name, FREE);
547 ste->ste_free = 1;
548 return PySet_Add(free, name) >= 0;
549 }
550 /* If a parent has a global statement, then call it global
551 explicit? It could also be global implicit.
552 */
553 if (global && PySet_Contains(global, name)) {
554 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
555 return 1;
556 }
557 if (ste->ste_nested)
558 ste->ste_free = 1;
559 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
560 return 1;
561 }
562
563 #undef SET_SCOPE
564
565 /* If a name is defined in free and also in locals, then this block
566 provides the binding for the free variable. The name should be
567 marked CELL in this block and removed from the free list.
568
569 Note that the current block's free variables are included in free.
570 That's safe because no name can be free and local in the same scope.
571 */
572
573 static int
analyze_cells(PyObject * scopes,PyObject * free)574 analyze_cells(PyObject *scopes, PyObject *free)
575 {
576 PyObject *name, *v, *v_cell;
577 int success = 0;
578 Py_ssize_t pos = 0;
579
580 v_cell = PyLong_FromLong(CELL);
581 if (!v_cell)
582 return 0;
583 while (PyDict_Next(scopes, &pos, &name, &v)) {
584 long scope;
585 assert(PyLong_Check(v));
586 scope = PyLong_AS_LONG(v);
587 if (scope != LOCAL)
588 continue;
589 if (!PySet_Contains(free, name))
590 continue;
591 /* Replace LOCAL with CELL for this name, and remove
592 from free. It is safe to replace the value of name
593 in the dict, because it will not cause a resize.
594 */
595 if (PyDict_SetItem(scopes, name, v_cell) < 0)
596 goto error;
597 if (PySet_Discard(free, name) < 0)
598 goto error;
599 }
600 success = 1;
601 error:
602 Py_DECREF(v_cell);
603 return success;
604 }
605
606 static int
drop_class_free(PySTEntryObject * ste,PyObject * free)607 drop_class_free(PySTEntryObject *ste, PyObject *free)
608 {
609 int res;
610 if (!GET_IDENTIFIER(__class__))
611 return 0;
612 res = PySet_Discard(free, __class__);
613 if (res < 0)
614 return 0;
615 if (res)
616 ste->ste_needs_class_closure = 1;
617 return 1;
618 }
619
620 /* Enter the final scope information into the ste_symbols dict.
621 *
622 * All arguments are dicts. Modifies symbols, others are read-only.
623 */
624 static int
update_symbols(PyObject * symbols,PyObject * scopes,PyObject * bound,PyObject * free,int classflag)625 update_symbols(PyObject *symbols, PyObject *scopes,
626 PyObject *bound, PyObject *free, int classflag)
627 {
628 PyObject *name = NULL, *itr = NULL;
629 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
630 Py_ssize_t pos = 0;
631
632 /* Update scope information for all symbols in this scope */
633 while (PyDict_Next(symbols, &pos, &name, &v)) {
634 long scope, flags;
635 assert(PyLong_Check(v));
636 flags = PyLong_AS_LONG(v);
637 v_scope = PyDict_GetItem(scopes, name);
638 assert(v_scope && PyLong_Check(v_scope));
639 scope = PyLong_AS_LONG(v_scope);
640 flags |= (scope << SCOPE_OFFSET);
641 v_new = PyLong_FromLong(flags);
642 if (!v_new)
643 return 0;
644 if (PyDict_SetItem(symbols, name, v_new) < 0) {
645 Py_DECREF(v_new);
646 return 0;
647 }
648 Py_DECREF(v_new);
649 }
650
651 /* Record not yet resolved free variables from children (if any) */
652 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
653 if (!v_free)
654 return 0;
655
656 itr = PyObject_GetIter(free);
657 if (itr == NULL) {
658 Py_DECREF(v_free);
659 return 0;
660 }
661
662 while ((name = PyIter_Next(itr))) {
663 v = PyDict_GetItemWithError(symbols, name);
664
665 /* Handle symbol that already exists in this scope */
666 if (v) {
667 /* Handle a free variable in a method of
668 the class that has the same name as a local
669 or global in the class scope.
670 */
671 if (classflag &&
672 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
673 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
674 v_new = PyLong_FromLong(flags);
675 if (!v_new) {
676 goto error;
677 }
678 if (PyDict_SetItem(symbols, name, v_new) < 0) {
679 Py_DECREF(v_new);
680 goto error;
681 }
682 Py_DECREF(v_new);
683 }
684 /* It's a cell, or already free in this scope */
685 Py_DECREF(name);
686 continue;
687 }
688 else if (PyErr_Occurred()) {
689 goto error;
690 }
691 /* Handle global symbol */
692 if (bound && !PySet_Contains(bound, name)) {
693 Py_DECREF(name);
694 continue; /* it's a global */
695 }
696 /* Propagate new free symbol up the lexical stack */
697 if (PyDict_SetItem(symbols, name, v_free) < 0) {
698 goto error;
699 }
700 Py_DECREF(name);
701 }
702 Py_DECREF(itr);
703 Py_DECREF(v_free);
704 return 1;
705 error:
706 Py_XDECREF(v_free);
707 Py_XDECREF(itr);
708 Py_XDECREF(name);
709 return 0;
710 }
711
712 /* Make final symbol table decisions for block of ste.
713
714 Arguments:
715 ste -- current symtable entry (input/output)
716 bound -- set of variables bound in enclosing scopes (input). bound
717 is NULL for module blocks.
718 free -- set of free variables in enclosed scopes (output)
719 globals -- set of declared global variables in enclosing scopes (input)
720
721 The implementation uses two mutually recursive functions,
722 analyze_block() and analyze_child_block(). analyze_block() is
723 responsible for analyzing the individual names defined in a block.
724 analyze_child_block() prepares temporary namespace dictionaries
725 used to evaluated nested blocks.
726
727 The two functions exist because a child block should see the name
728 bindings of its enclosing blocks, but those bindings should not
729 propagate back to a parent block.
730 */
731
732 static int
733 analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
734 PyObject *global, PyObject* child_free);
735
736 static int
analyze_block(PySTEntryObject * ste,PyObject * bound,PyObject * free,PyObject * global)737 analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
738 PyObject *global)
739 {
740 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
741 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
742 PyObject *temp;
743 int i, success = 0;
744 Py_ssize_t pos = 0;
745
746 local = PySet_New(NULL); /* collect new names bound in block */
747 if (!local)
748 goto error;
749 scopes = PyDict_New(); /* collect scopes defined for each name */
750 if (!scopes)
751 goto error;
752
753 /* Allocate new global and bound variable dictionaries. These
754 dictionaries hold the names visible in nested blocks. For
755 ClassBlocks, the bound and global names are initialized
756 before analyzing names, because class bindings aren't
757 visible in methods. For other blocks, they are initialized
758 after names are analyzed.
759 */
760
761 /* TODO(jhylton): Package these dicts in a struct so that we
762 can write reasonable helper functions?
763 */
764 newglobal = PySet_New(NULL);
765 if (!newglobal)
766 goto error;
767 newfree = PySet_New(NULL);
768 if (!newfree)
769 goto error;
770 newbound = PySet_New(NULL);
771 if (!newbound)
772 goto error;
773
774 /* Class namespace has no effect on names visible in
775 nested functions, so populate the global and bound
776 sets to be passed to child blocks before analyzing
777 this one.
778 */
779 if (ste->ste_type == ClassBlock) {
780 /* Pass down known globals */
781 temp = PyNumber_InPlaceOr(newglobal, global);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
785 /* Pass down previously bound symbols */
786 if (bound) {
787 temp = PyNumber_InPlaceOr(newbound, bound);
788 if (!temp)
789 goto error;
790 Py_DECREF(temp);
791 }
792 }
793
794 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
795 long flags = PyLong_AS_LONG(v);
796 if (!analyze_name(ste, scopes, name, flags,
797 bound, local, free, global))
798 goto error;
799 }
800
801 /* Populate global and bound sets to be passed to children. */
802 if (ste->ste_type != ClassBlock) {
803 /* Add function locals to bound set */
804 if (ste->ste_type == FunctionBlock) {
805 temp = PyNumber_InPlaceOr(newbound, local);
806 if (!temp)
807 goto error;
808 Py_DECREF(temp);
809 }
810 /* Pass down previously bound symbols */
811 if (bound) {
812 temp = PyNumber_InPlaceOr(newbound, bound);
813 if (!temp)
814 goto error;
815 Py_DECREF(temp);
816 }
817 /* Pass down known globals */
818 temp = PyNumber_InPlaceOr(newglobal, global);
819 if (!temp)
820 goto error;
821 Py_DECREF(temp);
822 }
823 else {
824 /* Special-case __class__ */
825 if (!GET_IDENTIFIER(__class__))
826 goto error;
827 if (PySet_Add(newbound, __class__) < 0)
828 goto error;
829 }
830
831 /* Recursively call analyze_child_block() on each child block.
832
833 newbound, newglobal now contain the names visible in
834 nested blocks. The free variables in the children will
835 be collected in allfree.
836 */
837 allfree = PySet_New(NULL);
838 if (!allfree)
839 goto error;
840 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
841 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
842 PySTEntryObject* entry;
843 assert(c && PySTEntry_Check(c));
844 entry = (PySTEntryObject*)c;
845 if (!analyze_child_block(entry, newbound, newfree, newglobal,
846 allfree))
847 goto error;
848 /* Check if any children have free variables */
849 if (entry->ste_free || entry->ste_child_free)
850 ste->ste_child_free = 1;
851 }
852
853 temp = PyNumber_InPlaceOr(newfree, allfree);
854 if (!temp)
855 goto error;
856 Py_DECREF(temp);
857
858 /* Check if any local variables must be converted to cell variables */
859 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
860 goto error;
861 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
862 goto error;
863 /* Records the results of the analysis in the symbol table entry */
864 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
865 ste->ste_type == ClassBlock))
866 goto error;
867
868 temp = PyNumber_InPlaceOr(free, newfree);
869 if (!temp)
870 goto error;
871 Py_DECREF(temp);
872 success = 1;
873 error:
874 Py_XDECREF(scopes);
875 Py_XDECREF(local);
876 Py_XDECREF(newbound);
877 Py_XDECREF(newglobal);
878 Py_XDECREF(newfree);
879 Py_XDECREF(allfree);
880 if (!success)
881 assert(PyErr_Occurred());
882 return success;
883 }
884
885 static int
analyze_child_block(PySTEntryObject * entry,PyObject * bound,PyObject * free,PyObject * global,PyObject * child_free)886 analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
887 PyObject *global, PyObject* child_free)
888 {
889 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
890 PyObject *temp;
891
892 /* Copy the bound and global dictionaries.
893
894 These dictionaries are used by all blocks enclosed by the
895 current block. The analyze_block() call modifies these
896 dictionaries.
897
898 */
899 temp_bound = PySet_New(bound);
900 if (!temp_bound)
901 goto error;
902 temp_free = PySet_New(free);
903 if (!temp_free)
904 goto error;
905 temp_global = PySet_New(global);
906 if (!temp_global)
907 goto error;
908
909 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
910 goto error;
911 temp = PyNumber_InPlaceOr(child_free, temp_free);
912 if (!temp)
913 goto error;
914 Py_DECREF(temp);
915 Py_DECREF(temp_bound);
916 Py_DECREF(temp_free);
917 Py_DECREF(temp_global);
918 return 1;
919 error:
920 Py_XDECREF(temp_bound);
921 Py_XDECREF(temp_free);
922 Py_XDECREF(temp_global);
923 return 0;
924 }
925
926 static int
symtable_analyze(struct symtable * st)927 symtable_analyze(struct symtable *st)
928 {
929 PyObject *free, *global;
930 int r;
931
932 free = PySet_New(NULL);
933 if (!free)
934 return 0;
935 global = PySet_New(NULL);
936 if (!global) {
937 Py_DECREF(free);
938 return 0;
939 }
940 r = analyze_block(st->st_top, NULL, free, global);
941 Py_DECREF(free);
942 Py_DECREF(global);
943 return r;
944 }
945
946 /* symtable_enter_block() gets a reference via ste_new.
947 This reference is released when the block is exited, via the DECREF
948 in symtable_exit_block().
949 */
950
951 static int
symtable_exit_block(struct symtable * st)952 symtable_exit_block(struct symtable *st)
953 {
954 Py_ssize_t size;
955
956 st->st_cur = NULL;
957 size = PyList_GET_SIZE(st->st_stack);
958 if (size) {
959 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
960 return 0;
961 if (--size)
962 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
963 }
964 return 1;
965 }
966
967 static int
symtable_enter_block(struct symtable * st,identifier name,_Py_block_ty block,void * ast,int lineno,int col_offset)968 symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
969 void *ast, int lineno, int col_offset)
970 {
971 PySTEntryObject *prev = NULL, *ste;
972
973 ste = ste_new(st, name, block, ast, lineno, col_offset);
974 if (ste == NULL)
975 return 0;
976 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
977 Py_DECREF(ste);
978 return 0;
979 }
980 prev = st->st_cur;
981 /* bpo-37757: For now, disallow *all* assignment expressions in the
982 * outermost iterator expression of a comprehension, even those inside
983 * a nested comprehension or a lambda expression.
984 */
985 if (prev) {
986 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
987 }
988 /* The entry is owned by the stack. Borrow it for st_cur. */
989 Py_DECREF(ste);
990 st->st_cur = ste;
991 if (block == ModuleBlock)
992 st->st_global = st->st_cur->ste_symbols;
993 if (prev) {
994 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
995 return 0;
996 }
997 }
998 return 1;
999 }
1000
1001 static long
symtable_lookup(struct symtable * st,PyObject * name)1002 symtable_lookup(struct symtable *st, PyObject *name)
1003 {
1004 PyObject *mangled = _Py_Mangle(st->st_private, name);
1005 if (!mangled)
1006 return 0;
1007 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1008 Py_DECREF(mangled);
1009 return ret;
1010 }
1011
1012 static int
symtable_add_def_helper(struct symtable * st,PyObject * name,int flag,struct _symtable_entry * ste)1013 symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
1014 {
1015 PyObject *o;
1016 PyObject *dict;
1017 long val;
1018 PyObject *mangled = _Py_Mangle(st->st_private, name);
1019
1020
1021 if (!mangled)
1022 return 0;
1023 dict = ste->ste_symbols;
1024 if ((o = PyDict_GetItemWithError(dict, mangled))) {
1025 val = PyLong_AS_LONG(o);
1026 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1027 /* Is it better to use 'mangled' or 'name' here? */
1028 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
1029 PyErr_SyntaxLocationObject(st->st_filename,
1030 ste->ste_lineno,
1031 ste->ste_col_offset + 1);
1032 goto error;
1033 }
1034 val |= flag;
1035 }
1036 else if (PyErr_Occurred()) {
1037 goto error;
1038 }
1039 else {
1040 val = flag;
1041 }
1042 if (ste->ste_comp_iter_target) {
1043 /* This name is an iteration variable in a comprehension,
1044 * so check for a binding conflict with any named expressions.
1045 * Otherwise, mark it as an iteration variable so subsequent
1046 * named expressions can check for conflicts.
1047 */
1048 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1049 PyErr_Format(PyExc_SyntaxError,
1050 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1051 PyErr_SyntaxLocationObject(st->st_filename,
1052 ste->ste_lineno,
1053 ste->ste_col_offset + 1);
1054 goto error;
1055 }
1056 val |= DEF_COMP_ITER;
1057 }
1058 o = PyLong_FromLong(val);
1059 if (o == NULL)
1060 goto error;
1061 if (PyDict_SetItem(dict, mangled, o) < 0) {
1062 Py_DECREF(o);
1063 goto error;
1064 }
1065 Py_DECREF(o);
1066
1067 if (flag & DEF_PARAM) {
1068 if (PyList_Append(ste->ste_varnames, mangled) < 0)
1069 goto error;
1070 } else if (flag & DEF_GLOBAL) {
1071 /* XXX need to update DEF_GLOBAL for other flags too;
1072 perhaps only DEF_FREE_GLOBAL */
1073 val = flag;
1074 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1075 val |= PyLong_AS_LONG(o);
1076 }
1077 o = PyLong_FromLong(val);
1078 if (o == NULL)
1079 goto error;
1080 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1081 Py_DECREF(o);
1082 goto error;
1083 }
1084 Py_DECREF(o);
1085 }
1086 Py_DECREF(mangled);
1087 return 1;
1088
1089 error:
1090 Py_DECREF(mangled);
1091 return 0;
1092 }
1093
1094 static int
symtable_add_def(struct symtable * st,PyObject * name,int flag)1095 symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1096 return symtable_add_def_helper(st, name, flag, st->st_cur);
1097 }
1098
1099 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1100 They use the ASDL name to synthesize the name of the C type and the visit
1101 function.
1102
1103 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1104 useful if the first node in the sequence requires special treatment.
1105
1106 VISIT_QUIT macro returns the specified value exiting from the function but
1107 first adjusts current recursion counter depth.
1108 */
1109
1110 #define VISIT_QUIT(ST, X) \
1111 return --(ST)->recursion_depth,(X)
1112
1113 #define VISIT(ST, TYPE, V) \
1114 if (!symtable_visit_ ## TYPE((ST), (V))) \
1115 VISIT_QUIT((ST), 0);
1116
1117 #define VISIT_SEQ(ST, TYPE, SEQ) { \
1118 int i; \
1119 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1120 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1121 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1122 if (!symtable_visit_ ## TYPE((ST), elt)) \
1123 VISIT_QUIT((ST), 0); \
1124 } \
1125 }
1126
1127 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1128 int i; \
1129 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1130 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1131 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1132 if (!symtable_visit_ ## TYPE((ST), elt)) \
1133 VISIT_QUIT((ST), 0); \
1134 } \
1135 }
1136
1137 #define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
1138 int i = 0; \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1142 if (!elt) continue; /* can be NULL */ \
1143 if (!symtable_visit_ ## TYPE((ST), elt)) \
1144 VISIT_QUIT((ST), 0); \
1145 } \
1146 }
1147
1148 static int
symtable_record_directive(struct symtable * st,identifier name,int lineno,int col_offset)1149 symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
1150 {
1151 PyObject *data, *mangled;
1152 int res;
1153 if (!st->st_cur->ste_directives) {
1154 st->st_cur->ste_directives = PyList_New(0);
1155 if (!st->st_cur->ste_directives)
1156 return 0;
1157 }
1158 mangled = _Py_Mangle(st->st_private, name);
1159 if (!mangled)
1160 return 0;
1161 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
1162 if (!data)
1163 return 0;
1164 res = PyList_Append(st->st_cur->ste_directives, data);
1165 Py_DECREF(data);
1166 return res == 0;
1167 }
1168
1169
1170 static int
symtable_visit_stmt(struct symtable * st,stmt_ty s)1171 symtable_visit_stmt(struct symtable *st, stmt_ty s)
1172 {
1173 if (++st->recursion_depth > st->recursion_limit) {
1174 PyErr_SetString(PyExc_RecursionError,
1175 "maximum recursion depth exceeded during compilation");
1176 VISIT_QUIT(st, 0);
1177 }
1178 switch (s->kind) {
1179 case FunctionDef_kind:
1180 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1181 VISIT_QUIT(st, 0);
1182 if (s->v.FunctionDef.args->defaults)
1183 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1184 if (s->v.FunctionDef.args->kw_defaults)
1185 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
1186 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
1187 s->v.FunctionDef.returns))
1188 VISIT_QUIT(st, 0);
1189 if (s->v.FunctionDef.decorator_list)
1190 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1191 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1192 FunctionBlock, (void *)s, s->lineno,
1193 s->col_offset))
1194 VISIT_QUIT(st, 0);
1195 VISIT(st, arguments, s->v.FunctionDef.args);
1196 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
1197 if (!symtable_exit_block(st))
1198 VISIT_QUIT(st, 0);
1199 break;
1200 case ClassDef_kind: {
1201 PyObject *tmp;
1202 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1203 VISIT_QUIT(st, 0);
1204 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1205 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1206 if (s->v.ClassDef.decorator_list)
1207 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1208 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1209 (void *)s, s->lineno, s->col_offset))
1210 VISIT_QUIT(st, 0);
1211 tmp = st->st_private;
1212 st->st_private = s->v.ClassDef.name;
1213 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
1214 st->st_private = tmp;
1215 if (!symtable_exit_block(st))
1216 VISIT_QUIT(st, 0);
1217 break;
1218 }
1219 case Return_kind:
1220 if (s->v.Return.value) {
1221 VISIT(st, expr, s->v.Return.value);
1222 st->st_cur->ste_returns_value = 1;
1223 }
1224 break;
1225 case Delete_kind:
1226 VISIT_SEQ(st, expr, s->v.Delete.targets);
1227 break;
1228 case Assign_kind:
1229 VISIT_SEQ(st, expr, s->v.Assign.targets);
1230 VISIT(st, expr, s->v.Assign.value);
1231 break;
1232 case AnnAssign_kind:
1233 if (s->v.AnnAssign.target->kind == Name_kind) {
1234 expr_ty e_name = s->v.AnnAssign.target;
1235 long cur = symtable_lookup(st, e_name->v.Name.id);
1236 if (cur < 0) {
1237 VISIT_QUIT(st, 0);
1238 }
1239 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1240 && (st->st_cur->ste_symbols != st->st_global)
1241 && s->v.AnnAssign.simple) {
1242 PyErr_Format(PyExc_SyntaxError,
1243 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1244 e_name->v.Name.id);
1245 PyErr_SyntaxLocationObject(st->st_filename,
1246 s->lineno,
1247 s->col_offset + 1);
1248 VISIT_QUIT(st, 0);
1249 }
1250 if (s->v.AnnAssign.simple &&
1251 !symtable_add_def(st, e_name->v.Name.id,
1252 DEF_ANNOT | DEF_LOCAL)) {
1253 VISIT_QUIT(st, 0);
1254 }
1255 else {
1256 if (s->v.AnnAssign.value
1257 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1258 VISIT_QUIT(st, 0);
1259 }
1260 }
1261 }
1262 else {
1263 VISIT(st, expr, s->v.AnnAssign.target);
1264 }
1265 VISIT(st, expr, s->v.AnnAssign.annotation);
1266 if (s->v.AnnAssign.value) {
1267 VISIT(st, expr, s->v.AnnAssign.value);
1268 }
1269 break;
1270 case AugAssign_kind:
1271 VISIT(st, expr, s->v.AugAssign.target);
1272 VISIT(st, expr, s->v.AugAssign.value);
1273 break;
1274 case For_kind:
1275 VISIT(st, expr, s->v.For.target);
1276 VISIT(st, expr, s->v.For.iter);
1277 VISIT_SEQ(st, stmt, s->v.For.body);
1278 if (s->v.For.orelse)
1279 VISIT_SEQ(st, stmt, s->v.For.orelse);
1280 break;
1281 case While_kind:
1282 VISIT(st, expr, s->v.While.test);
1283 VISIT_SEQ(st, stmt, s->v.While.body);
1284 if (s->v.While.orelse)
1285 VISIT_SEQ(st, stmt, s->v.While.orelse);
1286 break;
1287 case If_kind:
1288 /* XXX if 0: and lookup_yield() hacks */
1289 VISIT(st, expr, s->v.If.test);
1290 VISIT_SEQ(st, stmt, s->v.If.body);
1291 if (s->v.If.orelse)
1292 VISIT_SEQ(st, stmt, s->v.If.orelse);
1293 break;
1294 case Raise_kind:
1295 if (s->v.Raise.exc) {
1296 VISIT(st, expr, s->v.Raise.exc);
1297 if (s->v.Raise.cause) {
1298 VISIT(st, expr, s->v.Raise.cause);
1299 }
1300 }
1301 break;
1302 case Try_kind:
1303 VISIT_SEQ(st, stmt, s->v.Try.body);
1304 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1305 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1306 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
1307 break;
1308 case Assert_kind:
1309 VISIT(st, expr, s->v.Assert.test);
1310 if (s->v.Assert.msg)
1311 VISIT(st, expr, s->v.Assert.msg);
1312 break;
1313 case Import_kind:
1314 VISIT_SEQ(st, alias, s->v.Import.names);
1315 break;
1316 case ImportFrom_kind:
1317 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1318 break;
1319 case Global_kind: {
1320 int i;
1321 asdl_seq *seq = s->v.Global.names;
1322 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1323 identifier name = (identifier)asdl_seq_GET(seq, i);
1324 long cur = symtable_lookup(st, name);
1325 if (cur < 0)
1326 VISIT_QUIT(st, 0);
1327 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1328 const char* msg;
1329 if (cur & DEF_PARAM) {
1330 msg = GLOBAL_PARAM;
1331 } else if (cur & USE) {
1332 msg = GLOBAL_AFTER_USE;
1333 } else if (cur & DEF_ANNOT) {
1334 msg = GLOBAL_ANNOT;
1335 } else { /* DEF_LOCAL */
1336 msg = GLOBAL_AFTER_ASSIGN;
1337 }
1338 PyErr_Format(PyExc_SyntaxError,
1339 msg, name);
1340 PyErr_SyntaxLocationObject(st->st_filename,
1341 s->lineno,
1342 s->col_offset + 1);
1343 VISIT_QUIT(st, 0);
1344 }
1345 if (!symtable_add_def(st, name, DEF_GLOBAL))
1346 VISIT_QUIT(st, 0);
1347 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
1348 VISIT_QUIT(st, 0);
1349 }
1350 break;
1351 }
1352 case Nonlocal_kind: {
1353 int i;
1354 asdl_seq *seq = s->v.Nonlocal.names;
1355 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1356 identifier name = (identifier)asdl_seq_GET(seq, i);
1357 long cur = symtable_lookup(st, name);
1358 if (cur < 0)
1359 VISIT_QUIT(st, 0);
1360 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1361 const char* msg;
1362 if (cur & DEF_PARAM) {
1363 msg = NONLOCAL_PARAM;
1364 } else if (cur & USE) {
1365 msg = NONLOCAL_AFTER_USE;
1366 } else if (cur & DEF_ANNOT) {
1367 msg = NONLOCAL_ANNOT;
1368 } else { /* DEF_LOCAL */
1369 msg = NONLOCAL_AFTER_ASSIGN;
1370 }
1371 PyErr_Format(PyExc_SyntaxError, msg, name);
1372 PyErr_SyntaxLocationObject(st->st_filename,
1373 s->lineno,
1374 s->col_offset + 1);
1375 VISIT_QUIT(st, 0);
1376 }
1377 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1378 VISIT_QUIT(st, 0);
1379 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
1380 VISIT_QUIT(st, 0);
1381 }
1382 break;
1383 }
1384 case Expr_kind:
1385 VISIT(st, expr, s->v.Expr.value);
1386 break;
1387 case Pass_kind:
1388 case Break_kind:
1389 case Continue_kind:
1390 /* nothing to do here */
1391 break;
1392 case With_kind:
1393 VISIT_SEQ(st, withitem, s->v.With.items);
1394 VISIT_SEQ(st, stmt, s->v.With.body);
1395 break;
1396 case AsyncFunctionDef_kind:
1397 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1398 VISIT_QUIT(st, 0);
1399 if (s->v.AsyncFunctionDef.args->defaults)
1400 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1401 if (s->v.AsyncFunctionDef.args->kw_defaults)
1402 VISIT_SEQ_WITH_NULL(st, expr,
1403 s->v.AsyncFunctionDef.args->kw_defaults);
1404 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
1405 s->v.AsyncFunctionDef.returns))
1406 VISIT_QUIT(st, 0);
1407 if (s->v.AsyncFunctionDef.decorator_list)
1408 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1409 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1410 FunctionBlock, (void *)s, s->lineno,
1411 s->col_offset))
1412 VISIT_QUIT(st, 0);
1413 st->st_cur->ste_coroutine = 1;
1414 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1415 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1416 if (!symtable_exit_block(st))
1417 VISIT_QUIT(st, 0);
1418 break;
1419 case AsyncWith_kind:
1420 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1421 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1422 break;
1423 case AsyncFor_kind:
1424 VISIT(st, expr, s->v.AsyncFor.target);
1425 VISIT(st, expr, s->v.AsyncFor.iter);
1426 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1427 if (s->v.AsyncFor.orelse)
1428 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1429 break;
1430 }
1431 VISIT_QUIT(st, 1);
1432 }
1433
1434 static int
symtable_extend_namedexpr_scope(struct symtable * st,expr_ty e)1435 symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1436 {
1437 assert(st->st_stack);
1438 assert(e->kind == Name_kind);
1439
1440 PyObject *target_name = e->v.Name.id;
1441 Py_ssize_t i, size;
1442 struct _symtable_entry *ste;
1443 size = PyList_GET_SIZE(st->st_stack);
1444 assert(size);
1445
1446 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1447 for (i = size - 1; i >= 0; i--) {
1448 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1449
1450 /* If we find a comprehension scope, check for a target
1451 * binding conflict with iteration variables, otherwise skip it
1452 */
1453 if (ste->ste_comprehension) {
1454 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1455 if (target_in_scope & DEF_COMP_ITER) {
1456 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1457 PyErr_SyntaxLocationObject(st->st_filename,
1458 e->lineno,
1459 e->col_offset);
1460 VISIT_QUIT(st, 0);
1461 }
1462 continue;
1463 }
1464
1465 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
1466 if (ste->ste_type == FunctionBlock) {
1467 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1468 if (target_in_scope & DEF_GLOBAL) {
1469 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1470 VISIT_QUIT(st, 0);
1471 } else {
1472 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1473 VISIT_QUIT(st, 0);
1474 }
1475 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
1476 VISIT_QUIT(st, 0);
1477
1478 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
1479 }
1480 /* If we find a ModuleBlock entry, add as GLOBAL */
1481 if (ste->ste_type == ModuleBlock) {
1482 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1483 VISIT_QUIT(st, 0);
1484 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
1485 VISIT_QUIT(st, 0);
1486
1487 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
1488 }
1489 /* Disallow usage in ClassBlock */
1490 if (ste->ste_type == ClassBlock) {
1491 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
1492 PyErr_SyntaxLocationObject(st->st_filename,
1493 e->lineno,
1494 e->col_offset);
1495 VISIT_QUIT(st, 0);
1496 }
1497 }
1498
1499 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1500 and should never fall to this case
1501 */
1502 assert(0);
1503 return 0;
1504 }
1505
1506 static int
symtable_handle_namedexpr(struct symtable * st,expr_ty e)1507 symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1508 {
1509 if (st->st_cur->ste_comp_iter_expr > 0) {
1510 /* Assignment isn't allowed in a comprehension iterable expression */
1511 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1512 PyErr_SyntaxLocationObject(st->st_filename,
1513 e->lineno,
1514 e->col_offset);
1515 return 0;
1516 }
1517 if (st->st_cur->ste_comprehension) {
1518 /* Inside a comprehension body, so find the right target scope */
1519 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
1520 return 0;
1521 }
1522 VISIT(st, expr, e->v.NamedExpr.value);
1523 VISIT(st, expr, e->v.NamedExpr.target);
1524 return 1;
1525 }
1526
1527 static int
symtable_visit_expr(struct symtable * st,expr_ty e)1528 symtable_visit_expr(struct symtable *st, expr_ty e)
1529 {
1530 if (++st->recursion_depth > st->recursion_limit) {
1531 PyErr_SetString(PyExc_RecursionError,
1532 "maximum recursion depth exceeded during compilation");
1533 VISIT_QUIT(st, 0);
1534 }
1535 switch (e->kind) {
1536 case NamedExpr_kind:
1537 if(!symtable_handle_namedexpr(st, e))
1538 VISIT_QUIT(st, 0);
1539 break;
1540 case BoolOp_kind:
1541 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1542 break;
1543 case BinOp_kind:
1544 VISIT(st, expr, e->v.BinOp.left);
1545 VISIT(st, expr, e->v.BinOp.right);
1546 break;
1547 case UnaryOp_kind:
1548 VISIT(st, expr, e->v.UnaryOp.operand);
1549 break;
1550 case Lambda_kind: {
1551 if (!GET_IDENTIFIER(lambda))
1552 VISIT_QUIT(st, 0);
1553 if (e->v.Lambda.args->defaults)
1554 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1555 if (e->v.Lambda.args->kw_defaults)
1556 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
1557 if (!symtable_enter_block(st, lambda,
1558 FunctionBlock, (void *)e, e->lineno,
1559 e->col_offset))
1560 VISIT_QUIT(st, 0);
1561 VISIT(st, arguments, e->v.Lambda.args);
1562 VISIT(st, expr, e->v.Lambda.body);
1563 if (!symtable_exit_block(st))
1564 VISIT_QUIT(st, 0);
1565 break;
1566 }
1567 case IfExp_kind:
1568 VISIT(st, expr, e->v.IfExp.test);
1569 VISIT(st, expr, e->v.IfExp.body);
1570 VISIT(st, expr, e->v.IfExp.orelse);
1571 break;
1572 case Dict_kind:
1573 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
1574 VISIT_SEQ(st, expr, e->v.Dict.values);
1575 break;
1576 case Set_kind:
1577 VISIT_SEQ(st, expr, e->v.Set.elts);
1578 break;
1579 case GeneratorExp_kind:
1580 if (!symtable_visit_genexp(st, e))
1581 VISIT_QUIT(st, 0);
1582 break;
1583 case ListComp_kind:
1584 if (!symtable_visit_listcomp(st, e))
1585 VISIT_QUIT(st, 0);
1586 break;
1587 case SetComp_kind:
1588 if (!symtable_visit_setcomp(st, e))
1589 VISIT_QUIT(st, 0);
1590 break;
1591 case DictComp_kind:
1592 if (!symtable_visit_dictcomp(st, e))
1593 VISIT_QUIT(st, 0);
1594 break;
1595 case Yield_kind:
1596 if (e->v.Yield.value)
1597 VISIT(st, expr, e->v.Yield.value);
1598 st->st_cur->ste_generator = 1;
1599 break;
1600 case YieldFrom_kind:
1601 VISIT(st, expr, e->v.YieldFrom.value);
1602 st->st_cur->ste_generator = 1;
1603 break;
1604 case Await_kind:
1605 VISIT(st, expr, e->v.Await.value);
1606 st->st_cur->ste_coroutine = 1;
1607 break;
1608 case Compare_kind:
1609 VISIT(st, expr, e->v.Compare.left);
1610 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1611 break;
1612 case Call_kind:
1613 VISIT(st, expr, e->v.Call.func);
1614 VISIT_SEQ(st, expr, e->v.Call.args);
1615 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
1616 break;
1617 case FormattedValue_kind:
1618 VISIT(st, expr, e->v.FormattedValue.value);
1619 if (e->v.FormattedValue.format_spec)
1620 VISIT(st, expr, e->v.FormattedValue.format_spec);
1621 break;
1622 case JoinedStr_kind:
1623 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1624 break;
1625 case Constant_kind:
1626 /* Nothing to do here. */
1627 break;
1628 /* The following exprs can be assignment targets. */
1629 case Attribute_kind:
1630 VISIT(st, expr, e->v.Attribute.value);
1631 break;
1632 case Subscript_kind:
1633 VISIT(st, expr, e->v.Subscript.value);
1634 VISIT(st, expr, e->v.Subscript.slice);
1635 break;
1636 case Starred_kind:
1637 VISIT(st, expr, e->v.Starred.value);
1638 break;
1639 case Slice_kind:
1640 if (e->v.Slice.lower)
1641 VISIT(st, expr, e->v.Slice.lower)
1642 if (e->v.Slice.upper)
1643 VISIT(st, expr, e->v.Slice.upper)
1644 if (e->v.Slice.step)
1645 VISIT(st, expr, e->v.Slice.step)
1646 break;
1647 case Name_kind:
1648 if (!symtable_add_def(st, e->v.Name.id,
1649 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1650 VISIT_QUIT(st, 0);
1651 /* Special-case super: it counts as a use of __class__ */
1652 if (e->v.Name.ctx == Load &&
1653 st->st_cur->ste_type == FunctionBlock &&
1654 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
1655 if (!GET_IDENTIFIER(__class__) ||
1656 !symtable_add_def(st, __class__, USE))
1657 VISIT_QUIT(st, 0);
1658 }
1659 break;
1660 /* child nodes of List and Tuple will have expr_context set */
1661 case List_kind:
1662 VISIT_SEQ(st, expr, e->v.List.elts);
1663 break;
1664 case Tuple_kind:
1665 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1666 break;
1667 }
1668 VISIT_QUIT(st, 1);
1669 }
1670
1671 static int
symtable_implicit_arg(struct symtable * st,int pos)1672 symtable_implicit_arg(struct symtable *st, int pos)
1673 {
1674 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1675 if (id == NULL)
1676 return 0;
1677 if (!symtable_add_def(st, id, DEF_PARAM)) {
1678 Py_DECREF(id);
1679 return 0;
1680 }
1681 Py_DECREF(id);
1682 return 1;
1683 }
1684
1685 static int
symtable_visit_params(struct symtable * st,asdl_seq * args)1686 symtable_visit_params(struct symtable *st, asdl_seq *args)
1687 {
1688 int i;
1689
1690 if (!args)
1691 return -1;
1692
1693 for (i = 0; i < asdl_seq_LEN(args); i++) {
1694 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1695 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1696 return 0;
1697 }
1698
1699 return 1;
1700 }
1701
1702 static int
symtable_visit_argannotations(struct symtable * st,asdl_seq * args)1703 symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
1704 {
1705 int i;
1706
1707 if (!args)
1708 return -1;
1709
1710 for (i = 0; i < asdl_seq_LEN(args); i++) {
1711 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1712 if (arg->annotation)
1713 VISIT(st, expr, arg->annotation);
1714 }
1715
1716 return 1;
1717 }
1718
1719 static int
symtable_visit_annotations(struct symtable * st,arguments_ty a,expr_ty returns)1720 symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
1721 {
1722 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1723 return 0;
1724 if (a->args && !symtable_visit_argannotations(st, a->args))
1725 return 0;
1726 if (a->vararg && a->vararg->annotation)
1727 VISIT(st, expr, a->vararg->annotation);
1728 if (a->kwarg && a->kwarg->annotation)
1729 VISIT(st, expr, a->kwarg->annotation);
1730 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1731 return 0;
1732 if (returns)
1733 VISIT(st, expr, returns);
1734 return 1;
1735 }
1736
1737 static int
symtable_visit_arguments(struct symtable * st,arguments_ty a)1738 symtable_visit_arguments(struct symtable *st, arguments_ty a)
1739 {
1740 /* skip default arguments inside function block
1741 XXX should ast be different?
1742 */
1743 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1744 return 0;
1745 if (a->args && !symtable_visit_params(st, a->args))
1746 return 0;
1747 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1748 return 0;
1749 if (a->vararg) {
1750 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
1751 return 0;
1752 st->st_cur->ste_varargs = 1;
1753 }
1754 if (a->kwarg) {
1755 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
1756 return 0;
1757 st->st_cur->ste_varkeywords = 1;
1758 }
1759 return 1;
1760 }
1761
1762
1763 static int
symtable_visit_excepthandler(struct symtable * st,excepthandler_ty eh)1764 symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1765 {
1766 if (eh->v.ExceptHandler.type)
1767 VISIT(st, expr, eh->v.ExceptHandler.type);
1768 if (eh->v.ExceptHandler.name)
1769 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1770 return 0;
1771 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1772 return 1;
1773 }
1774
1775 static int
symtable_visit_withitem(struct symtable * st,withitem_ty item)1776 symtable_visit_withitem(struct symtable *st, withitem_ty item)
1777 {
1778 VISIT(st, expr, item->context_expr);
1779 if (item->optional_vars) {
1780 VISIT(st, expr, item->optional_vars);
1781 }
1782 return 1;
1783 }
1784
1785
1786 static int
symtable_visit_alias(struct symtable * st,alias_ty a)1787 symtable_visit_alias(struct symtable *st, alias_ty a)
1788 {
1789 /* Compute store_name, the name actually bound by the import
1790 operation. It is different than a->name when a->name is a
1791 dotted package name (e.g. spam.eggs)
1792 */
1793 PyObject *store_name;
1794 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1795 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1796 PyUnicode_GET_LENGTH(name), 1);
1797 if (dot != -1) {
1798 store_name = PyUnicode_Substring(name, 0, dot);
1799 if (!store_name)
1800 return 0;
1801 }
1802 else {
1803 store_name = name;
1804 Py_INCREF(store_name);
1805 }
1806 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
1807 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1808 Py_DECREF(store_name);
1809 return r;
1810 }
1811 else {
1812 if (st->st_cur->ste_type != ModuleBlock) {
1813 int lineno = st->st_cur->ste_lineno;
1814 int col_offset = st->st_cur->ste_col_offset;
1815 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1816 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
1817 Py_DECREF(store_name);
1818 return 0;
1819 }
1820 Py_DECREF(store_name);
1821 return 1;
1822 }
1823 }
1824
1825
1826 static int
symtable_visit_comprehension(struct symtable * st,comprehension_ty lc)1827 symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1828 {
1829 st->st_cur->ste_comp_iter_target = 1;
1830 VISIT(st, expr, lc->target);
1831 st->st_cur->ste_comp_iter_target = 0;
1832 st->st_cur->ste_comp_iter_expr++;
1833 VISIT(st, expr, lc->iter);
1834 st->st_cur->ste_comp_iter_expr--;
1835 VISIT_SEQ(st, expr, lc->ifs);
1836 if (lc->is_async) {
1837 st->st_cur->ste_coroutine = 1;
1838 }
1839 return 1;
1840 }
1841
1842
1843 static int
symtable_visit_keyword(struct symtable * st,keyword_ty k)1844 symtable_visit_keyword(struct symtable *st, keyword_ty k)
1845 {
1846 VISIT(st, expr, k->value);
1847 return 1;
1848 }
1849
1850
1851 static int
symtable_handle_comprehension(struct symtable * st,expr_ty e,identifier scope_name,asdl_seq * generators,expr_ty elt,expr_ty value)1852 symtable_handle_comprehension(struct symtable *st, expr_ty e,
1853 identifier scope_name, asdl_seq *generators,
1854 expr_ty elt, expr_ty value)
1855 {
1856 int is_generator = (e->kind == GeneratorExp_kind);
1857 comprehension_ty outermost = ((comprehension_ty)
1858 asdl_seq_GET(generators, 0));
1859 /* Outermost iterator is evaluated in current scope */
1860 st->st_cur->ste_comp_iter_expr++;
1861 VISIT(st, expr, outermost->iter);
1862 st->st_cur->ste_comp_iter_expr--;
1863 /* Create comprehension scope for the rest */
1864 if (!scope_name ||
1865 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1866 e->lineno, e->col_offset)) {
1867 return 0;
1868 }
1869 if (outermost->is_async) {
1870 st->st_cur->ste_coroutine = 1;
1871 }
1872 st->st_cur->ste_comprehension = 1;
1873
1874 /* Outermost iter is received as an argument */
1875 if (!symtable_implicit_arg(st, 0)) {
1876 symtable_exit_block(st);
1877 return 0;
1878 }
1879 /* Visit iteration variable target, and mark them as such */
1880 st->st_cur->ste_comp_iter_target = 1;
1881 VISIT(st, expr, outermost->target);
1882 st->st_cur->ste_comp_iter_target = 0;
1883 /* Visit the rest of the comprehension body */
1884 VISIT_SEQ(st, expr, outermost->ifs);
1885 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
1886 if (value)
1887 VISIT(st, expr, value);
1888 VISIT(st, expr, elt);
1889 if (st->st_cur->ste_generator) {
1890 PyErr_SetString(PyExc_SyntaxError,
1891 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1892 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1893 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1894 "'yield' inside generator expression");
1895 PyErr_SyntaxLocationObject(st->st_filename,
1896 st->st_cur->ste_lineno,
1897 st->st_cur->ste_col_offset + 1);
1898 symtable_exit_block(st);
1899 return 0;
1900 }
1901 st->st_cur->ste_generator = is_generator;
1902 return symtable_exit_block(st);
1903 }
1904
1905 static int
symtable_visit_genexp(struct symtable * st,expr_ty e)1906 symtable_visit_genexp(struct symtable *st, expr_ty e)
1907 {
1908 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1909 e->v.GeneratorExp.generators,
1910 e->v.GeneratorExp.elt, NULL);
1911 }
1912
1913 static int
symtable_visit_listcomp(struct symtable * st,expr_ty e)1914 symtable_visit_listcomp(struct symtable *st, expr_ty e)
1915 {
1916 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1917 e->v.ListComp.generators,
1918 e->v.ListComp.elt, NULL);
1919 }
1920
1921 static int
symtable_visit_setcomp(struct symtable * st,expr_ty e)1922 symtable_visit_setcomp(struct symtable *st, expr_ty e)
1923 {
1924 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1925 e->v.SetComp.generators,
1926 e->v.SetComp.elt, NULL);
1927 }
1928
1929 static int
symtable_visit_dictcomp(struct symtable * st,expr_ty e)1930 symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1931 {
1932 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1933 e->v.DictComp.generators,
1934 e->v.DictComp.key,
1935 e->v.DictComp.value);
1936 }
1937