summaryrefslogtreecommitdiffstats
path: root/Python/Python-ast.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-02-27 22:32:47 (GMT)
committerGuido van Rossum <guido@python.org>2006-02-27 22:32:47 (GMT)
commitc2e20744b2b7811632030470971c31630f0975e2 (patch)
treee97b1c1471fd00e4e5648ed317274c1d9005d2ca /Python/Python-ast.c
parent5fec904f84a40005f824abe295525a1710056be0 (diff)
downloadcpython-c2e20744b2b7811632030470971c31630f0975e2.zip
cpython-c2e20744b2b7811632030470971c31630f0975e2.tar.gz
cpython-c2e20744b2b7811632030470971c31630f0975e2.tar.bz2
PEP 343 -- the with-statement.
This was started by Mike Bland and completed by Guido (with help from Neal). This still needs a __future__ statement added; Thomas is working on Michael's patch for that aspect. There's a small amount of code cleanup and refactoring in ast.c, compile.c and ceval.c (I fixed the lltrace behavior when EXT_POP is used -- however I had to make lltrace a static global).
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r--Python/Python-ast.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index f3b4053..1403143 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -84,6 +84,12 @@ char *If_fields[]={
"body",
"orelse",
};
+PyTypeObject *With_type;
+char *With_fields[]={
+ "context_expr",
+ "optional_vars",
+ "body",
+};
PyTypeObject *Raise_type;
char *Raise_fields[]={
"type",
@@ -465,6 +471,8 @@ static int init_types(void)
if (!While_type) return 0;
If_type = make_type("If", stmt_type, If_fields, 3);
if (!If_type) return 0;
+ With_type = make_type("With", stmt_type, With_fields, 3);
+ if (!With_type) return 0;
Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
if (!Raise_type) return 0;
TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
@@ -1000,6 +1008,29 @@ If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, PyArena *arena)
}
stmt_ty
+With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
+ PyArena *arena)
+{
+ stmt_ty p;
+ if (!context_expr) {
+ PyErr_SetString(PyExc_ValueError,
+ "field context_expr is required for With");
+ return NULL;
+ }
+ p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
+ if (!p) {
+ PyErr_NoMemory();
+ return NULL;
+ }
+ p->kind = With_kind;
+ p->v.With.context_expr = context_expr;
+ p->v.With.optional_vars = optional_vars;
+ p->v.With.body = body;
+ p->lineno = lineno;
+ return p;
+}
+
+stmt_ty
Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, PyArena *arena)
{
stmt_ty p;
@@ -2062,6 +2093,26 @@ ast2obj_stmt(void* _o)
goto failed;
Py_DECREF(value);
break;
+ case With_kind:
+ result = PyType_GenericNew(With_type, NULL, NULL);
+ if (!result) goto failed;
+ value = ast2obj_expr(o->v.With.context_expr);
+ if (!value) goto failed;
+ if (PyObject_SetAttrString(result, "context_expr", value) == -1)
+ goto failed;
+ Py_DECREF(value);
+ value = ast2obj_expr(o->v.With.optional_vars);
+ if (!value) goto failed;
+ if (PyObject_SetAttrString(result, "optional_vars", value) ==
+ -1)
+ goto failed;
+ Py_DECREF(value);
+ value = ast2obj_list(o->v.With.body, ast2obj_stmt);
+ if (!value) goto failed;
+ if (PyObject_SetAttrString(result, "body", value) == -1)
+ goto failed;
+ Py_DECREF(value);
+ break;
case Raise_kind:
result = PyType_GenericNew(Raise_type, NULL, NULL);
if (!result) goto failed;
@@ -2922,6 +2973,7 @@ init_ast(void)
if(PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
if(PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
if(PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
+ if(PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
if(PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
if(PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) < 0)
return;