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