blob: f076c7e9645bdef704afe15e0ace4d4e59c5d697 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson7f670e52011-05-27 14:01:01 -05005 __version__ 9b11cc4e2918.
Thomas Wouterscf297e42007-02-23 15:07:44 +00006
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.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Neal Norwitz207c9f32008-03-31 04:42:11 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000045 "decorator_list",
Neal Norwitzc1505362006-12-28 06:47:50 +000046 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Neal Norwitz53d960c2006-02-28 22:47:29 +000048static PyTypeObject *ClassDef_type;
49static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000050 "name",
51 "bases",
Guido van Rossum52cc1d82007-03-18 15:41:51 +000052 "keywords",
53 "starargs",
54 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000055 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000056 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000057};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Return_type;
59static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "value",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Delete_type;
63static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Assign_type;
67static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000068 "targets",
69 "value",
70};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *AugAssign_type;
72static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000073 "target",
74 "op",
75 "value",
76};
Neal Norwitz53d960c2006-02-28 22:47:29 +000077static PyTypeObject *For_type;
78static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000079 "target",
80 "iter",
81 "body",
82 "orelse",
83};
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static PyTypeObject *While_type;
85static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000086 "test",
87 "body",
88 "orelse",
89};
Neal Norwitz53d960c2006-02-28 22:47:29 +000090static PyTypeObject *If_type;
91static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000092 "test",
93 "body",
94 "orelse",
95};
Neal Norwitz53d960c2006-02-28 22:47:29 +000096static PyTypeObject *With_type;
97static char *With_fields[]={
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -050098 "items",
Guido van Rossumc2e20742006-02-27 22:32:47 +000099 "body",
100};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000101static PyTypeObject *Raise_type;
102static char *Raise_fields[]={
Collin Winter828f04a2007-08-31 00:04:24 +0000103 "exc",
104 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000105};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000106static PyTypeObject *TryExcept_type;
107static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000108 "body",
109 "handlers",
110 "orelse",
111};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000112static PyTypeObject *TryFinally_type;
113static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000114 "body",
115 "finalbody",
116};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000117static PyTypeObject *Assert_type;
118static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000119 "test",
120 "msg",
121};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000122static PyTypeObject *Import_type;
123static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000124 "names",
125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *ImportFrom_type;
127static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128 "module",
129 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000130 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000131};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000132static PyTypeObject *Global_type;
133static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000134 "names",
135};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000136static PyTypeObject *Nonlocal_type;
137static char *Nonlocal_fields[]={
138 "names",
139};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000140static PyTypeObject *Expr_type;
141static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000142 "value",
143};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000144static PyTypeObject *Pass_type;
145static PyTypeObject *Break_type;
146static PyTypeObject *Continue_type;
147static PyTypeObject *expr_type;
148static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000149 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000150 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000151};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000152static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000153static PyTypeObject *BoolOp_type;
154static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000155 "op",
156 "values",
157};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000158static PyTypeObject *BinOp_type;
159static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000160 "left",
161 "op",
162 "right",
163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *UnaryOp_type;
165static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000166 "op",
167 "operand",
168};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000169static PyTypeObject *Lambda_type;
170static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000171 "args",
172 "body",
173};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000174static PyTypeObject *IfExp_type;
175static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000176 "test",
177 "body",
178 "orelse",
179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *Dict_type;
181static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000182 "keys",
183 "values",
184};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000185static PyTypeObject *Set_type;
186static char *Set_fields[]={
187 "elts",
188};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000189static PyTypeObject *ListComp_type;
190static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000191 "elt",
192 "generators",
193};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000194static PyTypeObject *SetComp_type;
195static char *SetComp_fields[]={
196 "elt",
197 "generators",
198};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000199static PyTypeObject *DictComp_type;
200static char *DictComp_fields[]={
201 "key",
202 "value",
203 "generators",
204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *GeneratorExp_type;
206static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207 "elt",
208 "generators",
209};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static PyTypeObject *Yield_type;
211static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000212 "value",
213};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000214static PyTypeObject *Compare_type;
215static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000216 "left",
217 "ops",
218 "comparators",
219};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000220static PyTypeObject *Call_type;
221static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000222 "func",
223 "args",
224 "keywords",
225 "starargs",
226 "kwargs",
227};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000228static PyTypeObject *Num_type;
229static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230 "n",
231};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000232static PyTypeObject *Str_type;
233static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000234 "s",
235};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000236static PyTypeObject *Bytes_type;
237static char *Bytes_fields[]={
238 "s",
239};
Georg Brandl52318d62006-09-06 07:06:08 +0000240static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000241static PyTypeObject *Attribute_type;
242static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000243 "value",
244 "attr",
245 "ctx",
246};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000247static PyTypeObject *Subscript_type;
248static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000249 "value",
250 "slice",
251 "ctx",
252};
Guido van Rossum0368b722007-05-11 16:50:42 +0000253static PyTypeObject *Starred_type;
254static char *Starred_fields[]={
255 "value",
256 "ctx",
257};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000258static PyTypeObject *Name_type;
259static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000260 "id",
261 "ctx",
262};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000263static PyTypeObject *List_type;
264static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000265 "elts",
266 "ctx",
267};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000268static PyTypeObject *Tuple_type;
269static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000270 "elts",
271 "ctx",
272};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000274static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
275*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
276static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *Load_type;
278static PyTypeObject *Store_type;
279static PyTypeObject *Del_type;
280static PyTypeObject *AugLoad_type;
281static PyTypeObject *AugStore_type;
282static PyTypeObject *Param_type;
283static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000284static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *Slice_type;
286static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287 "lower",
288 "upper",
289 "step",
290};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000291static PyTypeObject *ExtSlice_type;
292static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000293 "dims",
294};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000295static PyTypeObject *Index_type;
296static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000297 "value",
298};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000299static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000300static PyObject *And_singleton, *Or_singleton;
301static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000302static PyTypeObject *And_type;
303static PyTypeObject *Or_type;
304static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000305static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
306*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
307*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
308*FloorDiv_singleton;
309static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000310static PyTypeObject *Add_type;
311static PyTypeObject *Sub_type;
312static PyTypeObject *Mult_type;
313static PyTypeObject *Div_type;
314static PyTypeObject *Mod_type;
315static PyTypeObject *Pow_type;
316static PyTypeObject *LShift_type;
317static PyTypeObject *RShift_type;
318static PyTypeObject *BitOr_type;
319static PyTypeObject *BitXor_type;
320static PyTypeObject *BitAnd_type;
321static PyTypeObject *FloorDiv_type;
322static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000323static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
324*USub_singleton;
325static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000326static PyTypeObject *Invert_type;
327static PyTypeObject *Not_type;
328static PyTypeObject *UAdd_type;
329static PyTypeObject *USub_type;
330static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000331static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
332*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
333*NotIn_singleton;
334static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000335static PyTypeObject *Eq_type;
336static PyTypeObject *NotEq_type;
337static PyTypeObject *Lt_type;
338static PyTypeObject *LtE_type;
339static PyTypeObject *Gt_type;
340static PyTypeObject *GtE_type;
341static PyTypeObject *Is_type;
342static PyTypeObject *IsNot_type;
343static PyTypeObject *In_type;
344static PyTypeObject *NotIn_type;
345static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000347static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000348 "target",
349 "iter",
350 "ifs",
351};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000353static char *excepthandler_attributes[] = {
354 "lineno",
355 "col_offset",
356};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000357static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000358static PyTypeObject *ExceptHandler_type;
359static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000360 "type",
361 "name",
362 "body",
363};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367 "args",
368 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000369 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000370 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000371 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000372 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000373 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000374 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000375};
Neal Norwitzc1505362006-12-28 06:47:50 +0000376static PyTypeObject *arg_type;
377static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000378static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000379 "arg",
380 "annotation",
381};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000382static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000383static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000385 "arg",
386 "value",
387};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000388static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000389static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000390static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000391 "name",
392 "asname",
393};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500394static PyTypeObject *withitem_type;
395static PyObject* ast2obj_withitem(void*);
396static char *withitem_fields[]={
397 "context_expr",
398 "optional_vars",
399};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000400
401
Neal Norwitz207c9f32008-03-31 04:42:11 +0000402static int
403ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
404{
405 Py_ssize_t i, numfields = 0;
406 int res = -1;
407 PyObject *key, *value, *fields;
408 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
409 if (!fields)
410 PyErr_Clear();
411 if (fields) {
412 numfields = PySequence_Size(fields);
413 if (numfields == -1)
414 goto cleanup;
415 }
416 res = 0; /* if no error occurs, this stays 0 to the end */
417 if (PyTuple_GET_SIZE(args) > 0) {
418 if (numfields != PyTuple_GET_SIZE(args)) {
419 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000420 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000421 Py_TYPE(self)->tp_name,
422 numfields == 0 ? "" : "either 0 or ",
423 numfields, numfields == 1 ? "" : "s");
424 res = -1;
425 goto cleanup;
426 }
427 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
428 /* cannot be reached when fields is NULL */
429 PyObject *name = PySequence_GetItem(fields, i);
430 if (!name) {
431 res = -1;
432 goto cleanup;
433 }
434 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
435 Py_DECREF(name);
436 if (res < 0)
437 goto cleanup;
438 }
439 }
440 if (kw) {
441 i = 0; /* needed by PyDict_Next */
442 while (PyDict_Next(kw, &i, &key, &value)) {
443 res = PyObject_SetAttr(self, key, value);
444 if (res < 0)
445 goto cleanup;
446 }
447 }
448 cleanup:
449 Py_XDECREF(fields);
450 return res;
451}
452
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000453/* Pickling support */
454static PyObject *
455ast_type_reduce(PyObject *self, PyObject *unused)
456{
457 PyObject *res;
458 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
459 if (dict == NULL) {
460 if (PyErr_ExceptionMatches(PyExc_AttributeError))
461 PyErr_Clear();
462 else
463 return NULL;
464 }
465 if (dict) {
466 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
467 Py_DECREF(dict);
468 return res;
469 }
470 return Py_BuildValue("O()", Py_TYPE(self));
471}
472
473static PyMethodDef ast_type_methods[] = {
474 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
475 {NULL}
476};
477
Neal Norwitz207c9f32008-03-31 04:42:11 +0000478static PyTypeObject AST_type = {
479 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000480 "_ast.AST",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000481 sizeof(PyObject),
482 0,
483 0, /* tp_dealloc */
484 0, /* tp_print */
485 0, /* tp_getattr */
486 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000487 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000488 0, /* tp_repr */
489 0, /* tp_as_number */
490 0, /* tp_as_sequence */
491 0, /* tp_as_mapping */
492 0, /* tp_hash */
493 0, /* tp_call */
494 0, /* tp_str */
495 PyObject_GenericGetAttr, /* tp_getattro */
496 PyObject_GenericSetAttr, /* tp_setattro */
497 0, /* tp_as_buffer */
498 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
499 0, /* tp_doc */
500 0, /* tp_traverse */
501 0, /* tp_clear */
502 0, /* tp_richcompare */
503 0, /* tp_weaklistoffset */
504 0, /* tp_iter */
505 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000506 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000507 0, /* tp_members */
508 0, /* tp_getset */
509 0, /* tp_base */
510 0, /* tp_dict */
511 0, /* tp_descr_get */
512 0, /* tp_descr_set */
513 0, /* tp_dictoffset */
514 (initproc)ast_type_init, /* tp_init */
515 PyType_GenericAlloc, /* tp_alloc */
516 PyType_GenericNew, /* tp_new */
517 PyObject_Del, /* tp_free */
518};
519
520
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000521static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
522{
523 PyObject *fnames, *result;
524 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000525 fnames = PyTuple_New(num_fields);
526 if (!fnames) return NULL;
527 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000528 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000529 if (!field) {
530 Py_DECREF(fnames);
531 return NULL;
532 }
533 PyTuple_SET_ITEM(fnames, i, field);
534 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000535 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000536 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000537 Py_DECREF(fnames);
538 return (PyTypeObject*)result;
539}
540
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000541static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
542{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000544 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000545 if (!l)
546 return 0;
547 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000548 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000549 if (!s) {
550 Py_DECREF(l);
551 return 0;
552 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000553 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000554 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000555 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
556 Py_DECREF(l);
557 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000558}
559
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000560/* Conversion AST -> Python */
561
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000562static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
563{
564 int i, n = asdl_seq_LEN(seq);
565 PyObject *result = PyList_New(n);
566 PyObject *value;
567 if (!result)
568 return NULL;
569 for (i = 0; i < n; i++) {
570 value = func(asdl_seq_GET(seq, i));
571 if (!value) {
572 Py_DECREF(result);
573 return NULL;
574 }
575 PyList_SET_ITEM(result, i, value);
576 }
577 return result;
578}
579
580static PyObject* ast2obj_object(void *o)
581{
582 if (!o)
583 o = Py_None;
584 Py_INCREF((PyObject*)o);
585 return (PyObject*)o;
586}
587#define ast2obj_identifier ast2obj_object
588#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000589
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000590static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000591{
Christian Heimes217cfd12007-12-02 14:31:20 +0000592 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000593}
594
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000595/* Conversion Python -> AST */
596
597static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
598{
599 if (obj == Py_None)
600 obj = NULL;
601 if (obj)
602 PyArena_AddPyObject(arena, obj);
603 Py_XINCREF(obj);
604 *out = obj;
605 return 0;
606}
607
608#define obj2ast_identifier obj2ast_object
609#define obj2ast_string obj2ast_object
610
611static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
612{
613 int i;
614 if (!PyLong_Check(obj)) {
615 PyObject *s = PyObject_Repr(obj);
616 if (s == NULL) return 1;
617 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000618 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000619 Py_DECREF(s);
620 return 1;
621 }
622
623 i = (int)PyLong_AsLong(obj);
624 if (i == -1 && PyErr_Occurred())
625 return 1;
626 *out = i;
627 return 0;
628}
629
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000630static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000631{
632 PyObject *empty_tuple, *d;
633 if (PyType_Ready(&AST_type) < 0)
634 return -1;
635 d = AST_type.tp_dict;
636 empty_tuple = PyTuple_New(0);
637 if (!empty_tuple ||
638 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
639 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
640 Py_XDECREF(empty_tuple);
641 return -1;
642 }
643 Py_DECREF(empty_tuple);
644 return 0;
645}
646
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000647
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648static int init_types(void)
649{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000652 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000653 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!mod_type) return 0;
655 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 Interactive_type = make_type("Interactive", mod_type,
659 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 Expression_type = make_type("Expression", mod_type, Expression_fields,
662 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000666 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000668 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000670 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000672 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!If_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500688 With_type = make_type("With", stmt_type, With_fields, 2);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000689 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000690 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
695 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000702 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000706 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
707 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000710 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000712 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000714 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000716 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000718 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000721 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000725 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000727 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000731 Set_type = make_type("Set", expr_type, Set_fields, 1);
732 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000735 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
736 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000737 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
738 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 GeneratorExp_type = make_type("GeneratorExp", expr_type,
740 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000752 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
753 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000754 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
755 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000760 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
761 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000764 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000768 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!expr_context_type) return 0;
770 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000775 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000779 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000781 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000783 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000785 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000787 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000789 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000791 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000795 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!slice_type) return 0;
797 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000798 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000800 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000804 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!boolop_type) return 0;
806 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000807 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000809 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000810 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000811 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000813 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000815 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000816 if (!operator_type) return 0;
817 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000818 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000820 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000850 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000852 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000854 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000856 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000858 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000860 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000862 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000866 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!unaryop_type) return 0;
868 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000869 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000871 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000873 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000875 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000877 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000879 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000881 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000883 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000885 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!cmpop_type) return 0;
887 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000888 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000890 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000892 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000894 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000896 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000898 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000900 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000902 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000904 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000906 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000908 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000912 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000914 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000916 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000917 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000918 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000919 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000920 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000922 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000924 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000926 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000928 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000929 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000930 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000931 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000932 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000933 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
934 return 0;
935 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
936 ExceptHandler_fields, 3);
937 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000938 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000939 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000940 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000941 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000942 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000943 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000944 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000945 if (!alias_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500946 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
947 if (!withitem_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000948 initialized = 1;
949 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000950}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000951
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000952static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
953static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
954static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
955static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
956 arena);
957static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
958static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
959static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
960static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
961static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
962static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
963 arena);
964static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
965 arena);
966static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
967static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
968static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
969static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500970static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974{
975 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000977 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 p->kind = Module_kind;
980 p->v.Module.body = body;
981 return p;
982}
983
984mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000985Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986{
987 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000988 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000989 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 p->kind = Interactive_kind;
992 p->v.Interactive.body = body;
993 return p;
994}
995
996mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998{
999 mod_ty p;
1000 if (!body) {
1001 PyErr_SetString(PyExc_ValueError,
1002 "field body is required for Expression");
1003 return NULL;
1004 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001006 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 p->kind = Expression_kind;
1009 p->v.Expression.body = body;
1010 return p;
1011}
1012
1013mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001014Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015{
1016 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001017 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001018 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 p->kind = Suite_kind;
1021 p->v.Suite.body = body;
1022 return p;
1023}
1024
1025stmt_ty
1026FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001027 decorator_list, expr_ty returns, int lineno, int col_offset,
1028 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029{
1030 stmt_ty p;
1031 if (!name) {
1032 PyErr_SetString(PyExc_ValueError,
1033 "field name is required for FunctionDef");
1034 return NULL;
1035 }
1036 if (!args) {
1037 PyErr_SetString(PyExc_ValueError,
1038 "field args is required for FunctionDef");
1039 return NULL;
1040 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001041 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001042 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 p->kind = FunctionDef_kind;
1045 p->v.FunctionDef.name = name;
1046 p->v.FunctionDef.args = args;
1047 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001048 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001049 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001051 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return p;
1053}
1054
1055stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001056ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001057 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1058 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059{
1060 stmt_ty p;
1061 if (!name) {
1062 PyErr_SetString(PyExc_ValueError,
1063 "field name is required for ClassDef");
1064 return NULL;
1065 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001066 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001067 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 p->kind = ClassDef_kind;
1070 p->v.ClassDef.name = name;
1071 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001072 p->v.ClassDef.keywords = keywords;
1073 p->v.ClassDef.starargs = starargs;
1074 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001076 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001078 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 return p;
1080}
1081
1082stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001083Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084{
1085 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001086 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001087 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 p->kind = Return_kind;
1090 p->v.Return.value = value;
1091 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001092 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 return p;
1094}
1095
1096stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001097Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098{
1099 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001100 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001101 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 p->kind = Delete_kind;
1104 p->v.Delete.targets = targets;
1105 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 return p;
1108}
1109
1110stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001111Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1112 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113{
1114 stmt_ty p;
1115 if (!value) {
1116 PyErr_SetString(PyExc_ValueError,
1117 "field value is required for Assign");
1118 return NULL;
1119 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001121 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 p->kind = Assign_kind;
1124 p->v.Assign.targets = targets;
1125 p->v.Assign.value = value;
1126 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001127 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return p;
1129}
1130
1131stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001132AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1133 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134{
1135 stmt_ty p;
1136 if (!target) {
1137 PyErr_SetString(PyExc_ValueError,
1138 "field target is required for AugAssign");
1139 return NULL;
1140 }
1141 if (!op) {
1142 PyErr_SetString(PyExc_ValueError,
1143 "field op is required for AugAssign");
1144 return NULL;
1145 }
1146 if (!value) {
1147 PyErr_SetString(PyExc_ValueError,
1148 "field value is required for AugAssign");
1149 return NULL;
1150 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001152 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 p->kind = AugAssign_kind;
1155 p->v.AugAssign.target = target;
1156 p->v.AugAssign.op = op;
1157 p->v.AugAssign.value = value;
1158 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001159 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return p;
1161}
1162
1163stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001165 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166{
1167 stmt_ty p;
1168 if (!target) {
1169 PyErr_SetString(PyExc_ValueError,
1170 "field target is required for For");
1171 return NULL;
1172 }
1173 if (!iter) {
1174 PyErr_SetString(PyExc_ValueError,
1175 "field iter is required for For");
1176 return NULL;
1177 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001178 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001179 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 p->kind = For_kind;
1182 p->v.For.target = target;
1183 p->v.For.iter = iter;
1184 p->v.For.body = body;
1185 p->v.For.orelse = orelse;
1186 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001187 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 return p;
1189}
1190
1191stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001192While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1193 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194{
1195 stmt_ty p;
1196 if (!test) {
1197 PyErr_SetString(PyExc_ValueError,
1198 "field test is required for While");
1199 return NULL;
1200 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001202 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 p->kind = While_kind;
1205 p->v.While.test = test;
1206 p->v.While.body = body;
1207 p->v.While.orelse = orelse;
1208 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001209 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 return p;
1211}
1212
1213stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001214If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1215 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
1217 stmt_ty p;
1218 if (!test) {
1219 PyErr_SetString(PyExc_ValueError,
1220 "field test is required for If");
1221 return NULL;
1222 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001224 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 p->kind = If_kind;
1227 p->v.If.test = test;
1228 p->v.If.body = body;
1229 p->v.If.orelse = orelse;
1230 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001231 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 return p;
1233}
1234
1235stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001236With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1237 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001238{
1239 stmt_ty p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001240 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001241 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001242 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001243 p->kind = With_kind;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001244 p->v.With.items = items;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001245 p->v.With.body = body;
1246 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001247 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001248 return p;
1249}
1250
1251stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001252Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253{
1254 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001255 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001256 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001259 p->v.Raise.exc = exc;
1260 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001262 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 return p;
1264}
1265
1266stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001268 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269{
1270 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001272 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 p->kind = TryExcept_kind;
1275 p->v.TryExcept.body = body;
1276 p->v.TryExcept.handlers = handlers;
1277 p->v.TryExcept.orelse = orelse;
1278 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return p;
1281}
1282
1283stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1285 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286{
1287 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001289 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 p->kind = TryFinally_kind;
1292 p->v.TryFinally.body = body;
1293 p->v.TryFinally.finalbody = finalbody;
1294 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001295 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 return p;
1297}
1298
1299stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001300Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301{
1302 stmt_ty p;
1303 if (!test) {
1304 PyErr_SetString(PyExc_ValueError,
1305 "field test is required for Assert");
1306 return NULL;
1307 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001309 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 p->kind = Assert_kind;
1312 p->v.Assert.test = test;
1313 p->v.Assert.msg = msg;
1314 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001315 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return p;
1317}
1318
1319stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001320Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321{
1322 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001324 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 p->kind = Import_kind;
1327 p->v.Import.names = names;
1328 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 return p;
1331}
1332
1333stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001334ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1335 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336{
1337 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001338 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001339 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 p->kind = ImportFrom_kind;
1342 p->v.ImportFrom.module = module;
1343 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001344 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001346 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 return p;
1348}
1349
1350stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001351Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352{
1353 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001354 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001355 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 p->kind = Global_kind;
1358 p->v.Global.names = names;
1359 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001360 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 return p;
1362}
1363
1364stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001365Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1366{
1367 stmt_ty p;
1368 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1369 if (!p)
1370 return NULL;
1371 p->kind = Nonlocal_kind;
1372 p->v.Nonlocal.names = names;
1373 p->lineno = lineno;
1374 p->col_offset = col_offset;
1375 return p;
1376}
1377
1378stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001379Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
1381 stmt_ty p;
1382 if (!value) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "field value is required for Expr");
1385 return NULL;
1386 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001387 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001388 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 p->kind = Expr_kind;
1391 p->v.Expr.value = value;
1392 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001393 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 return p;
1395}
1396
1397stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001398Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399{
1400 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001401 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001402 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 p->kind = Pass_kind;
1405 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 return p;
1408}
1409
1410stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001411Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412{
1413 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001415 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 p->kind = Break_kind;
1418 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001419 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 return p;
1421}
1422
1423stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001424Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
1426 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001427 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001428 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 p->kind = Continue_kind;
1431 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001432 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 return p;
1434}
1435
1436expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001437BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1438 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439{
1440 expr_ty p;
1441 if (!op) {
1442 PyErr_SetString(PyExc_ValueError,
1443 "field op is required for BoolOp");
1444 return NULL;
1445 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001446 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001447 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 p->kind = BoolOp_kind;
1450 p->v.BoolOp.op = op;
1451 p->v.BoolOp.values = values;
1452 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001453 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return p;
1455}
1456
1457expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001458BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1459 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460{
1461 expr_ty p;
1462 if (!left) {
1463 PyErr_SetString(PyExc_ValueError,
1464 "field left is required for BinOp");
1465 return NULL;
1466 }
1467 if (!op) {
1468 PyErr_SetString(PyExc_ValueError,
1469 "field op is required for BinOp");
1470 return NULL;
1471 }
1472 if (!right) {
1473 PyErr_SetString(PyExc_ValueError,
1474 "field right is required for BinOp");
1475 return NULL;
1476 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001478 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 p->kind = BinOp_kind;
1481 p->v.BinOp.left = left;
1482 p->v.BinOp.op = op;
1483 p->v.BinOp.right = right;
1484 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 return p;
1487}
1488
1489expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1491 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492{
1493 expr_ty p;
1494 if (!op) {
1495 PyErr_SetString(PyExc_ValueError,
1496 "field op is required for UnaryOp");
1497 return NULL;
1498 }
1499 if (!operand) {
1500 PyErr_SetString(PyExc_ValueError,
1501 "field operand is required for UnaryOp");
1502 return NULL;
1503 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001504 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001505 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 p->kind = UnaryOp_kind;
1508 p->v.UnaryOp.op = op;
1509 p->v.UnaryOp.operand = operand;
1510 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 return p;
1513}
1514
1515expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001516Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1517 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518{
1519 expr_ty p;
1520 if (!args) {
1521 PyErr_SetString(PyExc_ValueError,
1522 "field args is required for Lambda");
1523 return NULL;
1524 }
1525 if (!body) {
1526 PyErr_SetString(PyExc_ValueError,
1527 "field body is required for Lambda");
1528 return NULL;
1529 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001530 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001531 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 p->kind = Lambda_kind;
1534 p->v.Lambda.args = args;
1535 p->v.Lambda.body = body;
1536 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001537 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 return p;
1539}
1540
1541expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001542IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1543 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001544{
1545 expr_ty p;
1546 if (!test) {
1547 PyErr_SetString(PyExc_ValueError,
1548 "field test is required for IfExp");
1549 return NULL;
1550 }
1551 if (!body) {
1552 PyErr_SetString(PyExc_ValueError,
1553 "field body is required for IfExp");
1554 return NULL;
1555 }
1556 if (!orelse) {
1557 PyErr_SetString(PyExc_ValueError,
1558 "field orelse is required for IfExp");
1559 return NULL;
1560 }
1561 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001562 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001563 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001564 p->kind = IfExp_kind;
1565 p->v.IfExp.test = test;
1566 p->v.IfExp.body = body;
1567 p->v.IfExp.orelse = orelse;
1568 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001569 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001570 return p;
1571}
1572
1573expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001574Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1575 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576{
1577 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001578 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001579 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 p->kind = Dict_kind;
1582 p->v.Dict.keys = keys;
1583 p->v.Dict.values = values;
1584 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001585 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 return p;
1587}
1588
1589expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001590Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1591{
1592 expr_ty p;
1593 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001594 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001595 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001596 p->kind = Set_kind;
1597 p->v.Set.elts = elts;
1598 p->lineno = lineno;
1599 p->col_offset = col_offset;
1600 return p;
1601}
1602
1603expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001604ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1605 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606{
1607 expr_ty p;
1608 if (!elt) {
1609 PyErr_SetString(PyExc_ValueError,
1610 "field elt is required for ListComp");
1611 return NULL;
1612 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001613 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001614 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 p->kind = ListComp_kind;
1617 p->v.ListComp.elt = elt;
1618 p->v.ListComp.generators = generators;
1619 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001620 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 return p;
1622}
1623
1624expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001625SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1626 *arena)
1627{
1628 expr_ty p;
1629 if (!elt) {
1630 PyErr_SetString(PyExc_ValueError,
1631 "field elt is required for SetComp");
1632 return NULL;
1633 }
1634 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1635 if (!p)
1636 return NULL;
1637 p->kind = SetComp_kind;
1638 p->v.SetComp.elt = elt;
1639 p->v.SetComp.generators = generators;
1640 p->lineno = lineno;
1641 p->col_offset = col_offset;
1642 return p;
1643}
1644
1645expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001646DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1647 col_offset, PyArena *arena)
1648{
1649 expr_ty p;
1650 if (!key) {
1651 PyErr_SetString(PyExc_ValueError,
1652 "field key is required for DictComp");
1653 return NULL;
1654 }
1655 if (!value) {
1656 PyErr_SetString(PyExc_ValueError,
1657 "field value is required for DictComp");
1658 return NULL;
1659 }
1660 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1661 if (!p)
1662 return NULL;
1663 p->kind = DictComp_kind;
1664 p->v.DictComp.key = key;
1665 p->v.DictComp.value = value;
1666 p->v.DictComp.generators = generators;
1667 p->lineno = lineno;
1668 p->col_offset = col_offset;
1669 return p;
1670}
1671
1672expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1674 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675{
1676 expr_ty p;
1677 if (!elt) {
1678 PyErr_SetString(PyExc_ValueError,
1679 "field elt is required for GeneratorExp");
1680 return NULL;
1681 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001683 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 p->kind = GeneratorExp_kind;
1686 p->v.GeneratorExp.elt = elt;
1687 p->v.GeneratorExp.generators = generators;
1688 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001689 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 return p;
1691}
1692
1693expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695{
1696 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001697 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001698 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 p->kind = Yield_kind;
1701 p->v.Yield.value = value;
1702 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001703 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 return p;
1705}
1706
1707expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001708Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1709 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
1711 expr_ty p;
1712 if (!left) {
1713 PyErr_SetString(PyExc_ValueError,
1714 "field left is required for Compare");
1715 return NULL;
1716 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001717 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001718 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 p->kind = Compare_kind;
1721 p->v.Compare.left = left;
1722 p->v.Compare.ops = ops;
1723 p->v.Compare.comparators = comparators;
1724 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001725 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return p;
1727}
1728
1729expr_ty
1730Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001731 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732{
1733 expr_ty p;
1734 if (!func) {
1735 PyErr_SetString(PyExc_ValueError,
1736 "field func is required for Call");
1737 return NULL;
1738 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001740 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 p->kind = Call_kind;
1743 p->v.Call.func = func;
1744 p->v.Call.args = args;
1745 p->v.Call.keywords = keywords;
1746 p->v.Call.starargs = starargs;
1747 p->v.Call.kwargs = kwargs;
1748 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001749 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 return p;
1751}
1752
1753expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001754Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755{
1756 expr_ty p;
1757 if (!n) {
1758 PyErr_SetString(PyExc_ValueError,
1759 "field n is required for Num");
1760 return NULL;
1761 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001762 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001763 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 p->kind = Num_kind;
1766 p->v.Num.n = n;
1767 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001768 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769 return p;
1770}
1771
1772expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774{
1775 expr_ty p;
1776 if (!s) {
1777 PyErr_SetString(PyExc_ValueError,
1778 "field s is required for Str");
1779 return NULL;
1780 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001781 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001782 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 p->kind = Str_kind;
1785 p->v.Str.s = s;
1786 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001787 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 return p;
1789}
1790
1791expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001792Bytes(string s, int lineno, int col_offset, PyArena *arena)
1793{
1794 expr_ty p;
1795 if (!s) {
1796 PyErr_SetString(PyExc_ValueError,
1797 "field s is required for Bytes");
1798 return NULL;
1799 }
1800 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001801 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001802 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001803 p->kind = Bytes_kind;
1804 p->v.Bytes.s = s;
1805 p->lineno = lineno;
1806 p->col_offset = col_offset;
1807 return p;
1808}
1809
1810expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001811Ellipsis(int lineno, int col_offset, PyArena *arena)
1812{
1813 expr_ty p;
1814 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001815 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001816 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001817 p->kind = Ellipsis_kind;
1818 p->lineno = lineno;
1819 p->col_offset = col_offset;
1820 return p;
1821}
1822
1823expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001824Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1825 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826{
1827 expr_ty p;
1828 if (!value) {
1829 PyErr_SetString(PyExc_ValueError,
1830 "field value is required for Attribute");
1831 return NULL;
1832 }
1833 if (!attr) {
1834 PyErr_SetString(PyExc_ValueError,
1835 "field attr is required for Attribute");
1836 return NULL;
1837 }
1838 if (!ctx) {
1839 PyErr_SetString(PyExc_ValueError,
1840 "field ctx is required for Attribute");
1841 return NULL;
1842 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001844 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 p->kind = Attribute_kind;
1847 p->v.Attribute.value = value;
1848 p->v.Attribute.attr = attr;
1849 p->v.Attribute.ctx = ctx;
1850 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001851 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 return p;
1853}
1854
1855expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001856Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1857 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858{
1859 expr_ty p;
1860 if (!value) {
1861 PyErr_SetString(PyExc_ValueError,
1862 "field value is required for Subscript");
1863 return NULL;
1864 }
1865 if (!slice) {
1866 PyErr_SetString(PyExc_ValueError,
1867 "field slice is required for Subscript");
1868 return NULL;
1869 }
1870 if (!ctx) {
1871 PyErr_SetString(PyExc_ValueError,
1872 "field ctx is required for Subscript");
1873 return NULL;
1874 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001875 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001876 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 p->kind = Subscript_kind;
1879 p->v.Subscript.value = value;
1880 p->v.Subscript.slice = slice;
1881 p->v.Subscript.ctx = ctx;
1882 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001883 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 return p;
1885}
1886
1887expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001888Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1889 *arena)
1890{
1891 expr_ty p;
1892 if (!value) {
1893 PyErr_SetString(PyExc_ValueError,
1894 "field value is required for Starred");
1895 return NULL;
1896 }
1897 if (!ctx) {
1898 PyErr_SetString(PyExc_ValueError,
1899 "field ctx is required for Starred");
1900 return NULL;
1901 }
1902 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1903 if (!p)
1904 return NULL;
1905 p->kind = Starred_kind;
1906 p->v.Starred.value = value;
1907 p->v.Starred.ctx = ctx;
1908 p->lineno = lineno;
1909 p->col_offset = col_offset;
1910 return p;
1911}
1912
1913expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001914Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1915 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916{
1917 expr_ty p;
1918 if (!id) {
1919 PyErr_SetString(PyExc_ValueError,
1920 "field id is required for Name");
1921 return NULL;
1922 }
1923 if (!ctx) {
1924 PyErr_SetString(PyExc_ValueError,
1925 "field ctx is required for Name");
1926 return NULL;
1927 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001928 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001929 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931 p->kind = Name_kind;
1932 p->v.Name.id = id;
1933 p->v.Name.ctx = ctx;
1934 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001935 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 return p;
1937}
1938
1939expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001940List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1941 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942{
1943 expr_ty p;
1944 if (!ctx) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field ctx is required for List");
1947 return NULL;
1948 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001949 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001950 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 p->kind = List_kind;
1953 p->v.List.elts = elts;
1954 p->v.List.ctx = ctx;
1955 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001956 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 return p;
1958}
1959
1960expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001961Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1962 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963{
1964 expr_ty p;
1965 if (!ctx) {
1966 PyErr_SetString(PyExc_ValueError,
1967 "field ctx is required for Tuple");
1968 return NULL;
1969 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001970 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001971 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 p->kind = Tuple_kind;
1974 p->v.Tuple.elts = elts;
1975 p->v.Tuple.ctx = ctx;
1976 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001977 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978 return p;
1979}
1980
1981slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983{
1984 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 p->kind = Slice_kind;
1989 p->v.Slice.lower = lower;
1990 p->v.Slice.upper = upper;
1991 p->v.Slice.step = step;
1992 return p;
1993}
1994
1995slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001996ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997{
1998 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001999 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002000 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 p->kind = ExtSlice_kind;
2003 p->v.ExtSlice.dims = dims;
2004 return p;
2005}
2006
2007slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002008Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009{
2010 slice_ty p;
2011 if (!value) {
2012 PyErr_SetString(PyExc_ValueError,
2013 "field value is required for Index");
2014 return NULL;
2015 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002016 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002017 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 p->kind = Index_kind;
2020 p->v.Index.value = value;
2021 return p;
2022}
2023
2024comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002025comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026{
2027 comprehension_ty p;
2028 if (!target) {
2029 PyErr_SetString(PyExc_ValueError,
2030 "field target is required for comprehension");
2031 return NULL;
2032 }
2033 if (!iter) {
2034 PyErr_SetString(PyExc_ValueError,
2035 "field iter is required for comprehension");
2036 return NULL;
2037 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002039 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 p->target = target;
2042 p->iter = iter;
2043 p->ifs = ifs;
2044 return p;
2045}
2046
2047excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002048ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002049 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050{
2051 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002052 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002053 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002055 p->kind = ExceptHandler_kind;
2056 p->v.ExceptHandler.type = type;
2057 p->v.ExceptHandler.name = name;
2058 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002059 p->lineno = lineno;
2060 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 return p;
2062}
2063
2064arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002065arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2066 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2067 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068{
2069 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002071 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 p->args = args;
2074 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002075 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002076 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002078 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002080 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 return p;
2082}
2083
Neal Norwitzc1505362006-12-28 06:47:50 +00002084arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002085arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002086{
2087 arg_ty p;
2088 if (!arg) {
2089 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002090 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002091 return NULL;
2092 }
2093 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002094 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002095 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002096 p->arg = arg;
2097 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002098 return p;
2099}
2100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103{
2104 keyword_ty p;
2105 if (!arg) {
2106 PyErr_SetString(PyExc_ValueError,
2107 "field arg is required for keyword");
2108 return NULL;
2109 }
2110 if (!value) {
2111 PyErr_SetString(PyExc_ValueError,
2112 "field value is required for keyword");
2113 return NULL;
2114 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002115 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002116 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 p->arg = arg;
2119 p->value = value;
2120 return p;
2121}
2122
2123alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125{
2126 alias_ty p;
2127 if (!name) {
2128 PyErr_SetString(PyExc_ValueError,
2129 "field name is required for alias");
2130 return NULL;
2131 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002132 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002133 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 p->name = name;
2136 p->asname = asname;
2137 return p;
2138}
2139
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002140withitem_ty
2141withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2142{
2143 withitem_ty p;
2144 if (!context_expr) {
2145 PyErr_SetString(PyExc_ValueError,
2146 "field context_expr is required for withitem");
2147 return NULL;
2148 }
2149 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2150 if (!p)
2151 return NULL;
2152 p->context_expr = context_expr;
2153 p->optional_vars = optional_vars;
2154 return p;
2155}
2156
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002157
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002158PyObject*
2159ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002160{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002161 mod_ty o = (mod_ty)_o;
2162 PyObject *result = NULL, *value = NULL;
2163 if (!o) {
2164 Py_INCREF(Py_None);
2165 return Py_None;
2166 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 switch (o->kind) {
2169 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002170 result = PyType_GenericNew(Module_type, NULL, NULL);
2171 if (!result) goto failed;
2172 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2173 if (!value) goto failed;
2174 if (PyObject_SetAttrString(result, "body", value) == -1)
2175 goto failed;
2176 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 break;
2178 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002179 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2180 if (!result) goto failed;
2181 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2182 if (!value) goto failed;
2183 if (PyObject_SetAttrString(result, "body", value) == -1)
2184 goto failed;
2185 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 break;
2187 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002188 result = PyType_GenericNew(Expression_type, NULL, NULL);
2189 if (!result) goto failed;
2190 value = ast2obj_expr(o->v.Expression.body);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "body", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 break;
2196 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002197 result = PyType_GenericNew(Suite_type, NULL, NULL);
2198 if (!result) goto failed;
2199 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2200 if (!value) goto failed;
2201 if (PyObject_SetAttrString(result, "body", value) == -1)
2202 goto failed;
2203 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 break;
2205 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002206 return result;
2207failed:
2208 Py_XDECREF(value);
2209 Py_XDECREF(result);
2210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211}
2212
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002213PyObject*
2214ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002216 stmt_ty o = (stmt_ty)_o;
2217 PyObject *result = NULL, *value = NULL;
2218 if (!o) {
2219 Py_INCREF(Py_None);
2220 return Py_None;
2221 }
2222
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 switch (o->kind) {
2224 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002225 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2226 if (!result) goto failed;
2227 value = ast2obj_identifier(o->v.FunctionDef.name);
2228 if (!value) goto failed;
2229 if (PyObject_SetAttrString(result, "name", value) == -1)
2230 goto failed;
2231 Py_DECREF(value);
2232 value = ast2obj_arguments(o->v.FunctionDef.args);
2233 if (!value) goto failed;
2234 if (PyObject_SetAttrString(result, "args", value) == -1)
2235 goto failed;
2236 Py_DECREF(value);
2237 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2238 if (!value) goto failed;
2239 if (PyObject_SetAttrString(result, "body", value) == -1)
2240 goto failed;
2241 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002242 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2243 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002244 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002245 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2246 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002247 goto failed;
2248 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002249 value = ast2obj_expr(o->v.FunctionDef.returns);
2250 if (!value) goto failed;
2251 if (PyObject_SetAttrString(result, "returns", value) == -1)
2252 goto failed;
2253 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 break;
2255 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002256 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2257 if (!result) goto failed;
2258 value = ast2obj_identifier(o->v.ClassDef.name);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "name", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
2263 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "bases", value) == -1)
2266 goto failed;
2267 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002268 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2269 if (!value) goto failed;
2270 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2271 goto failed;
2272 Py_DECREF(value);
2273 value = ast2obj_expr(o->v.ClassDef.starargs);
2274 if (!value) goto failed;
2275 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2276 goto failed;
2277 Py_DECREF(value);
2278 value = ast2obj_expr(o->v.ClassDef.kwargs);
2279 if (!value) goto failed;
2280 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2281 goto failed;
2282 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002283 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2284 if (!value) goto failed;
2285 if (PyObject_SetAttrString(result, "body", value) == -1)
2286 goto failed;
2287 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002288 value = ast2obj_list(o->v.ClassDef.decorator_list,
2289 ast2obj_expr);
2290 if (!value) goto failed;
2291 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2292 -1)
2293 goto failed;
2294 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 break;
2296 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002297 result = PyType_GenericNew(Return_type, NULL, NULL);
2298 if (!result) goto failed;
2299 value = ast2obj_expr(o->v.Return.value);
2300 if (!value) goto failed;
2301 if (PyObject_SetAttrString(result, "value", value) == -1)
2302 goto failed;
2303 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 break;
2305 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002306 result = PyType_GenericNew(Delete_type, NULL, NULL);
2307 if (!result) goto failed;
2308 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2309 if (!value) goto failed;
2310 if (PyObject_SetAttrString(result, "targets", value) == -1)
2311 goto failed;
2312 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 break;
2314 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002315 result = PyType_GenericNew(Assign_type, NULL, NULL);
2316 if (!result) goto failed;
2317 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2318 if (!value) goto failed;
2319 if (PyObject_SetAttrString(result, "targets", value) == -1)
2320 goto failed;
2321 Py_DECREF(value);
2322 value = ast2obj_expr(o->v.Assign.value);
2323 if (!value) goto failed;
2324 if (PyObject_SetAttrString(result, "value", value) == -1)
2325 goto failed;
2326 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 break;
2328 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002329 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2330 if (!result) goto failed;
2331 value = ast2obj_expr(o->v.AugAssign.target);
2332 if (!value) goto failed;
2333 if (PyObject_SetAttrString(result, "target", value) == -1)
2334 goto failed;
2335 Py_DECREF(value);
2336 value = ast2obj_operator(o->v.AugAssign.op);
2337 if (!value) goto failed;
2338 if (PyObject_SetAttrString(result, "op", value) == -1)
2339 goto failed;
2340 Py_DECREF(value);
2341 value = ast2obj_expr(o->v.AugAssign.value);
2342 if (!value) goto failed;
2343 if (PyObject_SetAttrString(result, "value", value) == -1)
2344 goto failed;
2345 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002348 result = PyType_GenericNew(For_type, NULL, NULL);
2349 if (!result) goto failed;
2350 value = ast2obj_expr(o->v.For.target);
2351 if (!value) goto failed;
2352 if (PyObject_SetAttrString(result, "target", value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
2355 value = ast2obj_expr(o->v.For.iter);
2356 if (!value) goto failed;
2357 if (PyObject_SetAttrString(result, "iter", value) == -1)
2358 goto failed;
2359 Py_DECREF(value);
2360 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2361 if (!value) goto failed;
2362 if (PyObject_SetAttrString(result, "body", value) == -1)
2363 goto failed;
2364 Py_DECREF(value);
2365 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2366 if (!value) goto failed;
2367 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2368 goto failed;
2369 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 break;
2371 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002372 result = PyType_GenericNew(While_type, NULL, NULL);
2373 if (!result) goto failed;
2374 value = ast2obj_expr(o->v.While.test);
2375 if (!value) goto failed;
2376 if (PyObject_SetAttrString(result, "test", value) == -1)
2377 goto failed;
2378 Py_DECREF(value);
2379 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2380 if (!value) goto failed;
2381 if (PyObject_SetAttrString(result, "body", value) == -1)
2382 goto failed;
2383 Py_DECREF(value);
2384 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2385 if (!value) goto failed;
2386 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2387 goto failed;
2388 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 break;
2390 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002391 result = PyType_GenericNew(If_type, NULL, NULL);
2392 if (!result) goto failed;
2393 value = ast2obj_expr(o->v.If.test);
2394 if (!value) goto failed;
2395 if (PyObject_SetAttrString(result, "test", value) == -1)
2396 goto failed;
2397 Py_DECREF(value);
2398 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2399 if (!value) goto failed;
2400 if (PyObject_SetAttrString(result, "body", value) == -1)
2401 goto failed;
2402 Py_DECREF(value);
2403 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2404 if (!value) goto failed;
2405 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2406 goto failed;
2407 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002409 case With_kind:
2410 result = PyType_GenericNew(With_type, NULL, NULL);
2411 if (!result) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002412 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002413 if (!value) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002414 if (PyObject_SetAttrString(result, "items", value) == -1)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002415 goto failed;
2416 Py_DECREF(value);
2417 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2418 if (!value) goto failed;
2419 if (PyObject_SetAttrString(result, "body", value) == -1)
2420 goto failed;
2421 Py_DECREF(value);
2422 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002424 result = PyType_GenericNew(Raise_type, NULL, NULL);
2425 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002426 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002427 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002428 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429 goto failed;
2430 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002431 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002432 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002433 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002434 goto failed;
2435 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 break;
2437 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002438 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2439 if (!result) goto failed;
2440 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2441 if (!value) goto failed;
2442 if (PyObject_SetAttrString(result, "body", value) == -1)
2443 goto failed;
2444 Py_DECREF(value);
2445 value = ast2obj_list(o->v.TryExcept.handlers,
2446 ast2obj_excepthandler);
2447 if (!value) goto failed;
2448 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2449 goto failed;
2450 Py_DECREF(value);
2451 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2452 if (!value) goto failed;
2453 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2454 goto failed;
2455 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 break;
2457 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002458 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2461 if (!value) goto failed;
2462 if (PyObject_SetAttrString(result, "body", value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 break;
2471 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002472 result = PyType_GenericNew(Assert_type, NULL, NULL);
2473 if (!result) goto failed;
2474 value = ast2obj_expr(o->v.Assert.test);
2475 if (!value) goto failed;
2476 if (PyObject_SetAttrString(result, "test", value) == -1)
2477 goto failed;
2478 Py_DECREF(value);
2479 value = ast2obj_expr(o->v.Assert.msg);
2480 if (!value) goto failed;
2481 if (PyObject_SetAttrString(result, "msg", value) == -1)
2482 goto failed;
2483 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 break;
2485 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002486 result = PyType_GenericNew(Import_type, NULL, NULL);
2487 if (!result) goto failed;
2488 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "names", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 break;
2494 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002495 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2496 if (!result) goto failed;
2497 value = ast2obj_identifier(o->v.ImportFrom.module);
2498 if (!value) goto failed;
2499 if (PyObject_SetAttrString(result, "module", value) == -1)
2500 goto failed;
2501 Py_DECREF(value);
2502 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "names", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002507 value = ast2obj_int(o->v.ImportFrom.level);
2508 if (!value) goto failed;
2509 if (PyObject_SetAttrString(result, "level", value) == -1)
2510 goto failed;
2511 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002514 result = PyType_GenericNew(Global_type, NULL, NULL);
2515 if (!result) goto failed;
2516 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "names", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002522 case Nonlocal_kind:
2523 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2524 if (!result) goto failed;
2525 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2526 if (!value) goto failed;
2527 if (PyObject_SetAttrString(result, "names", value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002532 result = PyType_GenericNew(Expr_type, NULL, NULL);
2533 if (!result) goto failed;
2534 value = ast2obj_expr(o->v.Expr.value);
2535 if (!value) goto failed;
2536 if (PyObject_SetAttrString(result, "value", value) == -1)
2537 goto failed;
2538 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 break;
2540 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002541 result = PyType_GenericNew(Pass_type, NULL, NULL);
2542 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 break;
2544 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002545 result = PyType_GenericNew(Break_type, NULL, NULL);
2546 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 break;
2548 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002549 result = PyType_GenericNew(Continue_type, NULL, NULL);
2550 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 break;
2552 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002553 value = ast2obj_int(o->lineno);
2554 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002555 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2556 goto failed;
2557 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002558 value = ast2obj_int(o->col_offset);
2559 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002560 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2561 goto failed;
2562 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002563 return result;
2564failed:
2565 Py_XDECREF(value);
2566 Py_XDECREF(result);
2567 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568}
2569
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002570PyObject*
2571ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002573 expr_ty o = (expr_ty)_o;
2574 PyObject *result = NULL, *value = NULL;
2575 if (!o) {
2576 Py_INCREF(Py_None);
2577 return Py_None;
2578 }
2579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 switch (o->kind) {
2581 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002582 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2583 if (!result) goto failed;
2584 value = ast2obj_boolop(o->v.BoolOp.op);
2585 if (!value) goto failed;
2586 if (PyObject_SetAttrString(result, "op", value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
2589 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "values", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 break;
2595 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002596 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2597 if (!result) goto failed;
2598 value = ast2obj_expr(o->v.BinOp.left);
2599 if (!value) goto failed;
2600 if (PyObject_SetAttrString(result, "left", value) == -1)
2601 goto failed;
2602 Py_DECREF(value);
2603 value = ast2obj_operator(o->v.BinOp.op);
2604 if (!value) goto failed;
2605 if (PyObject_SetAttrString(result, "op", value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 value = ast2obj_expr(o->v.BinOp.right);
2609 if (!value) goto failed;
2610 if (PyObject_SetAttrString(result, "right", value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 break;
2614 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002615 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2616 if (!result) goto failed;
2617 value = ast2obj_unaryop(o->v.UnaryOp.op);
2618 if (!value) goto failed;
2619 if (PyObject_SetAttrString(result, "op", value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 value = ast2obj_expr(o->v.UnaryOp.operand);
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "operand", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 break;
2628 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002629 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2630 if (!result) goto failed;
2631 value = ast2obj_arguments(o->v.Lambda.args);
2632 if (!value) goto failed;
2633 if (PyObject_SetAttrString(result, "args", value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
2636 value = ast2obj_expr(o->v.Lambda.body);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "body", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002642 case IfExp_kind:
2643 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2644 if (!result) goto failed;
2645 value = ast2obj_expr(o->v.IfExp.test);
2646 if (!value) goto failed;
2647 if (PyObject_SetAttrString(result, "test", value) == -1)
2648 goto failed;
2649 Py_DECREF(value);
2650 value = ast2obj_expr(o->v.IfExp.body);
2651 if (!value) goto failed;
2652 if (PyObject_SetAttrString(result, "body", value) == -1)
2653 goto failed;
2654 Py_DECREF(value);
2655 value = ast2obj_expr(o->v.IfExp.orelse);
2656 if (!value) goto failed;
2657 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
2660 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002662 result = PyType_GenericNew(Dict_type, NULL, NULL);
2663 if (!result) goto failed;
2664 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "keys", value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2670 if (!value) goto failed;
2671 if (PyObject_SetAttrString(result, "values", value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002675 case Set_kind:
2676 result = PyType_GenericNew(Set_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2679 if (!value) goto failed;
2680 if (PyObject_SetAttrString(result, "elts", value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002685 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2686 if (!result) goto failed;
2687 value = ast2obj_expr(o->v.ListComp.elt);
2688 if (!value) goto failed;
2689 if (PyObject_SetAttrString(result, "elt", value) == -1)
2690 goto failed;
2691 Py_DECREF(value);
2692 value = ast2obj_list(o->v.ListComp.generators,
2693 ast2obj_comprehension);
2694 if (!value) goto failed;
2695 if (PyObject_SetAttrString(result, "generators", value) == -1)
2696 goto failed;
2697 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002699 case SetComp_kind:
2700 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2701 if (!result) goto failed;
2702 value = ast2obj_expr(o->v.SetComp.elt);
2703 if (!value) goto failed;
2704 if (PyObject_SetAttrString(result, "elt", value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
2707 value = ast2obj_list(o->v.SetComp.generators,
2708 ast2obj_comprehension);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "generators", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
2713 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002714 case DictComp_kind:
2715 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2716 if (!result) goto failed;
2717 value = ast2obj_expr(o->v.DictComp.key);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "key", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_expr(o->v.DictComp.value);
2723 if (!value) goto failed;
2724 if (PyObject_SetAttrString(result, "value", value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
2727 value = ast2obj_list(o->v.DictComp.generators,
2728 ast2obj_comprehension);
2729 if (!value) goto failed;
2730 if (PyObject_SetAttrString(result, "generators", value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002735 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2736 if (!result) goto failed;
2737 value = ast2obj_expr(o->v.GeneratorExp.elt);
2738 if (!value) goto failed;
2739 if (PyObject_SetAttrString(result, "elt", value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 value = ast2obj_list(o->v.GeneratorExp.generators,
2743 ast2obj_comprehension);
2744 if (!value) goto failed;
2745 if (PyObject_SetAttrString(result, "generators", value) == -1)
2746 goto failed;
2747 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 break;
2749 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002750 result = PyType_GenericNew(Yield_type, NULL, NULL);
2751 if (!result) goto failed;
2752 value = ast2obj_expr(o->v.Yield.value);
2753 if (!value) goto failed;
2754 if (PyObject_SetAttrString(result, "value", value) == -1)
2755 goto failed;
2756 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 break;
2758 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002759 result = PyType_GenericNew(Compare_type, NULL, NULL);
2760 if (!result) goto failed;
2761 value = ast2obj_expr(o->v.Compare.left);
2762 if (!value) goto failed;
2763 if (PyObject_SetAttrString(result, "left", value) == -1)
2764 goto failed;
2765 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002766 {
2767 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2768 value = PyList_New(n);
2769 if (!value) goto failed;
2770 for(i = 0; i < n; i++)
2771 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2772 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002773 if (!value) goto failed;
2774 if (PyObject_SetAttrString(result, "ops", value) == -1)
2775 goto failed;
2776 Py_DECREF(value);
2777 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2778 if (!value) goto failed;
2779 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 break;
2783 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002784 result = PyType_GenericNew(Call_type, NULL, NULL);
2785 if (!result) goto failed;
2786 value = ast2obj_expr(o->v.Call.func);
2787 if (!value) goto failed;
2788 if (PyObject_SetAttrString(result, "func", value) == -1)
2789 goto failed;
2790 Py_DECREF(value);
2791 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2792 if (!value) goto failed;
2793 if (PyObject_SetAttrString(result, "args", value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2797 if (!value) goto failed;
2798 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2799 goto failed;
2800 Py_DECREF(value);
2801 value = ast2obj_expr(o->v.Call.starargs);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
2806 value = ast2obj_expr(o->v.Call.kwargs);
2807 if (!value) goto failed;
2808 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2809 goto failed;
2810 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002813 result = PyType_GenericNew(Num_type, NULL, NULL);
2814 if (!result) goto failed;
2815 value = ast2obj_object(o->v.Num.n);
2816 if (!value) goto failed;
2817 if (PyObject_SetAttrString(result, "n", value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 break;
2821 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002822 result = PyType_GenericNew(Str_type, NULL, NULL);
2823 if (!result) goto failed;
2824 value = ast2obj_string(o->v.Str.s);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "s", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002830 case Bytes_kind:
2831 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2832 if (!result) goto failed;
2833 value = ast2obj_string(o->v.Bytes.s);
2834 if (!value) goto failed;
2835 if (PyObject_SetAttrString(result, "s", value) == -1)
2836 goto failed;
2837 Py_DECREF(value);
2838 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002839 case Ellipsis_kind:
2840 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2841 if (!result) goto failed;
2842 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002844 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2845 if (!result) goto failed;
2846 value = ast2obj_expr(o->v.Attribute.value);
2847 if (!value) goto failed;
2848 if (PyObject_SetAttrString(result, "value", value) == -1)
2849 goto failed;
2850 Py_DECREF(value);
2851 value = ast2obj_identifier(o->v.Attribute.attr);
2852 if (!value) goto failed;
2853 if (PyObject_SetAttrString(result, "attr", value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 value = ast2obj_expr_context(o->v.Attribute.ctx);
2857 if (!value) goto failed;
2858 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 break;
2862 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002863 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2864 if (!result) goto failed;
2865 value = ast2obj_expr(o->v.Subscript.value);
2866 if (!value) goto failed;
2867 if (PyObject_SetAttrString(result, "value", value) == -1)
2868 goto failed;
2869 Py_DECREF(value);
2870 value = ast2obj_slice(o->v.Subscript.slice);
2871 if (!value) goto failed;
2872 if (PyObject_SetAttrString(result, "slice", value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 value = ast2obj_expr_context(o->v.Subscript.ctx);
2876 if (!value) goto failed;
2877 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002881 case Starred_kind:
2882 result = PyType_GenericNew(Starred_type, NULL, NULL);
2883 if (!result) goto failed;
2884 value = ast2obj_expr(o->v.Starred.value);
2885 if (!value) goto failed;
2886 if (PyObject_SetAttrString(result, "value", value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_expr_context(o->v.Starred.ctx);
2890 if (!value) goto failed;
2891 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002896 result = PyType_GenericNew(Name_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_identifier(o->v.Name.id);
2899 if (!value) goto failed;
2900 if (PyObject_SetAttrString(result, "id", value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_expr_context(o->v.Name.ctx);
2904 if (!value) goto failed;
2905 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 break;
2909 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002910 result = PyType_GenericNew(List_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2913 if (!value) goto failed;
2914 if (PyObject_SetAttrString(result, "elts", value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 value = ast2obj_expr_context(o->v.List.ctx);
2918 if (!value) goto failed;
2919 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 break;
2923 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002924 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2925 if (!result) goto failed;
2926 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2927 if (!value) goto failed;
2928 if (PyObject_SetAttrString(result, "elts", value) == -1)
2929 goto failed;
2930 Py_DECREF(value);
2931 value = ast2obj_expr_context(o->v.Tuple.ctx);
2932 if (!value) goto failed;
2933 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 break;
2937 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002938 value = ast2obj_int(o->lineno);
2939 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002940 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2941 goto failed;
2942 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002943 value = ast2obj_int(o->col_offset);
2944 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002945 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2946 goto failed;
2947 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002948 return result;
2949failed:
2950 Py_XDECREF(value);
2951 Py_XDECREF(result);
2952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953}
2954
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002955PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002957 switch(o) {
2958 case Load:
2959 Py_INCREF(Load_singleton);
2960 return Load_singleton;
2961 case Store:
2962 Py_INCREF(Store_singleton);
2963 return Store_singleton;
2964 case Del:
2965 Py_INCREF(Del_singleton);
2966 return Del_singleton;
2967 case AugLoad:
2968 Py_INCREF(AugLoad_singleton);
2969 return AugLoad_singleton;
2970 case AugStore:
2971 Py_INCREF(AugStore_singleton);
2972 return AugStore_singleton;
2973 case Param:
2974 Py_INCREF(Param_singleton);
2975 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002976 default:
2977 /* should never happen, but just in case ... */
2978 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2979 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002982PyObject*
2983ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002985 slice_ty o = (slice_ty)_o;
2986 PyObject *result = NULL, *value = NULL;
2987 if (!o) {
2988 Py_INCREF(Py_None);
2989 return Py_None;
2990 }
2991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002994 result = PyType_GenericNew(Slice_type, NULL, NULL);
2995 if (!result) goto failed;
2996 value = ast2obj_expr(o->v.Slice.lower);
2997 if (!value) goto failed;
2998 if (PyObject_SetAttrString(result, "lower", value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 value = ast2obj_expr(o->v.Slice.upper);
3002 if (!value) goto failed;
3003 if (PyObject_SetAttrString(result, "upper", value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
3006 value = ast2obj_expr(o->v.Slice.step);
3007 if (!value) goto failed;
3008 if (PyObject_SetAttrString(result, "step", value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 break;
3012 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003013 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3014 if (!result) goto failed;
3015 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3016 if (!value) goto failed;
3017 if (PyObject_SetAttrString(result, "dims", value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 break;
3021 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003022 result = PyType_GenericNew(Index_type, NULL, NULL);
3023 if (!result) goto failed;
3024 value = ast2obj_expr(o->v.Index.value);
3025 if (!value) goto failed;
3026 if (PyObject_SetAttrString(result, "value", value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 break;
3030 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003031 return result;
3032failed:
3033 Py_XDECREF(value);
3034 Py_XDECREF(result);
3035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036}
3037
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003038PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003040 switch(o) {
3041 case And:
3042 Py_INCREF(And_singleton);
3043 return And_singleton;
3044 case Or:
3045 Py_INCREF(Or_singleton);
3046 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003047 default:
3048 /* should never happen, but just in case ... */
3049 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003052}
3053PyObject* ast2obj_operator(operator_ty o)
3054{
3055 switch(o) {
3056 case Add:
3057 Py_INCREF(Add_singleton);
3058 return Add_singleton;
3059 case Sub:
3060 Py_INCREF(Sub_singleton);
3061 return Sub_singleton;
3062 case Mult:
3063 Py_INCREF(Mult_singleton);
3064 return Mult_singleton;
3065 case Div:
3066 Py_INCREF(Div_singleton);
3067 return Div_singleton;
3068 case Mod:
3069 Py_INCREF(Mod_singleton);
3070 return Mod_singleton;
3071 case Pow:
3072 Py_INCREF(Pow_singleton);
3073 return Pow_singleton;
3074 case LShift:
3075 Py_INCREF(LShift_singleton);
3076 return LShift_singleton;
3077 case RShift:
3078 Py_INCREF(RShift_singleton);
3079 return RShift_singleton;
3080 case BitOr:
3081 Py_INCREF(BitOr_singleton);
3082 return BitOr_singleton;
3083 case BitXor:
3084 Py_INCREF(BitXor_singleton);
3085 return BitXor_singleton;
3086 case BitAnd:
3087 Py_INCREF(BitAnd_singleton);
3088 return BitAnd_singleton;
3089 case FloorDiv:
3090 Py_INCREF(FloorDiv_singleton);
3091 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003092 default:
3093 /* should never happen, but just in case ... */
3094 PyErr_Format(PyExc_SystemError, "unknown operator found");
3095 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003096 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003097}
3098PyObject* ast2obj_unaryop(unaryop_ty o)
3099{
3100 switch(o) {
3101 case Invert:
3102 Py_INCREF(Invert_singleton);
3103 return Invert_singleton;
3104 case Not:
3105 Py_INCREF(Not_singleton);
3106 return Not_singleton;
3107 case UAdd:
3108 Py_INCREF(UAdd_singleton);
3109 return UAdd_singleton;
3110 case USub:
3111 Py_INCREF(USub_singleton);
3112 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003113 default:
3114 /* should never happen, but just in case ... */
3115 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3116 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003117 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003118}
3119PyObject* ast2obj_cmpop(cmpop_ty o)
3120{
3121 switch(o) {
3122 case Eq:
3123 Py_INCREF(Eq_singleton);
3124 return Eq_singleton;
3125 case NotEq:
3126 Py_INCREF(NotEq_singleton);
3127 return NotEq_singleton;
3128 case Lt:
3129 Py_INCREF(Lt_singleton);
3130 return Lt_singleton;
3131 case LtE:
3132 Py_INCREF(LtE_singleton);
3133 return LtE_singleton;
3134 case Gt:
3135 Py_INCREF(Gt_singleton);
3136 return Gt_singleton;
3137 case GtE:
3138 Py_INCREF(GtE_singleton);
3139 return GtE_singleton;
3140 case Is:
3141 Py_INCREF(Is_singleton);
3142 return Is_singleton;
3143 case IsNot:
3144 Py_INCREF(IsNot_singleton);
3145 return IsNot_singleton;
3146 case In:
3147 Py_INCREF(In_singleton);
3148 return In_singleton;
3149 case NotIn:
3150 Py_INCREF(NotIn_singleton);
3151 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003152 default:
3153 /* should never happen, but just in case ... */
3154 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3155 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003156 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003157}
3158PyObject*
3159ast2obj_comprehension(void* _o)
3160{
3161 comprehension_ty o = (comprehension_ty)_o;
3162 PyObject *result = NULL, *value = NULL;
3163 if (!o) {
3164 Py_INCREF(Py_None);
3165 return Py_None;
3166 }
3167
3168 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3169 if (!result) return NULL;
3170 value = ast2obj_expr(o->target);
3171 if (!value) goto failed;
3172 if (PyObject_SetAttrString(result, "target", value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 value = ast2obj_expr(o->iter);
3176 if (!value) goto failed;
3177 if (PyObject_SetAttrString(result, "iter", value) == -1)
3178 goto failed;
3179 Py_DECREF(value);
3180 value = ast2obj_list(o->ifs, ast2obj_expr);
3181 if (!value) goto failed;
3182 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3183 goto failed;
3184 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003185 return result;
3186failed:
3187 Py_XDECREF(value);
3188 Py_XDECREF(result);
3189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190}
3191
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003192PyObject*
3193ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003195 excepthandler_ty o = (excepthandler_ty)_o;
3196 PyObject *result = NULL, *value = NULL;
3197 if (!o) {
3198 Py_INCREF(Py_None);
3199 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003201
Neal Norwitzad74aa82008-03-31 05:14:30 +00003202 switch (o->kind) {
3203 case ExceptHandler_kind:
3204 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3205 if (!result) goto failed;
3206 value = ast2obj_expr(o->v.ExceptHandler.type);
3207 if (!value) goto failed;
3208 if (PyObject_SetAttrString(result, "type", value) == -1)
3209 goto failed;
3210 Py_DECREF(value);
3211 value = ast2obj_identifier(o->v.ExceptHandler.name);
3212 if (!value) goto failed;
3213 if (PyObject_SetAttrString(result, "name", value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3217 if (!value) goto failed;
3218 if (PyObject_SetAttrString(result, "body", value) == -1)
3219 goto failed;
3220 Py_DECREF(value);
3221 break;
3222 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003223 value = ast2obj_int(o->lineno);
3224 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003225 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003226 goto failed;
3227 Py_DECREF(value);
3228 value = ast2obj_int(o->col_offset);
3229 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003230 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003231 goto failed;
3232 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003233 return result;
3234failed:
3235 Py_XDECREF(value);
3236 Py_XDECREF(result);
3237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238}
3239
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003240PyObject*
3241ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003243 arguments_ty o = (arguments_ty)_o;
3244 PyObject *result = NULL, *value = NULL;
3245 if (!o) {
3246 Py_INCREF(Py_None);
3247 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003249
3250 result = PyType_GenericNew(arguments_type, NULL, NULL);
3251 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003252 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003253 if (!value) goto failed;
3254 if (PyObject_SetAttrString(result, "args", value) == -1)
3255 goto failed;
3256 Py_DECREF(value);
3257 value = ast2obj_identifier(o->vararg);
3258 if (!value) goto failed;
3259 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3260 goto failed;
3261 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003262 value = ast2obj_expr(o->varargannotation);
3263 if (!value) goto failed;
3264 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3265 goto failed;
3266 Py_DECREF(value);
3267 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003268 if (!value) goto failed;
3269 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3270 goto failed;
3271 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003272 value = ast2obj_identifier(o->kwarg);
3273 if (!value) goto failed;
3274 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003277 value = ast2obj_expr(o->kwargannotation);
3278 if (!value) goto failed;
3279 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003282 value = ast2obj_list(o->defaults, ast2obj_expr);
3283 if (!value) goto failed;
3284 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3285 goto failed;
3286 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003287 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3288 if (!value) goto failed;
3289 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3290 goto failed;
3291 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003292 return result;
3293failed:
3294 Py_XDECREF(value);
3295 Py_XDECREF(result);
3296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297}
3298
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003299PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003300ast2obj_arg(void* _o)
3301{
3302 arg_ty o = (arg_ty)_o;
3303 PyObject *result = NULL, *value = NULL;
3304 if (!o) {
3305 Py_INCREF(Py_None);
3306 return Py_None;
3307 }
3308
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003309 result = PyType_GenericNew(arg_type, NULL, NULL);
3310 if (!result) return NULL;
3311 value = ast2obj_identifier(o->arg);
3312 if (!value) goto failed;
3313 if (PyObject_SetAttrString(result, "arg", value) == -1)
3314 goto failed;
3315 Py_DECREF(value);
3316 value = ast2obj_expr(o->annotation);
3317 if (!value) goto failed;
3318 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3319 goto failed;
3320 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003321 return result;
3322failed:
3323 Py_XDECREF(value);
3324 Py_XDECREF(result);
3325 return NULL;
3326}
3327
3328PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003329ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003331 keyword_ty o = (keyword_ty)_o;
3332 PyObject *result = NULL, *value = NULL;
3333 if (!o) {
3334 Py_INCREF(Py_None);
3335 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003337
3338 result = PyType_GenericNew(keyword_type, NULL, NULL);
3339 if (!result) return NULL;
3340 value = ast2obj_identifier(o->arg);
3341 if (!value) goto failed;
3342 if (PyObject_SetAttrString(result, "arg", value) == -1)
3343 goto failed;
3344 Py_DECREF(value);
3345 value = ast2obj_expr(o->value);
3346 if (!value) goto failed;
3347 if (PyObject_SetAttrString(result, "value", value) == -1)
3348 goto failed;
3349 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003350 return result;
3351failed:
3352 Py_XDECREF(value);
3353 Py_XDECREF(result);
3354 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355}
3356
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003357PyObject*
3358ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003360 alias_ty o = (alias_ty)_o;
3361 PyObject *result = NULL, *value = NULL;
3362 if (!o) {
3363 Py_INCREF(Py_None);
3364 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003366
3367 result = PyType_GenericNew(alias_type, NULL, NULL);
3368 if (!result) return NULL;
3369 value = ast2obj_identifier(o->name);
3370 if (!value) goto failed;
3371 if (PyObject_SetAttrString(result, "name", value) == -1)
3372 goto failed;
3373 Py_DECREF(value);
3374 value = ast2obj_identifier(o->asname);
3375 if (!value) goto failed;
3376 if (PyObject_SetAttrString(result, "asname", value) == -1)
3377 goto failed;
3378 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003379 return result;
3380failed:
3381 Py_XDECREF(value);
3382 Py_XDECREF(result);
3383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384}
3385
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003386PyObject*
3387ast2obj_withitem(void* _o)
3388{
3389 withitem_ty o = (withitem_ty)_o;
3390 PyObject *result = NULL, *value = NULL;
3391 if (!o) {
3392 Py_INCREF(Py_None);
3393 return Py_None;
3394 }
3395
3396 result = PyType_GenericNew(withitem_type, NULL, NULL);
3397 if (!result) return NULL;
3398 value = ast2obj_expr(o->context_expr);
3399 if (!value) goto failed;
3400 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3401 goto failed;
3402 Py_DECREF(value);
3403 value = ast2obj_expr(o->optional_vars);
3404 if (!value) goto failed;
3405 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3406 goto failed;
3407 Py_DECREF(value);
3408 return result;
3409failed:
3410 Py_XDECREF(value);
3411 Py_XDECREF(result);
3412 return NULL;
3413}
3414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003416int
3417obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3418{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003419 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003420
Benjamin Petersond8f65972010-11-20 04:31:07 +00003421 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003422
3423 if (obj == Py_None) {
3424 *out = NULL;
3425 return 0;
3426 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003427 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3428 if (isinstance == -1) {
3429 return 1;
3430 }
3431 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003432 asdl_seq* body;
3433
3434 if (PyObject_HasAttrString(obj, "body")) {
3435 int res;
3436 Py_ssize_t len;
3437 Py_ssize_t i;
3438 tmp = PyObject_GetAttrString(obj, "body");
3439 if (tmp == NULL) goto failed;
3440 if (!PyList_Check(tmp)) {
3441 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3442 goto failed;
3443 }
3444 len = PyList_GET_SIZE(tmp);
3445 body = asdl_seq_new(len, arena);
3446 if (body == NULL) goto failed;
3447 for (i = 0; i < len; i++) {
3448 stmt_ty value;
3449 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3450 if (res != 0) goto failed;
3451 asdl_seq_SET(body, i, value);
3452 }
3453 Py_XDECREF(tmp);
3454 tmp = NULL;
3455 } else {
3456 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3457 return 1;
3458 }
3459 *out = Module(body, arena);
3460 if (*out == NULL) goto failed;
3461 return 0;
3462 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003463 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3464 if (isinstance == -1) {
3465 return 1;
3466 }
3467 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003468 asdl_seq* body;
3469
3470 if (PyObject_HasAttrString(obj, "body")) {
3471 int res;
3472 Py_ssize_t len;
3473 Py_ssize_t i;
3474 tmp = PyObject_GetAttrString(obj, "body");
3475 if (tmp == NULL) goto failed;
3476 if (!PyList_Check(tmp)) {
3477 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3478 goto failed;
3479 }
3480 len = PyList_GET_SIZE(tmp);
3481 body = asdl_seq_new(len, arena);
3482 if (body == NULL) goto failed;
3483 for (i = 0; i < len; i++) {
3484 stmt_ty value;
3485 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3486 if (res != 0) goto failed;
3487 asdl_seq_SET(body, i, value);
3488 }
3489 Py_XDECREF(tmp);
3490 tmp = NULL;
3491 } else {
3492 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3493 return 1;
3494 }
3495 *out = Interactive(body, arena);
3496 if (*out == NULL) goto failed;
3497 return 0;
3498 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003499 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3500 if (isinstance == -1) {
3501 return 1;
3502 }
3503 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003504 expr_ty body;
3505
3506 if (PyObject_HasAttrString(obj, "body")) {
3507 int res;
3508 tmp = PyObject_GetAttrString(obj, "body");
3509 if (tmp == NULL) goto failed;
3510 res = obj2ast_expr(tmp, &body, arena);
3511 if (res != 0) goto failed;
3512 Py_XDECREF(tmp);
3513 tmp = NULL;
3514 } else {
3515 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3516 return 1;
3517 }
3518 *out = Expression(body, arena);
3519 if (*out == NULL) goto failed;
3520 return 0;
3521 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003522 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3523 if (isinstance == -1) {
3524 return 1;
3525 }
3526 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003527 asdl_seq* body;
3528
3529 if (PyObject_HasAttrString(obj, "body")) {
3530 int res;
3531 Py_ssize_t len;
3532 Py_ssize_t i;
3533 tmp = PyObject_GetAttrString(obj, "body");
3534 if (tmp == NULL) goto failed;
3535 if (!PyList_Check(tmp)) {
3536 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3537 goto failed;
3538 }
3539 len = PyList_GET_SIZE(tmp);
3540 body = asdl_seq_new(len, arena);
3541 if (body == NULL) goto failed;
3542 for (i = 0; i < len; i++) {
3543 stmt_ty value;
3544 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3545 if (res != 0) goto failed;
3546 asdl_seq_SET(body, i, value);
3547 }
3548 Py_XDECREF(tmp);
3549 tmp = NULL;
3550 } else {
3551 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3552 return 1;
3553 }
3554 *out = Suite(body, arena);
3555 if (*out == NULL) goto failed;
3556 return 0;
3557 }
3558
Benjamin Peterson5b066812010-11-20 01:38:49 +00003559 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3560 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003561 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003562 return 1;
3563}
3564
3565int
3566obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3567{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003568 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003569
Benjamin Petersond8f65972010-11-20 04:31:07 +00003570 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003571 int lineno;
3572 int col_offset;
3573
3574 if (obj == Py_None) {
3575 *out = NULL;
3576 return 0;
3577 }
3578 if (PyObject_HasAttrString(obj, "lineno")) {
3579 int res;
3580 tmp = PyObject_GetAttrString(obj, "lineno");
3581 if (tmp == NULL) goto failed;
3582 res = obj2ast_int(tmp, &lineno, arena);
3583 if (res != 0) goto failed;
3584 Py_XDECREF(tmp);
3585 tmp = NULL;
3586 } else {
3587 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3588 return 1;
3589 }
3590 if (PyObject_HasAttrString(obj, "col_offset")) {
3591 int res;
3592 tmp = PyObject_GetAttrString(obj, "col_offset");
3593 if (tmp == NULL) goto failed;
3594 res = obj2ast_int(tmp, &col_offset, arena);
3595 if (res != 0) goto failed;
3596 Py_XDECREF(tmp);
3597 tmp = NULL;
3598 } else {
3599 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3600 return 1;
3601 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003602 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3603 if (isinstance == -1) {
3604 return 1;
3605 }
3606 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003607 identifier name;
3608 arguments_ty args;
3609 asdl_seq* body;
3610 asdl_seq* decorator_list;
3611 expr_ty returns;
3612
3613 if (PyObject_HasAttrString(obj, "name")) {
3614 int res;
3615 tmp = PyObject_GetAttrString(obj, "name");
3616 if (tmp == NULL) goto failed;
3617 res = obj2ast_identifier(tmp, &name, arena);
3618 if (res != 0) goto failed;
3619 Py_XDECREF(tmp);
3620 tmp = NULL;
3621 } else {
3622 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3623 return 1;
3624 }
3625 if (PyObject_HasAttrString(obj, "args")) {
3626 int res;
3627 tmp = PyObject_GetAttrString(obj, "args");
3628 if (tmp == NULL) goto failed;
3629 res = obj2ast_arguments(tmp, &args, arena);
3630 if (res != 0) goto failed;
3631 Py_XDECREF(tmp);
3632 tmp = NULL;
3633 } else {
3634 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3635 return 1;
3636 }
3637 if (PyObject_HasAttrString(obj, "body")) {
3638 int res;
3639 Py_ssize_t len;
3640 Py_ssize_t i;
3641 tmp = PyObject_GetAttrString(obj, "body");
3642 if (tmp == NULL) goto failed;
3643 if (!PyList_Check(tmp)) {
3644 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3645 goto failed;
3646 }
3647 len = PyList_GET_SIZE(tmp);
3648 body = asdl_seq_new(len, arena);
3649 if (body == NULL) goto failed;
3650 for (i = 0; i < len; i++) {
3651 stmt_ty value;
3652 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3653 if (res != 0) goto failed;
3654 asdl_seq_SET(body, i, value);
3655 }
3656 Py_XDECREF(tmp);
3657 tmp = NULL;
3658 } else {
3659 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3660 return 1;
3661 }
3662 if (PyObject_HasAttrString(obj, "decorator_list")) {
3663 int res;
3664 Py_ssize_t len;
3665 Py_ssize_t i;
3666 tmp = PyObject_GetAttrString(obj, "decorator_list");
3667 if (tmp == NULL) goto failed;
3668 if (!PyList_Check(tmp)) {
3669 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3670 goto failed;
3671 }
3672 len = PyList_GET_SIZE(tmp);
3673 decorator_list = asdl_seq_new(len, arena);
3674 if (decorator_list == NULL) goto failed;
3675 for (i = 0; i < len; i++) {
3676 expr_ty value;
3677 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3678 if (res != 0) goto failed;
3679 asdl_seq_SET(decorator_list, i, value);
3680 }
3681 Py_XDECREF(tmp);
3682 tmp = NULL;
3683 } else {
3684 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3685 return 1;
3686 }
3687 if (PyObject_HasAttrString(obj, "returns")) {
3688 int res;
3689 tmp = PyObject_GetAttrString(obj, "returns");
3690 if (tmp == NULL) goto failed;
3691 res = obj2ast_expr(tmp, &returns, arena);
3692 if (res != 0) goto failed;
3693 Py_XDECREF(tmp);
3694 tmp = NULL;
3695 } else {
3696 returns = NULL;
3697 }
3698 *out = FunctionDef(name, args, body, decorator_list, returns,
3699 lineno, col_offset, arena);
3700 if (*out == NULL) goto failed;
3701 return 0;
3702 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003703 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3704 if (isinstance == -1) {
3705 return 1;
3706 }
3707 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003708 identifier name;
3709 asdl_seq* bases;
3710 asdl_seq* keywords;
3711 expr_ty starargs;
3712 expr_ty kwargs;
3713 asdl_seq* body;
3714 asdl_seq* decorator_list;
3715
3716 if (PyObject_HasAttrString(obj, "name")) {
3717 int res;
3718 tmp = PyObject_GetAttrString(obj, "name");
3719 if (tmp == NULL) goto failed;
3720 res = obj2ast_identifier(tmp, &name, arena);
3721 if (res != 0) goto failed;
3722 Py_XDECREF(tmp);
3723 tmp = NULL;
3724 } else {
3725 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3726 return 1;
3727 }
3728 if (PyObject_HasAttrString(obj, "bases")) {
3729 int res;
3730 Py_ssize_t len;
3731 Py_ssize_t i;
3732 tmp = PyObject_GetAttrString(obj, "bases");
3733 if (tmp == NULL) goto failed;
3734 if (!PyList_Check(tmp)) {
3735 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3736 goto failed;
3737 }
3738 len = PyList_GET_SIZE(tmp);
3739 bases = asdl_seq_new(len, arena);
3740 if (bases == NULL) goto failed;
3741 for (i = 0; i < len; i++) {
3742 expr_ty value;
3743 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3744 if (res != 0) goto failed;
3745 asdl_seq_SET(bases, i, value);
3746 }
3747 Py_XDECREF(tmp);
3748 tmp = NULL;
3749 } else {
3750 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3751 return 1;
3752 }
3753 if (PyObject_HasAttrString(obj, "keywords")) {
3754 int res;
3755 Py_ssize_t len;
3756 Py_ssize_t i;
3757 tmp = PyObject_GetAttrString(obj, "keywords");
3758 if (tmp == NULL) goto failed;
3759 if (!PyList_Check(tmp)) {
3760 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3761 goto failed;
3762 }
3763 len = PyList_GET_SIZE(tmp);
3764 keywords = asdl_seq_new(len, arena);
3765 if (keywords == NULL) goto failed;
3766 for (i = 0; i < len; i++) {
3767 keyword_ty value;
3768 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3769 if (res != 0) goto failed;
3770 asdl_seq_SET(keywords, i, value);
3771 }
3772 Py_XDECREF(tmp);
3773 tmp = NULL;
3774 } else {
3775 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3776 return 1;
3777 }
3778 if (PyObject_HasAttrString(obj, "starargs")) {
3779 int res;
3780 tmp = PyObject_GetAttrString(obj, "starargs");
3781 if (tmp == NULL) goto failed;
3782 res = obj2ast_expr(tmp, &starargs, arena);
3783 if (res != 0) goto failed;
3784 Py_XDECREF(tmp);
3785 tmp = NULL;
3786 } else {
3787 starargs = NULL;
3788 }
3789 if (PyObject_HasAttrString(obj, "kwargs")) {
3790 int res;
3791 tmp = PyObject_GetAttrString(obj, "kwargs");
3792 if (tmp == NULL) goto failed;
3793 res = obj2ast_expr(tmp, &kwargs, arena);
3794 if (res != 0) goto failed;
3795 Py_XDECREF(tmp);
3796 tmp = NULL;
3797 } else {
3798 kwargs = NULL;
3799 }
3800 if (PyObject_HasAttrString(obj, "body")) {
3801 int res;
3802 Py_ssize_t len;
3803 Py_ssize_t i;
3804 tmp = PyObject_GetAttrString(obj, "body");
3805 if (tmp == NULL) goto failed;
3806 if (!PyList_Check(tmp)) {
3807 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3808 goto failed;
3809 }
3810 len = PyList_GET_SIZE(tmp);
3811 body = asdl_seq_new(len, arena);
3812 if (body == NULL) goto failed;
3813 for (i = 0; i < len; i++) {
3814 stmt_ty value;
3815 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3816 if (res != 0) goto failed;
3817 asdl_seq_SET(body, i, value);
3818 }
3819 Py_XDECREF(tmp);
3820 tmp = NULL;
3821 } else {
3822 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3823 return 1;
3824 }
3825 if (PyObject_HasAttrString(obj, "decorator_list")) {
3826 int res;
3827 Py_ssize_t len;
3828 Py_ssize_t i;
3829 tmp = PyObject_GetAttrString(obj, "decorator_list");
3830 if (tmp == NULL) goto failed;
3831 if (!PyList_Check(tmp)) {
3832 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3833 goto failed;
3834 }
3835 len = PyList_GET_SIZE(tmp);
3836 decorator_list = asdl_seq_new(len, arena);
3837 if (decorator_list == NULL) goto failed;
3838 for (i = 0; i < len; i++) {
3839 expr_ty value;
3840 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3841 if (res != 0) goto failed;
3842 asdl_seq_SET(decorator_list, i, value);
3843 }
3844 Py_XDECREF(tmp);
3845 tmp = NULL;
3846 } else {
3847 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3848 return 1;
3849 }
3850 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3851 decorator_list, lineno, col_offset, arena);
3852 if (*out == NULL) goto failed;
3853 return 0;
3854 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003855 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3856 if (isinstance == -1) {
3857 return 1;
3858 }
3859 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003860 expr_ty value;
3861
3862 if (PyObject_HasAttrString(obj, "value")) {
3863 int res;
3864 tmp = PyObject_GetAttrString(obj, "value");
3865 if (tmp == NULL) goto failed;
3866 res = obj2ast_expr(tmp, &value, arena);
3867 if (res != 0) goto failed;
3868 Py_XDECREF(tmp);
3869 tmp = NULL;
3870 } else {
3871 value = NULL;
3872 }
3873 *out = Return(value, lineno, col_offset, arena);
3874 if (*out == NULL) goto failed;
3875 return 0;
3876 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003877 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3878 if (isinstance == -1) {
3879 return 1;
3880 }
3881 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003882 asdl_seq* targets;
3883
3884 if (PyObject_HasAttrString(obj, "targets")) {
3885 int res;
3886 Py_ssize_t len;
3887 Py_ssize_t i;
3888 tmp = PyObject_GetAttrString(obj, "targets");
3889 if (tmp == NULL) goto failed;
3890 if (!PyList_Check(tmp)) {
3891 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3892 goto failed;
3893 }
3894 len = PyList_GET_SIZE(tmp);
3895 targets = asdl_seq_new(len, arena);
3896 if (targets == NULL) goto failed;
3897 for (i = 0; i < len; i++) {
3898 expr_ty value;
3899 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3900 if (res != 0) goto failed;
3901 asdl_seq_SET(targets, i, value);
3902 }
3903 Py_XDECREF(tmp);
3904 tmp = NULL;
3905 } else {
3906 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3907 return 1;
3908 }
3909 *out = Delete(targets, lineno, col_offset, arena);
3910 if (*out == NULL) goto failed;
3911 return 0;
3912 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003913 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3914 if (isinstance == -1) {
3915 return 1;
3916 }
3917 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003918 asdl_seq* targets;
3919 expr_ty value;
3920
3921 if (PyObject_HasAttrString(obj, "targets")) {
3922 int res;
3923 Py_ssize_t len;
3924 Py_ssize_t i;
3925 tmp = PyObject_GetAttrString(obj, "targets");
3926 if (tmp == NULL) goto failed;
3927 if (!PyList_Check(tmp)) {
3928 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3929 goto failed;
3930 }
3931 len = PyList_GET_SIZE(tmp);
3932 targets = asdl_seq_new(len, arena);
3933 if (targets == NULL) goto failed;
3934 for (i = 0; i < len; i++) {
3935 expr_ty value;
3936 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3937 if (res != 0) goto failed;
3938 asdl_seq_SET(targets, i, value);
3939 }
3940 Py_XDECREF(tmp);
3941 tmp = NULL;
3942 } else {
3943 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3944 return 1;
3945 }
3946 if (PyObject_HasAttrString(obj, "value")) {
3947 int res;
3948 tmp = PyObject_GetAttrString(obj, "value");
3949 if (tmp == NULL) goto failed;
3950 res = obj2ast_expr(tmp, &value, arena);
3951 if (res != 0) goto failed;
3952 Py_XDECREF(tmp);
3953 tmp = NULL;
3954 } else {
3955 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3956 return 1;
3957 }
3958 *out = Assign(targets, value, lineno, col_offset, arena);
3959 if (*out == NULL) goto failed;
3960 return 0;
3961 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003962 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3963 if (isinstance == -1) {
3964 return 1;
3965 }
3966 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003967 expr_ty target;
3968 operator_ty op;
3969 expr_ty value;
3970
3971 if (PyObject_HasAttrString(obj, "target")) {
3972 int res;
3973 tmp = PyObject_GetAttrString(obj, "target");
3974 if (tmp == NULL) goto failed;
3975 res = obj2ast_expr(tmp, &target, arena);
3976 if (res != 0) goto failed;
3977 Py_XDECREF(tmp);
3978 tmp = NULL;
3979 } else {
3980 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3981 return 1;
3982 }
3983 if (PyObject_HasAttrString(obj, "op")) {
3984 int res;
3985 tmp = PyObject_GetAttrString(obj, "op");
3986 if (tmp == NULL) goto failed;
3987 res = obj2ast_operator(tmp, &op, arena);
3988 if (res != 0) goto failed;
3989 Py_XDECREF(tmp);
3990 tmp = NULL;
3991 } else {
3992 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3993 return 1;
3994 }
3995 if (PyObject_HasAttrString(obj, "value")) {
3996 int res;
3997 tmp = PyObject_GetAttrString(obj, "value");
3998 if (tmp == NULL) goto failed;
3999 res = obj2ast_expr(tmp, &value, arena);
4000 if (res != 0) goto failed;
4001 Py_XDECREF(tmp);
4002 tmp = NULL;
4003 } else {
4004 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4005 return 1;
4006 }
4007 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4008 if (*out == NULL) goto failed;
4009 return 0;
4010 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004011 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4012 if (isinstance == -1) {
4013 return 1;
4014 }
4015 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004016 expr_ty target;
4017 expr_ty iter;
4018 asdl_seq* body;
4019 asdl_seq* orelse;
4020
4021 if (PyObject_HasAttrString(obj, "target")) {
4022 int res;
4023 tmp = PyObject_GetAttrString(obj, "target");
4024 if (tmp == NULL) goto failed;
4025 res = obj2ast_expr(tmp, &target, arena);
4026 if (res != 0) goto failed;
4027 Py_XDECREF(tmp);
4028 tmp = NULL;
4029 } else {
4030 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4031 return 1;
4032 }
4033 if (PyObject_HasAttrString(obj, "iter")) {
4034 int res;
4035 tmp = PyObject_GetAttrString(obj, "iter");
4036 if (tmp == NULL) goto failed;
4037 res = obj2ast_expr(tmp, &iter, arena);
4038 if (res != 0) goto failed;
4039 Py_XDECREF(tmp);
4040 tmp = NULL;
4041 } else {
4042 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4043 return 1;
4044 }
4045 if (PyObject_HasAttrString(obj, "body")) {
4046 int res;
4047 Py_ssize_t len;
4048 Py_ssize_t i;
4049 tmp = PyObject_GetAttrString(obj, "body");
4050 if (tmp == NULL) goto failed;
4051 if (!PyList_Check(tmp)) {
4052 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4053 goto failed;
4054 }
4055 len = PyList_GET_SIZE(tmp);
4056 body = asdl_seq_new(len, arena);
4057 if (body == NULL) goto failed;
4058 for (i = 0; i < len; i++) {
4059 stmt_ty value;
4060 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4061 if (res != 0) goto failed;
4062 asdl_seq_SET(body, i, value);
4063 }
4064 Py_XDECREF(tmp);
4065 tmp = NULL;
4066 } else {
4067 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4068 return 1;
4069 }
4070 if (PyObject_HasAttrString(obj, "orelse")) {
4071 int res;
4072 Py_ssize_t len;
4073 Py_ssize_t i;
4074 tmp = PyObject_GetAttrString(obj, "orelse");
4075 if (tmp == NULL) goto failed;
4076 if (!PyList_Check(tmp)) {
4077 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4078 goto failed;
4079 }
4080 len = PyList_GET_SIZE(tmp);
4081 orelse = asdl_seq_new(len, arena);
4082 if (orelse == NULL) goto failed;
4083 for (i = 0; i < len; i++) {
4084 stmt_ty value;
4085 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4086 if (res != 0) goto failed;
4087 asdl_seq_SET(orelse, i, value);
4088 }
4089 Py_XDECREF(tmp);
4090 tmp = NULL;
4091 } else {
4092 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4093 return 1;
4094 }
4095 *out = For(target, iter, body, orelse, lineno, col_offset,
4096 arena);
4097 if (*out == NULL) goto failed;
4098 return 0;
4099 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004100 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4101 if (isinstance == -1) {
4102 return 1;
4103 }
4104 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004105 expr_ty test;
4106 asdl_seq* body;
4107 asdl_seq* orelse;
4108
4109 if (PyObject_HasAttrString(obj, "test")) {
4110 int res;
4111 tmp = PyObject_GetAttrString(obj, "test");
4112 if (tmp == NULL) goto failed;
4113 res = obj2ast_expr(tmp, &test, arena);
4114 if (res != 0) goto failed;
4115 Py_XDECREF(tmp);
4116 tmp = NULL;
4117 } else {
4118 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4119 return 1;
4120 }
4121 if (PyObject_HasAttrString(obj, "body")) {
4122 int res;
4123 Py_ssize_t len;
4124 Py_ssize_t i;
4125 tmp = PyObject_GetAttrString(obj, "body");
4126 if (tmp == NULL) goto failed;
4127 if (!PyList_Check(tmp)) {
4128 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4129 goto failed;
4130 }
4131 len = PyList_GET_SIZE(tmp);
4132 body = asdl_seq_new(len, arena);
4133 if (body == NULL) goto failed;
4134 for (i = 0; i < len; i++) {
4135 stmt_ty value;
4136 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4137 if (res != 0) goto failed;
4138 asdl_seq_SET(body, i, value);
4139 }
4140 Py_XDECREF(tmp);
4141 tmp = NULL;
4142 } else {
4143 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4144 return 1;
4145 }
4146 if (PyObject_HasAttrString(obj, "orelse")) {
4147 int res;
4148 Py_ssize_t len;
4149 Py_ssize_t i;
4150 tmp = PyObject_GetAttrString(obj, "orelse");
4151 if (tmp == NULL) goto failed;
4152 if (!PyList_Check(tmp)) {
4153 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4154 goto failed;
4155 }
4156 len = PyList_GET_SIZE(tmp);
4157 orelse = asdl_seq_new(len, arena);
4158 if (orelse == NULL) goto failed;
4159 for (i = 0; i < len; i++) {
4160 stmt_ty value;
4161 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4162 if (res != 0) goto failed;
4163 asdl_seq_SET(orelse, i, value);
4164 }
4165 Py_XDECREF(tmp);
4166 tmp = NULL;
4167 } else {
4168 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4169 return 1;
4170 }
4171 *out = While(test, body, orelse, lineno, col_offset, arena);
4172 if (*out == NULL) goto failed;
4173 return 0;
4174 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004175 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4176 if (isinstance == -1) {
4177 return 1;
4178 }
4179 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004180 expr_ty test;
4181 asdl_seq* body;
4182 asdl_seq* orelse;
4183
4184 if (PyObject_HasAttrString(obj, "test")) {
4185 int res;
4186 tmp = PyObject_GetAttrString(obj, "test");
4187 if (tmp == NULL) goto failed;
4188 res = obj2ast_expr(tmp, &test, arena);
4189 if (res != 0) goto failed;
4190 Py_XDECREF(tmp);
4191 tmp = NULL;
4192 } else {
4193 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4194 return 1;
4195 }
4196 if (PyObject_HasAttrString(obj, "body")) {
4197 int res;
4198 Py_ssize_t len;
4199 Py_ssize_t i;
4200 tmp = PyObject_GetAttrString(obj, "body");
4201 if (tmp == NULL) goto failed;
4202 if (!PyList_Check(tmp)) {
4203 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4204 goto failed;
4205 }
4206 len = PyList_GET_SIZE(tmp);
4207 body = asdl_seq_new(len, arena);
4208 if (body == NULL) goto failed;
4209 for (i = 0; i < len; i++) {
4210 stmt_ty value;
4211 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4212 if (res != 0) goto failed;
4213 asdl_seq_SET(body, i, value);
4214 }
4215 Py_XDECREF(tmp);
4216 tmp = NULL;
4217 } else {
4218 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4219 return 1;
4220 }
4221 if (PyObject_HasAttrString(obj, "orelse")) {
4222 int res;
4223 Py_ssize_t len;
4224 Py_ssize_t i;
4225 tmp = PyObject_GetAttrString(obj, "orelse");
4226 if (tmp == NULL) goto failed;
4227 if (!PyList_Check(tmp)) {
4228 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4229 goto failed;
4230 }
4231 len = PyList_GET_SIZE(tmp);
4232 orelse = asdl_seq_new(len, arena);
4233 if (orelse == NULL) goto failed;
4234 for (i = 0; i < len; i++) {
4235 stmt_ty value;
4236 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4237 if (res != 0) goto failed;
4238 asdl_seq_SET(orelse, i, value);
4239 }
4240 Py_XDECREF(tmp);
4241 tmp = NULL;
4242 } else {
4243 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4244 return 1;
4245 }
4246 *out = If(test, body, orelse, lineno, col_offset, arena);
4247 if (*out == NULL) goto failed;
4248 return 0;
4249 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004250 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4251 if (isinstance == -1) {
4252 return 1;
4253 }
4254 if (isinstance) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004255 asdl_seq* items;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004256 asdl_seq* body;
4257
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004258 if (PyObject_HasAttrString(obj, "items")) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004259 int res;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004260 Py_ssize_t len;
4261 Py_ssize_t i;
4262 tmp = PyObject_GetAttrString(obj, "items");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004263 if (tmp == NULL) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264 if (!PyList_Check(tmp)) {
4265 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4266 goto failed;
4267 }
4268 len = PyList_GET_SIZE(tmp);
4269 items = asdl_seq_new(len, arena);
4270 if (items == NULL) goto failed;
4271 for (i = 0; i < len; i++) {
4272 withitem_ty value;
4273 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4274 if (res != 0) goto failed;
4275 asdl_seq_SET(items, i, value);
4276 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004277 Py_XDECREF(tmp);
4278 tmp = NULL;
4279 } else {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004280 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004281 return 1;
4282 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004283 if (PyObject_HasAttrString(obj, "body")) {
4284 int res;
4285 Py_ssize_t len;
4286 Py_ssize_t i;
4287 tmp = PyObject_GetAttrString(obj, "body");
4288 if (tmp == NULL) goto failed;
4289 if (!PyList_Check(tmp)) {
4290 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4291 goto failed;
4292 }
4293 len = PyList_GET_SIZE(tmp);
4294 body = asdl_seq_new(len, arena);
4295 if (body == NULL) goto failed;
4296 for (i = 0; i < len; i++) {
4297 stmt_ty value;
4298 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4299 if (res != 0) goto failed;
4300 asdl_seq_SET(body, i, value);
4301 }
4302 Py_XDECREF(tmp);
4303 tmp = NULL;
4304 } else {
4305 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4306 return 1;
4307 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004308 *out = With(items, body, lineno, col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004309 if (*out == NULL) goto failed;
4310 return 0;
4311 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004312 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4313 if (isinstance == -1) {
4314 return 1;
4315 }
4316 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004317 expr_ty exc;
4318 expr_ty cause;
4319
4320 if (PyObject_HasAttrString(obj, "exc")) {
4321 int res;
4322 tmp = PyObject_GetAttrString(obj, "exc");
4323 if (tmp == NULL) goto failed;
4324 res = obj2ast_expr(tmp, &exc, arena);
4325 if (res != 0) goto failed;
4326 Py_XDECREF(tmp);
4327 tmp = NULL;
4328 } else {
4329 exc = NULL;
4330 }
4331 if (PyObject_HasAttrString(obj, "cause")) {
4332 int res;
4333 tmp = PyObject_GetAttrString(obj, "cause");
4334 if (tmp == NULL) goto failed;
4335 res = obj2ast_expr(tmp, &cause, arena);
4336 if (res != 0) goto failed;
4337 Py_XDECREF(tmp);
4338 tmp = NULL;
4339 } else {
4340 cause = NULL;
4341 }
4342 *out = Raise(exc, cause, lineno, col_offset, arena);
4343 if (*out == NULL) goto failed;
4344 return 0;
4345 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004346 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4347 if (isinstance == -1) {
4348 return 1;
4349 }
4350 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004351 asdl_seq* body;
4352 asdl_seq* handlers;
4353 asdl_seq* orelse;
4354
4355 if (PyObject_HasAttrString(obj, "body")) {
4356 int res;
4357 Py_ssize_t len;
4358 Py_ssize_t i;
4359 tmp = PyObject_GetAttrString(obj, "body");
4360 if (tmp == NULL) goto failed;
4361 if (!PyList_Check(tmp)) {
4362 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4363 goto failed;
4364 }
4365 len = PyList_GET_SIZE(tmp);
4366 body = asdl_seq_new(len, arena);
4367 if (body == NULL) goto failed;
4368 for (i = 0; i < len; i++) {
4369 stmt_ty value;
4370 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4371 if (res != 0) goto failed;
4372 asdl_seq_SET(body, i, value);
4373 }
4374 Py_XDECREF(tmp);
4375 tmp = NULL;
4376 } else {
4377 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4378 return 1;
4379 }
4380 if (PyObject_HasAttrString(obj, "handlers")) {
4381 int res;
4382 Py_ssize_t len;
4383 Py_ssize_t i;
4384 tmp = PyObject_GetAttrString(obj, "handlers");
4385 if (tmp == NULL) goto failed;
4386 if (!PyList_Check(tmp)) {
4387 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4388 goto failed;
4389 }
4390 len = PyList_GET_SIZE(tmp);
4391 handlers = asdl_seq_new(len, arena);
4392 if (handlers == NULL) goto failed;
4393 for (i = 0; i < len; i++) {
4394 excepthandler_ty value;
4395 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4396 if (res != 0) goto failed;
4397 asdl_seq_SET(handlers, i, value);
4398 }
4399 Py_XDECREF(tmp);
4400 tmp = NULL;
4401 } else {
4402 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4403 return 1;
4404 }
4405 if (PyObject_HasAttrString(obj, "orelse")) {
4406 int res;
4407 Py_ssize_t len;
4408 Py_ssize_t i;
4409 tmp = PyObject_GetAttrString(obj, "orelse");
4410 if (tmp == NULL) goto failed;
4411 if (!PyList_Check(tmp)) {
4412 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4413 goto failed;
4414 }
4415 len = PyList_GET_SIZE(tmp);
4416 orelse = asdl_seq_new(len, arena);
4417 if (orelse == NULL) goto failed;
4418 for (i = 0; i < len; i++) {
4419 stmt_ty value;
4420 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4421 if (res != 0) goto failed;
4422 asdl_seq_SET(orelse, i, value);
4423 }
4424 Py_XDECREF(tmp);
4425 tmp = NULL;
4426 } else {
4427 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4428 return 1;
4429 }
4430 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4431 arena);
4432 if (*out == NULL) goto failed;
4433 return 0;
4434 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004435 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4436 if (isinstance == -1) {
4437 return 1;
4438 }
4439 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004440 asdl_seq* body;
4441 asdl_seq* finalbody;
4442
4443 if (PyObject_HasAttrString(obj, "body")) {
4444 int res;
4445 Py_ssize_t len;
4446 Py_ssize_t i;
4447 tmp = PyObject_GetAttrString(obj, "body");
4448 if (tmp == NULL) goto failed;
4449 if (!PyList_Check(tmp)) {
4450 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451 goto failed;
4452 }
4453 len = PyList_GET_SIZE(tmp);
4454 body = asdl_seq_new(len, arena);
4455 if (body == NULL) goto failed;
4456 for (i = 0; i < len; i++) {
4457 stmt_ty value;
4458 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4459 if (res != 0) goto failed;
4460 asdl_seq_SET(body, i, value);
4461 }
4462 Py_XDECREF(tmp);
4463 tmp = NULL;
4464 } else {
4465 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4466 return 1;
4467 }
4468 if (PyObject_HasAttrString(obj, "finalbody")) {
4469 int res;
4470 Py_ssize_t len;
4471 Py_ssize_t i;
4472 tmp = PyObject_GetAttrString(obj, "finalbody");
4473 if (tmp == NULL) goto failed;
4474 if (!PyList_Check(tmp)) {
4475 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4476 goto failed;
4477 }
4478 len = PyList_GET_SIZE(tmp);
4479 finalbody = asdl_seq_new(len, arena);
4480 if (finalbody == NULL) goto failed;
4481 for (i = 0; i < len; i++) {
4482 stmt_ty value;
4483 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4484 if (res != 0) goto failed;
4485 asdl_seq_SET(finalbody, i, value);
4486 }
4487 Py_XDECREF(tmp);
4488 tmp = NULL;
4489 } else {
4490 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4491 return 1;
4492 }
4493 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4494 if (*out == NULL) goto failed;
4495 return 0;
4496 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004497 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4498 if (isinstance == -1) {
4499 return 1;
4500 }
4501 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004502 expr_ty test;
4503 expr_ty msg;
4504
4505 if (PyObject_HasAttrString(obj, "test")) {
4506 int res;
4507 tmp = PyObject_GetAttrString(obj, "test");
4508 if (tmp == NULL) goto failed;
4509 res = obj2ast_expr(tmp, &test, arena);
4510 if (res != 0) goto failed;
4511 Py_XDECREF(tmp);
4512 tmp = NULL;
4513 } else {
4514 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4515 return 1;
4516 }
4517 if (PyObject_HasAttrString(obj, "msg")) {
4518 int res;
4519 tmp = PyObject_GetAttrString(obj, "msg");
4520 if (tmp == NULL) goto failed;
4521 res = obj2ast_expr(tmp, &msg, arena);
4522 if (res != 0) goto failed;
4523 Py_XDECREF(tmp);
4524 tmp = NULL;
4525 } else {
4526 msg = NULL;
4527 }
4528 *out = Assert(test, msg, lineno, col_offset, arena);
4529 if (*out == NULL) goto failed;
4530 return 0;
4531 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004532 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4533 if (isinstance == -1) {
4534 return 1;
4535 }
4536 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004537 asdl_seq* names;
4538
4539 if (PyObject_HasAttrString(obj, "names")) {
4540 int res;
4541 Py_ssize_t len;
4542 Py_ssize_t i;
4543 tmp = PyObject_GetAttrString(obj, "names");
4544 if (tmp == NULL) goto failed;
4545 if (!PyList_Check(tmp)) {
4546 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4547 goto failed;
4548 }
4549 len = PyList_GET_SIZE(tmp);
4550 names = asdl_seq_new(len, arena);
4551 if (names == NULL) goto failed;
4552 for (i = 0; i < len; i++) {
4553 alias_ty value;
4554 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4555 if (res != 0) goto failed;
4556 asdl_seq_SET(names, i, value);
4557 }
4558 Py_XDECREF(tmp);
4559 tmp = NULL;
4560 } else {
4561 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4562 return 1;
4563 }
4564 *out = Import(names, lineno, col_offset, arena);
4565 if (*out == NULL) goto failed;
4566 return 0;
4567 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004568 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4569 if (isinstance == -1) {
4570 return 1;
4571 }
4572 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004573 identifier module;
4574 asdl_seq* names;
4575 int level;
4576
4577 if (PyObject_HasAttrString(obj, "module")) {
4578 int res;
4579 tmp = PyObject_GetAttrString(obj, "module");
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_identifier(tmp, &module, arena);
4582 if (res != 0) goto failed;
4583 Py_XDECREF(tmp);
4584 tmp = NULL;
4585 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004586 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004587 }
4588 if (PyObject_HasAttrString(obj, "names")) {
4589 int res;
4590 Py_ssize_t len;
4591 Py_ssize_t i;
4592 tmp = PyObject_GetAttrString(obj, "names");
4593 if (tmp == NULL) goto failed;
4594 if (!PyList_Check(tmp)) {
4595 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4596 goto failed;
4597 }
4598 len = PyList_GET_SIZE(tmp);
4599 names = asdl_seq_new(len, arena);
4600 if (names == NULL) goto failed;
4601 for (i = 0; i < len; i++) {
4602 alias_ty value;
4603 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4604 if (res != 0) goto failed;
4605 asdl_seq_SET(names, i, value);
4606 }
4607 Py_XDECREF(tmp);
4608 tmp = NULL;
4609 } else {
4610 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4611 return 1;
4612 }
4613 if (PyObject_HasAttrString(obj, "level")) {
4614 int res;
4615 tmp = PyObject_GetAttrString(obj, "level");
4616 if (tmp == NULL) goto failed;
4617 res = obj2ast_int(tmp, &level, arena);
4618 if (res != 0) goto failed;
4619 Py_XDECREF(tmp);
4620 tmp = NULL;
4621 } else {
4622 level = 0;
4623 }
4624 *out = ImportFrom(module, names, level, lineno, col_offset,
4625 arena);
4626 if (*out == NULL) goto failed;
4627 return 0;
4628 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004629 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4630 if (isinstance == -1) {
4631 return 1;
4632 }
4633 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004634 asdl_seq* names;
4635
4636 if (PyObject_HasAttrString(obj, "names")) {
4637 int res;
4638 Py_ssize_t len;
4639 Py_ssize_t i;
4640 tmp = PyObject_GetAttrString(obj, "names");
4641 if (tmp == NULL) goto failed;
4642 if (!PyList_Check(tmp)) {
4643 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4644 goto failed;
4645 }
4646 len = PyList_GET_SIZE(tmp);
4647 names = asdl_seq_new(len, arena);
4648 if (names == NULL) goto failed;
4649 for (i = 0; i < len; i++) {
4650 identifier value;
4651 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4652 if (res != 0) goto failed;
4653 asdl_seq_SET(names, i, value);
4654 }
4655 Py_XDECREF(tmp);
4656 tmp = NULL;
4657 } else {
4658 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4659 return 1;
4660 }
4661 *out = Global(names, lineno, col_offset, arena);
4662 if (*out == NULL) goto failed;
4663 return 0;
4664 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004665 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4666 if (isinstance == -1) {
4667 return 1;
4668 }
4669 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004670 asdl_seq* names;
4671
4672 if (PyObject_HasAttrString(obj, "names")) {
4673 int res;
4674 Py_ssize_t len;
4675 Py_ssize_t i;
4676 tmp = PyObject_GetAttrString(obj, "names");
4677 if (tmp == NULL) goto failed;
4678 if (!PyList_Check(tmp)) {
4679 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4680 goto failed;
4681 }
4682 len = PyList_GET_SIZE(tmp);
4683 names = asdl_seq_new(len, arena);
4684 if (names == NULL) goto failed;
4685 for (i = 0; i < len; i++) {
4686 identifier value;
4687 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4688 if (res != 0) goto failed;
4689 asdl_seq_SET(names, i, value);
4690 }
4691 Py_XDECREF(tmp);
4692 tmp = NULL;
4693 } else {
4694 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4695 return 1;
4696 }
4697 *out = Nonlocal(names, lineno, col_offset, arena);
4698 if (*out == NULL) goto failed;
4699 return 0;
4700 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004701 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4702 if (isinstance == -1) {
4703 return 1;
4704 }
4705 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004706 expr_ty value;
4707
4708 if (PyObject_HasAttrString(obj, "value")) {
4709 int res;
4710 tmp = PyObject_GetAttrString(obj, "value");
4711 if (tmp == NULL) goto failed;
4712 res = obj2ast_expr(tmp, &value, arena);
4713 if (res != 0) goto failed;
4714 Py_XDECREF(tmp);
4715 tmp = NULL;
4716 } else {
4717 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4718 return 1;
4719 }
4720 *out = Expr(value, lineno, col_offset, arena);
4721 if (*out == NULL) goto failed;
4722 return 0;
4723 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004724 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4725 if (isinstance == -1) {
4726 return 1;
4727 }
4728 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004729
4730 *out = Pass(lineno, col_offset, arena);
4731 if (*out == NULL) goto failed;
4732 return 0;
4733 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004734 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4735 if (isinstance == -1) {
4736 return 1;
4737 }
4738 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004739
4740 *out = Break(lineno, col_offset, arena);
4741 if (*out == NULL) goto failed;
4742 return 0;
4743 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004744 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4745 if (isinstance == -1) {
4746 return 1;
4747 }
4748 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004749
4750 *out = Continue(lineno, col_offset, arena);
4751 if (*out == NULL) goto failed;
4752 return 0;
4753 }
4754
Benjamin Peterson5b066812010-11-20 01:38:49 +00004755 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4756 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004757 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004758 return 1;
4759}
4760
4761int
4762obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4763{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004764 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004765
Benjamin Petersond8f65972010-11-20 04:31:07 +00004766 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004767 int lineno;
4768 int col_offset;
4769
4770 if (obj == Py_None) {
4771 *out = NULL;
4772 return 0;
4773 }
4774 if (PyObject_HasAttrString(obj, "lineno")) {
4775 int res;
4776 tmp = PyObject_GetAttrString(obj, "lineno");
4777 if (tmp == NULL) goto failed;
4778 res = obj2ast_int(tmp, &lineno, arena);
4779 if (res != 0) goto failed;
4780 Py_XDECREF(tmp);
4781 tmp = NULL;
4782 } else {
4783 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4784 return 1;
4785 }
4786 if (PyObject_HasAttrString(obj, "col_offset")) {
4787 int res;
4788 tmp = PyObject_GetAttrString(obj, "col_offset");
4789 if (tmp == NULL) goto failed;
4790 res = obj2ast_int(tmp, &col_offset, arena);
4791 if (res != 0) goto failed;
4792 Py_XDECREF(tmp);
4793 tmp = NULL;
4794 } else {
4795 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4796 return 1;
4797 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004798 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4799 if (isinstance == -1) {
4800 return 1;
4801 }
4802 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004803 boolop_ty op;
4804 asdl_seq* values;
4805
4806 if (PyObject_HasAttrString(obj, "op")) {
4807 int res;
4808 tmp = PyObject_GetAttrString(obj, "op");
4809 if (tmp == NULL) goto failed;
4810 res = obj2ast_boolop(tmp, &op, arena);
4811 if (res != 0) goto failed;
4812 Py_XDECREF(tmp);
4813 tmp = NULL;
4814 } else {
4815 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4816 return 1;
4817 }
4818 if (PyObject_HasAttrString(obj, "values")) {
4819 int res;
4820 Py_ssize_t len;
4821 Py_ssize_t i;
4822 tmp = PyObject_GetAttrString(obj, "values");
4823 if (tmp == NULL) goto failed;
4824 if (!PyList_Check(tmp)) {
4825 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4826 goto failed;
4827 }
4828 len = PyList_GET_SIZE(tmp);
4829 values = asdl_seq_new(len, arena);
4830 if (values == NULL) goto failed;
4831 for (i = 0; i < len; i++) {
4832 expr_ty value;
4833 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4834 if (res != 0) goto failed;
4835 asdl_seq_SET(values, i, value);
4836 }
4837 Py_XDECREF(tmp);
4838 tmp = NULL;
4839 } else {
4840 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4841 return 1;
4842 }
4843 *out = BoolOp(op, values, lineno, col_offset, arena);
4844 if (*out == NULL) goto failed;
4845 return 0;
4846 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004847 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4848 if (isinstance == -1) {
4849 return 1;
4850 }
4851 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004852 expr_ty left;
4853 operator_ty op;
4854 expr_ty right;
4855
4856 if (PyObject_HasAttrString(obj, "left")) {
4857 int res;
4858 tmp = PyObject_GetAttrString(obj, "left");
4859 if (tmp == NULL) goto failed;
4860 res = obj2ast_expr(tmp, &left, arena);
4861 if (res != 0) goto failed;
4862 Py_XDECREF(tmp);
4863 tmp = NULL;
4864 } else {
4865 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4866 return 1;
4867 }
4868 if (PyObject_HasAttrString(obj, "op")) {
4869 int res;
4870 tmp = PyObject_GetAttrString(obj, "op");
4871 if (tmp == NULL) goto failed;
4872 res = obj2ast_operator(tmp, &op, arena);
4873 if (res != 0) goto failed;
4874 Py_XDECREF(tmp);
4875 tmp = NULL;
4876 } else {
4877 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4878 return 1;
4879 }
4880 if (PyObject_HasAttrString(obj, "right")) {
4881 int res;
4882 tmp = PyObject_GetAttrString(obj, "right");
4883 if (tmp == NULL) goto failed;
4884 res = obj2ast_expr(tmp, &right, arena);
4885 if (res != 0) goto failed;
4886 Py_XDECREF(tmp);
4887 tmp = NULL;
4888 } else {
4889 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4890 return 1;
4891 }
4892 *out = BinOp(left, op, right, lineno, col_offset, arena);
4893 if (*out == NULL) goto failed;
4894 return 0;
4895 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004896 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4897 if (isinstance == -1) {
4898 return 1;
4899 }
4900 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004901 unaryop_ty op;
4902 expr_ty operand;
4903
4904 if (PyObject_HasAttrString(obj, "op")) {
4905 int res;
4906 tmp = PyObject_GetAttrString(obj, "op");
4907 if (tmp == NULL) goto failed;
4908 res = obj2ast_unaryop(tmp, &op, arena);
4909 if (res != 0) goto failed;
4910 Py_XDECREF(tmp);
4911 tmp = NULL;
4912 } else {
4913 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4914 return 1;
4915 }
4916 if (PyObject_HasAttrString(obj, "operand")) {
4917 int res;
4918 tmp = PyObject_GetAttrString(obj, "operand");
4919 if (tmp == NULL) goto failed;
4920 res = obj2ast_expr(tmp, &operand, arena);
4921 if (res != 0) goto failed;
4922 Py_XDECREF(tmp);
4923 tmp = NULL;
4924 } else {
4925 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4926 return 1;
4927 }
4928 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4929 if (*out == NULL) goto failed;
4930 return 0;
4931 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004932 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4933 if (isinstance == -1) {
4934 return 1;
4935 }
4936 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004937 arguments_ty args;
4938 expr_ty body;
4939
4940 if (PyObject_HasAttrString(obj, "args")) {
4941 int res;
4942 tmp = PyObject_GetAttrString(obj, "args");
4943 if (tmp == NULL) goto failed;
4944 res = obj2ast_arguments(tmp, &args, arena);
4945 if (res != 0) goto failed;
4946 Py_XDECREF(tmp);
4947 tmp = NULL;
4948 } else {
4949 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4950 return 1;
4951 }
4952 if (PyObject_HasAttrString(obj, "body")) {
4953 int res;
4954 tmp = PyObject_GetAttrString(obj, "body");
4955 if (tmp == NULL) goto failed;
4956 res = obj2ast_expr(tmp, &body, arena);
4957 if (res != 0) goto failed;
4958 Py_XDECREF(tmp);
4959 tmp = NULL;
4960 } else {
4961 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4962 return 1;
4963 }
4964 *out = Lambda(args, body, lineno, col_offset, arena);
4965 if (*out == NULL) goto failed;
4966 return 0;
4967 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004968 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4969 if (isinstance == -1) {
4970 return 1;
4971 }
4972 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004973 expr_ty test;
4974 expr_ty body;
4975 expr_ty orelse;
4976
4977 if (PyObject_HasAttrString(obj, "test")) {
4978 int res;
4979 tmp = PyObject_GetAttrString(obj, "test");
4980 if (tmp == NULL) goto failed;
4981 res = obj2ast_expr(tmp, &test, arena);
4982 if (res != 0) goto failed;
4983 Py_XDECREF(tmp);
4984 tmp = NULL;
4985 } else {
4986 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4987 return 1;
4988 }
4989 if (PyObject_HasAttrString(obj, "body")) {
4990 int res;
4991 tmp = PyObject_GetAttrString(obj, "body");
4992 if (tmp == NULL) goto failed;
4993 res = obj2ast_expr(tmp, &body, arena);
4994 if (res != 0) goto failed;
4995 Py_XDECREF(tmp);
4996 tmp = NULL;
4997 } else {
4998 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4999 return 1;
5000 }
5001 if (PyObject_HasAttrString(obj, "orelse")) {
5002 int res;
5003 tmp = PyObject_GetAttrString(obj, "orelse");
5004 if (tmp == NULL) goto failed;
5005 res = obj2ast_expr(tmp, &orelse, arena);
5006 if (res != 0) goto failed;
5007 Py_XDECREF(tmp);
5008 tmp = NULL;
5009 } else {
5010 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5011 return 1;
5012 }
5013 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5014 if (*out == NULL) goto failed;
5015 return 0;
5016 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005017 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5018 if (isinstance == -1) {
5019 return 1;
5020 }
5021 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005022 asdl_seq* keys;
5023 asdl_seq* values;
5024
5025 if (PyObject_HasAttrString(obj, "keys")) {
5026 int res;
5027 Py_ssize_t len;
5028 Py_ssize_t i;
5029 tmp = PyObject_GetAttrString(obj, "keys");
5030 if (tmp == NULL) goto failed;
5031 if (!PyList_Check(tmp)) {
5032 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5033 goto failed;
5034 }
5035 len = PyList_GET_SIZE(tmp);
5036 keys = asdl_seq_new(len, arena);
5037 if (keys == NULL) goto failed;
5038 for (i = 0; i < len; i++) {
5039 expr_ty value;
5040 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5041 if (res != 0) goto failed;
5042 asdl_seq_SET(keys, i, value);
5043 }
5044 Py_XDECREF(tmp);
5045 tmp = NULL;
5046 } else {
5047 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5048 return 1;
5049 }
5050 if (PyObject_HasAttrString(obj, "values")) {
5051 int res;
5052 Py_ssize_t len;
5053 Py_ssize_t i;
5054 tmp = PyObject_GetAttrString(obj, "values");
5055 if (tmp == NULL) goto failed;
5056 if (!PyList_Check(tmp)) {
5057 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5058 goto failed;
5059 }
5060 len = PyList_GET_SIZE(tmp);
5061 values = asdl_seq_new(len, arena);
5062 if (values == NULL) goto failed;
5063 for (i = 0; i < len; i++) {
5064 expr_ty value;
5065 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5066 if (res != 0) goto failed;
5067 asdl_seq_SET(values, i, value);
5068 }
5069 Py_XDECREF(tmp);
5070 tmp = NULL;
5071 } else {
5072 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5073 return 1;
5074 }
5075 *out = Dict(keys, values, lineno, col_offset, arena);
5076 if (*out == NULL) goto failed;
5077 return 0;
5078 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005079 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5080 if (isinstance == -1) {
5081 return 1;
5082 }
5083 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005084 asdl_seq* elts;
5085
5086 if (PyObject_HasAttrString(obj, "elts")) {
5087 int res;
5088 Py_ssize_t len;
5089 Py_ssize_t i;
5090 tmp = PyObject_GetAttrString(obj, "elts");
5091 if (tmp == NULL) goto failed;
5092 if (!PyList_Check(tmp)) {
5093 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5094 goto failed;
5095 }
5096 len = PyList_GET_SIZE(tmp);
5097 elts = asdl_seq_new(len, arena);
5098 if (elts == NULL) goto failed;
5099 for (i = 0; i < len; i++) {
5100 expr_ty value;
5101 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5102 if (res != 0) goto failed;
5103 asdl_seq_SET(elts, i, value);
5104 }
5105 Py_XDECREF(tmp);
5106 tmp = NULL;
5107 } else {
5108 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5109 return 1;
5110 }
5111 *out = Set(elts, lineno, col_offset, arena);
5112 if (*out == NULL) goto failed;
5113 return 0;
5114 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005115 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5116 if (isinstance == -1) {
5117 return 1;
5118 }
5119 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005120 expr_ty elt;
5121 asdl_seq* generators;
5122
5123 if (PyObject_HasAttrString(obj, "elt")) {
5124 int res;
5125 tmp = PyObject_GetAttrString(obj, "elt");
5126 if (tmp == NULL) goto failed;
5127 res = obj2ast_expr(tmp, &elt, arena);
5128 if (res != 0) goto failed;
5129 Py_XDECREF(tmp);
5130 tmp = NULL;
5131 } else {
5132 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5133 return 1;
5134 }
5135 if (PyObject_HasAttrString(obj, "generators")) {
5136 int res;
5137 Py_ssize_t len;
5138 Py_ssize_t i;
5139 tmp = PyObject_GetAttrString(obj, "generators");
5140 if (tmp == NULL) goto failed;
5141 if (!PyList_Check(tmp)) {
5142 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5143 goto failed;
5144 }
5145 len = PyList_GET_SIZE(tmp);
5146 generators = asdl_seq_new(len, arena);
5147 if (generators == NULL) goto failed;
5148 for (i = 0; i < len; i++) {
5149 comprehension_ty value;
5150 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5151 if (res != 0) goto failed;
5152 asdl_seq_SET(generators, i, value);
5153 }
5154 Py_XDECREF(tmp);
5155 tmp = NULL;
5156 } else {
5157 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5158 return 1;
5159 }
5160 *out = ListComp(elt, generators, lineno, col_offset, arena);
5161 if (*out == NULL) goto failed;
5162 return 0;
5163 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005164 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5165 if (isinstance == -1) {
5166 return 1;
5167 }
5168 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005169 expr_ty elt;
5170 asdl_seq* generators;
5171
5172 if (PyObject_HasAttrString(obj, "elt")) {
5173 int res;
5174 tmp = PyObject_GetAttrString(obj, "elt");
5175 if (tmp == NULL) goto failed;
5176 res = obj2ast_expr(tmp, &elt, arena);
5177 if (res != 0) goto failed;
5178 Py_XDECREF(tmp);
5179 tmp = NULL;
5180 } else {
5181 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5182 return 1;
5183 }
5184 if (PyObject_HasAttrString(obj, "generators")) {
5185 int res;
5186 Py_ssize_t len;
5187 Py_ssize_t i;
5188 tmp = PyObject_GetAttrString(obj, "generators");
5189 if (tmp == NULL) goto failed;
5190 if (!PyList_Check(tmp)) {
5191 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5192 goto failed;
5193 }
5194 len = PyList_GET_SIZE(tmp);
5195 generators = asdl_seq_new(len, arena);
5196 if (generators == NULL) goto failed;
5197 for (i = 0; i < len; i++) {
5198 comprehension_ty value;
5199 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5200 if (res != 0) goto failed;
5201 asdl_seq_SET(generators, i, value);
5202 }
5203 Py_XDECREF(tmp);
5204 tmp = NULL;
5205 } else {
5206 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5207 return 1;
5208 }
5209 *out = SetComp(elt, generators, lineno, col_offset, arena);
5210 if (*out == NULL) goto failed;
5211 return 0;
5212 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005213 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5214 if (isinstance == -1) {
5215 return 1;
5216 }
5217 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005218 expr_ty key;
5219 expr_ty value;
5220 asdl_seq* generators;
5221
5222 if (PyObject_HasAttrString(obj, "key")) {
5223 int res;
5224 tmp = PyObject_GetAttrString(obj, "key");
5225 if (tmp == NULL) goto failed;
5226 res = obj2ast_expr(tmp, &key, arena);
5227 if (res != 0) goto failed;
5228 Py_XDECREF(tmp);
5229 tmp = NULL;
5230 } else {
5231 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5232 return 1;
5233 }
5234 if (PyObject_HasAttrString(obj, "value")) {
5235 int res;
5236 tmp = PyObject_GetAttrString(obj, "value");
5237 if (tmp == NULL) goto failed;
5238 res = obj2ast_expr(tmp, &value, arena);
5239 if (res != 0) goto failed;
5240 Py_XDECREF(tmp);
5241 tmp = NULL;
5242 } else {
5243 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5244 return 1;
5245 }
5246 if (PyObject_HasAttrString(obj, "generators")) {
5247 int res;
5248 Py_ssize_t len;
5249 Py_ssize_t i;
5250 tmp = PyObject_GetAttrString(obj, "generators");
5251 if (tmp == NULL) goto failed;
5252 if (!PyList_Check(tmp)) {
5253 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5254 goto failed;
5255 }
5256 len = PyList_GET_SIZE(tmp);
5257 generators = asdl_seq_new(len, arena);
5258 if (generators == NULL) goto failed;
5259 for (i = 0; i < len; i++) {
5260 comprehension_ty value;
5261 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5262 if (res != 0) goto failed;
5263 asdl_seq_SET(generators, i, value);
5264 }
5265 Py_XDECREF(tmp);
5266 tmp = NULL;
5267 } else {
5268 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5269 return 1;
5270 }
5271 *out = DictComp(key, value, generators, lineno, col_offset,
5272 arena);
5273 if (*out == NULL) goto failed;
5274 return 0;
5275 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005276 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5277 if (isinstance == -1) {
5278 return 1;
5279 }
5280 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005281 expr_ty elt;
5282 asdl_seq* generators;
5283
5284 if (PyObject_HasAttrString(obj, "elt")) {
5285 int res;
5286 tmp = PyObject_GetAttrString(obj, "elt");
5287 if (tmp == NULL) goto failed;
5288 res = obj2ast_expr(tmp, &elt, arena);
5289 if (res != 0) goto failed;
5290 Py_XDECREF(tmp);
5291 tmp = NULL;
5292 } else {
5293 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5294 return 1;
5295 }
5296 if (PyObject_HasAttrString(obj, "generators")) {
5297 int res;
5298 Py_ssize_t len;
5299 Py_ssize_t i;
5300 tmp = PyObject_GetAttrString(obj, "generators");
5301 if (tmp == NULL) goto failed;
5302 if (!PyList_Check(tmp)) {
5303 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5304 goto failed;
5305 }
5306 len = PyList_GET_SIZE(tmp);
5307 generators = asdl_seq_new(len, arena);
5308 if (generators == NULL) goto failed;
5309 for (i = 0; i < len; i++) {
5310 comprehension_ty value;
5311 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5312 if (res != 0) goto failed;
5313 asdl_seq_SET(generators, i, value);
5314 }
5315 Py_XDECREF(tmp);
5316 tmp = NULL;
5317 } else {
5318 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5319 return 1;
5320 }
5321 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5322 if (*out == NULL) goto failed;
5323 return 0;
5324 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005325 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5326 if (isinstance == -1) {
5327 return 1;
5328 }
5329 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005330 expr_ty value;
5331
5332 if (PyObject_HasAttrString(obj, "value")) {
5333 int res;
5334 tmp = PyObject_GetAttrString(obj, "value");
5335 if (tmp == NULL) goto failed;
5336 res = obj2ast_expr(tmp, &value, arena);
5337 if (res != 0) goto failed;
5338 Py_XDECREF(tmp);
5339 tmp = NULL;
5340 } else {
5341 value = NULL;
5342 }
5343 *out = Yield(value, lineno, col_offset, arena);
5344 if (*out == NULL) goto failed;
5345 return 0;
5346 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005347 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5348 if (isinstance == -1) {
5349 return 1;
5350 }
5351 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005352 expr_ty left;
5353 asdl_int_seq* ops;
5354 asdl_seq* comparators;
5355
5356 if (PyObject_HasAttrString(obj, "left")) {
5357 int res;
5358 tmp = PyObject_GetAttrString(obj, "left");
5359 if (tmp == NULL) goto failed;
5360 res = obj2ast_expr(tmp, &left, arena);
5361 if (res != 0) goto failed;
5362 Py_XDECREF(tmp);
5363 tmp = NULL;
5364 } else {
5365 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5366 return 1;
5367 }
5368 if (PyObject_HasAttrString(obj, "ops")) {
5369 int res;
5370 Py_ssize_t len;
5371 Py_ssize_t i;
5372 tmp = PyObject_GetAttrString(obj, "ops");
5373 if (tmp == NULL) goto failed;
5374 if (!PyList_Check(tmp)) {
5375 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5376 goto failed;
5377 }
5378 len = PyList_GET_SIZE(tmp);
5379 ops = asdl_int_seq_new(len, arena);
5380 if (ops == NULL) goto failed;
5381 for (i = 0; i < len; i++) {
5382 cmpop_ty value;
5383 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5384 if (res != 0) goto failed;
5385 asdl_seq_SET(ops, i, value);
5386 }
5387 Py_XDECREF(tmp);
5388 tmp = NULL;
5389 } else {
5390 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5391 return 1;
5392 }
5393 if (PyObject_HasAttrString(obj, "comparators")) {
5394 int res;
5395 Py_ssize_t len;
5396 Py_ssize_t i;
5397 tmp = PyObject_GetAttrString(obj, "comparators");
5398 if (tmp == NULL) goto failed;
5399 if (!PyList_Check(tmp)) {
5400 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5401 goto failed;
5402 }
5403 len = PyList_GET_SIZE(tmp);
5404 comparators = asdl_seq_new(len, arena);
5405 if (comparators == NULL) goto failed;
5406 for (i = 0; i < len; i++) {
5407 expr_ty value;
5408 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5409 if (res != 0) goto failed;
5410 asdl_seq_SET(comparators, i, value);
5411 }
5412 Py_XDECREF(tmp);
5413 tmp = NULL;
5414 } else {
5415 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5416 return 1;
5417 }
5418 *out = Compare(left, ops, comparators, lineno, col_offset,
5419 arena);
5420 if (*out == NULL) goto failed;
5421 return 0;
5422 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005423 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5424 if (isinstance == -1) {
5425 return 1;
5426 }
5427 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005428 expr_ty func;
5429 asdl_seq* args;
5430 asdl_seq* keywords;
5431 expr_ty starargs;
5432 expr_ty kwargs;
5433
5434 if (PyObject_HasAttrString(obj, "func")) {
5435 int res;
5436 tmp = PyObject_GetAttrString(obj, "func");
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_expr(tmp, &func, arena);
5439 if (res != 0) goto failed;
5440 Py_XDECREF(tmp);
5441 tmp = NULL;
5442 } else {
5443 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5444 return 1;
5445 }
5446 if (PyObject_HasAttrString(obj, "args")) {
5447 int res;
5448 Py_ssize_t len;
5449 Py_ssize_t i;
5450 tmp = PyObject_GetAttrString(obj, "args");
5451 if (tmp == NULL) goto failed;
5452 if (!PyList_Check(tmp)) {
5453 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5454 goto failed;
5455 }
5456 len = PyList_GET_SIZE(tmp);
5457 args = asdl_seq_new(len, arena);
5458 if (args == NULL) goto failed;
5459 for (i = 0; i < len; i++) {
5460 expr_ty value;
5461 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5462 if (res != 0) goto failed;
5463 asdl_seq_SET(args, i, value);
5464 }
5465 Py_XDECREF(tmp);
5466 tmp = NULL;
5467 } else {
5468 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5469 return 1;
5470 }
5471 if (PyObject_HasAttrString(obj, "keywords")) {
5472 int res;
5473 Py_ssize_t len;
5474 Py_ssize_t i;
5475 tmp = PyObject_GetAttrString(obj, "keywords");
5476 if (tmp == NULL) goto failed;
5477 if (!PyList_Check(tmp)) {
5478 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5479 goto failed;
5480 }
5481 len = PyList_GET_SIZE(tmp);
5482 keywords = asdl_seq_new(len, arena);
5483 if (keywords == NULL) goto failed;
5484 for (i = 0; i < len; i++) {
5485 keyword_ty value;
5486 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5487 if (res != 0) goto failed;
5488 asdl_seq_SET(keywords, i, value);
5489 }
5490 Py_XDECREF(tmp);
5491 tmp = NULL;
5492 } else {
5493 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5494 return 1;
5495 }
5496 if (PyObject_HasAttrString(obj, "starargs")) {
5497 int res;
5498 tmp = PyObject_GetAttrString(obj, "starargs");
5499 if (tmp == NULL) goto failed;
5500 res = obj2ast_expr(tmp, &starargs, arena);
5501 if (res != 0) goto failed;
5502 Py_XDECREF(tmp);
5503 tmp = NULL;
5504 } else {
5505 starargs = NULL;
5506 }
5507 if (PyObject_HasAttrString(obj, "kwargs")) {
5508 int res;
5509 tmp = PyObject_GetAttrString(obj, "kwargs");
5510 if (tmp == NULL) goto failed;
5511 res = obj2ast_expr(tmp, &kwargs, arena);
5512 if (res != 0) goto failed;
5513 Py_XDECREF(tmp);
5514 tmp = NULL;
5515 } else {
5516 kwargs = NULL;
5517 }
5518 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5519 col_offset, arena);
5520 if (*out == NULL) goto failed;
5521 return 0;
5522 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005523 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5524 if (isinstance == -1) {
5525 return 1;
5526 }
5527 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005528 object n;
5529
5530 if (PyObject_HasAttrString(obj, "n")) {
5531 int res;
5532 tmp = PyObject_GetAttrString(obj, "n");
5533 if (tmp == NULL) goto failed;
5534 res = obj2ast_object(tmp, &n, arena);
5535 if (res != 0) goto failed;
5536 Py_XDECREF(tmp);
5537 tmp = NULL;
5538 } else {
5539 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5540 return 1;
5541 }
5542 *out = Num(n, lineno, col_offset, arena);
5543 if (*out == NULL) goto failed;
5544 return 0;
5545 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005546 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5547 if (isinstance == -1) {
5548 return 1;
5549 }
5550 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005551 string s;
5552
5553 if (PyObject_HasAttrString(obj, "s")) {
5554 int res;
5555 tmp = PyObject_GetAttrString(obj, "s");
5556 if (tmp == NULL) goto failed;
5557 res = obj2ast_string(tmp, &s, arena);
5558 if (res != 0) goto failed;
5559 Py_XDECREF(tmp);
5560 tmp = NULL;
5561 } else {
5562 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5563 return 1;
5564 }
5565 *out = Str(s, lineno, col_offset, arena);
5566 if (*out == NULL) goto failed;
5567 return 0;
5568 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005569 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5570 if (isinstance == -1) {
5571 return 1;
5572 }
5573 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005574 string s;
5575
5576 if (PyObject_HasAttrString(obj, "s")) {
5577 int res;
5578 tmp = PyObject_GetAttrString(obj, "s");
5579 if (tmp == NULL) goto failed;
5580 res = obj2ast_string(tmp, &s, arena);
5581 if (res != 0) goto failed;
5582 Py_XDECREF(tmp);
5583 tmp = NULL;
5584 } else {
5585 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5586 return 1;
5587 }
5588 *out = Bytes(s, lineno, col_offset, arena);
5589 if (*out == NULL) goto failed;
5590 return 0;
5591 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005592 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5593 if (isinstance == -1) {
5594 return 1;
5595 }
5596 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005597
5598 *out = Ellipsis(lineno, col_offset, arena);
5599 if (*out == NULL) goto failed;
5600 return 0;
5601 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005602 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5603 if (isinstance == -1) {
5604 return 1;
5605 }
5606 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005607 expr_ty value;
5608 identifier attr;
5609 expr_context_ty ctx;
5610
5611 if (PyObject_HasAttrString(obj, "value")) {
5612 int res;
5613 tmp = PyObject_GetAttrString(obj, "value");
5614 if (tmp == NULL) goto failed;
5615 res = obj2ast_expr(tmp, &value, arena);
5616 if (res != 0) goto failed;
5617 Py_XDECREF(tmp);
5618 tmp = NULL;
5619 } else {
5620 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5621 return 1;
5622 }
5623 if (PyObject_HasAttrString(obj, "attr")) {
5624 int res;
5625 tmp = PyObject_GetAttrString(obj, "attr");
5626 if (tmp == NULL) goto failed;
5627 res = obj2ast_identifier(tmp, &attr, arena);
5628 if (res != 0) goto failed;
5629 Py_XDECREF(tmp);
5630 tmp = NULL;
5631 } else {
5632 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5633 return 1;
5634 }
5635 if (PyObject_HasAttrString(obj, "ctx")) {
5636 int res;
5637 tmp = PyObject_GetAttrString(obj, "ctx");
5638 if (tmp == NULL) goto failed;
5639 res = obj2ast_expr_context(tmp, &ctx, arena);
5640 if (res != 0) goto failed;
5641 Py_XDECREF(tmp);
5642 tmp = NULL;
5643 } else {
5644 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5645 return 1;
5646 }
5647 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5648 if (*out == NULL) goto failed;
5649 return 0;
5650 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005651 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5652 if (isinstance == -1) {
5653 return 1;
5654 }
5655 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005656 expr_ty value;
5657 slice_ty slice;
5658 expr_context_ty ctx;
5659
5660 if (PyObject_HasAttrString(obj, "value")) {
5661 int res;
5662 tmp = PyObject_GetAttrString(obj, "value");
5663 if (tmp == NULL) goto failed;
5664 res = obj2ast_expr(tmp, &value, arena);
5665 if (res != 0) goto failed;
5666 Py_XDECREF(tmp);
5667 tmp = NULL;
5668 } else {
5669 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5670 return 1;
5671 }
5672 if (PyObject_HasAttrString(obj, "slice")) {
5673 int res;
5674 tmp = PyObject_GetAttrString(obj, "slice");
5675 if (tmp == NULL) goto failed;
5676 res = obj2ast_slice(tmp, &slice, arena);
5677 if (res != 0) goto failed;
5678 Py_XDECREF(tmp);
5679 tmp = NULL;
5680 } else {
5681 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5682 return 1;
5683 }
5684 if (PyObject_HasAttrString(obj, "ctx")) {
5685 int res;
5686 tmp = PyObject_GetAttrString(obj, "ctx");
5687 if (tmp == NULL) goto failed;
5688 res = obj2ast_expr_context(tmp, &ctx, arena);
5689 if (res != 0) goto failed;
5690 Py_XDECREF(tmp);
5691 tmp = NULL;
5692 } else {
5693 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5694 return 1;
5695 }
5696 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5697 if (*out == NULL) goto failed;
5698 return 0;
5699 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005700 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5701 if (isinstance == -1) {
5702 return 1;
5703 }
5704 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005705 expr_ty value;
5706 expr_context_ty ctx;
5707
5708 if (PyObject_HasAttrString(obj, "value")) {
5709 int res;
5710 tmp = PyObject_GetAttrString(obj, "value");
5711 if (tmp == NULL) goto failed;
5712 res = obj2ast_expr(tmp, &value, arena);
5713 if (res != 0) goto failed;
5714 Py_XDECREF(tmp);
5715 tmp = NULL;
5716 } else {
5717 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5718 return 1;
5719 }
5720 if (PyObject_HasAttrString(obj, "ctx")) {
5721 int res;
5722 tmp = PyObject_GetAttrString(obj, "ctx");
5723 if (tmp == NULL) goto failed;
5724 res = obj2ast_expr_context(tmp, &ctx, arena);
5725 if (res != 0) goto failed;
5726 Py_XDECREF(tmp);
5727 tmp = NULL;
5728 } else {
5729 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5730 return 1;
5731 }
5732 *out = Starred(value, ctx, lineno, col_offset, arena);
5733 if (*out == NULL) goto failed;
5734 return 0;
5735 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005736 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5737 if (isinstance == -1) {
5738 return 1;
5739 }
5740 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005741 identifier id;
5742 expr_context_ty ctx;
5743
5744 if (PyObject_HasAttrString(obj, "id")) {
5745 int res;
5746 tmp = PyObject_GetAttrString(obj, "id");
5747 if (tmp == NULL) goto failed;
5748 res = obj2ast_identifier(tmp, &id, arena);
5749 if (res != 0) goto failed;
5750 Py_XDECREF(tmp);
5751 tmp = NULL;
5752 } else {
5753 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5754 return 1;
5755 }
5756 if (PyObject_HasAttrString(obj, "ctx")) {
5757 int res;
5758 tmp = PyObject_GetAttrString(obj, "ctx");
5759 if (tmp == NULL) goto failed;
5760 res = obj2ast_expr_context(tmp, &ctx, arena);
5761 if (res != 0) goto failed;
5762 Py_XDECREF(tmp);
5763 tmp = NULL;
5764 } else {
5765 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5766 return 1;
5767 }
5768 *out = Name(id, ctx, lineno, col_offset, arena);
5769 if (*out == NULL) goto failed;
5770 return 0;
5771 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005772 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5773 if (isinstance == -1) {
5774 return 1;
5775 }
5776 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005777 asdl_seq* elts;
5778 expr_context_ty ctx;
5779
5780 if (PyObject_HasAttrString(obj, "elts")) {
5781 int res;
5782 Py_ssize_t len;
5783 Py_ssize_t i;
5784 tmp = PyObject_GetAttrString(obj, "elts");
5785 if (tmp == NULL) goto failed;
5786 if (!PyList_Check(tmp)) {
5787 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5788 goto failed;
5789 }
5790 len = PyList_GET_SIZE(tmp);
5791 elts = asdl_seq_new(len, arena);
5792 if (elts == NULL) goto failed;
5793 for (i = 0; i < len; i++) {
5794 expr_ty value;
5795 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5796 if (res != 0) goto failed;
5797 asdl_seq_SET(elts, i, value);
5798 }
5799 Py_XDECREF(tmp);
5800 tmp = NULL;
5801 } else {
5802 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5803 return 1;
5804 }
5805 if (PyObject_HasAttrString(obj, "ctx")) {
5806 int res;
5807 tmp = PyObject_GetAttrString(obj, "ctx");
5808 if (tmp == NULL) goto failed;
5809 res = obj2ast_expr_context(tmp, &ctx, arena);
5810 if (res != 0) goto failed;
5811 Py_XDECREF(tmp);
5812 tmp = NULL;
5813 } else {
5814 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5815 return 1;
5816 }
5817 *out = List(elts, ctx, lineno, col_offset, arena);
5818 if (*out == NULL) goto failed;
5819 return 0;
5820 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005821 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5822 if (isinstance == -1) {
5823 return 1;
5824 }
5825 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005826 asdl_seq* elts;
5827 expr_context_ty ctx;
5828
5829 if (PyObject_HasAttrString(obj, "elts")) {
5830 int res;
5831 Py_ssize_t len;
5832 Py_ssize_t i;
5833 tmp = PyObject_GetAttrString(obj, "elts");
5834 if (tmp == NULL) goto failed;
5835 if (!PyList_Check(tmp)) {
5836 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5837 goto failed;
5838 }
5839 len = PyList_GET_SIZE(tmp);
5840 elts = asdl_seq_new(len, arena);
5841 if (elts == NULL) goto failed;
5842 for (i = 0; i < len; i++) {
5843 expr_ty value;
5844 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5845 if (res != 0) goto failed;
5846 asdl_seq_SET(elts, i, value);
5847 }
5848 Py_XDECREF(tmp);
5849 tmp = NULL;
5850 } else {
5851 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5852 return 1;
5853 }
5854 if (PyObject_HasAttrString(obj, "ctx")) {
5855 int res;
5856 tmp = PyObject_GetAttrString(obj, "ctx");
5857 if (tmp == NULL) goto failed;
5858 res = obj2ast_expr_context(tmp, &ctx, arena);
5859 if (res != 0) goto failed;
5860 Py_XDECREF(tmp);
5861 tmp = NULL;
5862 } else {
5863 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5864 return 1;
5865 }
5866 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5867 if (*out == NULL) goto failed;
5868 return 0;
5869 }
5870
Benjamin Peterson5b066812010-11-20 01:38:49 +00005871 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5872 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005873 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005874 return 1;
5875}
5876
5877int
5878obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5879{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005880 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005881
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005882 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5883 if (isinstance == -1) {
5884 return 1;
5885 }
5886 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005887 *out = Load;
5888 return 0;
5889 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005890 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5891 if (isinstance == -1) {
5892 return 1;
5893 }
5894 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005895 *out = Store;
5896 return 0;
5897 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005898 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5899 if (isinstance == -1) {
5900 return 1;
5901 }
5902 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005903 *out = Del;
5904 return 0;
5905 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005906 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5907 if (isinstance == -1) {
5908 return 1;
5909 }
5910 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005911 *out = AugLoad;
5912 return 0;
5913 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005914 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5915 if (isinstance == -1) {
5916 return 1;
5917 }
5918 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005919 *out = AugStore;
5920 return 0;
5921 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005922 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5923 if (isinstance == -1) {
5924 return 1;
5925 }
5926 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005927 *out = Param;
5928 return 0;
5929 }
5930
Benjamin Peterson5b066812010-11-20 01:38:49 +00005931 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005932 return 1;
5933}
5934
5935int
5936obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5937{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005938 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005939
Benjamin Petersond8f65972010-11-20 04:31:07 +00005940 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005941
5942 if (obj == Py_None) {
5943 *out = NULL;
5944 return 0;
5945 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005946 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5947 if (isinstance == -1) {
5948 return 1;
5949 }
5950 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005951 expr_ty lower;
5952 expr_ty upper;
5953 expr_ty step;
5954
5955 if (PyObject_HasAttrString(obj, "lower")) {
5956 int res;
5957 tmp = PyObject_GetAttrString(obj, "lower");
5958 if (tmp == NULL) goto failed;
5959 res = obj2ast_expr(tmp, &lower, arena);
5960 if (res != 0) goto failed;
5961 Py_XDECREF(tmp);
5962 tmp = NULL;
5963 } else {
5964 lower = NULL;
5965 }
5966 if (PyObject_HasAttrString(obj, "upper")) {
5967 int res;
5968 tmp = PyObject_GetAttrString(obj, "upper");
5969 if (tmp == NULL) goto failed;
5970 res = obj2ast_expr(tmp, &upper, arena);
5971 if (res != 0) goto failed;
5972 Py_XDECREF(tmp);
5973 tmp = NULL;
5974 } else {
5975 upper = NULL;
5976 }
5977 if (PyObject_HasAttrString(obj, "step")) {
5978 int res;
5979 tmp = PyObject_GetAttrString(obj, "step");
5980 if (tmp == NULL) goto failed;
5981 res = obj2ast_expr(tmp, &step, arena);
5982 if (res != 0) goto failed;
5983 Py_XDECREF(tmp);
5984 tmp = NULL;
5985 } else {
5986 step = NULL;
5987 }
5988 *out = Slice(lower, upper, step, arena);
5989 if (*out == NULL) goto failed;
5990 return 0;
5991 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005992 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5993 if (isinstance == -1) {
5994 return 1;
5995 }
5996 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005997 asdl_seq* dims;
5998
5999 if (PyObject_HasAttrString(obj, "dims")) {
6000 int res;
6001 Py_ssize_t len;
6002 Py_ssize_t i;
6003 tmp = PyObject_GetAttrString(obj, "dims");
6004 if (tmp == NULL) goto failed;
6005 if (!PyList_Check(tmp)) {
6006 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6007 goto failed;
6008 }
6009 len = PyList_GET_SIZE(tmp);
6010 dims = asdl_seq_new(len, arena);
6011 if (dims == NULL) goto failed;
6012 for (i = 0; i < len; i++) {
6013 slice_ty value;
6014 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6015 if (res != 0) goto failed;
6016 asdl_seq_SET(dims, i, value);
6017 }
6018 Py_XDECREF(tmp);
6019 tmp = NULL;
6020 } else {
6021 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6022 return 1;
6023 }
6024 *out = ExtSlice(dims, arena);
6025 if (*out == NULL) goto failed;
6026 return 0;
6027 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006028 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6029 if (isinstance == -1) {
6030 return 1;
6031 }
6032 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006033 expr_ty value;
6034
6035 if (PyObject_HasAttrString(obj, "value")) {
6036 int res;
6037 tmp = PyObject_GetAttrString(obj, "value");
6038 if (tmp == NULL) goto failed;
6039 res = obj2ast_expr(tmp, &value, arena);
6040 if (res != 0) goto failed;
6041 Py_XDECREF(tmp);
6042 tmp = NULL;
6043 } else {
6044 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6045 return 1;
6046 }
6047 *out = Index(value, arena);
6048 if (*out == NULL) goto failed;
6049 return 0;
6050 }
6051
Benjamin Peterson5b066812010-11-20 01:38:49 +00006052 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6053 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006054 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006055 return 1;
6056}
6057
6058int
6059obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6060{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006061 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006062
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006063 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6064 if (isinstance == -1) {
6065 return 1;
6066 }
6067 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006068 *out = And;
6069 return 0;
6070 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006071 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6072 if (isinstance == -1) {
6073 return 1;
6074 }
6075 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006076 *out = Or;
6077 return 0;
6078 }
6079
Benjamin Peterson5b066812010-11-20 01:38:49 +00006080 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006081 return 1;
6082}
6083
6084int
6085obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6086{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006087 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006088
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006089 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6090 if (isinstance == -1) {
6091 return 1;
6092 }
6093 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006094 *out = Add;
6095 return 0;
6096 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006097 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6098 if (isinstance == -1) {
6099 return 1;
6100 }
6101 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006102 *out = Sub;
6103 return 0;
6104 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006105 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6106 if (isinstance == -1) {
6107 return 1;
6108 }
6109 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006110 *out = Mult;
6111 return 0;
6112 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006113 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6114 if (isinstance == -1) {
6115 return 1;
6116 }
6117 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006118 *out = Div;
6119 return 0;
6120 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006121 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6122 if (isinstance == -1) {
6123 return 1;
6124 }
6125 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006126 *out = Mod;
6127 return 0;
6128 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006129 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6130 if (isinstance == -1) {
6131 return 1;
6132 }
6133 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006134 *out = Pow;
6135 return 0;
6136 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006137 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6138 if (isinstance == -1) {
6139 return 1;
6140 }
6141 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006142 *out = LShift;
6143 return 0;
6144 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006145 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6146 if (isinstance == -1) {
6147 return 1;
6148 }
6149 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006150 *out = RShift;
6151 return 0;
6152 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006153 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6154 if (isinstance == -1) {
6155 return 1;
6156 }
6157 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006158 *out = BitOr;
6159 return 0;
6160 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006161 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6162 if (isinstance == -1) {
6163 return 1;
6164 }
6165 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006166 *out = BitXor;
6167 return 0;
6168 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006169 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6170 if (isinstance == -1) {
6171 return 1;
6172 }
6173 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006174 *out = BitAnd;
6175 return 0;
6176 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006177 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6178 if (isinstance == -1) {
6179 return 1;
6180 }
6181 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006182 *out = FloorDiv;
6183 return 0;
6184 }
6185
Benjamin Peterson5b066812010-11-20 01:38:49 +00006186 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006187 return 1;
6188}
6189
6190int
6191obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6192{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006193 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006194
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006195 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6196 if (isinstance == -1) {
6197 return 1;
6198 }
6199 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006200 *out = Invert;
6201 return 0;
6202 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006203 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6204 if (isinstance == -1) {
6205 return 1;
6206 }
6207 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006208 *out = Not;
6209 return 0;
6210 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006211 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6212 if (isinstance == -1) {
6213 return 1;
6214 }
6215 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006216 *out = UAdd;
6217 return 0;
6218 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006219 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6220 if (isinstance == -1) {
6221 return 1;
6222 }
6223 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006224 *out = USub;
6225 return 0;
6226 }
6227
Benjamin Peterson5b066812010-11-20 01:38:49 +00006228 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006229 return 1;
6230}
6231
6232int
6233obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6234{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006235 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006236
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006237 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6238 if (isinstance == -1) {
6239 return 1;
6240 }
6241 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006242 *out = Eq;
6243 return 0;
6244 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006245 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6246 if (isinstance == -1) {
6247 return 1;
6248 }
6249 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006250 *out = NotEq;
6251 return 0;
6252 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006253 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6254 if (isinstance == -1) {
6255 return 1;
6256 }
6257 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006258 *out = Lt;
6259 return 0;
6260 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006261 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6262 if (isinstance == -1) {
6263 return 1;
6264 }
6265 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006266 *out = LtE;
6267 return 0;
6268 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006269 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6270 if (isinstance == -1) {
6271 return 1;
6272 }
6273 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006274 *out = Gt;
6275 return 0;
6276 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006277 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6278 if (isinstance == -1) {
6279 return 1;
6280 }
6281 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006282 *out = GtE;
6283 return 0;
6284 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006285 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6286 if (isinstance == -1) {
6287 return 1;
6288 }
6289 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006290 *out = Is;
6291 return 0;
6292 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006293 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6294 if (isinstance == -1) {
6295 return 1;
6296 }
6297 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006298 *out = IsNot;
6299 return 0;
6300 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006301 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6302 if (isinstance == -1) {
6303 return 1;
6304 }
6305 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006306 *out = In;
6307 return 0;
6308 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006309 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6310 if (isinstance == -1) {
6311 return 1;
6312 }
6313 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006314 *out = NotIn;
6315 return 0;
6316 }
6317
Benjamin Peterson5b066812010-11-20 01:38:49 +00006318 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006319 return 1;
6320}
6321
6322int
6323obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6324{
6325 PyObject* tmp = NULL;
6326 expr_ty target;
6327 expr_ty iter;
6328 asdl_seq* ifs;
6329
6330 if (PyObject_HasAttrString(obj, "target")) {
6331 int res;
6332 tmp = PyObject_GetAttrString(obj, "target");
6333 if (tmp == NULL) goto failed;
6334 res = obj2ast_expr(tmp, &target, arena);
6335 if (res != 0) goto failed;
6336 Py_XDECREF(tmp);
6337 tmp = NULL;
6338 } else {
6339 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6340 return 1;
6341 }
6342 if (PyObject_HasAttrString(obj, "iter")) {
6343 int res;
6344 tmp = PyObject_GetAttrString(obj, "iter");
6345 if (tmp == NULL) goto failed;
6346 res = obj2ast_expr(tmp, &iter, arena);
6347 if (res != 0) goto failed;
6348 Py_XDECREF(tmp);
6349 tmp = NULL;
6350 } else {
6351 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6352 return 1;
6353 }
6354 if (PyObject_HasAttrString(obj, "ifs")) {
6355 int res;
6356 Py_ssize_t len;
6357 Py_ssize_t i;
6358 tmp = PyObject_GetAttrString(obj, "ifs");
6359 if (tmp == NULL) goto failed;
6360 if (!PyList_Check(tmp)) {
6361 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6362 goto failed;
6363 }
6364 len = PyList_GET_SIZE(tmp);
6365 ifs = asdl_seq_new(len, arena);
6366 if (ifs == NULL) goto failed;
6367 for (i = 0; i < len; i++) {
6368 expr_ty value;
6369 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6370 if (res != 0) goto failed;
6371 asdl_seq_SET(ifs, i, value);
6372 }
6373 Py_XDECREF(tmp);
6374 tmp = NULL;
6375 } else {
6376 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6377 return 1;
6378 }
6379 *out = comprehension(target, iter, ifs, arena);
6380 return 0;
6381failed:
6382 Py_XDECREF(tmp);
6383 return 1;
6384}
6385
6386int
6387obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6388{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006389 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006390
Benjamin Petersond8f65972010-11-20 04:31:07 +00006391 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006392 int lineno;
6393 int col_offset;
6394
Neal Norwitzad74aa82008-03-31 05:14:30 +00006395 if (obj == Py_None) {
6396 *out = NULL;
6397 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006398 }
6399 if (PyObject_HasAttrString(obj, "lineno")) {
6400 int res;
6401 tmp = PyObject_GetAttrString(obj, "lineno");
6402 if (tmp == NULL) goto failed;
6403 res = obj2ast_int(tmp, &lineno, arena);
6404 if (res != 0) goto failed;
6405 Py_XDECREF(tmp);
6406 tmp = NULL;
6407 } else {
6408 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6409 return 1;
6410 }
6411 if (PyObject_HasAttrString(obj, "col_offset")) {
6412 int res;
6413 tmp = PyObject_GetAttrString(obj, "col_offset");
6414 if (tmp == NULL) goto failed;
6415 res = obj2ast_int(tmp, &col_offset, arena);
6416 if (res != 0) goto failed;
6417 Py_XDECREF(tmp);
6418 tmp = NULL;
6419 } else {
6420 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6421 return 1;
6422 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006423 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6424 if (isinstance == -1) {
6425 return 1;
6426 }
6427 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006428 expr_ty type;
6429 identifier name;
6430 asdl_seq* body;
6431
6432 if (PyObject_HasAttrString(obj, "type")) {
6433 int res;
6434 tmp = PyObject_GetAttrString(obj, "type");
6435 if (tmp == NULL) goto failed;
6436 res = obj2ast_expr(tmp, &type, arena);
6437 if (res != 0) goto failed;
6438 Py_XDECREF(tmp);
6439 tmp = NULL;
6440 } else {
6441 type = NULL;
6442 }
6443 if (PyObject_HasAttrString(obj, "name")) {
6444 int res;
6445 tmp = PyObject_GetAttrString(obj, "name");
6446 if (tmp == NULL) goto failed;
6447 res = obj2ast_identifier(tmp, &name, arena);
6448 if (res != 0) goto failed;
6449 Py_XDECREF(tmp);
6450 tmp = NULL;
6451 } else {
6452 name = NULL;
6453 }
6454 if (PyObject_HasAttrString(obj, "body")) {
6455 int res;
6456 Py_ssize_t len;
6457 Py_ssize_t i;
6458 tmp = PyObject_GetAttrString(obj, "body");
6459 if (tmp == NULL) goto failed;
6460 if (!PyList_Check(tmp)) {
6461 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6462 goto failed;
6463 }
6464 len = PyList_GET_SIZE(tmp);
6465 body = asdl_seq_new(len, arena);
6466 if (body == NULL) goto failed;
6467 for (i = 0; i < len; i++) {
6468 stmt_ty value;
6469 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6470 if (res != 0) goto failed;
6471 asdl_seq_SET(body, i, value);
6472 }
6473 Py_XDECREF(tmp);
6474 tmp = NULL;
6475 } else {
6476 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6477 return 1;
6478 }
6479 *out = ExceptHandler(type, name, body, lineno, col_offset,
6480 arena);
6481 if (*out == NULL) goto failed;
6482 return 0;
6483 }
6484
Benjamin Peterson5b066812010-11-20 01:38:49 +00006485 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6486 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006487 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006488 return 1;
6489}
6490
6491int
6492obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6493{
6494 PyObject* tmp = NULL;
6495 asdl_seq* args;
6496 identifier vararg;
6497 expr_ty varargannotation;
6498 asdl_seq* kwonlyargs;
6499 identifier kwarg;
6500 expr_ty kwargannotation;
6501 asdl_seq* defaults;
6502 asdl_seq* kw_defaults;
6503
6504 if (PyObject_HasAttrString(obj, "args")) {
6505 int res;
6506 Py_ssize_t len;
6507 Py_ssize_t i;
6508 tmp = PyObject_GetAttrString(obj, "args");
6509 if (tmp == NULL) goto failed;
6510 if (!PyList_Check(tmp)) {
6511 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6512 goto failed;
6513 }
6514 len = PyList_GET_SIZE(tmp);
6515 args = asdl_seq_new(len, arena);
6516 if (args == NULL) goto failed;
6517 for (i = 0; i < len; i++) {
6518 arg_ty value;
6519 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6520 if (res != 0) goto failed;
6521 asdl_seq_SET(args, i, value);
6522 }
6523 Py_XDECREF(tmp);
6524 tmp = NULL;
6525 } else {
6526 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6527 return 1;
6528 }
6529 if (PyObject_HasAttrString(obj, "vararg")) {
6530 int res;
6531 tmp = PyObject_GetAttrString(obj, "vararg");
6532 if (tmp == NULL) goto failed;
6533 res = obj2ast_identifier(tmp, &vararg, arena);
6534 if (res != 0) goto failed;
6535 Py_XDECREF(tmp);
6536 tmp = NULL;
6537 } else {
6538 vararg = NULL;
6539 }
6540 if (PyObject_HasAttrString(obj, "varargannotation")) {
6541 int res;
6542 tmp = PyObject_GetAttrString(obj, "varargannotation");
6543 if (tmp == NULL) goto failed;
6544 res = obj2ast_expr(tmp, &varargannotation, arena);
6545 if (res != 0) goto failed;
6546 Py_XDECREF(tmp);
6547 tmp = NULL;
6548 } else {
6549 varargannotation = NULL;
6550 }
6551 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6552 int res;
6553 Py_ssize_t len;
6554 Py_ssize_t i;
6555 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6556 if (tmp == NULL) goto failed;
6557 if (!PyList_Check(tmp)) {
6558 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6559 goto failed;
6560 }
6561 len = PyList_GET_SIZE(tmp);
6562 kwonlyargs = asdl_seq_new(len, arena);
6563 if (kwonlyargs == NULL) goto failed;
6564 for (i = 0; i < len; i++) {
6565 arg_ty value;
6566 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6567 if (res != 0) goto failed;
6568 asdl_seq_SET(kwonlyargs, i, value);
6569 }
6570 Py_XDECREF(tmp);
6571 tmp = NULL;
6572 } else {
6573 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6574 return 1;
6575 }
6576 if (PyObject_HasAttrString(obj, "kwarg")) {
6577 int res;
6578 tmp = PyObject_GetAttrString(obj, "kwarg");
6579 if (tmp == NULL) goto failed;
6580 res = obj2ast_identifier(tmp, &kwarg, arena);
6581 if (res != 0) goto failed;
6582 Py_XDECREF(tmp);
6583 tmp = NULL;
6584 } else {
6585 kwarg = NULL;
6586 }
6587 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6588 int res;
6589 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6590 if (tmp == NULL) goto failed;
6591 res = obj2ast_expr(tmp, &kwargannotation, arena);
6592 if (res != 0) goto failed;
6593 Py_XDECREF(tmp);
6594 tmp = NULL;
6595 } else {
6596 kwargannotation = NULL;
6597 }
6598 if (PyObject_HasAttrString(obj, "defaults")) {
6599 int res;
6600 Py_ssize_t len;
6601 Py_ssize_t i;
6602 tmp = PyObject_GetAttrString(obj, "defaults");
6603 if (tmp == NULL) goto failed;
6604 if (!PyList_Check(tmp)) {
6605 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6606 goto failed;
6607 }
6608 len = PyList_GET_SIZE(tmp);
6609 defaults = asdl_seq_new(len, arena);
6610 if (defaults == NULL) goto failed;
6611 for (i = 0; i < len; i++) {
6612 expr_ty value;
6613 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6614 if (res != 0) goto failed;
6615 asdl_seq_SET(defaults, i, value);
6616 }
6617 Py_XDECREF(tmp);
6618 tmp = NULL;
6619 } else {
6620 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6621 return 1;
6622 }
6623 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6624 int res;
6625 Py_ssize_t len;
6626 Py_ssize_t i;
6627 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6628 if (tmp == NULL) goto failed;
6629 if (!PyList_Check(tmp)) {
6630 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6631 goto failed;
6632 }
6633 len = PyList_GET_SIZE(tmp);
6634 kw_defaults = asdl_seq_new(len, arena);
6635 if (kw_defaults == NULL) goto failed;
6636 for (i = 0; i < len; i++) {
6637 expr_ty value;
6638 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6639 if (res != 0) goto failed;
6640 asdl_seq_SET(kw_defaults, i, value);
6641 }
6642 Py_XDECREF(tmp);
6643 tmp = NULL;
6644 } else {
6645 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6646 return 1;
6647 }
6648 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6649 kwargannotation, defaults, kw_defaults, arena);
6650 return 0;
6651failed:
6652 Py_XDECREF(tmp);
6653 return 1;
6654}
6655
6656int
6657obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6658{
6659 PyObject* tmp = NULL;
6660 identifier arg;
6661 expr_ty annotation;
6662
6663 if (PyObject_HasAttrString(obj, "arg")) {
6664 int res;
6665 tmp = PyObject_GetAttrString(obj, "arg");
6666 if (tmp == NULL) goto failed;
6667 res = obj2ast_identifier(tmp, &arg, arena);
6668 if (res != 0) goto failed;
6669 Py_XDECREF(tmp);
6670 tmp = NULL;
6671 } else {
6672 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6673 return 1;
6674 }
6675 if (PyObject_HasAttrString(obj, "annotation")) {
6676 int res;
6677 tmp = PyObject_GetAttrString(obj, "annotation");
6678 if (tmp == NULL) goto failed;
6679 res = obj2ast_expr(tmp, &annotation, arena);
6680 if (res != 0) goto failed;
6681 Py_XDECREF(tmp);
6682 tmp = NULL;
6683 } else {
6684 annotation = NULL;
6685 }
6686 *out = arg(arg, annotation, arena);
6687 return 0;
6688failed:
6689 Py_XDECREF(tmp);
6690 return 1;
6691}
6692
6693int
6694obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6695{
6696 PyObject* tmp = NULL;
6697 identifier arg;
6698 expr_ty value;
6699
6700 if (PyObject_HasAttrString(obj, "arg")) {
6701 int res;
6702 tmp = PyObject_GetAttrString(obj, "arg");
6703 if (tmp == NULL) goto failed;
6704 res = obj2ast_identifier(tmp, &arg, arena);
6705 if (res != 0) goto failed;
6706 Py_XDECREF(tmp);
6707 tmp = NULL;
6708 } else {
6709 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6710 return 1;
6711 }
6712 if (PyObject_HasAttrString(obj, "value")) {
6713 int res;
6714 tmp = PyObject_GetAttrString(obj, "value");
6715 if (tmp == NULL) goto failed;
6716 res = obj2ast_expr(tmp, &value, arena);
6717 if (res != 0) goto failed;
6718 Py_XDECREF(tmp);
6719 tmp = NULL;
6720 } else {
6721 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6722 return 1;
6723 }
6724 *out = keyword(arg, value, arena);
6725 return 0;
6726failed:
6727 Py_XDECREF(tmp);
6728 return 1;
6729}
6730
6731int
6732obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6733{
6734 PyObject* tmp = NULL;
6735 identifier name;
6736 identifier asname;
6737
6738 if (PyObject_HasAttrString(obj, "name")) {
6739 int res;
6740 tmp = PyObject_GetAttrString(obj, "name");
6741 if (tmp == NULL) goto failed;
6742 res = obj2ast_identifier(tmp, &name, arena);
6743 if (res != 0) goto failed;
6744 Py_XDECREF(tmp);
6745 tmp = NULL;
6746 } else {
6747 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6748 return 1;
6749 }
6750 if (PyObject_HasAttrString(obj, "asname")) {
6751 int res;
6752 tmp = PyObject_GetAttrString(obj, "asname");
6753 if (tmp == NULL) goto failed;
6754 res = obj2ast_identifier(tmp, &asname, arena);
6755 if (res != 0) goto failed;
6756 Py_XDECREF(tmp);
6757 tmp = NULL;
6758 } else {
6759 asname = NULL;
6760 }
6761 *out = alias(name, asname, arena);
6762 return 0;
6763failed:
6764 Py_XDECREF(tmp);
6765 return 1;
6766}
6767
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006768int
6769obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6770{
6771 PyObject* tmp = NULL;
6772 expr_ty context_expr;
6773 expr_ty optional_vars;
6774
6775 if (PyObject_HasAttrString(obj, "context_expr")) {
6776 int res;
6777 tmp = PyObject_GetAttrString(obj, "context_expr");
6778 if (tmp == NULL) goto failed;
6779 res = obj2ast_expr(tmp, &context_expr, arena);
6780 if (res != 0) goto failed;
6781 Py_XDECREF(tmp);
6782 tmp = NULL;
6783 } else {
6784 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6785 return 1;
6786 }
6787 if (PyObject_HasAttrString(obj, "optional_vars")) {
6788 int res;
6789 tmp = PyObject_GetAttrString(obj, "optional_vars");
6790 if (tmp == NULL) goto failed;
6791 res = obj2ast_expr(tmp, &optional_vars, arena);
6792 if (res != 0) goto failed;
6793 Py_XDECREF(tmp);
6794 tmp = NULL;
6795 } else {
6796 optional_vars = NULL;
6797 }
6798 *out = withitem(context_expr, optional_vars, arena);
6799 return 0;
6800failed:
6801 Py_XDECREF(tmp);
6802 return 1;
6803}
6804
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006805
Martin v. Löwis1a214512008-06-11 05:26:20 +00006806static struct PyModuleDef _astmodule = {
6807 PyModuleDef_HEAD_INIT, "_ast"
6808};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006809PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006810PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006811{
6812 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006813 if (!init_types()) return NULL;
6814 m = PyModule_Create(&_astmodule);
6815 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006816 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006817 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6818 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006819 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006820 return NULL;
Benjamin Peterson7f670e52011-05-27 14:01:01 -05006821 if (PyModule_AddStringConstant(m, "__version__", "9b11cc4e2918") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006822 return NULL;
6823 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6824 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006825 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006826 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006827 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006828 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006829 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006830 0) return NULL;
6831 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6832 NULL;
6833 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6834 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006835 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006836 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006837 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006838 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006839 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006840 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006841 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006842 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006843 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006844 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006845 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006846 0) return NULL;
6847 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6848 NULL;
6849 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6850 NULL;
6851 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6852 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6853 NULL;
6854 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6855 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006856 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006857 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006858 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006859 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006860 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006861 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006862 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006863 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006864 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006865 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006866 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006867 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006868 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006869 return NULL;
6870 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6873 NULL;
6874 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6875 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006876 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006877 return NULL;
6878 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6879 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006880 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006881 return NULL;
6882 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6883 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006884 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006885 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006886 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006887 return NULL;
6888 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6889 NULL;
6890 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6891 NULL;
6892 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6893 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006894 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006895 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006896 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006897 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006898 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006899 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006900 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006901 (PyObject*)GeneratorExp_type) < 0) return NULL;
6902 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6903 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006904 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006905 return NULL;
6906 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6907 NULL;
6908 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6909 NULL;
6910 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6911 NULL;
6912 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6913 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006914 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006915 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006916 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006917 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006918 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006919 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006920 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006921 return NULL;
6922 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6923 NULL;
6924 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6925 NULL;
6926 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6927 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006928 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006929 (PyObject*)expr_context_type) < 0) return NULL;
6930 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6931 NULL;
6932 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6933 NULL;
6934 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6935 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006936 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006937 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006938 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006939 return NULL;
6940 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6941 NULL;
6942 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6943 NULL;
6944 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6945 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006946 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006947 return NULL;
6948 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6949 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006950 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006951 return NULL;
6952 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6953 NULL;
6954 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006955 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006956 return NULL;
6957 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6958 NULL;
6959 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6960 NULL;
6961 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6962 NULL;
6963 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6964 NULL;
6965 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6966 NULL;
6967 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6968 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006969 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006970 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006971 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006972 return NULL;
6973 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6974 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006975 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006976 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006977 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006978 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006979 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006980 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006981 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006982 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006983 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006984 return NULL;
6985 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6986 NULL;
6987 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6988 NULL;
6989 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6990 NULL;
6991 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6992 NULL;
6993 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6994 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6995 NULL;
6996 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6997 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6998 NULL;
6999 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7000 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
7001 NULL;
7002 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7003 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7004 NULL;
7005 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7006 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7007 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007008 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00007009 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007010 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00007011 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007012 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00007013 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007014 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00007015 0) return NULL;
7016 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
7017 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007018 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007019 return NULL;
7020 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7021 NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007022 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7023 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00007024 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007025}
7026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007027
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007028PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007029{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007030 init_types();
7031 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007032}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007033
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007034/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7035mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007036{
7037 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007038 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
7039 (PyObject*)Interactive_type};
7040 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007041 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007042 assert(0 <= mode && mode <= 2);
7043
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007044 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007045
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007046 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7047 if (isinstance == -1)
7048 return NULL;
7049 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007050 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7051 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007052 return NULL;
7053 }
7054 if (obj2ast_mod(ast, &res, arena) != 0)
7055 return NULL;
7056 else
7057 return res;
7058}
7059
7060int PyAST_Check(PyObject* obj)
7061{
7062 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00007063 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007064}
7065
Martin v. Löwis5b222132007-06-10 09:51:05 +00007066