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