summaryrefslogtreecommitdiff
path: root/Python/Python-ast.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2007-02-27 06:50:52 +0000
committerJeremy Hylton <jeremy@alum.mit.edu>2007-02-27 06:50:52 +0000
commit81e9502df69394821416309c7c4b5357af51f4d5 (patch)
treead38831cbebfb32890c0c57cb8b36f653300c69f /Python/Python-ast.c
parent8b41c3dc28a16da97af50cc5f7b884db2cea7b0c (diff)
downloadcpython-git-81e9502df69394821416309c7c4b5357af51f4d5.tar.gz
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.
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r--Python/Python-ast.c31
1 files changed, 31 insertions, 0 deletions
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);
@@ -1146,6 +1152,20 @@ Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
}
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)
{
stmt_ty p;
@@ -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;