From 81e9502df69394821416309c7c4b5357af51f4d5 Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Tue, 27 Feb 2007 06:50:52 +0000 Subject: Provisional implementation of PEP 3104. Add nonlocal_stmt to Grammar and Nonlocal node to AST. They both parallel the definitions for globals. The symbol table treats variables declared as nonlocal just like variables that are free implicitly. This change is missing the language spec changes, but makes some decisions about what the spec should say via the unittests. The PEP is silent on a number of decisions, so we should review those before claiming that nonlocal is complete. Thomas Wouters made the grammer and ast changes. Jeremy Hylton added the symbol table changes and the tests. Pete Shinners and Neal Norwitz helped review the code. --- Python/Python-ast.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'Python/Python-ast.c') diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 5aefbacf65..7f1640f57b 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -131,6 +131,10 @@ static PyTypeObject *Global_type; static char *Global_fields[]={ "names", }; +static PyTypeObject *Nonlocal_type; +static char *Nonlocal_fields[]={ + "names", +}; static PyTypeObject *Expr_type; static char *Expr_fields[]={ "value", @@ -507,6 +511,8 @@ static int init_types(void) if (!ImportFrom_type) return 0; Global_type = make_type("Global", stmt_type, Global_fields, 1); if (!Global_type) return 0; + Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1); + if (!Nonlocal_type) return 0; Expr_type = make_type("Expr", stmt_type, Expr_fields, 1); if (!Expr_type) return 0; Pass_type = make_type("Pass", stmt_type, NULL, 0); @@ -1145,6 +1151,20 @@ Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena) return p; } +stmt_ty +Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena) +{ + stmt_ty p; + p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); + if (!p) + return NULL; + p->kind = Nonlocal_kind; + p->v.Nonlocal.names = names; + p->lineno = lineno; + p->col_offset = col_offset; + return p; +} + stmt_ty Expr(expr_ty value, int lineno, int col_offset, PyArena *arena) { @@ -2197,6 +2217,15 @@ ast2obj_stmt(void* _o) goto failed; Py_DECREF(value); break; + case Nonlocal_kind: + result = PyType_GenericNew(Nonlocal_type, NULL, NULL); + if (!result) goto failed; + value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "names", value) == -1) + goto failed; + Py_DECREF(value); + break; case Expr_kind: result = PyType_GenericNew(Expr_type, NULL, NULL); if (!result) goto failed; @@ -3049,6 +3078,8 @@ init_ast(void) 0) return; if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return; + if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0) + return; if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return; if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return; if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return; -- cgit v1.2.1