Hum, test skipping when the URL isn't reachable hadn't been applied to trunk.
[python.git] / Python / Python-ast.c
blob9ff3d8aa9f980ed5c5b8721c57faf74796d26908
1 /* File automatically generated by Parser/asdl_c.py. */
4 /*
5 __version__ 73421.
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
12 #include "Python.h"
13 #include "Python-ast.h"
15 static PyTypeObject AST_type;
16 static PyTypeObject *mod_type;
17 static PyObject* ast2obj_mod(void*);
18 static PyTypeObject *Module_type;
19 static char *Module_fields[]={
20 "body",
22 static PyTypeObject *Interactive_type;
23 static char *Interactive_fields[]={
24 "body",
26 static PyTypeObject *Expression_type;
27 static char *Expression_fields[]={
28 "body",
30 static PyTypeObject *Suite_type;
31 static char *Suite_fields[]={
32 "body",
34 static PyTypeObject *stmt_type;
35 static char *stmt_attributes[] = {
36 "lineno",
37 "col_offset",
39 static PyObject* ast2obj_stmt(void*);
40 static PyTypeObject *FunctionDef_type;
41 static char *FunctionDef_fields[]={
42 "name",
43 "args",
44 "body",
45 "decorator_list",
47 static PyTypeObject *ClassDef_type;
48 static char *ClassDef_fields[]={
49 "name",
50 "bases",
51 "body",
52 "decorator_list",
54 static PyTypeObject *Return_type;
55 static char *Return_fields[]={
56 "value",
58 static PyTypeObject *Delete_type;
59 static char *Delete_fields[]={
60 "targets",
62 static PyTypeObject *Assign_type;
63 static char *Assign_fields[]={
64 "targets",
65 "value",
67 static PyTypeObject *AugAssign_type;
68 static char *AugAssign_fields[]={
69 "target",
70 "op",
71 "value",
73 static PyTypeObject *Print_type;
74 static char *Print_fields[]={
75 "dest",
76 "values",
77 "nl",
79 static PyTypeObject *For_type;
80 static char *For_fields[]={
81 "target",
82 "iter",
83 "body",
84 "orelse",
86 static PyTypeObject *While_type;
87 static char *While_fields[]={
88 "test",
89 "body",
90 "orelse",
92 static PyTypeObject *If_type;
93 static char *If_fields[]={
94 "test",
95 "body",
96 "orelse",
98 static PyTypeObject *With_type;
99 static char *With_fields[]={
100 "context_expr",
101 "optional_vars",
102 "body",
104 static PyTypeObject *Raise_type;
105 static char *Raise_fields[]={
106 "type",
107 "inst",
108 "tback",
110 static PyTypeObject *TryExcept_type;
111 static char *TryExcept_fields[]={
112 "body",
113 "handlers",
114 "orelse",
116 static PyTypeObject *TryFinally_type;
117 static char *TryFinally_fields[]={
118 "body",
119 "finalbody",
121 static PyTypeObject *Assert_type;
122 static char *Assert_fields[]={
123 "test",
124 "msg",
126 static PyTypeObject *Import_type;
127 static char *Import_fields[]={
128 "names",
130 static PyTypeObject *ImportFrom_type;
131 static char *ImportFrom_fields[]={
132 "module",
133 "names",
134 "level",
136 static PyTypeObject *Exec_type;
137 static char *Exec_fields[]={
138 "body",
139 "globals",
140 "locals",
142 static PyTypeObject *Global_type;
143 static char *Global_fields[]={
144 "names",
146 static PyTypeObject *Expr_type;
147 static char *Expr_fields[]={
148 "value",
150 static PyTypeObject *Pass_type;
151 static PyTypeObject *Break_type;
152 static PyTypeObject *Continue_type;
153 static PyTypeObject *expr_type;
154 static char *expr_attributes[] = {
155 "lineno",
156 "col_offset",
158 static PyObject* ast2obj_expr(void*);
159 static PyTypeObject *BoolOp_type;
160 static char *BoolOp_fields[]={
161 "op",
162 "values",
164 static PyTypeObject *BinOp_type;
165 static char *BinOp_fields[]={
166 "left",
167 "op",
168 "right",
170 static PyTypeObject *UnaryOp_type;
171 static char *UnaryOp_fields[]={
172 "op",
173 "operand",
175 static PyTypeObject *Lambda_type;
176 static char *Lambda_fields[]={
177 "args",
178 "body",
180 static PyTypeObject *IfExp_type;
181 static char *IfExp_fields[]={
182 "test",
183 "body",
184 "orelse",
186 static PyTypeObject *Dict_type;
187 static char *Dict_fields[]={
188 "keys",
189 "values",
191 static PyTypeObject *ListComp_type;
192 static char *ListComp_fields[]={
193 "elt",
194 "generators",
196 static PyTypeObject *GeneratorExp_type;
197 static char *GeneratorExp_fields[]={
198 "elt",
199 "generators",
201 static PyTypeObject *Yield_type;
202 static char *Yield_fields[]={
203 "value",
205 static PyTypeObject *Compare_type;
206 static char *Compare_fields[]={
207 "left",
208 "ops",
209 "comparators",
211 static PyTypeObject *Call_type;
212 static char *Call_fields[]={
213 "func",
214 "args",
215 "keywords",
216 "starargs",
217 "kwargs",
219 static PyTypeObject *Repr_type;
220 static char *Repr_fields[]={
221 "value",
223 static PyTypeObject *Num_type;
224 static char *Num_fields[]={
225 "n",
227 static PyTypeObject *Str_type;
228 static char *Str_fields[]={
229 "s",
231 static PyTypeObject *Attribute_type;
232 static char *Attribute_fields[]={
233 "value",
234 "attr",
235 "ctx",
237 static PyTypeObject *Subscript_type;
238 static char *Subscript_fields[]={
239 "value",
240 "slice",
241 "ctx",
243 static PyTypeObject *Name_type;
244 static char *Name_fields[]={
245 "id",
246 "ctx",
248 static PyTypeObject *List_type;
249 static char *List_fields[]={
250 "elts",
251 "ctx",
253 static PyTypeObject *Tuple_type;
254 static char *Tuple_fields[]={
255 "elts",
256 "ctx",
258 static PyTypeObject *expr_context_type;
259 static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
260 *AugLoad_singleton, *AugStore_singleton, *Param_singleton;
261 static PyObject* ast2obj_expr_context(expr_context_ty);
262 static PyTypeObject *Load_type;
263 static PyTypeObject *Store_type;
264 static PyTypeObject *Del_type;
265 static PyTypeObject *AugLoad_type;
266 static PyTypeObject *AugStore_type;
267 static PyTypeObject *Param_type;
268 static PyTypeObject *slice_type;
269 static PyObject* ast2obj_slice(void*);
270 static PyTypeObject *Ellipsis_type;
271 static PyTypeObject *Slice_type;
272 static char *Slice_fields[]={
273 "lower",
274 "upper",
275 "step",
277 static PyTypeObject *ExtSlice_type;
278 static char *ExtSlice_fields[]={
279 "dims",
281 static PyTypeObject *Index_type;
282 static char *Index_fields[]={
283 "value",
285 static PyTypeObject *boolop_type;
286 static PyObject *And_singleton, *Or_singleton;
287 static PyObject* ast2obj_boolop(boolop_ty);
288 static PyTypeObject *And_type;
289 static PyTypeObject *Or_type;
290 static PyTypeObject *operator_type;
291 static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
292 *Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
293 *RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
294 *FloorDiv_singleton;
295 static PyObject* ast2obj_operator(operator_ty);
296 static PyTypeObject *Add_type;
297 static PyTypeObject *Sub_type;
298 static PyTypeObject *Mult_type;
299 static PyTypeObject *Div_type;
300 static PyTypeObject *Mod_type;
301 static PyTypeObject *Pow_type;
302 static PyTypeObject *LShift_type;
303 static PyTypeObject *RShift_type;
304 static PyTypeObject *BitOr_type;
305 static PyTypeObject *BitXor_type;
306 static PyTypeObject *BitAnd_type;
307 static PyTypeObject *FloorDiv_type;
308 static PyTypeObject *unaryop_type;
309 static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
310 *USub_singleton;
311 static PyObject* ast2obj_unaryop(unaryop_ty);
312 static PyTypeObject *Invert_type;
313 static PyTypeObject *Not_type;
314 static PyTypeObject *UAdd_type;
315 static PyTypeObject *USub_type;
316 static PyTypeObject *cmpop_type;
317 static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
318 *Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
319 *NotIn_singleton;
320 static PyObject* ast2obj_cmpop(cmpop_ty);
321 static PyTypeObject *Eq_type;
322 static PyTypeObject *NotEq_type;
323 static PyTypeObject *Lt_type;
324 static PyTypeObject *LtE_type;
325 static PyTypeObject *Gt_type;
326 static PyTypeObject *GtE_type;
327 static PyTypeObject *Is_type;
328 static PyTypeObject *IsNot_type;
329 static PyTypeObject *In_type;
330 static PyTypeObject *NotIn_type;
331 static PyTypeObject *comprehension_type;
332 static PyObject* ast2obj_comprehension(void*);
333 static char *comprehension_fields[]={
334 "target",
335 "iter",
336 "ifs",
338 static PyTypeObject *excepthandler_type;
339 static char *excepthandler_attributes[] = {
340 "lineno",
341 "col_offset",
343 static PyObject* ast2obj_excepthandler(void*);
344 static PyTypeObject *ExceptHandler_type;
345 static char *ExceptHandler_fields[]={
346 "type",
347 "name",
348 "body",
350 static PyTypeObject *arguments_type;
351 static PyObject* ast2obj_arguments(void*);
352 static char *arguments_fields[]={
353 "args",
354 "vararg",
355 "kwarg",
356 "defaults",
358 static PyTypeObject *keyword_type;
359 static PyObject* ast2obj_keyword(void*);
360 static char *keyword_fields[]={
361 "arg",
362 "value",
364 static PyTypeObject *alias_type;
365 static PyObject* ast2obj_alias(void*);
366 static char *alias_fields[]={
367 "name",
368 "asname",
372 static int
373 ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
375 Py_ssize_t i, numfields = 0;
376 int res = -1;
377 PyObject *key, *value, *fields;
378 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
379 if (!fields)
380 PyErr_Clear();
381 if (fields) {
382 numfields = PySequence_Size(fields);
383 if (numfields == -1)
384 goto cleanup;
386 res = 0; /* if no error occurs, this stays 0 to the end */
387 if (PyTuple_GET_SIZE(args) > 0) {
388 if (numfields != PyTuple_GET_SIZE(args)) {
389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
390 "%zd positional argument%s",
391 Py_TYPE(self)->tp_name,
392 numfields == 0 ? "" : "either 0 or ",
393 numfields, numfields == 1 ? "" : "s");
394 res = -1;
395 goto cleanup;
397 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
398 /* cannot be reached when fields is NULL */
399 PyObject *name = PySequence_GetItem(fields, i);
400 if (!name) {
401 res = -1;
402 goto cleanup;
404 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
405 Py_DECREF(name);
406 if (res < 0)
407 goto cleanup;
410 if (kw) {
411 i = 0; /* needed by PyDict_Next */
412 while (PyDict_Next(kw, &i, &key, &value)) {
413 res = PyObject_SetAttr(self, key, value);
414 if (res < 0)
415 goto cleanup;
418 cleanup:
419 Py_XDECREF(fields);
420 return res;
423 /* Pickling support */
424 static PyObject *
425 ast_type_reduce(PyObject *self, PyObject *unused)
427 PyObject *res;
428 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
429 if (dict == NULL) {
430 if (PyErr_ExceptionMatches(PyExc_AttributeError))
431 PyErr_Clear();
432 else
433 return NULL;
435 if (dict) {
436 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
437 Py_DECREF(dict);
438 return res;
440 return Py_BuildValue("O()", Py_TYPE(self));
443 static PyMethodDef ast_type_methods[] = {
444 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
445 {NULL}
448 static PyTypeObject AST_type = {
449 PyVarObject_HEAD_INIT(&PyType_Type, 0)
450 "_ast.AST",
451 sizeof(PyObject),
453 0, /* tp_dealloc */
454 0, /* tp_print */
455 0, /* tp_getattr */
456 0, /* tp_setattr */
457 0, /* tp_compare */
458 0, /* tp_repr */
459 0, /* tp_as_number */
460 0, /* tp_as_sequence */
461 0, /* tp_as_mapping */
462 0, /* tp_hash */
463 0, /* tp_call */
464 0, /* tp_str */
465 PyObject_GenericGetAttr, /* tp_getattro */
466 PyObject_GenericSetAttr, /* tp_setattro */
467 0, /* tp_as_buffer */
468 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
469 0, /* tp_doc */
470 0, /* tp_traverse */
471 0, /* tp_clear */
472 0, /* tp_richcompare */
473 0, /* tp_weaklistoffset */
474 0, /* tp_iter */
475 0, /* tp_iternext */
476 ast_type_methods, /* tp_methods */
477 0, /* tp_members */
478 0, /* tp_getset */
479 0, /* tp_base */
480 0, /* tp_dict */
481 0, /* tp_descr_get */
482 0, /* tp_descr_set */
483 0, /* tp_dictoffset */
484 (initproc)ast_type_init, /* tp_init */
485 PyType_GenericAlloc, /* tp_alloc */
486 PyType_GenericNew, /* tp_new */
487 PyObject_Del, /* tp_free */
491 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
493 PyObject *fnames, *result;
494 int i;
495 fnames = PyTuple_New(num_fields);
496 if (!fnames) return NULL;
497 for (i = 0; i < num_fields; i++) {
498 PyObject *field = PyString_FromString(fields[i]);
499 if (!field) {
500 Py_DECREF(fnames);
501 return NULL;
503 PyTuple_SET_ITEM(fnames, i, field);
505 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
506 type, base, "_fields", fnames, "__module__", "_ast");
507 Py_DECREF(fnames);
508 return (PyTypeObject*)result;
511 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
513 int i, result;
514 PyObject *s, *l = PyTuple_New(num_fields);
515 if (!l) return 0;
516 for(i = 0; i < num_fields; i++) {
517 s = PyString_FromString(attrs[i]);
518 if (!s) {
519 Py_DECREF(l);
520 return 0;
522 PyTuple_SET_ITEM(l, i, s);
524 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
525 Py_DECREF(l);
526 return result;
529 /* Conversion AST -> Python */
531 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
533 int i, n = asdl_seq_LEN(seq);
534 PyObject *result = PyList_New(n);
535 PyObject *value;
536 if (!result)
537 return NULL;
538 for (i = 0; i < n; i++) {
539 value = func(asdl_seq_GET(seq, i));
540 if (!value) {
541 Py_DECREF(result);
542 return NULL;
544 PyList_SET_ITEM(result, i, value);
546 return result;
549 static PyObject* ast2obj_object(void *o)
551 if (!o)
552 o = Py_None;
553 Py_INCREF((PyObject*)o);
554 return (PyObject*)o;
556 #define ast2obj_identifier ast2obj_object
557 #define ast2obj_string ast2obj_object
558 static PyObject* ast2obj_bool(bool b)
560 return PyBool_FromLong(b);
563 static PyObject* ast2obj_int(long b)
565 return PyInt_FromLong(b);
568 /* Conversion Python -> AST */
570 static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
572 if (obj == Py_None)
573 obj = NULL;
574 if (obj)
575 PyArena_AddPyObject(arena, obj);
576 Py_XINCREF(obj);
577 *out = obj;
578 return 0;
581 #define obj2ast_identifier obj2ast_object
582 #define obj2ast_string obj2ast_object
584 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
586 int i;
587 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
588 PyObject *s = PyObject_Repr(obj);
589 if (s == NULL) return 1;
590 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
591 PyString_AS_STRING(s));
592 Py_DECREF(s);
593 return 1;
596 i = (int)PyLong_AsLong(obj);
597 if (i == -1 && PyErr_Occurred())
598 return 1;
599 *out = i;
600 return 0;
603 static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
605 if (!PyBool_Check(obj)) {
606 PyObject *s = PyObject_Repr(obj);
607 if (s == NULL) return 1;
608 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
609 PyString_AS_STRING(s));
610 Py_DECREF(s);
611 return 1;
614 *out = (obj == Py_True);
615 return 0;
618 static int add_ast_fields(void)
620 PyObject *empty_tuple, *d;
621 if (PyType_Ready(&AST_type) < 0)
622 return -1;
623 d = AST_type.tp_dict;
624 empty_tuple = PyTuple_New(0);
625 if (!empty_tuple ||
626 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
627 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
628 Py_XDECREF(empty_tuple);
629 return -1;
631 Py_DECREF(empty_tuple);
632 return 0;
636 static int init_types(void)
638 static int initialized;
639 if (initialized) return 1;
640 if (add_ast_fields() < 0) return 0;
641 mod_type = make_type("mod", &AST_type, NULL, 0);
642 if (!mod_type) return 0;
643 if (!add_attributes(mod_type, NULL, 0)) return 0;
644 Module_type = make_type("Module", mod_type, Module_fields, 1);
645 if (!Module_type) return 0;
646 Interactive_type = make_type("Interactive", mod_type,
647 Interactive_fields, 1);
648 if (!Interactive_type) return 0;
649 Expression_type = make_type("Expression", mod_type, Expression_fields,
651 if (!Expression_type) return 0;
652 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
653 if (!Suite_type) return 0;
654 stmt_type = make_type("stmt", &AST_type, NULL, 0);
655 if (!stmt_type) return 0;
656 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
657 FunctionDef_type = make_type("FunctionDef", stmt_type,
658 FunctionDef_fields, 4);
659 if (!FunctionDef_type) return 0;
660 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
661 if (!ClassDef_type) return 0;
662 Return_type = make_type("Return", stmt_type, Return_fields, 1);
663 if (!Return_type) return 0;
664 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
665 if (!Delete_type) return 0;
666 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
667 if (!Assign_type) return 0;
668 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
669 if (!AugAssign_type) return 0;
670 Print_type = make_type("Print", stmt_type, Print_fields, 3);
671 if (!Print_type) return 0;
672 For_type = make_type("For", stmt_type, For_fields, 4);
673 if (!For_type) return 0;
674 While_type = make_type("While", stmt_type, While_fields, 3);
675 if (!While_type) return 0;
676 If_type = make_type("If", stmt_type, If_fields, 3);
677 if (!If_type) return 0;
678 With_type = make_type("With", stmt_type, With_fields, 3);
679 if (!With_type) return 0;
680 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
681 if (!Raise_type) return 0;
682 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
683 if (!TryExcept_type) return 0;
684 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
686 if (!TryFinally_type) return 0;
687 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
688 if (!Assert_type) return 0;
689 Import_type = make_type("Import", stmt_type, Import_fields, 1);
690 if (!Import_type) return 0;
691 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
693 if (!ImportFrom_type) return 0;
694 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
695 if (!Exec_type) return 0;
696 Global_type = make_type("Global", stmt_type, Global_fields, 1);
697 if (!Global_type) return 0;
698 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
699 if (!Expr_type) return 0;
700 Pass_type = make_type("Pass", stmt_type, NULL, 0);
701 if (!Pass_type) return 0;
702 Break_type = make_type("Break", stmt_type, NULL, 0);
703 if (!Break_type) return 0;
704 Continue_type = make_type("Continue", stmt_type, NULL, 0);
705 if (!Continue_type) return 0;
706 expr_type = make_type("expr", &AST_type, NULL, 0);
707 if (!expr_type) return 0;
708 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
709 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
710 if (!BoolOp_type) return 0;
711 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
712 if (!BinOp_type) return 0;
713 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
714 if (!UnaryOp_type) return 0;
715 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
716 if (!Lambda_type) return 0;
717 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
718 if (!IfExp_type) return 0;
719 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
720 if (!Dict_type) return 0;
721 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
722 if (!ListComp_type) return 0;
723 GeneratorExp_type = make_type("GeneratorExp", expr_type,
724 GeneratorExp_fields, 2);
725 if (!GeneratorExp_type) return 0;
726 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
727 if (!Yield_type) return 0;
728 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
729 if (!Compare_type) return 0;
730 Call_type = make_type("Call", expr_type, Call_fields, 5);
731 if (!Call_type) return 0;
732 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
733 if (!Repr_type) return 0;
734 Num_type = make_type("Num", expr_type, Num_fields, 1);
735 if (!Num_type) return 0;
736 Str_type = make_type("Str", expr_type, Str_fields, 1);
737 if (!Str_type) return 0;
738 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
739 if (!Attribute_type) return 0;
740 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
741 if (!Subscript_type) return 0;
742 Name_type = make_type("Name", expr_type, Name_fields, 2);
743 if (!Name_type) return 0;
744 List_type = make_type("List", expr_type, List_fields, 2);
745 if (!List_type) return 0;
746 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
747 if (!Tuple_type) return 0;
748 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
749 if (!expr_context_type) return 0;
750 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
751 Load_type = make_type("Load", expr_context_type, NULL, 0);
752 if (!Load_type) return 0;
753 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
754 if (!Load_singleton) return 0;
755 Store_type = make_type("Store", expr_context_type, NULL, 0);
756 if (!Store_type) return 0;
757 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
758 if (!Store_singleton) return 0;
759 Del_type = make_type("Del", expr_context_type, NULL, 0);
760 if (!Del_type) return 0;
761 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
762 if (!Del_singleton) return 0;
763 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
764 if (!AugLoad_type) return 0;
765 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
766 if (!AugLoad_singleton) return 0;
767 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
768 if (!AugStore_type) return 0;
769 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
770 if (!AugStore_singleton) return 0;
771 Param_type = make_type("Param", expr_context_type, NULL, 0);
772 if (!Param_type) return 0;
773 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
774 if (!Param_singleton) return 0;
775 slice_type = make_type("slice", &AST_type, NULL, 0);
776 if (!slice_type) return 0;
777 if (!add_attributes(slice_type, NULL, 0)) return 0;
778 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
779 if (!Ellipsis_type) return 0;
780 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
781 if (!Slice_type) return 0;
782 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
783 if (!ExtSlice_type) return 0;
784 Index_type = make_type("Index", slice_type, Index_fields, 1);
785 if (!Index_type) return 0;
786 boolop_type = make_type("boolop", &AST_type, NULL, 0);
787 if (!boolop_type) return 0;
788 if (!add_attributes(boolop_type, NULL, 0)) return 0;
789 And_type = make_type("And", boolop_type, NULL, 0);
790 if (!And_type) return 0;
791 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
792 if (!And_singleton) return 0;
793 Or_type = make_type("Or", boolop_type, NULL, 0);
794 if (!Or_type) return 0;
795 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
796 if (!Or_singleton) return 0;
797 operator_type = make_type("operator", &AST_type, NULL, 0);
798 if (!operator_type) return 0;
799 if (!add_attributes(operator_type, NULL, 0)) return 0;
800 Add_type = make_type("Add", operator_type, NULL, 0);
801 if (!Add_type) return 0;
802 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
803 if (!Add_singleton) return 0;
804 Sub_type = make_type("Sub", operator_type, NULL, 0);
805 if (!Sub_type) return 0;
806 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
807 if (!Sub_singleton) return 0;
808 Mult_type = make_type("Mult", operator_type, NULL, 0);
809 if (!Mult_type) return 0;
810 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
811 if (!Mult_singleton) return 0;
812 Div_type = make_type("Div", operator_type, NULL, 0);
813 if (!Div_type) return 0;
814 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
815 if (!Div_singleton) return 0;
816 Mod_type = make_type("Mod", operator_type, NULL, 0);
817 if (!Mod_type) return 0;
818 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
819 if (!Mod_singleton) return 0;
820 Pow_type = make_type("Pow", operator_type, NULL, 0);
821 if (!Pow_type) return 0;
822 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
823 if (!Pow_singleton) return 0;
824 LShift_type = make_type("LShift", operator_type, NULL, 0);
825 if (!LShift_type) return 0;
826 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
827 if (!LShift_singleton) return 0;
828 RShift_type = make_type("RShift", operator_type, NULL, 0);
829 if (!RShift_type) return 0;
830 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
831 if (!RShift_singleton) return 0;
832 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
833 if (!BitOr_type) return 0;
834 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
835 if (!BitOr_singleton) return 0;
836 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
837 if (!BitXor_type) return 0;
838 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
839 if (!BitXor_singleton) return 0;
840 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
841 if (!BitAnd_type) return 0;
842 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
843 if (!BitAnd_singleton) return 0;
844 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
845 if (!FloorDiv_type) return 0;
846 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
847 if (!FloorDiv_singleton) return 0;
848 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
849 if (!unaryop_type) return 0;
850 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
851 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
852 if (!Invert_type) return 0;
853 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
854 if (!Invert_singleton) return 0;
855 Not_type = make_type("Not", unaryop_type, NULL, 0);
856 if (!Not_type) return 0;
857 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
858 if (!Not_singleton) return 0;
859 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
860 if (!UAdd_type) return 0;
861 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
862 if (!UAdd_singleton) return 0;
863 USub_type = make_type("USub", unaryop_type, NULL, 0);
864 if (!USub_type) return 0;
865 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
866 if (!USub_singleton) return 0;
867 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
868 if (!cmpop_type) return 0;
869 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
870 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
871 if (!Eq_type) return 0;
872 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
873 if (!Eq_singleton) return 0;
874 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
875 if (!NotEq_type) return 0;
876 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
877 if (!NotEq_singleton) return 0;
878 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
879 if (!Lt_type) return 0;
880 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
881 if (!Lt_singleton) return 0;
882 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
883 if (!LtE_type) return 0;
884 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
885 if (!LtE_singleton) return 0;
886 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
887 if (!Gt_type) return 0;
888 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
889 if (!Gt_singleton) return 0;
890 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
891 if (!GtE_type) return 0;
892 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
893 if (!GtE_singleton) return 0;
894 Is_type = make_type("Is", cmpop_type, NULL, 0);
895 if (!Is_type) return 0;
896 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
897 if (!Is_singleton) return 0;
898 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
899 if (!IsNot_type) return 0;
900 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
901 if (!IsNot_singleton) return 0;
902 In_type = make_type("In", cmpop_type, NULL, 0);
903 if (!In_type) return 0;
904 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
905 if (!In_singleton) return 0;
906 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
907 if (!NotIn_type) return 0;
908 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
909 if (!NotIn_singleton) return 0;
910 comprehension_type = make_type("comprehension", &AST_type,
911 comprehension_fields, 3);
912 if (!comprehension_type) return 0;
913 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
914 if (!excepthandler_type) return 0;
915 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
916 return 0;
917 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
918 ExceptHandler_fields, 3);
919 if (!ExceptHandler_type) return 0;
920 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
921 if (!arguments_type) return 0;
922 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
923 if (!keyword_type) return 0;
924 alias_type = make_type("alias", &AST_type, alias_fields, 2);
925 if (!alias_type) return 0;
926 initialized = 1;
927 return 1;
930 static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
931 static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
932 static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
933 static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
934 arena);
935 static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
936 static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
937 static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
938 static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
939 static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
940 static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
941 arena);
942 static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
943 arena);
944 static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
945 static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
946 static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
948 mod_ty
949 Module(asdl_seq * body, PyArena *arena)
951 mod_ty p;
952 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
953 if (!p)
954 return NULL;
955 p->kind = Module_kind;
956 p->v.Module.body = body;
957 return p;
960 mod_ty
961 Interactive(asdl_seq * body, PyArena *arena)
963 mod_ty p;
964 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
965 if (!p)
966 return NULL;
967 p->kind = Interactive_kind;
968 p->v.Interactive.body = body;
969 return p;
972 mod_ty
973 Expression(expr_ty body, PyArena *arena)
975 mod_ty p;
976 if (!body) {
977 PyErr_SetString(PyExc_ValueError,
978 "field body is required for Expression");
979 return NULL;
981 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
982 if (!p)
983 return NULL;
984 p->kind = Expression_kind;
985 p->v.Expression.body = body;
986 return p;
989 mod_ty
990 Suite(asdl_seq * body, PyArena *arena)
992 mod_ty p;
993 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
994 if (!p)
995 return NULL;
996 p->kind = Suite_kind;
997 p->v.Suite.body = body;
998 return p;
1001 stmt_ty
1002 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
1003 decorator_list, int lineno, int col_offset, PyArena *arena)
1005 stmt_ty p;
1006 if (!name) {
1007 PyErr_SetString(PyExc_ValueError,
1008 "field name is required for FunctionDef");
1009 return NULL;
1011 if (!args) {
1012 PyErr_SetString(PyExc_ValueError,
1013 "field args is required for FunctionDef");
1014 return NULL;
1016 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1017 if (!p)
1018 return NULL;
1019 p->kind = FunctionDef_kind;
1020 p->v.FunctionDef.name = name;
1021 p->v.FunctionDef.args = args;
1022 p->v.FunctionDef.body = body;
1023 p->v.FunctionDef.decorator_list = decorator_list;
1024 p->lineno = lineno;
1025 p->col_offset = col_offset;
1026 return p;
1029 stmt_ty
1030 ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1031 decorator_list, int lineno, int col_offset, PyArena *arena)
1033 stmt_ty p;
1034 if (!name) {
1035 PyErr_SetString(PyExc_ValueError,
1036 "field name is required for ClassDef");
1037 return NULL;
1039 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1040 if (!p)
1041 return NULL;
1042 p->kind = ClassDef_kind;
1043 p->v.ClassDef.name = name;
1044 p->v.ClassDef.bases = bases;
1045 p->v.ClassDef.body = body;
1046 p->v.ClassDef.decorator_list = decorator_list;
1047 p->lineno = lineno;
1048 p->col_offset = col_offset;
1049 return p;
1052 stmt_ty
1053 Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
1055 stmt_ty p;
1056 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1057 if (!p)
1058 return NULL;
1059 p->kind = Return_kind;
1060 p->v.Return.value = value;
1061 p->lineno = lineno;
1062 p->col_offset = col_offset;
1063 return p;
1066 stmt_ty
1067 Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
1069 stmt_ty p;
1070 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1071 if (!p)
1072 return NULL;
1073 p->kind = Delete_kind;
1074 p->v.Delete.targets = targets;
1075 p->lineno = lineno;
1076 p->col_offset = col_offset;
1077 return p;
1080 stmt_ty
1081 Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1082 *arena)
1084 stmt_ty p;
1085 if (!value) {
1086 PyErr_SetString(PyExc_ValueError,
1087 "field value is required for Assign");
1088 return NULL;
1090 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1091 if (!p)
1092 return NULL;
1093 p->kind = Assign_kind;
1094 p->v.Assign.targets = targets;
1095 p->v.Assign.value = value;
1096 p->lineno = lineno;
1097 p->col_offset = col_offset;
1098 return p;
1101 stmt_ty
1102 AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1103 col_offset, PyArena *arena)
1105 stmt_ty p;
1106 if (!target) {
1107 PyErr_SetString(PyExc_ValueError,
1108 "field target is required for AugAssign");
1109 return NULL;
1111 if (!op) {
1112 PyErr_SetString(PyExc_ValueError,
1113 "field op is required for AugAssign");
1114 return NULL;
1116 if (!value) {
1117 PyErr_SetString(PyExc_ValueError,
1118 "field value is required for AugAssign");
1119 return NULL;
1121 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1122 if (!p)
1123 return NULL;
1124 p->kind = AugAssign_kind;
1125 p->v.AugAssign.target = target;
1126 p->v.AugAssign.op = op;
1127 p->v.AugAssign.value = value;
1128 p->lineno = lineno;
1129 p->col_offset = col_offset;
1130 return p;
1133 stmt_ty
1134 Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1135 PyArena *arena)
1137 stmt_ty p;
1138 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1139 if (!p)
1140 return NULL;
1141 p->kind = Print_kind;
1142 p->v.Print.dest = dest;
1143 p->v.Print.values = values;
1144 p->v.Print.nl = nl;
1145 p->lineno = lineno;
1146 p->col_offset = col_offset;
1147 return p;
1150 stmt_ty
1151 For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1152 lineno, int col_offset, PyArena *arena)
1154 stmt_ty p;
1155 if (!target) {
1156 PyErr_SetString(PyExc_ValueError,
1157 "field target is required for For");
1158 return NULL;
1160 if (!iter) {
1161 PyErr_SetString(PyExc_ValueError,
1162 "field iter is required for For");
1163 return NULL;
1165 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1166 if (!p)
1167 return NULL;
1168 p->kind = For_kind;
1169 p->v.For.target = target;
1170 p->v.For.iter = iter;
1171 p->v.For.body = body;
1172 p->v.For.orelse = orelse;
1173 p->lineno = lineno;
1174 p->col_offset = col_offset;
1175 return p;
1178 stmt_ty
1179 While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1180 col_offset, PyArena *arena)
1182 stmt_ty p;
1183 if (!test) {
1184 PyErr_SetString(PyExc_ValueError,
1185 "field test is required for While");
1186 return NULL;
1188 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1189 if (!p)
1190 return NULL;
1191 p->kind = While_kind;
1192 p->v.While.test = test;
1193 p->v.While.body = body;
1194 p->v.While.orelse = orelse;
1195 p->lineno = lineno;
1196 p->col_offset = col_offset;
1197 return p;
1200 stmt_ty
1201 If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1202 col_offset, PyArena *arena)
1204 stmt_ty p;
1205 if (!test) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "field test is required for If");
1208 return NULL;
1210 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1211 if (!p)
1212 return NULL;
1213 p->kind = If_kind;
1214 p->v.If.test = test;
1215 p->v.If.body = body;
1216 p->v.If.orelse = orelse;
1217 p->lineno = lineno;
1218 p->col_offset = col_offset;
1219 return p;
1222 stmt_ty
1223 With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
1224 int col_offset, PyArena *arena)
1226 stmt_ty p;
1227 if (!context_expr) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "field context_expr is required for With");
1230 return NULL;
1232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1233 if (!p)
1234 return NULL;
1235 p->kind = With_kind;
1236 p->v.With.context_expr = context_expr;
1237 p->v.With.optional_vars = optional_vars;
1238 p->v.With.body = body;
1239 p->lineno = lineno;
1240 p->col_offset = col_offset;
1241 return p;
1244 stmt_ty
1245 Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1246 PyArena *arena)
1248 stmt_ty p;
1249 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1250 if (!p)
1251 return NULL;
1252 p->kind = Raise_kind;
1253 p->v.Raise.type = type;
1254 p->v.Raise.inst = inst;
1255 p->v.Raise.tback = tback;
1256 p->lineno = lineno;
1257 p->col_offset = col_offset;
1258 return p;
1261 stmt_ty
1262 TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
1263 int col_offset, PyArena *arena)
1265 stmt_ty p;
1266 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1267 if (!p)
1268 return NULL;
1269 p->kind = TryExcept_kind;
1270 p->v.TryExcept.body = body;
1271 p->v.TryExcept.handlers = handlers;
1272 p->v.TryExcept.orelse = orelse;
1273 p->lineno = lineno;
1274 p->col_offset = col_offset;
1275 return p;
1278 stmt_ty
1279 TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1280 PyArena *arena)
1282 stmt_ty p;
1283 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1284 if (!p)
1285 return NULL;
1286 p->kind = TryFinally_kind;
1287 p->v.TryFinally.body = body;
1288 p->v.TryFinally.finalbody = finalbody;
1289 p->lineno = lineno;
1290 p->col_offset = col_offset;
1291 return p;
1294 stmt_ty
1295 Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
1297 stmt_ty p;
1298 if (!test) {
1299 PyErr_SetString(PyExc_ValueError,
1300 "field test is required for Assert");
1301 return NULL;
1303 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1304 if (!p)
1305 return NULL;
1306 p->kind = Assert_kind;
1307 p->v.Assert.test = test;
1308 p->v.Assert.msg = msg;
1309 p->lineno = lineno;
1310 p->col_offset = col_offset;
1311 return p;
1314 stmt_ty
1315 Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1317 stmt_ty p;
1318 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1319 if (!p)
1320 return NULL;
1321 p->kind = Import_kind;
1322 p->v.Import.names = names;
1323 p->lineno = lineno;
1324 p->col_offset = col_offset;
1325 return p;
1328 stmt_ty
1329 ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1330 col_offset, PyArena *arena)
1332 stmt_ty p;
1333 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1334 if (!p)
1335 return NULL;
1336 p->kind = ImportFrom_kind;
1337 p->v.ImportFrom.module = module;
1338 p->v.ImportFrom.names = names;
1339 p->v.ImportFrom.level = level;
1340 p->lineno = lineno;
1341 p->col_offset = col_offset;
1342 return p;
1345 stmt_ty
1346 Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1347 PyArena *arena)
1349 stmt_ty p;
1350 if (!body) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field body is required for Exec");
1353 return NULL;
1355 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1356 if (!p)
1357 return NULL;
1358 p->kind = Exec_kind;
1359 p->v.Exec.body = body;
1360 p->v.Exec.globals = globals;
1361 p->v.Exec.locals = locals;
1362 p->lineno = lineno;
1363 p->col_offset = col_offset;
1364 return p;
1367 stmt_ty
1368 Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1370 stmt_ty p;
1371 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1372 if (!p)
1373 return NULL;
1374 p->kind = Global_kind;
1375 p->v.Global.names = names;
1376 p->lineno = lineno;
1377 p->col_offset = col_offset;
1378 return p;
1381 stmt_ty
1382 Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1384 stmt_ty p;
1385 if (!value) {
1386 PyErr_SetString(PyExc_ValueError,
1387 "field value is required for Expr");
1388 return NULL;
1390 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1391 if (!p)
1392 return NULL;
1393 p->kind = Expr_kind;
1394 p->v.Expr.value = value;
1395 p->lineno = lineno;
1396 p->col_offset = col_offset;
1397 return p;
1400 stmt_ty
1401 Pass(int lineno, int col_offset, PyArena *arena)
1403 stmt_ty p;
1404 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1405 if (!p)
1406 return NULL;
1407 p->kind = Pass_kind;
1408 p->lineno = lineno;
1409 p->col_offset = col_offset;
1410 return p;
1413 stmt_ty
1414 Break(int lineno, int col_offset, PyArena *arena)
1416 stmt_ty p;
1417 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1418 if (!p)
1419 return NULL;
1420 p->kind = Break_kind;
1421 p->lineno = lineno;
1422 p->col_offset = col_offset;
1423 return p;
1426 stmt_ty
1427 Continue(int lineno, int col_offset, PyArena *arena)
1429 stmt_ty p;
1430 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1431 if (!p)
1432 return NULL;
1433 p->kind = Continue_kind;
1434 p->lineno = lineno;
1435 p->col_offset = col_offset;
1436 return p;
1439 expr_ty
1440 BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1441 *arena)
1443 expr_ty p;
1444 if (!op) {
1445 PyErr_SetString(PyExc_ValueError,
1446 "field op is required for BoolOp");
1447 return NULL;
1449 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1450 if (!p)
1451 return NULL;
1452 p->kind = BoolOp_kind;
1453 p->v.BoolOp.op = op;
1454 p->v.BoolOp.values = values;
1455 p->lineno = lineno;
1456 p->col_offset = col_offset;
1457 return p;
1460 expr_ty
1461 BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1462 PyArena *arena)
1464 expr_ty p;
1465 if (!left) {
1466 PyErr_SetString(PyExc_ValueError,
1467 "field left is required for BinOp");
1468 return NULL;
1470 if (!op) {
1471 PyErr_SetString(PyExc_ValueError,
1472 "field op is required for BinOp");
1473 return NULL;
1475 if (!right) {
1476 PyErr_SetString(PyExc_ValueError,
1477 "field right is required for BinOp");
1478 return NULL;
1480 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1481 if (!p)
1482 return NULL;
1483 p->kind = BinOp_kind;
1484 p->v.BinOp.left = left;
1485 p->v.BinOp.op = op;
1486 p->v.BinOp.right = right;
1487 p->lineno = lineno;
1488 p->col_offset = col_offset;
1489 return p;
1492 expr_ty
1493 UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1494 *arena)
1496 expr_ty p;
1497 if (!op) {
1498 PyErr_SetString(PyExc_ValueError,
1499 "field op is required for UnaryOp");
1500 return NULL;
1502 if (!operand) {
1503 PyErr_SetString(PyExc_ValueError,
1504 "field operand is required for UnaryOp");
1505 return NULL;
1507 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1508 if (!p)
1509 return NULL;
1510 p->kind = UnaryOp_kind;
1511 p->v.UnaryOp.op = op;
1512 p->v.UnaryOp.operand = operand;
1513 p->lineno = lineno;
1514 p->col_offset = col_offset;
1515 return p;
1518 expr_ty
1519 Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1520 *arena)
1522 expr_ty p;
1523 if (!args) {
1524 PyErr_SetString(PyExc_ValueError,
1525 "field args is required for Lambda");
1526 return NULL;
1528 if (!body) {
1529 PyErr_SetString(PyExc_ValueError,
1530 "field body is required for Lambda");
1531 return NULL;
1533 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1534 if (!p)
1535 return NULL;
1536 p->kind = Lambda_kind;
1537 p->v.Lambda.args = args;
1538 p->v.Lambda.body = body;
1539 p->lineno = lineno;
1540 p->col_offset = col_offset;
1541 return p;
1544 expr_ty
1545 IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1546 PyArena *arena)
1548 expr_ty p;
1549 if (!test) {
1550 PyErr_SetString(PyExc_ValueError,
1551 "field test is required for IfExp");
1552 return NULL;
1554 if (!body) {
1555 PyErr_SetString(PyExc_ValueError,
1556 "field body is required for IfExp");
1557 return NULL;
1559 if (!orelse) {
1560 PyErr_SetString(PyExc_ValueError,
1561 "field orelse is required for IfExp");
1562 return NULL;
1564 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1565 if (!p)
1566 return NULL;
1567 p->kind = IfExp_kind;
1568 p->v.IfExp.test = test;
1569 p->v.IfExp.body = body;
1570 p->v.IfExp.orelse = orelse;
1571 p->lineno = lineno;
1572 p->col_offset = col_offset;
1573 return p;
1576 expr_ty
1577 Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1578 *arena)
1580 expr_ty p;
1581 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1582 if (!p)
1583 return NULL;
1584 p->kind = Dict_kind;
1585 p->v.Dict.keys = keys;
1586 p->v.Dict.values = values;
1587 p->lineno = lineno;
1588 p->col_offset = col_offset;
1589 return p;
1592 expr_ty
1593 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1594 PyArena *arena)
1596 expr_ty p;
1597 if (!elt) {
1598 PyErr_SetString(PyExc_ValueError,
1599 "field elt is required for ListComp");
1600 return NULL;
1602 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1603 if (!p)
1604 return NULL;
1605 p->kind = ListComp_kind;
1606 p->v.ListComp.elt = elt;
1607 p->v.ListComp.generators = generators;
1608 p->lineno = lineno;
1609 p->col_offset = col_offset;
1610 return p;
1613 expr_ty
1614 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1615 PyArena *arena)
1617 expr_ty p;
1618 if (!elt) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "field elt is required for GeneratorExp");
1621 return NULL;
1623 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1624 if (!p)
1625 return NULL;
1626 p->kind = GeneratorExp_kind;
1627 p->v.GeneratorExp.elt = elt;
1628 p->v.GeneratorExp.generators = generators;
1629 p->lineno = lineno;
1630 p->col_offset = col_offset;
1631 return p;
1634 expr_ty
1635 Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
1637 expr_ty p;
1638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = Yield_kind;
1642 p->v.Yield.value = value;
1643 p->lineno = lineno;
1644 p->col_offset = col_offset;
1645 return p;
1648 expr_ty
1649 Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1650 int col_offset, PyArena *arena)
1652 expr_ty p;
1653 if (!left) {
1654 PyErr_SetString(PyExc_ValueError,
1655 "field left is required for Compare");
1656 return NULL;
1658 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1659 if (!p)
1660 return NULL;
1661 p->kind = Compare_kind;
1662 p->v.Compare.left = left;
1663 p->v.Compare.ops = ops;
1664 p->v.Compare.comparators = comparators;
1665 p->lineno = lineno;
1666 p->col_offset = col_offset;
1667 return p;
1670 expr_ty
1671 Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
1672 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
1674 expr_ty p;
1675 if (!func) {
1676 PyErr_SetString(PyExc_ValueError,
1677 "field func is required for Call");
1678 return NULL;
1680 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1681 if (!p)
1682 return NULL;
1683 p->kind = Call_kind;
1684 p->v.Call.func = func;
1685 p->v.Call.args = args;
1686 p->v.Call.keywords = keywords;
1687 p->v.Call.starargs = starargs;
1688 p->v.Call.kwargs = kwargs;
1689 p->lineno = lineno;
1690 p->col_offset = col_offset;
1691 return p;
1694 expr_ty
1695 Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1697 expr_ty p;
1698 if (!value) {
1699 PyErr_SetString(PyExc_ValueError,
1700 "field value is required for Repr");
1701 return NULL;
1703 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1704 if (!p)
1705 return NULL;
1706 p->kind = Repr_kind;
1707 p->v.Repr.value = value;
1708 p->lineno = lineno;
1709 p->col_offset = col_offset;
1710 return p;
1713 expr_ty
1714 Num(object n, int lineno, int col_offset, PyArena *arena)
1716 expr_ty p;
1717 if (!n) {
1718 PyErr_SetString(PyExc_ValueError,
1719 "field n is required for Num");
1720 return NULL;
1722 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1723 if (!p)
1724 return NULL;
1725 p->kind = Num_kind;
1726 p->v.Num.n = n;
1727 p->lineno = lineno;
1728 p->col_offset = col_offset;
1729 return p;
1732 expr_ty
1733 Str(string s, int lineno, int col_offset, PyArena *arena)
1735 expr_ty p;
1736 if (!s) {
1737 PyErr_SetString(PyExc_ValueError,
1738 "field s is required for Str");
1739 return NULL;
1741 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1742 if (!p)
1743 return NULL;
1744 p->kind = Str_kind;
1745 p->v.Str.s = s;
1746 p->lineno = lineno;
1747 p->col_offset = col_offset;
1748 return p;
1751 expr_ty
1752 Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1753 col_offset, PyArena *arena)
1755 expr_ty p;
1756 if (!value) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field value is required for Attribute");
1759 return NULL;
1761 if (!attr) {
1762 PyErr_SetString(PyExc_ValueError,
1763 "field attr is required for Attribute");
1764 return NULL;
1766 if (!ctx) {
1767 PyErr_SetString(PyExc_ValueError,
1768 "field ctx is required for Attribute");
1769 return NULL;
1771 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1772 if (!p)
1773 return NULL;
1774 p->kind = Attribute_kind;
1775 p->v.Attribute.value = value;
1776 p->v.Attribute.attr = attr;
1777 p->v.Attribute.ctx = ctx;
1778 p->lineno = lineno;
1779 p->col_offset = col_offset;
1780 return p;
1783 expr_ty
1784 Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1785 col_offset, PyArena *arena)
1787 expr_ty p;
1788 if (!value) {
1789 PyErr_SetString(PyExc_ValueError,
1790 "field value is required for Subscript");
1791 return NULL;
1793 if (!slice) {
1794 PyErr_SetString(PyExc_ValueError,
1795 "field slice is required for Subscript");
1796 return NULL;
1798 if (!ctx) {
1799 PyErr_SetString(PyExc_ValueError,
1800 "field ctx is required for Subscript");
1801 return NULL;
1803 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1804 if (!p)
1805 return NULL;
1806 p->kind = Subscript_kind;
1807 p->v.Subscript.value = value;
1808 p->v.Subscript.slice = slice;
1809 p->v.Subscript.ctx = ctx;
1810 p->lineno = lineno;
1811 p->col_offset = col_offset;
1812 return p;
1815 expr_ty
1816 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1817 *arena)
1819 expr_ty p;
1820 if (!id) {
1821 PyErr_SetString(PyExc_ValueError,
1822 "field id is required for Name");
1823 return NULL;
1825 if (!ctx) {
1826 PyErr_SetString(PyExc_ValueError,
1827 "field ctx is required for Name");
1828 return NULL;
1830 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1831 if (!p)
1832 return NULL;
1833 p->kind = Name_kind;
1834 p->v.Name.id = id;
1835 p->v.Name.ctx = ctx;
1836 p->lineno = lineno;
1837 p->col_offset = col_offset;
1838 return p;
1841 expr_ty
1842 List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1843 *arena)
1845 expr_ty p;
1846 if (!ctx) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field ctx is required for List");
1849 return NULL;
1851 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1852 if (!p)
1853 return NULL;
1854 p->kind = List_kind;
1855 p->v.List.elts = elts;
1856 p->v.List.ctx = ctx;
1857 p->lineno = lineno;
1858 p->col_offset = col_offset;
1859 return p;
1862 expr_ty
1863 Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1864 *arena)
1866 expr_ty p;
1867 if (!ctx) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field ctx is required for Tuple");
1870 return NULL;
1872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1873 if (!p)
1874 return NULL;
1875 p->kind = Tuple_kind;
1876 p->v.Tuple.elts = elts;
1877 p->v.Tuple.ctx = ctx;
1878 p->lineno = lineno;
1879 p->col_offset = col_offset;
1880 return p;
1883 slice_ty
1884 Ellipsis(PyArena *arena)
1886 slice_ty p;
1887 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1888 if (!p)
1889 return NULL;
1890 p->kind = Ellipsis_kind;
1891 return p;
1894 slice_ty
1895 Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
1897 slice_ty p;
1898 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1899 if (!p)
1900 return NULL;
1901 p->kind = Slice_kind;
1902 p->v.Slice.lower = lower;
1903 p->v.Slice.upper = upper;
1904 p->v.Slice.step = step;
1905 return p;
1908 slice_ty
1909 ExtSlice(asdl_seq * dims, PyArena *arena)
1911 slice_ty p;
1912 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1913 if (!p)
1914 return NULL;
1915 p->kind = ExtSlice_kind;
1916 p->v.ExtSlice.dims = dims;
1917 return p;
1920 slice_ty
1921 Index(expr_ty value, PyArena *arena)
1923 slice_ty p;
1924 if (!value) {
1925 PyErr_SetString(PyExc_ValueError,
1926 "field value is required for Index");
1927 return NULL;
1929 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1930 if (!p)
1931 return NULL;
1932 p->kind = Index_kind;
1933 p->v.Index.value = value;
1934 return p;
1937 comprehension_ty
1938 comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
1940 comprehension_ty p;
1941 if (!target) {
1942 PyErr_SetString(PyExc_ValueError,
1943 "field target is required for comprehension");
1944 return NULL;
1946 if (!iter) {
1947 PyErr_SetString(PyExc_ValueError,
1948 "field iter is required for comprehension");
1949 return NULL;
1951 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
1952 if (!p)
1953 return NULL;
1954 p->target = target;
1955 p->iter = iter;
1956 p->ifs = ifs;
1957 return p;
1960 excepthandler_ty
1961 ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
1962 col_offset, PyArena *arena)
1964 excepthandler_ty p;
1965 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
1966 if (!p)
1967 return NULL;
1968 p->kind = ExceptHandler_kind;
1969 p->v.ExceptHandler.type = type;
1970 p->v.ExceptHandler.name = name;
1971 p->v.ExceptHandler.body = body;
1972 p->lineno = lineno;
1973 p->col_offset = col_offset;
1974 return p;
1977 arguments_ty
1978 arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
1979 defaults, PyArena *arena)
1981 arguments_ty p;
1982 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
1983 if (!p)
1984 return NULL;
1985 p->args = args;
1986 p->vararg = vararg;
1987 p->kwarg = kwarg;
1988 p->defaults = defaults;
1989 return p;
1992 keyword_ty
1993 keyword(identifier arg, expr_ty value, PyArena *arena)
1995 keyword_ty p;
1996 if (!arg) {
1997 PyErr_SetString(PyExc_ValueError,
1998 "field arg is required for keyword");
1999 return NULL;
2001 if (!value) {
2002 PyErr_SetString(PyExc_ValueError,
2003 "field value is required for keyword");
2004 return NULL;
2006 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2007 if (!p)
2008 return NULL;
2009 p->arg = arg;
2010 p->value = value;
2011 return p;
2014 alias_ty
2015 alias(identifier name, identifier asname, PyArena *arena)
2017 alias_ty p;
2018 if (!name) {
2019 PyErr_SetString(PyExc_ValueError,
2020 "field name is required for alias");
2021 return NULL;
2023 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2024 if (!p)
2025 return NULL;
2026 p->name = name;
2027 p->asname = asname;
2028 return p;
2032 PyObject*
2033 ast2obj_mod(void* _o)
2035 mod_ty o = (mod_ty)_o;
2036 PyObject *result = NULL, *value = NULL;
2037 if (!o) {
2038 Py_INCREF(Py_None);
2039 return Py_None;
2042 switch (o->kind) {
2043 case Module_kind:
2044 result = PyType_GenericNew(Module_type, NULL, NULL);
2045 if (!result) goto failed;
2046 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2047 if (!value) goto failed;
2048 if (PyObject_SetAttrString(result, "body", value) == -1)
2049 goto failed;
2050 Py_DECREF(value);
2051 break;
2052 case Interactive_kind:
2053 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2054 if (!result) goto failed;
2055 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2056 if (!value) goto failed;
2057 if (PyObject_SetAttrString(result, "body", value) == -1)
2058 goto failed;
2059 Py_DECREF(value);
2060 break;
2061 case Expression_kind:
2062 result = PyType_GenericNew(Expression_type, NULL, NULL);
2063 if (!result) goto failed;
2064 value = ast2obj_expr(o->v.Expression.body);
2065 if (!value) goto failed;
2066 if (PyObject_SetAttrString(result, "body", value) == -1)
2067 goto failed;
2068 Py_DECREF(value);
2069 break;
2070 case Suite_kind:
2071 result = PyType_GenericNew(Suite_type, NULL, NULL);
2072 if (!result) goto failed;
2073 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2074 if (!value) goto failed;
2075 if (PyObject_SetAttrString(result, "body", value) == -1)
2076 goto failed;
2077 Py_DECREF(value);
2078 break;
2080 return result;
2081 failed:
2082 Py_XDECREF(value);
2083 Py_XDECREF(result);
2084 return NULL;
2087 PyObject*
2088 ast2obj_stmt(void* _o)
2090 stmt_ty o = (stmt_ty)_o;
2091 PyObject *result = NULL, *value = NULL;
2092 if (!o) {
2093 Py_INCREF(Py_None);
2094 return Py_None;
2097 switch (o->kind) {
2098 case FunctionDef_kind:
2099 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2100 if (!result) goto failed;
2101 value = ast2obj_identifier(o->v.FunctionDef.name);
2102 if (!value) goto failed;
2103 if (PyObject_SetAttrString(result, "name", value) == -1)
2104 goto failed;
2105 Py_DECREF(value);
2106 value = ast2obj_arguments(o->v.FunctionDef.args);
2107 if (!value) goto failed;
2108 if (PyObject_SetAttrString(result, "args", value) == -1)
2109 goto failed;
2110 Py_DECREF(value);
2111 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2112 if (!value) goto failed;
2113 if (PyObject_SetAttrString(result, "body", value) == -1)
2114 goto failed;
2115 Py_DECREF(value);
2116 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2117 ast2obj_expr);
2118 if (!value) goto failed;
2119 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2121 goto failed;
2122 Py_DECREF(value);
2123 break;
2124 case ClassDef_kind:
2125 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2126 if (!result) goto failed;
2127 value = ast2obj_identifier(o->v.ClassDef.name);
2128 if (!value) goto failed;
2129 if (PyObject_SetAttrString(result, "name", value) == -1)
2130 goto failed;
2131 Py_DECREF(value);
2132 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2133 if (!value) goto failed;
2134 if (PyObject_SetAttrString(result, "bases", value) == -1)
2135 goto failed;
2136 Py_DECREF(value);
2137 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2138 if (!value) goto failed;
2139 if (PyObject_SetAttrString(result, "body", value) == -1)
2140 goto failed;
2141 Py_DECREF(value);
2142 value = ast2obj_list(o->v.ClassDef.decorator_list,
2143 ast2obj_expr);
2144 if (!value) goto failed;
2145 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2147 goto failed;
2148 Py_DECREF(value);
2149 break;
2150 case Return_kind:
2151 result = PyType_GenericNew(Return_type, NULL, NULL);
2152 if (!result) goto failed;
2153 value = ast2obj_expr(o->v.Return.value);
2154 if (!value) goto failed;
2155 if (PyObject_SetAttrString(result, "value", value) == -1)
2156 goto failed;
2157 Py_DECREF(value);
2158 break;
2159 case Delete_kind:
2160 result = PyType_GenericNew(Delete_type, NULL, NULL);
2161 if (!result) goto failed;
2162 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2163 if (!value) goto failed;
2164 if (PyObject_SetAttrString(result, "targets", value) == -1)
2165 goto failed;
2166 Py_DECREF(value);
2167 break;
2168 case Assign_kind:
2169 result = PyType_GenericNew(Assign_type, NULL, NULL);
2170 if (!result) goto failed;
2171 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2172 if (!value) goto failed;
2173 if (PyObject_SetAttrString(result, "targets", value) == -1)
2174 goto failed;
2175 Py_DECREF(value);
2176 value = ast2obj_expr(o->v.Assign.value);
2177 if (!value) goto failed;
2178 if (PyObject_SetAttrString(result, "value", value) == -1)
2179 goto failed;
2180 Py_DECREF(value);
2181 break;
2182 case AugAssign_kind:
2183 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2184 if (!result) goto failed;
2185 value = ast2obj_expr(o->v.AugAssign.target);
2186 if (!value) goto failed;
2187 if (PyObject_SetAttrString(result, "target", value) == -1)
2188 goto failed;
2189 Py_DECREF(value);
2190 value = ast2obj_operator(o->v.AugAssign.op);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "op", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 value = ast2obj_expr(o->v.AugAssign.value);
2196 if (!value) goto failed;
2197 if (PyObject_SetAttrString(result, "value", value) == -1)
2198 goto failed;
2199 Py_DECREF(value);
2200 break;
2201 case Print_kind:
2202 result = PyType_GenericNew(Print_type, NULL, NULL);
2203 if (!result) goto failed;
2204 value = ast2obj_expr(o->v.Print.dest);
2205 if (!value) goto failed;
2206 if (PyObject_SetAttrString(result, "dest", value) == -1)
2207 goto failed;
2208 Py_DECREF(value);
2209 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2210 if (!value) goto failed;
2211 if (PyObject_SetAttrString(result, "values", value) == -1)
2212 goto failed;
2213 Py_DECREF(value);
2214 value = ast2obj_bool(o->v.Print.nl);
2215 if (!value) goto failed;
2216 if (PyObject_SetAttrString(result, "nl", value) == -1)
2217 goto failed;
2218 Py_DECREF(value);
2219 break;
2220 case For_kind:
2221 result = PyType_GenericNew(For_type, NULL, NULL);
2222 if (!result) goto failed;
2223 value = ast2obj_expr(o->v.For.target);
2224 if (!value) goto failed;
2225 if (PyObject_SetAttrString(result, "target", value) == -1)
2226 goto failed;
2227 Py_DECREF(value);
2228 value = ast2obj_expr(o->v.For.iter);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "iter", value) == -1)
2231 goto failed;
2232 Py_DECREF(value);
2233 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "body", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
2238 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
2243 break;
2244 case While_kind:
2245 result = PyType_GenericNew(While_type, NULL, NULL);
2246 if (!result) goto failed;
2247 value = ast2obj_expr(o->v.While.test);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "test", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
2252 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "body", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
2257 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
2262 break;
2263 case If_kind:
2264 result = PyType_GenericNew(If_type, NULL, NULL);
2265 if (!result) goto failed;
2266 value = ast2obj_expr(o->v.If.test);
2267 if (!value) goto failed;
2268 if (PyObject_SetAttrString(result, "test", value) == -1)
2269 goto failed;
2270 Py_DECREF(value);
2271 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2272 if (!value) goto failed;
2273 if (PyObject_SetAttrString(result, "body", value) == -1)
2274 goto failed;
2275 Py_DECREF(value);
2276 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2277 if (!value) goto failed;
2278 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2279 goto failed;
2280 Py_DECREF(value);
2281 break;
2282 case With_kind:
2283 result = PyType_GenericNew(With_type, NULL, NULL);
2284 if (!result) goto failed;
2285 value = ast2obj_expr(o->v.With.context_expr);
2286 if (!value) goto failed;
2287 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2288 goto failed;
2289 Py_DECREF(value);
2290 value = ast2obj_expr(o->v.With.optional_vars);
2291 if (!value) goto failed;
2292 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2294 goto failed;
2295 Py_DECREF(value);
2296 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2297 if (!value) goto failed;
2298 if (PyObject_SetAttrString(result, "body", value) == -1)
2299 goto failed;
2300 Py_DECREF(value);
2301 break;
2302 case Raise_kind:
2303 result = PyType_GenericNew(Raise_type, NULL, NULL);
2304 if (!result) goto failed;
2305 value = ast2obj_expr(o->v.Raise.type);
2306 if (!value) goto failed;
2307 if (PyObject_SetAttrString(result, "type", value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 value = ast2obj_expr(o->v.Raise.inst);
2311 if (!value) goto failed;
2312 if (PyObject_SetAttrString(result, "inst", value) == -1)
2313 goto failed;
2314 Py_DECREF(value);
2315 value = ast2obj_expr(o->v.Raise.tback);
2316 if (!value) goto failed;
2317 if (PyObject_SetAttrString(result, "tback", value) == -1)
2318 goto failed;
2319 Py_DECREF(value);
2320 break;
2321 case TryExcept_kind:
2322 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2323 if (!result) goto failed;
2324 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2325 if (!value) goto failed;
2326 if (PyObject_SetAttrString(result, "body", value) == -1)
2327 goto failed;
2328 Py_DECREF(value);
2329 value = ast2obj_list(o->v.TryExcept.handlers,
2330 ast2obj_excepthandler);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
2340 break;
2341 case TryFinally_kind:
2342 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2343 if (!result) goto failed;
2344 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "body", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
2354 break;
2355 case Assert_kind:
2356 result = PyType_GenericNew(Assert_type, NULL, NULL);
2357 if (!result) goto failed;
2358 value = ast2obj_expr(o->v.Assert.test);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "test", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 value = ast2obj_expr(o->v.Assert.msg);
2364 if (!value) goto failed;
2365 if (PyObject_SetAttrString(result, "msg", value) == -1)
2366 goto failed;
2367 Py_DECREF(value);
2368 break;
2369 case Import_kind:
2370 result = PyType_GenericNew(Import_type, NULL, NULL);
2371 if (!result) goto failed;
2372 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "names", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
2377 break;
2378 case ImportFrom_kind:
2379 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2380 if (!result) goto failed;
2381 value = ast2obj_identifier(o->v.ImportFrom.module);
2382 if (!value) goto failed;
2383 if (PyObject_SetAttrString(result, "module", value) == -1)
2384 goto failed;
2385 Py_DECREF(value);
2386 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "names", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
2391 value = ast2obj_int(o->v.ImportFrom.level);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "level", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 break;
2397 case Exec_kind:
2398 result = PyType_GenericNew(Exec_type, NULL, NULL);
2399 if (!result) goto failed;
2400 value = ast2obj_expr(o->v.Exec.body);
2401 if (!value) goto failed;
2402 if (PyObject_SetAttrString(result, "body", value) == -1)
2403 goto failed;
2404 Py_DECREF(value);
2405 value = ast2obj_expr(o->v.Exec.globals);
2406 if (!value) goto failed;
2407 if (PyObject_SetAttrString(result, "globals", value) == -1)
2408 goto failed;
2409 Py_DECREF(value);
2410 value = ast2obj_expr(o->v.Exec.locals);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "locals", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
2415 break;
2416 case Global_kind:
2417 result = PyType_GenericNew(Global_type, NULL, NULL);
2418 if (!result) goto failed;
2419 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "names", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
2424 break;
2425 case Expr_kind:
2426 result = PyType_GenericNew(Expr_type, NULL, NULL);
2427 if (!result) goto failed;
2428 value = ast2obj_expr(o->v.Expr.value);
2429 if (!value) goto failed;
2430 if (PyObject_SetAttrString(result, "value", value) == -1)
2431 goto failed;
2432 Py_DECREF(value);
2433 break;
2434 case Pass_kind:
2435 result = PyType_GenericNew(Pass_type, NULL, NULL);
2436 if (!result) goto failed;
2437 break;
2438 case Break_kind:
2439 result = PyType_GenericNew(Break_type, NULL, NULL);
2440 if (!result) goto failed;
2441 break;
2442 case Continue_kind:
2443 result = PyType_GenericNew(Continue_type, NULL, NULL);
2444 if (!result) goto failed;
2445 break;
2447 value = ast2obj_int(o->lineno);
2448 if (!value) goto failed;
2449 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2450 goto failed;
2451 Py_DECREF(value);
2452 value = ast2obj_int(o->col_offset);
2453 if (!value) goto failed;
2454 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2455 goto failed;
2456 Py_DECREF(value);
2457 return result;
2458 failed:
2459 Py_XDECREF(value);
2460 Py_XDECREF(result);
2461 return NULL;
2464 PyObject*
2465 ast2obj_expr(void* _o)
2467 expr_ty o = (expr_ty)_o;
2468 PyObject *result = NULL, *value = NULL;
2469 if (!o) {
2470 Py_INCREF(Py_None);
2471 return Py_None;
2474 switch (o->kind) {
2475 case BoolOp_kind:
2476 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_boolop(o->v.BoolOp.op);
2479 if (!value) goto failed;
2480 if (PyObject_SetAttrString(result, "op", value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "values", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
2488 break;
2489 case BinOp_kind:
2490 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2491 if (!result) goto failed;
2492 value = ast2obj_expr(o->v.BinOp.left);
2493 if (!value) goto failed;
2494 if (PyObject_SetAttrString(result, "left", value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
2497 value = ast2obj_operator(o->v.BinOp.op);
2498 if (!value) goto failed;
2499 if (PyObject_SetAttrString(result, "op", value) == -1)
2500 goto failed;
2501 Py_DECREF(value);
2502 value = ast2obj_expr(o->v.BinOp.right);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "right", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
2507 break;
2508 case UnaryOp_kind:
2509 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2510 if (!result) goto failed;
2511 value = ast2obj_unaryop(o->v.UnaryOp.op);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "op", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 value = ast2obj_expr(o->v.UnaryOp.operand);
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "operand", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
2521 break;
2522 case Lambda_kind:
2523 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2524 if (!result) goto failed;
2525 value = ast2obj_arguments(o->v.Lambda.args);
2526 if (!value) goto failed;
2527 if (PyObject_SetAttrString(result, "args", value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_expr(o->v.Lambda.body);
2531 if (!value) goto failed;
2532 if (PyObject_SetAttrString(result, "body", value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 break;
2536 case IfExp_kind:
2537 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2538 if (!result) goto failed;
2539 value = ast2obj_expr(o->v.IfExp.test);
2540 if (!value) goto failed;
2541 if (PyObject_SetAttrString(result, "test", value) == -1)
2542 goto failed;
2543 Py_DECREF(value);
2544 value = ast2obj_expr(o->v.IfExp.body);
2545 if (!value) goto failed;
2546 if (PyObject_SetAttrString(result, "body", value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_expr(o->v.IfExp.orelse);
2550 if (!value) goto failed;
2551 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 break;
2555 case Dict_kind:
2556 result = PyType_GenericNew(Dict_type, NULL, NULL);
2557 if (!result) goto failed;
2558 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2559 if (!value) goto failed;
2560 if (PyObject_SetAttrString(result, "keys", value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
2563 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2564 if (!value) goto failed;
2565 if (PyObject_SetAttrString(result, "values", value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
2568 break;
2569 case ListComp_kind:
2570 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2571 if (!result) goto failed;
2572 value = ast2obj_expr(o->v.ListComp.elt);
2573 if (!value) goto failed;
2574 if (PyObject_SetAttrString(result, "elt", value) == -1)
2575 goto failed;
2576 Py_DECREF(value);
2577 value = ast2obj_list(o->v.ListComp.generators,
2578 ast2obj_comprehension);
2579 if (!value) goto failed;
2580 if (PyObject_SetAttrString(result, "generators", value) == -1)
2581 goto failed;
2582 Py_DECREF(value);
2583 break;
2584 case GeneratorExp_kind:
2585 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2586 if (!result) goto failed;
2587 value = ast2obj_expr(o->v.GeneratorExp.elt);
2588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "elt", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 value = ast2obj_list(o->v.GeneratorExp.generators,
2593 ast2obj_comprehension);
2594 if (!value) goto failed;
2595 if (PyObject_SetAttrString(result, "generators", value) == -1)
2596 goto failed;
2597 Py_DECREF(value);
2598 break;
2599 case Yield_kind:
2600 result = PyType_GenericNew(Yield_type, NULL, NULL);
2601 if (!result) goto failed;
2602 value = ast2obj_expr(o->v.Yield.value);
2603 if (!value) goto failed;
2604 if (PyObject_SetAttrString(result, "value", value) == -1)
2605 goto failed;
2606 Py_DECREF(value);
2607 break;
2608 case Compare_kind:
2609 result = PyType_GenericNew(Compare_type, NULL, NULL);
2610 if (!result) goto failed;
2611 value = ast2obj_expr(o->v.Compare.left);
2612 if (!value) goto failed;
2613 if (PyObject_SetAttrString(result, "left", value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
2617 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2618 value = PyList_New(n);
2619 if (!value) goto failed;
2620 for(i = 0; i < n; i++)
2621 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "ops", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2628 if (!value) goto failed;
2629 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
2632 break;
2633 case Call_kind:
2634 result = PyType_GenericNew(Call_type, NULL, NULL);
2635 if (!result) goto failed;
2636 value = ast2obj_expr(o->v.Call.func);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "func", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
2641 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2642 if (!value) goto failed;
2643 if (PyObject_SetAttrString(result, "args", value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 value = ast2obj_expr(o->v.Call.starargs);
2652 if (!value) goto failed;
2653 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2654 goto failed;
2655 Py_DECREF(value);
2656 value = ast2obj_expr(o->v.Call.kwargs);
2657 if (!value) goto failed;
2658 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
2661 break;
2662 case Repr_kind:
2663 result = PyType_GenericNew(Repr_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_expr(o->v.Repr.value);
2666 if (!value) goto failed;
2667 if (PyObject_SetAttrString(result, "value", value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
2670 break;
2671 case Num_kind:
2672 result = PyType_GenericNew(Num_type, NULL, NULL);
2673 if (!result) goto failed;
2674 value = ast2obj_object(o->v.Num.n);
2675 if (!value) goto failed;
2676 if (PyObject_SetAttrString(result, "n", value) == -1)
2677 goto failed;
2678 Py_DECREF(value);
2679 break;
2680 case Str_kind:
2681 result = PyType_GenericNew(Str_type, NULL, NULL);
2682 if (!result) goto failed;
2683 value = ast2obj_string(o->v.Str.s);
2684 if (!value) goto failed;
2685 if (PyObject_SetAttrString(result, "s", value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
2688 break;
2689 case Attribute_kind:
2690 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2691 if (!result) goto failed;
2692 value = ast2obj_expr(o->v.Attribute.value);
2693 if (!value) goto failed;
2694 if (PyObject_SetAttrString(result, "value", value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
2697 value = ast2obj_identifier(o->v.Attribute.attr);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "attr", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
2702 value = ast2obj_expr_context(o->v.Attribute.ctx);
2703 if (!value) goto failed;
2704 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
2707 break;
2708 case Subscript_kind:
2709 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2710 if (!result) goto failed;
2711 value = ast2obj_expr(o->v.Subscript.value);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "value", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
2716 value = ast2obj_slice(o->v.Subscript.slice);
2717 if (!value) goto failed;
2718 if (PyObject_SetAttrString(result, "slice", value) == -1)
2719 goto failed;
2720 Py_DECREF(value);
2721 value = ast2obj_expr_context(o->v.Subscript.ctx);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 break;
2727 case Name_kind:
2728 result = PyType_GenericNew(Name_type, NULL, NULL);
2729 if (!result) goto failed;
2730 value = ast2obj_identifier(o->v.Name.id);
2731 if (!value) goto failed;
2732 if (PyObject_SetAttrString(result, "id", value) == -1)
2733 goto failed;
2734 Py_DECREF(value);
2735 value = ast2obj_expr_context(o->v.Name.ctx);
2736 if (!value) goto failed;
2737 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2738 goto failed;
2739 Py_DECREF(value);
2740 break;
2741 case List_kind:
2742 result = PyType_GenericNew(List_type, NULL, NULL);
2743 if (!result) goto failed;
2744 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2745 if (!value) goto failed;
2746 if (PyObject_SetAttrString(result, "elts", value) == -1)
2747 goto failed;
2748 Py_DECREF(value);
2749 value = ast2obj_expr_context(o->v.List.ctx);
2750 if (!value) goto failed;
2751 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
2754 break;
2755 case Tuple_kind:
2756 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2757 if (!result) goto failed;
2758 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2759 if (!value) goto failed;
2760 if (PyObject_SetAttrString(result, "elts", value) == -1)
2761 goto failed;
2762 Py_DECREF(value);
2763 value = ast2obj_expr_context(o->v.Tuple.ctx);
2764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
2768 break;
2770 value = ast2obj_int(o->lineno);
2771 if (!value) goto failed;
2772 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2773 goto failed;
2774 Py_DECREF(value);
2775 value = ast2obj_int(o->col_offset);
2776 if (!value) goto failed;
2777 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2778 goto failed;
2779 Py_DECREF(value);
2780 return result;
2781 failed:
2782 Py_XDECREF(value);
2783 Py_XDECREF(result);
2784 return NULL;
2787 PyObject* ast2obj_expr_context(expr_context_ty o)
2789 switch(o) {
2790 case Load:
2791 Py_INCREF(Load_singleton);
2792 return Load_singleton;
2793 case Store:
2794 Py_INCREF(Store_singleton);
2795 return Store_singleton;
2796 case Del:
2797 Py_INCREF(Del_singleton);
2798 return Del_singleton;
2799 case AugLoad:
2800 Py_INCREF(AugLoad_singleton);
2801 return AugLoad_singleton;
2802 case AugStore:
2803 Py_INCREF(AugStore_singleton);
2804 return AugStore_singleton;
2805 case Param:
2806 Py_INCREF(Param_singleton);
2807 return Param_singleton;
2808 default:
2809 /* should never happen, but just in case ... */
2810 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2811 return NULL;
2814 PyObject*
2815 ast2obj_slice(void* _o)
2817 slice_ty o = (slice_ty)_o;
2818 PyObject *result = NULL, *value = NULL;
2819 if (!o) {
2820 Py_INCREF(Py_None);
2821 return Py_None;
2824 switch (o->kind) {
2825 case Ellipsis_kind:
2826 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2827 if (!result) goto failed;
2828 break;
2829 case Slice_kind:
2830 result = PyType_GenericNew(Slice_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_expr(o->v.Slice.lower);
2833 if (!value) goto failed;
2834 if (PyObject_SetAttrString(result, "lower", value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_expr(o->v.Slice.upper);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "upper", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_expr(o->v.Slice.step);
2843 if (!value) goto failed;
2844 if (PyObject_SetAttrString(result, "step", value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 break;
2848 case ExtSlice_kind:
2849 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2850 if (!result) goto failed;
2851 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2852 if (!value) goto failed;
2853 if (PyObject_SetAttrString(result, "dims", value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 break;
2857 case Index_kind:
2858 result = PyType_GenericNew(Index_type, NULL, NULL);
2859 if (!result) goto failed;
2860 value = ast2obj_expr(o->v.Index.value);
2861 if (!value) goto failed;
2862 if (PyObject_SetAttrString(result, "value", value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 break;
2867 return result;
2868 failed:
2869 Py_XDECREF(value);
2870 Py_XDECREF(result);
2871 return NULL;
2874 PyObject* ast2obj_boolop(boolop_ty o)
2876 switch(o) {
2877 case And:
2878 Py_INCREF(And_singleton);
2879 return And_singleton;
2880 case Or:
2881 Py_INCREF(Or_singleton);
2882 return Or_singleton;
2883 default:
2884 /* should never happen, but just in case ... */
2885 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2886 return NULL;
2889 PyObject* ast2obj_operator(operator_ty o)
2891 switch(o) {
2892 case Add:
2893 Py_INCREF(Add_singleton);
2894 return Add_singleton;
2895 case Sub:
2896 Py_INCREF(Sub_singleton);
2897 return Sub_singleton;
2898 case Mult:
2899 Py_INCREF(Mult_singleton);
2900 return Mult_singleton;
2901 case Div:
2902 Py_INCREF(Div_singleton);
2903 return Div_singleton;
2904 case Mod:
2905 Py_INCREF(Mod_singleton);
2906 return Mod_singleton;
2907 case Pow:
2908 Py_INCREF(Pow_singleton);
2909 return Pow_singleton;
2910 case LShift:
2911 Py_INCREF(LShift_singleton);
2912 return LShift_singleton;
2913 case RShift:
2914 Py_INCREF(RShift_singleton);
2915 return RShift_singleton;
2916 case BitOr:
2917 Py_INCREF(BitOr_singleton);
2918 return BitOr_singleton;
2919 case BitXor:
2920 Py_INCREF(BitXor_singleton);
2921 return BitXor_singleton;
2922 case BitAnd:
2923 Py_INCREF(BitAnd_singleton);
2924 return BitAnd_singleton;
2925 case FloorDiv:
2926 Py_INCREF(FloorDiv_singleton);
2927 return FloorDiv_singleton;
2928 default:
2929 /* should never happen, but just in case ... */
2930 PyErr_Format(PyExc_SystemError, "unknown operator found");
2931 return NULL;
2934 PyObject* ast2obj_unaryop(unaryop_ty o)
2936 switch(o) {
2937 case Invert:
2938 Py_INCREF(Invert_singleton);
2939 return Invert_singleton;
2940 case Not:
2941 Py_INCREF(Not_singleton);
2942 return Not_singleton;
2943 case UAdd:
2944 Py_INCREF(UAdd_singleton);
2945 return UAdd_singleton;
2946 case USub:
2947 Py_INCREF(USub_singleton);
2948 return USub_singleton;
2949 default:
2950 /* should never happen, but just in case ... */
2951 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2952 return NULL;
2955 PyObject* ast2obj_cmpop(cmpop_ty o)
2957 switch(o) {
2958 case Eq:
2959 Py_INCREF(Eq_singleton);
2960 return Eq_singleton;
2961 case NotEq:
2962 Py_INCREF(NotEq_singleton);
2963 return NotEq_singleton;
2964 case Lt:
2965 Py_INCREF(Lt_singleton);
2966 return Lt_singleton;
2967 case LtE:
2968 Py_INCREF(LtE_singleton);
2969 return LtE_singleton;
2970 case Gt:
2971 Py_INCREF(Gt_singleton);
2972 return Gt_singleton;
2973 case GtE:
2974 Py_INCREF(GtE_singleton);
2975 return GtE_singleton;
2976 case Is:
2977 Py_INCREF(Is_singleton);
2978 return Is_singleton;
2979 case IsNot:
2980 Py_INCREF(IsNot_singleton);
2981 return IsNot_singleton;
2982 case In:
2983 Py_INCREF(In_singleton);
2984 return In_singleton;
2985 case NotIn:
2986 Py_INCREF(NotIn_singleton);
2987 return NotIn_singleton;
2988 default:
2989 /* should never happen, but just in case ... */
2990 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2991 return NULL;
2994 PyObject*
2995 ast2obj_comprehension(void* _o)
2997 comprehension_ty o = (comprehension_ty)_o;
2998 PyObject *result = NULL, *value = NULL;
2999 if (!o) {
3000 Py_INCREF(Py_None);
3001 return Py_None;
3004 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3005 if (!result) return NULL;
3006 value = ast2obj_expr(o->target);
3007 if (!value) goto failed;
3008 if (PyObject_SetAttrString(result, "target", value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
3011 value = ast2obj_expr(o->iter);
3012 if (!value) goto failed;
3013 if (PyObject_SetAttrString(result, "iter", value) == -1)
3014 goto failed;
3015 Py_DECREF(value);
3016 value = ast2obj_list(o->ifs, ast2obj_expr);
3017 if (!value) goto failed;
3018 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
3021 return result;
3022 failed:
3023 Py_XDECREF(value);
3024 Py_XDECREF(result);
3025 return NULL;
3028 PyObject*
3029 ast2obj_excepthandler(void* _o)
3031 excepthandler_ty o = (excepthandler_ty)_o;
3032 PyObject *result = NULL, *value = NULL;
3033 if (!o) {
3034 Py_INCREF(Py_None);
3035 return Py_None;
3038 switch (o->kind) {
3039 case ExceptHandler_kind:
3040 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3041 if (!result) goto failed;
3042 value = ast2obj_expr(o->v.ExceptHandler.type);
3043 if (!value) goto failed;
3044 if (PyObject_SetAttrString(result, "type", value) == -1)
3045 goto failed;
3046 Py_DECREF(value);
3047 value = ast2obj_expr(o->v.ExceptHandler.name);
3048 if (!value) goto failed;
3049 if (PyObject_SetAttrString(result, "name", value) == -1)
3050 goto failed;
3051 Py_DECREF(value);
3052 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3053 if (!value) goto failed;
3054 if (PyObject_SetAttrString(result, "body", value) == -1)
3055 goto failed;
3056 Py_DECREF(value);
3057 break;
3059 value = ast2obj_int(o->lineno);
3060 if (!value) goto failed;
3061 if (PyObject_SetAttrString(result, "lineno", value) < 0)
3062 goto failed;
3063 Py_DECREF(value);
3064 value = ast2obj_int(o->col_offset);
3065 if (!value) goto failed;
3066 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
3067 goto failed;
3068 Py_DECREF(value);
3069 return result;
3070 failed:
3071 Py_XDECREF(value);
3072 Py_XDECREF(result);
3073 return NULL;
3076 PyObject*
3077 ast2obj_arguments(void* _o)
3079 arguments_ty o = (arguments_ty)_o;
3080 PyObject *result = NULL, *value = NULL;
3081 if (!o) {
3082 Py_INCREF(Py_None);
3083 return Py_None;
3086 result = PyType_GenericNew(arguments_type, NULL, NULL);
3087 if (!result) return NULL;
3088 value = ast2obj_list(o->args, ast2obj_expr);
3089 if (!value) goto failed;
3090 if (PyObject_SetAttrString(result, "args", value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_identifier(o->vararg);
3094 if (!value) goto failed;
3095 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3096 goto failed;
3097 Py_DECREF(value);
3098 value = ast2obj_identifier(o->kwarg);
3099 if (!value) goto failed;
3100 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3101 goto failed;
3102 Py_DECREF(value);
3103 value = ast2obj_list(o->defaults, ast2obj_expr);
3104 if (!value) goto failed;
3105 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3106 goto failed;
3107 Py_DECREF(value);
3108 return result;
3109 failed:
3110 Py_XDECREF(value);
3111 Py_XDECREF(result);
3112 return NULL;
3115 PyObject*
3116 ast2obj_keyword(void* _o)
3118 keyword_ty o = (keyword_ty)_o;
3119 PyObject *result = NULL, *value = NULL;
3120 if (!o) {
3121 Py_INCREF(Py_None);
3122 return Py_None;
3125 result = PyType_GenericNew(keyword_type, NULL, NULL);
3126 if (!result) return NULL;
3127 value = ast2obj_identifier(o->arg);
3128 if (!value) goto failed;
3129 if (PyObject_SetAttrString(result, "arg", value) == -1)
3130 goto failed;
3131 Py_DECREF(value);
3132 value = ast2obj_expr(o->value);
3133 if (!value) goto failed;
3134 if (PyObject_SetAttrString(result, "value", value) == -1)
3135 goto failed;
3136 Py_DECREF(value);
3137 return result;
3138 failed:
3139 Py_XDECREF(value);
3140 Py_XDECREF(result);
3141 return NULL;
3144 PyObject*
3145 ast2obj_alias(void* _o)
3147 alias_ty o = (alias_ty)_o;
3148 PyObject *result = NULL, *value = NULL;
3149 if (!o) {
3150 Py_INCREF(Py_None);
3151 return Py_None;
3154 result = PyType_GenericNew(alias_type, NULL, NULL);
3155 if (!result) return NULL;
3156 value = ast2obj_identifier(o->name);
3157 if (!value) goto failed;
3158 if (PyObject_SetAttrString(result, "name", value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 value = ast2obj_identifier(o->asname);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "asname", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 return result;
3167 failed:
3168 Py_XDECREF(value);
3169 Py_XDECREF(result);
3170 return NULL;
3175 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3177 PyObject* tmp = NULL;
3180 if (obj == Py_None) {
3181 *out = NULL;
3182 return 0;
3184 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3185 asdl_seq* body;
3187 if (PyObject_HasAttrString(obj, "body")) {
3188 int res;
3189 Py_ssize_t len;
3190 Py_ssize_t i;
3191 tmp = PyObject_GetAttrString(obj, "body");
3192 if (tmp == NULL) goto failed;
3193 if (!PyList_Check(tmp)) {
3194 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3195 goto failed;
3197 len = PyList_GET_SIZE(tmp);
3198 body = asdl_seq_new(len, arena);
3199 if (body == NULL) goto failed;
3200 for (i = 0; i < len; i++) {
3201 stmt_ty value;
3202 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3203 if (res != 0) goto failed;
3204 asdl_seq_SET(body, i, value);
3206 Py_XDECREF(tmp);
3207 tmp = NULL;
3208 } else {
3209 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3210 return 1;
3212 *out = Module(body, arena);
3213 if (*out == NULL) goto failed;
3214 return 0;
3216 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3217 asdl_seq* body;
3219 if (PyObject_HasAttrString(obj, "body")) {
3220 int res;
3221 Py_ssize_t len;
3222 Py_ssize_t i;
3223 tmp = PyObject_GetAttrString(obj, "body");
3224 if (tmp == NULL) goto failed;
3225 if (!PyList_Check(tmp)) {
3226 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3227 goto failed;
3229 len = PyList_GET_SIZE(tmp);
3230 body = asdl_seq_new(len, arena);
3231 if (body == NULL) goto failed;
3232 for (i = 0; i < len; i++) {
3233 stmt_ty value;
3234 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3235 if (res != 0) goto failed;
3236 asdl_seq_SET(body, i, value);
3238 Py_XDECREF(tmp);
3239 tmp = NULL;
3240 } else {
3241 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3242 return 1;
3244 *out = Interactive(body, arena);
3245 if (*out == NULL) goto failed;
3246 return 0;
3248 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3249 expr_ty body;
3251 if (PyObject_HasAttrString(obj, "body")) {
3252 int res;
3253 tmp = PyObject_GetAttrString(obj, "body");
3254 if (tmp == NULL) goto failed;
3255 res = obj2ast_expr(tmp, &body, arena);
3256 if (res != 0) goto failed;
3257 Py_XDECREF(tmp);
3258 tmp = NULL;
3259 } else {
3260 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3261 return 1;
3263 *out = Expression(body, arena);
3264 if (*out == NULL) goto failed;
3265 return 0;
3267 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3268 asdl_seq* body;
3270 if (PyObject_HasAttrString(obj, "body")) {
3271 int res;
3272 Py_ssize_t len;
3273 Py_ssize_t i;
3274 tmp = PyObject_GetAttrString(obj, "body");
3275 if (tmp == NULL) goto failed;
3276 if (!PyList_Check(tmp)) {
3277 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3278 goto failed;
3280 len = PyList_GET_SIZE(tmp);
3281 body = asdl_seq_new(len, arena);
3282 if (body == NULL) goto failed;
3283 for (i = 0; i < len; i++) {
3284 stmt_ty value;
3285 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3286 if (res != 0) goto failed;
3287 asdl_seq_SET(body, i, value);
3289 Py_XDECREF(tmp);
3290 tmp = NULL;
3291 } else {
3292 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3293 return 1;
3295 *out = Suite(body, arena);
3296 if (*out == NULL) goto failed;
3297 return 0;
3300 tmp = PyObject_Repr(obj);
3301 if (tmp == NULL) goto failed;
3302 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3303 failed:
3304 Py_XDECREF(tmp);
3305 return 1;
3309 obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3311 PyObject* tmp = NULL;
3313 int lineno;
3314 int col_offset;
3316 if (obj == Py_None) {
3317 *out = NULL;
3318 return 0;
3320 if (PyObject_HasAttrString(obj, "lineno")) {
3321 int res;
3322 tmp = PyObject_GetAttrString(obj, "lineno");
3323 if (tmp == NULL) goto failed;
3324 res = obj2ast_int(tmp, &lineno, arena);
3325 if (res != 0) goto failed;
3326 Py_XDECREF(tmp);
3327 tmp = NULL;
3328 } else {
3329 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3330 return 1;
3332 if (PyObject_HasAttrString(obj, "col_offset")) {
3333 int res;
3334 tmp = PyObject_GetAttrString(obj, "col_offset");
3335 if (tmp == NULL) goto failed;
3336 res = obj2ast_int(tmp, &col_offset, arena);
3337 if (res != 0) goto failed;
3338 Py_XDECREF(tmp);
3339 tmp = NULL;
3340 } else {
3341 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3342 return 1;
3344 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3345 identifier name;
3346 arguments_ty args;
3347 asdl_seq* body;
3348 asdl_seq* decorator_list;
3350 if (PyObject_HasAttrString(obj, "name")) {
3351 int res;
3352 tmp = PyObject_GetAttrString(obj, "name");
3353 if (tmp == NULL) goto failed;
3354 res = obj2ast_identifier(tmp, &name, arena);
3355 if (res != 0) goto failed;
3356 Py_XDECREF(tmp);
3357 tmp = NULL;
3358 } else {
3359 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3360 return 1;
3362 if (PyObject_HasAttrString(obj, "args")) {
3363 int res;
3364 tmp = PyObject_GetAttrString(obj, "args");
3365 if (tmp == NULL) goto failed;
3366 res = obj2ast_arguments(tmp, &args, arena);
3367 if (res != 0) goto failed;
3368 Py_XDECREF(tmp);
3369 tmp = NULL;
3370 } else {
3371 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3372 return 1;
3374 if (PyObject_HasAttrString(obj, "body")) {
3375 int res;
3376 Py_ssize_t len;
3377 Py_ssize_t i;
3378 tmp = PyObject_GetAttrString(obj, "body");
3379 if (tmp == NULL) goto failed;
3380 if (!PyList_Check(tmp)) {
3381 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3382 goto failed;
3384 len = PyList_GET_SIZE(tmp);
3385 body = asdl_seq_new(len, arena);
3386 if (body == NULL) goto failed;
3387 for (i = 0; i < len; i++) {
3388 stmt_ty value;
3389 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3390 if (res != 0) goto failed;
3391 asdl_seq_SET(body, i, value);
3393 Py_XDECREF(tmp);
3394 tmp = NULL;
3395 } else {
3396 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3397 return 1;
3399 if (PyObject_HasAttrString(obj, "decorator_list")) {
3400 int res;
3401 Py_ssize_t len;
3402 Py_ssize_t i;
3403 tmp = PyObject_GetAttrString(obj, "decorator_list");
3404 if (tmp == NULL) goto failed;
3405 if (!PyList_Check(tmp)) {
3406 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3407 goto failed;
3409 len = PyList_GET_SIZE(tmp);
3410 decorator_list = asdl_seq_new(len, arena);
3411 if (decorator_list == NULL) goto failed;
3412 for (i = 0; i < len; i++) {
3413 expr_ty value;
3414 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3415 if (res != 0) goto failed;
3416 asdl_seq_SET(decorator_list, i, value);
3418 Py_XDECREF(tmp);
3419 tmp = NULL;
3420 } else {
3421 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3422 return 1;
3424 *out = FunctionDef(name, args, body, decorator_list, lineno,
3425 col_offset, arena);
3426 if (*out == NULL) goto failed;
3427 return 0;
3429 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3430 identifier name;
3431 asdl_seq* bases;
3432 asdl_seq* body;
3433 asdl_seq* decorator_list;
3435 if (PyObject_HasAttrString(obj, "name")) {
3436 int res;
3437 tmp = PyObject_GetAttrString(obj, "name");
3438 if (tmp == NULL) goto failed;
3439 res = obj2ast_identifier(tmp, &name, arena);
3440 if (res != 0) goto failed;
3441 Py_XDECREF(tmp);
3442 tmp = NULL;
3443 } else {
3444 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3445 return 1;
3447 if (PyObject_HasAttrString(obj, "bases")) {
3448 int res;
3449 Py_ssize_t len;
3450 Py_ssize_t i;
3451 tmp = PyObject_GetAttrString(obj, "bases");
3452 if (tmp == NULL) goto failed;
3453 if (!PyList_Check(tmp)) {
3454 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3455 goto failed;
3457 len = PyList_GET_SIZE(tmp);
3458 bases = asdl_seq_new(len, arena);
3459 if (bases == NULL) goto failed;
3460 for (i = 0; i < len; i++) {
3461 expr_ty value;
3462 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3463 if (res != 0) goto failed;
3464 asdl_seq_SET(bases, i, value);
3466 Py_XDECREF(tmp);
3467 tmp = NULL;
3468 } else {
3469 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3470 return 1;
3472 if (PyObject_HasAttrString(obj, "body")) {
3473 int res;
3474 Py_ssize_t len;
3475 Py_ssize_t i;
3476 tmp = PyObject_GetAttrString(obj, "body");
3477 if (tmp == NULL) goto failed;
3478 if (!PyList_Check(tmp)) {
3479 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3480 goto failed;
3482 len = PyList_GET_SIZE(tmp);
3483 body = asdl_seq_new(len, arena);
3484 if (body == NULL) goto failed;
3485 for (i = 0; i < len; i++) {
3486 stmt_ty value;
3487 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3488 if (res != 0) goto failed;
3489 asdl_seq_SET(body, i, value);
3491 Py_XDECREF(tmp);
3492 tmp = NULL;
3493 } else {
3494 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3495 return 1;
3497 if (PyObject_HasAttrString(obj, "decorator_list")) {
3498 int res;
3499 Py_ssize_t len;
3500 Py_ssize_t i;
3501 tmp = PyObject_GetAttrString(obj, "decorator_list");
3502 if (tmp == NULL) goto failed;
3503 if (!PyList_Check(tmp)) {
3504 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3505 goto failed;
3507 len = PyList_GET_SIZE(tmp);
3508 decorator_list = asdl_seq_new(len, arena);
3509 if (decorator_list == NULL) goto failed;
3510 for (i = 0; i < len; i++) {
3511 expr_ty value;
3512 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3513 if (res != 0) goto failed;
3514 asdl_seq_SET(decorator_list, i, value);
3516 Py_XDECREF(tmp);
3517 tmp = NULL;
3518 } else {
3519 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3520 return 1;
3522 *out = ClassDef(name, bases, body, decorator_list, lineno,
3523 col_offset, arena);
3524 if (*out == NULL) goto failed;
3525 return 0;
3527 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3528 expr_ty value;
3530 if (PyObject_HasAttrString(obj, "value")) {
3531 int res;
3532 tmp = PyObject_GetAttrString(obj, "value");
3533 if (tmp == NULL) goto failed;
3534 res = obj2ast_expr(tmp, &value, arena);
3535 if (res != 0) goto failed;
3536 Py_XDECREF(tmp);
3537 tmp = NULL;
3538 } else {
3539 value = NULL;
3541 *out = Return(value, lineno, col_offset, arena);
3542 if (*out == NULL) goto failed;
3543 return 0;
3545 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3546 asdl_seq* targets;
3548 if (PyObject_HasAttrString(obj, "targets")) {
3549 int res;
3550 Py_ssize_t len;
3551 Py_ssize_t i;
3552 tmp = PyObject_GetAttrString(obj, "targets");
3553 if (tmp == NULL) goto failed;
3554 if (!PyList_Check(tmp)) {
3555 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3556 goto failed;
3558 len = PyList_GET_SIZE(tmp);
3559 targets = asdl_seq_new(len, arena);
3560 if (targets == NULL) goto failed;
3561 for (i = 0; i < len; i++) {
3562 expr_ty value;
3563 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3564 if (res != 0) goto failed;
3565 asdl_seq_SET(targets, i, value);
3567 Py_XDECREF(tmp);
3568 tmp = NULL;
3569 } else {
3570 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3571 return 1;
3573 *out = Delete(targets, lineno, col_offset, arena);
3574 if (*out == NULL) goto failed;
3575 return 0;
3577 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3578 asdl_seq* targets;
3579 expr_ty value;
3581 if (PyObject_HasAttrString(obj, "targets")) {
3582 int res;
3583 Py_ssize_t len;
3584 Py_ssize_t i;
3585 tmp = PyObject_GetAttrString(obj, "targets");
3586 if (tmp == NULL) goto failed;
3587 if (!PyList_Check(tmp)) {
3588 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3589 goto failed;
3591 len = PyList_GET_SIZE(tmp);
3592 targets = asdl_seq_new(len, arena);
3593 if (targets == NULL) goto failed;
3594 for (i = 0; i < len; i++) {
3595 expr_ty value;
3596 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3597 if (res != 0) goto failed;
3598 asdl_seq_SET(targets, i, value);
3600 Py_XDECREF(tmp);
3601 tmp = NULL;
3602 } else {
3603 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3604 return 1;
3606 if (PyObject_HasAttrString(obj, "value")) {
3607 int res;
3608 tmp = PyObject_GetAttrString(obj, "value");
3609 if (tmp == NULL) goto failed;
3610 res = obj2ast_expr(tmp, &value, arena);
3611 if (res != 0) goto failed;
3612 Py_XDECREF(tmp);
3613 tmp = NULL;
3614 } else {
3615 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3616 return 1;
3618 *out = Assign(targets, value, lineno, col_offset, arena);
3619 if (*out == NULL) goto failed;
3620 return 0;
3622 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3623 expr_ty target;
3624 operator_ty op;
3625 expr_ty value;
3627 if (PyObject_HasAttrString(obj, "target")) {
3628 int res;
3629 tmp = PyObject_GetAttrString(obj, "target");
3630 if (tmp == NULL) goto failed;
3631 res = obj2ast_expr(tmp, &target, arena);
3632 if (res != 0) goto failed;
3633 Py_XDECREF(tmp);
3634 tmp = NULL;
3635 } else {
3636 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3637 return 1;
3639 if (PyObject_HasAttrString(obj, "op")) {
3640 int res;
3641 tmp = PyObject_GetAttrString(obj, "op");
3642 if (tmp == NULL) goto failed;
3643 res = obj2ast_operator(tmp, &op, arena);
3644 if (res != 0) goto failed;
3645 Py_XDECREF(tmp);
3646 tmp = NULL;
3647 } else {
3648 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3649 return 1;
3651 if (PyObject_HasAttrString(obj, "value")) {
3652 int res;
3653 tmp = PyObject_GetAttrString(obj, "value");
3654 if (tmp == NULL) goto failed;
3655 res = obj2ast_expr(tmp, &value, arena);
3656 if (res != 0) goto failed;
3657 Py_XDECREF(tmp);
3658 tmp = NULL;
3659 } else {
3660 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3661 return 1;
3663 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3664 if (*out == NULL) goto failed;
3665 return 0;
3667 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3668 expr_ty dest;
3669 asdl_seq* values;
3670 bool nl;
3672 if (PyObject_HasAttrString(obj, "dest")) {
3673 int res;
3674 tmp = PyObject_GetAttrString(obj, "dest");
3675 if (tmp == NULL) goto failed;
3676 res = obj2ast_expr(tmp, &dest, arena);
3677 if (res != 0) goto failed;
3678 Py_XDECREF(tmp);
3679 tmp = NULL;
3680 } else {
3681 dest = NULL;
3683 if (PyObject_HasAttrString(obj, "values")) {
3684 int res;
3685 Py_ssize_t len;
3686 Py_ssize_t i;
3687 tmp = PyObject_GetAttrString(obj, "values");
3688 if (tmp == NULL) goto failed;
3689 if (!PyList_Check(tmp)) {
3690 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3691 goto failed;
3693 len = PyList_GET_SIZE(tmp);
3694 values = asdl_seq_new(len, arena);
3695 if (values == NULL) goto failed;
3696 for (i = 0; i < len; i++) {
3697 expr_ty value;
3698 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3699 if (res != 0) goto failed;
3700 asdl_seq_SET(values, i, value);
3702 Py_XDECREF(tmp);
3703 tmp = NULL;
3704 } else {
3705 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3706 return 1;
3708 if (PyObject_HasAttrString(obj, "nl")) {
3709 int res;
3710 tmp = PyObject_GetAttrString(obj, "nl");
3711 if (tmp == NULL) goto failed;
3712 res = obj2ast_bool(tmp, &nl, arena);
3713 if (res != 0) goto failed;
3714 Py_XDECREF(tmp);
3715 tmp = NULL;
3716 } else {
3717 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3718 return 1;
3720 *out = Print(dest, values, nl, lineno, col_offset, arena);
3721 if (*out == NULL) goto failed;
3722 return 0;
3724 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3725 expr_ty target;
3726 expr_ty iter;
3727 asdl_seq* body;
3728 asdl_seq* orelse;
3730 if (PyObject_HasAttrString(obj, "target")) {
3731 int res;
3732 tmp = PyObject_GetAttrString(obj, "target");
3733 if (tmp == NULL) goto failed;
3734 res = obj2ast_expr(tmp, &target, arena);
3735 if (res != 0) goto failed;
3736 Py_XDECREF(tmp);
3737 tmp = NULL;
3738 } else {
3739 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3740 return 1;
3742 if (PyObject_HasAttrString(obj, "iter")) {
3743 int res;
3744 tmp = PyObject_GetAttrString(obj, "iter");
3745 if (tmp == NULL) goto failed;
3746 res = obj2ast_expr(tmp, &iter, arena);
3747 if (res != 0) goto failed;
3748 Py_XDECREF(tmp);
3749 tmp = NULL;
3750 } else {
3751 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3752 return 1;
3754 if (PyObject_HasAttrString(obj, "body")) {
3755 int res;
3756 Py_ssize_t len;
3757 Py_ssize_t i;
3758 tmp = PyObject_GetAttrString(obj, "body");
3759 if (tmp == NULL) goto failed;
3760 if (!PyList_Check(tmp)) {
3761 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3762 goto failed;
3764 len = PyList_GET_SIZE(tmp);
3765 body = asdl_seq_new(len, arena);
3766 if (body == NULL) goto failed;
3767 for (i = 0; i < len; i++) {
3768 stmt_ty value;
3769 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3770 if (res != 0) goto failed;
3771 asdl_seq_SET(body, i, value);
3773 Py_XDECREF(tmp);
3774 tmp = NULL;
3775 } else {
3776 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3777 return 1;
3779 if (PyObject_HasAttrString(obj, "orelse")) {
3780 int res;
3781 Py_ssize_t len;
3782 Py_ssize_t i;
3783 tmp = PyObject_GetAttrString(obj, "orelse");
3784 if (tmp == NULL) goto failed;
3785 if (!PyList_Check(tmp)) {
3786 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3787 goto failed;
3789 len = PyList_GET_SIZE(tmp);
3790 orelse = asdl_seq_new(len, arena);
3791 if (orelse == NULL) goto failed;
3792 for (i = 0; i < len; i++) {
3793 stmt_ty value;
3794 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3795 if (res != 0) goto failed;
3796 asdl_seq_SET(orelse, i, value);
3798 Py_XDECREF(tmp);
3799 tmp = NULL;
3800 } else {
3801 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3802 return 1;
3804 *out = For(target, iter, body, orelse, lineno, col_offset,
3805 arena);
3806 if (*out == NULL) goto failed;
3807 return 0;
3809 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3810 expr_ty test;
3811 asdl_seq* body;
3812 asdl_seq* orelse;
3814 if (PyObject_HasAttrString(obj, "test")) {
3815 int res;
3816 tmp = PyObject_GetAttrString(obj, "test");
3817 if (tmp == NULL) goto failed;
3818 res = obj2ast_expr(tmp, &test, arena);
3819 if (res != 0) goto failed;
3820 Py_XDECREF(tmp);
3821 tmp = NULL;
3822 } else {
3823 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3824 return 1;
3826 if (PyObject_HasAttrString(obj, "body")) {
3827 int res;
3828 Py_ssize_t len;
3829 Py_ssize_t i;
3830 tmp = PyObject_GetAttrString(obj, "body");
3831 if (tmp == NULL) goto failed;
3832 if (!PyList_Check(tmp)) {
3833 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3834 goto failed;
3836 len = PyList_GET_SIZE(tmp);
3837 body = asdl_seq_new(len, arena);
3838 if (body == NULL) goto failed;
3839 for (i = 0; i < len; i++) {
3840 stmt_ty value;
3841 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3842 if (res != 0) goto failed;
3843 asdl_seq_SET(body, i, value);
3845 Py_XDECREF(tmp);
3846 tmp = NULL;
3847 } else {
3848 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3849 return 1;
3851 if (PyObject_HasAttrString(obj, "orelse")) {
3852 int res;
3853 Py_ssize_t len;
3854 Py_ssize_t i;
3855 tmp = PyObject_GetAttrString(obj, "orelse");
3856 if (tmp == NULL) goto failed;
3857 if (!PyList_Check(tmp)) {
3858 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3859 goto failed;
3861 len = PyList_GET_SIZE(tmp);
3862 orelse = asdl_seq_new(len, arena);
3863 if (orelse == NULL) goto failed;
3864 for (i = 0; i < len; i++) {
3865 stmt_ty value;
3866 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3867 if (res != 0) goto failed;
3868 asdl_seq_SET(orelse, i, value);
3870 Py_XDECREF(tmp);
3871 tmp = NULL;
3872 } else {
3873 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3874 return 1;
3876 *out = While(test, body, orelse, lineno, col_offset, arena);
3877 if (*out == NULL) goto failed;
3878 return 0;
3880 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3881 expr_ty test;
3882 asdl_seq* body;
3883 asdl_seq* orelse;
3885 if (PyObject_HasAttrString(obj, "test")) {
3886 int res;
3887 tmp = PyObject_GetAttrString(obj, "test");
3888 if (tmp == NULL) goto failed;
3889 res = obj2ast_expr(tmp, &test, arena);
3890 if (res != 0) goto failed;
3891 Py_XDECREF(tmp);
3892 tmp = NULL;
3893 } else {
3894 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3895 return 1;
3897 if (PyObject_HasAttrString(obj, "body")) {
3898 int res;
3899 Py_ssize_t len;
3900 Py_ssize_t i;
3901 tmp = PyObject_GetAttrString(obj, "body");
3902 if (tmp == NULL) goto failed;
3903 if (!PyList_Check(tmp)) {
3904 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3905 goto failed;
3907 len = PyList_GET_SIZE(tmp);
3908 body = asdl_seq_new(len, arena);
3909 if (body == NULL) goto failed;
3910 for (i = 0; i < len; i++) {
3911 stmt_ty value;
3912 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3913 if (res != 0) goto failed;
3914 asdl_seq_SET(body, i, value);
3916 Py_XDECREF(tmp);
3917 tmp = NULL;
3918 } else {
3919 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3920 return 1;
3922 if (PyObject_HasAttrString(obj, "orelse")) {
3923 int res;
3924 Py_ssize_t len;
3925 Py_ssize_t i;
3926 tmp = PyObject_GetAttrString(obj, "orelse");
3927 if (tmp == NULL) goto failed;
3928 if (!PyList_Check(tmp)) {
3929 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3930 goto failed;
3932 len = PyList_GET_SIZE(tmp);
3933 orelse = asdl_seq_new(len, arena);
3934 if (orelse == NULL) goto failed;
3935 for (i = 0; i < len; i++) {
3936 stmt_ty value;
3937 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3938 if (res != 0) goto failed;
3939 asdl_seq_SET(orelse, i, value);
3941 Py_XDECREF(tmp);
3942 tmp = NULL;
3943 } else {
3944 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3945 return 1;
3947 *out = If(test, body, orelse, lineno, col_offset, arena);
3948 if (*out == NULL) goto failed;
3949 return 0;
3951 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3952 expr_ty context_expr;
3953 expr_ty optional_vars;
3954 asdl_seq* body;
3956 if (PyObject_HasAttrString(obj, "context_expr")) {
3957 int res;
3958 tmp = PyObject_GetAttrString(obj, "context_expr");
3959 if (tmp == NULL) goto failed;
3960 res = obj2ast_expr(tmp, &context_expr, arena);
3961 if (res != 0) goto failed;
3962 Py_XDECREF(tmp);
3963 tmp = NULL;
3964 } else {
3965 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3966 return 1;
3968 if (PyObject_HasAttrString(obj, "optional_vars")) {
3969 int res;
3970 tmp = PyObject_GetAttrString(obj, "optional_vars");
3971 if (tmp == NULL) goto failed;
3972 res = obj2ast_expr(tmp, &optional_vars, arena);
3973 if (res != 0) goto failed;
3974 Py_XDECREF(tmp);
3975 tmp = NULL;
3976 } else {
3977 optional_vars = NULL;
3979 if (PyObject_HasAttrString(obj, "body")) {
3980 int res;
3981 Py_ssize_t len;
3982 Py_ssize_t i;
3983 tmp = PyObject_GetAttrString(obj, "body");
3984 if (tmp == NULL) goto failed;
3985 if (!PyList_Check(tmp)) {
3986 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3987 goto failed;
3989 len = PyList_GET_SIZE(tmp);
3990 body = asdl_seq_new(len, arena);
3991 if (body == NULL) goto failed;
3992 for (i = 0; i < len; i++) {
3993 stmt_ty value;
3994 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3995 if (res != 0) goto failed;
3996 asdl_seq_SET(body, i, value);
3998 Py_XDECREF(tmp);
3999 tmp = NULL;
4000 } else {
4001 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4002 return 1;
4004 *out = With(context_expr, optional_vars, body, lineno,
4005 col_offset, arena);
4006 if (*out == NULL) goto failed;
4007 return 0;
4009 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4010 expr_ty type;
4011 expr_ty inst;
4012 expr_ty tback;
4014 if (PyObject_HasAttrString(obj, "type")) {
4015 int res;
4016 tmp = PyObject_GetAttrString(obj, "type");
4017 if (tmp == NULL) goto failed;
4018 res = obj2ast_expr(tmp, &type, arena);
4019 if (res != 0) goto failed;
4020 Py_XDECREF(tmp);
4021 tmp = NULL;
4022 } else {
4023 type = NULL;
4025 if (PyObject_HasAttrString(obj, "inst")) {
4026 int res;
4027 tmp = PyObject_GetAttrString(obj, "inst");
4028 if (tmp == NULL) goto failed;
4029 res = obj2ast_expr(tmp, &inst, arena);
4030 if (res != 0) goto failed;
4031 Py_XDECREF(tmp);
4032 tmp = NULL;
4033 } else {
4034 inst = NULL;
4036 if (PyObject_HasAttrString(obj, "tback")) {
4037 int res;
4038 tmp = PyObject_GetAttrString(obj, "tback");
4039 if (tmp == NULL) goto failed;
4040 res = obj2ast_expr(tmp, &tback, arena);
4041 if (res != 0) goto failed;
4042 Py_XDECREF(tmp);
4043 tmp = NULL;
4044 } else {
4045 tback = NULL;
4047 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4048 if (*out == NULL) goto failed;
4049 return 0;
4051 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4052 asdl_seq* body;
4053 asdl_seq* handlers;
4054 asdl_seq* orelse;
4056 if (PyObject_HasAttrString(obj, "body")) {
4057 int res;
4058 Py_ssize_t len;
4059 Py_ssize_t i;
4060 tmp = PyObject_GetAttrString(obj, "body");
4061 if (tmp == NULL) goto failed;
4062 if (!PyList_Check(tmp)) {
4063 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4064 goto failed;
4066 len = PyList_GET_SIZE(tmp);
4067 body = asdl_seq_new(len, arena);
4068 if (body == NULL) goto failed;
4069 for (i = 0; i < len; i++) {
4070 stmt_ty value;
4071 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4072 if (res != 0) goto failed;
4073 asdl_seq_SET(body, i, value);
4075 Py_XDECREF(tmp);
4076 tmp = NULL;
4077 } else {
4078 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4079 return 1;
4081 if (PyObject_HasAttrString(obj, "handlers")) {
4082 int res;
4083 Py_ssize_t len;
4084 Py_ssize_t i;
4085 tmp = PyObject_GetAttrString(obj, "handlers");
4086 if (tmp == NULL) goto failed;
4087 if (!PyList_Check(tmp)) {
4088 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4089 goto failed;
4091 len = PyList_GET_SIZE(tmp);
4092 handlers = asdl_seq_new(len, arena);
4093 if (handlers == NULL) goto failed;
4094 for (i = 0; i < len; i++) {
4095 excepthandler_ty value;
4096 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4097 if (res != 0) goto failed;
4098 asdl_seq_SET(handlers, i, value);
4100 Py_XDECREF(tmp);
4101 tmp = NULL;
4102 } else {
4103 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4104 return 1;
4106 if (PyObject_HasAttrString(obj, "orelse")) {
4107 int res;
4108 Py_ssize_t len;
4109 Py_ssize_t i;
4110 tmp = PyObject_GetAttrString(obj, "orelse");
4111 if (tmp == NULL) goto failed;
4112 if (!PyList_Check(tmp)) {
4113 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4114 goto failed;
4116 len = PyList_GET_SIZE(tmp);
4117 orelse = asdl_seq_new(len, arena);
4118 if (orelse == NULL) goto failed;
4119 for (i = 0; i < len; i++) {
4120 stmt_ty value;
4121 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4122 if (res != 0) goto failed;
4123 asdl_seq_SET(orelse, i, value);
4125 Py_XDECREF(tmp);
4126 tmp = NULL;
4127 } else {
4128 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4129 return 1;
4131 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4132 arena);
4133 if (*out == NULL) goto failed;
4134 return 0;
4136 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4137 asdl_seq* body;
4138 asdl_seq* finalbody;
4140 if (PyObject_HasAttrString(obj, "body")) {
4141 int res;
4142 Py_ssize_t len;
4143 Py_ssize_t i;
4144 tmp = PyObject_GetAttrString(obj, "body");
4145 if (tmp == NULL) goto failed;
4146 if (!PyList_Check(tmp)) {
4147 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4148 goto failed;
4150 len = PyList_GET_SIZE(tmp);
4151 body = asdl_seq_new(len, arena);
4152 if (body == NULL) goto failed;
4153 for (i = 0; i < len; i++) {
4154 stmt_ty value;
4155 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4156 if (res != 0) goto failed;
4157 asdl_seq_SET(body, i, value);
4159 Py_XDECREF(tmp);
4160 tmp = NULL;
4161 } else {
4162 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4163 return 1;
4165 if (PyObject_HasAttrString(obj, "finalbody")) {
4166 int res;
4167 Py_ssize_t len;
4168 Py_ssize_t i;
4169 tmp = PyObject_GetAttrString(obj, "finalbody");
4170 if (tmp == NULL) goto failed;
4171 if (!PyList_Check(tmp)) {
4172 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4173 goto failed;
4175 len = PyList_GET_SIZE(tmp);
4176 finalbody = asdl_seq_new(len, arena);
4177 if (finalbody == NULL) goto failed;
4178 for (i = 0; i < len; i++) {
4179 stmt_ty value;
4180 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4181 if (res != 0) goto failed;
4182 asdl_seq_SET(finalbody, i, value);
4184 Py_XDECREF(tmp);
4185 tmp = NULL;
4186 } else {
4187 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4188 return 1;
4190 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4191 if (*out == NULL) goto failed;
4192 return 0;
4194 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4195 expr_ty test;
4196 expr_ty msg;
4198 if (PyObject_HasAttrString(obj, "test")) {
4199 int res;
4200 tmp = PyObject_GetAttrString(obj, "test");
4201 if (tmp == NULL) goto failed;
4202 res = obj2ast_expr(tmp, &test, arena);
4203 if (res != 0) goto failed;
4204 Py_XDECREF(tmp);
4205 tmp = NULL;
4206 } else {
4207 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4208 return 1;
4210 if (PyObject_HasAttrString(obj, "msg")) {
4211 int res;
4212 tmp = PyObject_GetAttrString(obj, "msg");
4213 if (tmp == NULL) goto failed;
4214 res = obj2ast_expr(tmp, &msg, arena);
4215 if (res != 0) goto failed;
4216 Py_XDECREF(tmp);
4217 tmp = NULL;
4218 } else {
4219 msg = NULL;
4221 *out = Assert(test, msg, lineno, col_offset, arena);
4222 if (*out == NULL) goto failed;
4223 return 0;
4225 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4226 asdl_seq* names;
4228 if (PyObject_HasAttrString(obj, "names")) {
4229 int res;
4230 Py_ssize_t len;
4231 Py_ssize_t i;
4232 tmp = PyObject_GetAttrString(obj, "names");
4233 if (tmp == NULL) goto failed;
4234 if (!PyList_Check(tmp)) {
4235 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4236 goto failed;
4238 len = PyList_GET_SIZE(tmp);
4239 names = asdl_seq_new(len, arena);
4240 if (names == NULL) goto failed;
4241 for (i = 0; i < len; i++) {
4242 alias_ty value;
4243 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4244 if (res != 0) goto failed;
4245 asdl_seq_SET(names, i, value);
4247 Py_XDECREF(tmp);
4248 tmp = NULL;
4249 } else {
4250 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4251 return 1;
4253 *out = Import(names, lineno, col_offset, arena);
4254 if (*out == NULL) goto failed;
4255 return 0;
4257 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4258 identifier module;
4259 asdl_seq* names;
4260 int level;
4262 if (PyObject_HasAttrString(obj, "module")) {
4263 int res;
4264 tmp = PyObject_GetAttrString(obj, "module");
4265 if (tmp == NULL) goto failed;
4266 res = obj2ast_identifier(tmp, &module, arena);
4267 if (res != 0) goto failed;
4268 Py_XDECREF(tmp);
4269 tmp = NULL;
4270 } else {
4271 module = NULL;
4273 if (PyObject_HasAttrString(obj, "names")) {
4274 int res;
4275 Py_ssize_t len;
4276 Py_ssize_t i;
4277 tmp = PyObject_GetAttrString(obj, "names");
4278 if (tmp == NULL) goto failed;
4279 if (!PyList_Check(tmp)) {
4280 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4281 goto failed;
4283 len = PyList_GET_SIZE(tmp);
4284 names = asdl_seq_new(len, arena);
4285 if (names == NULL) goto failed;
4286 for (i = 0; i < len; i++) {
4287 alias_ty value;
4288 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4289 if (res != 0) goto failed;
4290 asdl_seq_SET(names, i, value);
4292 Py_XDECREF(tmp);
4293 tmp = NULL;
4294 } else {
4295 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4296 return 1;
4298 if (PyObject_HasAttrString(obj, "level")) {
4299 int res;
4300 tmp = PyObject_GetAttrString(obj, "level");
4301 if (tmp == NULL) goto failed;
4302 res = obj2ast_int(tmp, &level, arena);
4303 if (res != 0) goto failed;
4304 Py_XDECREF(tmp);
4305 tmp = NULL;
4306 } else {
4307 level = 0;
4309 *out = ImportFrom(module, names, level, lineno, col_offset,
4310 arena);
4311 if (*out == NULL) goto failed;
4312 return 0;
4314 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4315 expr_ty body;
4316 expr_ty globals;
4317 expr_ty locals;
4319 if (PyObject_HasAttrString(obj, "body")) {
4320 int res;
4321 tmp = PyObject_GetAttrString(obj, "body");
4322 if (tmp == NULL) goto failed;
4323 res = obj2ast_expr(tmp, &body, arena);
4324 if (res != 0) goto failed;
4325 Py_XDECREF(tmp);
4326 tmp = NULL;
4327 } else {
4328 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4329 return 1;
4331 if (PyObject_HasAttrString(obj, "globals")) {
4332 int res;
4333 tmp = PyObject_GetAttrString(obj, "globals");
4334 if (tmp == NULL) goto failed;
4335 res = obj2ast_expr(tmp, &globals, arena);
4336 if (res != 0) goto failed;
4337 Py_XDECREF(tmp);
4338 tmp = NULL;
4339 } else {
4340 globals = NULL;
4342 if (PyObject_HasAttrString(obj, "locals")) {
4343 int res;
4344 tmp = PyObject_GetAttrString(obj, "locals");
4345 if (tmp == NULL) goto failed;
4346 res = obj2ast_expr(tmp, &locals, arena);
4347 if (res != 0) goto failed;
4348 Py_XDECREF(tmp);
4349 tmp = NULL;
4350 } else {
4351 locals = NULL;
4353 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4354 if (*out == NULL) goto failed;
4355 return 0;
4357 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4358 asdl_seq* names;
4360 if (PyObject_HasAttrString(obj, "names")) {
4361 int res;
4362 Py_ssize_t len;
4363 Py_ssize_t i;
4364 tmp = PyObject_GetAttrString(obj, "names");
4365 if (tmp == NULL) goto failed;
4366 if (!PyList_Check(tmp)) {
4367 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4368 goto failed;
4370 len = PyList_GET_SIZE(tmp);
4371 names = asdl_seq_new(len, arena);
4372 if (names == NULL) goto failed;
4373 for (i = 0; i < len; i++) {
4374 identifier value;
4375 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4376 if (res != 0) goto failed;
4377 asdl_seq_SET(names, i, value);
4379 Py_XDECREF(tmp);
4380 tmp = NULL;
4381 } else {
4382 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4383 return 1;
4385 *out = Global(names, lineno, col_offset, arena);
4386 if (*out == NULL) goto failed;
4387 return 0;
4389 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4390 expr_ty value;
4392 if (PyObject_HasAttrString(obj, "value")) {
4393 int res;
4394 tmp = PyObject_GetAttrString(obj, "value");
4395 if (tmp == NULL) goto failed;
4396 res = obj2ast_expr(tmp, &value, arena);
4397 if (res != 0) goto failed;
4398 Py_XDECREF(tmp);
4399 tmp = NULL;
4400 } else {
4401 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4402 return 1;
4404 *out = Expr(value, lineno, col_offset, arena);
4405 if (*out == NULL) goto failed;
4406 return 0;
4408 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4410 *out = Pass(lineno, col_offset, arena);
4411 if (*out == NULL) goto failed;
4412 return 0;
4414 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4416 *out = Break(lineno, col_offset, arena);
4417 if (*out == NULL) goto failed;
4418 return 0;
4420 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4422 *out = Continue(lineno, col_offset, arena);
4423 if (*out == NULL) goto failed;
4424 return 0;
4427 tmp = PyObject_Repr(obj);
4428 if (tmp == NULL) goto failed;
4429 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4430 failed:
4431 Py_XDECREF(tmp);
4432 return 1;
4436 obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4438 PyObject* tmp = NULL;
4440 int lineno;
4441 int col_offset;
4443 if (obj == Py_None) {
4444 *out = NULL;
4445 return 0;
4447 if (PyObject_HasAttrString(obj, "lineno")) {
4448 int res;
4449 tmp = PyObject_GetAttrString(obj, "lineno");
4450 if (tmp == NULL) goto failed;
4451 res = obj2ast_int(tmp, &lineno, arena);
4452 if (res != 0) goto failed;
4453 Py_XDECREF(tmp);
4454 tmp = NULL;
4455 } else {
4456 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4457 return 1;
4459 if (PyObject_HasAttrString(obj, "col_offset")) {
4460 int res;
4461 tmp = PyObject_GetAttrString(obj, "col_offset");
4462 if (tmp == NULL) goto failed;
4463 res = obj2ast_int(tmp, &col_offset, arena);
4464 if (res != 0) goto failed;
4465 Py_XDECREF(tmp);
4466 tmp = NULL;
4467 } else {
4468 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4469 return 1;
4471 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4472 boolop_ty op;
4473 asdl_seq* values;
4475 if (PyObject_HasAttrString(obj, "op")) {
4476 int res;
4477 tmp = PyObject_GetAttrString(obj, "op");
4478 if (tmp == NULL) goto failed;
4479 res = obj2ast_boolop(tmp, &op, arena);
4480 if (res != 0) goto failed;
4481 Py_XDECREF(tmp);
4482 tmp = NULL;
4483 } else {
4484 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4485 return 1;
4487 if (PyObject_HasAttrString(obj, "values")) {
4488 int res;
4489 Py_ssize_t len;
4490 Py_ssize_t i;
4491 tmp = PyObject_GetAttrString(obj, "values");
4492 if (tmp == NULL) goto failed;
4493 if (!PyList_Check(tmp)) {
4494 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4495 goto failed;
4497 len = PyList_GET_SIZE(tmp);
4498 values = asdl_seq_new(len, arena);
4499 if (values == NULL) goto failed;
4500 for (i = 0; i < len; i++) {
4501 expr_ty value;
4502 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4503 if (res != 0) goto failed;
4504 asdl_seq_SET(values, i, value);
4506 Py_XDECREF(tmp);
4507 tmp = NULL;
4508 } else {
4509 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4510 return 1;
4512 *out = BoolOp(op, values, lineno, col_offset, arena);
4513 if (*out == NULL) goto failed;
4514 return 0;
4516 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4517 expr_ty left;
4518 operator_ty op;
4519 expr_ty right;
4521 if (PyObject_HasAttrString(obj, "left")) {
4522 int res;
4523 tmp = PyObject_GetAttrString(obj, "left");
4524 if (tmp == NULL) goto failed;
4525 res = obj2ast_expr(tmp, &left, arena);
4526 if (res != 0) goto failed;
4527 Py_XDECREF(tmp);
4528 tmp = NULL;
4529 } else {
4530 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4531 return 1;
4533 if (PyObject_HasAttrString(obj, "op")) {
4534 int res;
4535 tmp = PyObject_GetAttrString(obj, "op");
4536 if (tmp == NULL) goto failed;
4537 res = obj2ast_operator(tmp, &op, arena);
4538 if (res != 0) goto failed;
4539 Py_XDECREF(tmp);
4540 tmp = NULL;
4541 } else {
4542 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4543 return 1;
4545 if (PyObject_HasAttrString(obj, "right")) {
4546 int res;
4547 tmp = PyObject_GetAttrString(obj, "right");
4548 if (tmp == NULL) goto failed;
4549 res = obj2ast_expr(tmp, &right, arena);
4550 if (res != 0) goto failed;
4551 Py_XDECREF(tmp);
4552 tmp = NULL;
4553 } else {
4554 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4555 return 1;
4557 *out = BinOp(left, op, right, lineno, col_offset, arena);
4558 if (*out == NULL) goto failed;
4559 return 0;
4561 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4562 unaryop_ty op;
4563 expr_ty operand;
4565 if (PyObject_HasAttrString(obj, "op")) {
4566 int res;
4567 tmp = PyObject_GetAttrString(obj, "op");
4568 if (tmp == NULL) goto failed;
4569 res = obj2ast_unaryop(tmp, &op, arena);
4570 if (res != 0) goto failed;
4571 Py_XDECREF(tmp);
4572 tmp = NULL;
4573 } else {
4574 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4575 return 1;
4577 if (PyObject_HasAttrString(obj, "operand")) {
4578 int res;
4579 tmp = PyObject_GetAttrString(obj, "operand");
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_expr(tmp, &operand, arena);
4582 if (res != 0) goto failed;
4583 Py_XDECREF(tmp);
4584 tmp = NULL;
4585 } else {
4586 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4587 return 1;
4589 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4590 if (*out == NULL) goto failed;
4591 return 0;
4593 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4594 arguments_ty args;
4595 expr_ty body;
4597 if (PyObject_HasAttrString(obj, "args")) {
4598 int res;
4599 tmp = PyObject_GetAttrString(obj, "args");
4600 if (tmp == NULL) goto failed;
4601 res = obj2ast_arguments(tmp, &args, arena);
4602 if (res != 0) goto failed;
4603 Py_XDECREF(tmp);
4604 tmp = NULL;
4605 } else {
4606 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4607 return 1;
4609 if (PyObject_HasAttrString(obj, "body")) {
4610 int res;
4611 tmp = PyObject_GetAttrString(obj, "body");
4612 if (tmp == NULL) goto failed;
4613 res = obj2ast_expr(tmp, &body, arena);
4614 if (res != 0) goto failed;
4615 Py_XDECREF(tmp);
4616 tmp = NULL;
4617 } else {
4618 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4619 return 1;
4621 *out = Lambda(args, body, lineno, col_offset, arena);
4622 if (*out == NULL) goto failed;
4623 return 0;
4625 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4626 expr_ty test;
4627 expr_ty body;
4628 expr_ty orelse;
4630 if (PyObject_HasAttrString(obj, "test")) {
4631 int res;
4632 tmp = PyObject_GetAttrString(obj, "test");
4633 if (tmp == NULL) goto failed;
4634 res = obj2ast_expr(tmp, &test, arena);
4635 if (res != 0) goto failed;
4636 Py_XDECREF(tmp);
4637 tmp = NULL;
4638 } else {
4639 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4640 return 1;
4642 if (PyObject_HasAttrString(obj, "body")) {
4643 int res;
4644 tmp = PyObject_GetAttrString(obj, "body");
4645 if (tmp == NULL) goto failed;
4646 res = obj2ast_expr(tmp, &body, arena);
4647 if (res != 0) goto failed;
4648 Py_XDECREF(tmp);
4649 tmp = NULL;
4650 } else {
4651 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4652 return 1;
4654 if (PyObject_HasAttrString(obj, "orelse")) {
4655 int res;
4656 tmp = PyObject_GetAttrString(obj, "orelse");
4657 if (tmp == NULL) goto failed;
4658 res = obj2ast_expr(tmp, &orelse, arena);
4659 if (res != 0) goto failed;
4660 Py_XDECREF(tmp);
4661 tmp = NULL;
4662 } else {
4663 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4664 return 1;
4666 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4667 if (*out == NULL) goto failed;
4668 return 0;
4670 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4671 asdl_seq* keys;
4672 asdl_seq* values;
4674 if (PyObject_HasAttrString(obj, "keys")) {
4675 int res;
4676 Py_ssize_t len;
4677 Py_ssize_t i;
4678 tmp = PyObject_GetAttrString(obj, "keys");
4679 if (tmp == NULL) goto failed;
4680 if (!PyList_Check(tmp)) {
4681 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4682 goto failed;
4684 len = PyList_GET_SIZE(tmp);
4685 keys = asdl_seq_new(len, arena);
4686 if (keys == NULL) goto failed;
4687 for (i = 0; i < len; i++) {
4688 expr_ty value;
4689 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4690 if (res != 0) goto failed;
4691 asdl_seq_SET(keys, i, value);
4693 Py_XDECREF(tmp);
4694 tmp = NULL;
4695 } else {
4696 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4697 return 1;
4699 if (PyObject_HasAttrString(obj, "values")) {
4700 int res;
4701 Py_ssize_t len;
4702 Py_ssize_t i;
4703 tmp = PyObject_GetAttrString(obj, "values");
4704 if (tmp == NULL) goto failed;
4705 if (!PyList_Check(tmp)) {
4706 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4707 goto failed;
4709 len = PyList_GET_SIZE(tmp);
4710 values = asdl_seq_new(len, arena);
4711 if (values == NULL) goto failed;
4712 for (i = 0; i < len; i++) {
4713 expr_ty value;
4714 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4715 if (res != 0) goto failed;
4716 asdl_seq_SET(values, i, value);
4718 Py_XDECREF(tmp);
4719 tmp = NULL;
4720 } else {
4721 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4722 return 1;
4724 *out = Dict(keys, values, lineno, col_offset, arena);
4725 if (*out == NULL) goto failed;
4726 return 0;
4728 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4729 expr_ty elt;
4730 asdl_seq* generators;
4732 if (PyObject_HasAttrString(obj, "elt")) {
4733 int res;
4734 tmp = PyObject_GetAttrString(obj, "elt");
4735 if (tmp == NULL) goto failed;
4736 res = obj2ast_expr(tmp, &elt, arena);
4737 if (res != 0) goto failed;
4738 Py_XDECREF(tmp);
4739 tmp = NULL;
4740 } else {
4741 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4742 return 1;
4744 if (PyObject_HasAttrString(obj, "generators")) {
4745 int res;
4746 Py_ssize_t len;
4747 Py_ssize_t i;
4748 tmp = PyObject_GetAttrString(obj, "generators");
4749 if (tmp == NULL) goto failed;
4750 if (!PyList_Check(tmp)) {
4751 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4752 goto failed;
4754 len = PyList_GET_SIZE(tmp);
4755 generators = asdl_seq_new(len, arena);
4756 if (generators == NULL) goto failed;
4757 for (i = 0; i < len; i++) {
4758 comprehension_ty value;
4759 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4760 if (res != 0) goto failed;
4761 asdl_seq_SET(generators, i, value);
4763 Py_XDECREF(tmp);
4764 tmp = NULL;
4765 } else {
4766 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4767 return 1;
4769 *out = ListComp(elt, generators, lineno, col_offset, arena);
4770 if (*out == NULL) goto failed;
4771 return 0;
4773 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4774 expr_ty elt;
4775 asdl_seq* generators;
4777 if (PyObject_HasAttrString(obj, "elt")) {
4778 int res;
4779 tmp = PyObject_GetAttrString(obj, "elt");
4780 if (tmp == NULL) goto failed;
4781 res = obj2ast_expr(tmp, &elt, arena);
4782 if (res != 0) goto failed;
4783 Py_XDECREF(tmp);
4784 tmp = NULL;
4785 } else {
4786 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4787 return 1;
4789 if (PyObject_HasAttrString(obj, "generators")) {
4790 int res;
4791 Py_ssize_t len;
4792 Py_ssize_t i;
4793 tmp = PyObject_GetAttrString(obj, "generators");
4794 if (tmp == NULL) goto failed;
4795 if (!PyList_Check(tmp)) {
4796 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4797 goto failed;
4799 len = PyList_GET_SIZE(tmp);
4800 generators = asdl_seq_new(len, arena);
4801 if (generators == NULL) goto failed;
4802 for (i = 0; i < len; i++) {
4803 comprehension_ty value;
4804 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4805 if (res != 0) goto failed;
4806 asdl_seq_SET(generators, i, value);
4808 Py_XDECREF(tmp);
4809 tmp = NULL;
4810 } else {
4811 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4812 return 1;
4814 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4815 if (*out == NULL) goto failed;
4816 return 0;
4818 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4819 expr_ty value;
4821 if (PyObject_HasAttrString(obj, "value")) {
4822 int res;
4823 tmp = PyObject_GetAttrString(obj, "value");
4824 if (tmp == NULL) goto failed;
4825 res = obj2ast_expr(tmp, &value, arena);
4826 if (res != 0) goto failed;
4827 Py_XDECREF(tmp);
4828 tmp = NULL;
4829 } else {
4830 value = NULL;
4832 *out = Yield(value, lineno, col_offset, arena);
4833 if (*out == NULL) goto failed;
4834 return 0;
4836 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4837 expr_ty left;
4838 asdl_int_seq* ops;
4839 asdl_seq* comparators;
4841 if (PyObject_HasAttrString(obj, "left")) {
4842 int res;
4843 tmp = PyObject_GetAttrString(obj, "left");
4844 if (tmp == NULL) goto failed;
4845 res = obj2ast_expr(tmp, &left, arena);
4846 if (res != 0) goto failed;
4847 Py_XDECREF(tmp);
4848 tmp = NULL;
4849 } else {
4850 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4851 return 1;
4853 if (PyObject_HasAttrString(obj, "ops")) {
4854 int res;
4855 Py_ssize_t len;
4856 Py_ssize_t i;
4857 tmp = PyObject_GetAttrString(obj, "ops");
4858 if (tmp == NULL) goto failed;
4859 if (!PyList_Check(tmp)) {
4860 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4861 goto failed;
4863 len = PyList_GET_SIZE(tmp);
4864 ops = asdl_int_seq_new(len, arena);
4865 if (ops == NULL) goto failed;
4866 for (i = 0; i < len; i++) {
4867 cmpop_ty value;
4868 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4869 if (res != 0) goto failed;
4870 asdl_seq_SET(ops, i, value);
4872 Py_XDECREF(tmp);
4873 tmp = NULL;
4874 } else {
4875 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4876 return 1;
4878 if (PyObject_HasAttrString(obj, "comparators")) {
4879 int res;
4880 Py_ssize_t len;
4881 Py_ssize_t i;
4882 tmp = PyObject_GetAttrString(obj, "comparators");
4883 if (tmp == NULL) goto failed;
4884 if (!PyList_Check(tmp)) {
4885 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4886 goto failed;
4888 len = PyList_GET_SIZE(tmp);
4889 comparators = asdl_seq_new(len, arena);
4890 if (comparators == NULL) goto failed;
4891 for (i = 0; i < len; i++) {
4892 expr_ty value;
4893 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4894 if (res != 0) goto failed;
4895 asdl_seq_SET(comparators, i, value);
4897 Py_XDECREF(tmp);
4898 tmp = NULL;
4899 } else {
4900 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4901 return 1;
4903 *out = Compare(left, ops, comparators, lineno, col_offset,
4904 arena);
4905 if (*out == NULL) goto failed;
4906 return 0;
4908 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4909 expr_ty func;
4910 asdl_seq* args;
4911 asdl_seq* keywords;
4912 expr_ty starargs;
4913 expr_ty kwargs;
4915 if (PyObject_HasAttrString(obj, "func")) {
4916 int res;
4917 tmp = PyObject_GetAttrString(obj, "func");
4918 if (tmp == NULL) goto failed;
4919 res = obj2ast_expr(tmp, &func, arena);
4920 if (res != 0) goto failed;
4921 Py_XDECREF(tmp);
4922 tmp = NULL;
4923 } else {
4924 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4925 return 1;
4927 if (PyObject_HasAttrString(obj, "args")) {
4928 int res;
4929 Py_ssize_t len;
4930 Py_ssize_t i;
4931 tmp = PyObject_GetAttrString(obj, "args");
4932 if (tmp == NULL) goto failed;
4933 if (!PyList_Check(tmp)) {
4934 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4935 goto failed;
4937 len = PyList_GET_SIZE(tmp);
4938 args = asdl_seq_new(len, arena);
4939 if (args == NULL) goto failed;
4940 for (i = 0; i < len; i++) {
4941 expr_ty value;
4942 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4943 if (res != 0) goto failed;
4944 asdl_seq_SET(args, i, value);
4946 Py_XDECREF(tmp);
4947 tmp = NULL;
4948 } else {
4949 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4950 return 1;
4952 if (PyObject_HasAttrString(obj, "keywords")) {
4953 int res;
4954 Py_ssize_t len;
4955 Py_ssize_t i;
4956 tmp = PyObject_GetAttrString(obj, "keywords");
4957 if (tmp == NULL) goto failed;
4958 if (!PyList_Check(tmp)) {
4959 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4960 goto failed;
4962 len = PyList_GET_SIZE(tmp);
4963 keywords = asdl_seq_new(len, arena);
4964 if (keywords == NULL) goto failed;
4965 for (i = 0; i < len; i++) {
4966 keyword_ty value;
4967 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4968 if (res != 0) goto failed;
4969 asdl_seq_SET(keywords, i, value);
4971 Py_XDECREF(tmp);
4972 tmp = NULL;
4973 } else {
4974 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4975 return 1;
4977 if (PyObject_HasAttrString(obj, "starargs")) {
4978 int res;
4979 tmp = PyObject_GetAttrString(obj, "starargs");
4980 if (tmp == NULL) goto failed;
4981 res = obj2ast_expr(tmp, &starargs, arena);
4982 if (res != 0) goto failed;
4983 Py_XDECREF(tmp);
4984 tmp = NULL;
4985 } else {
4986 starargs = NULL;
4988 if (PyObject_HasAttrString(obj, "kwargs")) {
4989 int res;
4990 tmp = PyObject_GetAttrString(obj, "kwargs");
4991 if (tmp == NULL) goto failed;
4992 res = obj2ast_expr(tmp, &kwargs, arena);
4993 if (res != 0) goto failed;
4994 Py_XDECREF(tmp);
4995 tmp = NULL;
4996 } else {
4997 kwargs = NULL;
4999 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5000 col_offset, arena);
5001 if (*out == NULL) goto failed;
5002 return 0;
5004 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
5005 expr_ty value;
5007 if (PyObject_HasAttrString(obj, "value")) {
5008 int res;
5009 tmp = PyObject_GetAttrString(obj, "value");
5010 if (tmp == NULL) goto failed;
5011 res = obj2ast_expr(tmp, &value, arena);
5012 if (res != 0) goto failed;
5013 Py_XDECREF(tmp);
5014 tmp = NULL;
5015 } else {
5016 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5017 return 1;
5019 *out = Repr(value, lineno, col_offset, arena);
5020 if (*out == NULL) goto failed;
5021 return 0;
5023 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5024 object n;
5026 if (PyObject_HasAttrString(obj, "n")) {
5027 int res;
5028 tmp = PyObject_GetAttrString(obj, "n");
5029 if (tmp == NULL) goto failed;
5030 res = obj2ast_object(tmp, &n, arena);
5031 if (res != 0) goto failed;
5032 Py_XDECREF(tmp);
5033 tmp = NULL;
5034 } else {
5035 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5036 return 1;
5038 *out = Num(n, lineno, col_offset, arena);
5039 if (*out == NULL) goto failed;
5040 return 0;
5042 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5043 string s;
5045 if (PyObject_HasAttrString(obj, "s")) {
5046 int res;
5047 tmp = PyObject_GetAttrString(obj, "s");
5048 if (tmp == NULL) goto failed;
5049 res = obj2ast_string(tmp, &s, arena);
5050 if (res != 0) goto failed;
5051 Py_XDECREF(tmp);
5052 tmp = NULL;
5053 } else {
5054 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5055 return 1;
5057 *out = Str(s, lineno, col_offset, arena);
5058 if (*out == NULL) goto failed;
5059 return 0;
5061 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5062 expr_ty value;
5063 identifier attr;
5064 expr_context_ty ctx;
5066 if (PyObject_HasAttrString(obj, "value")) {
5067 int res;
5068 tmp = PyObject_GetAttrString(obj, "value");
5069 if (tmp == NULL) goto failed;
5070 res = obj2ast_expr(tmp, &value, arena);
5071 if (res != 0) goto failed;
5072 Py_XDECREF(tmp);
5073 tmp = NULL;
5074 } else {
5075 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5076 return 1;
5078 if (PyObject_HasAttrString(obj, "attr")) {
5079 int res;
5080 tmp = PyObject_GetAttrString(obj, "attr");
5081 if (tmp == NULL) goto failed;
5082 res = obj2ast_identifier(tmp, &attr, arena);
5083 if (res != 0) goto failed;
5084 Py_XDECREF(tmp);
5085 tmp = NULL;
5086 } else {
5087 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5088 return 1;
5090 if (PyObject_HasAttrString(obj, "ctx")) {
5091 int res;
5092 tmp = PyObject_GetAttrString(obj, "ctx");
5093 if (tmp == NULL) goto failed;
5094 res = obj2ast_expr_context(tmp, &ctx, arena);
5095 if (res != 0) goto failed;
5096 Py_XDECREF(tmp);
5097 tmp = NULL;
5098 } else {
5099 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5100 return 1;
5102 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5103 if (*out == NULL) goto failed;
5104 return 0;
5106 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5107 expr_ty value;
5108 slice_ty slice;
5109 expr_context_ty ctx;
5111 if (PyObject_HasAttrString(obj, "value")) {
5112 int res;
5113 tmp = PyObject_GetAttrString(obj, "value");
5114 if (tmp == NULL) goto failed;
5115 res = obj2ast_expr(tmp, &value, arena);
5116 if (res != 0) goto failed;
5117 Py_XDECREF(tmp);
5118 tmp = NULL;
5119 } else {
5120 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5121 return 1;
5123 if (PyObject_HasAttrString(obj, "slice")) {
5124 int res;
5125 tmp = PyObject_GetAttrString(obj, "slice");
5126 if (tmp == NULL) goto failed;
5127 res = obj2ast_slice(tmp, &slice, arena);
5128 if (res != 0) goto failed;
5129 Py_XDECREF(tmp);
5130 tmp = NULL;
5131 } else {
5132 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5133 return 1;
5135 if (PyObject_HasAttrString(obj, "ctx")) {
5136 int res;
5137 tmp = PyObject_GetAttrString(obj, "ctx");
5138 if (tmp == NULL) goto failed;
5139 res = obj2ast_expr_context(tmp, &ctx, arena);
5140 if (res != 0) goto failed;
5141 Py_XDECREF(tmp);
5142 tmp = NULL;
5143 } else {
5144 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5145 return 1;
5147 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5148 if (*out == NULL) goto failed;
5149 return 0;
5151 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5152 identifier id;
5153 expr_context_ty ctx;
5155 if (PyObject_HasAttrString(obj, "id")) {
5156 int res;
5157 tmp = PyObject_GetAttrString(obj, "id");
5158 if (tmp == NULL) goto failed;
5159 res = obj2ast_identifier(tmp, &id, arena);
5160 if (res != 0) goto failed;
5161 Py_XDECREF(tmp);
5162 tmp = NULL;
5163 } else {
5164 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5165 return 1;
5167 if (PyObject_HasAttrString(obj, "ctx")) {
5168 int res;
5169 tmp = PyObject_GetAttrString(obj, "ctx");
5170 if (tmp == NULL) goto failed;
5171 res = obj2ast_expr_context(tmp, &ctx, arena);
5172 if (res != 0) goto failed;
5173 Py_XDECREF(tmp);
5174 tmp = NULL;
5175 } else {
5176 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5177 return 1;
5179 *out = Name(id, ctx, lineno, col_offset, arena);
5180 if (*out == NULL) goto failed;
5181 return 0;
5183 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5184 asdl_seq* elts;
5185 expr_context_ty ctx;
5187 if (PyObject_HasAttrString(obj, "elts")) {
5188 int res;
5189 Py_ssize_t len;
5190 Py_ssize_t i;
5191 tmp = PyObject_GetAttrString(obj, "elts");
5192 if (tmp == NULL) goto failed;
5193 if (!PyList_Check(tmp)) {
5194 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5195 goto failed;
5197 len = PyList_GET_SIZE(tmp);
5198 elts = asdl_seq_new(len, arena);
5199 if (elts == NULL) goto failed;
5200 for (i = 0; i < len; i++) {
5201 expr_ty value;
5202 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5203 if (res != 0) goto failed;
5204 asdl_seq_SET(elts, i, value);
5206 Py_XDECREF(tmp);
5207 tmp = NULL;
5208 } else {
5209 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5210 return 1;
5212 if (PyObject_HasAttrString(obj, "ctx")) {
5213 int res;
5214 tmp = PyObject_GetAttrString(obj, "ctx");
5215 if (tmp == NULL) goto failed;
5216 res = obj2ast_expr_context(tmp, &ctx, arena);
5217 if (res != 0) goto failed;
5218 Py_XDECREF(tmp);
5219 tmp = NULL;
5220 } else {
5221 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5222 return 1;
5224 *out = List(elts, ctx, lineno, col_offset, arena);
5225 if (*out == NULL) goto failed;
5226 return 0;
5228 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5229 asdl_seq* elts;
5230 expr_context_ty ctx;
5232 if (PyObject_HasAttrString(obj, "elts")) {
5233 int res;
5234 Py_ssize_t len;
5235 Py_ssize_t i;
5236 tmp = PyObject_GetAttrString(obj, "elts");
5237 if (tmp == NULL) goto failed;
5238 if (!PyList_Check(tmp)) {
5239 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5240 goto failed;
5242 len = PyList_GET_SIZE(tmp);
5243 elts = asdl_seq_new(len, arena);
5244 if (elts == NULL) goto failed;
5245 for (i = 0; i < len; i++) {
5246 expr_ty value;
5247 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5248 if (res != 0) goto failed;
5249 asdl_seq_SET(elts, i, value);
5251 Py_XDECREF(tmp);
5252 tmp = NULL;
5253 } else {
5254 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5255 return 1;
5257 if (PyObject_HasAttrString(obj, "ctx")) {
5258 int res;
5259 tmp = PyObject_GetAttrString(obj, "ctx");
5260 if (tmp == NULL) goto failed;
5261 res = obj2ast_expr_context(tmp, &ctx, arena);
5262 if (res != 0) goto failed;
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5267 return 1;
5269 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5270 if (*out == NULL) goto failed;
5271 return 0;
5274 tmp = PyObject_Repr(obj);
5275 if (tmp == NULL) goto failed;
5276 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5277 failed:
5278 Py_XDECREF(tmp);
5279 return 1;
5283 obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5285 PyObject* tmp = NULL;
5287 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5288 *out = Load;
5289 return 0;
5291 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5292 *out = Store;
5293 return 0;
5295 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5296 *out = Del;
5297 return 0;
5299 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5300 *out = AugLoad;
5301 return 0;
5303 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5304 *out = AugStore;
5305 return 0;
5307 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5308 *out = Param;
5309 return 0;
5312 tmp = PyObject_Repr(obj);
5313 if (tmp == NULL) goto failed;
5314 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5315 failed:
5316 Py_XDECREF(tmp);
5317 return 1;
5321 obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5323 PyObject* tmp = NULL;
5326 if (obj == Py_None) {
5327 *out = NULL;
5328 return 0;
5330 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5332 *out = Ellipsis(arena);
5333 if (*out == NULL) goto failed;
5334 return 0;
5336 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5337 expr_ty lower;
5338 expr_ty upper;
5339 expr_ty step;
5341 if (PyObject_HasAttrString(obj, "lower")) {
5342 int res;
5343 tmp = PyObject_GetAttrString(obj, "lower");
5344 if (tmp == NULL) goto failed;
5345 res = obj2ast_expr(tmp, &lower, arena);
5346 if (res != 0) goto failed;
5347 Py_XDECREF(tmp);
5348 tmp = NULL;
5349 } else {
5350 lower = NULL;
5352 if (PyObject_HasAttrString(obj, "upper")) {
5353 int res;
5354 tmp = PyObject_GetAttrString(obj, "upper");
5355 if (tmp == NULL) goto failed;
5356 res = obj2ast_expr(tmp, &upper, arena);
5357 if (res != 0) goto failed;
5358 Py_XDECREF(tmp);
5359 tmp = NULL;
5360 } else {
5361 upper = NULL;
5363 if (PyObject_HasAttrString(obj, "step")) {
5364 int res;
5365 tmp = PyObject_GetAttrString(obj, "step");
5366 if (tmp == NULL) goto failed;
5367 res = obj2ast_expr(tmp, &step, arena);
5368 if (res != 0) goto failed;
5369 Py_XDECREF(tmp);
5370 tmp = NULL;
5371 } else {
5372 step = NULL;
5374 *out = Slice(lower, upper, step, arena);
5375 if (*out == NULL) goto failed;
5376 return 0;
5378 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5379 asdl_seq* dims;
5381 if (PyObject_HasAttrString(obj, "dims")) {
5382 int res;
5383 Py_ssize_t len;
5384 Py_ssize_t i;
5385 tmp = PyObject_GetAttrString(obj, "dims");
5386 if (tmp == NULL) goto failed;
5387 if (!PyList_Check(tmp)) {
5388 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5389 goto failed;
5391 len = PyList_GET_SIZE(tmp);
5392 dims = asdl_seq_new(len, arena);
5393 if (dims == NULL) goto failed;
5394 for (i = 0; i < len; i++) {
5395 slice_ty value;
5396 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5397 if (res != 0) goto failed;
5398 asdl_seq_SET(dims, i, value);
5400 Py_XDECREF(tmp);
5401 tmp = NULL;
5402 } else {
5403 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5404 return 1;
5406 *out = ExtSlice(dims, arena);
5407 if (*out == NULL) goto failed;
5408 return 0;
5410 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5411 expr_ty value;
5413 if (PyObject_HasAttrString(obj, "value")) {
5414 int res;
5415 tmp = PyObject_GetAttrString(obj, "value");
5416 if (tmp == NULL) goto failed;
5417 res = obj2ast_expr(tmp, &value, arena);
5418 if (res != 0) goto failed;
5419 Py_XDECREF(tmp);
5420 tmp = NULL;
5421 } else {
5422 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5423 return 1;
5425 *out = Index(value, arena);
5426 if (*out == NULL) goto failed;
5427 return 0;
5430 tmp = PyObject_Repr(obj);
5431 if (tmp == NULL) goto failed;
5432 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5433 failed:
5434 Py_XDECREF(tmp);
5435 return 1;
5439 obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5441 PyObject* tmp = NULL;
5443 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5444 *out = And;
5445 return 0;
5447 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5448 *out = Or;
5449 return 0;
5452 tmp = PyObject_Repr(obj);
5453 if (tmp == NULL) goto failed;
5454 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5455 failed:
5456 Py_XDECREF(tmp);
5457 return 1;
5461 obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5463 PyObject* tmp = NULL;
5465 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5466 *out = Add;
5467 return 0;
5469 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5470 *out = Sub;
5471 return 0;
5473 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5474 *out = Mult;
5475 return 0;
5477 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5478 *out = Div;
5479 return 0;
5481 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5482 *out = Mod;
5483 return 0;
5485 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5486 *out = Pow;
5487 return 0;
5489 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5490 *out = LShift;
5491 return 0;
5493 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5494 *out = RShift;
5495 return 0;
5497 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5498 *out = BitOr;
5499 return 0;
5501 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5502 *out = BitXor;
5503 return 0;
5505 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5506 *out = BitAnd;
5507 return 0;
5509 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5510 *out = FloorDiv;
5511 return 0;
5514 tmp = PyObject_Repr(obj);
5515 if (tmp == NULL) goto failed;
5516 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5517 failed:
5518 Py_XDECREF(tmp);
5519 return 1;
5523 obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5525 PyObject* tmp = NULL;
5527 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5528 *out = Invert;
5529 return 0;
5531 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5532 *out = Not;
5533 return 0;
5535 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5536 *out = UAdd;
5537 return 0;
5539 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5540 *out = USub;
5541 return 0;
5544 tmp = PyObject_Repr(obj);
5545 if (tmp == NULL) goto failed;
5546 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5547 failed:
5548 Py_XDECREF(tmp);
5549 return 1;
5553 obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5555 PyObject* tmp = NULL;
5557 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5558 *out = Eq;
5559 return 0;
5561 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5562 *out = NotEq;
5563 return 0;
5565 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5566 *out = Lt;
5567 return 0;
5569 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5570 *out = LtE;
5571 return 0;
5573 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5574 *out = Gt;
5575 return 0;
5577 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5578 *out = GtE;
5579 return 0;
5581 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5582 *out = Is;
5583 return 0;
5585 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5586 *out = IsNot;
5587 return 0;
5589 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5590 *out = In;
5591 return 0;
5593 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5594 *out = NotIn;
5595 return 0;
5598 tmp = PyObject_Repr(obj);
5599 if (tmp == NULL) goto failed;
5600 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5601 failed:
5602 Py_XDECREF(tmp);
5603 return 1;
5607 obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5609 PyObject* tmp = NULL;
5610 expr_ty target;
5611 expr_ty iter;
5612 asdl_seq* ifs;
5614 if (PyObject_HasAttrString(obj, "target")) {
5615 int res;
5616 tmp = PyObject_GetAttrString(obj, "target");
5617 if (tmp == NULL) goto failed;
5618 res = obj2ast_expr(tmp, &target, arena);
5619 if (res != 0) goto failed;
5620 Py_XDECREF(tmp);
5621 tmp = NULL;
5622 } else {
5623 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5624 return 1;
5626 if (PyObject_HasAttrString(obj, "iter")) {
5627 int res;
5628 tmp = PyObject_GetAttrString(obj, "iter");
5629 if (tmp == NULL) goto failed;
5630 res = obj2ast_expr(tmp, &iter, arena);
5631 if (res != 0) goto failed;
5632 Py_XDECREF(tmp);
5633 tmp = NULL;
5634 } else {
5635 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5636 return 1;
5638 if (PyObject_HasAttrString(obj, "ifs")) {
5639 int res;
5640 Py_ssize_t len;
5641 Py_ssize_t i;
5642 tmp = PyObject_GetAttrString(obj, "ifs");
5643 if (tmp == NULL) goto failed;
5644 if (!PyList_Check(tmp)) {
5645 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5646 goto failed;
5648 len = PyList_GET_SIZE(tmp);
5649 ifs = asdl_seq_new(len, arena);
5650 if (ifs == NULL) goto failed;
5651 for (i = 0; i < len; i++) {
5652 expr_ty value;
5653 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5654 if (res != 0) goto failed;
5655 asdl_seq_SET(ifs, i, value);
5657 Py_XDECREF(tmp);
5658 tmp = NULL;
5659 } else {
5660 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5661 return 1;
5663 *out = comprehension(target, iter, ifs, arena);
5664 return 0;
5665 failed:
5666 Py_XDECREF(tmp);
5667 return 1;
5671 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5673 PyObject* tmp = NULL;
5675 int lineno;
5676 int col_offset;
5678 if (obj == Py_None) {
5679 *out = NULL;
5680 return 0;
5682 if (PyObject_HasAttrString(obj, "lineno")) {
5683 int res;
5684 tmp = PyObject_GetAttrString(obj, "lineno");
5685 if (tmp == NULL) goto failed;
5686 res = obj2ast_int(tmp, &lineno, arena);
5687 if (res != 0) goto failed;
5688 Py_XDECREF(tmp);
5689 tmp = NULL;
5690 } else {
5691 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5692 return 1;
5694 if (PyObject_HasAttrString(obj, "col_offset")) {
5695 int res;
5696 tmp = PyObject_GetAttrString(obj, "col_offset");
5697 if (tmp == NULL) goto failed;
5698 res = obj2ast_int(tmp, &col_offset, arena);
5699 if (res != 0) goto failed;
5700 Py_XDECREF(tmp);
5701 tmp = NULL;
5702 } else {
5703 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5704 return 1;
5706 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
5707 expr_ty type;
5708 expr_ty name;
5709 asdl_seq* body;
5711 if (PyObject_HasAttrString(obj, "type")) {
5712 int res;
5713 tmp = PyObject_GetAttrString(obj, "type");
5714 if (tmp == NULL) goto failed;
5715 res = obj2ast_expr(tmp, &type, arena);
5716 if (res != 0) goto failed;
5717 Py_XDECREF(tmp);
5718 tmp = NULL;
5719 } else {
5720 type = NULL;
5722 if (PyObject_HasAttrString(obj, "name")) {
5723 int res;
5724 tmp = PyObject_GetAttrString(obj, "name");
5725 if (tmp == NULL) goto failed;
5726 res = obj2ast_expr(tmp, &name, arena);
5727 if (res != 0) goto failed;
5728 Py_XDECREF(tmp);
5729 tmp = NULL;
5730 } else {
5731 name = NULL;
5733 if (PyObject_HasAttrString(obj, "body")) {
5734 int res;
5735 Py_ssize_t len;
5736 Py_ssize_t i;
5737 tmp = PyObject_GetAttrString(obj, "body");
5738 if (tmp == NULL) goto failed;
5739 if (!PyList_Check(tmp)) {
5740 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5741 goto failed;
5743 len = PyList_GET_SIZE(tmp);
5744 body = asdl_seq_new(len, arena);
5745 if (body == NULL) goto failed;
5746 for (i = 0; i < len; i++) {
5747 stmt_ty value;
5748 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5749 if (res != 0) goto failed;
5750 asdl_seq_SET(body, i, value);
5752 Py_XDECREF(tmp);
5753 tmp = NULL;
5754 } else {
5755 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
5756 return 1;
5758 *out = ExceptHandler(type, name, body, lineno, col_offset,
5759 arena);
5760 if (*out == NULL) goto failed;
5761 return 0;
5764 tmp = PyObject_Repr(obj);
5765 if (tmp == NULL) goto failed;
5766 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
5767 failed:
5768 Py_XDECREF(tmp);
5769 return 1;
5773 obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5775 PyObject* tmp = NULL;
5776 asdl_seq* args;
5777 identifier vararg;
5778 identifier kwarg;
5779 asdl_seq* defaults;
5781 if (PyObject_HasAttrString(obj, "args")) {
5782 int res;
5783 Py_ssize_t len;
5784 Py_ssize_t i;
5785 tmp = PyObject_GetAttrString(obj, "args");
5786 if (tmp == NULL) goto failed;
5787 if (!PyList_Check(tmp)) {
5788 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5789 goto failed;
5791 len = PyList_GET_SIZE(tmp);
5792 args = asdl_seq_new(len, arena);
5793 if (args == NULL) goto failed;
5794 for (i = 0; i < len; i++) {
5795 expr_ty value;
5796 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5797 if (res != 0) goto failed;
5798 asdl_seq_SET(args, i, value);
5800 Py_XDECREF(tmp);
5801 tmp = NULL;
5802 } else {
5803 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5804 return 1;
5806 if (PyObject_HasAttrString(obj, "vararg")) {
5807 int res;
5808 tmp = PyObject_GetAttrString(obj, "vararg");
5809 if (tmp == NULL) goto failed;
5810 res = obj2ast_identifier(tmp, &vararg, arena);
5811 if (res != 0) goto failed;
5812 Py_XDECREF(tmp);
5813 tmp = NULL;
5814 } else {
5815 vararg = NULL;
5817 if (PyObject_HasAttrString(obj, "kwarg")) {
5818 int res;
5819 tmp = PyObject_GetAttrString(obj, "kwarg");
5820 if (tmp == NULL) goto failed;
5821 res = obj2ast_identifier(tmp, &kwarg, arena);
5822 if (res != 0) goto failed;
5823 Py_XDECREF(tmp);
5824 tmp = NULL;
5825 } else {
5826 kwarg = NULL;
5828 if (PyObject_HasAttrString(obj, "defaults")) {
5829 int res;
5830 Py_ssize_t len;
5831 Py_ssize_t i;
5832 tmp = PyObject_GetAttrString(obj, "defaults");
5833 if (tmp == NULL) goto failed;
5834 if (!PyList_Check(tmp)) {
5835 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5836 goto failed;
5838 len = PyList_GET_SIZE(tmp);
5839 defaults = asdl_seq_new(len, arena);
5840 if (defaults == NULL) goto failed;
5841 for (i = 0; i < len; i++) {
5842 expr_ty value;
5843 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5844 if (res != 0) goto failed;
5845 asdl_seq_SET(defaults, i, value);
5847 Py_XDECREF(tmp);
5848 tmp = NULL;
5849 } else {
5850 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5851 return 1;
5853 *out = arguments(args, vararg, kwarg, defaults, arena);
5854 return 0;
5855 failed:
5856 Py_XDECREF(tmp);
5857 return 1;
5861 obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5863 PyObject* tmp = NULL;
5864 identifier arg;
5865 expr_ty value;
5867 if (PyObject_HasAttrString(obj, "arg")) {
5868 int res;
5869 tmp = PyObject_GetAttrString(obj, "arg");
5870 if (tmp == NULL) goto failed;
5871 res = obj2ast_identifier(tmp, &arg, arena);
5872 if (res != 0) goto failed;
5873 Py_XDECREF(tmp);
5874 tmp = NULL;
5875 } else {
5876 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5877 return 1;
5879 if (PyObject_HasAttrString(obj, "value")) {
5880 int res;
5881 tmp = PyObject_GetAttrString(obj, "value");
5882 if (tmp == NULL) goto failed;
5883 res = obj2ast_expr(tmp, &value, arena);
5884 if (res != 0) goto failed;
5885 Py_XDECREF(tmp);
5886 tmp = NULL;
5887 } else {
5888 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5889 return 1;
5891 *out = keyword(arg, value, arena);
5892 return 0;
5893 failed:
5894 Py_XDECREF(tmp);
5895 return 1;
5899 obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5901 PyObject* tmp = NULL;
5902 identifier name;
5903 identifier asname;
5905 if (PyObject_HasAttrString(obj, "name")) {
5906 int res;
5907 tmp = PyObject_GetAttrString(obj, "name");
5908 if (tmp == NULL) goto failed;
5909 res = obj2ast_identifier(tmp, &name, arena);
5910 if (res != 0) goto failed;
5911 Py_XDECREF(tmp);
5912 tmp = NULL;
5913 } else {
5914 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5915 return 1;
5917 if (PyObject_HasAttrString(obj, "asname")) {
5918 int res;
5919 tmp = PyObject_GetAttrString(obj, "asname");
5920 if (tmp == NULL) goto failed;
5921 res = obj2ast_identifier(tmp, &asname, arena);
5922 if (res != 0) goto failed;
5923 Py_XDECREF(tmp);
5924 tmp = NULL;
5925 } else {
5926 asname = NULL;
5928 *out = alias(name, asname, arena);
5929 return 0;
5930 failed:
5931 Py_XDECREF(tmp);
5932 return 1;
5936 PyMODINIT_FUNC
5937 init_ast(void)
5939 PyObject *m, *d;
5940 if (!init_types()) return;
5941 m = Py_InitModule3("_ast", NULL, NULL);
5942 if (!m) return;
5943 d = PyModule_GetDict(m);
5944 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
5945 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5946 return;
5947 if (PyModule_AddStringConstant(m, "__version__", "73421") < 0)
5948 return;
5949 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5950 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5951 return;
5952 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5953 < 0) return;
5954 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5955 0) return;
5956 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5957 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5958 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5959 < 0) return;
5960 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5961 return;
5962 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5963 return;
5964 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5965 return;
5966 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5967 return;
5968 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5969 0) return;
5970 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5971 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5972 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5973 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5974 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5975 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5976 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5977 0) return;
5978 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5979 0) return;
5980 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5981 return;
5982 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5983 return;
5984 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5985 0) return;
5986 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5987 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5988 return;
5989 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5990 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5991 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5992 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5993 return;
5994 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5995 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5996 return;
5997 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5998 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5999 return;
6000 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6001 return;
6002 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6003 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
6004 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6005 return;
6006 if (PyDict_SetItemString(d, "GeneratorExp",
6007 (PyObject*)GeneratorExp_type) < 0) return;
6008 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6009 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6010 return;
6011 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6012 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6013 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6014 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6015 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6016 0) return;
6017 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6018 0) return;
6019 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6020 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6021 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6022 if (PyDict_SetItemString(d, "expr_context",
6023 (PyObject*)expr_context_type) < 0) return;
6024 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6025 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6026 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6027 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6028 return;
6029 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6030 return;
6031 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6032 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6033 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6034 return;
6035 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6036 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6037 return;
6038 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6039 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6040 return;
6041 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6042 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6043 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6044 return;
6045 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6046 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6047 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6048 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6049 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6050 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6051 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6052 return;
6053 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6054 return;
6055 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6056 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6057 return;
6058 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6059 return;
6060 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6061 return;
6062 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6063 return;
6064 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6065 return;
6066 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6067 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6068 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6069 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6070 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6071 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6072 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6073 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6074 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6075 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6076 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6077 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6078 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6079 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6080 if (PyDict_SetItemString(d, "comprehension",
6081 (PyObject*)comprehension_type) < 0) return;
6082 if (PyDict_SetItemString(d, "excepthandler",
6083 (PyObject*)excepthandler_type) < 0) return;
6084 if (PyDict_SetItemString(d, "ExceptHandler",
6085 (PyObject*)ExceptHandler_type) < 0) return;
6086 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6087 0) return;
6088 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6089 return;
6090 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
6094 PyObject* PyAST_mod2obj(mod_ty t)
6096 init_types();
6097 return ast2obj_mod(t);
6100 /* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6101 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
6103 mod_ty res;
6104 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6105 (PyObject*)Interactive_type};
6106 char *req_name[] = {"Module", "Expression", "Interactive"};
6107 assert(0 <= mode && mode <= 2);
6109 init_types();
6111 if (!PyObject_IsInstance(ast, req_type[mode])) {
6112 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6113 req_name[mode], Py_TYPE(ast)->tp_name);
6114 return NULL;
6116 if (obj2ast_mod(ast, &res, arena) != 0)
6117 return NULL;
6118 else
6119 return res;
6122 int PyAST_Check(PyObject* obj)
6124 init_types();
6125 return PyObject_IsInstance(obj, (PyObject*)&AST_type);