comparison Python/Python-ast.c @ 46208:ea75b12fe8e7

Merged revisions 62004 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62004 | georg.brandl | 2008-03-28 13:11:56 +0100 (Fr, 28 Mär 2008) | 4 lines Patch #1810 by Thomas Lee, reviewed by myself: allow compiling Python AST objects into code objects in compile(). ........
author Martin v. Löwis <martin@v.loewis.de>
date Sun, 30 Mar 2008 20:03:44 +0000
parents 48488fa50729
children a7fb4d87cbde
comparison
equal deleted inserted replaced
46199:ed88f4de496f 46208:ea75b12fe8e7
432 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; 432 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
433 Py_DECREF(l); 433 Py_DECREF(l);
434 return result; 434 return result;
435 } 435 }
436 436
437 /* Conversion AST -> Python */
438
437 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) 439 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
438 { 440 {
439 int i, n = asdl_seq_LEN(seq); 441 int i, n = asdl_seq_LEN(seq);
440 PyObject *result = PyList_New(n); 442 PyObject *result = PyList_New(n);
441 PyObject *value; 443 PyObject *value;
464 466
465 static PyObject* ast2obj_int(long b) 467 static PyObject* ast2obj_int(long b)
466 { 468 {
467 return PyLong_FromLong(b); 469 return PyLong_FromLong(b);
468 } 470 }
471
472 /* Conversion Python -> AST */
473
474 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
475 {
476 if (obj == Py_None)
477 obj = NULL;
478 if (obj)
479 PyArena_AddPyObject(arena, obj);
480 Py_XINCREF(obj);
481 *out = obj;
482 return 0;
483 }
484
485 #define obj2ast_identifier obj2ast_object
486 #define obj2ast_string obj2ast_object
487
488 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
489 {
490 int i;
491 if (!PyLong_Check(obj)) {
492 PyObject *s = PyObject_Repr(obj);
493 if (s == NULL) return 1;
494 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
495 PyString_AS_STRING(s));
496 Py_DECREF(s);
497 return 1;
498 }
499
500 i = (int)PyLong_AsLong(obj);
501 if (i == -1 && PyErr_Occurred())
502 return 1;
503 *out = i;
504 return 0;
505 }
506
469 507
470 static int init_types(void) 508 static int init_types(void)
471 { 509 {
472 static int initialized; 510 static int initialized;
473 if (initialized) return 1; 511 if (initialized) return 1;
763 if (!alias_type) return 0; 801 if (!alias_type) return 0;
764 initialized = 1; 802 initialized = 1;
765 return 1; 803 return 1;
766 } 804 }
767 805
806 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
807 static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
808 static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
809 static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
810 arena);
811 static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
812 static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
813 static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
814 static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
815 static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
816 static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
817 arena);
818 static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
819 arena);
820 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
821 static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
822 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
823 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
824
768 mod_ty 825 mod_ty
769 Module(asdl_seq * body, PyArena *arena) 826 Module(asdl_seq * body, PyArena *arena)
770 { 827 {
771 mod_ty p; 828 mod_ty p;
772 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); 829 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
2766 Py_INCREF(AugStore_singleton); 2823 Py_INCREF(AugStore_singleton);
2767 return AugStore_singleton; 2824 return AugStore_singleton;
2768 case Param: 2825 case Param:
2769 Py_INCREF(Param_singleton); 2826 Py_INCREF(Param_singleton);
2770 return Param_singleton; 2827 return Param_singleton;
2771 } 2828 default:
2772 return NULL; /* cannot happen */ 2829 /* should never happen, but just in case ... */
2830 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2831 return NULL;
2832 }
2773 } 2833 }
2774 PyObject* 2834 PyObject*
2775 ast2obj_slice(void* _o) 2835 ast2obj_slice(void* _o)
2776 { 2836 {
2777 slice_ty o = (slice_ty)_o; 2837 slice_ty o = (slice_ty)_o;
2834 Py_INCREF(And_singleton); 2894 Py_INCREF(And_singleton);
2835 return And_singleton; 2895 return And_singleton;
2836 case Or: 2896 case Or:
2837 Py_INCREF(Or_singleton); 2897 Py_INCREF(Or_singleton);
2838 return Or_singleton; 2898 return Or_singleton;
2839 } 2899 default:
2840 return NULL; /* cannot happen */ 2900 /* should never happen, but just in case ... */
2901 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2902 return NULL;
2903 }
2841 } 2904 }
2842 PyObject* ast2obj_operator(operator_ty o) 2905 PyObject* ast2obj_operator(operator_ty o)
2843 { 2906 {
2844 switch(o) { 2907 switch(o) {
2845 case Add: 2908 case Add:
2876 Py_INCREF(BitAnd_singleton); 2939 Py_INCREF(BitAnd_singleton);
2877 return BitAnd_singleton; 2940 return BitAnd_singleton;
2878 case FloorDiv: 2941 case FloorDiv:
2879 Py_INCREF(FloorDiv_singleton); 2942 Py_INCREF(FloorDiv_singleton);
2880 return FloorDiv_singleton; 2943 return FloorDiv_singleton;
2881 } 2944 default:
2882 return NULL; /* cannot happen */ 2945 /* should never happen, but just in case ... */
2946 PyErr_Format(PyExc_SystemError, "unknown operator found");
2947 return NULL;
2948 }
2883 } 2949 }
2884 PyObject* ast2obj_unaryop(unaryop_ty o) 2950 PyObject* ast2obj_unaryop(unaryop_ty o)
2885 { 2951 {
2886 switch(o) { 2952 switch(o) {
2887 case Invert: 2953 case Invert:
2894 Py_INCREF(UAdd_singleton); 2960 Py_INCREF(UAdd_singleton);
2895 return UAdd_singleton; 2961 return UAdd_singleton;
2896 case USub: 2962 case USub:
2897 Py_INCREF(USub_singleton); 2963 Py_INCREF(USub_singleton);
2898 return USub_singleton; 2964 return USub_singleton;
2899 } 2965 default:
2900 return NULL; /* cannot happen */ 2966 /* should never happen, but just in case ... */
2967 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2968 return NULL;
2969 }
2901 } 2970 }
2902 PyObject* ast2obj_cmpop(cmpop_ty o) 2971 PyObject* ast2obj_cmpop(cmpop_ty o)
2903 { 2972 {
2904 switch(o) { 2973 switch(o) {
2905 case Eq: 2974 case Eq:
2930 Py_INCREF(In_singleton); 2999 Py_INCREF(In_singleton);
2931 return In_singleton; 3000 return In_singleton;
2932 case NotIn: 3001 case NotIn:
2933 Py_INCREF(NotIn_singleton); 3002 Py_INCREF(NotIn_singleton);
2934 return NotIn_singleton; 3003 return NotIn_singleton;
2935 } 3004 default:
2936 return NULL; /* cannot happen */ 3005 /* should never happen, but just in case ... */
3006 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3007 return NULL;
3008 }
2937 } 3009 }
2938 PyObject* 3010 PyObject*
2939 ast2obj_comprehension(void* _o) 3011 ast2obj_comprehension(void* _o)
2940 { 3012 {
2941 comprehension_ty o = (comprehension_ty)_o; 3013 comprehension_ty o = (comprehension_ty)_o;
3155 return result; 3227 return result;
3156 failed: 3228 failed:
3157 Py_XDECREF(value); 3229 Py_XDECREF(value);
3158 Py_XDECREF(result); 3230 Py_XDECREF(result);
3159 return NULL; 3231 return NULL;
3232 }
3233
3234
3235 int
3236 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3237 {
3238 PyObject* tmp = NULL;
3239
3240
3241 if (obj == Py_None) {
3242 *out = NULL;
3243 return 0;
3244 }
3245 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3246 asdl_seq* body;
3247
3248 if (PyObject_HasAttrString(obj, "body")) {
3249 int res;
3250 Py_ssize_t len;
3251 Py_ssize_t i;
3252 tmp = PyObject_GetAttrString(obj, "body");
3253 if (tmp == NULL) goto failed;
3254 if (!PyList_Check(tmp)) {
3255 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3256 goto failed;
3257 }
3258 len = PyList_GET_SIZE(tmp);
3259 body = asdl_seq_new(len, arena);
3260 if (body == NULL) goto failed;
3261 for (i = 0; i < len; i++) {
3262 stmt_ty value;
3263 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3264 if (res != 0) goto failed;
3265 asdl_seq_SET(body, i, value);
3266 }
3267 Py_XDECREF(tmp);
3268 tmp = NULL;
3269 } else {
3270 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3271 return 1;
3272 }
3273 *out = Module(body, arena);
3274 if (*out == NULL) goto failed;
3275 return 0;
3276 }
3277 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3278 asdl_seq* body;
3279
3280 if (PyObject_HasAttrString(obj, "body")) {
3281 int res;
3282 Py_ssize_t len;
3283 Py_ssize_t i;
3284 tmp = PyObject_GetAttrString(obj, "body");
3285 if (tmp == NULL) goto failed;
3286 if (!PyList_Check(tmp)) {
3287 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3288 goto failed;
3289 }
3290 len = PyList_GET_SIZE(tmp);
3291 body = asdl_seq_new(len, arena);
3292 if (body == NULL) goto failed;
3293 for (i = 0; i < len; i++) {
3294 stmt_ty value;
3295 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3296 if (res != 0) goto failed;
3297 asdl_seq_SET(body, i, value);
3298 }
3299 Py_XDECREF(tmp);
3300 tmp = NULL;
3301 } else {
3302 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3303 return 1;
3304 }
3305 *out = Interactive(body, arena);
3306 if (*out == NULL) goto failed;
3307 return 0;
3308 }
3309 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3310 expr_ty body;
3311
3312 if (PyObject_HasAttrString(obj, "body")) {
3313 int res;
3314 tmp = PyObject_GetAttrString(obj, "body");
3315 if (tmp == NULL) goto failed;
3316 res = obj2ast_expr(tmp, &body, arena);
3317 if (res != 0) goto failed;
3318 Py_XDECREF(tmp);
3319 tmp = NULL;
3320 } else {
3321 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3322 return 1;
3323 }
3324 *out = Expression(body, arena);
3325 if (*out == NULL) goto failed;
3326 return 0;
3327 }
3328 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3329 asdl_seq* body;
3330
3331 if (PyObject_HasAttrString(obj, "body")) {
3332 int res;
3333 Py_ssize_t len;
3334 Py_ssize_t i;
3335 tmp = PyObject_GetAttrString(obj, "body");
3336 if (tmp == NULL) goto failed;
3337 if (!PyList_Check(tmp)) {
3338 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3339 goto failed;
3340 }
3341 len = PyList_GET_SIZE(tmp);
3342 body = asdl_seq_new(len, arena);
3343 if (body == NULL) goto failed;
3344 for (i = 0; i < len; i++) {
3345 stmt_ty value;
3346 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3347 if (res != 0) goto failed;
3348 asdl_seq_SET(body, i, value);
3349 }
3350 Py_XDECREF(tmp);
3351 tmp = NULL;
3352 } else {
3353 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3354 return 1;
3355 }
3356 *out = Suite(body, arena);
3357 if (*out == NULL) goto failed;
3358 return 0;
3359 }
3360
3361 tmp = PyObject_Repr(obj);
3362 if (tmp == NULL) goto failed;
3363 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3364 failed:
3365 Py_XDECREF(tmp);
3366 return 1;
3367 }
3368
3369 int
3370 obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3371 {
3372 PyObject* tmp = NULL;
3373
3374 int lineno;
3375 int col_offset;
3376
3377 if (obj == Py_None) {
3378 *out = NULL;
3379 return 0;
3380 }
3381 if (PyObject_HasAttrString(obj, "lineno")) {
3382 int res;
3383 tmp = PyObject_GetAttrString(obj, "lineno");
3384 if (tmp == NULL) goto failed;
3385 res = obj2ast_int(tmp, &lineno, arena);
3386 if (res != 0) goto failed;
3387 Py_XDECREF(tmp);
3388 tmp = NULL;
3389 } else {
3390 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3391 return 1;
3392 }
3393 if (PyObject_HasAttrString(obj, "col_offset")) {
3394 int res;
3395 tmp = PyObject_GetAttrString(obj, "col_offset");
3396 if (tmp == NULL) goto failed;
3397 res = obj2ast_int(tmp, &col_offset, arena);
3398 if (res != 0) goto failed;
3399 Py_XDECREF(tmp);
3400 tmp = NULL;
3401 } else {
3402 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3403 return 1;
3404 }
3405 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3406 identifier name;
3407 arguments_ty args;
3408 asdl_seq* body;
3409 asdl_seq* decorator_list;
3410 expr_ty returns;
3411
3412 if (PyObject_HasAttrString(obj, "name")) {
3413 int res;
3414 tmp = PyObject_GetAttrString(obj, "name");
3415 if (tmp == NULL) goto failed;
3416 res = obj2ast_identifier(tmp, &name, arena);
3417 if (res != 0) goto failed;
3418 Py_XDECREF(tmp);
3419 tmp = NULL;
3420 } else {
3421 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3422 return 1;
3423 }
3424 if (PyObject_HasAttrString(obj, "args")) {
3425 int res;
3426 tmp = PyObject_GetAttrString(obj, "args");
3427 if (tmp == NULL) goto failed;
3428 res = obj2ast_arguments(tmp, &args, arena);
3429 if (res != 0) goto failed;
3430 Py_XDECREF(tmp);
3431 tmp = NULL;
3432 } else {
3433 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3434 return 1;
3435 }
3436 if (PyObject_HasAttrString(obj, "body")) {
3437 int res;
3438 Py_ssize_t len;
3439 Py_ssize_t i;
3440 tmp = PyObject_GetAttrString(obj, "body");
3441 if (tmp == NULL) goto failed;
3442 if (!PyList_Check(tmp)) {
3443 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3444 goto failed;
3445 }
3446 len = PyList_GET_SIZE(tmp);
3447 body = asdl_seq_new(len, arena);
3448 if (body == NULL) goto failed;
3449 for (i = 0; i < len; i++) {
3450 stmt_ty value;
3451 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3452 if (res != 0) goto failed;
3453 asdl_seq_SET(body, i, value);
3454 }
3455 Py_XDECREF(tmp);
3456 tmp = NULL;
3457 } else {
3458 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3459 return 1;
3460 }
3461 if (PyObject_HasAttrString(obj, "decorator_list")) {
3462 int res;
3463 Py_ssize_t len;
3464 Py_ssize_t i;
3465 tmp = PyObject_GetAttrString(obj, "decorator_list");
3466 if (tmp == NULL) goto failed;
3467 if (!PyList_Check(tmp)) {
3468 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3469 goto failed;
3470 }
3471 len = PyList_GET_SIZE(tmp);
3472 decorator_list = asdl_seq_new(len, arena);
3473 if (decorator_list == NULL) goto failed;
3474 for (i = 0; i < len; i++) {
3475 expr_ty value;
3476 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3477 if (res != 0) goto failed;
3478 asdl_seq_SET(decorator_list, i, value);
3479 }
3480 Py_XDECREF(tmp);
3481 tmp = NULL;
3482 } else {
3483 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3484 return 1;
3485 }
3486 if (PyObject_HasAttrString(obj, "returns")) {
3487 int res;
3488 tmp = PyObject_GetAttrString(obj, "returns");
3489 if (tmp == NULL) goto failed;
3490 res = obj2ast_expr(tmp, &returns, arena);
3491 if (res != 0) goto failed;
3492 Py_XDECREF(tmp);
3493 tmp = NULL;
3494 } else {
3495 returns = NULL;
3496 }
3497 *out = FunctionDef(name, args, body, decorator_list, returns,
3498 lineno, col_offset, arena);
3499 if (*out == NULL) goto failed;
3500 return 0;
3501 }
3502 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3503 identifier name;
3504 asdl_seq* bases;
3505 asdl_seq* keywords;
3506 expr_ty starargs;
3507 expr_ty kwargs;
3508 asdl_seq* body;
3509 asdl_seq* decorator_list;
3510
3511 if (PyObject_HasAttrString(obj, "name")) {
3512 int res;
3513 tmp = PyObject_GetAttrString(obj, "name");
3514 if (tmp == NULL) goto failed;
3515 res = obj2ast_identifier(tmp, &name, arena);
3516 if (res != 0) goto failed;
3517 Py_XDECREF(tmp);
3518 tmp = NULL;
3519 } else {
3520 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3521 return 1;
3522 }
3523 if (PyObject_HasAttrString(obj, "bases")) {
3524 int res;
3525 Py_ssize_t len;
3526 Py_ssize_t i;
3527 tmp = PyObject_GetAttrString(obj, "bases");
3528 if (tmp == NULL) goto failed;
3529 if (!PyList_Check(tmp)) {
3530 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3531 goto failed;
3532 }
3533 len = PyList_GET_SIZE(tmp);
3534 bases = asdl_seq_new(len, arena);
3535 if (bases == NULL) goto failed;
3536 for (i = 0; i < len; i++) {
3537 expr_ty value;
3538 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3539 if (res != 0) goto failed;
3540 asdl_seq_SET(bases, i, value);
3541 }
3542 Py_XDECREF(tmp);
3543 tmp = NULL;
3544 } else {
3545 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3546 return 1;
3547 }
3548 if (PyObject_HasAttrString(obj, "keywords")) {
3549 int res;
3550 Py_ssize_t len;
3551 Py_ssize_t i;
3552 tmp = PyObject_GetAttrString(obj, "keywords");
3553 if (tmp == NULL) goto failed;
3554 if (!PyList_Check(tmp)) {
3555 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3556 goto failed;
3557 }
3558 len = PyList_GET_SIZE(tmp);
3559 keywords = asdl_seq_new(len, arena);
3560 if (keywords == NULL) goto failed;
3561 for (i = 0; i < len; i++) {
3562 keyword_ty value;
3563 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3564 if (res != 0) goto failed;
3565 asdl_seq_SET(keywords, i, value);
3566 }
3567 Py_XDECREF(tmp);
3568 tmp = NULL;
3569 } else {
3570 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3571 return 1;
3572 }
3573 if (PyObject_HasAttrString(obj, "starargs")) {
3574 int res;
3575 tmp = PyObject_GetAttrString(obj, "starargs");
3576 if (tmp == NULL) goto failed;
3577 res = obj2ast_expr(tmp, &starargs, arena);
3578 if (res != 0) goto failed;
3579 Py_XDECREF(tmp);
3580 tmp = NULL;
3581 } else {
3582 starargs = NULL;
3583 }
3584 if (PyObject_HasAttrString(obj, "kwargs")) {
3585 int res;
3586 tmp = PyObject_GetAttrString(obj, "kwargs");
3587 if (tmp == NULL) goto failed;
3588 res = obj2ast_expr(tmp, &kwargs, arena);
3589 if (res != 0) goto failed;
3590 Py_XDECREF(tmp);
3591 tmp = NULL;
3592 } else {
3593 kwargs = NULL;
3594 }
3595 if (PyObject_HasAttrString(obj, "body")) {
3596 int res;
3597 Py_ssize_t len;
3598 Py_ssize_t i;
3599 tmp = PyObject_GetAttrString(obj, "body");
3600 if (tmp == NULL) goto failed;
3601 if (!PyList_Check(tmp)) {
3602 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3603 goto failed;
3604 }
3605 len = PyList_GET_SIZE(tmp);
3606 body = asdl_seq_new(len, arena);
3607 if (body == NULL) goto failed;
3608 for (i = 0; i < len; i++) {
3609 stmt_ty value;
3610 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3611 if (res != 0) goto failed;
3612 asdl_seq_SET(body, i, value);
3613 }
3614 Py_XDECREF(tmp);
3615 tmp = NULL;
3616 } else {
3617 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3618 return 1;
3619 }
3620 if (PyObject_HasAttrString(obj, "decorator_list")) {
3621 int res;
3622 Py_ssize_t len;
3623 Py_ssize_t i;
3624 tmp = PyObject_GetAttrString(obj, "decorator_list");
3625 if (tmp == NULL) goto failed;
3626 if (!PyList_Check(tmp)) {
3627 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3628 goto failed;
3629 }
3630 len = PyList_GET_SIZE(tmp);
3631 decorator_list = asdl_seq_new(len, arena);
3632 if (decorator_list == NULL) goto failed;
3633 for (i = 0; i < len; i++) {
3634 expr_ty value;
3635 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3636 if (res != 0) goto failed;
3637 asdl_seq_SET(decorator_list, i, value);
3638 }
3639 Py_XDECREF(tmp);
3640 tmp = NULL;
3641 } else {
3642 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3643 return 1;
3644 }
3645 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3646 decorator_list, lineno, col_offset, arena);
3647 if (*out == NULL) goto failed;
3648 return 0;
3649 }
3650 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3651 expr_ty value;
3652
3653 if (PyObject_HasAttrString(obj, "value")) {
3654 int res;
3655 tmp = PyObject_GetAttrString(obj, "value");
3656 if (tmp == NULL) goto failed;
3657 res = obj2ast_expr(tmp, &value, arena);
3658 if (res != 0) goto failed;
3659 Py_XDECREF(tmp);
3660 tmp = NULL;
3661 } else {
3662 value = NULL;
3663 }
3664 *out = Return(value, lineno, col_offset, arena);
3665 if (*out == NULL) goto failed;
3666 return 0;
3667 }
3668 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3669 asdl_seq* targets;
3670
3671 if (PyObject_HasAttrString(obj, "targets")) {
3672 int res;
3673 Py_ssize_t len;
3674 Py_ssize_t i;
3675 tmp = PyObject_GetAttrString(obj, "targets");
3676 if (tmp == NULL) goto failed;
3677 if (!PyList_Check(tmp)) {
3678 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3679 goto failed;
3680 }
3681 len = PyList_GET_SIZE(tmp);
3682 targets = asdl_seq_new(len, arena);
3683 if (targets == NULL) goto failed;
3684 for (i = 0; i < len; i++) {
3685 expr_ty value;
3686 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3687 if (res != 0) goto failed;
3688 asdl_seq_SET(targets, i, value);
3689 }
3690 Py_XDECREF(tmp);
3691 tmp = NULL;
3692 } else {
3693 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3694 return 1;
3695 }
3696 *out = Delete(targets, lineno, col_offset, arena);
3697 if (*out == NULL) goto failed;
3698 return 0;
3699 }
3700 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3701 asdl_seq* targets;
3702 expr_ty value;
3703
3704 if (PyObject_HasAttrString(obj, "targets")) {
3705 int res;
3706 Py_ssize_t len;
3707 Py_ssize_t i;
3708 tmp = PyObject_GetAttrString(obj, "targets");
3709 if (tmp == NULL) goto failed;
3710 if (!PyList_Check(tmp)) {
3711 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3712 goto failed;
3713 }
3714 len = PyList_GET_SIZE(tmp);
3715 targets = asdl_seq_new(len, arena);
3716 if (targets == NULL) goto failed;
3717 for (i = 0; i < len; i++) {
3718 expr_ty value;
3719 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3720 if (res != 0) goto failed;
3721 asdl_seq_SET(targets, i, value);
3722 }
3723 Py_XDECREF(tmp);
3724 tmp = NULL;
3725 } else {
3726 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3727 return 1;
3728 }
3729 if (PyObject_HasAttrString(obj, "value")) {
3730 int res;
3731 tmp = PyObject_GetAttrString(obj, "value");
3732 if (tmp == NULL) goto failed;
3733 res = obj2ast_expr(tmp, &value, arena);
3734 if (res != 0) goto failed;
3735 Py_XDECREF(tmp);
3736 tmp = NULL;
3737 } else {
3738 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3739 return 1;
3740 }
3741 *out = Assign(targets, value, lineno, col_offset, arena);
3742 if (*out == NULL) goto failed;
3743 return 0;
3744 }
3745 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3746 expr_ty target;
3747 operator_ty op;
3748 expr_ty value;
3749
3750 if (PyObject_HasAttrString(obj, "target")) {
3751 int res;
3752 tmp = PyObject_GetAttrString(obj, "target");
3753 if (tmp == NULL) goto failed;
3754 res = obj2ast_expr(tmp, &target, arena);
3755 if (res != 0) goto failed;
3756 Py_XDECREF(tmp);
3757 tmp = NULL;
3758 } else {
3759 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3760 return 1;
3761 }
3762 if (PyObject_HasAttrString(obj, "op")) {
3763 int res;
3764 tmp = PyObject_GetAttrString(obj, "op");
3765 if (tmp == NULL) goto failed;
3766 res = obj2ast_operator(tmp, &op, arena);
3767 if (res != 0) goto failed;
3768 Py_XDECREF(tmp);
3769 tmp = NULL;
3770 } else {
3771 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3772 return 1;
3773 }
3774 if (PyObject_HasAttrString(obj, "value")) {
3775 int res;
3776 tmp = PyObject_GetAttrString(obj, "value");
3777 if (tmp == NULL) goto failed;
3778 res = obj2ast_expr(tmp, &value, arena);
3779 if (res != 0) goto failed;
3780 Py_XDECREF(tmp);
3781 tmp = NULL;
3782 } else {
3783 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3784 return 1;
3785 }
3786 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3787 if (*out == NULL) goto failed;
3788 return 0;
3789 }
3790 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3791 expr_ty target;
3792 expr_ty iter;
3793 asdl_seq* body;
3794 asdl_seq* orelse;
3795
3796 if (PyObject_HasAttrString(obj, "target")) {
3797 int res;
3798 tmp = PyObject_GetAttrString(obj, "target");
3799 if (tmp == NULL) goto failed;
3800 res = obj2ast_expr(tmp, &target, arena);
3801 if (res != 0) goto failed;
3802 Py_XDECREF(tmp);
3803 tmp = NULL;
3804 } else {
3805 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3806 return 1;
3807 }
3808 if (PyObject_HasAttrString(obj, "iter")) {
3809 int res;
3810 tmp = PyObject_GetAttrString(obj, "iter");
3811 if (tmp == NULL) goto failed;
3812 res = obj2ast_expr(tmp, &iter, arena);
3813 if (res != 0) goto failed;
3814 Py_XDECREF(tmp);
3815 tmp = NULL;
3816 } else {
3817 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3818 return 1;
3819 }
3820 if (PyObject_HasAttrString(obj, "body")) {
3821 int res;
3822 Py_ssize_t len;
3823 Py_ssize_t i;
3824 tmp = PyObject_GetAttrString(obj, "body");
3825 if (tmp == NULL) goto failed;
3826 if (!PyList_Check(tmp)) {
3827 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3828 goto failed;
3829 }
3830 len = PyList_GET_SIZE(tmp);
3831 body = asdl_seq_new(len, arena);
3832 if (body == NULL) goto failed;
3833 for (i = 0; i < len; i++) {
3834 stmt_ty value;
3835 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3836 if (res != 0) goto failed;
3837 asdl_seq_SET(body, i, value);
3838 }
3839 Py_XDECREF(tmp);
3840 tmp = NULL;
3841 } else {
3842 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3843 return 1;
3844 }
3845 if (PyObject_HasAttrString(obj, "orelse")) {
3846 int res;
3847 Py_ssize_t len;
3848 Py_ssize_t i;
3849 tmp = PyObject_GetAttrString(obj, "orelse");
3850 if (tmp == NULL) goto failed;
3851 if (!PyList_Check(tmp)) {
3852 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3853 goto failed;
3854 }
3855 len = PyList_GET_SIZE(tmp);
3856 orelse = asdl_seq_new(len, arena);
3857 if (orelse == NULL) goto failed;
3858 for (i = 0; i < len; i++) {
3859 stmt_ty value;
3860 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3861 if (res != 0) goto failed;
3862 asdl_seq_SET(orelse, i, value);
3863 }
3864 Py_XDECREF(tmp);
3865 tmp = NULL;
3866 } else {
3867 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3868 return 1;
3869 }
3870 *out = For(target, iter, body, orelse, lineno, col_offset,
3871 arena);
3872 if (*out == NULL) goto failed;
3873 return 0;
3874 }
3875 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3876 expr_ty test;
3877 asdl_seq* body;
3878 asdl_seq* orelse;
3879
3880 if (PyObject_HasAttrString(obj, "test")) {
3881 int res;
3882 tmp = PyObject_GetAttrString(obj, "test");
3883 if (tmp == NULL) goto failed;
3884 res = obj2ast_expr(tmp, &test, arena);
3885 if (res != 0) goto failed;
3886 Py_XDECREF(tmp);
3887 tmp = NULL;
3888 } else {
3889 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3890 return 1;
3891 }
3892 if (PyObject_HasAttrString(obj, "body")) {
3893 int res;
3894 Py_ssize_t len;
3895 Py_ssize_t i;
3896 tmp = PyObject_GetAttrString(obj, "body");
3897 if (tmp == NULL) goto failed;
3898 if (!PyList_Check(tmp)) {
3899 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3900 goto failed;
3901 }
3902 len = PyList_GET_SIZE(tmp);
3903 body = asdl_seq_new(len, arena);
3904 if (body == NULL) goto failed;
3905 for (i = 0; i < len; i++) {
3906 stmt_ty value;
3907 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3908 if (res != 0) goto failed;
3909 asdl_seq_SET(body, i, value);
3910 }
3911 Py_XDECREF(tmp);
3912 tmp = NULL;
3913 } else {
3914 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3915 return 1;
3916 }
3917 if (PyObject_HasAttrString(obj, "orelse")) {
3918 int res;
3919 Py_ssize_t len;
3920 Py_ssize_t i;
3921 tmp = PyObject_GetAttrString(obj, "orelse");
3922 if (tmp == NULL) goto failed;
3923 if (!PyList_Check(tmp)) {
3924 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3925 goto failed;
3926 }
3927 len = PyList_GET_SIZE(tmp);
3928 orelse = asdl_seq_new(len, arena);
3929 if (orelse == NULL) goto failed;
3930 for (i = 0; i < len; i++) {
3931 stmt_ty value;
3932 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3933 if (res != 0) goto failed;
3934 asdl_seq_SET(orelse, i, value);
3935 }
3936 Py_XDECREF(tmp);
3937 tmp = NULL;
3938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3940 return 1;
3941 }
3942 *out = While(test, body, orelse, lineno, col_offset, arena);
3943 if (*out == NULL) goto failed;
3944 return 0;
3945 }
3946 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3947 expr_ty test;
3948 asdl_seq* body;
3949 asdl_seq* orelse;
3950
3951 if (PyObject_HasAttrString(obj, "test")) {
3952 int res;
3953 tmp = PyObject_GetAttrString(obj, "test");
3954 if (tmp == NULL) goto failed;
3955 res = obj2ast_expr(tmp, &test, arena);
3956 if (res != 0) goto failed;
3957 Py_XDECREF(tmp);
3958 tmp = NULL;
3959 } else {
3960 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3961 return 1;
3962 }
3963 if (PyObject_HasAttrString(obj, "body")) {
3964 int res;
3965 Py_ssize_t len;
3966 Py_ssize_t i;
3967 tmp = PyObject_GetAttrString(obj, "body");
3968 if (tmp == NULL) goto failed;
3969 if (!PyList_Check(tmp)) {
3970 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3971 goto failed;
3972 }
3973 len = PyList_GET_SIZE(tmp);
3974 body = asdl_seq_new(len, arena);
3975 if (body == NULL) goto failed;
3976 for (i = 0; i < len; i++) {
3977 stmt_ty value;
3978 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3979 if (res != 0) goto failed;
3980 asdl_seq_SET(body, i, value);
3981 }
3982 Py_XDECREF(tmp);
3983 tmp = NULL;
3984 } else {
3985 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3986 return 1;
3987 }
3988 if (PyObject_HasAttrString(obj, "orelse")) {
3989 int res;
3990 Py_ssize_t len;
3991 Py_ssize_t i;
3992 tmp = PyObject_GetAttrString(obj, "orelse");
3993 if (tmp == NULL) goto failed;
3994 if (!PyList_Check(tmp)) {
3995 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3996 goto failed;
3997 }
3998 len = PyList_GET_SIZE(tmp);
3999 orelse = asdl_seq_new(len, arena);
4000 if (orelse == NULL) goto failed;
4001 for (i = 0; i < len; i++) {
4002 stmt_ty value;
4003 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4004 if (res != 0) goto failed;
4005 asdl_seq_SET(orelse, i, value);
4006 }
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4011 return 1;
4012 }
4013 *out = If(test, body, orelse, lineno, col_offset, arena);
4014 if (*out == NULL) goto failed;
4015 return 0;
4016 }
4017 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
4018 expr_ty context_expr;
4019 expr_ty optional_vars;
4020 asdl_seq* body;
4021
4022 if (PyObject_HasAttrString(obj, "context_expr")) {
4023 int res;
4024 tmp = PyObject_GetAttrString(obj, "context_expr");
4025 if (tmp == NULL) goto failed;
4026 res = obj2ast_expr(tmp, &context_expr, arena);
4027 if (res != 0) goto failed;
4028 Py_XDECREF(tmp);
4029 tmp = NULL;
4030 } else {
4031 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4032 return 1;
4033 }
4034 if (PyObject_HasAttrString(obj, "optional_vars")) {
4035 int res;
4036 tmp = PyObject_GetAttrString(obj, "optional_vars");
4037 if (tmp == NULL) goto failed;
4038 res = obj2ast_expr(tmp, &optional_vars, arena);
4039 if (res != 0) goto failed;
4040 Py_XDECREF(tmp);
4041 tmp = NULL;
4042 } else {
4043 optional_vars = NULL;
4044 }
4045 if (PyObject_HasAttrString(obj, "body")) {
4046 int res;
4047 Py_ssize_t len;
4048 Py_ssize_t i;
4049 tmp = PyObject_GetAttrString(obj, "body");
4050 if (tmp == NULL) goto failed;
4051 if (!PyList_Check(tmp)) {
4052 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4053 goto failed;
4054 }
4055 len = PyList_GET_SIZE(tmp);
4056 body = asdl_seq_new(len, arena);
4057 if (body == NULL) goto failed;
4058 for (i = 0; i < len; i++) {
4059 stmt_ty value;
4060 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4061 if (res != 0) goto failed;
4062 asdl_seq_SET(body, i, value);
4063 }
4064 Py_XDECREF(tmp);
4065 tmp = NULL;
4066 } else {
4067 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4068 return 1;
4069 }
4070 *out = With(context_expr, optional_vars, body, lineno,
4071 col_offset, arena);
4072 if (*out == NULL) goto failed;
4073 return 0;
4074 }
4075 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4076 expr_ty exc;
4077 expr_ty cause;
4078
4079 if (PyObject_HasAttrString(obj, "exc")) {
4080 int res;
4081 tmp = PyObject_GetAttrString(obj, "exc");
4082 if (tmp == NULL) goto failed;
4083 res = obj2ast_expr(tmp, &exc, arena);
4084 if (res != 0) goto failed;
4085 Py_XDECREF(tmp);
4086 tmp = NULL;
4087 } else {
4088 exc = NULL;
4089 }
4090 if (PyObject_HasAttrString(obj, "cause")) {
4091 int res;
4092 tmp = PyObject_GetAttrString(obj, "cause");
4093 if (tmp == NULL) goto failed;
4094 res = obj2ast_expr(tmp, &cause, arena);
4095 if (res != 0) goto failed;
4096 Py_XDECREF(tmp);
4097 tmp = NULL;
4098 } else {
4099 cause = NULL;
4100 }
4101 *out = Raise(exc, cause, lineno, col_offset, arena);
4102 if (*out == NULL) goto failed;
4103 return 0;
4104 }
4105 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4106 asdl_seq* body;
4107 asdl_seq* handlers;
4108 asdl_seq* orelse;
4109
4110 if (PyObject_HasAttrString(obj, "body")) {
4111 int res;
4112 Py_ssize_t len;
4113 Py_ssize_t i;
4114 tmp = PyObject_GetAttrString(obj, "body");
4115 if (tmp == NULL) goto failed;
4116 if (!PyList_Check(tmp)) {
4117 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4118 goto failed;
4119 }
4120 len = PyList_GET_SIZE(tmp);
4121 body = asdl_seq_new(len, arena);
4122 if (body == NULL) goto failed;
4123 for (i = 0; i < len; i++) {
4124 stmt_ty value;
4125 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4126 if (res != 0) goto failed;
4127 asdl_seq_SET(body, i, value);
4128 }
4129 Py_XDECREF(tmp);
4130 tmp = NULL;
4131 } else {
4132 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4133 return 1;
4134 }
4135 if (PyObject_HasAttrString(obj, "handlers")) {
4136 int res;
4137 Py_ssize_t len;
4138 Py_ssize_t i;
4139 tmp = PyObject_GetAttrString(obj, "handlers");
4140 if (tmp == NULL) goto failed;
4141 if (!PyList_Check(tmp)) {
4142 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4143 goto failed;
4144 }
4145 len = PyList_GET_SIZE(tmp);
4146 handlers = asdl_seq_new(len, arena);
4147 if (handlers == NULL) goto failed;
4148 for (i = 0; i < len; i++) {
4149 excepthandler_ty value;
4150 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4151 if (res != 0) goto failed;
4152 asdl_seq_SET(handlers, i, value);
4153 }
4154 Py_XDECREF(tmp);
4155 tmp = NULL;
4156 } else {
4157 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4158 return 1;
4159 }
4160 if (PyObject_HasAttrString(obj, "orelse")) {
4161 int res;
4162 Py_ssize_t len;
4163 Py_ssize_t i;
4164 tmp = PyObject_GetAttrString(obj, "orelse");
4165 if (tmp == NULL) goto failed;
4166 if (!PyList_Check(tmp)) {
4167 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4168 goto failed;
4169 }
4170 len = PyList_GET_SIZE(tmp);
4171 orelse = asdl_seq_new(len, arena);
4172 if (orelse == NULL) goto failed;
4173 for (i = 0; i < len; i++) {
4174 stmt_ty value;
4175 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4176 if (res != 0) goto failed;
4177 asdl_seq_SET(orelse, i, value);
4178 }
4179 Py_XDECREF(tmp);
4180 tmp = NULL;
4181 } else {
4182 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4183 return 1;
4184 }
4185 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4186 arena);
4187 if (*out == NULL) goto failed;
4188 return 0;
4189 }
4190 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4191 asdl_seq* body;
4192 asdl_seq* finalbody;
4193
4194 if (PyObject_HasAttrString(obj, "body")) {
4195 int res;
4196 Py_ssize_t len;
4197 Py_ssize_t i;
4198 tmp = PyObject_GetAttrString(obj, "body");
4199 if (tmp == NULL) goto failed;
4200 if (!PyList_Check(tmp)) {
4201 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4202 goto failed;
4203 }
4204 len = PyList_GET_SIZE(tmp);
4205 body = asdl_seq_new(len, arena);
4206 if (body == NULL) goto failed;
4207 for (i = 0; i < len; i++) {
4208 stmt_ty value;
4209 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4210 if (res != 0) goto failed;
4211 asdl_seq_SET(body, i, value);
4212 }
4213 Py_XDECREF(tmp);
4214 tmp = NULL;
4215 } else {
4216 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4217 return 1;
4218 }
4219 if (PyObject_HasAttrString(obj, "finalbody")) {
4220 int res;
4221 Py_ssize_t len;
4222 Py_ssize_t i;
4223 tmp = PyObject_GetAttrString(obj, "finalbody");
4224 if (tmp == NULL) goto failed;
4225 if (!PyList_Check(tmp)) {
4226 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4227 goto failed;
4228 }
4229 len = PyList_GET_SIZE(tmp);
4230 finalbody = asdl_seq_new(len, arena);
4231 if (finalbody == NULL) goto failed;
4232 for (i = 0; i < len; i++) {
4233 stmt_ty value;
4234 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4235 if (res != 0) goto failed;
4236 asdl_seq_SET(finalbody, i, value);
4237 }
4238 Py_XDECREF(tmp);
4239 tmp = NULL;
4240 } else {
4241 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4242 return 1;
4243 }
4244 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4245 if (*out == NULL) goto failed;
4246 return 0;
4247 }
4248 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4249 expr_ty test;
4250 expr_ty msg;
4251
4252 if (PyObject_HasAttrString(obj, "test")) {
4253 int res;
4254 tmp = PyObject_GetAttrString(obj, "test");
4255 if (tmp == NULL) goto failed;
4256 res = obj2ast_expr(tmp, &test, arena);
4257 if (res != 0) goto failed;
4258 Py_XDECREF(tmp);
4259 tmp = NULL;
4260 } else {
4261 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4262 return 1;
4263 }
4264 if (PyObject_HasAttrString(obj, "msg")) {
4265 int res;
4266 tmp = PyObject_GetAttrString(obj, "msg");
4267 if (tmp == NULL) goto failed;
4268 res = obj2ast_expr(tmp, &msg, arena);
4269 if (res != 0) goto failed;
4270 Py_XDECREF(tmp);
4271 tmp = NULL;
4272 } else {
4273 msg = NULL;
4274 }
4275 *out = Assert(test, msg, lineno, col_offset, arena);
4276 if (*out == NULL) goto failed;
4277 return 0;
4278 }
4279 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4280 asdl_seq* names;
4281
4282 if (PyObject_HasAttrString(obj, "names")) {
4283 int res;
4284 Py_ssize_t len;
4285 Py_ssize_t i;
4286 tmp = PyObject_GetAttrString(obj, "names");
4287 if (tmp == NULL) goto failed;
4288 if (!PyList_Check(tmp)) {
4289 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4290 goto failed;
4291 }
4292 len = PyList_GET_SIZE(tmp);
4293 names = asdl_seq_new(len, arena);
4294 if (names == NULL) goto failed;
4295 for (i = 0; i < len; i++) {
4296 alias_ty value;
4297 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4298 if (res != 0) goto failed;
4299 asdl_seq_SET(names, i, value);
4300 }
4301 Py_XDECREF(tmp);
4302 tmp = NULL;
4303 } else {
4304 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4305 return 1;
4306 }
4307 *out = Import(names, lineno, col_offset, arena);
4308 if (*out == NULL) goto failed;
4309 return 0;
4310 }
4311 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4312 identifier module;
4313 asdl_seq* names;
4314 int level;
4315
4316 if (PyObject_HasAttrString(obj, "module")) {
4317 int res;
4318 tmp = PyObject_GetAttrString(obj, "module");
4319 if (tmp == NULL) goto failed;
4320 res = obj2ast_identifier(tmp, &module, arena);
4321 if (res != 0) goto failed;
4322 Py_XDECREF(tmp);
4323 tmp = NULL;
4324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4326 return 1;
4327 }
4328 if (PyObject_HasAttrString(obj, "names")) {
4329 int res;
4330 Py_ssize_t len;
4331 Py_ssize_t i;
4332 tmp = PyObject_GetAttrString(obj, "names");
4333 if (tmp == NULL) goto failed;
4334 if (!PyList_Check(tmp)) {
4335 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4336 goto failed;
4337 }
4338 len = PyList_GET_SIZE(tmp);
4339 names = asdl_seq_new(len, arena);
4340 if (names == NULL) goto failed;
4341 for (i = 0; i < len; i++) {
4342 alias_ty value;
4343 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4344 if (res != 0) goto failed;
4345 asdl_seq_SET(names, i, value);
4346 }
4347 Py_XDECREF(tmp);
4348 tmp = NULL;
4349 } else {
4350 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4351 return 1;
4352 }
4353 if (PyObject_HasAttrString(obj, "level")) {
4354 int res;
4355 tmp = PyObject_GetAttrString(obj, "level");
4356 if (tmp == NULL) goto failed;
4357 res = obj2ast_int(tmp, &level, arena);
4358 if (res != 0) goto failed;
4359 Py_XDECREF(tmp);
4360 tmp = NULL;
4361 } else {
4362 level = 0;
4363 }
4364 *out = ImportFrom(module, names, level, lineno, col_offset,
4365 arena);
4366 if (*out == NULL) goto failed;
4367 return 0;
4368 }
4369 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4370 asdl_seq* names;
4371
4372 if (PyObject_HasAttrString(obj, "names")) {
4373 int res;
4374 Py_ssize_t len;
4375 Py_ssize_t i;
4376 tmp = PyObject_GetAttrString(obj, "names");
4377 if (tmp == NULL) goto failed;
4378 if (!PyList_Check(tmp)) {
4379 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4380 goto failed;
4381 }
4382 len = PyList_GET_SIZE(tmp);
4383 names = asdl_seq_new(len, arena);
4384 if (names == NULL) goto failed;
4385 for (i = 0; i < len; i++) {
4386 identifier value;
4387 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4388 if (res != 0) goto failed;
4389 asdl_seq_SET(names, i, value);
4390 }
4391 Py_XDECREF(tmp);
4392 tmp = NULL;
4393 } else {
4394 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4395 return 1;
4396 }
4397 *out = Global(names, lineno, col_offset, arena);
4398 if (*out == NULL) goto failed;
4399 return 0;
4400 }
4401 if (PyObject_IsInstance(obj, (PyObject*)Nonlocal_type)) {
4402 asdl_seq* names;
4403
4404 if (PyObject_HasAttrString(obj, "names")) {
4405 int res;
4406 Py_ssize_t len;
4407 Py_ssize_t i;
4408 tmp = PyObject_GetAttrString(obj, "names");
4409 if (tmp == NULL) goto failed;
4410 if (!PyList_Check(tmp)) {
4411 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4412 goto failed;
4413 }
4414 len = PyList_GET_SIZE(tmp);
4415 names = asdl_seq_new(len, arena);
4416 if (names == NULL) goto failed;
4417 for (i = 0; i < len; i++) {
4418 identifier value;
4419 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4420 if (res != 0) goto failed;
4421 asdl_seq_SET(names, i, value);
4422 }
4423 Py_XDECREF(tmp);
4424 tmp = NULL;
4425 } else {
4426 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4427 return 1;
4428 }
4429 *out = Nonlocal(names, lineno, col_offset, arena);
4430 if (*out == NULL) goto failed;
4431 return 0;
4432 }
4433 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4434 expr_ty value;
4435
4436 if (PyObject_HasAttrString(obj, "value")) {
4437 int res;
4438 tmp = PyObject_GetAttrString(obj, "value");
4439 if (tmp == NULL) goto failed;
4440 res = obj2ast_expr(tmp, &value, arena);
4441 if (res != 0) goto failed;
4442 Py_XDECREF(tmp);
4443 tmp = NULL;
4444 } else {
4445 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4446 return 1;
4447 }
4448 *out = Expr(value, lineno, col_offset, arena);
4449 if (*out == NULL) goto failed;
4450 return 0;
4451 }
4452 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4453
4454 *out = Pass(lineno, col_offset, arena);
4455 if (*out == NULL) goto failed;
4456 return 0;
4457 }
4458 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4459
4460 *out = Break(lineno, col_offset, arena);
4461 if (*out == NULL) goto failed;
4462 return 0;
4463 }
4464 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4465
4466 *out = Continue(lineno, col_offset, arena);
4467 if (*out == NULL) goto failed;
4468 return 0;
4469 }
4470
4471 tmp = PyObject_Repr(obj);
4472 if (tmp == NULL) goto failed;
4473 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4474 failed:
4475 Py_XDECREF(tmp);
4476 return 1;
4477 }
4478
4479 int
4480 obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4481 {
4482 PyObject* tmp = NULL;
4483
4484 int lineno;
4485 int col_offset;
4486
4487 if (obj == Py_None) {
4488 *out = NULL;
4489 return 0;
4490 }
4491 if (PyObject_HasAttrString(obj, "lineno")) {
4492 int res;
4493 tmp = PyObject_GetAttrString(obj, "lineno");
4494 if (tmp == NULL) goto failed;
4495 res = obj2ast_int(tmp, &lineno, arena);
4496 if (res != 0) goto failed;
4497 Py_XDECREF(tmp);
4498 tmp = NULL;
4499 } else {
4500 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4501 return 1;
4502 }
4503 if (PyObject_HasAttrString(obj, "col_offset")) {
4504 int res;
4505 tmp = PyObject_GetAttrString(obj, "col_offset");
4506 if (tmp == NULL) goto failed;
4507 res = obj2ast_int(tmp, &col_offset, arena);
4508 if (res != 0) goto failed;
4509 Py_XDECREF(tmp);
4510 tmp = NULL;
4511 } else {
4512 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4513 return 1;
4514 }
4515 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4516 boolop_ty op;
4517 asdl_seq* values;
4518
4519 if (PyObject_HasAttrString(obj, "op")) {
4520 int res;
4521 tmp = PyObject_GetAttrString(obj, "op");
4522 if (tmp == NULL) goto failed;
4523 res = obj2ast_boolop(tmp, &op, arena);
4524 if (res != 0) goto failed;
4525 Py_XDECREF(tmp);
4526 tmp = NULL;
4527 } else {
4528 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4529 return 1;
4530 }
4531 if (PyObject_HasAttrString(obj, "values")) {
4532 int res;
4533 Py_ssize_t len;
4534 Py_ssize_t i;
4535 tmp = PyObject_GetAttrString(obj, "values");
4536 if (tmp == NULL) goto failed;
4537 if (!PyList_Check(tmp)) {
4538 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4539 goto failed;
4540 }
4541 len = PyList_GET_SIZE(tmp);
4542 values = asdl_seq_new(len, arena);
4543 if (values == NULL) goto failed;
4544 for (i = 0; i < len; i++) {
4545 expr_ty value;
4546 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4547 if (res != 0) goto failed;
4548 asdl_seq_SET(values, i, value);
4549 }
4550 Py_XDECREF(tmp);
4551 tmp = NULL;
4552 } else {
4553 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4554 return 1;
4555 }
4556 *out = BoolOp(op, values, lineno, col_offset, arena);
4557 if (*out == NULL) goto failed;
4558 return 0;
4559 }
4560 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4561 expr_ty left;
4562 operator_ty op;
4563 expr_ty right;
4564
4565 if (PyObject_HasAttrString(obj, "left")) {
4566 int res;
4567 tmp = PyObject_GetAttrString(obj, "left");
4568 if (tmp == NULL) goto failed;
4569 res = obj2ast_expr(tmp, &left, arena);
4570 if (res != 0) goto failed;
4571 Py_XDECREF(tmp);
4572 tmp = NULL;
4573 } else {
4574 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4575 return 1;
4576 }
4577 if (PyObject_HasAttrString(obj, "op")) {
4578 int res;
4579 tmp = PyObject_GetAttrString(obj, "op");
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_operator(tmp, &op, arena);
4582 if (res != 0) goto failed;
4583 Py_XDECREF(tmp);
4584 tmp = NULL;
4585 } else {
4586 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4587 return 1;
4588 }
4589 if (PyObject_HasAttrString(obj, "right")) {
4590 int res;
4591 tmp = PyObject_GetAttrString(obj, "right");
4592 if (tmp == NULL) goto failed;
4593 res = obj2ast_expr(tmp, &right, arena);
4594 if (res != 0) goto failed;
4595 Py_XDECREF(tmp);
4596 tmp = NULL;
4597 } else {
4598 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4599 return 1;
4600 }
4601 *out = BinOp(left, op, right, lineno, col_offset, arena);
4602 if (*out == NULL) goto failed;
4603 return 0;
4604 }
4605 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4606 unaryop_ty op;
4607 expr_ty operand;
4608
4609 if (PyObject_HasAttrString(obj, "op")) {
4610 int res;
4611 tmp = PyObject_GetAttrString(obj, "op");
4612 if (tmp == NULL) goto failed;
4613 res = obj2ast_unaryop(tmp, &op, arena);
4614 if (res != 0) goto failed;
4615 Py_XDECREF(tmp);
4616 tmp = NULL;
4617 } else {
4618 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4619 return 1;
4620 }
4621 if (PyObject_HasAttrString(obj, "operand")) {
4622 int res;
4623 tmp = PyObject_GetAttrString(obj, "operand");
4624 if (tmp == NULL) goto failed;
4625 res = obj2ast_expr(tmp, &operand, arena);
4626 if (res != 0) goto failed;
4627 Py_XDECREF(tmp);
4628 tmp = NULL;
4629 } else {
4630 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4631 return 1;
4632 }
4633 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4634 if (*out == NULL) goto failed;
4635 return 0;
4636 }
4637 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4638 arguments_ty args;
4639 expr_ty body;
4640
4641 if (PyObject_HasAttrString(obj, "args")) {
4642 int res;
4643 tmp = PyObject_GetAttrString(obj, "args");
4644 if (tmp == NULL) goto failed;
4645 res = obj2ast_arguments(tmp, &args, arena);
4646 if (res != 0) goto failed;
4647 Py_XDECREF(tmp);
4648 tmp = NULL;
4649 } else {
4650 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4651 return 1;
4652 }
4653 if (PyObject_HasAttrString(obj, "body")) {
4654 int res;
4655 tmp = PyObject_GetAttrString(obj, "body");
4656 if (tmp == NULL) goto failed;
4657 res = obj2ast_expr(tmp, &body, arena);
4658 if (res != 0) goto failed;
4659 Py_XDECREF(tmp);
4660 tmp = NULL;
4661 } else {
4662 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4663 return 1;
4664 }
4665 *out = Lambda(args, body, lineno, col_offset, arena);
4666 if (*out == NULL) goto failed;
4667 return 0;
4668 }
4669 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4670 expr_ty test;
4671 expr_ty body;
4672 expr_ty orelse;
4673
4674 if (PyObject_HasAttrString(obj, "test")) {
4675 int res;
4676 tmp = PyObject_GetAttrString(obj, "test");
4677 if (tmp == NULL) goto failed;
4678 res = obj2ast_expr(tmp, &test, arena);
4679 if (res != 0) goto failed;
4680 Py_XDECREF(tmp);
4681 tmp = NULL;
4682 } else {
4683 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4684 return 1;
4685 }
4686 if (PyObject_HasAttrString(obj, "body")) {
4687 int res;
4688 tmp = PyObject_GetAttrString(obj, "body");
4689 if (tmp == NULL) goto failed;
4690 res = obj2ast_expr(tmp, &body, arena);
4691 if (res != 0) goto failed;
4692 Py_XDECREF(tmp);
4693 tmp = NULL;
4694 } else {
4695 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4696 return 1;
4697 }
4698 if (PyObject_HasAttrString(obj, "orelse")) {
4699 int res;
4700 tmp = PyObject_GetAttrString(obj, "orelse");
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_expr(tmp, &orelse, arena);
4703 if (res != 0) goto failed;
4704 Py_XDECREF(tmp);
4705 tmp = NULL;
4706 } else {
4707 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4708 return 1;
4709 }
4710 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4711 if (*out == NULL) goto failed;
4712 return 0;
4713 }
4714 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4715 asdl_seq* keys;
4716 asdl_seq* values;
4717
4718 if (PyObject_HasAttrString(obj, "keys")) {
4719 int res;
4720 Py_ssize_t len;
4721 Py_ssize_t i;
4722 tmp = PyObject_GetAttrString(obj, "keys");
4723 if (tmp == NULL) goto failed;
4724 if (!PyList_Check(tmp)) {
4725 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4726 goto failed;
4727 }
4728 len = PyList_GET_SIZE(tmp);
4729 keys = asdl_seq_new(len, arena);
4730 if (keys == NULL) goto failed;
4731 for (i = 0; i < len; i++) {
4732 expr_ty value;
4733 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4734 if (res != 0) goto failed;
4735 asdl_seq_SET(keys, i, value);
4736 }
4737 Py_XDECREF(tmp);
4738 tmp = NULL;
4739 } else {
4740 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4741 return 1;
4742 }
4743 if (PyObject_HasAttrString(obj, "values")) {
4744 int res;
4745 Py_ssize_t len;
4746 Py_ssize_t i;
4747 tmp = PyObject_GetAttrString(obj, "values");
4748 if (tmp == NULL) goto failed;
4749 if (!PyList_Check(tmp)) {
4750 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4751 goto failed;
4752 }
4753 len = PyList_GET_SIZE(tmp);
4754 values = asdl_seq_new(len, arena);
4755 if (values == NULL) goto failed;
4756 for (i = 0; i < len; i++) {
4757 expr_ty value;
4758 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4759 if (res != 0) goto failed;
4760 asdl_seq_SET(values, i, value);
4761 }
4762 Py_XDECREF(tmp);
4763 tmp = NULL;
4764 } else {
4765 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4766 return 1;
4767 }
4768 *out = Dict(keys, values, lineno, col_offset, arena);
4769 if (*out == NULL) goto failed;
4770 return 0;
4771 }
4772 if (PyObject_IsInstance(obj, (PyObject*)Set_type)) {
4773 asdl_seq* elts;
4774
4775 if (PyObject_HasAttrString(obj, "elts")) {
4776 int res;
4777 Py_ssize_t len;
4778 Py_ssize_t i;
4779 tmp = PyObject_GetAttrString(obj, "elts");
4780 if (tmp == NULL) goto failed;
4781 if (!PyList_Check(tmp)) {
4782 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4783 goto failed;
4784 }
4785 len = PyList_GET_SIZE(tmp);
4786 elts = asdl_seq_new(len, arena);
4787 if (elts == NULL) goto failed;
4788 for (i = 0; i < len; i++) {
4789 expr_ty value;
4790 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4791 if (res != 0) goto failed;
4792 asdl_seq_SET(elts, i, value);
4793 }
4794 Py_XDECREF(tmp);
4795 tmp = NULL;
4796 } else {
4797 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4798 return 1;
4799 }
4800 *out = Set(elts, lineno, col_offset, arena);
4801 if (*out == NULL) goto failed;
4802 return 0;
4803 }
4804 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4805 expr_ty elt;
4806 asdl_seq* generators;
4807
4808 if (PyObject_HasAttrString(obj, "elt")) {
4809 int res;
4810 tmp = PyObject_GetAttrString(obj, "elt");
4811 if (tmp == NULL) goto failed;
4812 res = obj2ast_expr(tmp, &elt, arena);
4813 if (res != 0) goto failed;
4814 Py_XDECREF(tmp);
4815 tmp = NULL;
4816 } else {
4817 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4818 return 1;
4819 }
4820 if (PyObject_HasAttrString(obj, "generators")) {
4821 int res;
4822 Py_ssize_t len;
4823 Py_ssize_t i;
4824 tmp = PyObject_GetAttrString(obj, "generators");
4825 if (tmp == NULL) goto failed;
4826 if (!PyList_Check(tmp)) {
4827 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4828 goto failed;
4829 }
4830 len = PyList_GET_SIZE(tmp);
4831 generators = asdl_seq_new(len, arena);
4832 if (generators == NULL) goto failed;
4833 for (i = 0; i < len; i++) {
4834 comprehension_ty value;
4835 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4836 if (res != 0) goto failed;
4837 asdl_seq_SET(generators, i, value);
4838 }
4839 Py_XDECREF(tmp);
4840 tmp = NULL;
4841 } else {
4842 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4843 return 1;
4844 }
4845 *out = ListComp(elt, generators, lineno, col_offset, arena);
4846 if (*out == NULL) goto failed;
4847 return 0;
4848 }
4849 if (PyObject_IsInstance(obj, (PyObject*)SetComp_type)) {
4850 expr_ty elt;
4851 asdl_seq* generators;
4852
4853 if (PyObject_HasAttrString(obj, "elt")) {
4854 int res;
4855 tmp = PyObject_GetAttrString(obj, "elt");
4856 if (tmp == NULL) goto failed;
4857 res = obj2ast_expr(tmp, &elt, arena);
4858 if (res != 0) goto failed;
4859 Py_XDECREF(tmp);
4860 tmp = NULL;
4861 } else {
4862 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
4863 return 1;
4864 }
4865 if (PyObject_HasAttrString(obj, "generators")) {
4866 int res;
4867 Py_ssize_t len;
4868 Py_ssize_t i;
4869 tmp = PyObject_GetAttrString(obj, "generators");
4870 if (tmp == NULL) goto failed;
4871 if (!PyList_Check(tmp)) {
4872 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4873 goto failed;
4874 }
4875 len = PyList_GET_SIZE(tmp);
4876 generators = asdl_seq_new(len, arena);
4877 if (generators == NULL) goto failed;
4878 for (i = 0; i < len; i++) {
4879 comprehension_ty value;
4880 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4881 if (res != 0) goto failed;
4882 asdl_seq_SET(generators, i, value);
4883 }
4884 Py_XDECREF(tmp);
4885 tmp = NULL;
4886 } else {
4887 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
4888 return 1;
4889 }
4890 *out = SetComp(elt, generators, lineno, col_offset, arena);
4891 if (*out == NULL) goto failed;
4892 return 0;
4893 }
4894 if (PyObject_IsInstance(obj, (PyObject*)DictComp_type)) {
4895 expr_ty key;
4896 expr_ty value;
4897 asdl_seq* generators;
4898
4899 if (PyObject_HasAttrString(obj, "key")) {
4900 int res;
4901 tmp = PyObject_GetAttrString(obj, "key");
4902 if (tmp == NULL) goto failed;
4903 res = obj2ast_expr(tmp, &key, arena);
4904 if (res != 0) goto failed;
4905 Py_XDECREF(tmp);
4906 tmp = NULL;
4907 } else {
4908 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
4909 return 1;
4910 }
4911 if (PyObject_HasAttrString(obj, "value")) {
4912 int res;
4913 tmp = PyObject_GetAttrString(obj, "value");
4914 if (tmp == NULL) goto failed;
4915 res = obj2ast_expr(tmp, &value, arena);
4916 if (res != 0) goto failed;
4917 Py_XDECREF(tmp);
4918 tmp = NULL;
4919 } else {
4920 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
4921 return 1;
4922 }
4923 if (PyObject_HasAttrString(obj, "generators")) {
4924 int res;
4925 Py_ssize_t len;
4926 Py_ssize_t i;
4927 tmp = PyObject_GetAttrString(obj, "generators");
4928 if (tmp == NULL) goto failed;
4929 if (!PyList_Check(tmp)) {
4930 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4931 goto failed;
4932 }
4933 len = PyList_GET_SIZE(tmp);
4934 generators = asdl_seq_new(len, arena);
4935 if (generators == NULL) goto failed;
4936 for (i = 0; i < len; i++) {
4937 comprehension_ty value;
4938 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4939 if (res != 0) goto failed;
4940 asdl_seq_SET(generators, i, value);
4941 }
4942 Py_XDECREF(tmp);
4943 tmp = NULL;
4944 } else {
4945 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
4946 return 1;
4947 }
4948 *out = DictComp(key, value, generators, lineno, col_offset,
4949 arena);
4950 if (*out == NULL) goto failed;
4951 return 0;
4952 }
4953 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4954 expr_ty elt;
4955 asdl_seq* generators;
4956
4957 if (PyObject_HasAttrString(obj, "elt")) {
4958 int res;
4959 tmp = PyObject_GetAttrString(obj, "elt");
4960 if (tmp == NULL) goto failed;
4961 res = obj2ast_expr(tmp, &elt, arena);
4962 if (res != 0) goto failed;
4963 Py_XDECREF(tmp);
4964 tmp = NULL;
4965 } else {
4966 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4967 return 1;
4968 }
4969 if (PyObject_HasAttrString(obj, "generators")) {
4970 int res;
4971 Py_ssize_t len;
4972 Py_ssize_t i;
4973 tmp = PyObject_GetAttrString(obj, "generators");
4974 if (tmp == NULL) goto failed;
4975 if (!PyList_Check(tmp)) {
4976 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4977 goto failed;
4978 }
4979 len = PyList_GET_SIZE(tmp);
4980 generators = asdl_seq_new(len, arena);
4981 if (generators == NULL) goto failed;
4982 for (i = 0; i < len; i++) {
4983 comprehension_ty value;
4984 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4985 if (res != 0) goto failed;
4986 asdl_seq_SET(generators, i, value);
4987 }
4988 Py_XDECREF(tmp);
4989 tmp = NULL;
4990 } else {
4991 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4992 return 1;
4993 }
4994 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4995 if (*out == NULL) goto failed;
4996 return 0;
4997 }
4998 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4999 expr_ty value;
5000
5001 if (PyObject_HasAttrString(obj, "value")) {
5002 int res;
5003 tmp = PyObject_GetAttrString(obj, "value");
5004 if (tmp == NULL) goto failed;
5005 res = obj2ast_expr(tmp, &value, arena);
5006 if (res != 0) goto failed;
5007 Py_XDECREF(tmp);
5008 tmp = NULL;
5009 } else {
5010 value = NULL;
5011 }
5012 *out = Yield(value, lineno, col_offset, arena);
5013 if (*out == NULL) goto failed;
5014 return 0;
5015 }
5016 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
5017 expr_ty left;
5018 asdl_int_seq* ops;
5019 asdl_seq* comparators;
5020
5021 if (PyObject_HasAttrString(obj, "left")) {
5022 int res;
5023 tmp = PyObject_GetAttrString(obj, "left");
5024 if (tmp == NULL) goto failed;
5025 res = obj2ast_expr(tmp, &left, arena);
5026 if (res != 0) goto failed;
5027 Py_XDECREF(tmp);
5028 tmp = NULL;
5029 } else {
5030 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5031 return 1;
5032 }
5033 if (PyObject_HasAttrString(obj, "ops")) {
5034 int res;
5035 Py_ssize_t len;
5036 Py_ssize_t i;
5037 tmp = PyObject_GetAttrString(obj, "ops");
5038 if (tmp == NULL) goto failed;
5039 if (!PyList_Check(tmp)) {
5040 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5041 goto failed;
5042 }
5043 len = PyList_GET_SIZE(tmp);
5044 ops = asdl_int_seq_new(len, arena);
5045 if (ops == NULL) goto failed;
5046 for (i = 0; i < len; i++) {
5047 cmpop_ty value;
5048 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5049 if (res != 0) goto failed;
5050 asdl_seq_SET(ops, i, value);
5051 }
5052 Py_XDECREF(tmp);
5053 tmp = NULL;
5054 } else {
5055 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5056 return 1;
5057 }
5058 if (PyObject_HasAttrString(obj, "comparators")) {
5059 int res;
5060 Py_ssize_t len;
5061 Py_ssize_t i;
5062 tmp = PyObject_GetAttrString(obj, "comparators");
5063 if (tmp == NULL) goto failed;
5064 if (!PyList_Check(tmp)) {
5065 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5066 goto failed;
5067 }
5068 len = PyList_GET_SIZE(tmp);
5069 comparators = asdl_seq_new(len, arena);
5070 if (comparators == NULL) goto failed;
5071 for (i = 0; i < len; i++) {
5072 expr_ty value;
5073 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5074 if (res != 0) goto failed;
5075 asdl_seq_SET(comparators, i, value);
5076 }
5077 Py_XDECREF(tmp);
5078 tmp = NULL;
5079 } else {
5080 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5081 return 1;
5082 }
5083 *out = Compare(left, ops, comparators, lineno, col_offset,
5084 arena);
5085 if (*out == NULL) goto failed;
5086 return 0;
5087 }
5088 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
5089 expr_ty func;
5090 asdl_seq* args;
5091 asdl_seq* keywords;
5092 expr_ty starargs;
5093 expr_ty kwargs;
5094
5095 if (PyObject_HasAttrString(obj, "func")) {
5096 int res;
5097 tmp = PyObject_GetAttrString(obj, "func");
5098 if (tmp == NULL) goto failed;
5099 res = obj2ast_expr(tmp, &func, arena);
5100 if (res != 0) goto failed;
5101 Py_XDECREF(tmp);
5102 tmp = NULL;
5103 } else {
5104 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5105 return 1;
5106 }
5107 if (PyObject_HasAttrString(obj, "args")) {
5108 int res;
5109 Py_ssize_t len;
5110 Py_ssize_t i;
5111 tmp = PyObject_GetAttrString(obj, "args");
5112 if (tmp == NULL) goto failed;
5113 if (!PyList_Check(tmp)) {
5114 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5115 goto failed;
5116 }
5117 len = PyList_GET_SIZE(tmp);
5118 args = asdl_seq_new(len, arena);
5119 if (args == NULL) goto failed;
5120 for (i = 0; i < len; i++) {
5121 expr_ty value;
5122 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5123 if (res != 0) goto failed;
5124 asdl_seq_SET(args, i, value);
5125 }
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5130 return 1;
5131 }
5132 if (PyObject_HasAttrString(obj, "keywords")) {
5133 int res;
5134 Py_ssize_t len;
5135 Py_ssize_t i;
5136 tmp = PyObject_GetAttrString(obj, "keywords");
5137 if (tmp == NULL) goto failed;
5138 if (!PyList_Check(tmp)) {
5139 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5140 goto failed;
5141 }
5142 len = PyList_GET_SIZE(tmp);
5143 keywords = asdl_seq_new(len, arena);
5144 if (keywords == NULL) goto failed;
5145 for (i = 0; i < len; i++) {
5146 keyword_ty value;
5147 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5148 if (res != 0) goto failed;
5149 asdl_seq_SET(keywords, i, value);
5150 }
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "starargs")) {
5158 int res;
5159 tmp = PyObject_GetAttrString(obj, "starargs");
5160 if (tmp == NULL) goto failed;
5161 res = obj2ast_expr(tmp, &starargs, arena);
5162 if (res != 0) goto failed;
5163 Py_XDECREF(tmp);
5164 tmp = NULL;
5165 } else {
5166 starargs = NULL;
5167 }
5168 if (PyObject_HasAttrString(obj, "kwargs")) {
5169 int res;
5170 tmp = PyObject_GetAttrString(obj, "kwargs");
5171 if (tmp == NULL) goto failed;
5172 res = obj2ast_expr(tmp, &kwargs, arena);
5173 if (res != 0) goto failed;
5174 Py_XDECREF(tmp);
5175 tmp = NULL;
5176 } else {
5177 kwargs = NULL;
5178 }
5179 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5180 col_offset, arena);
5181 if (*out == NULL) goto failed;
5182 return 0;
5183 }
5184 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5185 object n;
5186
5187 if (PyObject_HasAttrString(obj, "n")) {
5188 int res;
5189 tmp = PyObject_GetAttrString(obj, "n");
5190 if (tmp == NULL) goto failed;
5191 res = obj2ast_object(tmp, &n, arena);
5192 if (res != 0) goto failed;
5193 Py_XDECREF(tmp);
5194 tmp = NULL;
5195 } else {
5196 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5197 return 1;
5198 }
5199 *out = Num(n, lineno, col_offset, arena);
5200 if (*out == NULL) goto failed;
5201 return 0;
5202 }
5203 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5204 string s;
5205
5206 if (PyObject_HasAttrString(obj, "s")) {
5207 int res;
5208 tmp = PyObject_GetAttrString(obj, "s");
5209 if (tmp == NULL) goto failed;
5210 res = obj2ast_string(tmp, &s, arena);
5211 if (res != 0) goto failed;
5212 Py_XDECREF(tmp);
5213 tmp = NULL;
5214 } else {
5215 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5216 return 1;
5217 }
5218 *out = Str(s, lineno, col_offset, arena);
5219 if (*out == NULL) goto failed;
5220 return 0;
5221 }
5222 if (PyObject_IsInstance(obj, (PyObject*)Bytes_type)) {
5223 string s;
5224
5225 if (PyObject_HasAttrString(obj, "s")) {
5226 int res;
5227 tmp = PyObject_GetAttrString(obj, "s");
5228 if (tmp == NULL) goto failed;
5229 res = obj2ast_string(tmp, &s, arena);
5230 if (res != 0) goto failed;
5231 Py_XDECREF(tmp);
5232 tmp = NULL;
5233 } else {
5234 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5235 return 1;
5236 }
5237 *out = Bytes(s, lineno, col_offset, arena);
5238 if (*out == NULL) goto failed;
5239 return 0;
5240 }
5241 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5242
5243 *out = Ellipsis(lineno, col_offset, arena);
5244 if (*out == NULL) goto failed;
5245 return 0;
5246 }
5247 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5248 expr_ty value;
5249 identifier attr;
5250 expr_context_ty ctx;
5251
5252 if (PyObject_HasAttrString(obj, "value")) {
5253 int res;
5254 tmp = PyObject_GetAttrString(obj, "value");
5255 if (tmp == NULL) goto failed;
5256 res = obj2ast_expr(tmp, &value, arena);
5257 if (res != 0) goto failed;
5258 Py_XDECREF(tmp);
5259 tmp = NULL;
5260 } else {
5261 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5262 return 1;
5263 }
5264 if (PyObject_HasAttrString(obj, "attr")) {
5265 int res;
5266 tmp = PyObject_GetAttrString(obj, "attr");
5267 if (tmp == NULL) goto failed;
5268 res = obj2ast_identifier(tmp, &attr, arena);
5269 if (res != 0) goto failed;
5270 Py_XDECREF(tmp);
5271 tmp = NULL;
5272 } else {
5273 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5274 return 1;
5275 }
5276 if (PyObject_HasAttrString(obj, "ctx")) {
5277 int res;
5278 tmp = PyObject_GetAttrString(obj, "ctx");
5279 if (tmp == NULL) goto failed;
5280 res = obj2ast_expr_context(tmp, &ctx, arena);
5281 if (res != 0) goto failed;
5282 Py_XDECREF(tmp);
5283 tmp = NULL;
5284 } else {
5285 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5286 return 1;
5287 }
5288 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5289 if (*out == NULL) goto failed;
5290 return 0;
5291 }
5292 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5293 expr_ty value;
5294 slice_ty slice;
5295 expr_context_ty ctx;
5296
5297 if (PyObject_HasAttrString(obj, "value")) {
5298 int res;
5299 tmp = PyObject_GetAttrString(obj, "value");
5300 if (tmp == NULL) goto failed;
5301 res = obj2ast_expr(tmp, &value, arena);
5302 if (res != 0) goto failed;
5303 Py_XDECREF(tmp);
5304 tmp = NULL;
5305 } else {
5306 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5307 return 1;
5308 }
5309 if (PyObject_HasAttrString(obj, "slice")) {
5310 int res;
5311 tmp = PyObject_GetAttrString(obj, "slice");
5312 if (tmp == NULL) goto failed;
5313 res = obj2ast_slice(tmp, &slice, arena);
5314 if (res != 0) goto failed;
5315 Py_XDECREF(tmp);
5316 tmp = NULL;
5317 } else {
5318 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5319 return 1;
5320 }
5321 if (PyObject_HasAttrString(obj, "ctx")) {
5322 int res;
5323 tmp = PyObject_GetAttrString(obj, "ctx");
5324 if (tmp == NULL) goto failed;
5325 res = obj2ast_expr_context(tmp, &ctx, arena);
5326 if (res != 0) goto failed;
5327 Py_XDECREF(tmp);
5328 tmp = NULL;
5329 } else {
5330 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5331 return 1;
5332 }
5333 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5334 if (*out == NULL) goto failed;
5335 return 0;
5336 }
5337 if (PyObject_IsInstance(obj, (PyObject*)Starred_type)) {
5338 expr_ty value;
5339 expr_context_ty ctx;
5340
5341 if (PyObject_HasAttrString(obj, "value")) {
5342 int res;
5343 tmp = PyObject_GetAttrString(obj, "value");
5344 if (tmp == NULL) goto failed;
5345 res = obj2ast_expr(tmp, &value, arena);
5346 if (res != 0) goto failed;
5347 Py_XDECREF(tmp);
5348 tmp = NULL;
5349 } else {
5350 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5351 return 1;
5352 }
5353 if (PyObject_HasAttrString(obj, "ctx")) {
5354 int res;
5355 tmp = PyObject_GetAttrString(obj, "ctx");
5356 if (tmp == NULL) goto failed;
5357 res = obj2ast_expr_context(tmp, &ctx, arena);
5358 if (res != 0) goto failed;
5359 Py_XDECREF(tmp);
5360 tmp = NULL;
5361 } else {
5362 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5363 return 1;
5364 }
5365 *out = Starred(value, ctx, lineno, col_offset, arena);
5366 if (*out == NULL) goto failed;
5367 return 0;
5368 }
5369 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5370 identifier id;
5371 expr_context_ty ctx;
5372
5373 if (PyObject_HasAttrString(obj, "id")) {
5374 int res;
5375 tmp = PyObject_GetAttrString(obj, "id");
5376 if (tmp == NULL) goto failed;
5377 res = obj2ast_identifier(tmp, &id, arena);
5378 if (res != 0) goto failed;
5379 Py_XDECREF(tmp);
5380 tmp = NULL;
5381 } else {
5382 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5383 return 1;
5384 }
5385 if (PyObject_HasAttrString(obj, "ctx")) {
5386 int res;
5387 tmp = PyObject_GetAttrString(obj, "ctx");
5388 if (tmp == NULL) goto failed;
5389 res = obj2ast_expr_context(tmp, &ctx, arena);
5390 if (res != 0) goto failed;
5391 Py_XDECREF(tmp);
5392 tmp = NULL;
5393 } else {
5394 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5395 return 1;
5396 }
5397 *out = Name(id, ctx, lineno, col_offset, arena);
5398 if (*out == NULL) goto failed;
5399 return 0;
5400 }
5401 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5402 asdl_seq* elts;
5403 expr_context_ty ctx;
5404
5405 if (PyObject_HasAttrString(obj, "elts")) {
5406 int res;
5407 Py_ssize_t len;
5408 Py_ssize_t i;
5409 tmp = PyObject_GetAttrString(obj, "elts");
5410 if (tmp == NULL) goto failed;
5411 if (!PyList_Check(tmp)) {
5412 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5413 goto failed;
5414 }
5415 len = PyList_GET_SIZE(tmp);
5416 elts = asdl_seq_new(len, arena);
5417 if (elts == NULL) goto failed;
5418 for (i = 0; i < len; i++) {
5419 expr_ty value;
5420 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5421 if (res != 0) goto failed;
5422 asdl_seq_SET(elts, i, value);
5423 }
5424 Py_XDECREF(tmp);
5425 tmp = NULL;
5426 } else {
5427 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5428 return 1;
5429 }
5430 if (PyObject_HasAttrString(obj, "ctx")) {
5431 int res;
5432 tmp = PyObject_GetAttrString(obj, "ctx");
5433 if (tmp == NULL) goto failed;
5434 res = obj2ast_expr_context(tmp, &ctx, arena);
5435 if (res != 0) goto failed;
5436 Py_XDECREF(tmp);
5437 tmp = NULL;
5438 } else {
5439 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5440 return 1;
5441 }
5442 *out = List(elts, ctx, lineno, col_offset, arena);
5443 if (*out == NULL) goto failed;
5444 return 0;
5445 }
5446 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5447 asdl_seq* elts;
5448 expr_context_ty ctx;
5449
5450 if (PyObject_HasAttrString(obj, "elts")) {
5451 int res;
5452 Py_ssize_t len;
5453 Py_ssize_t i;
5454 tmp = PyObject_GetAttrString(obj, "elts");
5455 if (tmp == NULL) goto failed;
5456 if (!PyList_Check(tmp)) {
5457 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5458 goto failed;
5459 }
5460 len = PyList_GET_SIZE(tmp);
5461 elts = asdl_seq_new(len, arena);
5462 if (elts == NULL) goto failed;
5463 for (i = 0; i < len; i++) {
5464 expr_ty value;
5465 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5466 if (res != 0) goto failed;
5467 asdl_seq_SET(elts, i, value);
5468 }
5469 Py_XDECREF(tmp);
5470 tmp = NULL;
5471 } else {
5472 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5473 return 1;
5474 }
5475 if (PyObject_HasAttrString(obj, "ctx")) {
5476 int res;
5477 tmp = PyObject_GetAttrString(obj, "ctx");
5478 if (tmp == NULL) goto failed;
5479 res = obj2ast_expr_context(tmp, &ctx, arena);
5480 if (res != 0) goto failed;
5481 Py_XDECREF(tmp);
5482 tmp = NULL;
5483 } else {
5484 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5485 return 1;
5486 }
5487 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5488 if (*out == NULL) goto failed;
5489 return 0;
5490 }
5491
5492 tmp = PyObject_Repr(obj);
5493 if (tmp == NULL) goto failed;
5494 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5495 failed:
5496 Py_XDECREF(tmp);
5497 return 1;
5498 }
5499
5500 int
5501 obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5502 {
5503 PyObject* tmp = NULL;
5504
5505 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5506 *out = Load;
5507 return 0;
5508 }
5509 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5510 *out = Store;
5511 return 0;
5512 }
5513 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5514 *out = Del;
5515 return 0;
5516 }
5517 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5518 *out = AugLoad;
5519 return 0;
5520 }
5521 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5522 *out = AugStore;
5523 return 0;
5524 }
5525 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5526 *out = Param;
5527 return 0;
5528 }
5529
5530 tmp = PyObject_Repr(obj);
5531 if (tmp == NULL) goto failed;
5532 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5533 failed:
5534 Py_XDECREF(tmp);
5535 return 1;
5536 }
5537
5538 int
5539 obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5540 {
5541 PyObject* tmp = NULL;
5542
5543
5544 if (obj == Py_None) {
5545 *out = NULL;
5546 return 0;
5547 }
5548 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5549 expr_ty lower;
5550 expr_ty upper;
5551 expr_ty step;
5552
5553 if (PyObject_HasAttrString(obj, "lower")) {
5554 int res;
5555 tmp = PyObject_GetAttrString(obj, "lower");
5556 if (tmp == NULL) goto failed;
5557 res = obj2ast_expr(tmp, &lower, arena);
5558 if (res != 0) goto failed;
5559 Py_XDECREF(tmp);
5560 tmp = NULL;
5561 } else {
5562 lower = NULL;
5563 }
5564 if (PyObject_HasAttrString(obj, "upper")) {
5565 int res;
5566 tmp = PyObject_GetAttrString(obj, "upper");
5567 if (tmp == NULL) goto failed;
5568 res = obj2ast_expr(tmp, &upper, arena);
5569 if (res != 0) goto failed;
5570 Py_XDECREF(tmp);
5571 tmp = NULL;
5572 } else {
5573 upper = NULL;
5574 }
5575 if (PyObject_HasAttrString(obj, "step")) {
5576 int res;
5577 tmp = PyObject_GetAttrString(obj, "step");
5578 if (tmp == NULL) goto failed;
5579 res = obj2ast_expr(tmp, &step, arena);
5580 if (res != 0) goto failed;
5581 Py_XDECREF(tmp);
5582 tmp = NULL;
5583 } else {
5584 step = NULL;
5585 }
5586 *out = Slice(lower, upper, step, arena);
5587 if (*out == NULL) goto failed;
5588 return 0;
5589 }
5590 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5591 asdl_seq* dims;
5592
5593 if (PyObject_HasAttrString(obj, "dims")) {
5594 int res;
5595 Py_ssize_t len;
5596 Py_ssize_t i;
5597 tmp = PyObject_GetAttrString(obj, "dims");
5598 if (tmp == NULL) goto failed;
5599 if (!PyList_Check(tmp)) {
5600 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5601 goto failed;
5602 }
5603 len = PyList_GET_SIZE(tmp);
5604 dims = asdl_seq_new(len, arena);
5605 if (dims == NULL) goto failed;
5606 for (i = 0; i < len; i++) {
5607 slice_ty value;
5608 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5609 if (res != 0) goto failed;
5610 asdl_seq_SET(dims, i, value);
5611 }
5612 Py_XDECREF(tmp);
5613 tmp = NULL;
5614 } else {
5615 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5616 return 1;
5617 }
5618 *out = ExtSlice(dims, arena);
5619 if (*out == NULL) goto failed;
5620 return 0;
5621 }
5622 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5623 expr_ty value;
5624
5625 if (PyObject_HasAttrString(obj, "value")) {
5626 int res;
5627 tmp = PyObject_GetAttrString(obj, "value");
5628 if (tmp == NULL) goto failed;
5629 res = obj2ast_expr(tmp, &value, arena);
5630 if (res != 0) goto failed;
5631 Py_XDECREF(tmp);
5632 tmp = NULL;
5633 } else {
5634 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5635 return 1;
5636 }
5637 *out = Index(value, arena);
5638 if (*out == NULL) goto failed;
5639 return 0;
5640 }
5641
5642 tmp = PyObject_Repr(obj);
5643 if (tmp == NULL) goto failed;
5644 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5645 failed:
5646 Py_XDECREF(tmp);
5647 return 1;
5648 }
5649
5650 int
5651 obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5652 {
5653 PyObject* tmp = NULL;
5654
5655 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5656 *out = And;
5657 return 0;
5658 }
5659 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5660 *out = Or;
5661 return 0;
5662 }
5663
5664 tmp = PyObject_Repr(obj);
5665 if (tmp == NULL) goto failed;
5666 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5667 failed:
5668 Py_XDECREF(tmp);
5669 return 1;
5670 }
5671
5672 int
5673 obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5674 {
5675 PyObject* tmp = NULL;
5676
5677 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5678 *out = Add;
5679 return 0;
5680 }
5681 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5682 *out = Sub;
5683 return 0;
5684 }
5685 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5686 *out = Mult;
5687 return 0;
5688 }
5689 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5690 *out = Div;
5691 return 0;
5692 }
5693 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5694 *out = Mod;
5695 return 0;
5696 }
5697 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5698 *out = Pow;
5699 return 0;
5700 }
5701 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5702 *out = LShift;
5703 return 0;
5704 }
5705 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5706 *out = RShift;
5707 return 0;
5708 }
5709 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5710 *out = BitOr;
5711 return 0;
5712 }
5713 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5714 *out = BitXor;
5715 return 0;
5716 }
5717 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5718 *out = BitAnd;
5719 return 0;
5720 }
5721 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5722 *out = FloorDiv;
5723 return 0;
5724 }
5725
5726 tmp = PyObject_Repr(obj);
5727 if (tmp == NULL) goto failed;
5728 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5729 failed:
5730 Py_XDECREF(tmp);
5731 return 1;
5732 }
5733
5734 int
5735 obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5736 {
5737 PyObject* tmp = NULL;
5738
5739 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5740 *out = Invert;
5741 return 0;
5742 }
5743 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5744 *out = Not;
5745 return 0;
5746 }
5747 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5748 *out = UAdd;
5749 return 0;
5750 }
5751 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5752 *out = USub;
5753 return 0;
5754 }
5755
5756 tmp = PyObject_Repr(obj);
5757 if (tmp == NULL) goto failed;
5758 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5759 failed:
5760 Py_XDECREF(tmp);
5761 return 1;
5762 }
5763
5764 int
5765 obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5766 {
5767 PyObject* tmp = NULL;
5768
5769 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5770 *out = Eq;
5771 return 0;
5772 }
5773 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5774 *out = NotEq;
5775 return 0;
5776 }
5777 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5778 *out = Lt;
5779 return 0;
5780 }
5781 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5782 *out = LtE;
5783 return 0;
5784 }
5785 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5786 *out = Gt;
5787 return 0;
5788 }
5789 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5790 *out = GtE;
5791 return 0;
5792 }
5793 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5794 *out = Is;
5795 return 0;
5796 }
5797 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5798 *out = IsNot;
5799 return 0;
5800 }
5801 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5802 *out = In;
5803 return 0;
5804 }
5805 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5806 *out = NotIn;
5807 return 0;
5808 }
5809
5810 tmp = PyObject_Repr(obj);
5811 if (tmp == NULL) goto failed;
5812 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5813 failed:
5814 Py_XDECREF(tmp);
5815 return 1;
5816 }
5817
5818 int
5819 obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5820 {
5821 PyObject* tmp = NULL;
5822 expr_ty target;
5823 expr_ty iter;
5824 asdl_seq* ifs;
5825
5826 if (PyObject_HasAttrString(obj, "target")) {
5827 int res;
5828 tmp = PyObject_GetAttrString(obj, "target");
5829 if (tmp == NULL) goto failed;
5830 res = obj2ast_expr(tmp, &target, arena);
5831 if (res != 0) goto failed;
5832 Py_XDECREF(tmp);
5833 tmp = NULL;
5834 } else {
5835 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5836 return 1;
5837 }
5838 if (PyObject_HasAttrString(obj, "iter")) {
5839 int res;
5840 tmp = PyObject_GetAttrString(obj, "iter");
5841 if (tmp == NULL) goto failed;
5842 res = obj2ast_expr(tmp, &iter, arena);
5843 if (res != 0) goto failed;
5844 Py_XDECREF(tmp);
5845 tmp = NULL;
5846 } else {
5847 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5848 return 1;
5849 }
5850 if (PyObject_HasAttrString(obj, "ifs")) {
5851 int res;
5852 Py_ssize_t len;
5853 Py_ssize_t i;
5854 tmp = PyObject_GetAttrString(obj, "ifs");
5855 if (tmp == NULL) goto failed;
5856 if (!PyList_Check(tmp)) {
5857 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5858 goto failed;
5859 }
5860 len = PyList_GET_SIZE(tmp);
5861 ifs = asdl_seq_new(len, arena);
5862 if (ifs == NULL) goto failed;
5863 for (i = 0; i < len; i++) {
5864 expr_ty value;
5865 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5866 if (res != 0) goto failed;
5867 asdl_seq_SET(ifs, i, value);
5868 }
5869 Py_XDECREF(tmp);
5870 tmp = NULL;
5871 } else {
5872 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5873 return 1;
5874 }
5875 *out = comprehension(target, iter, ifs, arena);
5876 return 0;
5877 failed:
5878 Py_XDECREF(tmp);
5879 return 1;
5880 }
5881
5882 int
5883 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5884 {
5885 PyObject* tmp = NULL;
5886 expr_ty type;
5887 identifier name;
5888 asdl_seq* body;
5889 int lineno;
5890 int col_offset;
5891
5892 if (PyObject_HasAttrString(obj, "type")) {
5893 int res;
5894 tmp = PyObject_GetAttrString(obj, "type");
5895 if (tmp == NULL) goto failed;
5896 res = obj2ast_expr(tmp, &type, arena);
5897 if (res != 0) goto failed;
5898 Py_XDECREF(tmp);
5899 tmp = NULL;
5900 } else {
5901 type = NULL;
5902 }
5903 if (PyObject_HasAttrString(obj, "name")) {
5904 int res;
5905 tmp = PyObject_GetAttrString(obj, "name");
5906 if (tmp == NULL) goto failed;
5907 res = obj2ast_identifier(tmp, &name, arena);
5908 if (res != 0) goto failed;
5909 Py_XDECREF(tmp);
5910 tmp = NULL;
5911 } else {
5912 name = NULL;
5913 }
5914 if (PyObject_HasAttrString(obj, "body")) {
5915 int res;
5916 Py_ssize_t len;
5917 Py_ssize_t i;
5918 tmp = PyObject_GetAttrString(obj, "body");
5919 if (tmp == NULL) goto failed;
5920 if (!PyList_Check(tmp)) {
5921 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5922 goto failed;
5923 }
5924 len = PyList_GET_SIZE(tmp);
5925 body = asdl_seq_new(len, arena);
5926 if (body == NULL) goto failed;
5927 for (i = 0; i < len; i++) {
5928 stmt_ty value;
5929 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5930 if (res != 0) goto failed;
5931 asdl_seq_SET(body, i, value);
5932 }
5933 Py_XDECREF(tmp);
5934 tmp = NULL;
5935 } else {
5936 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
5937 return 1;
5938 }
5939 if (PyObject_HasAttrString(obj, "lineno")) {
5940 int res;
5941 tmp = PyObject_GetAttrString(obj, "lineno");
5942 if (tmp == NULL) goto failed;
5943 res = obj2ast_int(tmp, &lineno, arena);
5944 if (res != 0) goto failed;
5945 Py_XDECREF(tmp);
5946 tmp = NULL;
5947 } else {
5948 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5949 return 1;
5950 }
5951 if (PyObject_HasAttrString(obj, "col_offset")) {
5952 int res;
5953 tmp = PyObject_GetAttrString(obj, "col_offset");
5954 if (tmp == NULL) goto failed;
5955 res = obj2ast_int(tmp, &col_offset, arena);
5956 if (res != 0) goto failed;
5957 Py_XDECREF(tmp);
5958 tmp = NULL;
5959 } else {
5960 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5961 return 1;
5962 }
5963 *out = excepthandler(type, name, body, lineno, col_offset, arena);
5964 return 0;
5965 failed:
5966 Py_XDECREF(tmp);
5967 return 1;
5968 }
5969
5970 int
5971 obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5972 {
5973 PyObject* tmp = NULL;
5974 asdl_seq* args;
5975 identifier vararg;
5976 expr_ty varargannotation;
5977 asdl_seq* kwonlyargs;
5978 identifier kwarg;
5979 expr_ty kwargannotation;
5980 asdl_seq* defaults;
5981 asdl_seq* kw_defaults;
5982
5983 if (PyObject_HasAttrString(obj, "args")) {
5984 int res;
5985 Py_ssize_t len;
5986 Py_ssize_t i;
5987 tmp = PyObject_GetAttrString(obj, "args");
5988 if (tmp == NULL) goto failed;
5989 if (!PyList_Check(tmp)) {
5990 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5991 goto failed;
5992 }
5993 len = PyList_GET_SIZE(tmp);
5994 args = asdl_seq_new(len, arena);
5995 if (args == NULL) goto failed;
5996 for (i = 0; i < len; i++) {
5997 arg_ty value;
5998 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
5999 if (res != 0) goto failed;
6000 asdl_seq_SET(args, i, value);
6001 }
6002 Py_XDECREF(tmp);
6003 tmp = NULL;
6004 } else {
6005 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6006 return 1;
6007 }
6008 if (PyObject_HasAttrString(obj, "vararg")) {
6009 int res;
6010 tmp = PyObject_GetAttrString(obj, "vararg");
6011 if (tmp == NULL) goto failed;
6012 res = obj2ast_identifier(tmp, &vararg, arena);
6013 if (res != 0) goto failed;
6014 Py_XDECREF(tmp);
6015 tmp = NULL;
6016 } else {
6017 vararg = NULL;
6018 }
6019 if (PyObject_HasAttrString(obj, "varargannotation")) {
6020 int res;
6021 tmp = PyObject_GetAttrString(obj, "varargannotation");
6022 if (tmp == NULL) goto failed;
6023 res = obj2ast_expr(tmp, &varargannotation, arena);
6024 if (res != 0) goto failed;
6025 Py_XDECREF(tmp);
6026 tmp = NULL;
6027 } else {
6028 varargannotation = NULL;
6029 }
6030 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6031 int res;
6032 Py_ssize_t len;
6033 Py_ssize_t i;
6034 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6035 if (tmp == NULL) goto failed;
6036 if (!PyList_Check(tmp)) {
6037 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6038 goto failed;
6039 }
6040 len = PyList_GET_SIZE(tmp);
6041 kwonlyargs = asdl_seq_new(len, arena);
6042 if (kwonlyargs == NULL) goto failed;
6043 for (i = 0; i < len; i++) {
6044 arg_ty value;
6045 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6046 if (res != 0) goto failed;
6047 asdl_seq_SET(kwonlyargs, i, value);
6048 }
6049 Py_XDECREF(tmp);
6050 tmp = NULL;
6051 } else {
6052 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6053 return 1;
6054 }
6055 if (PyObject_HasAttrString(obj, "kwarg")) {
6056 int res;
6057 tmp = PyObject_GetAttrString(obj, "kwarg");
6058 if (tmp == NULL) goto failed;
6059 res = obj2ast_identifier(tmp, &kwarg, arena);
6060 if (res != 0) goto failed;
6061 Py_XDECREF(tmp);
6062 tmp = NULL;
6063 } else {
6064 kwarg = NULL;
6065 }
6066 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6067 int res;
6068 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6069 if (tmp == NULL) goto failed;
6070 res = obj2ast_expr(tmp, &kwargannotation, arena);
6071 if (res != 0) goto failed;
6072 Py_XDECREF(tmp);
6073 tmp = NULL;
6074 } else {
6075 kwargannotation = NULL;
6076 }
6077 if (PyObject_HasAttrString(obj, "defaults")) {
6078 int res;
6079 Py_ssize_t len;
6080 Py_ssize_t i;
6081 tmp = PyObject_GetAttrString(obj, "defaults");
6082 if (tmp == NULL) goto failed;
6083 if (!PyList_Check(tmp)) {
6084 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6085 goto failed;
6086 }
6087 len = PyList_GET_SIZE(tmp);
6088 defaults = asdl_seq_new(len, arena);
6089 if (defaults == NULL) goto failed;
6090 for (i = 0; i < len; i++) {
6091 expr_ty value;
6092 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6093 if (res != 0) goto failed;
6094 asdl_seq_SET(defaults, i, value);
6095 }
6096 Py_XDECREF(tmp);
6097 tmp = NULL;
6098 } else {
6099 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6100 return 1;
6101 }
6102 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6103 int res;
6104 Py_ssize_t len;
6105 Py_ssize_t i;
6106 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6107 if (tmp == NULL) goto failed;
6108 if (!PyList_Check(tmp)) {
6109 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6110 goto failed;
6111 }
6112 len = PyList_GET_SIZE(tmp);
6113 kw_defaults = asdl_seq_new(len, arena);
6114 if (kw_defaults == NULL) goto failed;
6115 for (i = 0; i < len; i++) {
6116 expr_ty value;
6117 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6118 if (res != 0) goto failed;
6119 asdl_seq_SET(kw_defaults, i, value);
6120 }
6121 Py_XDECREF(tmp);
6122 tmp = NULL;
6123 } else {
6124 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6125 return 1;
6126 }
6127 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6128 kwargannotation, defaults, kw_defaults, arena);
6129 return 0;
6130 failed:
6131 Py_XDECREF(tmp);
6132 return 1;
6133 }
6134
6135 int
6136 obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6137 {
6138 PyObject* tmp = NULL;
6139 identifier arg;
6140 expr_ty annotation;
6141
6142 if (PyObject_HasAttrString(obj, "arg")) {
6143 int res;
6144 tmp = PyObject_GetAttrString(obj, "arg");
6145 if (tmp == NULL) goto failed;
6146 res = obj2ast_identifier(tmp, &arg, arena);
6147 if (res != 0) goto failed;
6148 Py_XDECREF(tmp);
6149 tmp = NULL;
6150 } else {
6151 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6152 return 1;
6153 }
6154 if (PyObject_HasAttrString(obj, "annotation")) {
6155 int res;
6156 tmp = PyObject_GetAttrString(obj, "annotation");
6157 if (tmp == NULL) goto failed;
6158 res = obj2ast_expr(tmp, &annotation, arena);
6159 if (res != 0) goto failed;
6160 Py_XDECREF(tmp);
6161 tmp = NULL;
6162 } else {
6163 annotation = NULL;
6164 }
6165 *out = arg(arg, annotation, arena);
6166 return 0;
6167 failed:
6168 Py_XDECREF(tmp);
6169 return 1;
6170 }
6171
6172 int
6173 obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6174 {
6175 PyObject* tmp = NULL;
6176 identifier arg;
6177 expr_ty value;
6178
6179 if (PyObject_HasAttrString(obj, "arg")) {
6180 int res;
6181 tmp = PyObject_GetAttrString(obj, "arg");
6182 if (tmp == NULL) goto failed;
6183 res = obj2ast_identifier(tmp, &arg, arena);
6184 if (res != 0) goto failed;
6185 Py_XDECREF(tmp);
6186 tmp = NULL;
6187 } else {
6188 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6189 return 1;
6190 }
6191 if (PyObject_HasAttrString(obj, "value")) {
6192 int res;
6193 tmp = PyObject_GetAttrString(obj, "value");
6194 if (tmp == NULL) goto failed;
6195 res = obj2ast_expr(tmp, &value, arena);
6196 if (res != 0) goto failed;
6197 Py_XDECREF(tmp);
6198 tmp = NULL;
6199 } else {
6200 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6201 return 1;
6202 }
6203 *out = keyword(arg, value, arena);
6204 return 0;
6205 failed:
6206 Py_XDECREF(tmp);
6207 return 1;
6208 }
6209
6210 int
6211 obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6212 {
6213 PyObject* tmp = NULL;
6214 identifier name;
6215 identifier asname;
6216
6217 if (PyObject_HasAttrString(obj, "name")) {
6218 int res;
6219 tmp = PyObject_GetAttrString(obj, "name");
6220 if (tmp == NULL) goto failed;
6221 res = obj2ast_identifier(tmp, &name, arena);
6222 if (res != 0) goto failed;
6223 Py_XDECREF(tmp);
6224 tmp = NULL;
6225 } else {
6226 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6227 return 1;
6228 }
6229 if (PyObject_HasAttrString(obj, "asname")) {
6230 int res;
6231 tmp = PyObject_GetAttrString(obj, "asname");
6232 if (tmp == NULL) goto failed;
6233 res = obj2ast_identifier(tmp, &asname, arena);
6234 if (res != 0) goto failed;
6235 Py_XDECREF(tmp);
6236 tmp = NULL;
6237 } else {
6238 asname = NULL;
6239 }
6240 *out = alias(name, asname, arena);
6241 return 0;
6242 failed:
6243 Py_XDECREF(tmp);
6244 return 1;
3160 } 6245 }
3161 6246
3162 6247
3163 PyMODINIT_FUNC 6248 PyMODINIT_FUNC
3164 init_ast(void) 6249 init_ast(void)
3328 { 6413 {
3329 init_types(); 6414 init_types();
3330 return ast2obj_mod(t); 6415 return ast2obj_mod(t);
3331 } 6416 }
3332 6417
3333 6418 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
6419 {
6420 mod_ty res;
6421 init_types();
6422 if (!PyObject_IsInstance(ast, mod_type)) {
6423 PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
6424 "or Expression node");
6425 return NULL;
6426 }
6427 if (obj2ast_mod(ast, &res, arena) != 0)
6428 return NULL;
6429 else
6430 return res;
6431 }
6432
6433 int PyAST_Check(PyObject* obj)
6434 {
6435 init_types();
6436 return PyObject_IsInstance(obj, (PyObject*)AST_type);
6437 }
6438
6439