comparison Python/Python-ast.c @ 40965:30e5cb3a55ac

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.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Tue, 27 Feb 2007 06:50:52 +0000
parents 2ca14d6a369b
children c3b28ba918f8
comparison
equal deleted inserted replaced
40964:d5546f09f20e 40965:30e5cb3a55ac
127 "names", 127 "names",
128 "level", 128 "level",
129 }; 129 };
130 static PyTypeObject *Global_type; 130 static PyTypeObject *Global_type;
131 static char *Global_fields[]={ 131 static char *Global_fields[]={
132 "names",
133 };
134 static PyTypeObject *Nonlocal_type;
135 static char *Nonlocal_fields[]={
132 "names", 136 "names",
133 }; 137 };
134 static PyTypeObject *Expr_type; 138 static PyTypeObject *Expr_type;
135 static char *Expr_fields[]={ 139 static char *Expr_fields[]={
136 "value", 140 "value",
505 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 509 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
506 3); 510 3);
507 if (!ImportFrom_type) return 0; 511 if (!ImportFrom_type) return 0;
508 Global_type = make_type("Global", stmt_type, Global_fields, 1); 512 Global_type = make_type("Global", stmt_type, Global_fields, 1);
509 if (!Global_type) return 0; 513 if (!Global_type) return 0;
514 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
515 if (!Nonlocal_type) return 0;
510 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1); 516 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
511 if (!Expr_type) return 0; 517 if (!Expr_type) return 0;
512 Pass_type = make_type("Pass", stmt_type, NULL, 0); 518 Pass_type = make_type("Pass", stmt_type, NULL, 0);
513 if (!Pass_type) return 0; 519 if (!Pass_type) return 0;
514 Break_type = make_type("Break", stmt_type, NULL, 0); 520 Break_type = make_type("Break", stmt_type, NULL, 0);
1144 p->col_offset = col_offset; 1150 p->col_offset = col_offset;
1145 return p; 1151 return p;
1146 } 1152 }
1147 1153
1148 stmt_ty 1154 stmt_ty
1155 Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1156 {
1157 stmt_ty p;
1158 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1159 if (!p)
1160 return NULL;
1161 p->kind = Nonlocal_kind;
1162 p->v.Nonlocal.names = names;
1163 p->lineno = lineno;
1164 p->col_offset = col_offset;
1165 return p;
1166 }
1167
1168 stmt_ty
1149 Expr(expr_ty value, int lineno, int col_offset, PyArena *arena) 1169 Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1150 { 1170 {
1151 stmt_ty p; 1171 stmt_ty p;
1152 if (!value) { 1172 if (!value) {
1153 PyErr_SetString(PyExc_ValueError, 1173 PyErr_SetString(PyExc_ValueError,
2190 break; 2210 break;
2191 case Global_kind: 2211 case Global_kind:
2192 result = PyType_GenericNew(Global_type, NULL, NULL); 2212 result = PyType_GenericNew(Global_type, NULL, NULL);
2193 if (!result) goto failed; 2213 if (!result) goto failed;
2194 value = ast2obj_list(o->v.Global.names, ast2obj_identifier); 2214 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2215 if (!value) goto failed;
2216 if (PyObject_SetAttrString(result, "names", value) == -1)
2217 goto failed;
2218 Py_DECREF(value);
2219 break;
2220 case Nonlocal_kind:
2221 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2222 if (!result) goto failed;
2223 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2195 if (!value) goto failed; 2224 if (!value) goto failed;
2196 if (PyObject_SetAttrString(result, "names", value) == -1) 2225 if (PyObject_SetAttrString(result, "names", value) == -1)
2197 goto failed; 2226 goto failed;
2198 Py_DECREF(value); 2227 Py_DECREF(value);
2199 break; 2228 break;
3046 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) 3075 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
3047 return; 3076 return;
3048 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 3077 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
3049 0) return; 3078 0) return;
3050 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) 3079 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
3080 return;
3081 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
3051 return; 3082 return;
3052 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return; 3083 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
3053 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return; 3084 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
3054 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return; 3085 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
3055 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0) 3086 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)