From dca3b9c797f6dd4b08d590fa2aa1031e22ab598e Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Mon, 27 Feb 2006 00:24:13 +0000 Subject: PEP 308 implementation, including minor refdocs and some testcases. It breaks the parser module, because it adds the if/else construct as well as two new grammar rules for backward compatibility. If no one else fixes parsermodule, I guess I'll go ahead and fix it later this week. The TeX code was checked with texcheck.py, but not rendered. There is actually a slight incompatibility: >>> (x for x in lambda:0) Traceback (most recent call last): File "", line 1, in TypeError: iteration over non-sequence changes into >>> (x for x in lambda: 0) File "", line 1 (x for x in lambda: 0) ^ SyntaxError: invalid syntax Since there's no way the former version can be useful, it's probably a bugfix ;) --- Python/Python-ast.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'Python/Python-ast.c') diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 6935851df3..8d148e37d0 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -151,6 +151,12 @@ char *Lambda_fields[]={ "args", "body", }; +PyTypeObject *IfExp_type; +char *IfExp_fields[]={ + "test", + "body", + "orelse", +}; PyTypeObject *Dict_type; char *Dict_fields[]={ "keys", @@ -431,6 +437,7 @@ static int init_types(void) BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3); UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2); Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2); + IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3); Dict_type = make_type("Dict", expr_type, Dict_fields, 2); ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); GeneratorExp_type = make_type("GeneratorExp", expr_type, @@ -1137,6 +1144,38 @@ Lambda(arguments_ty args, expr_ty body, int lineno, PyArena *arena) return p; } +expr_ty +IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, PyArena *arena) +{ + expr_ty p; + if (!test) { + PyErr_SetString(PyExc_ValueError, + "field test is required for IfExp"); + return NULL; + } + if (!body) { + PyErr_SetString(PyExc_ValueError, + "field body is required for IfExp"); + return NULL; + } + if (!orelse) { + PyErr_SetString(PyExc_ValueError, + "field orelse is required for IfExp"); + return NULL; + } + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); + if (!p) { + PyErr_NoMemory(); + return NULL; + } + p->kind = IfExp_kind; + p->v.IfExp.test = test; + p->v.IfExp.body = body; + p->v.IfExp.orelse = orelse; + p->lineno = lineno; + return p; +} + expr_ty Dict(asdl_seq * keys, asdl_seq * values, int lineno, PyArena *arena) { @@ -2077,6 +2116,25 @@ ast2obj_expr(void* _o) goto failed; Py_DECREF(value); break; + case IfExp_kind: + result = PyType_GenericNew(IfExp_type, NULL, NULL); + if (!result) goto failed; + value = ast2obj_expr(o->v.IfExp.test); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "test", value) == -1) + goto failed; + Py_DECREF(value); + value = ast2obj_expr(o->v.IfExp.body); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "body", value) == -1) + goto failed; + Py_DECREF(value); + value = ast2obj_expr(o->v.IfExp.orelse); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "orelse", value) == -1) + goto failed; + Py_DECREF(value); + break; case Dict_kind: result = PyType_GenericNew(Dict_type, NULL, NULL); if (!result) goto failed; -- cgit v1.2.1