comparison Python/Python-ast.c @ 46162:ca38f7a4b4c9 legacy-trunk

Patch #1810 by Thomas Lee, reviewed by myself: allow compiling Python AST objects into code objects in compile().
author Georg Brandl <georg@python.org>
date Fri, 28 Mar 2008 12:11:56 +0000
parents 3915d5f650fa
children 1b5b6abb1f2e
comparison
equal deleted inserted replaced
46159:f9a629f41656 46162:ca38f7a4b4c9
407 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; 407 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
408 Py_DECREF(l); 408 Py_DECREF(l);
409 return result; 409 return result;
410 } 410 }
411 411
412 /* Conversion AST -> Python */
413
412 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) 414 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
413 { 415 {
414 int i, n = asdl_seq_LEN(seq); 416 int i, n = asdl_seq_LEN(seq);
415 PyObject *result = PyList_New(n); 417 PyObject *result = PyList_New(n);
416 PyObject *value; 418 PyObject *value;
443 445
444 static PyObject* ast2obj_int(long b) 446 static PyObject* ast2obj_int(long b)
445 { 447 {
446 return PyInt_FromLong(b); 448 return PyInt_FromLong(b);
447 } 449 }
450
451 /* Conversion Python -> AST */
452
453 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
454 {
455 if (obj == Py_None)
456 obj = NULL;
457 if (obj)
458 PyArena_AddPyObject(arena, obj);
459 Py_XINCREF(obj);
460 *out = obj;
461 return 0;
462 }
463
464 #define obj2ast_identifier obj2ast_object
465 #define obj2ast_string obj2ast_object
466
467 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
468 {
469 int i;
470 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
471 PyObject *s = PyObject_Repr(obj);
472 if (s == NULL) return 1;
473 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
474 PyString_AS_STRING(s));
475 Py_DECREF(s);
476 return 1;
477 }
478
479 i = (int)PyLong_AsLong(obj);
480 if (i == -1 && PyErr_Occurred())
481 return 1;
482 *out = i;
483 return 0;
484 }
485
486 static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
487 {
488 if (!PyBool_Check(obj)) {
489 PyObject *s = PyObject_Repr(obj);
490 if (s == NULL) return 1;
491 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
492 PyString_AS_STRING(s));
493 Py_DECREF(s);
494 return 1;
495 }
496
497 *out = (obj == Py_True);
498 return 0;
499 }
500
448 501
449 static int init_types(void) 502 static int init_types(void)
450 { 503 {
451 static int initialized; 504 static int initialized;
452 if (initialized) return 1; 505 if (initialized) return 1;
734 if (!alias_type) return 0; 787 if (!alias_type) return 0;
735 initialized = 1; 788 initialized = 1;
736 return 1; 789 return 1;
737 } 790 }
738 791
792 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
793 static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
794 static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
795 static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
796 arena);
797 static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
798 static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
799 static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
800 static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
801 static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
802 static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
803 arena);
804 static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
805 arena);
806 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
807 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
808 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
809
739 mod_ty 810 mod_ty
740 Module(asdl_seq * body, PyArena *arena) 811 Module(asdl_seq * body, PyArena *arena)
741 { 812 {
742 mod_ty p; 813 mod_ty p;
743 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); 814 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
2598 Py_INCREF(AugStore_singleton); 2669 Py_INCREF(AugStore_singleton);
2599 return AugStore_singleton; 2670 return AugStore_singleton;
2600 case Param: 2671 case Param:
2601 Py_INCREF(Param_singleton); 2672 Py_INCREF(Param_singleton);
2602 return Param_singleton; 2673 return Param_singleton;
2603 } 2674 default:
2604 return NULL; /* cannot happen */ 2675 /* should never happen, but just in case ... */
2676 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2677 return NULL;
2678 }
2605 } 2679 }
2606 PyObject* 2680 PyObject*
2607 ast2obj_slice(void* _o) 2681 ast2obj_slice(void* _o)
2608 { 2682 {
2609 slice_ty o = (slice_ty)_o; 2683 slice_ty o = (slice_ty)_o;
2670 Py_INCREF(And_singleton); 2744 Py_INCREF(And_singleton);
2671 return And_singleton; 2745 return And_singleton;
2672 case Or: 2746 case Or:
2673 Py_INCREF(Or_singleton); 2747 Py_INCREF(Or_singleton);
2674 return Or_singleton; 2748 return Or_singleton;
2675 } 2749 default:
2676 return NULL; /* cannot happen */ 2750 /* should never happen, but just in case ... */
2751 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2752 return NULL;
2753 }
2677 } 2754 }
2678 PyObject* ast2obj_operator(operator_ty o) 2755 PyObject* ast2obj_operator(operator_ty o)
2679 { 2756 {
2680 switch(o) { 2757 switch(o) {
2681 case Add: 2758 case Add:
2712 Py_INCREF(BitAnd_singleton); 2789 Py_INCREF(BitAnd_singleton);
2713 return BitAnd_singleton; 2790 return BitAnd_singleton;
2714 case FloorDiv: 2791 case FloorDiv:
2715 Py_INCREF(FloorDiv_singleton); 2792 Py_INCREF(FloorDiv_singleton);
2716 return FloorDiv_singleton; 2793 return FloorDiv_singleton;
2717 } 2794 default:
2718 return NULL; /* cannot happen */ 2795 /* should never happen, but just in case ... */
2796 PyErr_Format(PyExc_SystemError, "unknown operator found");
2797 return NULL;
2798 }
2719 } 2799 }
2720 PyObject* ast2obj_unaryop(unaryop_ty o) 2800 PyObject* ast2obj_unaryop(unaryop_ty o)
2721 { 2801 {
2722 switch(o) { 2802 switch(o) {
2723 case Invert: 2803 case Invert:
2730 Py_INCREF(UAdd_singleton); 2810 Py_INCREF(UAdd_singleton);
2731 return UAdd_singleton; 2811 return UAdd_singleton;
2732 case USub: 2812 case USub:
2733 Py_INCREF(USub_singleton); 2813 Py_INCREF(USub_singleton);
2734 return USub_singleton; 2814 return USub_singleton;
2735 } 2815 default:
2736 return NULL; /* cannot happen */ 2816 /* should never happen, but just in case ... */
2817 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2818 return NULL;
2819 }
2737 } 2820 }
2738 PyObject* ast2obj_cmpop(cmpop_ty o) 2821 PyObject* ast2obj_cmpop(cmpop_ty o)
2739 { 2822 {
2740 switch(o) { 2823 switch(o) {
2741 case Eq: 2824 case Eq:
2766 Py_INCREF(In_singleton); 2849 Py_INCREF(In_singleton);
2767 return In_singleton; 2850 return In_singleton;
2768 case NotIn: 2851 case NotIn:
2769 Py_INCREF(NotIn_singleton); 2852 Py_INCREF(NotIn_singleton);
2770 return NotIn_singleton; 2853 return NotIn_singleton;
2771 } 2854 default:
2772 return NULL; /* cannot happen */ 2855 /* should never happen, but just in case ... */
2856 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2857 return NULL;
2858 }
2773 } 2859 }
2774 PyObject* 2860 PyObject*
2775 ast2obj_comprehension(void* _o) 2861 ast2obj_comprehension(void* _o)
2776 { 2862 {
2777 comprehension_ty o = (comprehension_ty)_o; 2863 comprehension_ty o = (comprehension_ty)_o;
2942 return result; 3028 return result;
2943 failed: 3029 failed:
2944 Py_XDECREF(value); 3030 Py_XDECREF(value);
2945 Py_XDECREF(result); 3031 Py_XDECREF(result);
2946 return NULL; 3032 return NULL;
3033 }
3034
3035
3036 int
3037 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3038 {
3039 PyObject* tmp = NULL;
3040
3041
3042 if (obj == Py_None) {
3043 *out = NULL;
3044 return 0;
3045 }
3046 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3047 asdl_seq* body;
3048
3049 if (PyObject_HasAttrString(obj, "body")) {
3050 int res;
3051 Py_ssize_t len;
3052 Py_ssize_t i;
3053 tmp = PyObject_GetAttrString(obj, "body");
3054 if (tmp == NULL) goto failed;
3055 if (!PyList_Check(tmp)) {
3056 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3057 goto failed;
3058 }
3059 len = PyList_GET_SIZE(tmp);
3060 body = asdl_seq_new(len, arena);
3061 if (body == NULL) goto failed;
3062 for (i = 0; i < len; i++) {
3063 stmt_ty value;
3064 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3065 if (res != 0) goto failed;
3066 asdl_seq_SET(body, i, value);
3067 }
3068 Py_XDECREF(tmp);
3069 tmp = NULL;
3070 } else {
3071 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3072 return 1;
3073 }
3074 *out = Module(body, arena);
3075 if (*out == NULL) goto failed;
3076 return 0;
3077 }
3078 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3079 asdl_seq* body;
3080
3081 if (PyObject_HasAttrString(obj, "body")) {
3082 int res;
3083 Py_ssize_t len;
3084 Py_ssize_t i;
3085 tmp = PyObject_GetAttrString(obj, "body");
3086 if (tmp == NULL) goto failed;
3087 if (!PyList_Check(tmp)) {
3088 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3089 goto failed;
3090 }
3091 len = PyList_GET_SIZE(tmp);
3092 body = asdl_seq_new(len, arena);
3093 if (body == NULL) goto failed;
3094 for (i = 0; i < len; i++) {
3095 stmt_ty value;
3096 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3097 if (res != 0) goto failed;
3098 asdl_seq_SET(body, i, value);
3099 }
3100 Py_XDECREF(tmp);
3101 tmp = NULL;
3102 } else {
3103 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3104 return 1;
3105 }
3106 *out = Interactive(body, arena);
3107 if (*out == NULL) goto failed;
3108 return 0;
3109 }
3110 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3111 expr_ty body;
3112
3113 if (PyObject_HasAttrString(obj, "body")) {
3114 int res;
3115 tmp = PyObject_GetAttrString(obj, "body");
3116 if (tmp == NULL) goto failed;
3117 res = obj2ast_expr(tmp, &body, arena);
3118 if (res != 0) goto failed;
3119 Py_XDECREF(tmp);
3120 tmp = NULL;
3121 } else {
3122 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3123 return 1;
3124 }
3125 *out = Expression(body, arena);
3126 if (*out == NULL) goto failed;
3127 return 0;
3128 }
3129 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3130 asdl_seq* body;
3131
3132 if (PyObject_HasAttrString(obj, "body")) {
3133 int res;
3134 Py_ssize_t len;
3135 Py_ssize_t i;
3136 tmp = PyObject_GetAttrString(obj, "body");
3137 if (tmp == NULL) goto failed;
3138 if (!PyList_Check(tmp)) {
3139 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3140 goto failed;
3141 }
3142 len = PyList_GET_SIZE(tmp);
3143 body = asdl_seq_new(len, arena);
3144 if (body == NULL) goto failed;
3145 for (i = 0; i < len; i++) {
3146 stmt_ty value;
3147 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3148 if (res != 0) goto failed;
3149 asdl_seq_SET(body, i, value);
3150 }
3151 Py_XDECREF(tmp);
3152 tmp = NULL;
3153 } else {
3154 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3155 return 1;
3156 }
3157 *out = Suite(body, arena);
3158 if (*out == NULL) goto failed;
3159 return 0;
3160 }
3161
3162 tmp = PyObject_Repr(obj);
3163 if (tmp == NULL) goto failed;
3164 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3165 failed:
3166 Py_XDECREF(tmp);
3167 return 1;
3168 }
3169
3170 int
3171 obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3172 {
3173 PyObject* tmp = NULL;
3174
3175 int lineno;
3176 int col_offset;
3177
3178 if (obj == Py_None) {
3179 *out = NULL;
3180 return 0;
3181 }
3182 if (PyObject_HasAttrString(obj, "lineno")) {
3183 int res;
3184 tmp = PyObject_GetAttrString(obj, "lineno");
3185 if (tmp == NULL) goto failed;
3186 res = obj2ast_int(tmp, &lineno, arena);
3187 if (res != 0) goto failed;
3188 Py_XDECREF(tmp);
3189 tmp = NULL;
3190 } else {
3191 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3192 return 1;
3193 }
3194 if (PyObject_HasAttrString(obj, "col_offset")) {
3195 int res;
3196 tmp = PyObject_GetAttrString(obj, "col_offset");
3197 if (tmp == NULL) goto failed;
3198 res = obj2ast_int(tmp, &col_offset, arena);
3199 if (res != 0) goto failed;
3200 Py_XDECREF(tmp);
3201 tmp = NULL;
3202 } else {
3203 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3204 return 1;
3205 }
3206 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3207 identifier name;
3208 arguments_ty args;
3209 asdl_seq* body;
3210 asdl_seq* decorator_list;
3211
3212 if (PyObject_HasAttrString(obj, "name")) {
3213 int res;
3214 tmp = PyObject_GetAttrString(obj, "name");
3215 if (tmp == NULL) goto failed;
3216 res = obj2ast_identifier(tmp, &name, arena);
3217 if (res != 0) goto failed;
3218 Py_XDECREF(tmp);
3219 tmp = NULL;
3220 } else {
3221 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3222 return 1;
3223 }
3224 if (PyObject_HasAttrString(obj, "args")) {
3225 int res;
3226 tmp = PyObject_GetAttrString(obj, "args");
3227 if (tmp == NULL) goto failed;
3228 res = obj2ast_arguments(tmp, &args, arena);
3229 if (res != 0) goto failed;
3230 Py_XDECREF(tmp);
3231 tmp = NULL;
3232 } else {
3233 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3234 return 1;
3235 }
3236 if (PyObject_HasAttrString(obj, "body")) {
3237 int res;
3238 Py_ssize_t len;
3239 Py_ssize_t i;
3240 tmp = PyObject_GetAttrString(obj, "body");
3241 if (tmp == NULL) goto failed;
3242 if (!PyList_Check(tmp)) {
3243 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3244 goto failed;
3245 }
3246 len = PyList_GET_SIZE(tmp);
3247 body = asdl_seq_new(len, arena);
3248 if (body == NULL) goto failed;
3249 for (i = 0; i < len; i++) {
3250 stmt_ty value;
3251 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3252 if (res != 0) goto failed;
3253 asdl_seq_SET(body, i, value);
3254 }
3255 Py_XDECREF(tmp);
3256 tmp = NULL;
3257 } else {
3258 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3259 return 1;
3260 }
3261 if (PyObject_HasAttrString(obj, "decorator_list")) {
3262 int res;
3263 Py_ssize_t len;
3264 Py_ssize_t i;
3265 tmp = PyObject_GetAttrString(obj, "decorator_list");
3266 if (tmp == NULL) goto failed;
3267 if (!PyList_Check(tmp)) {
3268 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3269 goto failed;
3270 }
3271 len = PyList_GET_SIZE(tmp);
3272 decorator_list = asdl_seq_new(len, arena);
3273 if (decorator_list == NULL) goto failed;
3274 for (i = 0; i < len; i++) {
3275 expr_ty value;
3276 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3277 if (res != 0) goto failed;
3278 asdl_seq_SET(decorator_list, i, value);
3279 }
3280 Py_XDECREF(tmp);
3281 tmp = NULL;
3282 } else {
3283 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3284 return 1;
3285 }
3286 *out = FunctionDef(name, args, body, decorator_list, lineno,
3287 col_offset, arena);
3288 if (*out == NULL) goto failed;
3289 return 0;
3290 }
3291 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3292 identifier name;
3293 asdl_seq* bases;
3294 asdl_seq* body;
3295 asdl_seq* decorator_list;
3296
3297 if (PyObject_HasAttrString(obj, "name")) {
3298 int res;
3299 tmp = PyObject_GetAttrString(obj, "name");
3300 if (tmp == NULL) goto failed;
3301 res = obj2ast_identifier(tmp, &name, arena);
3302 if (res != 0) goto failed;
3303 Py_XDECREF(tmp);
3304 tmp = NULL;
3305 } else {
3306 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3307 return 1;
3308 }
3309 if (PyObject_HasAttrString(obj, "bases")) {
3310 int res;
3311 Py_ssize_t len;
3312 Py_ssize_t i;
3313 tmp = PyObject_GetAttrString(obj, "bases");
3314 if (tmp == NULL) goto failed;
3315 if (!PyList_Check(tmp)) {
3316 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3317 goto failed;
3318 }
3319 len = PyList_GET_SIZE(tmp);
3320 bases = asdl_seq_new(len, arena);
3321 if (bases == NULL) goto failed;
3322 for (i = 0; i < len; i++) {
3323 expr_ty value;
3324 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3325 if (res != 0) goto failed;
3326 asdl_seq_SET(bases, i, value);
3327 }
3328 Py_XDECREF(tmp);
3329 tmp = NULL;
3330 } else {
3331 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3332 return 1;
3333 }
3334 if (PyObject_HasAttrString(obj, "body")) {
3335 int res;
3336 Py_ssize_t len;
3337 Py_ssize_t i;
3338 tmp = PyObject_GetAttrString(obj, "body");
3339 if (tmp == NULL) goto failed;
3340 if (!PyList_Check(tmp)) {
3341 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3342 goto failed;
3343 }
3344 len = PyList_GET_SIZE(tmp);
3345 body = asdl_seq_new(len, arena);
3346 if (body == NULL) goto failed;
3347 for (i = 0; i < len; i++) {
3348 stmt_ty value;
3349 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3350 if (res != 0) goto failed;
3351 asdl_seq_SET(body, i, value);
3352 }
3353 Py_XDECREF(tmp);
3354 tmp = NULL;
3355 } else {
3356 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3357 return 1;
3358 }
3359 if (PyObject_HasAttrString(obj, "decorator_list")) {
3360 int res;
3361 Py_ssize_t len;
3362 Py_ssize_t i;
3363 tmp = PyObject_GetAttrString(obj, "decorator_list");
3364 if (tmp == NULL) goto failed;
3365 if (!PyList_Check(tmp)) {
3366 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3367 goto failed;
3368 }
3369 len = PyList_GET_SIZE(tmp);
3370 decorator_list = asdl_seq_new(len, arena);
3371 if (decorator_list == NULL) goto failed;
3372 for (i = 0; i < len; i++) {
3373 expr_ty value;
3374 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3375 if (res != 0) goto failed;
3376 asdl_seq_SET(decorator_list, i, value);
3377 }
3378 Py_XDECREF(tmp);
3379 tmp = NULL;
3380 } else {
3381 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3382 return 1;
3383 }
3384 *out = ClassDef(name, bases, body, decorator_list, lineno,
3385 col_offset, arena);
3386 if (*out == NULL) goto failed;
3387 return 0;
3388 }
3389 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3390 expr_ty value;
3391
3392 if (PyObject_HasAttrString(obj, "value")) {
3393 int res;
3394 tmp = PyObject_GetAttrString(obj, "value");
3395 if (tmp == NULL) goto failed;
3396 res = obj2ast_expr(tmp, &value, arena);
3397 if (res != 0) goto failed;
3398 Py_XDECREF(tmp);
3399 tmp = NULL;
3400 } else {
3401 value = NULL;
3402 }
3403 *out = Return(value, lineno, col_offset, arena);
3404 if (*out == NULL) goto failed;
3405 return 0;
3406 }
3407 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3408 asdl_seq* targets;
3409
3410 if (PyObject_HasAttrString(obj, "targets")) {
3411 int res;
3412 Py_ssize_t len;
3413 Py_ssize_t i;
3414 tmp = PyObject_GetAttrString(obj, "targets");
3415 if (tmp == NULL) goto failed;
3416 if (!PyList_Check(tmp)) {
3417 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3418 goto failed;
3419 }
3420 len = PyList_GET_SIZE(tmp);
3421 targets = asdl_seq_new(len, arena);
3422 if (targets == NULL) goto failed;
3423 for (i = 0; i < len; i++) {
3424 expr_ty value;
3425 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3426 if (res != 0) goto failed;
3427 asdl_seq_SET(targets, i, value);
3428 }
3429 Py_XDECREF(tmp);
3430 tmp = NULL;
3431 } else {
3432 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3433 return 1;
3434 }
3435 *out = Delete(targets, lineno, col_offset, arena);
3436 if (*out == NULL) goto failed;
3437 return 0;
3438 }
3439 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3440 asdl_seq* targets;
3441 expr_ty value;
3442
3443 if (PyObject_HasAttrString(obj, "targets")) {
3444 int res;
3445 Py_ssize_t len;
3446 Py_ssize_t i;
3447 tmp = PyObject_GetAttrString(obj, "targets");
3448 if (tmp == NULL) goto failed;
3449 if (!PyList_Check(tmp)) {
3450 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3451 goto failed;
3452 }
3453 len = PyList_GET_SIZE(tmp);
3454 targets = asdl_seq_new(len, arena);
3455 if (targets == NULL) goto failed;
3456 for (i = 0; i < len; i++) {
3457 expr_ty value;
3458 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3459 if (res != 0) goto failed;
3460 asdl_seq_SET(targets, i, value);
3461 }
3462 Py_XDECREF(tmp);
3463 tmp = NULL;
3464 } else {
3465 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3466 return 1;
3467 }
3468 if (PyObject_HasAttrString(obj, "value")) {
3469 int res;
3470 tmp = PyObject_GetAttrString(obj, "value");
3471 if (tmp == NULL) goto failed;
3472 res = obj2ast_expr(tmp, &value, arena);
3473 if (res != 0) goto failed;
3474 Py_XDECREF(tmp);
3475 tmp = NULL;
3476 } else {
3477 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3478 return 1;
3479 }
3480 *out = Assign(targets, value, lineno, col_offset, arena);
3481 if (*out == NULL) goto failed;
3482 return 0;
3483 }
3484 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3485 expr_ty target;
3486 operator_ty op;
3487 expr_ty value;
3488
3489 if (PyObject_HasAttrString(obj, "target")) {
3490 int res;
3491 tmp = PyObject_GetAttrString(obj, "target");
3492 if (tmp == NULL) goto failed;
3493 res = obj2ast_expr(tmp, &target, arena);
3494 if (res != 0) goto failed;
3495 Py_XDECREF(tmp);
3496 tmp = NULL;
3497 } else {
3498 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3499 return 1;
3500 }
3501 if (PyObject_HasAttrString(obj, "op")) {
3502 int res;
3503 tmp = PyObject_GetAttrString(obj, "op");
3504 if (tmp == NULL) goto failed;
3505 res = obj2ast_operator(tmp, &op, arena);
3506 if (res != 0) goto failed;
3507 Py_XDECREF(tmp);
3508 tmp = NULL;
3509 } else {
3510 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3511 return 1;
3512 }
3513 if (PyObject_HasAttrString(obj, "value")) {
3514 int res;
3515 tmp = PyObject_GetAttrString(obj, "value");
3516 if (tmp == NULL) goto failed;
3517 res = obj2ast_expr(tmp, &value, arena);
3518 if (res != 0) goto failed;
3519 Py_XDECREF(tmp);
3520 tmp = NULL;
3521 } else {
3522 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3523 return 1;
3524 }
3525 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3526 if (*out == NULL) goto failed;
3527 return 0;
3528 }
3529 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3530 expr_ty dest;
3531 asdl_seq* values;
3532 bool nl;
3533
3534 if (PyObject_HasAttrString(obj, "dest")) {
3535 int res;
3536 tmp = PyObject_GetAttrString(obj, "dest");
3537 if (tmp == NULL) goto failed;
3538 res = obj2ast_expr(tmp, &dest, arena);
3539 if (res != 0) goto failed;
3540 Py_XDECREF(tmp);
3541 tmp = NULL;
3542 } else {
3543 dest = NULL;
3544 }
3545 if (PyObject_HasAttrString(obj, "values")) {
3546 int res;
3547 Py_ssize_t len;
3548 Py_ssize_t i;
3549 tmp = PyObject_GetAttrString(obj, "values");
3550 if (tmp == NULL) goto failed;
3551 if (!PyList_Check(tmp)) {
3552 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3553 goto failed;
3554 }
3555 len = PyList_GET_SIZE(tmp);
3556 values = asdl_seq_new(len, arena);
3557 if (values == NULL) goto failed;
3558 for (i = 0; i < len; i++) {
3559 expr_ty value;
3560 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3561 if (res != 0) goto failed;
3562 asdl_seq_SET(values, i, value);
3563 }
3564 Py_XDECREF(tmp);
3565 tmp = NULL;
3566 } else {
3567 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3568 return 1;
3569 }
3570 if (PyObject_HasAttrString(obj, "nl")) {
3571 int res;
3572 tmp = PyObject_GetAttrString(obj, "nl");
3573 if (tmp == NULL) goto failed;
3574 res = obj2ast_bool(tmp, &nl, arena);
3575 if (res != 0) goto failed;
3576 Py_XDECREF(tmp);
3577 tmp = NULL;
3578 } else {
3579 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3580 return 1;
3581 }
3582 *out = Print(dest, values, nl, lineno, col_offset, arena);
3583 if (*out == NULL) goto failed;
3584 return 0;
3585 }
3586 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3587 expr_ty target;
3588 expr_ty iter;
3589 asdl_seq* body;
3590 asdl_seq* orelse;
3591
3592 if (PyObject_HasAttrString(obj, "target")) {
3593 int res;
3594 tmp = PyObject_GetAttrString(obj, "target");
3595 if (tmp == NULL) goto failed;
3596 res = obj2ast_expr(tmp, &target, arena);
3597 if (res != 0) goto failed;
3598 Py_XDECREF(tmp);
3599 tmp = NULL;
3600 } else {
3601 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3602 return 1;
3603 }
3604 if (PyObject_HasAttrString(obj, "iter")) {
3605 int res;
3606 tmp = PyObject_GetAttrString(obj, "iter");
3607 if (tmp == NULL) goto failed;
3608 res = obj2ast_expr(tmp, &iter, arena);
3609 if (res != 0) goto failed;
3610 Py_XDECREF(tmp);
3611 tmp = NULL;
3612 } else {
3613 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3614 return 1;
3615 }
3616 if (PyObject_HasAttrString(obj, "body")) {
3617 int res;
3618 Py_ssize_t len;
3619 Py_ssize_t i;
3620 tmp = PyObject_GetAttrString(obj, "body");
3621 if (tmp == NULL) goto failed;
3622 if (!PyList_Check(tmp)) {
3623 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3624 goto failed;
3625 }
3626 len = PyList_GET_SIZE(tmp);
3627 body = asdl_seq_new(len, arena);
3628 if (body == NULL) goto failed;
3629 for (i = 0; i < len; i++) {
3630 stmt_ty value;
3631 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3632 if (res != 0) goto failed;
3633 asdl_seq_SET(body, i, value);
3634 }
3635 Py_XDECREF(tmp);
3636 tmp = NULL;
3637 } else {
3638 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3639 return 1;
3640 }
3641 if (PyObject_HasAttrString(obj, "orelse")) {
3642 int res;
3643 Py_ssize_t len;
3644 Py_ssize_t i;
3645 tmp = PyObject_GetAttrString(obj, "orelse");
3646 if (tmp == NULL) goto failed;
3647 if (!PyList_Check(tmp)) {
3648 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3649 goto failed;
3650 }
3651 len = PyList_GET_SIZE(tmp);
3652 orelse = asdl_seq_new(len, arena);
3653 if (orelse == NULL) goto failed;
3654 for (i = 0; i < len; i++) {
3655 stmt_ty value;
3656 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3657 if (res != 0) goto failed;
3658 asdl_seq_SET(orelse, i, value);
3659 }
3660 Py_XDECREF(tmp);
3661 tmp = NULL;
3662 } else {
3663 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3664 return 1;
3665 }
3666 *out = For(target, iter, body, orelse, lineno, col_offset,
3667 arena);
3668 if (*out == NULL) goto failed;
3669 return 0;
3670 }
3671 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3672 expr_ty test;
3673 asdl_seq* body;
3674 asdl_seq* orelse;
3675
3676 if (PyObject_HasAttrString(obj, "test")) {
3677 int res;
3678 tmp = PyObject_GetAttrString(obj, "test");
3679 if (tmp == NULL) goto failed;
3680 res = obj2ast_expr(tmp, &test, arena);
3681 if (res != 0) goto failed;
3682 Py_XDECREF(tmp);
3683 tmp = NULL;
3684 } else {
3685 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3686 return 1;
3687 }
3688 if (PyObject_HasAttrString(obj, "body")) {
3689 int res;
3690 Py_ssize_t len;
3691 Py_ssize_t i;
3692 tmp = PyObject_GetAttrString(obj, "body");
3693 if (tmp == NULL) goto failed;
3694 if (!PyList_Check(tmp)) {
3695 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3696 goto failed;
3697 }
3698 len = PyList_GET_SIZE(tmp);
3699 body = asdl_seq_new(len, arena);
3700 if (body == NULL) goto failed;
3701 for (i = 0; i < len; i++) {
3702 stmt_ty value;
3703 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3704 if (res != 0) goto failed;
3705 asdl_seq_SET(body, i, value);
3706 }
3707 Py_XDECREF(tmp);
3708 tmp = NULL;
3709 } else {
3710 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3711 return 1;
3712 }
3713 if (PyObject_HasAttrString(obj, "orelse")) {
3714 int res;
3715 Py_ssize_t len;
3716 Py_ssize_t i;
3717 tmp = PyObject_GetAttrString(obj, "orelse");
3718 if (tmp == NULL) goto failed;
3719 if (!PyList_Check(tmp)) {
3720 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3721 goto failed;
3722 }
3723 len = PyList_GET_SIZE(tmp);
3724 orelse = asdl_seq_new(len, arena);
3725 if (orelse == NULL) goto failed;
3726 for (i = 0; i < len; i++) {
3727 stmt_ty value;
3728 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3729 if (res != 0) goto failed;
3730 asdl_seq_SET(orelse, i, value);
3731 }
3732 Py_XDECREF(tmp);
3733 tmp = NULL;
3734 } else {
3735 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3736 return 1;
3737 }
3738 *out = While(test, body, orelse, lineno, col_offset, arena);
3739 if (*out == NULL) goto failed;
3740 return 0;
3741 }
3742 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3743 expr_ty test;
3744 asdl_seq* body;
3745 asdl_seq* orelse;
3746
3747 if (PyObject_HasAttrString(obj, "test")) {
3748 int res;
3749 tmp = PyObject_GetAttrString(obj, "test");
3750 if (tmp == NULL) goto failed;
3751 res = obj2ast_expr(tmp, &test, arena);
3752 if (res != 0) goto failed;
3753 Py_XDECREF(tmp);
3754 tmp = NULL;
3755 } else {
3756 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3757 return 1;
3758 }
3759 if (PyObject_HasAttrString(obj, "body")) {
3760 int res;
3761 Py_ssize_t len;
3762 Py_ssize_t i;
3763 tmp = PyObject_GetAttrString(obj, "body");
3764 if (tmp == NULL) goto failed;
3765 if (!PyList_Check(tmp)) {
3766 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3767 goto failed;
3768 }
3769 len = PyList_GET_SIZE(tmp);
3770 body = asdl_seq_new(len, arena);
3771 if (body == NULL) goto failed;
3772 for (i = 0; i < len; i++) {
3773 stmt_ty value;
3774 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3775 if (res != 0) goto failed;
3776 asdl_seq_SET(body, i, value);
3777 }
3778 Py_XDECREF(tmp);
3779 tmp = NULL;
3780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3782 return 1;
3783 }
3784 if (PyObject_HasAttrString(obj, "orelse")) {
3785 int res;
3786 Py_ssize_t len;
3787 Py_ssize_t i;
3788 tmp = PyObject_GetAttrString(obj, "orelse");
3789 if (tmp == NULL) goto failed;
3790 if (!PyList_Check(tmp)) {
3791 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3792 goto failed;
3793 }
3794 len = PyList_GET_SIZE(tmp);
3795 orelse = asdl_seq_new(len, arena);
3796 if (orelse == NULL) goto failed;
3797 for (i = 0; i < len; i++) {
3798 stmt_ty value;
3799 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3800 if (res != 0) goto failed;
3801 asdl_seq_SET(orelse, i, value);
3802 }
3803 Py_XDECREF(tmp);
3804 tmp = NULL;
3805 } else {
3806 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3807 return 1;
3808 }
3809 *out = If(test, body, orelse, lineno, col_offset, arena);
3810 if (*out == NULL) goto failed;
3811 return 0;
3812 }
3813 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3814 expr_ty context_expr;
3815 expr_ty optional_vars;
3816 asdl_seq* body;
3817
3818 if (PyObject_HasAttrString(obj, "context_expr")) {
3819 int res;
3820 tmp = PyObject_GetAttrString(obj, "context_expr");
3821 if (tmp == NULL) goto failed;
3822 res = obj2ast_expr(tmp, &context_expr, arena);
3823 if (res != 0) goto failed;
3824 Py_XDECREF(tmp);
3825 tmp = NULL;
3826 } else {
3827 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3828 return 1;
3829 }
3830 if (PyObject_HasAttrString(obj, "optional_vars")) {
3831 int res;
3832 tmp = PyObject_GetAttrString(obj, "optional_vars");
3833 if (tmp == NULL) goto failed;
3834 res = obj2ast_expr(tmp, &optional_vars, arena);
3835 if (res != 0) goto failed;
3836 Py_XDECREF(tmp);
3837 tmp = NULL;
3838 } else {
3839 optional_vars = NULL;
3840 }
3841 if (PyObject_HasAttrString(obj, "body")) {
3842 int res;
3843 Py_ssize_t len;
3844 Py_ssize_t i;
3845 tmp = PyObject_GetAttrString(obj, "body");
3846 if (tmp == NULL) goto failed;
3847 if (!PyList_Check(tmp)) {
3848 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3849 goto failed;
3850 }
3851 len = PyList_GET_SIZE(tmp);
3852 body = asdl_seq_new(len, arena);
3853 if (body == NULL) goto failed;
3854 for (i = 0; i < len; i++) {
3855 stmt_ty value;
3856 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3857 if (res != 0) goto failed;
3858 asdl_seq_SET(body, i, value);
3859 }
3860 Py_XDECREF(tmp);
3861 tmp = NULL;
3862 } else {
3863 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3864 return 1;
3865 }
3866 *out = With(context_expr, optional_vars, body, lineno,
3867 col_offset, arena);
3868 if (*out == NULL) goto failed;
3869 return 0;
3870 }
3871 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3872 expr_ty type;
3873 expr_ty inst;
3874 expr_ty tback;
3875
3876 if (PyObject_HasAttrString(obj, "type")) {
3877 int res;
3878 tmp = PyObject_GetAttrString(obj, "type");
3879 if (tmp == NULL) goto failed;
3880 res = obj2ast_expr(tmp, &type, arena);
3881 if (res != 0) goto failed;
3882 Py_XDECREF(tmp);
3883 tmp = NULL;
3884 } else {
3885 type = NULL;
3886 }
3887 if (PyObject_HasAttrString(obj, "inst")) {
3888 int res;
3889 tmp = PyObject_GetAttrString(obj, "inst");
3890 if (tmp == NULL) goto failed;
3891 res = obj2ast_expr(tmp, &inst, arena);
3892 if (res != 0) goto failed;
3893 Py_XDECREF(tmp);
3894 tmp = NULL;
3895 } else {
3896 inst = NULL;
3897 }
3898 if (PyObject_HasAttrString(obj, "tback")) {
3899 int res;
3900 tmp = PyObject_GetAttrString(obj, "tback");
3901 if (tmp == NULL) goto failed;
3902 res = obj2ast_expr(tmp, &tback, arena);
3903 if (res != 0) goto failed;
3904 Py_XDECREF(tmp);
3905 tmp = NULL;
3906 } else {
3907 tback = NULL;
3908 }
3909 *out = Raise(type, inst, tback, lineno, col_offset, arena);
3910 if (*out == NULL) goto failed;
3911 return 0;
3912 }
3913 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
3914 asdl_seq* body;
3915 asdl_seq* handlers;
3916 asdl_seq* orelse;
3917
3918 if (PyObject_HasAttrString(obj, "body")) {
3919 int res;
3920 Py_ssize_t len;
3921 Py_ssize_t i;
3922 tmp = PyObject_GetAttrString(obj, "body");
3923 if (tmp == NULL) goto failed;
3924 if (!PyList_Check(tmp)) {
3925 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3926 goto failed;
3927 }
3928 len = PyList_GET_SIZE(tmp);
3929 body = asdl_seq_new(len, arena);
3930 if (body == NULL) goto failed;
3931 for (i = 0; i < len; i++) {
3932 stmt_ty value;
3933 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3934 if (res != 0) goto failed;
3935 asdl_seq_SET(body, i, value);
3936 }
3937 Py_XDECREF(tmp);
3938 tmp = NULL;
3939 } else {
3940 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
3941 return 1;
3942 }
3943 if (PyObject_HasAttrString(obj, "handlers")) {
3944 int res;
3945 Py_ssize_t len;
3946 Py_ssize_t i;
3947 tmp = PyObject_GetAttrString(obj, "handlers");
3948 if (tmp == NULL) goto failed;
3949 if (!PyList_Check(tmp)) {
3950 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3951 goto failed;
3952 }
3953 len = PyList_GET_SIZE(tmp);
3954 handlers = asdl_seq_new(len, arena);
3955 if (handlers == NULL) goto failed;
3956 for (i = 0; i < len; i++) {
3957 excepthandler_ty value;
3958 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
3959 if (res != 0) goto failed;
3960 asdl_seq_SET(handlers, i, value);
3961 }
3962 Py_XDECREF(tmp);
3963 tmp = NULL;
3964 } else {
3965 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
3966 return 1;
3967 }
3968 if (PyObject_HasAttrString(obj, "orelse")) {
3969 int res;
3970 Py_ssize_t len;
3971 Py_ssize_t i;
3972 tmp = PyObject_GetAttrString(obj, "orelse");
3973 if (tmp == NULL) goto failed;
3974 if (!PyList_Check(tmp)) {
3975 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3976 goto failed;
3977 }
3978 len = PyList_GET_SIZE(tmp);
3979 orelse = asdl_seq_new(len, arena);
3980 if (orelse == NULL) goto failed;
3981 for (i = 0; i < len; i++) {
3982 stmt_ty value;
3983 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3984 if (res != 0) goto failed;
3985 asdl_seq_SET(orelse, i, value);
3986 }
3987 Py_XDECREF(tmp);
3988 tmp = NULL;
3989 } else {
3990 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
3991 return 1;
3992 }
3993 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
3994 arena);
3995 if (*out == NULL) goto failed;
3996 return 0;
3997 }
3998 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
3999 asdl_seq* body;
4000 asdl_seq* finalbody;
4001
4002 if (PyObject_HasAttrString(obj, "body")) {
4003 int res;
4004 Py_ssize_t len;
4005 Py_ssize_t i;
4006 tmp = PyObject_GetAttrString(obj, "body");
4007 if (tmp == NULL) goto failed;
4008 if (!PyList_Check(tmp)) {
4009 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4010 goto failed;
4011 }
4012 len = PyList_GET_SIZE(tmp);
4013 body = asdl_seq_new(len, arena);
4014 if (body == NULL) goto failed;
4015 for (i = 0; i < len; i++) {
4016 stmt_ty value;
4017 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4018 if (res != 0) goto failed;
4019 asdl_seq_SET(body, i, value);
4020 }
4021 Py_XDECREF(tmp);
4022 tmp = NULL;
4023 } else {
4024 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4025 return 1;
4026 }
4027 if (PyObject_HasAttrString(obj, "finalbody")) {
4028 int res;
4029 Py_ssize_t len;
4030 Py_ssize_t i;
4031 tmp = PyObject_GetAttrString(obj, "finalbody");
4032 if (tmp == NULL) goto failed;
4033 if (!PyList_Check(tmp)) {
4034 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4035 goto failed;
4036 }
4037 len = PyList_GET_SIZE(tmp);
4038 finalbody = asdl_seq_new(len, arena);
4039 if (finalbody == NULL) goto failed;
4040 for (i = 0; i < len; i++) {
4041 stmt_ty value;
4042 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4043 if (res != 0) goto failed;
4044 asdl_seq_SET(finalbody, i, value);
4045 }
4046 Py_XDECREF(tmp);
4047 tmp = NULL;
4048 } else {
4049 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4050 return 1;
4051 }
4052 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4053 if (*out == NULL) goto failed;
4054 return 0;
4055 }
4056 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4057 expr_ty test;
4058 expr_ty msg;
4059
4060 if (PyObject_HasAttrString(obj, "test")) {
4061 int res;
4062 tmp = PyObject_GetAttrString(obj, "test");
4063 if (tmp == NULL) goto failed;
4064 res = obj2ast_expr(tmp, &test, arena);
4065 if (res != 0) goto failed;
4066 Py_XDECREF(tmp);
4067 tmp = NULL;
4068 } else {
4069 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4070 return 1;
4071 }
4072 if (PyObject_HasAttrString(obj, "msg")) {
4073 int res;
4074 tmp = PyObject_GetAttrString(obj, "msg");
4075 if (tmp == NULL) goto failed;
4076 res = obj2ast_expr(tmp, &msg, arena);
4077 if (res != 0) goto failed;
4078 Py_XDECREF(tmp);
4079 tmp = NULL;
4080 } else {
4081 msg = NULL;
4082 }
4083 *out = Assert(test, msg, lineno, col_offset, arena);
4084 if (*out == NULL) goto failed;
4085 return 0;
4086 }
4087 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4088 asdl_seq* names;
4089
4090 if (PyObject_HasAttrString(obj, "names")) {
4091 int res;
4092 Py_ssize_t len;
4093 Py_ssize_t i;
4094 tmp = PyObject_GetAttrString(obj, "names");
4095 if (tmp == NULL) goto failed;
4096 if (!PyList_Check(tmp)) {
4097 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4098 goto failed;
4099 }
4100 len = PyList_GET_SIZE(tmp);
4101 names = asdl_seq_new(len, arena);
4102 if (names == NULL) goto failed;
4103 for (i = 0; i < len; i++) {
4104 alias_ty value;
4105 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4106 if (res != 0) goto failed;
4107 asdl_seq_SET(names, i, value);
4108 }
4109 Py_XDECREF(tmp);
4110 tmp = NULL;
4111 } else {
4112 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4113 return 1;
4114 }
4115 *out = Import(names, lineno, col_offset, arena);
4116 if (*out == NULL) goto failed;
4117 return 0;
4118 }
4119 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4120 identifier module;
4121 asdl_seq* names;
4122 int level;
4123
4124 if (PyObject_HasAttrString(obj, "module")) {
4125 int res;
4126 tmp = PyObject_GetAttrString(obj, "module");
4127 if (tmp == NULL) goto failed;
4128 res = obj2ast_identifier(tmp, &module, arena);
4129 if (res != 0) goto failed;
4130 Py_XDECREF(tmp);
4131 tmp = NULL;
4132 } else {
4133 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4134 return 1;
4135 }
4136 if (PyObject_HasAttrString(obj, "names")) {
4137 int res;
4138 Py_ssize_t len;
4139 Py_ssize_t i;
4140 tmp = PyObject_GetAttrString(obj, "names");
4141 if (tmp == NULL) goto failed;
4142 if (!PyList_Check(tmp)) {
4143 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4144 goto failed;
4145 }
4146 len = PyList_GET_SIZE(tmp);
4147 names = asdl_seq_new(len, arena);
4148 if (names == NULL) goto failed;
4149 for (i = 0; i < len; i++) {
4150 alias_ty value;
4151 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4152 if (res != 0) goto failed;
4153 asdl_seq_SET(names, i, value);
4154 }
4155 Py_XDECREF(tmp);
4156 tmp = NULL;
4157 } else {
4158 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4159 return 1;
4160 }
4161 if (PyObject_HasAttrString(obj, "level")) {
4162 int res;
4163 tmp = PyObject_GetAttrString(obj, "level");
4164 if (tmp == NULL) goto failed;
4165 res = obj2ast_int(tmp, &level, arena);
4166 if (res != 0) goto failed;
4167 Py_XDECREF(tmp);
4168 tmp = NULL;
4169 } else {
4170 level = 0;
4171 }
4172 *out = ImportFrom(module, names, level, lineno, col_offset,
4173 arena);
4174 if (*out == NULL) goto failed;
4175 return 0;
4176 }
4177 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4178 expr_ty body;
4179 expr_ty globals;
4180 expr_ty locals;
4181
4182 if (PyObject_HasAttrString(obj, "body")) {
4183 int res;
4184 tmp = PyObject_GetAttrString(obj, "body");
4185 if (tmp == NULL) goto failed;
4186 res = obj2ast_expr(tmp, &body, arena);
4187 if (res != 0) goto failed;
4188 Py_XDECREF(tmp);
4189 tmp = NULL;
4190 } else {
4191 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4192 return 1;
4193 }
4194 if (PyObject_HasAttrString(obj, "globals")) {
4195 int res;
4196 tmp = PyObject_GetAttrString(obj, "globals");
4197 if (tmp == NULL) goto failed;
4198 res = obj2ast_expr(tmp, &globals, arena);
4199 if (res != 0) goto failed;
4200 Py_XDECREF(tmp);
4201 tmp = NULL;
4202 } else {
4203 globals = NULL;
4204 }
4205 if (PyObject_HasAttrString(obj, "locals")) {
4206 int res;
4207 tmp = PyObject_GetAttrString(obj, "locals");
4208 if (tmp == NULL) goto failed;
4209 res = obj2ast_expr(tmp, &locals, arena);
4210 if (res != 0) goto failed;
4211 Py_XDECREF(tmp);
4212 tmp = NULL;
4213 } else {
4214 locals = NULL;
4215 }
4216 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4217 if (*out == NULL) goto failed;
4218 return 0;
4219 }
4220 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4221 asdl_seq* names;
4222
4223 if (PyObject_HasAttrString(obj, "names")) {
4224 int res;
4225 Py_ssize_t len;
4226 Py_ssize_t i;
4227 tmp = PyObject_GetAttrString(obj, "names");
4228 if (tmp == NULL) goto failed;
4229 if (!PyList_Check(tmp)) {
4230 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4231 goto failed;
4232 }
4233 len = PyList_GET_SIZE(tmp);
4234 names = asdl_seq_new(len, arena);
4235 if (names == NULL) goto failed;
4236 for (i = 0; i < len; i++) {
4237 identifier value;
4238 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4239 if (res != 0) goto failed;
4240 asdl_seq_SET(names, i, value);
4241 }
4242 Py_XDECREF(tmp);
4243 tmp = NULL;
4244 } else {
4245 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4246 return 1;
4247 }
4248 *out = Global(names, lineno, col_offset, arena);
4249 if (*out == NULL) goto failed;
4250 return 0;
4251 }
4252 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4253 expr_ty value;
4254
4255 if (PyObject_HasAttrString(obj, "value")) {
4256 int res;
4257 tmp = PyObject_GetAttrString(obj, "value");
4258 if (tmp == NULL) goto failed;
4259 res = obj2ast_expr(tmp, &value, arena);
4260 if (res != 0) goto failed;
4261 Py_XDECREF(tmp);
4262 tmp = NULL;
4263 } else {
4264 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4265 return 1;
4266 }
4267 *out = Expr(value, lineno, col_offset, arena);
4268 if (*out == NULL) goto failed;
4269 return 0;
4270 }
4271 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4272
4273 *out = Pass(lineno, col_offset, arena);
4274 if (*out == NULL) goto failed;
4275 return 0;
4276 }
4277 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4278
4279 *out = Break(lineno, col_offset, arena);
4280 if (*out == NULL) goto failed;
4281 return 0;
4282 }
4283 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4284
4285 *out = Continue(lineno, col_offset, arena);
4286 if (*out == NULL) goto failed;
4287 return 0;
4288 }
4289
4290 tmp = PyObject_Repr(obj);
4291 if (tmp == NULL) goto failed;
4292 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4293 failed:
4294 Py_XDECREF(tmp);
4295 return 1;
4296 }
4297
4298 int
4299 obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4300 {
4301 PyObject* tmp = NULL;
4302
4303 int lineno;
4304 int col_offset;
4305
4306 if (obj == Py_None) {
4307 *out = NULL;
4308 return 0;
4309 }
4310 if (PyObject_HasAttrString(obj, "lineno")) {
4311 int res;
4312 tmp = PyObject_GetAttrString(obj, "lineno");
4313 if (tmp == NULL) goto failed;
4314 res = obj2ast_int(tmp, &lineno, arena);
4315 if (res != 0) goto failed;
4316 Py_XDECREF(tmp);
4317 tmp = NULL;
4318 } else {
4319 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4320 return 1;
4321 }
4322 if (PyObject_HasAttrString(obj, "col_offset")) {
4323 int res;
4324 tmp = PyObject_GetAttrString(obj, "col_offset");
4325 if (tmp == NULL) goto failed;
4326 res = obj2ast_int(tmp, &col_offset, arena);
4327 if (res != 0) goto failed;
4328 Py_XDECREF(tmp);
4329 tmp = NULL;
4330 } else {
4331 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4332 return 1;
4333 }
4334 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4335 boolop_ty op;
4336 asdl_seq* values;
4337
4338 if (PyObject_HasAttrString(obj, "op")) {
4339 int res;
4340 tmp = PyObject_GetAttrString(obj, "op");
4341 if (tmp == NULL) goto failed;
4342 res = obj2ast_boolop(tmp, &op, arena);
4343 if (res != 0) goto failed;
4344 Py_XDECREF(tmp);
4345 tmp = NULL;
4346 } else {
4347 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4348 return 1;
4349 }
4350 if (PyObject_HasAttrString(obj, "values")) {
4351 int res;
4352 Py_ssize_t len;
4353 Py_ssize_t i;
4354 tmp = PyObject_GetAttrString(obj, "values");
4355 if (tmp == NULL) goto failed;
4356 if (!PyList_Check(tmp)) {
4357 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4358 goto failed;
4359 }
4360 len = PyList_GET_SIZE(tmp);
4361 values = asdl_seq_new(len, arena);
4362 if (values == NULL) goto failed;
4363 for (i = 0; i < len; i++) {
4364 expr_ty value;
4365 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4366 if (res != 0) goto failed;
4367 asdl_seq_SET(values, i, value);
4368 }
4369 Py_XDECREF(tmp);
4370 tmp = NULL;
4371 } else {
4372 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4373 return 1;
4374 }
4375 *out = BoolOp(op, values, lineno, col_offset, arena);
4376 if (*out == NULL) goto failed;
4377 return 0;
4378 }
4379 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4380 expr_ty left;
4381 operator_ty op;
4382 expr_ty right;
4383
4384 if (PyObject_HasAttrString(obj, "left")) {
4385 int res;
4386 tmp = PyObject_GetAttrString(obj, "left");
4387 if (tmp == NULL) goto failed;
4388 res = obj2ast_expr(tmp, &left, arena);
4389 if (res != 0) goto failed;
4390 Py_XDECREF(tmp);
4391 tmp = NULL;
4392 } else {
4393 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4394 return 1;
4395 }
4396 if (PyObject_HasAttrString(obj, "op")) {
4397 int res;
4398 tmp = PyObject_GetAttrString(obj, "op");
4399 if (tmp == NULL) goto failed;
4400 res = obj2ast_operator(tmp, &op, arena);
4401 if (res != 0) goto failed;
4402 Py_XDECREF(tmp);
4403 tmp = NULL;
4404 } else {
4405 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4406 return 1;
4407 }
4408 if (PyObject_HasAttrString(obj, "right")) {
4409 int res;
4410 tmp = PyObject_GetAttrString(obj, "right");
4411 if (tmp == NULL) goto failed;
4412 res = obj2ast_expr(tmp, &right, arena);
4413 if (res != 0) goto failed;
4414 Py_XDECREF(tmp);
4415 tmp = NULL;
4416 } else {
4417 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4418 return 1;
4419 }
4420 *out = BinOp(left, op, right, lineno, col_offset, arena);
4421 if (*out == NULL) goto failed;
4422 return 0;
4423 }
4424 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4425 unaryop_ty op;
4426 expr_ty operand;
4427
4428 if (PyObject_HasAttrString(obj, "op")) {
4429 int res;
4430 tmp = PyObject_GetAttrString(obj, "op");
4431 if (tmp == NULL) goto failed;
4432 res = obj2ast_unaryop(tmp, &op, arena);
4433 if (res != 0) goto failed;
4434 Py_XDECREF(tmp);
4435 tmp = NULL;
4436 } else {
4437 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4438 return 1;
4439 }
4440 if (PyObject_HasAttrString(obj, "operand")) {
4441 int res;
4442 tmp = PyObject_GetAttrString(obj, "operand");
4443 if (tmp == NULL) goto failed;
4444 res = obj2ast_expr(tmp, &operand, arena);
4445 if (res != 0) goto failed;
4446 Py_XDECREF(tmp);
4447 tmp = NULL;
4448 } else {
4449 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4450 return 1;
4451 }
4452 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4453 if (*out == NULL) goto failed;
4454 return 0;
4455 }
4456 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4457 arguments_ty args;
4458 expr_ty body;
4459
4460 if (PyObject_HasAttrString(obj, "args")) {
4461 int res;
4462 tmp = PyObject_GetAttrString(obj, "args");
4463 if (tmp == NULL) goto failed;
4464 res = obj2ast_arguments(tmp, &args, arena);
4465 if (res != 0) goto failed;
4466 Py_XDECREF(tmp);
4467 tmp = NULL;
4468 } else {
4469 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4470 return 1;
4471 }
4472 if (PyObject_HasAttrString(obj, "body")) {
4473 int res;
4474 tmp = PyObject_GetAttrString(obj, "body");
4475 if (tmp == NULL) goto failed;
4476 res = obj2ast_expr(tmp, &body, arena);
4477 if (res != 0) goto failed;
4478 Py_XDECREF(tmp);
4479 tmp = NULL;
4480 } else {
4481 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4482 return 1;
4483 }
4484 *out = Lambda(args, body, lineno, col_offset, arena);
4485 if (*out == NULL) goto failed;
4486 return 0;
4487 }
4488 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4489 expr_ty test;
4490 expr_ty body;
4491 expr_ty orelse;
4492
4493 if (PyObject_HasAttrString(obj, "test")) {
4494 int res;
4495 tmp = PyObject_GetAttrString(obj, "test");
4496 if (tmp == NULL) goto failed;
4497 res = obj2ast_expr(tmp, &test, arena);
4498 if (res != 0) goto failed;
4499 Py_XDECREF(tmp);
4500 tmp = NULL;
4501 } else {
4502 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4503 return 1;
4504 }
4505 if (PyObject_HasAttrString(obj, "body")) {
4506 int res;
4507 tmp = PyObject_GetAttrString(obj, "body");
4508 if (tmp == NULL) goto failed;
4509 res = obj2ast_expr(tmp, &body, arena);
4510 if (res != 0) goto failed;
4511 Py_XDECREF(tmp);
4512 tmp = NULL;
4513 } else {
4514 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4515 return 1;
4516 }
4517 if (PyObject_HasAttrString(obj, "orelse")) {
4518 int res;
4519 tmp = PyObject_GetAttrString(obj, "orelse");
4520 if (tmp == NULL) goto failed;
4521 res = obj2ast_expr(tmp, &orelse, arena);
4522 if (res != 0) goto failed;
4523 Py_XDECREF(tmp);
4524 tmp = NULL;
4525 } else {
4526 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4527 return 1;
4528 }
4529 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4530 if (*out == NULL) goto failed;
4531 return 0;
4532 }
4533 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4534 asdl_seq* keys;
4535 asdl_seq* values;
4536
4537 if (PyObject_HasAttrString(obj, "keys")) {
4538 int res;
4539 Py_ssize_t len;
4540 Py_ssize_t i;
4541 tmp = PyObject_GetAttrString(obj, "keys");
4542 if (tmp == NULL) goto failed;
4543 if (!PyList_Check(tmp)) {
4544 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4545 goto failed;
4546 }
4547 len = PyList_GET_SIZE(tmp);
4548 keys = asdl_seq_new(len, arena);
4549 if (keys == NULL) goto failed;
4550 for (i = 0; i < len; i++) {
4551 expr_ty value;
4552 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4553 if (res != 0) goto failed;
4554 asdl_seq_SET(keys, i, value);
4555 }
4556 Py_XDECREF(tmp);
4557 tmp = NULL;
4558 } else {
4559 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4560 return 1;
4561 }
4562 if (PyObject_HasAttrString(obj, "values")) {
4563 int res;
4564 Py_ssize_t len;
4565 Py_ssize_t i;
4566 tmp = PyObject_GetAttrString(obj, "values");
4567 if (tmp == NULL) goto failed;
4568 if (!PyList_Check(tmp)) {
4569 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4570 goto failed;
4571 }
4572 len = PyList_GET_SIZE(tmp);
4573 values = asdl_seq_new(len, arena);
4574 if (values == NULL) goto failed;
4575 for (i = 0; i < len; i++) {
4576 expr_ty value;
4577 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4578 if (res != 0) goto failed;
4579 asdl_seq_SET(values, i, value);
4580 }
4581 Py_XDECREF(tmp);
4582 tmp = NULL;
4583 } else {
4584 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4585 return 1;
4586 }
4587 *out = Dict(keys, values, lineno, col_offset, arena);
4588 if (*out == NULL) goto failed;
4589 return 0;
4590 }
4591 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4592 expr_ty elt;
4593 asdl_seq* generators;
4594
4595 if (PyObject_HasAttrString(obj, "elt")) {
4596 int res;
4597 tmp = PyObject_GetAttrString(obj, "elt");
4598 if (tmp == NULL) goto failed;
4599 res = obj2ast_expr(tmp, &elt, arena);
4600 if (res != 0) goto failed;
4601 Py_XDECREF(tmp);
4602 tmp = NULL;
4603 } else {
4604 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4605 return 1;
4606 }
4607 if (PyObject_HasAttrString(obj, "generators")) {
4608 int res;
4609 Py_ssize_t len;
4610 Py_ssize_t i;
4611 tmp = PyObject_GetAttrString(obj, "generators");
4612 if (tmp == NULL) goto failed;
4613 if (!PyList_Check(tmp)) {
4614 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4615 goto failed;
4616 }
4617 len = PyList_GET_SIZE(tmp);
4618 generators = asdl_seq_new(len, arena);
4619 if (generators == NULL) goto failed;
4620 for (i = 0; i < len; i++) {
4621 comprehension_ty value;
4622 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4623 if (res != 0) goto failed;
4624 asdl_seq_SET(generators, i, value);
4625 }
4626 Py_XDECREF(tmp);
4627 tmp = NULL;
4628 } else {
4629 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4630 return 1;
4631 }
4632 *out = ListComp(elt, generators, lineno, col_offset, arena);
4633 if (*out == NULL) goto failed;
4634 return 0;
4635 }
4636 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4637 expr_ty elt;
4638 asdl_seq* generators;
4639
4640 if (PyObject_HasAttrString(obj, "elt")) {
4641 int res;
4642 tmp = PyObject_GetAttrString(obj, "elt");
4643 if (tmp == NULL) goto failed;
4644 res = obj2ast_expr(tmp, &elt, arena);
4645 if (res != 0) goto failed;
4646 Py_XDECREF(tmp);
4647 tmp = NULL;
4648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4650 return 1;
4651 }
4652 if (PyObject_HasAttrString(obj, "generators")) {
4653 int res;
4654 Py_ssize_t len;
4655 Py_ssize_t i;
4656 tmp = PyObject_GetAttrString(obj, "generators");
4657 if (tmp == NULL) goto failed;
4658 if (!PyList_Check(tmp)) {
4659 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4660 goto failed;
4661 }
4662 len = PyList_GET_SIZE(tmp);
4663 generators = asdl_seq_new(len, arena);
4664 if (generators == NULL) goto failed;
4665 for (i = 0; i < len; i++) {
4666 comprehension_ty value;
4667 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4668 if (res != 0) goto failed;
4669 asdl_seq_SET(generators, i, value);
4670 }
4671 Py_XDECREF(tmp);
4672 tmp = NULL;
4673 } else {
4674 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4675 return 1;
4676 }
4677 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4678 if (*out == NULL) goto failed;
4679 return 0;
4680 }
4681 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4682 expr_ty value;
4683
4684 if (PyObject_HasAttrString(obj, "value")) {
4685 int res;
4686 tmp = PyObject_GetAttrString(obj, "value");
4687 if (tmp == NULL) goto failed;
4688 res = obj2ast_expr(tmp, &value, arena);
4689 if (res != 0) goto failed;
4690 Py_XDECREF(tmp);
4691 tmp = NULL;
4692 } else {
4693 value = NULL;
4694 }
4695 *out = Yield(value, lineno, col_offset, arena);
4696 if (*out == NULL) goto failed;
4697 return 0;
4698 }
4699 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4700 expr_ty left;
4701 asdl_int_seq* ops;
4702 asdl_seq* comparators;
4703
4704 if (PyObject_HasAttrString(obj, "left")) {
4705 int res;
4706 tmp = PyObject_GetAttrString(obj, "left");
4707 if (tmp == NULL) goto failed;
4708 res = obj2ast_expr(tmp, &left, arena);
4709 if (res != 0) goto failed;
4710 Py_XDECREF(tmp);
4711 tmp = NULL;
4712 } else {
4713 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4714 return 1;
4715 }
4716 if (PyObject_HasAttrString(obj, "ops")) {
4717 int res;
4718 Py_ssize_t len;
4719 Py_ssize_t i;
4720 tmp = PyObject_GetAttrString(obj, "ops");
4721 if (tmp == NULL) goto failed;
4722 if (!PyList_Check(tmp)) {
4723 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4724 goto failed;
4725 }
4726 len = PyList_GET_SIZE(tmp);
4727 ops = asdl_int_seq_new(len, arena);
4728 if (ops == NULL) goto failed;
4729 for (i = 0; i < len; i++) {
4730 cmpop_ty value;
4731 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4732 if (res != 0) goto failed;
4733 asdl_seq_SET(ops, i, value);
4734 }
4735 Py_XDECREF(tmp);
4736 tmp = NULL;
4737 } else {
4738 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4739 return 1;
4740 }
4741 if (PyObject_HasAttrString(obj, "comparators")) {
4742 int res;
4743 Py_ssize_t len;
4744 Py_ssize_t i;
4745 tmp = PyObject_GetAttrString(obj, "comparators");
4746 if (tmp == NULL) goto failed;
4747 if (!PyList_Check(tmp)) {
4748 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4749 goto failed;
4750 }
4751 len = PyList_GET_SIZE(tmp);
4752 comparators = asdl_seq_new(len, arena);
4753 if (comparators == NULL) goto failed;
4754 for (i = 0; i < len; i++) {
4755 expr_ty value;
4756 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4757 if (res != 0) goto failed;
4758 asdl_seq_SET(comparators, i, value);
4759 }
4760 Py_XDECREF(tmp);
4761 tmp = NULL;
4762 } else {
4763 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4764 return 1;
4765 }
4766 *out = Compare(left, ops, comparators, lineno, col_offset,
4767 arena);
4768 if (*out == NULL) goto failed;
4769 return 0;
4770 }
4771 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4772 expr_ty func;
4773 asdl_seq* args;
4774 asdl_seq* keywords;
4775 expr_ty starargs;
4776 expr_ty kwargs;
4777
4778 if (PyObject_HasAttrString(obj, "func")) {
4779 int res;
4780 tmp = PyObject_GetAttrString(obj, "func");
4781 if (tmp == NULL) goto failed;
4782 res = obj2ast_expr(tmp, &func, arena);
4783 if (res != 0) goto failed;
4784 Py_XDECREF(tmp);
4785 tmp = NULL;
4786 } else {
4787 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4788 return 1;
4789 }
4790 if (PyObject_HasAttrString(obj, "args")) {
4791 int res;
4792 Py_ssize_t len;
4793 Py_ssize_t i;
4794 tmp = PyObject_GetAttrString(obj, "args");
4795 if (tmp == NULL) goto failed;
4796 if (!PyList_Check(tmp)) {
4797 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4798 goto failed;
4799 }
4800 len = PyList_GET_SIZE(tmp);
4801 args = asdl_seq_new(len, arena);
4802 if (args == NULL) goto failed;
4803 for (i = 0; i < len; i++) {
4804 expr_ty value;
4805 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4806 if (res != 0) goto failed;
4807 asdl_seq_SET(args, i, value);
4808 }
4809 Py_XDECREF(tmp);
4810 tmp = NULL;
4811 } else {
4812 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4813 return 1;
4814 }
4815 if (PyObject_HasAttrString(obj, "keywords")) {
4816 int res;
4817 Py_ssize_t len;
4818 Py_ssize_t i;
4819 tmp = PyObject_GetAttrString(obj, "keywords");
4820 if (tmp == NULL) goto failed;
4821 if (!PyList_Check(tmp)) {
4822 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4823 goto failed;
4824 }
4825 len = PyList_GET_SIZE(tmp);
4826 keywords = asdl_seq_new(len, arena);
4827 if (keywords == NULL) goto failed;
4828 for (i = 0; i < len; i++) {
4829 keyword_ty value;
4830 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4831 if (res != 0) goto failed;
4832 asdl_seq_SET(keywords, i, value);
4833 }
4834 Py_XDECREF(tmp);
4835 tmp = NULL;
4836 } else {
4837 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4838 return 1;
4839 }
4840 if (PyObject_HasAttrString(obj, "starargs")) {
4841 int res;
4842 tmp = PyObject_GetAttrString(obj, "starargs");
4843 if (tmp == NULL) goto failed;
4844 res = obj2ast_expr(tmp, &starargs, arena);
4845 if (res != 0) goto failed;
4846 Py_XDECREF(tmp);
4847 tmp = NULL;
4848 } else {
4849 starargs = NULL;
4850 }
4851 if (PyObject_HasAttrString(obj, "kwargs")) {
4852 int res;
4853 tmp = PyObject_GetAttrString(obj, "kwargs");
4854 if (tmp == NULL) goto failed;
4855 res = obj2ast_expr(tmp, &kwargs, arena);
4856 if (res != 0) goto failed;
4857 Py_XDECREF(tmp);
4858 tmp = NULL;
4859 } else {
4860 kwargs = NULL;
4861 }
4862 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4863 col_offset, arena);
4864 if (*out == NULL) goto failed;
4865 return 0;
4866 }
4867 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4868 expr_ty value;
4869
4870 if (PyObject_HasAttrString(obj, "value")) {
4871 int res;
4872 tmp = PyObject_GetAttrString(obj, "value");
4873 if (tmp == NULL) goto failed;
4874 res = obj2ast_expr(tmp, &value, arena);
4875 if (res != 0) goto failed;
4876 Py_XDECREF(tmp);
4877 tmp = NULL;
4878 } else {
4879 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
4880 return 1;
4881 }
4882 *out = Repr(value, lineno, col_offset, arena);
4883 if (*out == NULL) goto failed;
4884 return 0;
4885 }
4886 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
4887 object n;
4888
4889 if (PyObject_HasAttrString(obj, "n")) {
4890 int res;
4891 tmp = PyObject_GetAttrString(obj, "n");
4892 if (tmp == NULL) goto failed;
4893 res = obj2ast_object(tmp, &n, arena);
4894 if (res != 0) goto failed;
4895 Py_XDECREF(tmp);
4896 tmp = NULL;
4897 } else {
4898 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
4899 return 1;
4900 }
4901 *out = Num(n, lineno, col_offset, arena);
4902 if (*out == NULL) goto failed;
4903 return 0;
4904 }
4905 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
4906 string s;
4907
4908 if (PyObject_HasAttrString(obj, "s")) {
4909 int res;
4910 tmp = PyObject_GetAttrString(obj, "s");
4911 if (tmp == NULL) goto failed;
4912 res = obj2ast_string(tmp, &s, arena);
4913 if (res != 0) goto failed;
4914 Py_XDECREF(tmp);
4915 tmp = NULL;
4916 } else {
4917 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
4918 return 1;
4919 }
4920 *out = Str(s, lineno, col_offset, arena);
4921 if (*out == NULL) goto failed;
4922 return 0;
4923 }
4924 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
4925 expr_ty value;
4926 identifier attr;
4927 expr_context_ty ctx;
4928
4929 if (PyObject_HasAttrString(obj, "value")) {
4930 int res;
4931 tmp = PyObject_GetAttrString(obj, "value");
4932 if (tmp == NULL) goto failed;
4933 res = obj2ast_expr(tmp, &value, arena);
4934 if (res != 0) goto failed;
4935 Py_XDECREF(tmp);
4936 tmp = NULL;
4937 } else {
4938 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
4939 return 1;
4940 }
4941 if (PyObject_HasAttrString(obj, "attr")) {
4942 int res;
4943 tmp = PyObject_GetAttrString(obj, "attr");
4944 if (tmp == NULL) goto failed;
4945 res = obj2ast_identifier(tmp, &attr, arena);
4946 if (res != 0) goto failed;
4947 Py_XDECREF(tmp);
4948 tmp = NULL;
4949 } else {
4950 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
4951 return 1;
4952 }
4953 if (PyObject_HasAttrString(obj, "ctx")) {
4954 int res;
4955 tmp = PyObject_GetAttrString(obj, "ctx");
4956 if (tmp == NULL) goto failed;
4957 res = obj2ast_expr_context(tmp, &ctx, arena);
4958 if (res != 0) goto failed;
4959 Py_XDECREF(tmp);
4960 tmp = NULL;
4961 } else {
4962 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
4963 return 1;
4964 }
4965 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
4966 if (*out == NULL) goto failed;
4967 return 0;
4968 }
4969 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
4970 expr_ty value;
4971 slice_ty slice;
4972 expr_context_ty ctx;
4973
4974 if (PyObject_HasAttrString(obj, "value")) {
4975 int res;
4976 tmp = PyObject_GetAttrString(obj, "value");
4977 if (tmp == NULL) goto failed;
4978 res = obj2ast_expr(tmp, &value, arena);
4979 if (res != 0) goto failed;
4980 Py_XDECREF(tmp);
4981 tmp = NULL;
4982 } else {
4983 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
4984 return 1;
4985 }
4986 if (PyObject_HasAttrString(obj, "slice")) {
4987 int res;
4988 tmp = PyObject_GetAttrString(obj, "slice");
4989 if (tmp == NULL) goto failed;
4990 res = obj2ast_slice(tmp, &slice, arena);
4991 if (res != 0) goto failed;
4992 Py_XDECREF(tmp);
4993 tmp = NULL;
4994 } else {
4995 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
4996 return 1;
4997 }
4998 if (PyObject_HasAttrString(obj, "ctx")) {
4999 int res;
5000 tmp = PyObject_GetAttrString(obj, "ctx");
5001 if (tmp == NULL) goto failed;
5002 res = obj2ast_expr_context(tmp, &ctx, arena);
5003 if (res != 0) goto failed;
5004 Py_XDECREF(tmp);
5005 tmp = NULL;
5006 } else {
5007 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5008 return 1;
5009 }
5010 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5011 if (*out == NULL) goto failed;
5012 return 0;
5013 }
5014 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5015 identifier id;
5016 expr_context_ty ctx;
5017
5018 if (PyObject_HasAttrString(obj, "id")) {
5019 int res;
5020 tmp = PyObject_GetAttrString(obj, "id");
5021 if (tmp == NULL) goto failed;
5022 res = obj2ast_identifier(tmp, &id, arena);
5023 if (res != 0) goto failed;
5024 Py_XDECREF(tmp);
5025 tmp = NULL;
5026 } else {
5027 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5028 return 1;
5029 }
5030 if (PyObject_HasAttrString(obj, "ctx")) {
5031 int res;
5032 tmp = PyObject_GetAttrString(obj, "ctx");
5033 if (tmp == NULL) goto failed;
5034 res = obj2ast_expr_context(tmp, &ctx, arena);
5035 if (res != 0) goto failed;
5036 Py_XDECREF(tmp);
5037 tmp = NULL;
5038 } else {
5039 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5040 return 1;
5041 }
5042 *out = Name(id, ctx, lineno, col_offset, arena);
5043 if (*out == NULL) goto failed;
5044 return 0;
5045 }
5046 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5047 asdl_seq* elts;
5048 expr_context_ty ctx;
5049
5050 if (PyObject_HasAttrString(obj, "elts")) {
5051 int res;
5052 Py_ssize_t len;
5053 Py_ssize_t i;
5054 tmp = PyObject_GetAttrString(obj, "elts");
5055 if (tmp == NULL) goto failed;
5056 if (!PyList_Check(tmp)) {
5057 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5058 goto failed;
5059 }
5060 len = PyList_GET_SIZE(tmp);
5061 elts = asdl_seq_new(len, arena);
5062 if (elts == NULL) goto failed;
5063 for (i = 0; i < len; i++) {
5064 expr_ty value;
5065 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5066 if (res != 0) goto failed;
5067 asdl_seq_SET(elts, i, value);
5068 }
5069 Py_XDECREF(tmp);
5070 tmp = NULL;
5071 } else {
5072 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5073 return 1;
5074 }
5075 if (PyObject_HasAttrString(obj, "ctx")) {
5076 int res;
5077 tmp = PyObject_GetAttrString(obj, "ctx");
5078 if (tmp == NULL) goto failed;
5079 res = obj2ast_expr_context(tmp, &ctx, arena);
5080 if (res != 0) goto failed;
5081 Py_XDECREF(tmp);
5082 tmp = NULL;
5083 } else {
5084 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5085 return 1;
5086 }
5087 *out = List(elts, ctx, lineno, col_offset, arena);
5088 if (*out == NULL) goto failed;
5089 return 0;
5090 }
5091 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5092 asdl_seq* elts;
5093 expr_context_ty ctx;
5094
5095 if (PyObject_HasAttrString(obj, "elts")) {
5096 int res;
5097 Py_ssize_t len;
5098 Py_ssize_t i;
5099 tmp = PyObject_GetAttrString(obj, "elts");
5100 if (tmp == NULL) goto failed;
5101 if (!PyList_Check(tmp)) {
5102 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5103 goto failed;
5104 }
5105 len = PyList_GET_SIZE(tmp);
5106 elts = asdl_seq_new(len, arena);
5107 if (elts == NULL) goto failed;
5108 for (i = 0; i < len; i++) {
5109 expr_ty value;
5110 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5111 if (res != 0) goto failed;
5112 asdl_seq_SET(elts, i, value);
5113 }
5114 Py_XDECREF(tmp);
5115 tmp = NULL;
5116 } else {
5117 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5118 return 1;
5119 }
5120 if (PyObject_HasAttrString(obj, "ctx")) {
5121 int res;
5122 tmp = PyObject_GetAttrString(obj, "ctx");
5123 if (tmp == NULL) goto failed;
5124 res = obj2ast_expr_context(tmp, &ctx, arena);
5125 if (res != 0) goto failed;
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5130 return 1;
5131 }
5132 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5133 if (*out == NULL) goto failed;
5134 return 0;
5135 }
5136
5137 tmp = PyObject_Repr(obj);
5138 if (tmp == NULL) goto failed;
5139 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5140 failed:
5141 Py_XDECREF(tmp);
5142 return 1;
5143 }
5144
5145 int
5146 obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5147 {
5148 PyObject* tmp = NULL;
5149
5150 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5151 *out = Load;
5152 return 0;
5153 }
5154 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5155 *out = Store;
5156 return 0;
5157 }
5158 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5159 *out = Del;
5160 return 0;
5161 }
5162 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5163 *out = AugLoad;
5164 return 0;
5165 }
5166 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5167 *out = AugStore;
5168 return 0;
5169 }
5170 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5171 *out = Param;
5172 return 0;
5173 }
5174
5175 tmp = PyObject_Repr(obj);
5176 if (tmp == NULL) goto failed;
5177 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5178 failed:
5179 Py_XDECREF(tmp);
5180 return 1;
5181 }
5182
5183 int
5184 obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5185 {
5186 PyObject* tmp = NULL;
5187
5188
5189 if (obj == Py_None) {
5190 *out = NULL;
5191 return 0;
5192 }
5193 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5194
5195 *out = Ellipsis(arena);
5196 if (*out == NULL) goto failed;
5197 return 0;
5198 }
5199 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5200 expr_ty lower;
5201 expr_ty upper;
5202 expr_ty step;
5203
5204 if (PyObject_HasAttrString(obj, "lower")) {
5205 int res;
5206 tmp = PyObject_GetAttrString(obj, "lower");
5207 if (tmp == NULL) goto failed;
5208 res = obj2ast_expr(tmp, &lower, arena);
5209 if (res != 0) goto failed;
5210 Py_XDECREF(tmp);
5211 tmp = NULL;
5212 } else {
5213 lower = NULL;
5214 }
5215 if (PyObject_HasAttrString(obj, "upper")) {
5216 int res;
5217 tmp = PyObject_GetAttrString(obj, "upper");
5218 if (tmp == NULL) goto failed;
5219 res = obj2ast_expr(tmp, &upper, arena);
5220 if (res != 0) goto failed;
5221 Py_XDECREF(tmp);
5222 tmp = NULL;
5223 } else {
5224 upper = NULL;
5225 }
5226 if (PyObject_HasAttrString(obj, "step")) {
5227 int res;
5228 tmp = PyObject_GetAttrString(obj, "step");
5229 if (tmp == NULL) goto failed;
5230 res = obj2ast_expr(tmp, &step, arena);
5231 if (res != 0) goto failed;
5232 Py_XDECREF(tmp);
5233 tmp = NULL;
5234 } else {
5235 step = NULL;
5236 }
5237 *out = Slice(lower, upper, step, arena);
5238 if (*out == NULL) goto failed;
5239 return 0;
5240 }
5241 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5242 asdl_seq* dims;
5243
5244 if (PyObject_HasAttrString(obj, "dims")) {
5245 int res;
5246 Py_ssize_t len;
5247 Py_ssize_t i;
5248 tmp = PyObject_GetAttrString(obj, "dims");
5249 if (tmp == NULL) goto failed;
5250 if (!PyList_Check(tmp)) {
5251 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5252 goto failed;
5253 }
5254 len = PyList_GET_SIZE(tmp);
5255 dims = asdl_seq_new(len, arena);
5256 if (dims == NULL) goto failed;
5257 for (i = 0; i < len; i++) {
5258 slice_ty value;
5259 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5260 if (res != 0) goto failed;
5261 asdl_seq_SET(dims, i, value);
5262 }
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5267 return 1;
5268 }
5269 *out = ExtSlice(dims, arena);
5270 if (*out == NULL) goto failed;
5271 return 0;
5272 }
5273 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5274 expr_ty value;
5275
5276 if (PyObject_HasAttrString(obj, "value")) {
5277 int res;
5278 tmp = PyObject_GetAttrString(obj, "value");
5279 if (tmp == NULL) goto failed;
5280 res = obj2ast_expr(tmp, &value, arena);
5281 if (res != 0) goto failed;
5282 Py_XDECREF(tmp);
5283 tmp = NULL;
5284 } else {
5285 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5286 return 1;
5287 }
5288 *out = Index(value, arena);
5289 if (*out == NULL) goto failed;
5290 return 0;
5291 }
5292
5293 tmp = PyObject_Repr(obj);
5294 if (tmp == NULL) goto failed;
5295 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5296 failed:
5297 Py_XDECREF(tmp);
5298 return 1;
5299 }
5300
5301 int
5302 obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5303 {
5304 PyObject* tmp = NULL;
5305
5306 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5307 *out = And;
5308 return 0;
5309 }
5310 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5311 *out = Or;
5312 return 0;
5313 }
5314
5315 tmp = PyObject_Repr(obj);
5316 if (tmp == NULL) goto failed;
5317 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5318 failed:
5319 Py_XDECREF(tmp);
5320 return 1;
5321 }
5322
5323 int
5324 obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5325 {
5326 PyObject* tmp = NULL;
5327
5328 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5329 *out = Add;
5330 return 0;
5331 }
5332 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5333 *out = Sub;
5334 return 0;
5335 }
5336 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5337 *out = Mult;
5338 return 0;
5339 }
5340 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5341 *out = Div;
5342 return 0;
5343 }
5344 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5345 *out = Mod;
5346 return 0;
5347 }
5348 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5349 *out = Pow;
5350 return 0;
5351 }
5352 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5353 *out = LShift;
5354 return 0;
5355 }
5356 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5357 *out = RShift;
5358 return 0;
5359 }
5360 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5361 *out = BitOr;
5362 return 0;
5363 }
5364 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5365 *out = BitXor;
5366 return 0;
5367 }
5368 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5369 *out = BitAnd;
5370 return 0;
5371 }
5372 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5373 *out = FloorDiv;
5374 return 0;
5375 }
5376
5377 tmp = PyObject_Repr(obj);
5378 if (tmp == NULL) goto failed;
5379 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5380 failed:
5381 Py_XDECREF(tmp);
5382 return 1;
5383 }
5384
5385 int
5386 obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5387 {
5388 PyObject* tmp = NULL;
5389
5390 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5391 *out = Invert;
5392 return 0;
5393 }
5394 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5395 *out = Not;
5396 return 0;
5397 }
5398 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5399 *out = UAdd;
5400 return 0;
5401 }
5402 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5403 *out = USub;
5404 return 0;
5405 }
5406
5407 tmp = PyObject_Repr(obj);
5408 if (tmp == NULL) goto failed;
5409 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5410 failed:
5411 Py_XDECREF(tmp);
5412 return 1;
5413 }
5414
5415 int
5416 obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5417 {
5418 PyObject* tmp = NULL;
5419
5420 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5421 *out = Eq;
5422 return 0;
5423 }
5424 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5425 *out = NotEq;
5426 return 0;
5427 }
5428 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5429 *out = Lt;
5430 return 0;
5431 }
5432 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5433 *out = LtE;
5434 return 0;
5435 }
5436 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5437 *out = Gt;
5438 return 0;
5439 }
5440 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5441 *out = GtE;
5442 return 0;
5443 }
5444 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5445 *out = Is;
5446 return 0;
5447 }
5448 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5449 *out = IsNot;
5450 return 0;
5451 }
5452 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5453 *out = In;
5454 return 0;
5455 }
5456 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5457 *out = NotIn;
5458 return 0;
5459 }
5460
5461 tmp = PyObject_Repr(obj);
5462 if (tmp == NULL) goto failed;
5463 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5464 failed:
5465 Py_XDECREF(tmp);
5466 return 1;
5467 }
5468
5469 int
5470 obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5471 {
5472 PyObject* tmp = NULL;
5473 expr_ty target;
5474 expr_ty iter;
5475 asdl_seq* ifs;
5476
5477 if (PyObject_HasAttrString(obj, "target")) {
5478 int res;
5479 tmp = PyObject_GetAttrString(obj, "target");
5480 if (tmp == NULL) goto failed;
5481 res = obj2ast_expr(tmp, &target, arena);
5482 if (res != 0) goto failed;
5483 Py_XDECREF(tmp);
5484 tmp = NULL;
5485 } else {
5486 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5487 return 1;
5488 }
5489 if (PyObject_HasAttrString(obj, "iter")) {
5490 int res;
5491 tmp = PyObject_GetAttrString(obj, "iter");
5492 if (tmp == NULL) goto failed;
5493 res = obj2ast_expr(tmp, &iter, arena);
5494 if (res != 0) goto failed;
5495 Py_XDECREF(tmp);
5496 tmp = NULL;
5497 } else {
5498 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5499 return 1;
5500 }
5501 if (PyObject_HasAttrString(obj, "ifs")) {
5502 int res;
5503 Py_ssize_t len;
5504 Py_ssize_t i;
5505 tmp = PyObject_GetAttrString(obj, "ifs");
5506 if (tmp == NULL) goto failed;
5507 if (!PyList_Check(tmp)) {
5508 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5509 goto failed;
5510 }
5511 len = PyList_GET_SIZE(tmp);
5512 ifs = asdl_seq_new(len, arena);
5513 if (ifs == NULL) goto failed;
5514 for (i = 0; i < len; i++) {
5515 expr_ty value;
5516 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5517 if (res != 0) goto failed;
5518 asdl_seq_SET(ifs, i, value);
5519 }
5520 Py_XDECREF(tmp);
5521 tmp = NULL;
5522 } else {
5523 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5524 return 1;
5525 }
5526 *out = comprehension(target, iter, ifs, arena);
5527 return 0;
5528 failed:
5529 Py_XDECREF(tmp);
5530 return 1;
5531 }
5532
5533 int
5534 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5535 {
5536 PyObject* tmp = NULL;
5537 expr_ty type;
5538 expr_ty name;
5539 asdl_seq* body;
5540 int lineno;
5541 int col_offset;
5542
5543 if (PyObject_HasAttrString(obj, "type")) {
5544 int res;
5545 tmp = PyObject_GetAttrString(obj, "type");
5546 if (tmp == NULL) goto failed;
5547 res = obj2ast_expr(tmp, &type, arena);
5548 if (res != 0) goto failed;
5549 Py_XDECREF(tmp);
5550 tmp = NULL;
5551 } else {
5552 type = NULL;
5553 }
5554 if (PyObject_HasAttrString(obj, "name")) {
5555 int res;
5556 tmp = PyObject_GetAttrString(obj, "name");
5557 if (tmp == NULL) goto failed;
5558 res = obj2ast_expr(tmp, &name, arena);
5559 if (res != 0) goto failed;
5560 Py_XDECREF(tmp);
5561 tmp = NULL;
5562 } else {
5563 name = NULL;
5564 }
5565 if (PyObject_HasAttrString(obj, "body")) {
5566 int res;
5567 Py_ssize_t len;
5568 Py_ssize_t i;
5569 tmp = PyObject_GetAttrString(obj, "body");
5570 if (tmp == NULL) goto failed;
5571 if (!PyList_Check(tmp)) {
5572 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5573 goto failed;
5574 }
5575 len = PyList_GET_SIZE(tmp);
5576 body = asdl_seq_new(len, arena);
5577 if (body == NULL) goto failed;
5578 for (i = 0; i < len; i++) {
5579 stmt_ty value;
5580 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5581 if (res != 0) goto failed;
5582 asdl_seq_SET(body, i, value);
5583 }
5584 Py_XDECREF(tmp);
5585 tmp = NULL;
5586 } else {
5587 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
5588 return 1;
5589 }
5590 if (PyObject_HasAttrString(obj, "lineno")) {
5591 int res;
5592 tmp = PyObject_GetAttrString(obj, "lineno");
5593 if (tmp == NULL) goto failed;
5594 res = obj2ast_int(tmp, &lineno, arena);
5595 if (res != 0) goto failed;
5596 Py_XDECREF(tmp);
5597 tmp = NULL;
5598 } else {
5599 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5600 return 1;
5601 }
5602 if (PyObject_HasAttrString(obj, "col_offset")) {
5603 int res;
5604 tmp = PyObject_GetAttrString(obj, "col_offset");
5605 if (tmp == NULL) goto failed;
5606 res = obj2ast_int(tmp, &col_offset, arena);
5607 if (res != 0) goto failed;
5608 Py_XDECREF(tmp);
5609 tmp = NULL;
5610 } else {
5611 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5612 return 1;
5613 }
5614 *out = excepthandler(type, name, body, lineno, col_offset, arena);
5615 return 0;
5616 failed:
5617 Py_XDECREF(tmp);
5618 return 1;
5619 }
5620
5621 int
5622 obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5623 {
5624 PyObject* tmp = NULL;
5625 asdl_seq* args;
5626 identifier vararg;
5627 identifier kwarg;
5628 asdl_seq* defaults;
5629
5630 if (PyObject_HasAttrString(obj, "args")) {
5631 int res;
5632 Py_ssize_t len;
5633 Py_ssize_t i;
5634 tmp = PyObject_GetAttrString(obj, "args");
5635 if (tmp == NULL) goto failed;
5636 if (!PyList_Check(tmp)) {
5637 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5638 goto failed;
5639 }
5640 len = PyList_GET_SIZE(tmp);
5641 args = asdl_seq_new(len, arena);
5642 if (args == NULL) goto failed;
5643 for (i = 0; i < len; i++) {
5644 expr_ty value;
5645 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5646 if (res != 0) goto failed;
5647 asdl_seq_SET(args, i, value);
5648 }
5649 Py_XDECREF(tmp);
5650 tmp = NULL;
5651 } else {
5652 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5653 return 1;
5654 }
5655 if (PyObject_HasAttrString(obj, "vararg")) {
5656 int res;
5657 tmp = PyObject_GetAttrString(obj, "vararg");
5658 if (tmp == NULL) goto failed;
5659 res = obj2ast_identifier(tmp, &vararg, arena);
5660 if (res != 0) goto failed;
5661 Py_XDECREF(tmp);
5662 tmp = NULL;
5663 } else {
5664 vararg = NULL;
5665 }
5666 if (PyObject_HasAttrString(obj, "kwarg")) {
5667 int res;
5668 tmp = PyObject_GetAttrString(obj, "kwarg");
5669 if (tmp == NULL) goto failed;
5670 res = obj2ast_identifier(tmp, &kwarg, arena);
5671 if (res != 0) goto failed;
5672 Py_XDECREF(tmp);
5673 tmp = NULL;
5674 } else {
5675 kwarg = NULL;
5676 }
5677 if (PyObject_HasAttrString(obj, "defaults")) {
5678 int res;
5679 Py_ssize_t len;
5680 Py_ssize_t i;
5681 tmp = PyObject_GetAttrString(obj, "defaults");
5682 if (tmp == NULL) goto failed;
5683 if (!PyList_Check(tmp)) {
5684 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5685 goto failed;
5686 }
5687 len = PyList_GET_SIZE(tmp);
5688 defaults = asdl_seq_new(len, arena);
5689 if (defaults == NULL) goto failed;
5690 for (i = 0; i < len; i++) {
5691 expr_ty value;
5692 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5693 if (res != 0) goto failed;
5694 asdl_seq_SET(defaults, i, value);
5695 }
5696 Py_XDECREF(tmp);
5697 tmp = NULL;
5698 } else {
5699 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5700 return 1;
5701 }
5702 *out = arguments(args, vararg, kwarg, defaults, arena);
5703 return 0;
5704 failed:
5705 Py_XDECREF(tmp);
5706 return 1;
5707 }
5708
5709 int
5710 obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5711 {
5712 PyObject* tmp = NULL;
5713 identifier arg;
5714 expr_ty value;
5715
5716 if (PyObject_HasAttrString(obj, "arg")) {
5717 int res;
5718 tmp = PyObject_GetAttrString(obj, "arg");
5719 if (tmp == NULL) goto failed;
5720 res = obj2ast_identifier(tmp, &arg, arena);
5721 if (res != 0) goto failed;
5722 Py_XDECREF(tmp);
5723 tmp = NULL;
5724 } else {
5725 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5726 return 1;
5727 }
5728 if (PyObject_HasAttrString(obj, "value")) {
5729 int res;
5730 tmp = PyObject_GetAttrString(obj, "value");
5731 if (tmp == NULL) goto failed;
5732 res = obj2ast_expr(tmp, &value, arena);
5733 if (res != 0) goto failed;
5734 Py_XDECREF(tmp);
5735 tmp = NULL;
5736 } else {
5737 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5738 return 1;
5739 }
5740 *out = keyword(arg, value, arena);
5741 return 0;
5742 failed:
5743 Py_XDECREF(tmp);
5744 return 1;
5745 }
5746
5747 int
5748 obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5749 {
5750 PyObject* tmp = NULL;
5751 identifier name;
5752 identifier asname;
5753
5754 if (PyObject_HasAttrString(obj, "name")) {
5755 int res;
5756 tmp = PyObject_GetAttrString(obj, "name");
5757 if (tmp == NULL) goto failed;
5758 res = obj2ast_identifier(tmp, &name, arena);
5759 if (res != 0) goto failed;
5760 Py_XDECREF(tmp);
5761 tmp = NULL;
5762 } else {
5763 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5764 return 1;
5765 }
5766 if (PyObject_HasAttrString(obj, "asname")) {
5767 int res;
5768 tmp = PyObject_GetAttrString(obj, "asname");
5769 if (tmp == NULL) goto failed;
5770 res = obj2ast_identifier(tmp, &asname, arena);
5771 if (res != 0) goto failed;
5772 Py_XDECREF(tmp);
5773 tmp = NULL;
5774 } else {
5775 asname = NULL;
5776 }
5777 *out = alias(name, asname, arena);
5778 return 0;
5779 failed:
5780 Py_XDECREF(tmp);
5781 return 1;
2947 } 5782 }
2948 5783
2949 5784
2950 PyMODINIT_FUNC 5785 PyMODINIT_FUNC
2951 init_ast(void) 5786 init_ast(void)
3107 { 5942 {
3108 init_types(); 5943 init_types();
3109 return ast2obj_mod(t); 5944 return ast2obj_mod(t);
3110 } 5945 }
3111 5946
3112 5947 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
5948 {
5949 mod_ty res;
5950 init_types();
5951 if (!PyObject_IsInstance(ast, mod_type)) {
5952 PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
5953 "or Expression node");
5954 return NULL;
5955 }
5956 if (obj2ast_mod(ast, &res, arena) != 0)
5957 return NULL;
5958 else
5959 return res;
5960 }
5961
5962 int PyAST_Check(PyObject* obj)
5963 {
5964 init_types();
5965 return PyObject_IsInstance(obj, (PyObject*)AST_type);
5966 }
5967
5968