comparison Python/Python-ast.c @ 36666:6165927431d6 legacy-trunk

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 "<stdin>", line 1, in <module> TypeError: iteration over non-sequence changes into >>> (x for x in lambda: 0) File "<stdin>", 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 ;)
author Thomas Wouters <thomas@python.org>
date Mon, 27 Feb 2006 00:24:13 +0000
parents 58a8451ff795
children 9504f7ac3669
comparison
equal deleted inserted replaced
36665:58a8451ff795 36666:6165927431d6
148 }; 148 };
149 PyTypeObject *Lambda_type; 149 PyTypeObject *Lambda_type;
150 char *Lambda_fields[]={ 150 char *Lambda_fields[]={
151 "args", 151 "args",
152 "body", 152 "body",
153 };
154 PyTypeObject *IfExp_type;
155 char *IfExp_fields[]={
156 "test",
157 "body",
158 "orelse",
153 }; 159 };
154 PyTypeObject *Dict_type; 160 PyTypeObject *Dict_type;
155 char *Dict_fields[]={ 161 char *Dict_fields[]={
156 "keys", 162 "keys",
157 "values", 163 "values",
429 expr_type = make_type("expr", &PyBaseObject_Type, NULL, 0); 435 expr_type = make_type("expr", &PyBaseObject_Type, NULL, 0);
430 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2); 436 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
431 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3); 437 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
432 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2); 438 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
433 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2); 439 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
440 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
434 Dict_type = make_type("Dict", expr_type, Dict_fields, 2); 441 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
435 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2); 442 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
436 GeneratorExp_type = make_type("GeneratorExp", expr_type, 443 GeneratorExp_type = make_type("GeneratorExp", expr_type,
437 GeneratorExp_fields, 2); 444 GeneratorExp_fields, 2);
438 Yield_type = make_type("Yield", expr_type, Yield_fields, 1); 445 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
1136 p->lineno = lineno; 1143 p->lineno = lineno;
1137 return p; 1144 return p;
1138 } 1145 }
1139 1146
1140 expr_ty 1147 expr_ty
1148 IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, PyArena *arena)
1149 {
1150 expr_ty p;
1151 if (!test) {
1152 PyErr_SetString(PyExc_ValueError,
1153 "field test is required for IfExp");
1154 return NULL;
1155 }
1156 if (!body) {
1157 PyErr_SetString(PyExc_ValueError,
1158 "field body is required for IfExp");
1159 return NULL;
1160 }
1161 if (!orelse) {
1162 PyErr_SetString(PyExc_ValueError,
1163 "field orelse is required for IfExp");
1164 return NULL;
1165 }
1166 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1167 if (!p) {
1168 PyErr_NoMemory();
1169 return NULL;
1170 }
1171 p->kind = IfExp_kind;
1172 p->v.IfExp.test = test;
1173 p->v.IfExp.body = body;
1174 p->v.IfExp.orelse = orelse;
1175 p->lineno = lineno;
1176 return p;
1177 }
1178
1179 expr_ty
1141 Dict(asdl_seq * keys, asdl_seq * values, int lineno, PyArena *arena) 1180 Dict(asdl_seq * keys, asdl_seq * values, int lineno, PyArena *arena)
1142 { 1181 {
1143 expr_ty p; 1182 expr_ty p;
1144 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); 1183 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1145 if (!p) { 1184 if (!p) {
2072 goto failed; 2111 goto failed;
2073 Py_DECREF(value); 2112 Py_DECREF(value);
2074 value = ast2obj_expr(o->v.Lambda.body); 2113 value = ast2obj_expr(o->v.Lambda.body);
2075 if (!value) goto failed; 2114 if (!value) goto failed;
2076 if (PyObject_SetAttrString(result, "body", value) == -1) 2115 if (PyObject_SetAttrString(result, "body", value) == -1)
2116 goto failed;
2117 Py_DECREF(value);
2118 break;
2119 case IfExp_kind:
2120 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2121 if (!result) goto failed;
2122 value = ast2obj_expr(o->v.IfExp.test);
2123 if (!value) goto failed;
2124 if (PyObject_SetAttrString(result, "test", value) == -1)
2125 goto failed;
2126 Py_DECREF(value);
2127 value = ast2obj_expr(o->v.IfExp.body);
2128 if (!value) goto failed;
2129 if (PyObject_SetAttrString(result, "body", value) == -1)
2130 goto failed;
2131 Py_DECREF(value);
2132 value = ast2obj_expr(o->v.IfExp.orelse);
2133 if (!value) goto failed;
2134 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2077 goto failed; 2135 goto failed;
2078 Py_DECREF(value); 2136 Py_DECREF(value);
2079 break; 2137 break;
2080 case Dict_kind: 2138 case Dict_kind:
2081 result = PyType_GenericNew(Dict_type, NULL, NULL); 2139 result = PyType_GenericNew(Dict_type, NULL, NULL);