comparison Python/Python-ast.c @ 70438:9b11cc4e2918

reflect with statements with multiple items in the AST (closes #12106)
author Benjamin Peterson <benjamin@python.org>
date Fri, 27 May 2011 13:58:08 -0500
parents 5f23a6557e36
children 69d4938382c1
comparison
equal deleted inserted replaced
70437:4d2ddd86b531 70438:9b11cc4e2918
93 "body", 93 "body",
94 "orelse", 94 "orelse",
95 }; 95 };
96 static PyTypeObject *With_type; 96 static PyTypeObject *With_type;
97 static char *With_fields[]={ 97 static char *With_fields[]={
98 "context_expr", 98 "items",
99 "optional_vars",
100 "body", 99 "body",
101 }; 100 };
102 static PyTypeObject *Raise_type; 101 static PyTypeObject *Raise_type;
103 static char *Raise_fields[]={ 102 static char *Raise_fields[]={
104 "exc", 103 "exc",
390 static PyObject* ast2obj_alias(void*); 389 static PyObject* ast2obj_alias(void*);
391 static char *alias_fields[]={ 390 static char *alias_fields[]={
392 "name", 391 "name",
393 "asname", 392 "asname",
394 }; 393 };
394 static PyTypeObject *withitem_type;
395 static PyObject* ast2obj_withitem(void*);
396 static char *withitem_fields[]={
397 "context_expr",
398 "optional_vars",
399 };
395 400
396 401
397 static int 402 static int
398 ast_type_init(PyObject *self, PyObject *args, PyObject *kw) 403 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
399 { 404 {
678 if (!For_type) return 0; 683 if (!For_type) return 0;
679 While_type = make_type("While", stmt_type, While_fields, 3); 684 While_type = make_type("While", stmt_type, While_fields, 3);
680 if (!While_type) return 0; 685 if (!While_type) return 0;
681 If_type = make_type("If", stmt_type, If_fields, 3); 686 If_type = make_type("If", stmt_type, If_fields, 3);
682 if (!If_type) return 0; 687 if (!If_type) return 0;
683 With_type = make_type("With", stmt_type, With_fields, 3); 688 With_type = make_type("With", stmt_type, With_fields, 2);
684 if (!With_type) return 0; 689 if (!With_type) return 0;
685 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2); 690 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
686 if (!Raise_type) return 0; 691 if (!Raise_type) return 0;
687 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3); 692 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
688 if (!TryExcept_type) return 0; 693 if (!TryExcept_type) return 0;
936 if (!arg_type) return 0; 941 if (!arg_type) return 0;
937 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2); 942 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
938 if (!keyword_type) return 0; 943 if (!keyword_type) return 0;
939 alias_type = make_type("alias", &AST_type, alias_fields, 2); 944 alias_type = make_type("alias", &AST_type, alias_fields, 2);
940 if (!alias_type) return 0; 945 if (!alias_type) return 0;
946 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
947 if (!withitem_type) return 0;
941 initialized = 1; 948 initialized = 1;
942 return 1; 949 return 1;
943 } 950 }
944 951
945 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena); 952 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
958 arena); 965 arena);
959 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena); 966 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
960 static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena); 967 static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
961 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena); 968 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
962 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena); 969 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
970 static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
963 971
964 mod_ty 972 mod_ty
965 Module(asdl_seq * body, PyArena *arena) 973 Module(asdl_seq * body, PyArena *arena)
966 { 974 {
967 mod_ty p; 975 mod_ty p;
1223 p->col_offset = col_offset; 1231 p->col_offset = col_offset;
1224 return p; 1232 return p;
1225 } 1233 }
1226 1234
1227 stmt_ty 1235 stmt_ty
1228 With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno, 1236 With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1229 int col_offset, PyArena *arena) 1237 *arena)
1230 { 1238 {
1231 stmt_ty p; 1239 stmt_ty p;
1232 if (!context_expr) {
1233 PyErr_SetString(PyExc_ValueError,
1234 "field context_expr is required for With");
1235 return NULL;
1236 }
1237 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); 1240 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1238 if (!p) 1241 if (!p)
1239 return NULL; 1242 return NULL;
1240 p->kind = With_kind; 1243 p->kind = With_kind;
1241 p->v.With.context_expr = context_expr; 1244 p->v.With.items = items;
1242 p->v.With.optional_vars = optional_vars;
1243 p->v.With.body = body; 1245 p->v.With.body = body;
1244 p->lineno = lineno; 1246 p->lineno = lineno;
1245 p->col_offset = col_offset; 1247 p->col_offset = col_offset;
1246 return p; 1248 return p;
1247 } 1249 }
2133 p->name = name; 2135 p->name = name;
2134 p->asname = asname; 2136 p->asname = asname;
2135 return p; 2137 return p;
2136 } 2138 }
2137 2139
2140 withitem_ty
2141 withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2142 {
2143 withitem_ty p;
2144 if (!context_expr) {
2145 PyErr_SetString(PyExc_ValueError,
2146 "field context_expr is required for withitem");
2147 return NULL;
2148 }
2149 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2150 if (!p)
2151 return NULL;
2152 p->context_expr = context_expr;
2153 p->optional_vars = optional_vars;
2154 return p;
2155 }
2156
2138 2157
2139 PyObject* 2158 PyObject*
2140 ast2obj_mod(void* _o) 2159 ast2obj_mod(void* _o)
2141 { 2160 {
2142 mod_ty o = (mod_ty)_o; 2161 mod_ty o = (mod_ty)_o;
2388 Py_DECREF(value); 2407 Py_DECREF(value);
2389 break; 2408 break;
2390 case With_kind: 2409 case With_kind:
2391 result = PyType_GenericNew(With_type, NULL, NULL); 2410 result = PyType_GenericNew(With_type, NULL, NULL);
2392 if (!result) goto failed; 2411 if (!result) goto failed;
2393 value = ast2obj_expr(o->v.With.context_expr); 2412 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2394 if (!value) goto failed; 2413 if (!value) goto failed;
2395 if (PyObject_SetAttrString(result, "context_expr", value) == -1) 2414 if (PyObject_SetAttrString(result, "items", value) == -1)
2396 goto failed;
2397 Py_DECREF(value);
2398 value = ast2obj_expr(o->v.With.optional_vars);
2399 if (!value) goto failed;
2400 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2401 -1)
2402 goto failed; 2415 goto failed;
2403 Py_DECREF(value); 2416 Py_DECREF(value);
2404 value = ast2obj_list(o->v.With.body, ast2obj_stmt); 2417 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2405 if (!value) goto failed; 2418 if (!value) goto failed;
2406 if (PyObject_SetAttrString(result, "body", value) == -1) 2419 if (PyObject_SetAttrString(result, "body", value) == -1)
3368 Py_XDECREF(value); 3381 Py_XDECREF(value);
3369 Py_XDECREF(result); 3382 Py_XDECREF(result);
3370 return NULL; 3383 return NULL;
3371 } 3384 }
3372 3385
3386 PyObject*
3387 ast2obj_withitem(void* _o)
3388 {
3389 withitem_ty o = (withitem_ty)_o;
3390 PyObject *result = NULL, *value = NULL;
3391 if (!o) {
3392 Py_INCREF(Py_None);
3393 return Py_None;
3394 }
3395
3396 result = PyType_GenericNew(withitem_type, NULL, NULL);
3397 if (!result) return NULL;
3398 value = ast2obj_expr(o->context_expr);
3399 if (!value) goto failed;
3400 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3401 goto failed;
3402 Py_DECREF(value);
3403 value = ast2obj_expr(o->optional_vars);
3404 if (!value) goto failed;
3405 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3406 goto failed;
3407 Py_DECREF(value);
3408 return result;
3409 failed:
3410 Py_XDECREF(value);
3411 Py_XDECREF(result);
3412 return NULL;
3413 }
3414
3373 3415
3374 int 3416 int
3375 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena) 3417 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3376 { 3418 {
3377 int isinstance; 3419 int isinstance;
4208 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type); 4250 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4209 if (isinstance == -1) { 4251 if (isinstance == -1) {
4210 return 1; 4252 return 1;
4211 } 4253 }
4212 if (isinstance) { 4254 if (isinstance) {
4213 expr_ty context_expr; 4255 asdl_seq* items;
4214 expr_ty optional_vars;
4215 asdl_seq* body; 4256 asdl_seq* body;
4216 4257
4217 if (PyObject_HasAttrString(obj, "context_expr")) { 4258 if (PyObject_HasAttrString(obj, "items")) {
4218 int res; 4259 int res;
4219 tmp = PyObject_GetAttrString(obj, "context_expr"); 4260 Py_ssize_t len;
4220 if (tmp == NULL) goto failed; 4261 Py_ssize_t i;
4221 res = obj2ast_expr(tmp, &context_expr, arena); 4262 tmp = PyObject_GetAttrString(obj, "items");
4222 if (res != 0) goto failed; 4263 if (tmp == NULL) goto failed;
4223 Py_XDECREF(tmp); 4264 if (!PyList_Check(tmp)) {
4224 tmp = NULL; 4265 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4225 } else { 4266 goto failed;
4226 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With"); 4267 }
4227 return 1; 4268 len = PyList_GET_SIZE(tmp);
4228 } 4269 items = asdl_seq_new(len, arena);
4229 if (PyObject_HasAttrString(obj, "optional_vars")) { 4270 if (items == NULL) goto failed;
4230 int res; 4271 for (i = 0; i < len; i++) {
4231 tmp = PyObject_GetAttrString(obj, "optional_vars"); 4272 withitem_ty value;
4232 if (tmp == NULL) goto failed; 4273 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4233 res = obj2ast_expr(tmp, &optional_vars, arena); 4274 if (res != 0) goto failed;
4234 if (res != 0) goto failed; 4275 asdl_seq_SET(items, i, value);
4235 Py_XDECREF(tmp); 4276 }
4236 tmp = NULL; 4277 Py_XDECREF(tmp);
4237 } else { 4278 tmp = NULL;
4238 optional_vars = NULL; 4279 } else {
4280 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4281 return 1;
4239 } 4282 }
4240 if (PyObject_HasAttrString(obj, "body")) { 4283 if (PyObject_HasAttrString(obj, "body")) {
4241 int res; 4284 int res;
4242 Py_ssize_t len; 4285 Py_ssize_t len;
4243 Py_ssize_t i; 4286 Py_ssize_t i;
4260 tmp = NULL; 4303 tmp = NULL;
4261 } else { 4304 } else {
4262 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With"); 4305 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4263 return 1; 4306 return 1;
4264 } 4307 }
4265 *out = With(context_expr, optional_vars, body, lineno, 4308 *out = With(items, body, lineno, col_offset, arena);
4266 col_offset, arena);
4267 if (*out == NULL) goto failed; 4309 if (*out == NULL) goto failed;
4268 return 0; 4310 return 0;
4269 } 4311 }
4270 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type); 4312 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4271 if (isinstance == -1) { 4313 if (isinstance == -1) {
6715 tmp = NULL; 6757 tmp = NULL;
6716 } else { 6758 } else {
6717 asname = NULL; 6759 asname = NULL;
6718 } 6760 }
6719 *out = alias(name, asname, arena); 6761 *out = alias(name, asname, arena);
6762 return 0;
6763 failed:
6764 Py_XDECREF(tmp);
6765 return 1;
6766 }
6767
6768 int
6769 obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6770 {
6771 PyObject* tmp = NULL;
6772 expr_ty context_expr;
6773 expr_ty optional_vars;
6774
6775 if (PyObject_HasAttrString(obj, "context_expr")) {
6776 int res;
6777 tmp = PyObject_GetAttrString(obj, "context_expr");
6778 if (tmp == NULL) goto failed;
6779 res = obj2ast_expr(tmp, &context_expr, arena);
6780 if (res != 0) goto failed;
6781 Py_XDECREF(tmp);
6782 tmp = NULL;
6783 } else {
6784 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6785 return 1;
6786 }
6787 if (PyObject_HasAttrString(obj, "optional_vars")) {
6788 int res;
6789 tmp = PyObject_GetAttrString(obj, "optional_vars");
6790 if (tmp == NULL) goto failed;
6791 res = obj2ast_expr(tmp, &optional_vars, arena);
6792 if (res != 0) goto failed;
6793 Py_XDECREF(tmp);
6794 tmp = NULL;
6795 } else {
6796 optional_vars = NULL;
6797 }
6798 *out = withitem(context_expr, optional_vars, arena);
6720 return 0; 6799 return 0;
6721 failed: 6800 failed:
6722 Py_XDECREF(tmp); 6801 Py_XDECREF(tmp);
6723 return 1; 6802 return 1;
6724 } 6803 }
6938 NULL; 7017 NULL;
6939 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) 7018 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6940 return NULL; 7019 return NULL;
6941 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return 7020 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6942 NULL; 7021 NULL;
7022 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7023 return NULL;
6943 return m; 7024 return m;
6944 } 7025 }
6945 7026
6946 7027
6947 PyObject* PyAST_mod2obj(mod_ty t) 7028 PyObject* PyAST_mod2obj(mod_ty t)