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