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