From e2dbc2f5b3b51b6ed7a79a70b0347080f5e2dc6b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 31 Oct 2012 20:26:20 -0400 Subject: point errors related to nonlocals and globals to the statement declaring them (closes #10189) --- Python/symtable.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index ff6e8b79b9..ff440242cd 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -56,6 +56,8 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, if (ste->ste_children == NULL) goto fail; + ste->ste_directives = NULL; + ste->ste_type = block; ste->ste_unoptimized = 0; ste->ste_nested = 0; @@ -102,6 +104,7 @@ ste_dealloc(PySTEntryObject *ste) Py_XDECREF(ste->ste_symbols); Py_XDECREF(ste->ste_varnames); Py_XDECREF(ste->ste_children); + Py_XDECREF(ste->ste_directives); PyObject_Del(ste); } @@ -319,6 +322,24 @@ PyST_GetScope(PySTEntryObject *ste, PyObject *name) return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK; } +static int +error_at_directive(PySTEntryObject *ste, PyObject *name) +{ + Py_ssize_t i; + PyObject *data; + assert(ste->ste_directives); + for (i = 0; ; i++) { + data = PyList_GET_ITEM(ste->ste_directives, i); + assert(PyTuple_CheckExact(data)); + if (PyTuple_GET_ITEM(data, 0) == name) + break; + } + PyErr_SyntaxLocationEx(ste->ste_table->st_filename, + PyLong_AsLong(PyTuple_GET_ITEM(data, 1)), + PyLong_AsLong(PyTuple_GET_ITEM(data, 2))); + return 0; +} + /* Analyze raw symbol information to determine scope of each name. @@ -393,16 +414,13 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, PyErr_Format(PyExc_SyntaxError, "name '%U' is parameter and global", name); - PyErr_SyntaxLocationEx(ste->ste_table->st_filename, - ste->ste_lineno, ste->ste_col_offset); - - return 0; + return error_at_directive(ste, name); } if (flags & DEF_NONLOCAL) { PyErr_Format(PyExc_SyntaxError, "name '%U' is nonlocal and global", name); - return 0; + return error_at_directive(ste, name); } SET_SCOPE(scopes, name, GLOBAL_EXPLICIT); if (PySet_Add(global, name) < 0) @@ -416,19 +434,19 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, PyErr_Format(PyExc_SyntaxError, "name '%U' is parameter and nonlocal", name); - return 0; + return error_at_directive(ste, name); } if (!bound) { PyErr_Format(PyExc_SyntaxError, "nonlocal declaration not allowed at module level"); - return 0; + return error_at_directive(ste, name); } if (!PySet_Contains(bound, name)) { PyErr_Format(PyExc_SyntaxError, "no binding for nonlocal '%U' found", name); - return 0; + return error_at_directive(ste, name); } SET_SCOPE(scopes, name, FREE); ste->ste_free = 1; @@ -1068,6 +1086,25 @@ symtable_new_tmpname(struct symtable *st) } +static int +symtable_record_directive(struct symtable *st, identifier name, stmt_ty s) +{ + PyObject *data; + int res; + if (!st->st_cur->ste_directives) { + st->st_cur->ste_directives = PyList_New(0); + if (!st->st_cur->ste_directives) + return 0; + } + data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset); + if (!data) + return 0; + res = PyList_Append(st->st_cur->ste_directives, data); + Py_DECREF(data); + return res == 0; +} + + static int symtable_visit_stmt(struct symtable *st, stmt_ty s) { @@ -1223,6 +1260,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) } if (!symtable_add_def(st, name, DEF_GLOBAL)) return 0; + if (!symtable_record_directive(st, name, s)) + return 0; } break; } @@ -1252,6 +1291,8 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) } if (!symtable_add_def(st, name, DEF_NONLOCAL)) return 0; + if (!symtable_record_directive(st, name, s)) + return 0; } break; } -- cgit v1.2.1 From 8fbf4fa9edcb9efd1ca5d650ed235eb4770dea3a Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 6 Dec 2012 17:41:04 -0500 Subject: create NameConstant AST class for None, True, and False literals (closes #16619) --- Python/symtable.c | 1 + 1 file changed, 1 insertion(+) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index 8d941f0d54..d16bfbcad3 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -1437,6 +1437,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e) case Str_kind: case Bytes_kind: case Ellipsis_kind: + case NameConstant_kind: /* Nothing to do here. */ break; /* The following exprs can be assignment targets. */ -- cgit v1.2.1 From 7eadff200c3acb1286b95d417edfcc56cee617ee Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 18 Mar 2013 10:48:58 -0700 Subject: unify some ast.argument's attrs; change Attribute column offset (closes #16795) Patch from Sven Brauch. --- Python/symtable.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index d16bfbcad3..b9ca615721 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -1530,10 +1530,10 @@ symtable_visit_annotations(struct symtable *st, stmt_ty s) if (a->args && !symtable_visit_argannotations(st, a->args)) return 0; - if (a->varargannotation) - VISIT(st, expr, a->varargannotation); - if (a->kwargannotation) - VISIT(st, expr, a->kwargannotation); + if (a->vararg && a->vararg->annotation) + VISIT(st, expr, a->vararg->annotation); + if (a->kwarg && a->kwarg->annotation) + VISIT(st, expr, a->kwarg->annotation); if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs)) return 0; if (s->v.FunctionDef.returns) @@ -1552,12 +1552,12 @@ symtable_visit_arguments(struct symtable *st, arguments_ty a) if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs)) return 0; if (a->vararg) { - if (!symtable_add_def(st, a->vararg, DEF_PARAM)) + if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM)) return 0; st->st_cur->ste_varargs = 1; } if (a->kwarg) { - if (!symtable_add_def(st, a->kwarg, DEF_PARAM)) + if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM)) return 0; st->st_cur->ste_varkeywords = 1; } -- cgit v1.2.1 From f773ed5048253f3e3d86b28e47f8245c2542027c Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 15 May 2013 15:26:42 -0500 Subject: hide the __class__ closure from the class body (#12370) --- Python/symtable.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index b9ca615721..056c6c2858 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -77,6 +77,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, ste->ste_child_free = 0; ste->ste_generator = 0; ste->ste_returns_value = 0; + ste->ste_needs_class_closure = 0; if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0) goto fail; @@ -514,13 +515,10 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, Note that the current block's free variables are included in free. That's safe because no name can be free and local in the same scope. - - The 'restricted' argument may be set to a string to restrict the analysis - to the one variable whose name equals that string (e.g. "__class__"). */ static int -analyze_cells(PyObject *scopes, PyObject *free, const char *restricted) +analyze_cells(PyObject *scopes, PyObject *free) { PyObject *name, *v, *v_cell; int success = 0; @@ -537,9 +535,6 @@ analyze_cells(PyObject *scopes, PyObject *free, const char *restricted) continue; if (!PySet_Contains(free, name)) continue; - if (restricted != NULL && - PyUnicode_CompareWithASCIIString(name, restricted)) - continue; /* Replace LOCAL with CELL for this name, and remove from free. It is safe to replace the value of name in the dict, because it will not cause a resize. @@ -555,6 +550,20 @@ analyze_cells(PyObject *scopes, PyObject *free, const char *restricted) return success; } +static int +drop_class_free(PySTEntryObject *ste, PyObject *free) +{ + int res; + if (!GET_IDENTIFIER(__class__)) + return 0; + res = PySet_Discard(free, __class__); + if (res < 0) + return 0; + if (res) + ste->ste_needs_class_closure = 1; + return 1; +} + /* Check for illegal statements in unoptimized namespaces */ static int check_unoptimized(const PySTEntryObject* ste) { @@ -785,7 +794,6 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, /* Special-case __class__ */ if (!GET_IDENTIFIER(__class__)) goto error; - assert(PySet_Contains(local, __class__) == 1); if (PySet_Add(newbound, __class__) < 0) goto error; } @@ -818,11 +826,9 @@ analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, Py_DECREF(temp); /* Check if any local variables must be converted to cell variables */ - if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree, - NULL)) + if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree)) goto error; - else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree, - "__class__")) + else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree)) goto error; /* Records the results of the analysis in the symbol table entry */ if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, @@ -1179,9 +1185,7 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, (void *)s, s->lineno, s->col_offset)) VISIT_QUIT(st, 0); - if (!GET_IDENTIFIER(__class__) || - !symtable_add_def(st, __class__, DEF_LOCAL) || - !GET_IDENTIFIER(__locals__) || + if (!GET_IDENTIFIER(__locals__) || !symtable_add_def(st, __locals__, DEF_PARAM)) { symtable_exit_block(st, s); VISIT_QUIT(st, 0); -- cgit v1.2.1 From df197a8dcb7f4aba4ba0e6b28f910cb28088549e Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 16 May 2013 14:37:25 -0500 Subject: rather than passing locals to the class body, just execute the class body in the proper environment --- Python/symtable.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index 056c6c2858..758aefb903 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -192,7 +192,7 @@ static int symtable_visit_withitem(struct symtable *st, withitem_ty item); static identifier top = NULL, lambda = NULL, genexpr = NULL, listcomp = NULL, setcomp = NULL, dictcomp = NULL, - __class__ = NULL, __locals__ = NULL; + __class__ = NULL; #define GET_IDENTIFIER(VAR) \ ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR))) @@ -1185,11 +1185,6 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, (void *)s, s->lineno, s->col_offset)) VISIT_QUIT(st, 0); - if (!GET_IDENTIFIER(__locals__) || - !symtable_add_def(st, __locals__, DEF_PARAM)) { - symtable_exit_block(st, s); - VISIT_QUIT(st, 0); - } tmp = st->st_private; st->st_private = s->v.ClassDef.name; VISIT_SEQ(st, stmt, s->v.ClassDef.body); -- cgit v1.2.1 From ec1218faf67f13f151aac35ef29ff69fde58256f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 11 Jul 2013 22:49:00 +0200 Subject: Issue #18408: ste_new() initialize all attributes before handling error If an attribute is not initialized, the destructor can crash --- Python/symtable.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'Python/symtable.c') diff --git a/Python/symtable.c b/Python/symtable.c index 758aefb903..183bf69133 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -37,25 +37,13 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, ste->ste_table = st; ste->ste_id = k; /* ste owns reference to k */ - ste->ste_name = name; Py_INCREF(name); + ste->ste_name = name; ste->ste_symbols = NULL; ste->ste_varnames = NULL; ste->ste_children = NULL; - ste->ste_symbols = PyDict_New(); - if (ste->ste_symbols == NULL) - goto fail; - - ste->ste_varnames = PyList_New(0); - if (ste->ste_varnames == NULL) - goto fail; - - ste->ste_children = PyList_New(0); - if (ste->ste_children == NULL) - goto fail; - ste->ste_directives = NULL; ste->ste_type = block; @@ -79,6 +67,14 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block, ste->ste_returns_value = 0; ste->ste_needs_class_closure = 0; + ste->ste_symbols = PyDict_New(); + ste->ste_varnames = PyList_New(0); + ste->ste_children = PyList_New(0); + if (ste->ste_symbols == NULL + || ste->ste_varnames == NULL + || ste->ste_children == NULL) + goto fail; + if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0) goto fail; -- cgit v1.2.1