Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c
[python.git] / Python / Python-ast.c
blob46a1817dd7acd941ad4fef297b8e377406e035b2
1 /* File automatically generated by Parser/asdl_c.py. */
4 /*
5 __version__ 53731.
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 "decorators",
47 static PyTypeObject *ClassDef_type;
48 static char *ClassDef_fields[]={
49 "name",
50 "bases",
51 "body",
53 static PyTypeObject *Return_type;
54 static char *Return_fields[]={
55 "value",
57 static PyTypeObject *Delete_type;
58 static char *Delete_fields[]={
59 "targets",
61 static PyTypeObject *Assign_type;
62 static char *Assign_fields[]={
63 "targets",
64 "value",
66 static PyTypeObject *AugAssign_type;
67 static char *AugAssign_fields[]={
68 "target",
69 "op",
70 "value",
72 static PyTypeObject *Print_type;
73 static char *Print_fields[]={
74 "dest",
75 "values",
76 "nl",
78 static PyTypeObject *For_type;
79 static char *For_fields[]={
80 "target",
81 "iter",
82 "body",
83 "orelse",
85 static PyTypeObject *While_type;
86 static char *While_fields[]={
87 "test",
88 "body",
89 "orelse",
91 static PyTypeObject *If_type;
92 static char *If_fields[]={
93 "test",
94 "body",
95 "orelse",
97 static PyTypeObject *With_type;
98 static char *With_fields[]={
99 "context_expr",
100 "optional_vars",
101 "body",
103 static PyTypeObject *Raise_type;
104 static char *Raise_fields[]={
105 "type",
106 "inst",
107 "tback",
109 static PyTypeObject *TryExcept_type;
110 static char *TryExcept_fields[]={
111 "body",
112 "handlers",
113 "orelse",
115 static PyTypeObject *TryFinally_type;
116 static char *TryFinally_fields[]={
117 "body",
118 "finalbody",
120 static PyTypeObject *Assert_type;
121 static char *Assert_fields[]={
122 "test",
123 "msg",
125 static PyTypeObject *Import_type;
126 static char *Import_fields[]={
127 "names",
129 static PyTypeObject *ImportFrom_type;
130 static char *ImportFrom_fields[]={
131 "module",
132 "names",
133 "level",
135 static PyTypeObject *Exec_type;
136 static char *Exec_fields[]={
137 "body",
138 "globals",
139 "locals",
141 static PyTypeObject *Global_type;
142 static char *Global_fields[]={
143 "names",
145 static PyTypeObject *Expr_type;
146 static char *Expr_fields[]={
147 "value",
149 static PyTypeObject *Pass_type;
150 static PyTypeObject *Break_type;
151 static PyTypeObject *Continue_type;
152 static PyTypeObject *expr_type;
153 static char *expr_attributes[] = {
154 "lineno",
155 "col_offset",
157 static PyObject* ast2obj_expr(void*);
158 static PyTypeObject *BoolOp_type;
159 static char *BoolOp_fields[]={
160 "op",
161 "values",
163 static PyTypeObject *BinOp_type;
164 static char *BinOp_fields[]={
165 "left",
166 "op",
167 "right",
169 static PyTypeObject *UnaryOp_type;
170 static char *UnaryOp_fields[]={
171 "op",
172 "operand",
174 static PyTypeObject *Lambda_type;
175 static char *Lambda_fields[]={
176 "args",
177 "body",
179 static PyTypeObject *IfExp_type;
180 static char *IfExp_fields[]={
181 "test",
182 "body",
183 "orelse",
185 static PyTypeObject *Dict_type;
186 static char *Dict_fields[]={
187 "keys",
188 "values",
190 static PyTypeObject *ListComp_type;
191 static char *ListComp_fields[]={
192 "elt",
193 "generators",
195 static PyTypeObject *GeneratorExp_type;
196 static char *GeneratorExp_fields[]={
197 "elt",
198 "generators",
200 static PyTypeObject *Yield_type;
201 static char *Yield_fields[]={
202 "value",
204 static PyTypeObject *Compare_type;
205 static char *Compare_fields[]={
206 "left",
207 "ops",
208 "comparators",
210 static PyTypeObject *Call_type;
211 static char *Call_fields[]={
212 "func",
213 "args",
214 "keywords",
215 "starargs",
216 "kwargs",
218 static PyTypeObject *Repr_type;
219 static char *Repr_fields[]={
220 "value",
222 static PyTypeObject *Num_type;
223 static char *Num_fields[]={
224 "n",
226 static PyTypeObject *Str_type;
227 static char *Str_fields[]={
228 "s",
230 static PyTypeObject *Attribute_type;
231 static char *Attribute_fields[]={
232 "value",
233 "attr",
234 "ctx",
236 static PyTypeObject *Subscript_type;
237 static char *Subscript_fields[]={
238 "value",
239 "slice",
240 "ctx",
242 static PyTypeObject *Name_type;
243 static char *Name_fields[]={
244 "id",
245 "ctx",
247 static PyTypeObject *List_type;
248 static char *List_fields[]={
249 "elts",
250 "ctx",
252 static PyTypeObject *Tuple_type;
253 static char *Tuple_fields[]={
254 "elts",
255 "ctx",
257 static PyTypeObject *expr_context_type;
258 static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
259 *AugLoad_singleton, *AugStore_singleton, *Param_singleton;
260 static PyObject* ast2obj_expr_context(expr_context_ty);
261 static PyTypeObject *Load_type;
262 static PyTypeObject *Store_type;
263 static PyTypeObject *Del_type;
264 static PyTypeObject *AugLoad_type;
265 static PyTypeObject *AugStore_type;
266 static PyTypeObject *Param_type;
267 static PyTypeObject *slice_type;
268 static PyObject* ast2obj_slice(void*);
269 static PyTypeObject *Ellipsis_type;
270 static PyTypeObject *Slice_type;
271 static char *Slice_fields[]={
272 "lower",
273 "upper",
274 "step",
276 static PyTypeObject *ExtSlice_type;
277 static char *ExtSlice_fields[]={
278 "dims",
280 static PyTypeObject *Index_type;
281 static char *Index_fields[]={
282 "value",
284 static PyTypeObject *boolop_type;
285 static PyObject *And_singleton, *Or_singleton;
286 static PyObject* ast2obj_boolop(boolop_ty);
287 static PyTypeObject *And_type;
288 static PyTypeObject *Or_type;
289 static PyTypeObject *operator_type;
290 static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
291 *Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
292 *RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
293 *FloorDiv_singleton;
294 static PyObject* ast2obj_operator(operator_ty);
295 static PyTypeObject *Add_type;
296 static PyTypeObject *Sub_type;
297 static PyTypeObject *Mult_type;
298 static PyTypeObject *Div_type;
299 static PyTypeObject *Mod_type;
300 static PyTypeObject *Pow_type;
301 static PyTypeObject *LShift_type;
302 static PyTypeObject *RShift_type;
303 static PyTypeObject *BitOr_type;
304 static PyTypeObject *BitXor_type;
305 static PyTypeObject *BitAnd_type;
306 static PyTypeObject *FloorDiv_type;
307 static PyTypeObject *unaryop_type;
308 static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
309 *USub_singleton;
310 static PyObject* ast2obj_unaryop(unaryop_ty);
311 static PyTypeObject *Invert_type;
312 static PyTypeObject *Not_type;
313 static PyTypeObject *UAdd_type;
314 static PyTypeObject *USub_type;
315 static PyTypeObject *cmpop_type;
316 static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
317 *Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
318 *NotIn_singleton;
319 static PyObject* ast2obj_cmpop(cmpop_ty);
320 static PyTypeObject *Eq_type;
321 static PyTypeObject *NotEq_type;
322 static PyTypeObject *Lt_type;
323 static PyTypeObject *LtE_type;
324 static PyTypeObject *Gt_type;
325 static PyTypeObject *GtE_type;
326 static PyTypeObject *Is_type;
327 static PyTypeObject *IsNot_type;
328 static PyTypeObject *In_type;
329 static PyTypeObject *NotIn_type;
330 static PyTypeObject *comprehension_type;
331 static PyObject* ast2obj_comprehension(void*);
332 static char *comprehension_fields[]={
333 "target",
334 "iter",
335 "ifs",
337 static PyTypeObject *excepthandler_type;
338 static PyObject* ast2obj_excepthandler(void*);
339 static char *excepthandler_fields[]={
340 "type",
341 "name",
342 "body",
343 "lineno",
344 "col_offset",
346 static PyTypeObject *arguments_type;
347 static PyObject* ast2obj_arguments(void*);
348 static char *arguments_fields[]={
349 "args",
350 "vararg",
351 "kwarg",
352 "defaults",
354 static PyTypeObject *keyword_type;
355 static PyObject* ast2obj_keyword(void*);
356 static char *keyword_fields[]={
357 "arg",
358 "value",
360 static PyTypeObject *alias_type;
361 static PyObject* ast2obj_alias(void*);
362 static char *alias_fields[]={
363 "name",
364 "asname",
368 static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
370 PyObject *fnames, *result;
371 int i;
372 if (num_fields) {
373 fnames = PyTuple_New(num_fields);
374 if (!fnames) return NULL;
375 } else {
376 fnames = Py_None;
377 Py_INCREF(Py_None);
379 for(i=0; i < num_fields; i++) {
380 PyObject *field = PyString_FromString(fields[i]);
381 if (!field) {
382 Py_DECREF(fnames);
383 return NULL;
385 PyTuple_SET_ITEM(fnames, i, field);
387 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
388 type, base, "_fields", fnames, "__module__", "_ast");
389 Py_DECREF(fnames);
390 return (PyTypeObject*)result;
393 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
395 int i, result;
396 PyObject *s, *l = PyList_New(num_fields);
397 if (!l) return 0;
398 for(i = 0; i < num_fields; i++) {
399 s = PyString_FromString(attrs[i]);
400 if (!s) {
401 Py_DECREF(l);
402 return 0;
404 PyList_SET_ITEM(l, i, s);
406 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
407 Py_DECREF(l);
408 return result;
411 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
413 int i, n = asdl_seq_LEN(seq);
414 PyObject *result = PyList_New(n);
415 PyObject *value;
416 if (!result)
417 return NULL;
418 for (i = 0; i < n; i++) {
419 value = func(asdl_seq_GET(seq, i));
420 if (!value) {
421 Py_DECREF(result);
422 return NULL;
424 PyList_SET_ITEM(result, i, value);
426 return result;
429 static PyObject* ast2obj_object(void *o)
431 if (!o)
432 o = Py_None;
433 Py_INCREF((PyObject*)o);
434 return (PyObject*)o;
436 #define ast2obj_identifier ast2obj_object
437 #define ast2obj_string ast2obj_object
438 static PyObject* ast2obj_bool(bool b)
440 return PyBool_FromLong(b);
443 static PyObject* ast2obj_int(bool b)
445 return PyInt_FromLong(b);
448 static int init_types(void)
450 static int initialized;
451 if (initialized) return 1;
452 AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);
453 mod_type = make_type("mod", AST_type, NULL, 0);
454 if (!mod_type) return 0;
455 if (!add_attributes(mod_type, NULL, 0)) return 0;
456 Module_type = make_type("Module", mod_type, Module_fields, 1);
457 if (!Module_type) return 0;
458 Interactive_type = make_type("Interactive", mod_type,
459 Interactive_fields, 1);
460 if (!Interactive_type) return 0;
461 Expression_type = make_type("Expression", mod_type, Expression_fields,
463 if (!Expression_type) return 0;
464 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
465 if (!Suite_type) return 0;
466 stmt_type = make_type("stmt", AST_type, NULL, 0);
467 if (!stmt_type) return 0;
468 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
469 FunctionDef_type = make_type("FunctionDef", stmt_type,
470 FunctionDef_fields, 4);
471 if (!FunctionDef_type) return 0;
472 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 3);
473 if (!ClassDef_type) return 0;
474 Return_type = make_type("Return", stmt_type, Return_fields, 1);
475 if (!Return_type) return 0;
476 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
477 if (!Delete_type) return 0;
478 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
479 if (!Assign_type) return 0;
480 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
481 if (!AugAssign_type) return 0;
482 Print_type = make_type("Print", stmt_type, Print_fields, 3);
483 if (!Print_type) return 0;
484 For_type = make_type("For", stmt_type, For_fields, 4);
485 if (!For_type) return 0;
486 While_type = make_type("While", stmt_type, While_fields, 3);
487 if (!While_type) return 0;
488 If_type = make_type("If", stmt_type, If_fields, 3);
489 if (!If_type) return 0;
490 With_type = make_type("With", stmt_type, With_fields, 3);
491 if (!With_type) return 0;
492 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
493 if (!Raise_type) return 0;
494 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
495 if (!TryExcept_type) return 0;
496 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
498 if (!TryFinally_type) return 0;
499 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
500 if (!Assert_type) return 0;
501 Import_type = make_type("Import", stmt_type, Import_fields, 1);
502 if (!Import_type) return 0;
503 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
505 if (!ImportFrom_type) return 0;
506 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
507 if (!Exec_type) return 0;
508 Global_type = make_type("Global", stmt_type, Global_fields, 1);
509 if (!Global_type) return 0;
510 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
511 if (!Expr_type) return 0;
512 Pass_type = make_type("Pass", stmt_type, NULL, 0);
513 if (!Pass_type) return 0;
514 Break_type = make_type("Break", stmt_type, NULL, 0);
515 if (!Break_type) return 0;
516 Continue_type = make_type("Continue", stmt_type, NULL, 0);
517 if (!Continue_type) return 0;
518 expr_type = make_type("expr", AST_type, NULL, 0);
519 if (!expr_type) return 0;
520 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
521 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
522 if (!BoolOp_type) return 0;
523 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
524 if (!BinOp_type) return 0;
525 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
526 if (!UnaryOp_type) return 0;
527 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
528 if (!Lambda_type) return 0;
529 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
530 if (!IfExp_type) return 0;
531 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
532 if (!Dict_type) return 0;
533 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
534 if (!ListComp_type) return 0;
535 GeneratorExp_type = make_type("GeneratorExp", expr_type,
536 GeneratorExp_fields, 2);
537 if (!GeneratorExp_type) return 0;
538 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
539 if (!Yield_type) return 0;
540 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
541 if (!Compare_type) return 0;
542 Call_type = make_type("Call", expr_type, Call_fields, 5);
543 if (!Call_type) return 0;
544 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
545 if (!Repr_type) return 0;
546 Num_type = make_type("Num", expr_type, Num_fields, 1);
547 if (!Num_type) return 0;
548 Str_type = make_type("Str", expr_type, Str_fields, 1);
549 if (!Str_type) return 0;
550 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
551 if (!Attribute_type) return 0;
552 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
553 if (!Subscript_type) return 0;
554 Name_type = make_type("Name", expr_type, Name_fields, 2);
555 if (!Name_type) return 0;
556 List_type = make_type("List", expr_type, List_fields, 2);
557 if (!List_type) return 0;
558 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
559 if (!Tuple_type) return 0;
560 expr_context_type = make_type("expr_context", AST_type, NULL, 0);
561 if (!expr_context_type) return 0;
562 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
563 Load_type = make_type("Load", expr_context_type, NULL, 0);
564 if (!Load_type) return 0;
565 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
566 if (!Load_singleton) return 0;
567 Store_type = make_type("Store", expr_context_type, NULL, 0);
568 if (!Store_type) return 0;
569 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
570 if (!Store_singleton) return 0;
571 Del_type = make_type("Del", expr_context_type, NULL, 0);
572 if (!Del_type) return 0;
573 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
574 if (!Del_singleton) return 0;
575 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
576 if (!AugLoad_type) return 0;
577 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
578 if (!AugLoad_singleton) return 0;
579 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
580 if (!AugStore_type) return 0;
581 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
582 if (!AugStore_singleton) return 0;
583 Param_type = make_type("Param", expr_context_type, NULL, 0);
584 if (!Param_type) return 0;
585 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
586 if (!Param_singleton) return 0;
587 slice_type = make_type("slice", AST_type, NULL, 0);
588 if (!slice_type) return 0;
589 if (!add_attributes(slice_type, NULL, 0)) return 0;
590 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
591 if (!Ellipsis_type) return 0;
592 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
593 if (!Slice_type) return 0;
594 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
595 if (!ExtSlice_type) return 0;
596 Index_type = make_type("Index", slice_type, Index_fields, 1);
597 if (!Index_type) return 0;
598 boolop_type = make_type("boolop", AST_type, NULL, 0);
599 if (!boolop_type) return 0;
600 if (!add_attributes(boolop_type, NULL, 0)) return 0;
601 And_type = make_type("And", boolop_type, NULL, 0);
602 if (!And_type) return 0;
603 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
604 if (!And_singleton) return 0;
605 Or_type = make_type("Or", boolop_type, NULL, 0);
606 if (!Or_type) return 0;
607 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
608 if (!Or_singleton) return 0;
609 operator_type = make_type("operator", AST_type, NULL, 0);
610 if (!operator_type) return 0;
611 if (!add_attributes(operator_type, NULL, 0)) return 0;
612 Add_type = make_type("Add", operator_type, NULL, 0);
613 if (!Add_type) return 0;
614 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
615 if (!Add_singleton) return 0;
616 Sub_type = make_type("Sub", operator_type, NULL, 0);
617 if (!Sub_type) return 0;
618 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
619 if (!Sub_singleton) return 0;
620 Mult_type = make_type("Mult", operator_type, NULL, 0);
621 if (!Mult_type) return 0;
622 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
623 if (!Mult_singleton) return 0;
624 Div_type = make_type("Div", operator_type, NULL, 0);
625 if (!Div_type) return 0;
626 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
627 if (!Div_singleton) return 0;
628 Mod_type = make_type("Mod", operator_type, NULL, 0);
629 if (!Mod_type) return 0;
630 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
631 if (!Mod_singleton) return 0;
632 Pow_type = make_type("Pow", operator_type, NULL, 0);
633 if (!Pow_type) return 0;
634 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
635 if (!Pow_singleton) return 0;
636 LShift_type = make_type("LShift", operator_type, NULL, 0);
637 if (!LShift_type) return 0;
638 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
639 if (!LShift_singleton) return 0;
640 RShift_type = make_type("RShift", operator_type, NULL, 0);
641 if (!RShift_type) return 0;
642 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
643 if (!RShift_singleton) return 0;
644 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
645 if (!BitOr_type) return 0;
646 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
647 if (!BitOr_singleton) return 0;
648 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
649 if (!BitXor_type) return 0;
650 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
651 if (!BitXor_singleton) return 0;
652 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
653 if (!BitAnd_type) return 0;
654 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
655 if (!BitAnd_singleton) return 0;
656 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
657 if (!FloorDiv_type) return 0;
658 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
659 if (!FloorDiv_singleton) return 0;
660 unaryop_type = make_type("unaryop", AST_type, NULL, 0);
661 if (!unaryop_type) return 0;
662 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
663 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
664 if (!Invert_type) return 0;
665 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
666 if (!Invert_singleton) return 0;
667 Not_type = make_type("Not", unaryop_type, NULL, 0);
668 if (!Not_type) return 0;
669 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
670 if (!Not_singleton) return 0;
671 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
672 if (!UAdd_type) return 0;
673 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
674 if (!UAdd_singleton) return 0;
675 USub_type = make_type("USub", unaryop_type, NULL, 0);
676 if (!USub_type) return 0;
677 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
678 if (!USub_singleton) return 0;
679 cmpop_type = make_type("cmpop", AST_type, NULL, 0);
680 if (!cmpop_type) return 0;
681 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
682 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
683 if (!Eq_type) return 0;
684 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
685 if (!Eq_singleton) return 0;
686 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
687 if (!NotEq_type) return 0;
688 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
689 if (!NotEq_singleton) return 0;
690 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
691 if (!Lt_type) return 0;
692 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
693 if (!Lt_singleton) return 0;
694 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
695 if (!LtE_type) return 0;
696 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
697 if (!LtE_singleton) return 0;
698 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
699 if (!Gt_type) return 0;
700 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
701 if (!Gt_singleton) return 0;
702 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
703 if (!GtE_type) return 0;
704 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
705 if (!GtE_singleton) return 0;
706 Is_type = make_type("Is", cmpop_type, NULL, 0);
707 if (!Is_type) return 0;
708 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
709 if (!Is_singleton) return 0;
710 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
711 if (!IsNot_type) return 0;
712 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
713 if (!IsNot_singleton) return 0;
714 In_type = make_type("In", cmpop_type, NULL, 0);
715 if (!In_type) return 0;
716 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
717 if (!In_singleton) return 0;
718 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
719 if (!NotIn_type) return 0;
720 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
721 if (!NotIn_singleton) return 0;
722 comprehension_type = make_type("comprehension", AST_type,
723 comprehension_fields, 3);
724 if (!comprehension_type) return 0;
725 excepthandler_type = make_type("excepthandler", AST_type,
726 excepthandler_fields, 5);
727 if (!excepthandler_type) return 0;
728 arguments_type = make_type("arguments", AST_type, arguments_fields, 4);
729 if (!arguments_type) return 0;
730 keyword_type = make_type("keyword", AST_type, keyword_fields, 2);
731 if (!keyword_type) return 0;
732 alias_type = make_type("alias", AST_type, alias_fields, 2);
733 if (!alias_type) return 0;
734 initialized = 1;
735 return 1;
738 mod_ty
739 Module(asdl_seq * body, PyArena *arena)
741 mod_ty p;
742 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
743 if (!p) {
744 PyErr_NoMemory();
745 return NULL;
747 p->kind = Module_kind;
748 p->v.Module.body = body;
749 return p;
752 mod_ty
753 Interactive(asdl_seq * body, PyArena *arena)
755 mod_ty p;
756 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
757 if (!p) {
758 PyErr_NoMemory();
759 return NULL;
761 p->kind = Interactive_kind;
762 p->v.Interactive.body = body;
763 return p;
766 mod_ty
767 Expression(expr_ty body, PyArena *arena)
769 mod_ty p;
770 if (!body) {
771 PyErr_SetString(PyExc_ValueError,
772 "field body is required for Expression");
773 return NULL;
775 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
776 if (!p) {
777 PyErr_NoMemory();
778 return NULL;
780 p->kind = Expression_kind;
781 p->v.Expression.body = body;
782 return p;
785 mod_ty
786 Suite(asdl_seq * body, PyArena *arena)
788 mod_ty p;
789 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
790 if (!p) {
791 PyErr_NoMemory();
792 return NULL;
794 p->kind = Suite_kind;
795 p->v.Suite.body = body;
796 return p;
799 stmt_ty
800 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
801 decorators, int lineno, int col_offset, PyArena *arena)
803 stmt_ty p;
804 if (!name) {
805 PyErr_SetString(PyExc_ValueError,
806 "field name is required for FunctionDef");
807 return NULL;
809 if (!args) {
810 PyErr_SetString(PyExc_ValueError,
811 "field args is required for FunctionDef");
812 return NULL;
814 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
815 if (!p) {
816 PyErr_NoMemory();
817 return NULL;
819 p->kind = FunctionDef_kind;
820 p->v.FunctionDef.name = name;
821 p->v.FunctionDef.args = args;
822 p->v.FunctionDef.body = body;
823 p->v.FunctionDef.decorators = decorators;
824 p->lineno = lineno;
825 p->col_offset = col_offset;
826 return p;
829 stmt_ty
830 ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int
831 col_offset, PyArena *arena)
833 stmt_ty p;
834 if (!name) {
835 PyErr_SetString(PyExc_ValueError,
836 "field name is required for ClassDef");
837 return NULL;
839 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
840 if (!p) {
841 PyErr_NoMemory();
842 return NULL;
844 p->kind = ClassDef_kind;
845 p->v.ClassDef.name = name;
846 p->v.ClassDef.bases = bases;
847 p->v.ClassDef.body = body;
848 p->lineno = lineno;
849 p->col_offset = col_offset;
850 return p;
853 stmt_ty
854 Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
856 stmt_ty p;
857 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
858 if (!p) {
859 PyErr_NoMemory();
860 return NULL;
862 p->kind = Return_kind;
863 p->v.Return.value = value;
864 p->lineno = lineno;
865 p->col_offset = col_offset;
866 return p;
869 stmt_ty
870 Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
872 stmt_ty p;
873 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
874 if (!p) {
875 PyErr_NoMemory();
876 return NULL;
878 p->kind = Delete_kind;
879 p->v.Delete.targets = targets;
880 p->lineno = lineno;
881 p->col_offset = col_offset;
882 return p;
885 stmt_ty
886 Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
887 *arena)
889 stmt_ty p;
890 if (!value) {
891 PyErr_SetString(PyExc_ValueError,
892 "field value is required for Assign");
893 return NULL;
895 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
896 if (!p) {
897 PyErr_NoMemory();
898 return NULL;
900 p->kind = Assign_kind;
901 p->v.Assign.targets = targets;
902 p->v.Assign.value = value;
903 p->lineno = lineno;
904 p->col_offset = col_offset;
905 return p;
908 stmt_ty
909 AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
910 col_offset, PyArena *arena)
912 stmt_ty p;
913 if (!target) {
914 PyErr_SetString(PyExc_ValueError,
915 "field target is required for AugAssign");
916 return NULL;
918 if (!op) {
919 PyErr_SetString(PyExc_ValueError,
920 "field op is required for AugAssign");
921 return NULL;
923 if (!value) {
924 PyErr_SetString(PyExc_ValueError,
925 "field value is required for AugAssign");
926 return NULL;
928 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
929 if (!p) {
930 PyErr_NoMemory();
931 return NULL;
933 p->kind = AugAssign_kind;
934 p->v.AugAssign.target = target;
935 p->v.AugAssign.op = op;
936 p->v.AugAssign.value = value;
937 p->lineno = lineno;
938 p->col_offset = col_offset;
939 return p;
942 stmt_ty
943 Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
944 PyArena *arena)
946 stmt_ty p;
947 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
948 if (!p) {
949 PyErr_NoMemory();
950 return NULL;
952 p->kind = Print_kind;
953 p->v.Print.dest = dest;
954 p->v.Print.values = values;
955 p->v.Print.nl = nl;
956 p->lineno = lineno;
957 p->col_offset = col_offset;
958 return p;
961 stmt_ty
962 For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
963 lineno, int col_offset, PyArena *arena)
965 stmt_ty p;
966 if (!target) {
967 PyErr_SetString(PyExc_ValueError,
968 "field target is required for For");
969 return NULL;
971 if (!iter) {
972 PyErr_SetString(PyExc_ValueError,
973 "field iter is required for For");
974 return NULL;
976 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
977 if (!p) {
978 PyErr_NoMemory();
979 return NULL;
981 p->kind = For_kind;
982 p->v.For.target = target;
983 p->v.For.iter = iter;
984 p->v.For.body = body;
985 p->v.For.orelse = orelse;
986 p->lineno = lineno;
987 p->col_offset = col_offset;
988 return p;
991 stmt_ty
992 While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
993 col_offset, PyArena *arena)
995 stmt_ty p;
996 if (!test) {
997 PyErr_SetString(PyExc_ValueError,
998 "field test is required for While");
999 return NULL;
1001 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1002 if (!p) {
1003 PyErr_NoMemory();
1004 return NULL;
1006 p->kind = While_kind;
1007 p->v.While.test = test;
1008 p->v.While.body = body;
1009 p->v.While.orelse = orelse;
1010 p->lineno = lineno;
1011 p->col_offset = col_offset;
1012 return p;
1015 stmt_ty
1016 If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1017 col_offset, PyArena *arena)
1019 stmt_ty p;
1020 if (!test) {
1021 PyErr_SetString(PyExc_ValueError,
1022 "field test is required for If");
1023 return NULL;
1025 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1026 if (!p) {
1027 PyErr_NoMemory();
1028 return NULL;
1030 p->kind = If_kind;
1031 p->v.If.test = test;
1032 p->v.If.body = body;
1033 p->v.If.orelse = orelse;
1034 p->lineno = lineno;
1035 p->col_offset = col_offset;
1036 return p;
1039 stmt_ty
1040 With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
1041 int col_offset, PyArena *arena)
1043 stmt_ty p;
1044 if (!context_expr) {
1045 PyErr_SetString(PyExc_ValueError,
1046 "field context_expr is required for With");
1047 return NULL;
1049 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1050 if (!p) {
1051 PyErr_NoMemory();
1052 return NULL;
1054 p->kind = With_kind;
1055 p->v.With.context_expr = context_expr;
1056 p->v.With.optional_vars = optional_vars;
1057 p->v.With.body = body;
1058 p->lineno = lineno;
1059 p->col_offset = col_offset;
1060 return p;
1063 stmt_ty
1064 Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1065 PyArena *arena)
1067 stmt_ty p;
1068 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1069 if (!p) {
1070 PyErr_NoMemory();
1071 return NULL;
1073 p->kind = Raise_kind;
1074 p->v.Raise.type = type;
1075 p->v.Raise.inst = inst;
1076 p->v.Raise.tback = tback;
1077 p->lineno = lineno;
1078 p->col_offset = col_offset;
1079 return p;
1082 stmt_ty
1083 TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
1084 int col_offset, PyArena *arena)
1086 stmt_ty p;
1087 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1088 if (!p) {
1089 PyErr_NoMemory();
1090 return NULL;
1092 p->kind = TryExcept_kind;
1093 p->v.TryExcept.body = body;
1094 p->v.TryExcept.handlers = handlers;
1095 p->v.TryExcept.orelse = orelse;
1096 p->lineno = lineno;
1097 p->col_offset = col_offset;
1098 return p;
1101 stmt_ty
1102 TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1103 PyArena *arena)
1105 stmt_ty p;
1106 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1107 if (!p) {
1108 PyErr_NoMemory();
1109 return NULL;
1111 p->kind = TryFinally_kind;
1112 p->v.TryFinally.body = body;
1113 p->v.TryFinally.finalbody = finalbody;
1114 p->lineno = lineno;
1115 p->col_offset = col_offset;
1116 return p;
1119 stmt_ty
1120 Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
1122 stmt_ty p;
1123 if (!test) {
1124 PyErr_SetString(PyExc_ValueError,
1125 "field test is required for Assert");
1126 return NULL;
1128 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1129 if (!p) {
1130 PyErr_NoMemory();
1131 return NULL;
1133 p->kind = Assert_kind;
1134 p->v.Assert.test = test;
1135 p->v.Assert.msg = msg;
1136 p->lineno = lineno;
1137 p->col_offset = col_offset;
1138 return p;
1141 stmt_ty
1142 Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1144 stmt_ty p;
1145 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1146 if (!p) {
1147 PyErr_NoMemory();
1148 return NULL;
1150 p->kind = Import_kind;
1151 p->v.Import.names = names;
1152 p->lineno = lineno;
1153 p->col_offset = col_offset;
1154 return p;
1157 stmt_ty
1158 ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1159 col_offset, PyArena *arena)
1161 stmt_ty p;
1162 if (!module) {
1163 PyErr_SetString(PyExc_ValueError,
1164 "field module is required for ImportFrom");
1165 return NULL;
1167 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1168 if (!p) {
1169 PyErr_NoMemory();
1170 return NULL;
1172 p->kind = ImportFrom_kind;
1173 p->v.ImportFrom.module = module;
1174 p->v.ImportFrom.names = names;
1175 p->v.ImportFrom.level = level;
1176 p->lineno = lineno;
1177 p->col_offset = col_offset;
1178 return p;
1181 stmt_ty
1182 Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1183 PyArena *arena)
1185 stmt_ty p;
1186 if (!body) {
1187 PyErr_SetString(PyExc_ValueError,
1188 "field body is required for Exec");
1189 return NULL;
1191 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1192 if (!p) {
1193 PyErr_NoMemory();
1194 return NULL;
1196 p->kind = Exec_kind;
1197 p->v.Exec.body = body;
1198 p->v.Exec.globals = globals;
1199 p->v.Exec.locals = locals;
1200 p->lineno = lineno;
1201 p->col_offset = col_offset;
1202 return p;
1205 stmt_ty
1206 Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1208 stmt_ty p;
1209 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1210 if (!p) {
1211 PyErr_NoMemory();
1212 return NULL;
1214 p->kind = Global_kind;
1215 p->v.Global.names = names;
1216 p->lineno = lineno;
1217 p->col_offset = col_offset;
1218 return p;
1221 stmt_ty
1222 Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1224 stmt_ty p;
1225 if (!value) {
1226 PyErr_SetString(PyExc_ValueError,
1227 "field value is required for Expr");
1228 return NULL;
1230 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1231 if (!p) {
1232 PyErr_NoMemory();
1233 return NULL;
1235 p->kind = Expr_kind;
1236 p->v.Expr.value = value;
1237 p->lineno = lineno;
1238 p->col_offset = col_offset;
1239 return p;
1242 stmt_ty
1243 Pass(int lineno, int col_offset, PyArena *arena)
1245 stmt_ty p;
1246 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1247 if (!p) {
1248 PyErr_NoMemory();
1249 return NULL;
1251 p->kind = Pass_kind;
1252 p->lineno = lineno;
1253 p->col_offset = col_offset;
1254 return p;
1257 stmt_ty
1258 Break(int lineno, int col_offset, PyArena *arena)
1260 stmt_ty p;
1261 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1262 if (!p) {
1263 PyErr_NoMemory();
1264 return NULL;
1266 p->kind = Break_kind;
1267 p->lineno = lineno;
1268 p->col_offset = col_offset;
1269 return p;
1272 stmt_ty
1273 Continue(int lineno, int col_offset, PyArena *arena)
1275 stmt_ty p;
1276 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1277 if (!p) {
1278 PyErr_NoMemory();
1279 return NULL;
1281 p->kind = Continue_kind;
1282 p->lineno = lineno;
1283 p->col_offset = col_offset;
1284 return p;
1287 expr_ty
1288 BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1289 *arena)
1291 expr_ty p;
1292 if (!op) {
1293 PyErr_SetString(PyExc_ValueError,
1294 "field op is required for BoolOp");
1295 return NULL;
1297 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1298 if (!p) {
1299 PyErr_NoMemory();
1300 return NULL;
1302 p->kind = BoolOp_kind;
1303 p->v.BoolOp.op = op;
1304 p->v.BoolOp.values = values;
1305 p->lineno = lineno;
1306 p->col_offset = col_offset;
1307 return p;
1310 expr_ty
1311 BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1312 PyArena *arena)
1314 expr_ty p;
1315 if (!left) {
1316 PyErr_SetString(PyExc_ValueError,
1317 "field left is required for BinOp");
1318 return NULL;
1320 if (!op) {
1321 PyErr_SetString(PyExc_ValueError,
1322 "field op is required for BinOp");
1323 return NULL;
1325 if (!right) {
1326 PyErr_SetString(PyExc_ValueError,
1327 "field right is required for BinOp");
1328 return NULL;
1330 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1331 if (!p) {
1332 PyErr_NoMemory();
1333 return NULL;
1335 p->kind = BinOp_kind;
1336 p->v.BinOp.left = left;
1337 p->v.BinOp.op = op;
1338 p->v.BinOp.right = right;
1339 p->lineno = lineno;
1340 p->col_offset = col_offset;
1341 return p;
1344 expr_ty
1345 UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1346 *arena)
1348 expr_ty p;
1349 if (!op) {
1350 PyErr_SetString(PyExc_ValueError,
1351 "field op is required for UnaryOp");
1352 return NULL;
1354 if (!operand) {
1355 PyErr_SetString(PyExc_ValueError,
1356 "field operand is required for UnaryOp");
1357 return NULL;
1359 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1360 if (!p) {
1361 PyErr_NoMemory();
1362 return NULL;
1364 p->kind = UnaryOp_kind;
1365 p->v.UnaryOp.op = op;
1366 p->v.UnaryOp.operand = operand;
1367 p->lineno = lineno;
1368 p->col_offset = col_offset;
1369 return p;
1372 expr_ty
1373 Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1374 *arena)
1376 expr_ty p;
1377 if (!args) {
1378 PyErr_SetString(PyExc_ValueError,
1379 "field args is required for Lambda");
1380 return NULL;
1382 if (!body) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "field body is required for Lambda");
1385 return NULL;
1387 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1388 if (!p) {
1389 PyErr_NoMemory();
1390 return NULL;
1392 p->kind = Lambda_kind;
1393 p->v.Lambda.args = args;
1394 p->v.Lambda.body = body;
1395 p->lineno = lineno;
1396 p->col_offset = col_offset;
1397 return p;
1400 expr_ty
1401 IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1402 PyArena *arena)
1404 expr_ty p;
1405 if (!test) {
1406 PyErr_SetString(PyExc_ValueError,
1407 "field test is required for IfExp");
1408 return NULL;
1410 if (!body) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field body is required for IfExp");
1413 return NULL;
1415 if (!orelse) {
1416 PyErr_SetString(PyExc_ValueError,
1417 "field orelse is required for IfExp");
1418 return NULL;
1420 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1421 if (!p) {
1422 PyErr_NoMemory();
1423 return NULL;
1425 p->kind = IfExp_kind;
1426 p->v.IfExp.test = test;
1427 p->v.IfExp.body = body;
1428 p->v.IfExp.orelse = orelse;
1429 p->lineno = lineno;
1430 p->col_offset = col_offset;
1431 return p;
1434 expr_ty
1435 Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1436 *arena)
1438 expr_ty p;
1439 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1440 if (!p) {
1441 PyErr_NoMemory();
1442 return NULL;
1444 p->kind = Dict_kind;
1445 p->v.Dict.keys = keys;
1446 p->v.Dict.values = values;
1447 p->lineno = lineno;
1448 p->col_offset = col_offset;
1449 return p;
1452 expr_ty
1453 ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1454 PyArena *arena)
1456 expr_ty p;
1457 if (!elt) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "field elt is required for ListComp");
1460 return NULL;
1462 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1463 if (!p) {
1464 PyErr_NoMemory();
1465 return NULL;
1467 p->kind = ListComp_kind;
1468 p->v.ListComp.elt = elt;
1469 p->v.ListComp.generators = generators;
1470 p->lineno = lineno;
1471 p->col_offset = col_offset;
1472 return p;
1475 expr_ty
1476 GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1477 PyArena *arena)
1479 expr_ty p;
1480 if (!elt) {
1481 PyErr_SetString(PyExc_ValueError,
1482 "field elt is required for GeneratorExp");
1483 return NULL;
1485 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1486 if (!p) {
1487 PyErr_NoMemory();
1488 return NULL;
1490 p->kind = GeneratorExp_kind;
1491 p->v.GeneratorExp.elt = elt;
1492 p->v.GeneratorExp.generators = generators;
1493 p->lineno = lineno;
1494 p->col_offset = col_offset;
1495 return p;
1498 expr_ty
1499 Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
1501 expr_ty p;
1502 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1503 if (!p) {
1504 PyErr_NoMemory();
1505 return NULL;
1507 p->kind = Yield_kind;
1508 p->v.Yield.value = value;
1509 p->lineno = lineno;
1510 p->col_offset = col_offset;
1511 return p;
1514 expr_ty
1515 Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1516 int col_offset, PyArena *arena)
1518 expr_ty p;
1519 if (!left) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field left is required for Compare");
1522 return NULL;
1524 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1525 if (!p) {
1526 PyErr_NoMemory();
1527 return NULL;
1529 p->kind = Compare_kind;
1530 p->v.Compare.left = left;
1531 p->v.Compare.ops = ops;
1532 p->v.Compare.comparators = comparators;
1533 p->lineno = lineno;
1534 p->col_offset = col_offset;
1535 return p;
1538 expr_ty
1539 Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
1540 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
1542 expr_ty p;
1543 if (!func) {
1544 PyErr_SetString(PyExc_ValueError,
1545 "field func is required for Call");
1546 return NULL;
1548 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1549 if (!p) {
1550 PyErr_NoMemory();
1551 return NULL;
1553 p->kind = Call_kind;
1554 p->v.Call.func = func;
1555 p->v.Call.args = args;
1556 p->v.Call.keywords = keywords;
1557 p->v.Call.starargs = starargs;
1558 p->v.Call.kwargs = kwargs;
1559 p->lineno = lineno;
1560 p->col_offset = col_offset;
1561 return p;
1564 expr_ty
1565 Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
1567 expr_ty p;
1568 if (!value) {
1569 PyErr_SetString(PyExc_ValueError,
1570 "field value is required for Repr");
1571 return NULL;
1573 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1574 if (!p) {
1575 PyErr_NoMemory();
1576 return NULL;
1578 p->kind = Repr_kind;
1579 p->v.Repr.value = value;
1580 p->lineno = lineno;
1581 p->col_offset = col_offset;
1582 return p;
1585 expr_ty
1586 Num(object n, int lineno, int col_offset, PyArena *arena)
1588 expr_ty p;
1589 if (!n) {
1590 PyErr_SetString(PyExc_ValueError,
1591 "field n is required for Num");
1592 return NULL;
1594 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1595 if (!p) {
1596 PyErr_NoMemory();
1597 return NULL;
1599 p->kind = Num_kind;
1600 p->v.Num.n = n;
1601 p->lineno = lineno;
1602 p->col_offset = col_offset;
1603 return p;
1606 expr_ty
1607 Str(string s, int lineno, int col_offset, PyArena *arena)
1609 expr_ty p;
1610 if (!s) {
1611 PyErr_SetString(PyExc_ValueError,
1612 "field s is required for Str");
1613 return NULL;
1615 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1616 if (!p) {
1617 PyErr_NoMemory();
1618 return NULL;
1620 p->kind = Str_kind;
1621 p->v.Str.s = s;
1622 p->lineno = lineno;
1623 p->col_offset = col_offset;
1624 return p;
1627 expr_ty
1628 Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1629 col_offset, PyArena *arena)
1631 expr_ty p;
1632 if (!value) {
1633 PyErr_SetString(PyExc_ValueError,
1634 "field value is required for Attribute");
1635 return NULL;
1637 if (!attr) {
1638 PyErr_SetString(PyExc_ValueError,
1639 "field attr is required for Attribute");
1640 return NULL;
1642 if (!ctx) {
1643 PyErr_SetString(PyExc_ValueError,
1644 "field ctx is required for Attribute");
1645 return NULL;
1647 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1648 if (!p) {
1649 PyErr_NoMemory();
1650 return NULL;
1652 p->kind = Attribute_kind;
1653 p->v.Attribute.value = value;
1654 p->v.Attribute.attr = attr;
1655 p->v.Attribute.ctx = ctx;
1656 p->lineno = lineno;
1657 p->col_offset = col_offset;
1658 return p;
1661 expr_ty
1662 Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1663 col_offset, PyArena *arena)
1665 expr_ty p;
1666 if (!value) {
1667 PyErr_SetString(PyExc_ValueError,
1668 "field value is required for Subscript");
1669 return NULL;
1671 if (!slice) {
1672 PyErr_SetString(PyExc_ValueError,
1673 "field slice is required for Subscript");
1674 return NULL;
1676 if (!ctx) {
1677 PyErr_SetString(PyExc_ValueError,
1678 "field ctx is required for Subscript");
1679 return NULL;
1681 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1682 if (!p) {
1683 PyErr_NoMemory();
1684 return NULL;
1686 p->kind = Subscript_kind;
1687 p->v.Subscript.value = value;
1688 p->v.Subscript.slice = slice;
1689 p->v.Subscript.ctx = ctx;
1690 p->lineno = lineno;
1691 p->col_offset = col_offset;
1692 return p;
1695 expr_ty
1696 Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1697 *arena)
1699 expr_ty p;
1700 if (!id) {
1701 PyErr_SetString(PyExc_ValueError,
1702 "field id is required for Name");
1703 return NULL;
1705 if (!ctx) {
1706 PyErr_SetString(PyExc_ValueError,
1707 "field ctx is required for Name");
1708 return NULL;
1710 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1711 if (!p) {
1712 PyErr_NoMemory();
1713 return NULL;
1715 p->kind = Name_kind;
1716 p->v.Name.id = id;
1717 p->v.Name.ctx = ctx;
1718 p->lineno = lineno;
1719 p->col_offset = col_offset;
1720 return p;
1723 expr_ty
1724 List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1725 *arena)
1727 expr_ty p;
1728 if (!ctx) {
1729 PyErr_SetString(PyExc_ValueError,
1730 "field ctx is required for List");
1731 return NULL;
1733 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1734 if (!p) {
1735 PyErr_NoMemory();
1736 return NULL;
1738 p->kind = List_kind;
1739 p->v.List.elts = elts;
1740 p->v.List.ctx = ctx;
1741 p->lineno = lineno;
1742 p->col_offset = col_offset;
1743 return p;
1746 expr_ty
1747 Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1748 *arena)
1750 expr_ty p;
1751 if (!ctx) {
1752 PyErr_SetString(PyExc_ValueError,
1753 "field ctx is required for Tuple");
1754 return NULL;
1756 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1757 if (!p) {
1758 PyErr_NoMemory();
1759 return NULL;
1761 p->kind = Tuple_kind;
1762 p->v.Tuple.elts = elts;
1763 p->v.Tuple.ctx = ctx;
1764 p->lineno = lineno;
1765 p->col_offset = col_offset;
1766 return p;
1769 slice_ty
1770 Ellipsis(PyArena *arena)
1772 slice_ty p;
1773 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1774 if (!p) {
1775 PyErr_NoMemory();
1776 return NULL;
1778 p->kind = Ellipsis_kind;
1779 return p;
1782 slice_ty
1783 Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
1785 slice_ty p;
1786 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1787 if (!p) {
1788 PyErr_NoMemory();
1789 return NULL;
1791 p->kind = Slice_kind;
1792 p->v.Slice.lower = lower;
1793 p->v.Slice.upper = upper;
1794 p->v.Slice.step = step;
1795 return p;
1798 slice_ty
1799 ExtSlice(asdl_seq * dims, PyArena *arena)
1801 slice_ty p;
1802 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1803 if (!p) {
1804 PyErr_NoMemory();
1805 return NULL;
1807 p->kind = ExtSlice_kind;
1808 p->v.ExtSlice.dims = dims;
1809 return p;
1812 slice_ty
1813 Index(expr_ty value, PyArena *arena)
1815 slice_ty p;
1816 if (!value) {
1817 PyErr_SetString(PyExc_ValueError,
1818 "field value is required for Index");
1819 return NULL;
1821 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
1822 if (!p) {
1823 PyErr_NoMemory();
1824 return NULL;
1826 p->kind = Index_kind;
1827 p->v.Index.value = value;
1828 return p;
1831 comprehension_ty
1832 comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
1834 comprehension_ty p;
1835 if (!target) {
1836 PyErr_SetString(PyExc_ValueError,
1837 "field target is required for comprehension");
1838 return NULL;
1840 if (!iter) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field iter is required for comprehension");
1843 return NULL;
1845 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
1846 if (!p) {
1847 PyErr_NoMemory();
1848 return NULL;
1850 p->target = target;
1851 p->iter = iter;
1852 p->ifs = ifs;
1853 return p;
1856 excepthandler_ty
1857 excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
1858 col_offset, PyArena *arena)
1860 excepthandler_ty p;
1861 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
1862 if (!p) {
1863 PyErr_NoMemory();
1864 return NULL;
1866 p->type = type;
1867 p->name = name;
1868 p->body = body;
1869 p->lineno = lineno;
1870 p->col_offset = col_offset;
1871 return p;
1874 arguments_ty
1875 arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
1876 defaults, PyArena *arena)
1878 arguments_ty p;
1879 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
1880 if (!p) {
1881 PyErr_NoMemory();
1882 return NULL;
1884 p->args = args;
1885 p->vararg = vararg;
1886 p->kwarg = kwarg;
1887 p->defaults = defaults;
1888 return p;
1891 keyword_ty
1892 keyword(identifier arg, expr_ty value, PyArena *arena)
1894 keyword_ty p;
1895 if (!arg) {
1896 PyErr_SetString(PyExc_ValueError,
1897 "field arg is required for keyword");
1898 return NULL;
1900 if (!value) {
1901 PyErr_SetString(PyExc_ValueError,
1902 "field value is required for keyword");
1903 return NULL;
1905 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
1906 if (!p) {
1907 PyErr_NoMemory();
1908 return NULL;
1910 p->arg = arg;
1911 p->value = value;
1912 return p;
1915 alias_ty
1916 alias(identifier name, identifier asname, PyArena *arena)
1918 alias_ty p;
1919 if (!name) {
1920 PyErr_SetString(PyExc_ValueError,
1921 "field name is required for alias");
1922 return NULL;
1924 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
1925 if (!p) {
1926 PyErr_NoMemory();
1927 return NULL;
1929 p->name = name;
1930 p->asname = asname;
1931 return p;
1935 PyObject*
1936 ast2obj_mod(void* _o)
1938 mod_ty o = (mod_ty)_o;
1939 PyObject *result = NULL, *value = NULL;
1940 if (!o) {
1941 Py_INCREF(Py_None);
1942 return Py_None;
1945 switch (o->kind) {
1946 case Module_kind:
1947 result = PyType_GenericNew(Module_type, NULL, NULL);
1948 if (!result) goto failed;
1949 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
1950 if (!value) goto failed;
1951 if (PyObject_SetAttrString(result, "body", value) == -1)
1952 goto failed;
1953 Py_DECREF(value);
1954 break;
1955 case Interactive_kind:
1956 result = PyType_GenericNew(Interactive_type, NULL, NULL);
1957 if (!result) goto failed;
1958 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
1959 if (!value) goto failed;
1960 if (PyObject_SetAttrString(result, "body", value) == -1)
1961 goto failed;
1962 Py_DECREF(value);
1963 break;
1964 case Expression_kind:
1965 result = PyType_GenericNew(Expression_type, NULL, NULL);
1966 if (!result) goto failed;
1967 value = ast2obj_expr(o->v.Expression.body);
1968 if (!value) goto failed;
1969 if (PyObject_SetAttrString(result, "body", value) == -1)
1970 goto failed;
1971 Py_DECREF(value);
1972 break;
1973 case Suite_kind:
1974 result = PyType_GenericNew(Suite_type, NULL, NULL);
1975 if (!result) goto failed;
1976 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
1977 if (!value) goto failed;
1978 if (PyObject_SetAttrString(result, "body", value) == -1)
1979 goto failed;
1980 Py_DECREF(value);
1981 break;
1983 return result;
1984 failed:
1985 Py_XDECREF(value);
1986 Py_XDECREF(result);
1987 return NULL;
1990 PyObject*
1991 ast2obj_stmt(void* _o)
1993 stmt_ty o = (stmt_ty)_o;
1994 PyObject *result = NULL, *value = NULL;
1995 if (!o) {
1996 Py_INCREF(Py_None);
1997 return Py_None;
2000 switch (o->kind) {
2001 case FunctionDef_kind:
2002 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2003 if (!result) goto failed;
2004 value = ast2obj_identifier(o->v.FunctionDef.name);
2005 if (!value) goto failed;
2006 if (PyObject_SetAttrString(result, "name", value) == -1)
2007 goto failed;
2008 Py_DECREF(value);
2009 value = ast2obj_arguments(o->v.FunctionDef.args);
2010 if (!value) goto failed;
2011 if (PyObject_SetAttrString(result, "args", value) == -1)
2012 goto failed;
2013 Py_DECREF(value);
2014 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2015 if (!value) goto failed;
2016 if (PyObject_SetAttrString(result, "body", value) == -1)
2017 goto failed;
2018 Py_DECREF(value);
2019 value = ast2obj_list(o->v.FunctionDef.decorators, ast2obj_expr);
2020 if (!value) goto failed;
2021 if (PyObject_SetAttrString(result, "decorators", value) == -1)
2022 goto failed;
2023 Py_DECREF(value);
2024 break;
2025 case ClassDef_kind:
2026 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2027 if (!result) goto failed;
2028 value = ast2obj_identifier(o->v.ClassDef.name);
2029 if (!value) goto failed;
2030 if (PyObject_SetAttrString(result, "name", value) == -1)
2031 goto failed;
2032 Py_DECREF(value);
2033 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2034 if (!value) goto failed;
2035 if (PyObject_SetAttrString(result, "bases", value) == -1)
2036 goto failed;
2037 Py_DECREF(value);
2038 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2039 if (!value) goto failed;
2040 if (PyObject_SetAttrString(result, "body", value) == -1)
2041 goto failed;
2042 Py_DECREF(value);
2043 break;
2044 case Return_kind:
2045 result = PyType_GenericNew(Return_type, NULL, NULL);
2046 if (!result) goto failed;
2047 value = ast2obj_expr(o->v.Return.value);
2048 if (!value) goto failed;
2049 if (PyObject_SetAttrString(result, "value", value) == -1)
2050 goto failed;
2051 Py_DECREF(value);
2052 break;
2053 case Delete_kind:
2054 result = PyType_GenericNew(Delete_type, NULL, NULL);
2055 if (!result) goto failed;
2056 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2057 if (!value) goto failed;
2058 if (PyObject_SetAttrString(result, "targets", value) == -1)
2059 goto failed;
2060 Py_DECREF(value);
2061 break;
2062 case Assign_kind:
2063 result = PyType_GenericNew(Assign_type, NULL, NULL);
2064 if (!result) goto failed;
2065 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2066 if (!value) goto failed;
2067 if (PyObject_SetAttrString(result, "targets", value) == -1)
2068 goto failed;
2069 Py_DECREF(value);
2070 value = ast2obj_expr(o->v.Assign.value);
2071 if (!value) goto failed;
2072 if (PyObject_SetAttrString(result, "value", value) == -1)
2073 goto failed;
2074 Py_DECREF(value);
2075 break;
2076 case AugAssign_kind:
2077 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2078 if (!result) goto failed;
2079 value = ast2obj_expr(o->v.AugAssign.target);
2080 if (!value) goto failed;
2081 if (PyObject_SetAttrString(result, "target", value) == -1)
2082 goto failed;
2083 Py_DECREF(value);
2084 value = ast2obj_operator(o->v.AugAssign.op);
2085 if (!value) goto failed;
2086 if (PyObject_SetAttrString(result, "op", value) == -1)
2087 goto failed;
2088 Py_DECREF(value);
2089 value = ast2obj_expr(o->v.AugAssign.value);
2090 if (!value) goto failed;
2091 if (PyObject_SetAttrString(result, "value", value) == -1)
2092 goto failed;
2093 Py_DECREF(value);
2094 break;
2095 case Print_kind:
2096 result = PyType_GenericNew(Print_type, NULL, NULL);
2097 if (!result) goto failed;
2098 value = ast2obj_expr(o->v.Print.dest);
2099 if (!value) goto failed;
2100 if (PyObject_SetAttrString(result, "dest", value) == -1)
2101 goto failed;
2102 Py_DECREF(value);
2103 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2104 if (!value) goto failed;
2105 if (PyObject_SetAttrString(result, "values", value) == -1)
2106 goto failed;
2107 Py_DECREF(value);
2108 value = ast2obj_bool(o->v.Print.nl);
2109 if (!value) goto failed;
2110 if (PyObject_SetAttrString(result, "nl", value) == -1)
2111 goto failed;
2112 Py_DECREF(value);
2113 break;
2114 case For_kind:
2115 result = PyType_GenericNew(For_type, NULL, NULL);
2116 if (!result) goto failed;
2117 value = ast2obj_expr(o->v.For.target);
2118 if (!value) goto failed;
2119 if (PyObject_SetAttrString(result, "target", value) == -1)
2120 goto failed;
2121 Py_DECREF(value);
2122 value = ast2obj_expr(o->v.For.iter);
2123 if (!value) goto failed;
2124 if (PyObject_SetAttrString(result, "iter", value) == -1)
2125 goto failed;
2126 Py_DECREF(value);
2127 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2128 if (!value) goto failed;
2129 if (PyObject_SetAttrString(result, "body", value) == -1)
2130 goto failed;
2131 Py_DECREF(value);
2132 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2133 if (!value) goto failed;
2134 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2135 goto failed;
2136 Py_DECREF(value);
2137 break;
2138 case While_kind:
2139 result = PyType_GenericNew(While_type, NULL, NULL);
2140 if (!result) goto failed;
2141 value = ast2obj_expr(o->v.While.test);
2142 if (!value) goto failed;
2143 if (PyObject_SetAttrString(result, "test", value) == -1)
2144 goto failed;
2145 Py_DECREF(value);
2146 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2147 if (!value) goto failed;
2148 if (PyObject_SetAttrString(result, "body", value) == -1)
2149 goto failed;
2150 Py_DECREF(value);
2151 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2152 if (!value) goto failed;
2153 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2154 goto failed;
2155 Py_DECREF(value);
2156 break;
2157 case If_kind:
2158 result = PyType_GenericNew(If_type, NULL, NULL);
2159 if (!result) goto failed;
2160 value = ast2obj_expr(o->v.If.test);
2161 if (!value) goto failed;
2162 if (PyObject_SetAttrString(result, "test", value) == -1)
2163 goto failed;
2164 Py_DECREF(value);
2165 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2166 if (!value) goto failed;
2167 if (PyObject_SetAttrString(result, "body", value) == -1)
2168 goto failed;
2169 Py_DECREF(value);
2170 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2171 if (!value) goto failed;
2172 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2173 goto failed;
2174 Py_DECREF(value);
2175 break;
2176 case With_kind:
2177 result = PyType_GenericNew(With_type, NULL, NULL);
2178 if (!result) goto failed;
2179 value = ast2obj_expr(o->v.With.context_expr);
2180 if (!value) goto failed;
2181 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2182 goto failed;
2183 Py_DECREF(value);
2184 value = ast2obj_expr(o->v.With.optional_vars);
2185 if (!value) goto failed;
2186 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2188 goto failed;
2189 Py_DECREF(value);
2190 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "body", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 break;
2196 case Raise_kind:
2197 result = PyType_GenericNew(Raise_type, NULL, NULL);
2198 if (!result) goto failed;
2199 value = ast2obj_expr(o->v.Raise.type);
2200 if (!value) goto failed;
2201 if (PyObject_SetAttrString(result, "type", value) == -1)
2202 goto failed;
2203 Py_DECREF(value);
2204 value = ast2obj_expr(o->v.Raise.inst);
2205 if (!value) goto failed;
2206 if (PyObject_SetAttrString(result, "inst", value) == -1)
2207 goto failed;
2208 Py_DECREF(value);
2209 value = ast2obj_expr(o->v.Raise.tback);
2210 if (!value) goto failed;
2211 if (PyObject_SetAttrString(result, "tback", value) == -1)
2212 goto failed;
2213 Py_DECREF(value);
2214 break;
2215 case TryExcept_kind:
2216 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2217 if (!result) goto failed;
2218 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2219 if (!value) goto failed;
2220 if (PyObject_SetAttrString(result, "body", value) == -1)
2221 goto failed;
2222 Py_DECREF(value);
2223 value = ast2obj_list(o->v.TryExcept.handlers,
2224 ast2obj_excepthandler);
2225 if (!value) goto failed;
2226 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2227 goto failed;
2228 Py_DECREF(value);
2229 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2230 if (!value) goto failed;
2231 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2232 goto failed;
2233 Py_DECREF(value);
2234 break;
2235 case TryFinally_kind:
2236 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2237 if (!result) goto failed;
2238 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "body", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
2243 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2244 if (!value) goto failed;
2245 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2246 goto failed;
2247 Py_DECREF(value);
2248 break;
2249 case Assert_kind:
2250 result = PyType_GenericNew(Assert_type, NULL, NULL);
2251 if (!result) goto failed;
2252 value = ast2obj_expr(o->v.Assert.test);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "test", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
2257 value = ast2obj_expr(o->v.Assert.msg);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "msg", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
2262 break;
2263 case Import_kind:
2264 result = PyType_GenericNew(Import_type, NULL, NULL);
2265 if (!result) goto failed;
2266 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2267 if (!value) goto failed;
2268 if (PyObject_SetAttrString(result, "names", value) == -1)
2269 goto failed;
2270 Py_DECREF(value);
2271 break;
2272 case ImportFrom_kind:
2273 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2274 if (!result) goto failed;
2275 value = ast2obj_identifier(o->v.ImportFrom.module);
2276 if (!value) goto failed;
2277 if (PyObject_SetAttrString(result, "module", value) == -1)
2278 goto failed;
2279 Py_DECREF(value);
2280 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2281 if (!value) goto failed;
2282 if (PyObject_SetAttrString(result, "names", value) == -1)
2283 goto failed;
2284 Py_DECREF(value);
2285 value = ast2obj_int(o->v.ImportFrom.level);
2286 if (!value) goto failed;
2287 if (PyObject_SetAttrString(result, "level", value) == -1)
2288 goto failed;
2289 Py_DECREF(value);
2290 break;
2291 case Exec_kind:
2292 result = PyType_GenericNew(Exec_type, NULL, NULL);
2293 if (!result) goto failed;
2294 value = ast2obj_expr(o->v.Exec.body);
2295 if (!value) goto failed;
2296 if (PyObject_SetAttrString(result, "body", value) == -1)
2297 goto failed;
2298 Py_DECREF(value);
2299 value = ast2obj_expr(o->v.Exec.globals);
2300 if (!value) goto failed;
2301 if (PyObject_SetAttrString(result, "globals", value) == -1)
2302 goto failed;
2303 Py_DECREF(value);
2304 value = ast2obj_expr(o->v.Exec.locals);
2305 if (!value) goto failed;
2306 if (PyObject_SetAttrString(result, "locals", value) == -1)
2307 goto failed;
2308 Py_DECREF(value);
2309 break;
2310 case Global_kind:
2311 result = PyType_GenericNew(Global_type, NULL, NULL);
2312 if (!result) goto failed;
2313 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2314 if (!value) goto failed;
2315 if (PyObject_SetAttrString(result, "names", value) == -1)
2316 goto failed;
2317 Py_DECREF(value);
2318 break;
2319 case Expr_kind:
2320 result = PyType_GenericNew(Expr_type, NULL, NULL);
2321 if (!result) goto failed;
2322 value = ast2obj_expr(o->v.Expr.value);
2323 if (!value) goto failed;
2324 if (PyObject_SetAttrString(result, "value", value) == -1)
2325 goto failed;
2326 Py_DECREF(value);
2327 break;
2328 case Pass_kind:
2329 result = PyType_GenericNew(Pass_type, NULL, NULL);
2330 if (!result) goto failed;
2331 break;
2332 case Break_kind:
2333 result = PyType_GenericNew(Break_type, NULL, NULL);
2334 if (!result) goto failed;
2335 break;
2336 case Continue_kind:
2337 result = PyType_GenericNew(Continue_type, NULL, NULL);
2338 if (!result) goto failed;
2339 break;
2341 value = ast2obj_int(o->lineno);
2342 if (!value) goto failed;
2343 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2344 goto failed;
2345 Py_DECREF(value);
2346 value = ast2obj_int(o->col_offset);
2347 if (!value) goto failed;
2348 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2349 goto failed;
2350 Py_DECREF(value);
2351 return result;
2352 failed:
2353 Py_XDECREF(value);
2354 Py_XDECREF(result);
2355 return NULL;
2358 PyObject*
2359 ast2obj_expr(void* _o)
2361 expr_ty o = (expr_ty)_o;
2362 PyObject *result = NULL, *value = NULL;
2363 if (!o) {
2364 Py_INCREF(Py_None);
2365 return Py_None;
2368 switch (o->kind) {
2369 case BoolOp_kind:
2370 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2371 if (!result) goto failed;
2372 value = ast2obj_boolop(o->v.BoolOp.op);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "op", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
2377 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "values", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 break;
2383 case BinOp_kind:
2384 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2385 if (!result) goto failed;
2386 value = ast2obj_expr(o->v.BinOp.left);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "left", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
2391 value = ast2obj_operator(o->v.BinOp.op);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "op", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 value = ast2obj_expr(o->v.BinOp.right);
2397 if (!value) goto failed;
2398 if (PyObject_SetAttrString(result, "right", value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
2401 break;
2402 case UnaryOp_kind:
2403 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2404 if (!result) goto failed;
2405 value = ast2obj_unaryop(o->v.UnaryOp.op);
2406 if (!value) goto failed;
2407 if (PyObject_SetAttrString(result, "op", value) == -1)
2408 goto failed;
2409 Py_DECREF(value);
2410 value = ast2obj_expr(o->v.UnaryOp.operand);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "operand", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
2415 break;
2416 case Lambda_kind:
2417 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2418 if (!result) goto failed;
2419 value = ast2obj_arguments(o->v.Lambda.args);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "args", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
2424 value = ast2obj_expr(o->v.Lambda.body);
2425 if (!value) goto failed;
2426 if (PyObject_SetAttrString(result, "body", value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
2429 break;
2430 case IfExp_kind:
2431 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2432 if (!result) goto failed;
2433 value = ast2obj_expr(o->v.IfExp.test);
2434 if (!value) goto failed;
2435 if (PyObject_SetAttrString(result, "test", value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
2438 value = ast2obj_expr(o->v.IfExp.body);
2439 if (!value) goto failed;
2440 if (PyObject_SetAttrString(result, "body", value) == -1)
2441 goto failed;
2442 Py_DECREF(value);
2443 value = ast2obj_expr(o->v.IfExp.orelse);
2444 if (!value) goto failed;
2445 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2446 goto failed;
2447 Py_DECREF(value);
2448 break;
2449 case Dict_kind:
2450 result = PyType_GenericNew(Dict_type, NULL, NULL);
2451 if (!result) goto failed;
2452 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2453 if (!value) goto failed;
2454 if (PyObject_SetAttrString(result, "keys", value) == -1)
2455 goto failed;
2456 Py_DECREF(value);
2457 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2458 if (!value) goto failed;
2459 if (PyObject_SetAttrString(result, "values", value) == -1)
2460 goto failed;
2461 Py_DECREF(value);
2462 break;
2463 case ListComp_kind:
2464 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2465 if (!result) goto failed;
2466 value = ast2obj_expr(o->v.ListComp.elt);
2467 if (!value) goto failed;
2468 if (PyObject_SetAttrString(result, "elt", value) == -1)
2469 goto failed;
2470 Py_DECREF(value);
2471 value = ast2obj_list(o->v.ListComp.generators,
2472 ast2obj_comprehension);
2473 if (!value) goto failed;
2474 if (PyObject_SetAttrString(result, "generators", value) == -1)
2475 goto failed;
2476 Py_DECREF(value);
2477 break;
2478 case GeneratorExp_kind:
2479 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2480 if (!result) goto failed;
2481 value = ast2obj_expr(o->v.GeneratorExp.elt);
2482 if (!value) goto failed;
2483 if (PyObject_SetAttrString(result, "elt", value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
2486 value = ast2obj_list(o->v.GeneratorExp.generators,
2487 ast2obj_comprehension);
2488 if (!value) goto failed;
2489 if (PyObject_SetAttrString(result, "generators", value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 break;
2493 case Yield_kind:
2494 result = PyType_GenericNew(Yield_type, NULL, NULL);
2495 if (!result) goto failed;
2496 value = ast2obj_expr(o->v.Yield.value);
2497 if (!value) goto failed;
2498 if (PyObject_SetAttrString(result, "value", value) == -1)
2499 goto failed;
2500 Py_DECREF(value);
2501 break;
2502 case Compare_kind:
2503 result = PyType_GenericNew(Compare_type, NULL, NULL);
2504 if (!result) goto failed;
2505 value = ast2obj_expr(o->v.Compare.left);
2506 if (!value) goto failed;
2507 if (PyObject_SetAttrString(result, "left", value) == -1)
2508 goto failed;
2509 Py_DECREF(value);
2511 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2512 value = PyList_New(n);
2513 if (!value) goto failed;
2514 for(i = 0; i < n; i++)
2515 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "ops", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
2521 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2522 if (!value) goto failed;
2523 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2524 goto failed;
2525 Py_DECREF(value);
2526 break;
2527 case Call_kind:
2528 result = PyType_GenericNew(Call_type, NULL, NULL);
2529 if (!result) goto failed;
2530 value = ast2obj_expr(o->v.Call.func);
2531 if (!value) goto failed;
2532 if (PyObject_SetAttrString(result, "func", value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2536 if (!value) goto failed;
2537 if (PyObject_SetAttrString(result, "args", value) == -1)
2538 goto failed;
2539 Py_DECREF(value);
2540 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2541 if (!value) goto failed;
2542 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2543 goto failed;
2544 Py_DECREF(value);
2545 value = ast2obj_expr(o->v.Call.starargs);
2546 if (!value) goto failed;
2547 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2548 goto failed;
2549 Py_DECREF(value);
2550 value = ast2obj_expr(o->v.Call.kwargs);
2551 if (!value) goto failed;
2552 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2553 goto failed;
2554 Py_DECREF(value);
2555 break;
2556 case Repr_kind:
2557 result = PyType_GenericNew(Repr_type, NULL, NULL);
2558 if (!result) goto failed;
2559 value = ast2obj_expr(o->v.Repr.value);
2560 if (!value) goto failed;
2561 if (PyObject_SetAttrString(result, "value", value) == -1)
2562 goto failed;
2563 Py_DECREF(value);
2564 break;
2565 case Num_kind:
2566 result = PyType_GenericNew(Num_type, NULL, NULL);
2567 if (!result) goto failed;
2568 value = ast2obj_object(o->v.Num.n);
2569 if (!value) goto failed;
2570 if (PyObject_SetAttrString(result, "n", value) == -1)
2571 goto failed;
2572 Py_DECREF(value);
2573 break;
2574 case Str_kind:
2575 result = PyType_GenericNew(Str_type, NULL, NULL);
2576 if (!result) goto failed;
2577 value = ast2obj_string(o->v.Str.s);
2578 if (!value) goto failed;
2579 if (PyObject_SetAttrString(result, "s", value) == -1)
2580 goto failed;
2581 Py_DECREF(value);
2582 break;
2583 case Attribute_kind:
2584 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2585 if (!result) goto failed;
2586 value = ast2obj_expr(o->v.Attribute.value);
2587 if (!value) goto failed;
2588 if (PyObject_SetAttrString(result, "value", value) == -1)
2589 goto failed;
2590 Py_DECREF(value);
2591 value = ast2obj_identifier(o->v.Attribute.attr);
2592 if (!value) goto failed;
2593 if (PyObject_SetAttrString(result, "attr", value) == -1)
2594 goto failed;
2595 Py_DECREF(value);
2596 value = ast2obj_expr_context(o->v.Attribute.ctx);
2597 if (!value) goto failed;
2598 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
2601 break;
2602 case Subscript_kind:
2603 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2604 if (!result) goto failed;
2605 value = ast2obj_expr(o->v.Subscript.value);
2606 if (!value) goto failed;
2607 if (PyObject_SetAttrString(result, "value", value) == -1)
2608 goto failed;
2609 Py_DECREF(value);
2610 value = ast2obj_slice(o->v.Subscript.slice);
2611 if (!value) goto failed;
2612 if (PyObject_SetAttrString(result, "slice", value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
2615 value = ast2obj_expr_context(o->v.Subscript.ctx);
2616 if (!value) goto failed;
2617 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
2620 break;
2621 case Name_kind:
2622 result = PyType_GenericNew(Name_type, NULL, NULL);
2623 if (!result) goto failed;
2624 value = ast2obj_identifier(o->v.Name.id);
2625 if (!value) goto failed;
2626 if (PyObject_SetAttrString(result, "id", value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
2629 value = ast2obj_expr_context(o->v.Name.ctx);
2630 if (!value) goto failed;
2631 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 break;
2635 case List_kind:
2636 result = PyType_GenericNew(List_type, NULL, NULL);
2637 if (!result) goto failed;
2638 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "elts", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
2643 value = ast2obj_expr_context(o->v.List.ctx);
2644 if (!value) goto failed;
2645 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
2648 break;
2649 case Tuple_kind:
2650 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2651 if (!result) goto failed;
2652 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "elts", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 value = ast2obj_expr_context(o->v.Tuple.ctx);
2658 if (!value) goto failed;
2659 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 break;
2664 value = ast2obj_int(o->lineno);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_int(o->col_offset);
2670 if (!value) goto failed;
2671 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2672 goto failed;
2673 Py_DECREF(value);
2674 return result;
2675 failed:
2676 Py_XDECREF(value);
2677 Py_XDECREF(result);
2678 return NULL;
2681 PyObject* ast2obj_expr_context(expr_context_ty o)
2683 switch(o) {
2684 case Load:
2685 Py_INCREF(Load_singleton);
2686 return Load_singleton;
2687 case Store:
2688 Py_INCREF(Store_singleton);
2689 return Store_singleton;
2690 case Del:
2691 Py_INCREF(Del_singleton);
2692 return Del_singleton;
2693 case AugLoad:
2694 Py_INCREF(AugLoad_singleton);
2695 return AugLoad_singleton;
2696 case AugStore:
2697 Py_INCREF(AugStore_singleton);
2698 return AugStore_singleton;
2699 case Param:
2700 Py_INCREF(Param_singleton);
2701 return Param_singleton;
2703 return NULL; /* cannot happen */
2705 PyObject*
2706 ast2obj_slice(void* _o)
2708 slice_ty o = (slice_ty)_o;
2709 PyObject *result = NULL, *value = NULL;
2710 if (!o) {
2711 Py_INCREF(Py_None);
2712 return Py_None;
2715 switch (o->kind) {
2716 case Ellipsis_kind:
2717 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2718 if (!result) goto failed;
2719 break;
2720 case Slice_kind:
2721 result = PyType_GenericNew(Slice_type, NULL, NULL);
2722 if (!result) goto failed;
2723 value = ast2obj_expr(o->v.Slice.lower);
2724 if (!value) goto failed;
2725 if (PyObject_SetAttrString(result, "lower", value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_expr(o->v.Slice.upper);
2729 if (!value) goto failed;
2730 if (PyObject_SetAttrString(result, "upper", value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 value = ast2obj_expr(o->v.Slice.step);
2734 if (!value) goto failed;
2735 if (PyObject_SetAttrString(result, "step", value) == -1)
2736 goto failed;
2737 Py_DECREF(value);
2738 break;
2739 case ExtSlice_kind:
2740 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2741 if (!result) goto failed;
2742 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2743 if (!value) goto failed;
2744 if (PyObject_SetAttrString(result, "dims", value) == -1)
2745 goto failed;
2746 Py_DECREF(value);
2747 break;
2748 case Index_kind:
2749 result = PyType_GenericNew(Index_type, NULL, NULL);
2750 if (!result) goto failed;
2751 value = ast2obj_expr(o->v.Index.value);
2752 if (!value) goto failed;
2753 if (PyObject_SetAttrString(result, "value", value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 break;
2758 return result;
2759 failed:
2760 Py_XDECREF(value);
2761 Py_XDECREF(result);
2762 return NULL;
2765 PyObject* ast2obj_boolop(boolop_ty o)
2767 switch(o) {
2768 case And:
2769 Py_INCREF(And_singleton);
2770 return And_singleton;
2771 case Or:
2772 Py_INCREF(Or_singleton);
2773 return Or_singleton;
2775 return NULL; /* cannot happen */
2777 PyObject* ast2obj_operator(operator_ty o)
2779 switch(o) {
2780 case Add:
2781 Py_INCREF(Add_singleton);
2782 return Add_singleton;
2783 case Sub:
2784 Py_INCREF(Sub_singleton);
2785 return Sub_singleton;
2786 case Mult:
2787 Py_INCREF(Mult_singleton);
2788 return Mult_singleton;
2789 case Div:
2790 Py_INCREF(Div_singleton);
2791 return Div_singleton;
2792 case Mod:
2793 Py_INCREF(Mod_singleton);
2794 return Mod_singleton;
2795 case Pow:
2796 Py_INCREF(Pow_singleton);
2797 return Pow_singleton;
2798 case LShift:
2799 Py_INCREF(LShift_singleton);
2800 return LShift_singleton;
2801 case RShift:
2802 Py_INCREF(RShift_singleton);
2803 return RShift_singleton;
2804 case BitOr:
2805 Py_INCREF(BitOr_singleton);
2806 return BitOr_singleton;
2807 case BitXor:
2808 Py_INCREF(BitXor_singleton);
2809 return BitXor_singleton;
2810 case BitAnd:
2811 Py_INCREF(BitAnd_singleton);
2812 return BitAnd_singleton;
2813 case FloorDiv:
2814 Py_INCREF(FloorDiv_singleton);
2815 return FloorDiv_singleton;
2817 return NULL; /* cannot happen */
2819 PyObject* ast2obj_unaryop(unaryop_ty o)
2821 switch(o) {
2822 case Invert:
2823 Py_INCREF(Invert_singleton);
2824 return Invert_singleton;
2825 case Not:
2826 Py_INCREF(Not_singleton);
2827 return Not_singleton;
2828 case UAdd:
2829 Py_INCREF(UAdd_singleton);
2830 return UAdd_singleton;
2831 case USub:
2832 Py_INCREF(USub_singleton);
2833 return USub_singleton;
2835 return NULL; /* cannot happen */
2837 PyObject* ast2obj_cmpop(cmpop_ty o)
2839 switch(o) {
2840 case Eq:
2841 Py_INCREF(Eq_singleton);
2842 return Eq_singleton;
2843 case NotEq:
2844 Py_INCREF(NotEq_singleton);
2845 return NotEq_singleton;
2846 case Lt:
2847 Py_INCREF(Lt_singleton);
2848 return Lt_singleton;
2849 case LtE:
2850 Py_INCREF(LtE_singleton);
2851 return LtE_singleton;
2852 case Gt:
2853 Py_INCREF(Gt_singleton);
2854 return Gt_singleton;
2855 case GtE:
2856 Py_INCREF(GtE_singleton);
2857 return GtE_singleton;
2858 case Is:
2859 Py_INCREF(Is_singleton);
2860 return Is_singleton;
2861 case IsNot:
2862 Py_INCREF(IsNot_singleton);
2863 return IsNot_singleton;
2864 case In:
2865 Py_INCREF(In_singleton);
2866 return In_singleton;
2867 case NotIn:
2868 Py_INCREF(NotIn_singleton);
2869 return NotIn_singleton;
2871 return NULL; /* cannot happen */
2873 PyObject*
2874 ast2obj_comprehension(void* _o)
2876 comprehension_ty o = (comprehension_ty)_o;
2877 PyObject *result = NULL, *value = NULL;
2878 if (!o) {
2879 Py_INCREF(Py_None);
2880 return Py_None;
2883 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2884 if (!result) return NULL;
2885 value = ast2obj_expr(o->target);
2886 if (!value) goto failed;
2887 if (PyObject_SetAttrString(result, "target", value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_expr(o->iter);
2891 if (!value) goto failed;
2892 if (PyObject_SetAttrString(result, "iter", value) == -1)
2893 goto failed;
2894 Py_DECREF(value);
2895 value = ast2obj_list(o->ifs, ast2obj_expr);
2896 if (!value) goto failed;
2897 if (PyObject_SetAttrString(result, "ifs", value) == -1)
2898 goto failed;
2899 Py_DECREF(value);
2900 return result;
2901 failed:
2902 Py_XDECREF(value);
2903 Py_XDECREF(result);
2904 return NULL;
2907 PyObject*
2908 ast2obj_excepthandler(void* _o)
2910 excepthandler_ty o = (excepthandler_ty)_o;
2911 PyObject *result = NULL, *value = NULL;
2912 if (!o) {
2913 Py_INCREF(Py_None);
2914 return Py_None;
2917 result = PyType_GenericNew(excepthandler_type, NULL, NULL);
2918 if (!result) return NULL;
2919 value = ast2obj_expr(o->type);
2920 if (!value) goto failed;
2921 if (PyObject_SetAttrString(result, "type", value) == -1)
2922 goto failed;
2923 Py_DECREF(value);
2924 value = ast2obj_expr(o->name);
2925 if (!value) goto failed;
2926 if (PyObject_SetAttrString(result, "name", value) == -1)
2927 goto failed;
2928 Py_DECREF(value);
2929 value = ast2obj_list(o->body, ast2obj_stmt);
2930 if (!value) goto failed;
2931 if (PyObject_SetAttrString(result, "body", value) == -1)
2932 goto failed;
2933 Py_DECREF(value);
2934 value = ast2obj_int(o->lineno);
2935 if (!value) goto failed;
2936 if (PyObject_SetAttrString(result, "lineno", value) == -1)
2937 goto failed;
2938 Py_DECREF(value);
2939 value = ast2obj_int(o->col_offset);
2940 if (!value) goto failed;
2941 if (PyObject_SetAttrString(result, "col_offset", value) == -1)
2942 goto failed;
2943 Py_DECREF(value);
2944 return result;
2945 failed:
2946 Py_XDECREF(value);
2947 Py_XDECREF(result);
2948 return NULL;
2951 PyObject*
2952 ast2obj_arguments(void* _o)
2954 arguments_ty o = (arguments_ty)_o;
2955 PyObject *result = NULL, *value = NULL;
2956 if (!o) {
2957 Py_INCREF(Py_None);
2958 return Py_None;
2961 result = PyType_GenericNew(arguments_type, NULL, NULL);
2962 if (!result) return NULL;
2963 value = ast2obj_list(o->args, ast2obj_expr);
2964 if (!value) goto failed;
2965 if (PyObject_SetAttrString(result, "args", value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 value = ast2obj_identifier(o->vararg);
2969 if (!value) goto failed;
2970 if (PyObject_SetAttrString(result, "vararg", value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 value = ast2obj_identifier(o->kwarg);
2974 if (!value) goto failed;
2975 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 value = ast2obj_list(o->defaults, ast2obj_expr);
2979 if (!value) goto failed;
2980 if (PyObject_SetAttrString(result, "defaults", value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
2983 return result;
2984 failed:
2985 Py_XDECREF(value);
2986 Py_XDECREF(result);
2987 return NULL;
2990 PyObject*
2991 ast2obj_keyword(void* _o)
2993 keyword_ty o = (keyword_ty)_o;
2994 PyObject *result = NULL, *value = NULL;
2995 if (!o) {
2996 Py_INCREF(Py_None);
2997 return Py_None;
3000 result = PyType_GenericNew(keyword_type, NULL, NULL);
3001 if (!result) return NULL;
3002 value = ast2obj_identifier(o->arg);
3003 if (!value) goto failed;
3004 if (PyObject_SetAttrString(result, "arg", value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
3007 value = ast2obj_expr(o->value);
3008 if (!value) goto failed;
3009 if (PyObject_SetAttrString(result, "value", value) == -1)
3010 goto failed;
3011 Py_DECREF(value);
3012 return result;
3013 failed:
3014 Py_XDECREF(value);
3015 Py_XDECREF(result);
3016 return NULL;
3019 PyObject*
3020 ast2obj_alias(void* _o)
3022 alias_ty o = (alias_ty)_o;
3023 PyObject *result = NULL, *value = NULL;
3024 if (!o) {
3025 Py_INCREF(Py_None);
3026 return Py_None;
3029 result = PyType_GenericNew(alias_type, NULL, NULL);
3030 if (!result) return NULL;
3031 value = ast2obj_identifier(o->name);
3032 if (!value) goto failed;
3033 if (PyObject_SetAttrString(result, "name", value) == -1)
3034 goto failed;
3035 Py_DECREF(value);
3036 value = ast2obj_identifier(o->asname);
3037 if (!value) goto failed;
3038 if (PyObject_SetAttrString(result, "asname", value) == -1)
3039 goto failed;
3040 Py_DECREF(value);
3041 return result;
3042 failed:
3043 Py_XDECREF(value);
3044 Py_XDECREF(result);
3045 return NULL;
3049 PyMODINIT_FUNC
3050 init_ast(void)
3052 PyObject *m, *d;
3053 if (!init_types()) return;
3054 m = Py_InitModule3("_ast", NULL, NULL);
3055 if (!m) return;
3056 d = PyModule_GetDict(m);
3057 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;
3058 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
3059 return;
3060 if (PyModule_AddStringConstant(m, "__version__", "53731") < 0)
3061 return;
3062 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
3063 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
3064 return;
3065 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
3066 < 0) return;
3067 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
3068 0) return;
3069 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
3070 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
3071 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
3072 < 0) return;
3073 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
3074 return;
3075 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
3076 return;
3077 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
3078 return;
3079 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
3080 return;
3081 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
3082 0) return;
3083 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
3084 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
3085 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
3086 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
3087 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
3088 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
3089 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
3090 0) return;
3091 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
3092 0) return;
3093 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
3094 return;
3095 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
3096 return;
3097 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
3098 0) return;
3099 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
3100 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
3101 return;
3102 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
3103 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
3104 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
3105 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
3106 return;
3107 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
3108 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
3109 return;
3110 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
3111 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
3112 return;
3113 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
3114 return;
3115 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
3116 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
3117 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
3118 return;
3119 if (PyDict_SetItemString(d, "GeneratorExp",
3120 (PyObject*)GeneratorExp_type) < 0) return;
3121 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
3122 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
3123 return;
3124 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
3125 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
3126 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
3127 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
3128 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
3129 0) return;
3130 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
3131 0) return;
3132 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
3133 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
3134 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
3135 if (PyDict_SetItemString(d, "expr_context",
3136 (PyObject*)expr_context_type) < 0) return;
3137 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
3138 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
3139 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
3140 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
3141 return;
3142 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
3143 return;
3144 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
3145 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
3146 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
3147 return;
3148 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
3149 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
3150 return;
3151 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
3152 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
3153 return;
3154 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
3155 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
3156 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
3157 return;
3158 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
3159 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
3160 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
3161 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
3162 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
3163 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
3164 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
3165 return;
3166 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
3167 return;
3168 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
3169 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
3170 return;
3171 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
3172 return;
3173 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
3174 return;
3175 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
3176 return;
3177 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
3178 return;
3179 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
3180 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
3181 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
3182 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
3183 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
3184 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
3185 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
3186 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
3187 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
3188 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
3189 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
3190 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
3191 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
3192 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
3193 if (PyDict_SetItemString(d, "comprehension",
3194 (PyObject*)comprehension_type) < 0) return;
3195 if (PyDict_SetItemString(d, "excepthandler",
3196 (PyObject*)excepthandler_type) < 0) return;
3197 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
3198 0) return;
3199 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
3200 return;
3201 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
3205 PyObject* PyAST_mod2obj(mod_ty t)
3207 init_types();
3208 return ast2obj_mod(t);