2 #include "Python-ast.h"
5 #include "structmember.h"
7 /* error strings used for warnings */
8 #define GLOBAL_AFTER_ASSIGN \
9 "name '%.400s' is assigned to before global declaration"
11 #define NONLOCAL_AFTER_ASSIGN \
12 "name '%.400s' is assigned to before nonlocal declaration"
14 #define GLOBAL_AFTER_USE \
15 "name '%.400s' is used prior to global declaration"
17 #define NONLOCAL_AFTER_USE \
18 "name '%.400s' is used prior to nonlocal declaration"
20 #define IMPORT_STAR_WARNING "import * only allowed at module level"
22 #define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
26 static PySTEntryObject
*
27 ste_new(struct symtable
*st
, identifier name
, _Py_block_ty block
,
28 void *key
, int lineno
)
30 PySTEntryObject
*ste
= NULL
;
33 k
= PyLong_FromVoidPtr(key
);
36 ste
= PyObject_New(PySTEntryObject
, &PySTEntry_Type
);
46 ste
->ste_symbols
= NULL
;
47 ste
->ste_varnames
= NULL
;
48 ste
->ste_children
= NULL
;
50 ste
->ste_symbols
= PyDict_New();
51 if (ste
->ste_symbols
== NULL
)
54 ste
->ste_varnames
= PyList_New(0);
55 if (ste
->ste_varnames
== NULL
)
58 ste
->ste_children
= PyList_New(0);
59 if (ste
->ste_children
== NULL
)
62 ste
->ste_type
= block
;
63 ste
->ste_unoptimized
= 0;
67 ste
->ste_varkeywords
= 0;
68 ste
->ste_opt_lineno
= 0;
70 ste
->ste_lineno
= lineno
;
72 if (st
->st_cur
!= NULL
&&
73 (st
->st_cur
->ste_nested
||
74 st
->st_cur
->ste_type
== FunctionBlock
))
76 ste
->ste_child_free
= 0;
77 ste
->ste_generator
= 0;
78 ste
->ste_returns_value
= 0;
80 if (PyDict_SetItem(st
->st_blocks
, ste
->ste_id
, (PyObject
*)ste
) < 0)
90 ste_repr(PySTEntryObject
*ste
)
92 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 PyLong_AS_LONG(ste
->ste_id
), ste
->ste_lineno
);
98 ste_dealloc(PySTEntryObject
*ste
)
100 ste
->ste_table
= NULL
;
101 Py_XDECREF(ste
->ste_id
);
102 Py_XDECREF(ste
->ste_name
);
103 Py_XDECREF(ste
->ste_symbols
);
104 Py_XDECREF(ste
->ste_varnames
);
105 Py_XDECREF(ste
->ste_children
);
109 #define OFF(x) offsetof(PySTEntryObject, x)
111 static PyMemberDef ste_memberlist
[] = {
112 {"id", T_OBJECT
, OFF(ste_id
), READONLY
},
113 {"name", T_OBJECT
, OFF(ste_name
), READONLY
},
114 {"symbols", T_OBJECT
, OFF(ste_symbols
), READONLY
},
115 {"varnames", T_OBJECT
, OFF(ste_varnames
), READONLY
},
116 {"children", T_OBJECT
, OFF(ste_children
), READONLY
},
117 {"optimized",T_INT
, OFF(ste_unoptimized
), READONLY
},
118 {"nested", T_INT
, OFF(ste_nested
), READONLY
},
119 {"type", T_INT
, OFF(ste_type
), READONLY
},
120 {"lineno", T_INT
, OFF(ste_lineno
), READONLY
},
124 PyTypeObject PySTEntry_Type
= {
125 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
127 sizeof(PySTEntryObject
),
129 (destructor
)ste_dealloc
, /* tp_dealloc */
134 (reprfunc
)ste_repr
, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
141 PyObject_GenericGetAttr
, /* tp_getattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT
, /* tp_flags */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
153 ste_memberlist
, /* tp_members */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
165 static int symtable_analyze(struct symtable
*st
);
166 static int symtable_warn(struct symtable
*st
, char *msg
, int lineno
);
167 static int symtable_enter_block(struct symtable
*st
, identifier name
,
168 _Py_block_ty block
, void *ast
, int lineno
);
169 static int symtable_exit_block(struct symtable
*st
, void *ast
);
170 static int symtable_visit_stmt(struct symtable
*st
, stmt_ty s
);
171 static int symtable_visit_expr(struct symtable
*st
, expr_ty s
);
172 static int symtable_visit_genexp(struct symtable
*st
, expr_ty s
);
173 static int symtable_visit_listcomp(struct symtable
*st
, expr_ty s
);
174 static int symtable_visit_setcomp(struct symtable
*st
, expr_ty s
);
175 static int symtable_visit_dictcomp(struct symtable
*st
, expr_ty s
);
176 static int symtable_visit_arguments(struct symtable
*st
, arguments_ty
);
177 static int symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty
);
178 static int symtable_visit_alias(struct symtable
*st
, alias_ty
);
179 static int symtable_visit_comprehension(struct symtable
*st
, comprehension_ty
);
180 static int symtable_visit_keyword(struct symtable
*st
, keyword_ty
);
181 static int symtable_visit_slice(struct symtable
*st
, slice_ty
);
182 static int symtable_visit_params(struct symtable
*st
, asdl_seq
*args
);
183 static int symtable_visit_argannotations(struct symtable
*st
, asdl_seq
*args
);
184 static int symtable_implicit_arg(struct symtable
*st
, int pos
);
185 static int symtable_visit_annotations(struct symtable
*st
, stmt_ty s
);
188 static identifier top
= NULL
, lambda
= NULL
, genexpr
= NULL
,
189 listcomp
= NULL
, setcomp
= NULL
, dictcomp
= NULL
,
190 __class__
= NULL
, __locals__
= NULL
;
192 #define GET_IDENTIFIER(VAR) \
193 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
195 #define DUPLICATE_ARGUMENT \
196 "duplicate argument '%U' in function definition"
198 static struct symtable
*
203 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
207 st
->st_filename
= NULL
;
208 st
->st_blocks
= NULL
;
210 if ((st
->st_stack
= PyList_New(0)) == NULL
)
212 if ((st
->st_blocks
= PyDict_New()) == NULL
)
215 st
->st_private
= NULL
;
223 PySymtable_Build(mod_ty mod
, const char *filename
, PyFutureFeatures
*future
)
225 struct symtable
*st
= symtable_new();
231 st
->st_filename
= filename
;
232 st
->st_future
= future
;
233 /* Make the initial symbol information gathering pass */
234 if (!GET_IDENTIFIER(top
) ||
235 !symtable_enter_block(st
, top
, ModuleBlock
, (void *)mod
, 0)) {
240 st
->st_top
= st
->st_cur
;
241 st
->st_cur
->ste_unoptimized
= OPT_TOPLEVEL
;
244 seq
= mod
->v
.Module
.body
;
245 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
246 if (!symtable_visit_stmt(st
,
247 (stmt_ty
)asdl_seq_GET(seq
, i
)))
250 case Expression_kind
:
251 if (!symtable_visit_expr(st
, mod
->v
.Expression
.body
))
254 case Interactive_kind
:
255 seq
= mod
->v
.Interactive
.body
;
256 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++)
257 if (!symtable_visit_stmt(st
,
258 (stmt_ty
)asdl_seq_GET(seq
, i
)))
262 PyErr_SetString(PyExc_RuntimeError
,
263 "this compiler does not handle Suites");
266 if (!symtable_exit_block(st
, (void *)mod
)) {
270 /* Make the second symbol analysis pass */
271 if (symtable_analyze(st
))
276 (void) symtable_exit_block(st
, (void *)mod
);
282 PySymtable_Free(struct symtable
*st
)
284 Py_XDECREF(st
->st_blocks
);
285 Py_XDECREF(st
->st_stack
);
286 PyMem_Free((void *)st
);
290 PySymtable_Lookup(struct symtable
*st
, void *key
)
294 k
= PyLong_FromVoidPtr(key
);
297 v
= PyDict_GetItem(st
->st_blocks
, k
);
299 assert(PySTEntry_Check(v
));
303 PyErr_SetString(PyExc_KeyError
,
304 "unknown symbol table entry");
308 return (PySTEntryObject
*)v
;
312 PyST_GetScope(PySTEntryObject
*ste
, PyObject
*name
)
314 PyObject
*v
= PyDict_GetItem(ste
->ste_symbols
, name
);
317 assert(PyLong_Check(v
));
318 return (PyLong_AS_LONG(v
) >> SCOPE_OFFSET
) & SCOPE_MASK
;
322 /* Analyze raw symbol information to determine scope of each name.
324 The next several functions are helpers for symtable_analyze(),
325 which determines whether a name is local, global, or free. In addition,
326 it determines which local variables are cell variables; they provide
327 bindings that are used for free variables in enclosed blocks.
329 There are also two kinds of global variables, implicit and explicit. An
330 explicit global is declared with the global statement. An implicit
331 global is a free variable for which the compiler has found no binding
332 in an enclosing function scope. The implicit global is either a global
333 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
334 to handle these names to implement slightly odd semantics. In such a
335 block, the name is treated as global until it is assigned to; then it
336 is treated as a local.
338 The symbol table requires two passes to determine the scope of each name.
339 The first pass collects raw facts from the AST via the symtable_visit_*
340 functions: the name is a parameter here, the name is used but not defined
341 here, etc. The second pass analyzes these facts during a pass over the
342 PySTEntryObjects created during pass 1.
344 When a function is entered during the second pass, the parent passes
345 the set of all name bindings visible to its children. These bindings
346 are used to determine if non-local variables are free or implicit globals.
347 Names which are explicitly declared nonlocal must exist in this set of
348 visible names - if they do not, a syntax error is raised. After doing
349 the local analysis, it analyzes each of its child blocks using an
350 updated set of name bindings.
352 The children update the free variable set. If a local variable is added to
353 the free variable set by the child, the variable is marked as a cell. The
354 function object being defined must provide runtime storage for the variable
355 that may outlive the function's frame. Cell variables are removed from the
356 free set before the analyze function returns to its parent.
358 During analysis, the names are:
359 symbols: dict mapping from symbol names to flag values (including offset scope values)
360 scopes: dict mapping from symbol names to scope values (no offset)
361 local: set of all symbol names local to the current scope
362 bound: set of all symbol names local to a containing function scope
363 free: set of all symbol names referenced but not bound in child scopes
364 global: set of all symbol names explicitly declared as global
367 #define SET_SCOPE(DICT, NAME, I) { \
368 PyObject *o = PyLong_FromLong(I); \
371 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
378 /* Decide on scope of name, given flags.
380 The namespace dictionaries may be modified to record information
381 about the new name. For example, a new global will add an entry to
382 global. A name that was global can be changed to local.
386 analyze_name(PySTEntryObject
*ste
, PyObject
*scopes
, PyObject
*name
, long flags
,
387 PyObject
*bound
, PyObject
*local
, PyObject
*free
,
390 if (flags
& DEF_GLOBAL
) {
391 if (flags
& DEF_PARAM
) {
392 PyErr_Format(PyExc_SyntaxError
,
393 "name '%U' is parameter and global",
395 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
400 if (flags
& DEF_NONLOCAL
) {
401 PyErr_Format(PyExc_SyntaxError
,
402 "name '%U' is nonlocal and global",
406 SET_SCOPE(scopes
, name
, GLOBAL_EXPLICIT
);
407 if (PySet_Add(global
, name
) < 0)
409 if (bound
&& (PySet_Discard(bound
, name
) < 0))
413 if (flags
& DEF_NONLOCAL
) {
414 if (flags
& DEF_PARAM
) {
415 PyErr_Format(PyExc_SyntaxError
,
416 "name '%U' is parameter and nonlocal",
421 PyErr_Format(PyExc_SyntaxError
,
422 "nonlocal declaration not allowed at module level");
425 if (!PySet_Contains(bound
, name
)) {
426 PyErr_Format(PyExc_SyntaxError
,
427 "no binding for nonlocal '%U' found",
432 SET_SCOPE(scopes
, name
, FREE
);
434 return PySet_Add(free
, name
) >= 0;
436 if (flags
& DEF_BOUND
) {
437 SET_SCOPE(scopes
, name
, LOCAL
);
438 if (PySet_Add(local
, name
) < 0)
440 if (PySet_Discard(global
, name
) < 0)
444 /* If an enclosing block has a binding for this name, it
445 is a free variable rather than a global variable.
446 Note that having a non-NULL bound implies that the block
449 if (bound
&& PySet_Contains(bound
, name
)) {
450 SET_SCOPE(scopes
, name
, FREE
);
452 return PySet_Add(free
, name
) >= 0;
454 /* If a parent has a global statement, then call it global
455 explicit? It could also be global implicit.
457 if (global
&& PySet_Contains(global
, name
)) {
458 SET_SCOPE(scopes
, name
, GLOBAL_IMPLICIT
);
463 SET_SCOPE(scopes
, name
, GLOBAL_IMPLICIT
);
469 /* If a name is defined in free and also in locals, then this block
470 provides the binding for the free variable. The name should be
471 marked CELL in this block and removed from the free list.
473 Note that the current block's free variables are included in free.
474 That's safe because no name can be free and local in the same scope.
476 The 'restricted' argument may be set to a string to restrict the analysis
477 to the one variable whose name equals that string (e.g. "__class__").
481 analyze_cells(PyObject
*scopes
, PyObject
*free
, const char *restricted
)
483 PyObject
*name
, *v
, *v_cell
;
487 v_cell
= PyLong_FromLong(CELL
);
490 while (PyDict_Next(scopes
, &pos
, &name
, &v
)) {
492 assert(PyLong_Check(v
));
493 scope
= PyLong_AS_LONG(v
);
496 if (!PySet_Contains(free
, name
))
498 if (restricted
!= NULL
&&
499 PyUnicode_CompareWithASCIIString(name
, restricted
))
501 /* Replace LOCAL with CELL for this name, and remove
502 from free. It is safe to replace the value of name
503 in the dict, because it will not cause a resize.
505 if (PyDict_SetItem(scopes
, name
, v_cell
) < 0)
507 if (PySet_Discard(free
, name
) < 0)
516 /* Check for illegal statements in unoptimized namespaces */
518 check_unoptimized(const PySTEntryObject
* ste
) {
521 if (ste
->ste_type
!= FunctionBlock
|| !ste
->ste_unoptimized
522 || !(ste
->ste_free
|| ste
->ste_child_free
))
525 trailer
= (ste
->ste_child_free
?
526 "contains a nested function with free variables" :
527 "is a nested function");
529 switch (ste
->ste_unoptimized
) {
530 case OPT_TOPLEVEL
: /* import * at top-level is fine */
532 case OPT_IMPORT_STAR
:
533 PyErr_Format(PyExc_SyntaxError
,
534 "import * is not allowed in function '%U' because it %s",
535 ste
->ste_name
, trailer
);
539 PyErr_SyntaxLocation(ste
->ste_table
->st_filename
,
540 ste
->ste_opt_lineno
);
544 /* Enter the final scope information into the ste_symbols dict.
546 * All arguments are dicts. Modifies symbols, others are read-only.
549 update_symbols(PyObject
*symbols
, PyObject
*scopes
,
550 PyObject
*bound
, PyObject
*free
, int classflag
)
552 PyObject
*name
= NULL
, *itr
= NULL
;
553 PyObject
*v
= NULL
, *v_scope
= NULL
, *v_new
= NULL
, *v_free
= NULL
;
556 /* Update scope information for all symbols in this scope */
557 while (PyDict_Next(symbols
, &pos
, &name
, &v
)) {
559 assert(PyLong_Check(v
));
560 flags
= PyLong_AS_LONG(v
);
561 v_scope
= PyDict_GetItem(scopes
, name
);
562 assert(v_scope
&& PyLong_Check(v_scope
));
563 scope
= PyLong_AS_LONG(v_scope
);
564 flags
|= (scope
<< SCOPE_OFFSET
);
565 v_new
= PyLong_FromLong(flags
);
568 if (PyDict_SetItem(symbols
, name
, v_new
) < 0) {
575 /* Record not yet resolved free variables from children (if any) */
576 v_free
= PyLong_FromLong(FREE
<< SCOPE_OFFSET
);
580 itr
= PyObject_GetIter(free
);
584 while ((name
= PyIter_Next(itr
))) {
585 v
= PyDict_GetItem(symbols
, name
);
587 /* Handle symbol that already exists in this scope */
589 /* Handle a free variable in a method of
590 the class that has the same name as a local
591 or global in the class scope.
594 PyLong_AS_LONG(v
) & (DEF_BOUND
| DEF_GLOBAL
)) {
595 long flags
= PyLong_AS_LONG(v
) | DEF_FREE_CLASS
;
596 v_new
= PyLong_FromLong(flags
);
600 if (PyDict_SetItem(symbols
, name
, v_new
) < 0) {
606 /* It's a cell, or already free in this scope */
610 /* Handle global symbol */
611 if (!PySet_Contains(bound
, name
)) {
613 continue; /* it's a global */
615 /* Propagate new free symbol up the lexical stack */
616 if (PyDict_SetItem(symbols
, name
, v_free
) < 0) {
631 /* Make final symbol table decisions for block of ste.
634 ste -- current symtable entry (input/output)
635 bound -- set of variables bound in enclosing scopes (input). bound
636 is NULL for module blocks.
637 free -- set of free variables in enclosed scopes (output)
638 globals -- set of declared global variables in enclosing scopes (input)
640 The implementation uses two mutually recursive functions,
641 analyze_block() and analyze_child_block(). analyze_block() is
642 responsible for analyzing the individual names defined in a block.
643 analyze_child_block() prepares temporary namespace dictionaries
644 used to evaluated nested blocks.
646 The two functions exist because a child block should see the name
647 bindings of its enclosing blocks, but those bindings should not
648 propagate back to a parent block.
652 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
653 PyObject
*global
, PyObject
* child_free
);
656 analyze_block(PySTEntryObject
*ste
, PyObject
*bound
, PyObject
*free
,
659 PyObject
*name
, *v
, *local
= NULL
, *scopes
= NULL
, *newbound
= NULL
;
660 PyObject
*newglobal
= NULL
, *newfree
= NULL
, *allfree
= NULL
;
665 local
= PySet_New(NULL
); /* collect new names bound in block */
668 scopes
= PyDict_New(); /* collect scopes defined for each name */
672 /* Allocate new global and bound variable dictionaries. These
673 dictionaries hold the names visible in nested blocks. For
674 ClassBlocks, the bound and global names are initialized
675 before analyzing names, because class bindings aren't
676 visible in methods. For other blocks, they are initialized
677 after names are analyzed.
680 /* TODO(jhylton): Package these dicts in a struct so that we
681 can write reasonable helper functions?
683 newglobal
= PySet_New(NULL
);
686 newfree
= PySet_New(NULL
);
689 newbound
= PySet_New(NULL
);
693 /* Class namespace has no effect on names visible in
694 nested functions, so populate the global and bound
695 sets to be passed to child blocks before analyzing
698 if (ste
->ste_type
== ClassBlock
) {
699 /* Pass down known globals */
700 temp
= PyNumber_InPlaceOr(newglobal
, global
);
704 /* Pass down previously bound symbols */
706 temp
= PyNumber_InPlaceOr(newbound
, bound
);
713 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
714 long flags
= PyLong_AS_LONG(v
);
715 if (!analyze_name(ste
, scopes
, name
, flags
,
716 bound
, local
, free
, global
))
720 /* Populate global and bound sets to be passed to children. */
721 if (ste
->ste_type
!= ClassBlock
) {
722 /* Add function locals to bound set */
723 if (ste
->ste_type
== FunctionBlock
) {
724 temp
= PyNumber_InPlaceOr(newbound
, local
);
729 /* Pass down previously bound symbols */
731 temp
= PyNumber_InPlaceOr(newbound
, bound
);
736 /* Pass down known globals */
737 temp
= PyNumber_InPlaceOr(newglobal
, global
);
743 /* Special-case __class__ */
744 if (!GET_IDENTIFIER(__class__
))
746 assert(PySet_Contains(local
, __class__
) == 1);
747 if (PySet_Add(newbound
, __class__
) < 0)
751 /* Recursively call analyze_block() on each child block.
753 newbound, newglobal now contain the names visible in
754 nested blocks. The free variables in the children will
755 be collected in allfree.
757 allfree
= PySet_New(NULL
);
760 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
761 PyObject
*c
= PyList_GET_ITEM(ste
->ste_children
, i
);
762 PySTEntryObject
* entry
;
763 assert(c
&& PySTEntry_Check(c
));
764 entry
= (PySTEntryObject
*)c
;
765 if (!analyze_child_block(entry
, newbound
, newfree
, newglobal
,
768 /* Check if any children have free variables */
769 if (entry
->ste_free
|| entry
->ste_child_free
)
770 ste
->ste_child_free
= 1;
773 temp
= PyNumber_InPlaceOr(newfree
, allfree
);
778 /* Check if any local variables must be converted to cell variables */
779 if (ste
->ste_type
== FunctionBlock
&& !analyze_cells(scopes
, newfree
,
782 else if (ste
->ste_type
== ClassBlock
&& !analyze_cells(scopes
, newfree
,
785 /* Records the results of the analysis in the symbol table entry */
786 if (!update_symbols(ste
->ste_symbols
, scopes
, bound
, newfree
,
787 ste
->ste_type
== ClassBlock
))
789 if (!check_unoptimized(ste
))
792 temp
= PyNumber_InPlaceOr(free
, newfree
);
800 Py_XDECREF(newbound
);
801 Py_XDECREF(newglobal
);
805 assert(PyErr_Occurred());
810 analyze_child_block(PySTEntryObject
*entry
, PyObject
*bound
, PyObject
*free
,
811 PyObject
*global
, PyObject
* child_free
)
813 PyObject
*temp_bound
= NULL
, *temp_global
= NULL
, *temp_free
= NULL
;
816 /* Copy the bound and global dictionaries.
818 These dictionary are used by all blocks enclosed by the
819 current block. The analyze_block() call modifies these
823 temp_bound
= PySet_New(bound
);
826 temp_free
= PySet_New(free
);
829 temp_global
= PySet_New(global
);
833 if (!analyze_block(entry
, temp_bound
, temp_free
, temp_global
))
835 temp
= PyNumber_InPlaceOr(child_free
, temp_free
);
839 Py_DECREF(temp_bound
);
840 Py_DECREF(temp_free
);
841 Py_DECREF(temp_global
);
844 Py_XDECREF(temp_bound
);
845 Py_XDECREF(temp_free
);
846 Py_XDECREF(temp_global
);
851 symtable_analyze(struct symtable
*st
)
853 PyObject
*free
, *global
;
856 free
= PySet_New(NULL
);
859 global
= PySet_New(NULL
);
864 r
= analyze_block(st
->st_top
, NULL
, free
, global
);
872 symtable_warn(struct symtable
*st
, char *msg
, int lineno
)
874 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, st
->st_filename
,
875 lineno
, NULL
, NULL
) < 0) {
876 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
877 PyErr_SetString(PyExc_SyntaxError
, msg
);
878 PyErr_SyntaxLocation(st
->st_filename
,
879 st
->st_cur
->ste_lineno
);
886 /* symtable_enter_block() gets a reference via ste_new.
887 This reference is released when the block is exited, via the DECREF
888 in symtable_exit_block().
892 symtable_exit_block(struct symtable
*st
, void *ast
)
896 Py_CLEAR(st
->st_cur
);
897 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
899 st
->st_cur
= (PySTEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
901 if (st
->st_cur
== NULL
)
903 Py_INCREF(st
->st_cur
);
904 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
911 symtable_enter_block(struct symtable
*st
, identifier name
, _Py_block_ty block
,
912 void *ast
, int lineno
)
914 PySTEntryObject
*prev
= NULL
;
918 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
921 Py_DECREF(st
->st_cur
);
923 st
->st_cur
= ste_new(st
, name
, block
, ast
, lineno
);
924 if (st
->st_cur
== NULL
)
926 if (name
== GET_IDENTIFIER(top
))
927 st
->st_global
= st
->st_cur
->ste_symbols
;
929 if (PyList_Append(prev
->ste_children
,
930 (PyObject
*)st
->st_cur
) < 0) {
938 symtable_lookup(struct symtable
*st
, PyObject
*name
)
941 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
944 o
= PyDict_GetItem(st
->st_cur
->ste_symbols
, mangled
);
948 return PyLong_AsLong(o
);
952 symtable_add_def(struct symtable
*st
, PyObject
*name
, int flag
)
957 PyObject
*mangled
= _Py_Mangle(st
->st_private
, name
);
962 dict
= st
->st_cur
->ste_symbols
;
963 if ((o
= PyDict_GetItem(dict
, mangled
))) {
964 val
= PyLong_AS_LONG(o
);
965 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
966 /* Is it better to use 'mangled' or 'name' here? */
967 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
, name
);
968 PyErr_SyntaxLocation(st
->st_filename
,
969 st
->st_cur
->ste_lineno
);
975 o
= PyLong_FromLong(val
);
978 if (PyDict_SetItem(dict
, mangled
, o
) < 0) {
984 if (flag
& DEF_PARAM
) {
985 if (PyList_Append(st
->st_cur
->ste_varnames
, mangled
) < 0)
987 } else if (flag
& DEF_GLOBAL
) {
988 /* XXX need to update DEF_GLOBAL for other flags too;
989 perhaps only DEF_FREE_GLOBAL */
991 if ((o
= PyDict_GetItem(st
->st_global
, mangled
))) {
992 val
|= PyLong_AS_LONG(o
);
994 o
= PyLong_FromLong(val
);
997 if (PyDict_SetItem(st
->st_global
, mangled
, o
) < 0) {
1011 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1012 They use the ASDL name to synthesize the name of the C type and the visit
1015 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1016 useful if the first node in the sequence requires special treatment.
1019 #define VISIT(ST, TYPE, V) \
1020 if (!symtable_visit_ ## TYPE((ST), (V))) \
1023 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
1024 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1025 symtable_exit_block((ST), (S)); \
1029 #define VISIT_SEQ(ST, TYPE, SEQ) { \
1031 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1032 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1033 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1034 if (!symtable_visit_ ## TYPE((ST), elt)) \
1039 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
1041 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1042 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1043 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1044 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1045 symtable_exit_block((ST), (S)); \
1051 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1053 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1054 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1055 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1056 if (!symtable_visit_ ## TYPE((ST), elt)) \
1061 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
1063 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1064 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1065 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1066 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1067 symtable_exit_block((ST), (S)); \
1073 #define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
1075 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1076 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1077 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1078 if (!elt) continue; /* can be NULL */ \
1079 if (!symtable_visit_expr((ST), elt)) \
1085 symtable_new_tmpname(struct symtable
*st
)
1090 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]",
1091 ++st
->st_cur
->ste_tmpname
);
1092 tmp
= PyUnicode_InternFromString(tmpname
);
1095 if (!symtable_add_def(st
, tmp
, DEF_LOCAL
))
1104 symtable_visit_stmt(struct symtable
*st
, stmt_ty s
)
1107 case FunctionDef_kind
:
1108 if (!symtable_add_def(st
, s
->v
.FunctionDef
.name
, DEF_LOCAL
))
1110 if (s
->v
.FunctionDef
.args
->defaults
)
1111 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.args
->defaults
);
1112 if (s
->v
.FunctionDef
.args
->kw_defaults
)
1113 VISIT_KWONLYDEFAULTS(st
,
1114 s
->v
.FunctionDef
.args
->kw_defaults
);
1115 if (!symtable_visit_annotations(st
, s
))
1117 if (s
->v
.FunctionDef
.decorator_list
)
1118 VISIT_SEQ(st
, expr
, s
->v
.FunctionDef
.decorator_list
);
1119 if (!symtable_enter_block(st
, s
->v
.FunctionDef
.name
,
1120 FunctionBlock
, (void *)s
, s
->lineno
))
1122 VISIT_IN_BLOCK(st
, arguments
, s
->v
.FunctionDef
.args
, s
);
1123 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.FunctionDef
.body
, s
);
1124 if (!symtable_exit_block(st
, s
))
1127 case ClassDef_kind
: {
1129 if (!symtable_add_def(st
, s
->v
.ClassDef
.name
, DEF_LOCAL
))
1131 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.bases
);
1132 VISIT_SEQ(st
, keyword
, s
->v
.ClassDef
.keywords
);
1133 if (s
->v
.ClassDef
.starargs
)
1134 VISIT(st
, expr
, s
->v
.ClassDef
.starargs
);
1135 if (s
->v
.ClassDef
.kwargs
)
1136 VISIT(st
, expr
, s
->v
.ClassDef
.kwargs
);
1137 if (s
->v
.ClassDef
.decorator_list
)
1138 VISIT_SEQ(st
, expr
, s
->v
.ClassDef
.decorator_list
);
1139 if (!symtable_enter_block(st
, s
->v
.ClassDef
.name
, ClassBlock
,
1140 (void *)s
, s
->lineno
))
1142 if (!GET_IDENTIFIER(__class__
) ||
1143 !symtable_add_def(st
, __class__
, DEF_LOCAL
) ||
1144 !GET_IDENTIFIER(__locals__
) ||
1145 !symtable_add_def(st
, __locals__
, DEF_PARAM
)) {
1146 symtable_exit_block(st
, s
);
1149 tmp
= st
->st_private
;
1150 st
->st_private
= s
->v
.ClassDef
.name
;
1151 VISIT_SEQ_IN_BLOCK(st
, stmt
, s
->v
.ClassDef
.body
, s
);
1152 st
->st_private
= tmp
;
1153 if (!symtable_exit_block(st
, s
))
1158 if (s
->v
.Return
.value
) {
1159 VISIT(st
, expr
, s
->v
.Return
.value
);
1160 st
->st_cur
->ste_returns_value
= 1;
1161 if (st
->st_cur
->ste_generator
) {
1162 PyErr_SetString(PyExc_SyntaxError
,
1163 RETURN_VAL_IN_GENERATOR
);
1164 PyErr_SyntaxLocation(st
->st_filename
,
1171 VISIT_SEQ(st
, expr
, s
->v
.Delete
.targets
);
1174 VISIT_SEQ(st
, expr
, s
->v
.Assign
.targets
);
1175 VISIT(st
, expr
, s
->v
.Assign
.value
);
1177 case AugAssign_kind
:
1178 VISIT(st
, expr
, s
->v
.AugAssign
.target
);
1179 VISIT(st
, expr
, s
->v
.AugAssign
.value
);
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
);
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
);
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
);
1199 VISIT_SEQ(st
, stmt
, s
->v
.If
.orelse
);
1202 if (s
->v
.Raise
.exc
) {
1203 VISIT(st
, expr
, s
->v
.Raise
.exc
);
1204 if (s
->v
.Raise
.cause
) {
1205 VISIT(st
, expr
, s
->v
.Raise
.cause
);
1209 case TryExcept_kind
:
1210 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.body
);
1211 VISIT_SEQ(st
, stmt
, s
->v
.TryExcept
.orelse
);
1212 VISIT_SEQ(st
, excepthandler
, s
->v
.TryExcept
.handlers
);
1214 case TryFinally_kind
:
1215 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.body
);
1216 VISIT_SEQ(st
, stmt
, s
->v
.TryFinally
.finalbody
);
1219 VISIT(st
, expr
, s
->v
.Assert
.test
);
1220 if (s
->v
.Assert
.msg
)
1221 VISIT(st
, expr
, s
->v
.Assert
.msg
);
1224 VISIT_SEQ(st
, alias
, s
->v
.Import
.names
);
1225 /* XXX Don't have the lineno available inside
1227 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1228 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1230 case ImportFrom_kind
:
1231 VISIT_SEQ(st
, alias
, s
->v
.ImportFrom
.names
);
1232 /* XXX Don't have the lineno available inside
1234 if (st
->st_cur
->ste_unoptimized
&& !st
->st_cur
->ste_opt_lineno
)
1235 st
->st_cur
->ste_opt_lineno
= s
->lineno
;
1239 asdl_seq
*seq
= s
->v
.Global
.names
;
1240 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1241 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1242 char *c_name
= _PyUnicode_AsString(name
);
1243 long cur
= symtable_lookup(st
, name
);
1246 if (cur
& (DEF_LOCAL
| USE
)) {
1248 if (cur
& DEF_LOCAL
)
1249 PyOS_snprintf(buf
, sizeof(buf
),
1250 GLOBAL_AFTER_ASSIGN
,
1253 PyOS_snprintf(buf
, sizeof(buf
),
1256 if (!symtable_warn(st
, buf
, s
->lineno
))
1259 if (!symtable_add_def(st
, name
, DEF_GLOBAL
))
1264 case Nonlocal_kind
: {
1266 asdl_seq
*seq
= s
->v
.Nonlocal
.names
;
1267 for (i
= 0; i
< asdl_seq_LEN(seq
); i
++) {
1268 identifier name
= (identifier
)asdl_seq_GET(seq
, i
);
1269 char *c_name
= _PyUnicode_AsString(name
);
1270 long cur
= symtable_lookup(st
, name
);
1273 if (cur
& (DEF_LOCAL
| USE
)) {
1275 if (cur
& DEF_LOCAL
)
1276 PyOS_snprintf(buf
, sizeof(buf
),
1277 NONLOCAL_AFTER_ASSIGN
,
1280 PyOS_snprintf(buf
, sizeof(buf
),
1283 if (!symtable_warn(st
, buf
, s
->lineno
))
1286 if (!symtable_add_def(st
, name
, DEF_NONLOCAL
))
1292 VISIT(st
, expr
, s
->v
.Expr
.value
);
1297 /* nothing to do here */
1300 if (!symtable_new_tmpname(st
))
1302 VISIT(st
, expr
, s
->v
.With
.context_expr
);
1303 if (s
->v
.With
.optional_vars
) {
1304 if (!symtable_new_tmpname(st
))
1306 VISIT(st
, expr
, s
->v
.With
.optional_vars
);
1308 VISIT_SEQ(st
, stmt
, s
->v
.With
.body
);
1315 symtable_visit_expr(struct symtable
*st
, expr_ty e
)
1319 VISIT_SEQ(st
, expr
, e
->v
.BoolOp
.values
);
1322 VISIT(st
, expr
, e
->v
.BinOp
.left
);
1323 VISIT(st
, expr
, e
->v
.BinOp
.right
);
1326 VISIT(st
, expr
, e
->v
.UnaryOp
.operand
);
1329 if (!GET_IDENTIFIER(lambda
) ||
1330 !symtable_add_def(st
, lambda
, DEF_LOCAL
))
1332 if (e
->v
.Lambda
.args
->defaults
)
1333 VISIT_SEQ(st
, expr
, e
->v
.Lambda
.args
->defaults
);
1334 if (!symtable_enter_block(st
, lambda
,
1335 FunctionBlock
, (void *)e
, e
->lineno
))
1337 VISIT_IN_BLOCK(st
, arguments
, e
->v
.Lambda
.args
, (void*)e
);
1338 VISIT_IN_BLOCK(st
, expr
, e
->v
.Lambda
.body
, (void*)e
);
1339 if (!symtable_exit_block(st
, (void *)e
))
1344 VISIT(st
, expr
, e
->v
.IfExp
.test
);
1345 VISIT(st
, expr
, e
->v
.IfExp
.body
);
1346 VISIT(st
, expr
, e
->v
.IfExp
.orelse
);
1349 VISIT_SEQ(st
, expr
, e
->v
.Dict
.keys
);
1350 VISIT_SEQ(st
, expr
, e
->v
.Dict
.values
);
1353 VISIT_SEQ(st
, expr
, e
->v
.Set
.elts
);
1355 case GeneratorExp_kind
:
1356 if (!symtable_visit_genexp(st
, e
))
1360 if (!symtable_visit_listcomp(st
, e
))
1364 if (!symtable_visit_setcomp(st
, e
))
1368 if (!symtable_visit_dictcomp(st
, e
))
1372 if (e
->v
.Yield
.value
)
1373 VISIT(st
, expr
, e
->v
.Yield
.value
);
1374 st
->st_cur
->ste_generator
= 1;
1375 if (st
->st_cur
->ste_returns_value
) {
1376 PyErr_SetString(PyExc_SyntaxError
,
1377 RETURN_VAL_IN_GENERATOR
);
1378 PyErr_SyntaxLocation(st
->st_filename
,
1384 VISIT(st
, expr
, e
->v
.Compare
.left
);
1385 VISIT_SEQ(st
, expr
, e
->v
.Compare
.comparators
);
1388 VISIT(st
, expr
, e
->v
.Call
.func
);
1389 VISIT_SEQ(st
, expr
, e
->v
.Call
.args
);
1390 VISIT_SEQ(st
, keyword
, e
->v
.Call
.keywords
);
1391 if (e
->v
.Call
.starargs
)
1392 VISIT(st
, expr
, e
->v
.Call
.starargs
);
1393 if (e
->v
.Call
.kwargs
)
1394 VISIT(st
, expr
, e
->v
.Call
.kwargs
);
1400 /* Nothing to do here. */
1402 /* The following exprs can be assignment targets. */
1403 case Attribute_kind
:
1404 VISIT(st
, expr
, e
->v
.Attribute
.value
);
1406 case Subscript_kind
:
1407 VISIT(st
, expr
, e
->v
.Subscript
.value
);
1408 VISIT(st
, slice
, e
->v
.Subscript
.slice
);
1411 VISIT(st
, expr
, e
->v
.Starred
.value
);
1414 if (!symtable_add_def(st
, e
->v
.Name
.id
,
1415 e
->v
.Name
.ctx
== Load
? USE
: DEF_LOCAL
))
1417 /* Special-case super: it counts as a use of __class__ */
1418 if (e
->v
.Name
.ctx
== Load
&&
1419 st
->st_cur
->ste_type
== FunctionBlock
&&
1420 !PyUnicode_CompareWithASCIIString(e
->v
.Name
.id
, "super")) {
1421 if (!GET_IDENTIFIER(__class__
) ||
1422 !symtable_add_def(st
, __class__
, USE
))
1426 /* child nodes of List and Tuple will have expr_context set */
1428 VISIT_SEQ(st
, expr
, e
->v
.List
.elts
);
1431 VISIT_SEQ(st
, expr
, e
->v
.Tuple
.elts
);
1438 symtable_implicit_arg(struct symtable
*st
, int pos
)
1440 PyObject
*id
= PyUnicode_FromFormat(".%d", pos
);
1443 if (!symtable_add_def(st
, id
, DEF_PARAM
)) {
1452 symtable_visit_params(struct symtable
*st
, asdl_seq
*args
)
1459 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1460 arg_ty arg
= (arg_ty
)asdl_seq_GET(args
, i
);
1461 if (!symtable_add_def(st
, arg
->arg
, DEF_PARAM
))
1469 symtable_visit_argannotations(struct symtable
*st
, asdl_seq
*args
)
1476 for (i
= 0; i
< asdl_seq_LEN(args
); i
++) {
1477 arg_ty arg
= (arg_ty
)asdl_seq_GET(args
, i
);
1478 if (arg
->annotation
)
1479 VISIT(st
, expr
, arg
->annotation
);
1486 symtable_visit_annotations(struct symtable
*st
, stmt_ty s
)
1488 arguments_ty a
= s
->v
.FunctionDef
.args
;
1490 if (a
->args
&& !symtable_visit_argannotations(st
, a
->args
))
1492 if (a
->varargannotation
)
1493 VISIT(st
, expr
, a
->varargannotation
);
1494 if (a
->kwargannotation
)
1495 VISIT(st
, expr
, a
->kwargannotation
);
1496 if (a
->kwonlyargs
&& !symtable_visit_argannotations(st
, a
->kwonlyargs
))
1498 if (s
->v
.FunctionDef
.returns
)
1499 VISIT(st
, expr
, s
->v
.FunctionDef
.returns
);
1504 symtable_visit_arguments(struct symtable
*st
, arguments_ty a
)
1506 /* skip default arguments inside function block
1507 XXX should ast be different?
1509 if (a
->args
&& !symtable_visit_params(st
, a
->args
))
1511 if (a
->kwonlyargs
&& !symtable_visit_params(st
, a
->kwonlyargs
))
1514 if (!symtable_add_def(st
, a
->vararg
, DEF_PARAM
))
1516 st
->st_cur
->ste_varargs
= 1;
1519 if (!symtable_add_def(st
, a
->kwarg
, DEF_PARAM
))
1521 st
->st_cur
->ste_varkeywords
= 1;
1528 symtable_visit_excepthandler(struct symtable
*st
, excepthandler_ty eh
)
1530 if (eh
->v
.ExceptHandler
.type
)
1531 VISIT(st
, expr
, eh
->v
.ExceptHandler
.type
);
1532 if (eh
->v
.ExceptHandler
.name
)
1533 if (!symtable_add_def(st
, eh
->v
.ExceptHandler
.name
, DEF_LOCAL
))
1535 VISIT_SEQ(st
, stmt
, eh
->v
.ExceptHandler
.body
);
1541 symtable_visit_alias(struct symtable
*st
, alias_ty a
)
1543 /* Compute store_name, the name actually bound by the import
1544 operation. It is diferent than a->name when a->name is a
1545 dotted package name (e.g. spam.eggs)
1547 PyObject
*store_name
;
1548 PyObject
*name
= (a
->asname
== NULL
) ? a
->name
: a
->asname
;
1549 const Py_UNICODE
*base
= PyUnicode_AS_UNICODE(name
);
1550 Py_UNICODE
*dot
= Py_UNICODE_strchr(base
, '.');
1552 store_name
= PyUnicode_FromUnicode(base
, dot
- base
);
1558 Py_INCREF(store_name
);
1560 if (PyUnicode_CompareWithASCIIString(name
, "*")) {
1561 int r
= symtable_add_def(st
, store_name
, DEF_IMPORT
);
1562 Py_DECREF(store_name
);
1566 if (st
->st_cur
->ste_type
!= ModuleBlock
) {
1567 int lineno
= st
->st_cur
->ste_lineno
;
1568 PyErr_SetString(PyExc_SyntaxError
, IMPORT_STAR_WARNING
);
1569 PyErr_SyntaxLocation(st
->st_filename
, lineno
);
1570 Py_DECREF(store_name
);
1573 st
->st_cur
->ste_unoptimized
|= OPT_IMPORT_STAR
;
1574 Py_DECREF(store_name
);
1581 symtable_visit_comprehension(struct symtable
*st
, comprehension_ty lc
)
1583 VISIT(st
, expr
, lc
->target
);
1584 VISIT(st
, expr
, lc
->iter
);
1585 VISIT_SEQ(st
, expr
, lc
->ifs
);
1591 symtable_visit_keyword(struct symtable
*st
, keyword_ty k
)
1593 VISIT(st
, expr
, k
->value
);
1599 symtable_visit_slice(struct symtable
*st
, slice_ty s
)
1603 if (s
->v
.Slice
.lower
)
1604 VISIT(st
, expr
, s
->v
.Slice
.lower
)
1605 if (s
->v
.Slice
.upper
)
1606 VISIT(st
, expr
, s
->v
.Slice
.upper
)
1607 if (s
->v
.Slice
.step
)
1608 VISIT(st
, expr
, s
->v
.Slice
.step
)
1611 VISIT_SEQ(st
, slice
, s
->v
.ExtSlice
.dims
)
1614 VISIT(st
, expr
, s
->v
.Index
.value
)
1621 symtable_handle_comprehension(struct symtable
*st
, expr_ty e
,
1622 identifier scope_name
, asdl_seq
*generators
,
1623 expr_ty elt
, expr_ty value
)
1625 int is_generator
= (e
->kind
== GeneratorExp_kind
);
1626 int needs_tmp
= !is_generator
;
1627 comprehension_ty outermost
= ((comprehension_ty
)
1628 asdl_seq_GET(generators
, 0));
1629 /* Outermost iterator is evaluated in current scope */
1630 VISIT(st
, expr
, outermost
->iter
);
1631 /* Create comprehension scope for the rest */
1633 !symtable_enter_block(st
, scope_name
, FunctionBlock
, (void *)e
, e
->lineno
)) {
1636 st
->st_cur
->ste_generator
= is_generator
;
1637 /* Outermost iter is received as an argument */
1638 if (!symtable_implicit_arg(st
, 0)) {
1639 symtable_exit_block(st
, (void *)e
);
1642 /* Allocate temporary name if needed */
1643 if (needs_tmp
&& !symtable_new_tmpname(st
)) {
1644 symtable_exit_block(st
, (void *)e
);
1647 VISIT_IN_BLOCK(st
, expr
, outermost
->target
, (void*)e
);
1648 VISIT_SEQ_IN_BLOCK(st
, expr
, outermost
->ifs
, (void*)e
);
1649 VISIT_SEQ_TAIL_IN_BLOCK(st
, comprehension
,
1650 generators
, 1, (void*)e
);
1652 VISIT_IN_BLOCK(st
, expr
, value
, (void*)e
);
1653 VISIT_IN_BLOCK(st
, expr
, elt
, (void*)e
);
1654 return symtable_exit_block(st
, (void *)e
);
1658 symtable_visit_genexp(struct symtable
*st
, expr_ty e
)
1660 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(genexpr
),
1661 e
->v
.GeneratorExp
.generators
,
1662 e
->v
.GeneratorExp
.elt
, NULL
);
1666 symtable_visit_listcomp(struct symtable
*st
, expr_ty e
)
1668 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(listcomp
),
1669 e
->v
.ListComp
.generators
,
1670 e
->v
.ListComp
.elt
, NULL
);
1674 symtable_visit_setcomp(struct symtable
*st
, expr_ty e
)
1676 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(setcomp
),
1677 e
->v
.SetComp
.generators
,
1678 e
->v
.SetComp
.elt
, NULL
);
1682 symtable_visit_dictcomp(struct symtable
*st
, expr_ty e
)
1684 return symtable_handle_comprehension(st
, e
, GET_IDENTIFIER(dictcomp
),
1685 e
->v
.DictComp
.generators
,
1687 e
->v
.DictComp
.value
);