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