comparison Python/Python-ast.c @ 70499:e0e663132363

unify TryExcept and TryFinally (closes #12199)
author Benjamin Peterson <benjamin@python.org>
date Sun, 29 May 2011 11:43:10 -0500
parents 69d4938382c1
children b8c2db47a562
comparison
equal deleted inserted replaced
70498:6cbd27cb761b 70499:e0e663132363
101 static PyTypeObject *Raise_type; 101 static PyTypeObject *Raise_type;
102 static char *Raise_fields[]={ 102 static char *Raise_fields[]={
103 "exc", 103 "exc",
104 "cause", 104 "cause",
105 }; 105 };
106 static PyTypeObject *TryExcept_type; 106 static PyTypeObject *Try_type;
107 static char *TryExcept_fields[]={ 107 static char *Try_fields[]={
108 "body", 108 "body",
109 "handlers", 109 "handlers",
110 "orelse", 110 "orelse",
111 };
112 static PyTypeObject *TryFinally_type;
113 static char *TryFinally_fields[]={
114 "body",
115 "finalbody", 111 "finalbody",
116 }; 112 };
117 static PyTypeObject *Assert_type; 113 static PyTypeObject *Assert_type;
118 static char *Assert_fields[]={ 114 static char *Assert_fields[]={
119 "test", 115 "test",
687 if (!If_type) return 0; 683 if (!If_type) return 0;
688 With_type = make_type("With", stmt_type, With_fields, 2); 684 With_type = make_type("With", stmt_type, With_fields, 2);
689 if (!With_type) return 0; 685 if (!With_type) return 0;
690 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2); 686 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
691 if (!Raise_type) return 0; 687 if (!Raise_type) return 0;
692 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3); 688 Try_type = make_type("Try", stmt_type, Try_fields, 4);
693 if (!TryExcept_type) return 0; 689 if (!Try_type) return 0;
694 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
695 2);
696 if (!TryFinally_type) return 0;
697 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2); 690 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
698 if (!Assert_type) return 0; 691 if (!Assert_type) return 0;
699 Import_type = make_type("Import", stmt_type, Import_fields, 1); 692 Import_type = make_type("Import", stmt_type, Import_fields, 1);
700 if (!Import_type) return 0; 693 if (!Import_type) return 0;
701 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 694 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
1262 p->col_offset = col_offset; 1255 p->col_offset = col_offset;
1263 return p; 1256 return p;
1264 } 1257 }
1265 1258
1266 stmt_ty 1259 stmt_ty
1267 TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno, 1260 Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1268 int col_offset, PyArena *arena) 1261 finalbody, int lineno, int col_offset, PyArena *arena)
1269 { 1262 {
1270 stmt_ty p; 1263 stmt_ty p;
1271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); 1264 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1272 if (!p) 1265 if (!p)
1273 return NULL; 1266 return NULL;
1274 p->kind = TryExcept_kind; 1267 p->kind = Try_kind;
1275 p->v.TryExcept.body = body; 1268 p->v.Try.body = body;
1276 p->v.TryExcept.handlers = handlers; 1269 p->v.Try.handlers = handlers;
1277 p->v.TryExcept.orelse = orelse; 1270 p->v.Try.orelse = orelse;
1278 p->lineno = lineno; 1271 p->v.Try.finalbody = finalbody;
1279 p->col_offset = col_offset;
1280 return p;
1281 }
1282
1283 stmt_ty
1284 TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1285 PyArena *arena)
1286 {
1287 stmt_ty p;
1288 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1289 if (!p)
1290 return NULL;
1291 p->kind = TryFinally_kind;
1292 p->v.TryFinally.body = body;
1293 p->v.TryFinally.finalbody = finalbody;
1294 p->lineno = lineno; 1272 p->lineno = lineno;
1295 p->col_offset = col_offset; 1273 p->col_offset = col_offset;
1296 return p; 1274 return p;
1297 } 1275 }
1298 1276
2432 if (!value) goto failed; 2410 if (!value) goto failed;
2433 if (PyObject_SetAttrString(result, "cause", value) == -1) 2411 if (PyObject_SetAttrString(result, "cause", value) == -1)
2434 goto failed; 2412 goto failed;
2435 Py_DECREF(value); 2413 Py_DECREF(value);
2436 break; 2414 break;
2437 case TryExcept_kind: 2415 case Try_kind:
2438 result = PyType_GenericNew(TryExcept_type, NULL, NULL); 2416 result = PyType_GenericNew(Try_type, NULL, NULL);
2439 if (!result) goto failed; 2417 if (!result) goto failed;
2440 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt); 2418 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2441 if (!value) goto failed; 2419 if (!value) goto failed;
2442 if (PyObject_SetAttrString(result, "body", value) == -1) 2420 if (PyObject_SetAttrString(result, "body", value) == -1)
2443 goto failed; 2421 goto failed;
2444 Py_DECREF(value); 2422 Py_DECREF(value);
2445 value = ast2obj_list(o->v.TryExcept.handlers, 2423 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2446 ast2obj_excepthandler);
2447 if (!value) goto failed; 2424 if (!value) goto failed;
2448 if (PyObject_SetAttrString(result, "handlers", value) == -1) 2425 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2449 goto failed; 2426 goto failed;
2450 Py_DECREF(value); 2427 Py_DECREF(value);
2451 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt); 2428 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2452 if (!value) goto failed; 2429 if (!value) goto failed;
2453 if (PyObject_SetAttrString(result, "orelse", value) == -1) 2430 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2454 goto failed; 2431 goto failed;
2455 Py_DECREF(value); 2432 Py_DECREF(value);
2456 break; 2433 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2457 case TryFinally_kind:
2458 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2461 if (!value) goto failed;
2462 if (PyObject_SetAttrString(result, "body", value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2466 if (!value) goto failed; 2434 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "finalbody", value) == -1) 2435 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2468 goto failed; 2436 goto failed;
2469 Py_DECREF(value); 2437 Py_DECREF(value);
2470 break; 2438 break;
4341 } 4309 }
4342 *out = Raise(exc, cause, lineno, col_offset, arena); 4310 *out = Raise(exc, cause, lineno, col_offset, arena);
4343 if (*out == NULL) goto failed; 4311 if (*out == NULL) goto failed;
4344 return 0; 4312 return 0;
4345 } 4313 }
4346 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type); 4314 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4347 if (isinstance == -1) { 4315 if (isinstance == -1) {
4348 return 1; 4316 return 1;
4349 } 4317 }
4350 if (isinstance) { 4318 if (isinstance) {
4351 asdl_seq* body; 4319 asdl_seq* body;
4352 asdl_seq* handlers; 4320 asdl_seq* handlers;
4353 asdl_seq* orelse; 4321 asdl_seq* orelse;
4322 asdl_seq* finalbody;
4354 4323
4355 if (PyObject_HasAttrString(obj, "body")) { 4324 if (PyObject_HasAttrString(obj, "body")) {
4356 int res; 4325 int res;
4357 Py_ssize_t len; 4326 Py_ssize_t len;
4358 Py_ssize_t i; 4327 Py_ssize_t i;
4359 tmp = PyObject_GetAttrString(obj, "body"); 4328 tmp = PyObject_GetAttrString(obj, "body");
4360 if (tmp == NULL) goto failed; 4329 if (tmp == NULL) goto failed;
4361 if (!PyList_Check(tmp)) { 4330 if (!PyList_Check(tmp)) {
4362 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name); 4331 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4363 goto failed; 4332 goto failed;
4364 } 4333 }
4365 len = PyList_GET_SIZE(tmp); 4334 len = PyList_GET_SIZE(tmp);
4366 body = asdl_seq_new(len, arena); 4335 body = asdl_seq_new(len, arena);
4367 if (body == NULL) goto failed; 4336 if (body == NULL) goto failed;
4372 asdl_seq_SET(body, i, value); 4341 asdl_seq_SET(body, i, value);
4373 } 4342 }
4374 Py_XDECREF(tmp); 4343 Py_XDECREF(tmp);
4375 tmp = NULL; 4344 tmp = NULL;
4376 } else { 4345 } else {
4377 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept"); 4346 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4378 return 1; 4347 return 1;
4379 } 4348 }
4380 if (PyObject_HasAttrString(obj, "handlers")) { 4349 if (PyObject_HasAttrString(obj, "handlers")) {
4381 int res; 4350 int res;
4382 Py_ssize_t len; 4351 Py_ssize_t len;
4383 Py_ssize_t i; 4352 Py_ssize_t i;
4384 tmp = PyObject_GetAttrString(obj, "handlers"); 4353 tmp = PyObject_GetAttrString(obj, "handlers");
4385 if (tmp == NULL) goto failed; 4354 if (tmp == NULL) goto failed;
4386 if (!PyList_Check(tmp)) { 4355 if (!PyList_Check(tmp)) {
4387 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name); 4356 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4388 goto failed; 4357 goto failed;
4389 } 4358 }
4390 len = PyList_GET_SIZE(tmp); 4359 len = PyList_GET_SIZE(tmp);
4391 handlers = asdl_seq_new(len, arena); 4360 handlers = asdl_seq_new(len, arena);
4392 if (handlers == NULL) goto failed; 4361 if (handlers == NULL) goto failed;
4397 asdl_seq_SET(handlers, i, value); 4366 asdl_seq_SET(handlers, i, value);
4398 } 4367 }
4399 Py_XDECREF(tmp); 4368 Py_XDECREF(tmp);
4400 tmp = NULL; 4369 tmp = NULL;
4401 } else { 4370 } else {
4402 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept"); 4371 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4403 return 1; 4372 return 1;
4404 } 4373 }
4405 if (PyObject_HasAttrString(obj, "orelse")) { 4374 if (PyObject_HasAttrString(obj, "orelse")) {
4406 int res; 4375 int res;
4407 Py_ssize_t len; 4376 Py_ssize_t len;
4408 Py_ssize_t i; 4377 Py_ssize_t i;
4409 tmp = PyObject_GetAttrString(obj, "orelse"); 4378 tmp = PyObject_GetAttrString(obj, "orelse");
4410 if (tmp == NULL) goto failed; 4379 if (tmp == NULL) goto failed;
4411 if (!PyList_Check(tmp)) { 4380 if (!PyList_Check(tmp)) {
4412 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name); 4381 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4413 goto failed; 4382 goto failed;
4414 } 4383 }
4415 len = PyList_GET_SIZE(tmp); 4384 len = PyList_GET_SIZE(tmp);
4416 orelse = asdl_seq_new(len, arena); 4385 orelse = asdl_seq_new(len, arena);
4417 if (orelse == NULL) goto failed; 4386 if (orelse == NULL) goto failed;
4422 asdl_seq_SET(orelse, i, value); 4391 asdl_seq_SET(orelse, i, value);
4423 } 4392 }
4424 Py_XDECREF(tmp); 4393 Py_XDECREF(tmp);
4425 tmp = NULL; 4394 tmp = NULL;
4426 } else { 4395 } else {
4427 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept"); 4396 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4428 return 1;
4429 }
4430 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4431 arena);
4432 if (*out == NULL) goto failed;
4433 return 0;
4434 }
4435 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4436 if (isinstance == -1) {
4437 return 1;
4438 }
4439 if (isinstance) {
4440 asdl_seq* body;
4441 asdl_seq* finalbody;
4442
4443 if (PyObject_HasAttrString(obj, "body")) {
4444 int res;
4445 Py_ssize_t len;
4446 Py_ssize_t i;
4447 tmp = PyObject_GetAttrString(obj, "body");
4448 if (tmp == NULL) goto failed;
4449 if (!PyList_Check(tmp)) {
4450 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451 goto failed;
4452 }
4453 len = PyList_GET_SIZE(tmp);
4454 body = asdl_seq_new(len, arena);
4455 if (body == NULL) goto failed;
4456 for (i = 0; i < len; i++) {
4457 stmt_ty value;
4458 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4459 if (res != 0) goto failed;
4460 asdl_seq_SET(body, i, value);
4461 }
4462 Py_XDECREF(tmp);
4463 tmp = NULL;
4464 } else {
4465 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4466 return 1; 4397 return 1;
4467 } 4398 }
4468 if (PyObject_HasAttrString(obj, "finalbody")) { 4399 if (PyObject_HasAttrString(obj, "finalbody")) {
4469 int res; 4400 int res;
4470 Py_ssize_t len; 4401 Py_ssize_t len;
4471 Py_ssize_t i; 4402 Py_ssize_t i;
4472 tmp = PyObject_GetAttrString(obj, "finalbody"); 4403 tmp = PyObject_GetAttrString(obj, "finalbody");
4473 if (tmp == NULL) goto failed; 4404 if (tmp == NULL) goto failed;
4474 if (!PyList_Check(tmp)) { 4405 if (!PyList_Check(tmp)) {
4475 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name); 4406 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4476 goto failed; 4407 goto failed;
4477 } 4408 }
4478 len = PyList_GET_SIZE(tmp); 4409 len = PyList_GET_SIZE(tmp);
4479 finalbody = asdl_seq_new(len, arena); 4410 finalbody = asdl_seq_new(len, arena);
4480 if (finalbody == NULL) goto failed; 4411 if (finalbody == NULL) goto failed;
4485 asdl_seq_SET(finalbody, i, value); 4416 asdl_seq_SET(finalbody, i, value);
4486 } 4417 }
4487 Py_XDECREF(tmp); 4418 Py_XDECREF(tmp);
4488 tmp = NULL; 4419 tmp = NULL;
4489 } else { 4420 } else {
4490 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally"); 4421 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4491 return 1; 4422 return 1;
4492 } 4423 }
4493 *out = TryFinally(body, finalbody, lineno, col_offset, arena); 4424 *out = Try(body, handlers, orelse, finalbody, lineno,
4425 col_offset, arena);
4494 if (*out == NULL) goto failed; 4426 if (*out == NULL) goto failed;
4495 return 0; 4427 return 0;
4496 } 4428 }
4497 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type); 4429 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4498 if (isinstance == -1) { 4430 if (isinstance == -1) {
6851 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL; 6783 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6852 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return 6784 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6853 NULL; 6785 NULL;
6854 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return 6786 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6855 NULL; 6787 NULL;
6856 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) < 6788 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
6857 0) return NULL; 6789 NULL;
6858 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6859 0) return NULL;
6860 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) 6790 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6861 return NULL; 6791 return NULL;
6862 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) 6792 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6863 return NULL; 6793 return NULL;
6864 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 6794 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <