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