blob: b5976264635facafab9473f9b7ae6499e2acb876 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson04a90b42011-05-29 11:45:29 -05005 __version__ e0e663132363.
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};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500106static PyTypeObject *Try_type;
107static char *Try_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000108 "body",
109 "handlers",
110 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000111 "finalbody",
112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *Assert_type;
114static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115 "test",
116 "msg",
117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *Import_type;
119static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120 "names",
121};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000122static PyTypeObject *ImportFrom_type;
123static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000124 "module",
125 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000126 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000127};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000128static PyTypeObject *Global_type;
129static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000130 "names",
131};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000132static PyTypeObject *Nonlocal_type;
133static char *Nonlocal_fields[]={
134 "names",
135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *Expr_type;
137static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138 "value",
139};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000140static PyTypeObject *Pass_type;
141static PyTypeObject *Break_type;
142static PyTypeObject *Continue_type;
143static PyTypeObject *expr_type;
144static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000145 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000146 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000147};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000148static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000149static PyTypeObject *BoolOp_type;
150static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000151 "op",
152 "values",
153};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *BinOp_type;
155static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000156 "left",
157 "op",
158 "right",
159};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000160static PyTypeObject *UnaryOp_type;
161static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000162 "op",
163 "operand",
164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *Lambda_type;
166static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167 "args",
168 "body",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *IfExp_type;
171static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000172 "test",
173 "body",
174 "orelse",
175};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000176static PyTypeObject *Dict_type;
177static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000178 "keys",
179 "values",
180};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000181static PyTypeObject *Set_type;
182static char *Set_fields[]={
183 "elts",
184};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *ListComp_type;
186static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000187 "elt",
188 "generators",
189};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000190static PyTypeObject *SetComp_type;
191static char *SetComp_fields[]={
192 "elt",
193 "generators",
194};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000195static PyTypeObject *DictComp_type;
196static char *DictComp_fields[]={
197 "key",
198 "value",
199 "generators",
200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *GeneratorExp_type;
202static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000203 "elt",
204 "generators",
205};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000206static PyTypeObject *Yield_type;
207static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000208 "value",
209};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static PyTypeObject *Compare_type;
211static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000212 "left",
213 "ops",
214 "comparators",
215};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000216static PyTypeObject *Call_type;
217static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000218 "func",
219 "args",
220 "keywords",
221 "starargs",
222 "kwargs",
223};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000224static PyTypeObject *Num_type;
225static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000226 "n",
227};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000228static PyTypeObject *Str_type;
229static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230 "s",
231};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000232static PyTypeObject *Bytes_type;
233static char *Bytes_fields[]={
234 "s",
235};
Georg Brandl52318d62006-09-06 07:06:08 +0000236static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000237static PyTypeObject *Attribute_type;
238static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000239 "value",
240 "attr",
241 "ctx",
242};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000243static PyTypeObject *Subscript_type;
244static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000245 "value",
246 "slice",
247 "ctx",
248};
Guido van Rossum0368b722007-05-11 16:50:42 +0000249static PyTypeObject *Starred_type;
250static char *Starred_fields[]={
251 "value",
252 "ctx",
253};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000254static PyTypeObject *Name_type;
255static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000256 "id",
257 "ctx",
258};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000259static PyTypeObject *List_type;
260static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000261 "elts",
262 "ctx",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *Tuple_type;
265static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000266 "elts",
267 "ctx",
268};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000269static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000270static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
271*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
272static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static PyTypeObject *Load_type;
274static PyTypeObject *Store_type;
275static PyTypeObject *Del_type;
276static PyTypeObject *AugLoad_type;
277static PyTypeObject *AugStore_type;
278static PyTypeObject *Param_type;
279static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000280static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *Slice_type;
282static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000283 "lower",
284 "upper",
285 "step",
286};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000287static PyTypeObject *ExtSlice_type;
288static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000289 "dims",
290};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000291static PyTypeObject *Index_type;
292static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000293 "value",
294};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000295static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000296static PyObject *And_singleton, *Or_singleton;
297static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000298static PyTypeObject *And_type;
299static PyTypeObject *Or_type;
300static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
302*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
303*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
304*FloorDiv_singleton;
305static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000306static PyTypeObject *Add_type;
307static PyTypeObject *Sub_type;
308static PyTypeObject *Mult_type;
309static PyTypeObject *Div_type;
310static PyTypeObject *Mod_type;
311static PyTypeObject *Pow_type;
312static PyTypeObject *LShift_type;
313static PyTypeObject *RShift_type;
314static PyTypeObject *BitOr_type;
315static PyTypeObject *BitXor_type;
316static PyTypeObject *BitAnd_type;
317static PyTypeObject *FloorDiv_type;
318static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000319static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
320*USub_singleton;
321static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000322static PyTypeObject *Invert_type;
323static PyTypeObject *Not_type;
324static PyTypeObject *UAdd_type;
325static PyTypeObject *USub_type;
326static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000327static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
328*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
329*NotIn_singleton;
330static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000331static PyTypeObject *Eq_type;
332static PyTypeObject *NotEq_type;
333static PyTypeObject *Lt_type;
334static PyTypeObject *LtE_type;
335static PyTypeObject *Gt_type;
336static PyTypeObject *GtE_type;
337static PyTypeObject *Is_type;
338static PyTypeObject *IsNot_type;
339static PyTypeObject *In_type;
340static PyTypeObject *NotIn_type;
341static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000342static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000343static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344 "target",
345 "iter",
346 "ifs",
347};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000349static char *excepthandler_attributes[] = {
350 "lineno",
351 "col_offset",
352};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000354static PyTypeObject *ExceptHandler_type;
355static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000356 "type",
357 "name",
358 "body",
359};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000360static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000362static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000363 "args",
364 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000365 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000366 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000368 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000370 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000371};
Neal Norwitzc1505362006-12-28 06:47:50 +0000372static PyTypeObject *arg_type;
373static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000374static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000375 "arg",
376 "annotation",
377};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000378static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000379static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000380static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000381 "arg",
382 "value",
383};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000385static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000386static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387 "name",
388 "asname",
389};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500390static PyTypeObject *withitem_type;
391static PyObject* ast2obj_withitem(void*);
392static char *withitem_fields[]={
393 "context_expr",
394 "optional_vars",
395};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000396
397
Neal Norwitz207c9f32008-03-31 04:42:11 +0000398static int
399ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
400{
401 Py_ssize_t i, numfields = 0;
402 int res = -1;
403 PyObject *key, *value, *fields;
404 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
405 if (!fields)
406 PyErr_Clear();
407 if (fields) {
408 numfields = PySequence_Size(fields);
409 if (numfields == -1)
410 goto cleanup;
411 }
412 res = 0; /* if no error occurs, this stays 0 to the end */
413 if (PyTuple_GET_SIZE(args) > 0) {
414 if (numfields != PyTuple_GET_SIZE(args)) {
415 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000416 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000417 Py_TYPE(self)->tp_name,
418 numfields == 0 ? "" : "either 0 or ",
419 numfields, numfields == 1 ? "" : "s");
420 res = -1;
421 goto cleanup;
422 }
423 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
424 /* cannot be reached when fields is NULL */
425 PyObject *name = PySequence_GetItem(fields, i);
426 if (!name) {
427 res = -1;
428 goto cleanup;
429 }
430 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
431 Py_DECREF(name);
432 if (res < 0)
433 goto cleanup;
434 }
435 }
436 if (kw) {
437 i = 0; /* needed by PyDict_Next */
438 while (PyDict_Next(kw, &i, &key, &value)) {
439 res = PyObject_SetAttr(self, key, value);
440 if (res < 0)
441 goto cleanup;
442 }
443 }
444 cleanup:
445 Py_XDECREF(fields);
446 return res;
447}
448
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000449/* Pickling support */
450static PyObject *
451ast_type_reduce(PyObject *self, PyObject *unused)
452{
453 PyObject *res;
454 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
455 if (dict == NULL) {
456 if (PyErr_ExceptionMatches(PyExc_AttributeError))
457 PyErr_Clear();
458 else
459 return NULL;
460 }
461 if (dict) {
462 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
463 Py_DECREF(dict);
464 return res;
465 }
466 return Py_BuildValue("O()", Py_TYPE(self));
467}
468
469static PyMethodDef ast_type_methods[] = {
470 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
471 {NULL}
472};
473
Neal Norwitz207c9f32008-03-31 04:42:11 +0000474static PyTypeObject AST_type = {
475 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000476 "_ast.AST",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000477 sizeof(PyObject),
478 0,
479 0, /* tp_dealloc */
480 0, /* tp_print */
481 0, /* tp_getattr */
482 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000483 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000484 0, /* tp_repr */
485 0, /* tp_as_number */
486 0, /* tp_as_sequence */
487 0, /* tp_as_mapping */
488 0, /* tp_hash */
489 0, /* tp_call */
490 0, /* tp_str */
491 PyObject_GenericGetAttr, /* tp_getattro */
492 PyObject_GenericSetAttr, /* tp_setattro */
493 0, /* tp_as_buffer */
494 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
495 0, /* tp_doc */
496 0, /* tp_traverse */
497 0, /* tp_clear */
498 0, /* tp_richcompare */
499 0, /* tp_weaklistoffset */
500 0, /* tp_iter */
501 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000502 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000503 0, /* tp_members */
504 0, /* tp_getset */
505 0, /* tp_base */
506 0, /* tp_dict */
507 0, /* tp_descr_get */
508 0, /* tp_descr_set */
509 0, /* tp_dictoffset */
510 (initproc)ast_type_init, /* tp_init */
511 PyType_GenericAlloc, /* tp_alloc */
512 PyType_GenericNew, /* tp_new */
513 PyObject_Del, /* tp_free */
514};
515
516
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000517static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
518{
519 PyObject *fnames, *result;
520 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000521 fnames = PyTuple_New(num_fields);
522 if (!fnames) return NULL;
523 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000524 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000525 if (!field) {
526 Py_DECREF(fnames);
527 return NULL;
528 }
529 PyTuple_SET_ITEM(fnames, i, field);
530 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000531 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000532 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000533 Py_DECREF(fnames);
534 return (PyTypeObject*)result;
535}
536
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000537static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
538{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000540 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000541 if (!l)
542 return 0;
543 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000544 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000545 if (!s) {
546 Py_DECREF(l);
547 return 0;
548 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000549 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000550 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
552 Py_DECREF(l);
553 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000554}
555
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000556/* Conversion AST -> Python */
557
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000558static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
559{
560 int i, n = asdl_seq_LEN(seq);
561 PyObject *result = PyList_New(n);
562 PyObject *value;
563 if (!result)
564 return NULL;
565 for (i = 0; i < n; i++) {
566 value = func(asdl_seq_GET(seq, i));
567 if (!value) {
568 Py_DECREF(result);
569 return NULL;
570 }
571 PyList_SET_ITEM(result, i, value);
572 }
573 return result;
574}
575
576static PyObject* ast2obj_object(void *o)
577{
578 if (!o)
579 o = Py_None;
580 Py_INCREF((PyObject*)o);
581 return (PyObject*)o;
582}
583#define ast2obj_identifier ast2obj_object
584#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000585
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000586static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000587{
Christian Heimes217cfd12007-12-02 14:31:20 +0000588 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000589}
590
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000591/* Conversion Python -> AST */
592
593static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
594{
595 if (obj == Py_None)
596 obj = NULL;
597 if (obj)
598 PyArena_AddPyObject(arena, obj);
599 Py_XINCREF(obj);
600 *out = obj;
601 return 0;
602}
603
604#define obj2ast_identifier obj2ast_object
605#define obj2ast_string obj2ast_object
606
607static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
608{
609 int i;
610 if (!PyLong_Check(obj)) {
611 PyObject *s = PyObject_Repr(obj);
612 if (s == NULL) return 1;
613 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000614 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000615 Py_DECREF(s);
616 return 1;
617 }
618
619 i = (int)PyLong_AsLong(obj);
620 if (i == -1 && PyErr_Occurred())
621 return 1;
622 *out = i;
623 return 0;
624}
625
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000626static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000627{
628 PyObject *empty_tuple, *d;
629 if (PyType_Ready(&AST_type) < 0)
630 return -1;
631 d = AST_type.tp_dict;
632 empty_tuple = PyTuple_New(0);
633 if (!empty_tuple ||
634 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
635 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
636 Py_XDECREF(empty_tuple);
637 return -1;
638 }
639 Py_DECREF(empty_tuple);
640 return 0;
641}
642
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000643
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644static int init_types(void)
645{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000647 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000648 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000649 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!mod_type) return 0;
651 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000653 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 Interactive_type = make_type("Interactive", mod_type,
655 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000657 Expression_type = make_type("Expression", mod_type, Expression_fields,
658 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000662 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000664 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000665 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000666 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000668 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!If_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500684 With_type = make_type("With", stmt_type, With_fields, 2);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000685 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000686 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Raise_type) return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500688 Try_type = make_type("Try", stmt_type, Try_fields, 4);
689 if (!Try_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000695 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000699 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
700 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000703 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000705 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000707 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000709 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000711 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000716 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000718 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000720 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000724 Set_type = make_type("Set", expr_type, Set_fields, 1);
725 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000728 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
729 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000730 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
731 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 GeneratorExp_type = make_type("GeneratorExp", expr_type,
733 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000737 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000741 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000743 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000745 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
746 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000747 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
748 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000749 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000753 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
754 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000759 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000761 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!expr_context_type) return 0;
763 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000764 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000768 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000770 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000772 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000776 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000778 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000780 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000788 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!slice_type) return 0;
790 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000795 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000797 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!boolop_type) return 0;
799 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000800 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000804 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000806 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000808 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!operator_type) return 0;
810 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000811 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000813 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000815 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000816 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000817 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000819 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000821 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000823 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000825 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000827 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000829 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000831 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000833 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000835 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000837 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000839 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000841 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000843 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000845 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000847 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000849 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000851 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000853 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000855 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000857 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000859 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!unaryop_type) return 0;
861 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000862 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000866 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000868 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000870 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000872 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000874 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000876 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000878 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!cmpop_type) return 0;
880 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000881 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000883 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000885 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000887 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000889 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000890 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000891 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000893 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000895 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000897 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000898 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000899 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000900 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000901 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000902 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000903 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000904 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000905 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000906 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000907 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000908 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000909 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000910 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000911 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000913 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000915 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000917 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000919 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000921 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000922 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000924 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000926 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
927 return 0;
928 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
929 ExceptHandler_fields, 3);
930 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000931 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000932 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000933 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000934 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000935 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000936 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000937 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000938 if (!alias_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500939 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
940 if (!withitem_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000941 initialized = 1;
942 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000943}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000944
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000945static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
946static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
947static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
948static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
949 arena);
950static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
951static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
952static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
953static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
954static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
955static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
956 arena);
957static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
958 arena);
959static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
960static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
961static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
962static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500963static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000964
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000966Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967{
968 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000969 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000970 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 p->kind = Module_kind;
973 p->v.Module.body = body;
974 return p;
975}
976
977mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000978Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979{
980 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000982 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 p->kind = Interactive_kind;
985 p->v.Interactive.body = body;
986 return p;
987}
988
989mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991{
992 mod_ty p;
993 if (!body) {
994 PyErr_SetString(PyExc_ValueError,
995 "field body is required for Expression");
996 return NULL;
997 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000999 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 p->kind = Expression_kind;
1002 p->v.Expression.body = body;
1003 return p;
1004}
1005
1006mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001007Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008{
1009 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001010 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001011 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 p->kind = Suite_kind;
1014 p->v.Suite.body = body;
1015 return p;
1016}
1017
1018stmt_ty
1019FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001020 decorator_list, expr_ty returns, int lineno, int col_offset,
1021 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022{
1023 stmt_ty p;
1024 if (!name) {
1025 PyErr_SetString(PyExc_ValueError,
1026 "field name is required for FunctionDef");
1027 return NULL;
1028 }
1029 if (!args) {
1030 PyErr_SetString(PyExc_ValueError,
1031 "field args is required for FunctionDef");
1032 return NULL;
1033 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001034 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001035 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 p->kind = FunctionDef_kind;
1038 p->v.FunctionDef.name = name;
1039 p->v.FunctionDef.args = args;
1040 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001041 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001042 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001044 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 return p;
1046}
1047
1048stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001049ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001050 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1051 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052{
1053 stmt_ty p;
1054 if (!name) {
1055 PyErr_SetString(PyExc_ValueError,
1056 "field name is required for ClassDef");
1057 return NULL;
1058 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001059 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001060 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 p->kind = ClassDef_kind;
1063 p->v.ClassDef.name = name;
1064 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001065 p->v.ClassDef.keywords = keywords;
1066 p->v.ClassDef.starargs = starargs;
1067 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001069 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001071 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 return p;
1073}
1074
1075stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001076Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077{
1078 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001079 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001080 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 p->kind = Return_kind;
1083 p->v.Return.value = value;
1084 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001085 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return p;
1087}
1088
1089stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001090Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091{
1092 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001094 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 p->kind = Delete_kind;
1097 p->v.Delete.targets = targets;
1098 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001099 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 return p;
1101}
1102
1103stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001104Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1105 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106{
1107 stmt_ty p;
1108 if (!value) {
1109 PyErr_SetString(PyExc_ValueError,
1110 "field value is required for Assign");
1111 return NULL;
1112 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001113 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001114 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 p->kind = Assign_kind;
1117 p->v.Assign.targets = targets;
1118 p->v.Assign.value = value;
1119 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 return p;
1122}
1123
1124stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001125AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1126 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127{
1128 stmt_ty p;
1129 if (!target) {
1130 PyErr_SetString(PyExc_ValueError,
1131 "field target is required for AugAssign");
1132 return NULL;
1133 }
1134 if (!op) {
1135 PyErr_SetString(PyExc_ValueError,
1136 "field op is required for AugAssign");
1137 return NULL;
1138 }
1139 if (!value) {
1140 PyErr_SetString(PyExc_ValueError,
1141 "field value is required for AugAssign");
1142 return NULL;
1143 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001145 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 p->kind = AugAssign_kind;
1148 p->v.AugAssign.target = target;
1149 p->v.AugAssign.op = op;
1150 p->v.AugAssign.value = value;
1151 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 return p;
1154}
1155
1156stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001158 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159{
1160 stmt_ty p;
1161 if (!target) {
1162 PyErr_SetString(PyExc_ValueError,
1163 "field target is required for For");
1164 return NULL;
1165 }
1166 if (!iter) {
1167 PyErr_SetString(PyExc_ValueError,
1168 "field iter is required for For");
1169 return NULL;
1170 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001172 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 p->kind = For_kind;
1175 p->v.For.target = target;
1176 p->v.For.iter = iter;
1177 p->v.For.body = body;
1178 p->v.For.orelse = orelse;
1179 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001180 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 return p;
1182}
1183
1184stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1186 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187{
1188 stmt_ty p;
1189 if (!test) {
1190 PyErr_SetString(PyExc_ValueError,
1191 "field test is required for While");
1192 return NULL;
1193 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001195 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 p->kind = While_kind;
1198 p->v.While.test = test;
1199 p->v.While.body = body;
1200 p->v.While.orelse = orelse;
1201 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001202 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 return p;
1204}
1205
1206stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001207If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1208 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209{
1210 stmt_ty p;
1211 if (!test) {
1212 PyErr_SetString(PyExc_ValueError,
1213 "field test is required for If");
1214 return NULL;
1215 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001216 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001217 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 p->kind = If_kind;
1220 p->v.If.test = test;
1221 p->v.If.body = body;
1222 p->v.If.orelse = orelse;
1223 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001224 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return p;
1226}
1227
1228stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001229With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1230 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001231{
1232 stmt_ty p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001233 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001234 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001235 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001236 p->kind = With_kind;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001237 p->v.With.items = items;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001238 p->v.With.body = body;
1239 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001241 return p;
1242}
1243
1244stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001245Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246{
1247 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001249 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001252 p->v.Raise.exc = exc;
1253 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001255 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 return p;
1257}
1258
1259stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001260Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1261 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262{
1263 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001265 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 return NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001267 p->kind = Try_kind;
1268 p->v.Try.body = body;
1269 p->v.Try.handlers = handlers;
1270 p->v.Try.orelse = orelse;
1271 p->v.Try.finalbody = finalbody;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001273 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return p;
1275}
1276
1277stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279{
1280 stmt_ty p;
1281 if (!test) {
1282 PyErr_SetString(PyExc_ValueError,
1283 "field test is required for Assert");
1284 return NULL;
1285 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001286 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001287 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 p->kind = Assert_kind;
1290 p->v.Assert.test = test;
1291 p->v.Assert.msg = msg;
1292 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001293 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 return p;
1295}
1296
1297stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001298Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299{
1300 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001302 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 p->kind = Import_kind;
1305 p->v.Import.names = names;
1306 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001307 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 return p;
1309}
1310
1311stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001312ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1313 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314{
1315 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001316 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001317 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 p->kind = ImportFrom_kind;
1320 p->v.ImportFrom.module = module;
1321 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001322 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001324 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return p;
1326}
1327
1328stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
1331 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001333 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 p->kind = Global_kind;
1336 p->v.Global.names = names;
1337 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001338 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 return p;
1340}
1341
1342stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001343Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1344{
1345 stmt_ty p;
1346 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1347 if (!p)
1348 return NULL;
1349 p->kind = Nonlocal_kind;
1350 p->v.Nonlocal.names = names;
1351 p->lineno = lineno;
1352 p->col_offset = col_offset;
1353 return p;
1354}
1355
1356stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001357Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358{
1359 stmt_ty p;
1360 if (!value) {
1361 PyErr_SetString(PyExc_ValueError,
1362 "field value is required for Expr");
1363 return NULL;
1364 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001365 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001366 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 p->kind = Expr_kind;
1369 p->v.Expr.value = value;
1370 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001371 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return p;
1373}
1374
1375stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001376Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377{
1378 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001379 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001380 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 p->kind = Pass_kind;
1383 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001384 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 return p;
1386}
1387
1388stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001389Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390{
1391 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001393 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 p->kind = Break_kind;
1396 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001397 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 return p;
1399}
1400
1401stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001402Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403{
1404 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001405 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001406 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 p->kind = Continue_kind;
1409 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001410 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 return p;
1412}
1413
1414expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001415BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1416 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417{
1418 expr_ty p;
1419 if (!op) {
1420 PyErr_SetString(PyExc_ValueError,
1421 "field op is required for BoolOp");
1422 return NULL;
1423 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001424 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001425 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 p->kind = BoolOp_kind;
1428 p->v.BoolOp.op = op;
1429 p->v.BoolOp.values = values;
1430 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return p;
1433}
1434
1435expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001436BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1437 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438{
1439 expr_ty p;
1440 if (!left) {
1441 PyErr_SetString(PyExc_ValueError,
1442 "field left is required for BinOp");
1443 return NULL;
1444 }
1445 if (!op) {
1446 PyErr_SetString(PyExc_ValueError,
1447 "field op is required for BinOp");
1448 return NULL;
1449 }
1450 if (!right) {
1451 PyErr_SetString(PyExc_ValueError,
1452 "field right is required for BinOp");
1453 return NULL;
1454 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001455 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001456 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 p->kind = BinOp_kind;
1459 p->v.BinOp.left = left;
1460 p->v.BinOp.op = op;
1461 p->v.BinOp.right = right;
1462 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001463 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 return p;
1465}
1466
1467expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1469 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470{
1471 expr_ty p;
1472 if (!op) {
1473 PyErr_SetString(PyExc_ValueError,
1474 "field op is required for UnaryOp");
1475 return NULL;
1476 }
1477 if (!operand) {
1478 PyErr_SetString(PyExc_ValueError,
1479 "field operand is required for UnaryOp");
1480 return NULL;
1481 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001482 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001483 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 p->kind = UnaryOp_kind;
1486 p->v.UnaryOp.op = op;
1487 p->v.UnaryOp.operand = operand;
1488 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001489 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 return p;
1491}
1492
1493expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1495 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496{
1497 expr_ty p;
1498 if (!args) {
1499 PyErr_SetString(PyExc_ValueError,
1500 "field args is required for Lambda");
1501 return NULL;
1502 }
1503 if (!body) {
1504 PyErr_SetString(PyExc_ValueError,
1505 "field body is required for Lambda");
1506 return NULL;
1507 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001508 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001509 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 p->kind = Lambda_kind;
1512 p->v.Lambda.args = args;
1513 p->v.Lambda.body = body;
1514 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001515 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 return p;
1517}
1518
1519expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001520IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1521 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001522{
1523 expr_ty p;
1524 if (!test) {
1525 PyErr_SetString(PyExc_ValueError,
1526 "field test is required for IfExp");
1527 return NULL;
1528 }
1529 if (!body) {
1530 PyErr_SetString(PyExc_ValueError,
1531 "field body is required for IfExp");
1532 return NULL;
1533 }
1534 if (!orelse) {
1535 PyErr_SetString(PyExc_ValueError,
1536 "field orelse is required for IfExp");
1537 return NULL;
1538 }
1539 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001540 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001541 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001542 p->kind = IfExp_kind;
1543 p->v.IfExp.test = test;
1544 p->v.IfExp.body = body;
1545 p->v.IfExp.orelse = orelse;
1546 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001547 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001548 return p;
1549}
1550
1551expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001552Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1553 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554{
1555 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001556 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001557 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 p->kind = Dict_kind;
1560 p->v.Dict.keys = keys;
1561 p->v.Dict.values = values;
1562 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001563 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 return p;
1565}
1566
1567expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001568Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1569{
1570 expr_ty p;
1571 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001572 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001573 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001574 p->kind = Set_kind;
1575 p->v.Set.elts = elts;
1576 p->lineno = lineno;
1577 p->col_offset = col_offset;
1578 return p;
1579}
1580
1581expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001582ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1583 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584{
1585 expr_ty p;
1586 if (!elt) {
1587 PyErr_SetString(PyExc_ValueError,
1588 "field elt is required for ListComp");
1589 return NULL;
1590 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001591 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001592 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 p->kind = ListComp_kind;
1595 p->v.ListComp.elt = elt;
1596 p->v.ListComp.generators = generators;
1597 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001598 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 return p;
1600}
1601
1602expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001603SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1604 *arena)
1605{
1606 expr_ty p;
1607 if (!elt) {
1608 PyErr_SetString(PyExc_ValueError,
1609 "field elt is required for SetComp");
1610 return NULL;
1611 }
1612 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1613 if (!p)
1614 return NULL;
1615 p->kind = SetComp_kind;
1616 p->v.SetComp.elt = elt;
1617 p->v.SetComp.generators = generators;
1618 p->lineno = lineno;
1619 p->col_offset = col_offset;
1620 return p;
1621}
1622
1623expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001624DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1625 col_offset, PyArena *arena)
1626{
1627 expr_ty p;
1628 if (!key) {
1629 PyErr_SetString(PyExc_ValueError,
1630 "field key is required for DictComp");
1631 return NULL;
1632 }
1633 if (!value) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field value is required for DictComp");
1636 return NULL;
1637 }
1638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = DictComp_kind;
1642 p->v.DictComp.key = key;
1643 p->v.DictComp.value = value;
1644 p->v.DictComp.generators = generators;
1645 p->lineno = lineno;
1646 p->col_offset = col_offset;
1647 return p;
1648}
1649
1650expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1652 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653{
1654 expr_ty p;
1655 if (!elt) {
1656 PyErr_SetString(PyExc_ValueError,
1657 "field elt is required for GeneratorExp");
1658 return NULL;
1659 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001660 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001661 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 p->kind = GeneratorExp_kind;
1664 p->v.GeneratorExp.elt = elt;
1665 p->v.GeneratorExp.generators = generators;
1666 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001667 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 return p;
1669}
1670
1671expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001672Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673{
1674 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001675 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001676 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 p->kind = Yield_kind;
1679 p->v.Yield.value = value;
1680 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001681 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 return p;
1683}
1684
1685expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001686Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1687 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688{
1689 expr_ty p;
1690 if (!left) {
1691 PyErr_SetString(PyExc_ValueError,
1692 "field left is required for Compare");
1693 return NULL;
1694 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001695 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001696 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 p->kind = Compare_kind;
1699 p->v.Compare.left = left;
1700 p->v.Compare.ops = ops;
1701 p->v.Compare.comparators = comparators;
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
1708Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
1711 expr_ty p;
1712 if (!func) {
1713 PyErr_SetString(PyExc_ValueError,
1714 "field func is required for Call");
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 = Call_kind;
1721 p->v.Call.func = func;
1722 p->v.Call.args = args;
1723 p->v.Call.keywords = keywords;
1724 p->v.Call.starargs = starargs;
1725 p->v.Call.kwargs = kwargs;
1726 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001727 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 return p;
1729}
1730
1731expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001732Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733{
1734 expr_ty p;
1735 if (!n) {
1736 PyErr_SetString(PyExc_ValueError,
1737 "field n is required for Num");
1738 return NULL;
1739 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001740 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001741 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 p->kind = Num_kind;
1744 p->v.Num.n = n;
1745 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return p;
1748}
1749
1750expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001751Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
1753 expr_ty p;
1754 if (!s) {
1755 PyErr_SetString(PyExc_ValueError,
1756 "field s is required for Str");
1757 return NULL;
1758 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001759 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001760 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762 p->kind = Str_kind;
1763 p->v.Str.s = s;
1764 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001765 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return p;
1767}
1768
1769expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001770Bytes(string s, int lineno, int col_offset, PyArena *arena)
1771{
1772 expr_ty p;
1773 if (!s) {
1774 PyErr_SetString(PyExc_ValueError,
1775 "field s is required for Bytes");
1776 return NULL;
1777 }
1778 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001779 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001780 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001781 p->kind = Bytes_kind;
1782 p->v.Bytes.s = s;
1783 p->lineno = lineno;
1784 p->col_offset = col_offset;
1785 return p;
1786}
1787
1788expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001789Ellipsis(int lineno, int col_offset, PyArena *arena)
1790{
1791 expr_ty p;
1792 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001793 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001794 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001795 p->kind = Ellipsis_kind;
1796 p->lineno = lineno;
1797 p->col_offset = col_offset;
1798 return p;
1799}
1800
1801expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001802Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1803 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804{
1805 expr_ty p;
1806 if (!value) {
1807 PyErr_SetString(PyExc_ValueError,
1808 "field value is required for Attribute");
1809 return NULL;
1810 }
1811 if (!attr) {
1812 PyErr_SetString(PyExc_ValueError,
1813 "field attr is required for Attribute");
1814 return NULL;
1815 }
1816 if (!ctx) {
1817 PyErr_SetString(PyExc_ValueError,
1818 "field ctx is required for Attribute");
1819 return NULL;
1820 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001821 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001822 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 p->kind = Attribute_kind;
1825 p->v.Attribute.value = value;
1826 p->v.Attribute.attr = attr;
1827 p->v.Attribute.ctx = ctx;
1828 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001829 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 return p;
1831}
1832
1833expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001834Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1835 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836{
1837 expr_ty p;
1838 if (!value) {
1839 PyErr_SetString(PyExc_ValueError,
1840 "field value is required for Subscript");
1841 return NULL;
1842 }
1843 if (!slice) {
1844 PyErr_SetString(PyExc_ValueError,
1845 "field slice is required for Subscript");
1846 return NULL;
1847 }
1848 if (!ctx) {
1849 PyErr_SetString(PyExc_ValueError,
1850 "field ctx is required for Subscript");
1851 return NULL;
1852 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001853 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001854 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 p->kind = Subscript_kind;
1857 p->v.Subscript.value = value;
1858 p->v.Subscript.slice = slice;
1859 p->v.Subscript.ctx = ctx;
1860 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001861 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 return p;
1863}
1864
1865expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001866Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1867 *arena)
1868{
1869 expr_ty p;
1870 if (!value) {
1871 PyErr_SetString(PyExc_ValueError,
1872 "field value is required for Starred");
1873 return NULL;
1874 }
1875 if (!ctx) {
1876 PyErr_SetString(PyExc_ValueError,
1877 "field ctx is required for Starred");
1878 return NULL;
1879 }
1880 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1881 if (!p)
1882 return NULL;
1883 p->kind = Starred_kind;
1884 p->v.Starred.value = value;
1885 p->v.Starred.ctx = ctx;
1886 p->lineno = lineno;
1887 p->col_offset = col_offset;
1888 return p;
1889}
1890
1891expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001892Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1893 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894{
1895 expr_ty p;
1896 if (!id) {
1897 PyErr_SetString(PyExc_ValueError,
1898 "field id is required for Name");
1899 return NULL;
1900 }
1901 if (!ctx) {
1902 PyErr_SetString(PyExc_ValueError,
1903 "field ctx is required for Name");
1904 return NULL;
1905 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001907 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 p->kind = Name_kind;
1910 p->v.Name.id = id;
1911 p->v.Name.ctx = ctx;
1912 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001913 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 return p;
1915}
1916
1917expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001918List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1919 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920{
1921 expr_ty p;
1922 if (!ctx) {
1923 PyErr_SetString(PyExc_ValueError,
1924 "field ctx is required for List");
1925 return NULL;
1926 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001928 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 p->kind = List_kind;
1931 p->v.List.elts = elts;
1932 p->v.List.ctx = ctx;
1933 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001934 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 return p;
1936}
1937
1938expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001939Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1940 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941{
1942 expr_ty p;
1943 if (!ctx) {
1944 PyErr_SetString(PyExc_ValueError,
1945 "field ctx is required for Tuple");
1946 return NULL;
1947 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001948 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001949 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 p->kind = Tuple_kind;
1952 p->v.Tuple.elts = elts;
1953 p->v.Tuple.ctx = ctx;
1954 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001955 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 return p;
1957}
1958
1959slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001960Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961{
1962 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001963 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001964 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 p->kind = Slice_kind;
1967 p->v.Slice.lower = lower;
1968 p->v.Slice.upper = upper;
1969 p->v.Slice.step = step;
1970 return p;
1971}
1972
1973slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001974ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975{
1976 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001977 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001978 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 p->kind = ExtSlice_kind;
1981 p->v.ExtSlice.dims = dims;
1982 return p;
1983}
1984
1985slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001986Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987{
1988 slice_ty p;
1989 if (!value) {
1990 PyErr_SetString(PyExc_ValueError,
1991 "field value is required for Index");
1992 return NULL;
1993 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001994 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001995 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 p->kind = Index_kind;
1998 p->v.Index.value = value;
1999 return p;
2000}
2001
2002comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002003comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004{
2005 comprehension_ty p;
2006 if (!target) {
2007 PyErr_SetString(PyExc_ValueError,
2008 "field target is required for comprehension");
2009 return NULL;
2010 }
2011 if (!iter) {
2012 PyErr_SetString(PyExc_ValueError,
2013 "field iter is required for comprehension");
2014 return NULL;
2015 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002016 p = (comprehension_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->target = target;
2020 p->iter = iter;
2021 p->ifs = ifs;
2022 return p;
2023}
2024
2025excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002026ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002027 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028{
2029 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002031 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002033 p->kind = ExceptHandler_kind;
2034 p->v.ExceptHandler.type = type;
2035 p->v.ExceptHandler.name = name;
2036 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002037 p->lineno = lineno;
2038 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 return p;
2040}
2041
2042arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002043arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2044 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2045 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046{
2047 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002048 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002049 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 p->args = args;
2052 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002053 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002054 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002056 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002058 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 return p;
2060}
2061
Neal Norwitzc1505362006-12-28 06:47:50 +00002062arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002063arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002064{
2065 arg_ty p;
2066 if (!arg) {
2067 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002068 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002069 return NULL;
2070 }
2071 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002072 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002073 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002074 p->arg = arg;
2075 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002076 return p;
2077}
2078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081{
2082 keyword_ty p;
2083 if (!arg) {
2084 PyErr_SetString(PyExc_ValueError,
2085 "field arg is required for keyword");
2086 return NULL;
2087 }
2088 if (!value) {
2089 PyErr_SetString(PyExc_ValueError,
2090 "field value is required for keyword");
2091 return NULL;
2092 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002094 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 p->arg = arg;
2097 p->value = value;
2098 return p;
2099}
2100
2101alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103{
2104 alias_ty p;
2105 if (!name) {
2106 PyErr_SetString(PyExc_ValueError,
2107 "field name is required for alias");
2108 return NULL;
2109 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002110 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002111 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 p->name = name;
2114 p->asname = asname;
2115 return p;
2116}
2117
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002118withitem_ty
2119withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2120{
2121 withitem_ty p;
2122 if (!context_expr) {
2123 PyErr_SetString(PyExc_ValueError,
2124 "field context_expr is required for withitem");
2125 return NULL;
2126 }
2127 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2128 if (!p)
2129 return NULL;
2130 p->context_expr = context_expr;
2131 p->optional_vars = optional_vars;
2132 return p;
2133}
2134
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002135
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002136PyObject*
2137ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002138{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002139 mod_ty o = (mod_ty)_o;
2140 PyObject *result = NULL, *value = NULL;
2141 if (!o) {
2142 Py_INCREF(Py_None);
2143 return Py_None;
2144 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 switch (o->kind) {
2147 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002148 result = PyType_GenericNew(Module_type, NULL, NULL);
2149 if (!result) goto failed;
2150 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2151 if (!value) goto failed;
2152 if (PyObject_SetAttrString(result, "body", value) == -1)
2153 goto failed;
2154 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 break;
2156 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002157 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2158 if (!result) goto failed;
2159 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2160 if (!value) goto failed;
2161 if (PyObject_SetAttrString(result, "body", value) == -1)
2162 goto failed;
2163 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 break;
2165 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002166 result = PyType_GenericNew(Expression_type, NULL, NULL);
2167 if (!result) goto failed;
2168 value = ast2obj_expr(o->v.Expression.body);
2169 if (!value) goto failed;
2170 if (PyObject_SetAttrString(result, "body", value) == -1)
2171 goto failed;
2172 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 break;
2174 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002175 result = PyType_GenericNew(Suite_type, NULL, NULL);
2176 if (!result) goto failed;
2177 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2178 if (!value) goto failed;
2179 if (PyObject_SetAttrString(result, "body", value) == -1)
2180 goto failed;
2181 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 break;
2183 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002184 return result;
2185failed:
2186 Py_XDECREF(value);
2187 Py_XDECREF(result);
2188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189}
2190
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002191PyObject*
2192ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002194 stmt_ty o = (stmt_ty)_o;
2195 PyObject *result = NULL, *value = NULL;
2196 if (!o) {
2197 Py_INCREF(Py_None);
2198 return Py_None;
2199 }
2200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 switch (o->kind) {
2202 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002203 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2204 if (!result) goto failed;
2205 value = ast2obj_identifier(o->v.FunctionDef.name);
2206 if (!value) goto failed;
2207 if (PyObject_SetAttrString(result, "name", value) == -1)
2208 goto failed;
2209 Py_DECREF(value);
2210 value = ast2obj_arguments(o->v.FunctionDef.args);
2211 if (!value) goto failed;
2212 if (PyObject_SetAttrString(result, "args", value) == -1)
2213 goto failed;
2214 Py_DECREF(value);
2215 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2216 if (!value) goto failed;
2217 if (PyObject_SetAttrString(result, "body", value) == -1)
2218 goto failed;
2219 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002220 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2221 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002222 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002223 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2224 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002225 goto failed;
2226 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002227 value = ast2obj_expr(o->v.FunctionDef.returns);
2228 if (!value) goto failed;
2229 if (PyObject_SetAttrString(result, "returns", value) == -1)
2230 goto failed;
2231 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 break;
2233 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002234 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2235 if (!result) goto failed;
2236 value = ast2obj_identifier(o->v.ClassDef.name);
2237 if (!value) goto failed;
2238 if (PyObject_SetAttrString(result, "name", value) == -1)
2239 goto failed;
2240 Py_DECREF(value);
2241 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2242 if (!value) goto failed;
2243 if (PyObject_SetAttrString(result, "bases", value) == -1)
2244 goto failed;
2245 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002246 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2247 if (!value) goto failed;
2248 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2249 goto failed;
2250 Py_DECREF(value);
2251 value = ast2obj_expr(o->v.ClassDef.starargs);
2252 if (!value) goto failed;
2253 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2254 goto failed;
2255 Py_DECREF(value);
2256 value = ast2obj_expr(o->v.ClassDef.kwargs);
2257 if (!value) goto failed;
2258 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2259 goto failed;
2260 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002261 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2262 if (!value) goto failed;
2263 if (PyObject_SetAttrString(result, "body", value) == -1)
2264 goto failed;
2265 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002266 value = ast2obj_list(o->v.ClassDef.decorator_list,
2267 ast2obj_expr);
2268 if (!value) goto failed;
2269 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2270 -1)
2271 goto failed;
2272 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 break;
2274 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002275 result = PyType_GenericNew(Return_type, NULL, NULL);
2276 if (!result) goto failed;
2277 value = ast2obj_expr(o->v.Return.value);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "value", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 break;
2283 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 result = PyType_GenericNew(Delete_type, NULL, NULL);
2285 if (!result) goto failed;
2286 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2287 if (!value) goto failed;
2288 if (PyObject_SetAttrString(result, "targets", value) == -1)
2289 goto failed;
2290 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 break;
2292 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002293 result = PyType_GenericNew(Assign_type, NULL, NULL);
2294 if (!result) goto failed;
2295 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2296 if (!value) goto failed;
2297 if (PyObject_SetAttrString(result, "targets", value) == -1)
2298 goto failed;
2299 Py_DECREF(value);
2300 value = ast2obj_expr(o->v.Assign.value);
2301 if (!value) goto failed;
2302 if (PyObject_SetAttrString(result, "value", value) == -1)
2303 goto failed;
2304 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 break;
2306 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002307 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2308 if (!result) goto failed;
2309 value = ast2obj_expr(o->v.AugAssign.target);
2310 if (!value) goto failed;
2311 if (PyObject_SetAttrString(result, "target", value) == -1)
2312 goto failed;
2313 Py_DECREF(value);
2314 value = ast2obj_operator(o->v.AugAssign.op);
2315 if (!value) goto failed;
2316 if (PyObject_SetAttrString(result, "op", value) == -1)
2317 goto failed;
2318 Py_DECREF(value);
2319 value = ast2obj_expr(o->v.AugAssign.value);
2320 if (!value) goto failed;
2321 if (PyObject_SetAttrString(result, "value", value) == -1)
2322 goto failed;
2323 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002326 result = PyType_GenericNew(For_type, NULL, NULL);
2327 if (!result) goto failed;
2328 value = ast2obj_expr(o->v.For.target);
2329 if (!value) goto failed;
2330 if (PyObject_SetAttrString(result, "target", value) == -1)
2331 goto failed;
2332 Py_DECREF(value);
2333 value = ast2obj_expr(o->v.For.iter);
2334 if (!value) goto failed;
2335 if (PyObject_SetAttrString(result, "iter", value) == -1)
2336 goto failed;
2337 Py_DECREF(value);
2338 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2339 if (!value) goto failed;
2340 if (PyObject_SetAttrString(result, "body", value) == -1)
2341 goto failed;
2342 Py_DECREF(value);
2343 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2344 if (!value) goto failed;
2345 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2346 goto failed;
2347 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 break;
2349 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002350 result = PyType_GenericNew(While_type, NULL, NULL);
2351 if (!result) goto failed;
2352 value = ast2obj_expr(o->v.While.test);
2353 if (!value) goto failed;
2354 if (PyObject_SetAttrString(result, "test", value) == -1)
2355 goto failed;
2356 Py_DECREF(value);
2357 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2358 if (!value) goto failed;
2359 if (PyObject_SetAttrString(result, "body", value) == -1)
2360 goto failed;
2361 Py_DECREF(value);
2362 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2363 if (!value) goto failed;
2364 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2365 goto failed;
2366 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 break;
2368 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002369 result = PyType_GenericNew(If_type, NULL, NULL);
2370 if (!result) goto failed;
2371 value = ast2obj_expr(o->v.If.test);
2372 if (!value) goto failed;
2373 if (PyObject_SetAttrString(result, "test", value) == -1)
2374 goto failed;
2375 Py_DECREF(value);
2376 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2377 if (!value) goto failed;
2378 if (PyObject_SetAttrString(result, "body", value) == -1)
2379 goto failed;
2380 Py_DECREF(value);
2381 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2382 if (!value) goto failed;
2383 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2384 goto failed;
2385 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002387 case With_kind:
2388 result = PyType_GenericNew(With_type, NULL, NULL);
2389 if (!result) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002390 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002391 if (!value) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002392 if (PyObject_SetAttrString(result, "items", value) == -1)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002393 goto failed;
2394 Py_DECREF(value);
2395 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2396 if (!value) goto failed;
2397 if (PyObject_SetAttrString(result, "body", value) == -1)
2398 goto failed;
2399 Py_DECREF(value);
2400 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002402 result = PyType_GenericNew(Raise_type, NULL, NULL);
2403 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002404 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002405 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002406 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002407 goto failed;
2408 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002409 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002410 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002411 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002412 goto failed;
2413 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002415 case Try_kind:
2416 result = PyType_GenericNew(Try_type, NULL, NULL);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002417 if (!result) goto failed;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002418 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002419 if (!value) goto failed;
2420 if (PyObject_SetAttrString(result, "body", value) == -1)
2421 goto failed;
2422 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002423 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002424 if (!value) goto failed;
2425 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2426 goto failed;
2427 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002428 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429 if (!value) goto failed;
2430 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2431 goto failed;
2432 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002433 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002434 if (!value) goto failed;
2435 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 break;
2439 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 result = PyType_GenericNew(Assert_type, NULL, NULL);
2441 if (!result) goto failed;
2442 value = ast2obj_expr(o->v.Assert.test);
2443 if (!value) goto failed;
2444 if (PyObject_SetAttrString(result, "test", value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
2447 value = ast2obj_expr(o->v.Assert.msg);
2448 if (!value) goto failed;
2449 if (PyObject_SetAttrString(result, "msg", value) == -1)
2450 goto failed;
2451 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 break;
2453 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002454 result = PyType_GenericNew(Import_type, NULL, NULL);
2455 if (!result) goto failed;
2456 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2457 if (!value) goto failed;
2458 if (PyObject_SetAttrString(result, "names", value) == -1)
2459 goto failed;
2460 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 break;
2462 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002463 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2464 if (!result) goto failed;
2465 value = ast2obj_identifier(o->v.ImportFrom.module);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "module", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
2470 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2471 if (!value) goto failed;
2472 if (PyObject_SetAttrString(result, "names", value) == -1)
2473 goto failed;
2474 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002475 value = ast2obj_int(o->v.ImportFrom.level);
2476 if (!value) goto failed;
2477 if (PyObject_SetAttrString(result, "level", value) == -1)
2478 goto failed;
2479 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002482 result = PyType_GenericNew(Global_type, NULL, NULL);
2483 if (!result) goto failed;
2484 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2485 if (!value) goto failed;
2486 if (PyObject_SetAttrString(result, "names", value) == -1)
2487 goto failed;
2488 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002490 case Nonlocal_kind:
2491 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2492 if (!result) goto failed;
2493 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "names", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
2498 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002500 result = PyType_GenericNew(Expr_type, NULL, NULL);
2501 if (!result) goto failed;
2502 value = ast2obj_expr(o->v.Expr.value);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "value", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 break;
2508 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002509 result = PyType_GenericNew(Pass_type, NULL, NULL);
2510 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 break;
2512 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002513 result = PyType_GenericNew(Break_type, NULL, NULL);
2514 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 break;
2516 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002517 result = PyType_GenericNew(Continue_type, NULL, NULL);
2518 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 break;
2520 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002521 value = ast2obj_int(o->lineno);
2522 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002523 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2524 goto failed;
2525 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002526 value = ast2obj_int(o->col_offset);
2527 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002528 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2529 goto failed;
2530 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002531 return result;
2532failed:
2533 Py_XDECREF(value);
2534 Py_XDECREF(result);
2535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536}
2537
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002538PyObject*
2539ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002541 expr_ty o = (expr_ty)_o;
2542 PyObject *result = NULL, *value = NULL;
2543 if (!o) {
2544 Py_INCREF(Py_None);
2545 return Py_None;
2546 }
2547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 switch (o->kind) {
2549 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002550 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2551 if (!result) goto failed;
2552 value = ast2obj_boolop(o->v.BoolOp.op);
2553 if (!value) goto failed;
2554 if (PyObject_SetAttrString(result, "op", value) == -1)
2555 goto failed;
2556 Py_DECREF(value);
2557 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2558 if (!value) goto failed;
2559 if (PyObject_SetAttrString(result, "values", value) == -1)
2560 goto failed;
2561 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 break;
2563 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002564 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2565 if (!result) goto failed;
2566 value = ast2obj_expr(o->v.BinOp.left);
2567 if (!value) goto failed;
2568 if (PyObject_SetAttrString(result, "left", value) == -1)
2569 goto failed;
2570 Py_DECREF(value);
2571 value = ast2obj_operator(o->v.BinOp.op);
2572 if (!value) goto failed;
2573 if (PyObject_SetAttrString(result, "op", value) == -1)
2574 goto failed;
2575 Py_DECREF(value);
2576 value = ast2obj_expr(o->v.BinOp.right);
2577 if (!value) goto failed;
2578 if (PyObject_SetAttrString(result, "right", value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 break;
2582 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002583 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2584 if (!result) goto failed;
2585 value = ast2obj_unaryop(o->v.UnaryOp.op);
2586 if (!value) goto failed;
2587 if (PyObject_SetAttrString(result, "op", value) == -1)
2588 goto failed;
2589 Py_DECREF(value);
2590 value = ast2obj_expr(o->v.UnaryOp.operand);
2591 if (!value) goto failed;
2592 if (PyObject_SetAttrString(result, "operand", value) == -1)
2593 goto failed;
2594 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 break;
2596 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002597 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2598 if (!result) goto failed;
2599 value = ast2obj_arguments(o->v.Lambda.args);
2600 if (!value) goto failed;
2601 if (PyObject_SetAttrString(result, "args", value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
2604 value = ast2obj_expr(o->v.Lambda.body);
2605 if (!value) goto failed;
2606 if (PyObject_SetAttrString(result, "body", value) == -1)
2607 goto failed;
2608 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002610 case IfExp_kind:
2611 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2612 if (!result) goto failed;
2613 value = ast2obj_expr(o->v.IfExp.test);
2614 if (!value) goto failed;
2615 if (PyObject_SetAttrString(result, "test", value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
2618 value = ast2obj_expr(o->v.IfExp.body);
2619 if (!value) goto failed;
2620 if (PyObject_SetAttrString(result, "body", value) == -1)
2621 goto failed;
2622 Py_DECREF(value);
2623 value = ast2obj_expr(o->v.IfExp.orelse);
2624 if (!value) goto failed;
2625 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002630 result = PyType_GenericNew(Dict_type, NULL, NULL);
2631 if (!result) goto failed;
2632 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2633 if (!value) goto failed;
2634 if (PyObject_SetAttrString(result, "keys", value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2638 if (!value) goto failed;
2639 if (PyObject_SetAttrString(result, "values", value) == -1)
2640 goto failed;
2641 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002643 case Set_kind:
2644 result = PyType_GenericNew(Set_type, NULL, NULL);
2645 if (!result) goto failed;
2646 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "elts", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002653 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2654 if (!result) goto failed;
2655 value = ast2obj_expr(o->v.ListComp.elt);
2656 if (!value) goto failed;
2657 if (PyObject_SetAttrString(result, "elt", value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
2660 value = ast2obj_list(o->v.ListComp.generators,
2661 ast2obj_comprehension);
2662 if (!value) goto failed;
2663 if (PyObject_SetAttrString(result, "generators", value) == -1)
2664 goto failed;
2665 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002667 case SetComp_kind:
2668 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2669 if (!result) goto failed;
2670 value = ast2obj_expr(o->v.SetComp.elt);
2671 if (!value) goto failed;
2672 if (PyObject_SetAttrString(result, "elt", value) == -1)
2673 goto failed;
2674 Py_DECREF(value);
2675 value = ast2obj_list(o->v.SetComp.generators,
2676 ast2obj_comprehension);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "generators", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002682 case DictComp_kind:
2683 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_expr(o->v.DictComp.key);
2686 if (!value) goto failed;
2687 if (PyObject_SetAttrString(result, "key", value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 value = ast2obj_expr(o->v.DictComp.value);
2691 if (!value) goto failed;
2692 if (PyObject_SetAttrString(result, "value", value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 value = ast2obj_list(o->v.DictComp.generators,
2696 ast2obj_comprehension);
2697 if (!value) goto failed;
2698 if (PyObject_SetAttrString(result, "generators", value) == -1)
2699 goto failed;
2700 Py_DECREF(value);
2701 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002703 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2704 if (!result) goto failed;
2705 value = ast2obj_expr(o->v.GeneratorExp.elt);
2706 if (!value) goto failed;
2707 if (PyObject_SetAttrString(result, "elt", value) == -1)
2708 goto failed;
2709 Py_DECREF(value);
2710 value = ast2obj_list(o->v.GeneratorExp.generators,
2711 ast2obj_comprehension);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "generators", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 break;
2717 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002718 result = PyType_GenericNew(Yield_type, NULL, NULL);
2719 if (!result) goto failed;
2720 value = ast2obj_expr(o->v.Yield.value);
2721 if (!value) goto failed;
2722 if (PyObject_SetAttrString(result, "value", value) == -1)
2723 goto failed;
2724 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 break;
2726 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002727 result = PyType_GenericNew(Compare_type, NULL, NULL);
2728 if (!result) goto failed;
2729 value = ast2obj_expr(o->v.Compare.left);
2730 if (!value) goto failed;
2731 if (PyObject_SetAttrString(result, "left", value) == -1)
2732 goto failed;
2733 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002734 {
2735 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2736 value = PyList_New(n);
2737 if (!value) goto failed;
2738 for(i = 0; i < n; i++)
2739 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2740 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002741 if (!value) goto failed;
2742 if (PyObject_SetAttrString(result, "ops", value) == -1)
2743 goto failed;
2744 Py_DECREF(value);
2745 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750 break;
2751 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002752 result = PyType_GenericNew(Call_type, NULL, NULL);
2753 if (!result) goto failed;
2754 value = ast2obj_expr(o->v.Call.func);
2755 if (!value) goto failed;
2756 if (PyObject_SetAttrString(result, "func", value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
2759 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2760 if (!value) goto failed;
2761 if (PyObject_SetAttrString(result, "args", value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2765 if (!value) goto failed;
2766 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2767 goto failed;
2768 Py_DECREF(value);
2769 value = ast2obj_expr(o->v.Call.starargs);
2770 if (!value) goto failed;
2771 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
2774 value = ast2obj_expr(o->v.Call.kwargs);
2775 if (!value) goto failed;
2776 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002781 result = PyType_GenericNew(Num_type, NULL, NULL);
2782 if (!result) goto failed;
2783 value = ast2obj_object(o->v.Num.n);
2784 if (!value) goto failed;
2785 if (PyObject_SetAttrString(result, "n", value) == -1)
2786 goto failed;
2787 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 break;
2789 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002790 result = PyType_GenericNew(Str_type, NULL, NULL);
2791 if (!result) goto failed;
2792 value = ast2obj_string(o->v.Str.s);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "s", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002798 case Bytes_kind:
2799 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2800 if (!result) goto failed;
2801 value = ast2obj_string(o->v.Bytes.s);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "s", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
2806 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002807 case Ellipsis_kind:
2808 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2809 if (!result) goto failed;
2810 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002812 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2813 if (!result) goto failed;
2814 value = ast2obj_expr(o->v.Attribute.value);
2815 if (!value) goto failed;
2816 if (PyObject_SetAttrString(result, "value", value) == -1)
2817 goto failed;
2818 Py_DECREF(value);
2819 value = ast2obj_identifier(o->v.Attribute.attr);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "attr", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_expr_context(o->v.Attribute.ctx);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 break;
2830 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002831 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2832 if (!result) goto failed;
2833 value = ast2obj_expr(o->v.Subscript.value);
2834 if (!value) goto failed;
2835 if (PyObject_SetAttrString(result, "value", value) == -1)
2836 goto failed;
2837 Py_DECREF(value);
2838 value = ast2obj_slice(o->v.Subscript.slice);
2839 if (!value) goto failed;
2840 if (PyObject_SetAttrString(result, "slice", value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
2843 value = ast2obj_expr_context(o->v.Subscript.ctx);
2844 if (!value) goto failed;
2845 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002849 case Starred_kind:
2850 result = PyType_GenericNew(Starred_type, NULL, NULL);
2851 if (!result) goto failed;
2852 value = ast2obj_expr(o->v.Starred.value);
2853 if (!value) goto failed;
2854 if (PyObject_SetAttrString(result, "value", value) == -1)
2855 goto failed;
2856 Py_DECREF(value);
2857 value = ast2obj_expr_context(o->v.Starred.ctx);
2858 if (!value) goto failed;
2859 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002864 result = PyType_GenericNew(Name_type, NULL, NULL);
2865 if (!result) goto failed;
2866 value = ast2obj_identifier(o->v.Name.id);
2867 if (!value) goto failed;
2868 if (PyObject_SetAttrString(result, "id", value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 value = ast2obj_expr_context(o->v.Name.ctx);
2872 if (!value) goto failed;
2873 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 break;
2877 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002878 result = PyType_GenericNew(List_type, NULL, NULL);
2879 if (!result) goto failed;
2880 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2881 if (!value) goto failed;
2882 if (PyObject_SetAttrString(result, "elts", value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 value = ast2obj_expr_context(o->v.List.ctx);
2886 if (!value) goto failed;
2887 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 break;
2891 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002892 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2893 if (!result) goto failed;
2894 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2895 if (!value) goto failed;
2896 if (PyObject_SetAttrString(result, "elts", value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
2899 value = ast2obj_expr_context(o->v.Tuple.ctx);
2900 if (!value) goto failed;
2901 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2902 goto failed;
2903 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 break;
2905 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002906 value = ast2obj_int(o->lineno);
2907 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002908 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2909 goto failed;
2910 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002911 value = ast2obj_int(o->col_offset);
2912 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002913 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2914 goto failed;
2915 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002916 return result;
2917failed:
2918 Py_XDECREF(value);
2919 Py_XDECREF(result);
2920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921}
2922
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002923PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002925 switch(o) {
2926 case Load:
2927 Py_INCREF(Load_singleton);
2928 return Load_singleton;
2929 case Store:
2930 Py_INCREF(Store_singleton);
2931 return Store_singleton;
2932 case Del:
2933 Py_INCREF(Del_singleton);
2934 return Del_singleton;
2935 case AugLoad:
2936 Py_INCREF(AugLoad_singleton);
2937 return AugLoad_singleton;
2938 case AugStore:
2939 Py_INCREF(AugStore_singleton);
2940 return AugStore_singleton;
2941 case Param:
2942 Py_INCREF(Param_singleton);
2943 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002944 default:
2945 /* should never happen, but just in case ... */
2946 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002950PyObject*
2951ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002953 slice_ty o = (slice_ty)_o;
2954 PyObject *result = NULL, *value = NULL;
2955 if (!o) {
2956 Py_INCREF(Py_None);
2957 return Py_None;
2958 }
2959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002962 result = PyType_GenericNew(Slice_type, NULL, NULL);
2963 if (!result) goto failed;
2964 value = ast2obj_expr(o->v.Slice.lower);
2965 if (!value) goto failed;
2966 if (PyObject_SetAttrString(result, "lower", value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 value = ast2obj_expr(o->v.Slice.upper);
2970 if (!value) goto failed;
2971 if (PyObject_SetAttrString(result, "upper", value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
2974 value = ast2obj_expr(o->v.Slice.step);
2975 if (!value) goto failed;
2976 if (PyObject_SetAttrString(result, "step", value) == -1)
2977 goto failed;
2978 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 break;
2980 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002981 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2982 if (!result) goto failed;
2983 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2984 if (!value) goto failed;
2985 if (PyObject_SetAttrString(result, "dims", value) == -1)
2986 goto failed;
2987 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 break;
2989 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002990 result = PyType_GenericNew(Index_type, NULL, NULL);
2991 if (!result) goto failed;
2992 value = ast2obj_expr(o->v.Index.value);
2993 if (!value) goto failed;
2994 if (PyObject_SetAttrString(result, "value", value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 break;
2998 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002999 return result;
3000failed:
3001 Py_XDECREF(value);
3002 Py_XDECREF(result);
3003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004}
3005
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003006PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003008 switch(o) {
3009 case And:
3010 Py_INCREF(And_singleton);
3011 return And_singleton;
3012 case Or:
3013 Py_INCREF(Or_singleton);
3014 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003015 default:
3016 /* should never happen, but just in case ... */
3017 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003020}
3021PyObject* ast2obj_operator(operator_ty o)
3022{
3023 switch(o) {
3024 case Add:
3025 Py_INCREF(Add_singleton);
3026 return Add_singleton;
3027 case Sub:
3028 Py_INCREF(Sub_singleton);
3029 return Sub_singleton;
3030 case Mult:
3031 Py_INCREF(Mult_singleton);
3032 return Mult_singleton;
3033 case Div:
3034 Py_INCREF(Div_singleton);
3035 return Div_singleton;
3036 case Mod:
3037 Py_INCREF(Mod_singleton);
3038 return Mod_singleton;
3039 case Pow:
3040 Py_INCREF(Pow_singleton);
3041 return Pow_singleton;
3042 case LShift:
3043 Py_INCREF(LShift_singleton);
3044 return LShift_singleton;
3045 case RShift:
3046 Py_INCREF(RShift_singleton);
3047 return RShift_singleton;
3048 case BitOr:
3049 Py_INCREF(BitOr_singleton);
3050 return BitOr_singleton;
3051 case BitXor:
3052 Py_INCREF(BitXor_singleton);
3053 return BitXor_singleton;
3054 case BitAnd:
3055 Py_INCREF(BitAnd_singleton);
3056 return BitAnd_singleton;
3057 case FloorDiv:
3058 Py_INCREF(FloorDiv_singleton);
3059 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003060 default:
3061 /* should never happen, but just in case ... */
3062 PyErr_Format(PyExc_SystemError, "unknown operator found");
3063 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003064 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003065}
3066PyObject* ast2obj_unaryop(unaryop_ty o)
3067{
3068 switch(o) {
3069 case Invert:
3070 Py_INCREF(Invert_singleton);
3071 return Invert_singleton;
3072 case Not:
3073 Py_INCREF(Not_singleton);
3074 return Not_singleton;
3075 case UAdd:
3076 Py_INCREF(UAdd_singleton);
3077 return UAdd_singleton;
3078 case USub:
3079 Py_INCREF(USub_singleton);
3080 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003081 default:
3082 /* should never happen, but just in case ... */
3083 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3084 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003085 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003086}
3087PyObject* ast2obj_cmpop(cmpop_ty o)
3088{
3089 switch(o) {
3090 case Eq:
3091 Py_INCREF(Eq_singleton);
3092 return Eq_singleton;
3093 case NotEq:
3094 Py_INCREF(NotEq_singleton);
3095 return NotEq_singleton;
3096 case Lt:
3097 Py_INCREF(Lt_singleton);
3098 return Lt_singleton;
3099 case LtE:
3100 Py_INCREF(LtE_singleton);
3101 return LtE_singleton;
3102 case Gt:
3103 Py_INCREF(Gt_singleton);
3104 return Gt_singleton;
3105 case GtE:
3106 Py_INCREF(GtE_singleton);
3107 return GtE_singleton;
3108 case Is:
3109 Py_INCREF(Is_singleton);
3110 return Is_singleton;
3111 case IsNot:
3112 Py_INCREF(IsNot_singleton);
3113 return IsNot_singleton;
3114 case In:
3115 Py_INCREF(In_singleton);
3116 return In_singleton;
3117 case NotIn:
3118 Py_INCREF(NotIn_singleton);
3119 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003120 default:
3121 /* should never happen, but just in case ... */
3122 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3123 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003124 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003125}
3126PyObject*
3127ast2obj_comprehension(void* _o)
3128{
3129 comprehension_ty o = (comprehension_ty)_o;
3130 PyObject *result = NULL, *value = NULL;
3131 if (!o) {
3132 Py_INCREF(Py_None);
3133 return Py_None;
3134 }
3135
3136 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3137 if (!result) return NULL;
3138 value = ast2obj_expr(o->target);
3139 if (!value) goto failed;
3140 if (PyObject_SetAttrString(result, "target", value) == -1)
3141 goto failed;
3142 Py_DECREF(value);
3143 value = ast2obj_expr(o->iter);
3144 if (!value) goto failed;
3145 if (PyObject_SetAttrString(result, "iter", value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
3148 value = ast2obj_list(o->ifs, ast2obj_expr);
3149 if (!value) goto failed;
3150 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3151 goto failed;
3152 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003153 return result;
3154failed:
3155 Py_XDECREF(value);
3156 Py_XDECREF(result);
3157 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158}
3159
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003160PyObject*
3161ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003163 excepthandler_ty o = (excepthandler_ty)_o;
3164 PyObject *result = NULL, *value = NULL;
3165 if (!o) {
3166 Py_INCREF(Py_None);
3167 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003169
Neal Norwitzad74aa82008-03-31 05:14:30 +00003170 switch (o->kind) {
3171 case ExceptHandler_kind:
3172 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3173 if (!result) goto failed;
3174 value = ast2obj_expr(o->v.ExceptHandler.type);
3175 if (!value) goto failed;
3176 if (PyObject_SetAttrString(result, "type", value) == -1)
3177 goto failed;
3178 Py_DECREF(value);
3179 value = ast2obj_identifier(o->v.ExceptHandler.name);
3180 if (!value) goto failed;
3181 if (PyObject_SetAttrString(result, "name", value) == -1)
3182 goto failed;
3183 Py_DECREF(value);
3184 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3185 if (!value) goto failed;
3186 if (PyObject_SetAttrString(result, "body", value) == -1)
3187 goto failed;
3188 Py_DECREF(value);
3189 break;
3190 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003191 value = ast2obj_int(o->lineno);
3192 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003193 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003194 goto failed;
3195 Py_DECREF(value);
3196 value = ast2obj_int(o->col_offset);
3197 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003198 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003199 goto failed;
3200 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003201 return result;
3202failed:
3203 Py_XDECREF(value);
3204 Py_XDECREF(result);
3205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206}
3207
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003208PyObject*
3209ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003211 arguments_ty o = (arguments_ty)_o;
3212 PyObject *result = NULL, *value = NULL;
3213 if (!o) {
3214 Py_INCREF(Py_None);
3215 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003217
3218 result = PyType_GenericNew(arguments_type, NULL, NULL);
3219 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003220 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003221 if (!value) goto failed;
3222 if (PyObject_SetAttrString(result, "args", value) == -1)
3223 goto failed;
3224 Py_DECREF(value);
3225 value = ast2obj_identifier(o->vararg);
3226 if (!value) goto failed;
3227 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3228 goto failed;
3229 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003230 value = ast2obj_expr(o->varargannotation);
3231 if (!value) goto failed;
3232 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003236 if (!value) goto failed;
3237 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003240 value = ast2obj_identifier(o->kwarg);
3241 if (!value) goto failed;
3242 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3243 goto failed;
3244 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003245 value = ast2obj_expr(o->kwargannotation);
3246 if (!value) goto failed;
3247 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3248 goto failed;
3249 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003250 value = ast2obj_list(o->defaults, ast2obj_expr);
3251 if (!value) goto failed;
3252 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3253 goto failed;
3254 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003255 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3256 if (!value) goto failed;
3257 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3258 goto failed;
3259 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003260 return result;
3261failed:
3262 Py_XDECREF(value);
3263 Py_XDECREF(result);
3264 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265}
3266
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003267PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003268ast2obj_arg(void* _o)
3269{
3270 arg_ty o = (arg_ty)_o;
3271 PyObject *result = NULL, *value = NULL;
3272 if (!o) {
3273 Py_INCREF(Py_None);
3274 return Py_None;
3275 }
3276
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003277 result = PyType_GenericNew(arg_type, NULL, NULL);
3278 if (!result) return NULL;
3279 value = ast2obj_identifier(o->arg);
3280 if (!value) goto failed;
3281 if (PyObject_SetAttrString(result, "arg", value) == -1)
3282 goto failed;
3283 Py_DECREF(value);
3284 value = ast2obj_expr(o->annotation);
3285 if (!value) goto failed;
3286 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3287 goto failed;
3288 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003289 return result;
3290failed:
3291 Py_XDECREF(value);
3292 Py_XDECREF(result);
3293 return NULL;
3294}
3295
3296PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003297ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003299 keyword_ty o = (keyword_ty)_o;
3300 PyObject *result = NULL, *value = NULL;
3301 if (!o) {
3302 Py_INCREF(Py_None);
3303 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003305
3306 result = PyType_GenericNew(keyword_type, NULL, NULL);
3307 if (!result) return NULL;
3308 value = ast2obj_identifier(o->arg);
3309 if (!value) goto failed;
3310 if (PyObject_SetAttrString(result, "arg", value) == -1)
3311 goto failed;
3312 Py_DECREF(value);
3313 value = ast2obj_expr(o->value);
3314 if (!value) goto failed;
3315 if (PyObject_SetAttrString(result, "value", value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003318 return result;
3319failed:
3320 Py_XDECREF(value);
3321 Py_XDECREF(result);
3322 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323}
3324
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003325PyObject*
3326ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003328 alias_ty o = (alias_ty)_o;
3329 PyObject *result = NULL, *value = NULL;
3330 if (!o) {
3331 Py_INCREF(Py_None);
3332 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003334
3335 result = PyType_GenericNew(alias_type, NULL, NULL);
3336 if (!result) return NULL;
3337 value = ast2obj_identifier(o->name);
3338 if (!value) goto failed;
3339 if (PyObject_SetAttrString(result, "name", value) == -1)
3340 goto failed;
3341 Py_DECREF(value);
3342 value = ast2obj_identifier(o->asname);
3343 if (!value) goto failed;
3344 if (PyObject_SetAttrString(result, "asname", value) == -1)
3345 goto failed;
3346 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003347 return result;
3348failed:
3349 Py_XDECREF(value);
3350 Py_XDECREF(result);
3351 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352}
3353
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003354PyObject*
3355ast2obj_withitem(void* _o)
3356{
3357 withitem_ty o = (withitem_ty)_o;
3358 PyObject *result = NULL, *value = NULL;
3359 if (!o) {
3360 Py_INCREF(Py_None);
3361 return Py_None;
3362 }
3363
3364 result = PyType_GenericNew(withitem_type, NULL, NULL);
3365 if (!result) return NULL;
3366 value = ast2obj_expr(o->context_expr);
3367 if (!value) goto failed;
3368 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3369 goto failed;
3370 Py_DECREF(value);
3371 value = ast2obj_expr(o->optional_vars);
3372 if (!value) goto failed;
3373 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3374 goto failed;
3375 Py_DECREF(value);
3376 return result;
3377failed:
3378 Py_XDECREF(value);
3379 Py_XDECREF(result);
3380 return NULL;
3381}
3382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003384int
3385obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3386{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003387 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003388
Benjamin Petersond8f65972010-11-20 04:31:07 +00003389 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003390
3391 if (obj == Py_None) {
3392 *out = NULL;
3393 return 0;
3394 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003395 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3396 if (isinstance == -1) {
3397 return 1;
3398 }
3399 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003400 asdl_seq* body;
3401
3402 if (PyObject_HasAttrString(obj, "body")) {
3403 int res;
3404 Py_ssize_t len;
3405 Py_ssize_t i;
3406 tmp = PyObject_GetAttrString(obj, "body");
3407 if (tmp == NULL) goto failed;
3408 if (!PyList_Check(tmp)) {
3409 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3410 goto failed;
3411 }
3412 len = PyList_GET_SIZE(tmp);
3413 body = asdl_seq_new(len, arena);
3414 if (body == NULL) goto failed;
3415 for (i = 0; i < len; i++) {
3416 stmt_ty value;
3417 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3418 if (res != 0) goto failed;
3419 asdl_seq_SET(body, i, value);
3420 }
3421 Py_XDECREF(tmp);
3422 tmp = NULL;
3423 } else {
3424 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3425 return 1;
3426 }
3427 *out = Module(body, arena);
3428 if (*out == NULL) goto failed;
3429 return 0;
3430 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003431 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3432 if (isinstance == -1) {
3433 return 1;
3434 }
3435 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003436 asdl_seq* body;
3437
3438 if (PyObject_HasAttrString(obj, "body")) {
3439 int res;
3440 Py_ssize_t len;
3441 Py_ssize_t i;
3442 tmp = PyObject_GetAttrString(obj, "body");
3443 if (tmp == NULL) goto failed;
3444 if (!PyList_Check(tmp)) {
3445 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3446 goto failed;
3447 }
3448 len = PyList_GET_SIZE(tmp);
3449 body = asdl_seq_new(len, arena);
3450 if (body == NULL) goto failed;
3451 for (i = 0; i < len; i++) {
3452 stmt_ty value;
3453 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3454 if (res != 0) goto failed;
3455 asdl_seq_SET(body, i, value);
3456 }
3457 Py_XDECREF(tmp);
3458 tmp = NULL;
3459 } else {
3460 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3461 return 1;
3462 }
3463 *out = Interactive(body, arena);
3464 if (*out == NULL) goto failed;
3465 return 0;
3466 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003467 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3468 if (isinstance == -1) {
3469 return 1;
3470 }
3471 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003472 expr_ty body;
3473
3474 if (PyObject_HasAttrString(obj, "body")) {
3475 int res;
3476 tmp = PyObject_GetAttrString(obj, "body");
3477 if (tmp == NULL) goto failed;
3478 res = obj2ast_expr(tmp, &body, arena);
3479 if (res != 0) goto failed;
3480 Py_XDECREF(tmp);
3481 tmp = NULL;
3482 } else {
3483 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3484 return 1;
3485 }
3486 *out = Expression(body, arena);
3487 if (*out == NULL) goto failed;
3488 return 0;
3489 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003490 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3491 if (isinstance == -1) {
3492 return 1;
3493 }
3494 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003495 asdl_seq* body;
3496
3497 if (PyObject_HasAttrString(obj, "body")) {
3498 int res;
3499 Py_ssize_t len;
3500 Py_ssize_t i;
3501 tmp = PyObject_GetAttrString(obj, "body");
3502 if (tmp == NULL) goto failed;
3503 if (!PyList_Check(tmp)) {
3504 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3505 goto failed;
3506 }
3507 len = PyList_GET_SIZE(tmp);
3508 body = asdl_seq_new(len, arena);
3509 if (body == NULL) goto failed;
3510 for (i = 0; i < len; i++) {
3511 stmt_ty value;
3512 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3513 if (res != 0) goto failed;
3514 asdl_seq_SET(body, i, value);
3515 }
3516 Py_XDECREF(tmp);
3517 tmp = NULL;
3518 } else {
3519 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3520 return 1;
3521 }
3522 *out = Suite(body, arena);
3523 if (*out == NULL) goto failed;
3524 return 0;
3525 }
3526
Benjamin Peterson5b066812010-11-20 01:38:49 +00003527 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3528 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003529 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003530 return 1;
3531}
3532
3533int
3534obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3535{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003536 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003537
Benjamin Petersond8f65972010-11-20 04:31:07 +00003538 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003539 int lineno;
3540 int col_offset;
3541
3542 if (obj == Py_None) {
3543 *out = NULL;
3544 return 0;
3545 }
3546 if (PyObject_HasAttrString(obj, "lineno")) {
3547 int res;
3548 tmp = PyObject_GetAttrString(obj, "lineno");
3549 if (tmp == NULL) goto failed;
3550 res = obj2ast_int(tmp, &lineno, arena);
3551 if (res != 0) goto failed;
3552 Py_XDECREF(tmp);
3553 tmp = NULL;
3554 } else {
3555 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3556 return 1;
3557 }
3558 if (PyObject_HasAttrString(obj, "col_offset")) {
3559 int res;
3560 tmp = PyObject_GetAttrString(obj, "col_offset");
3561 if (tmp == NULL) goto failed;
3562 res = obj2ast_int(tmp, &col_offset, arena);
3563 if (res != 0) goto failed;
3564 Py_XDECREF(tmp);
3565 tmp = NULL;
3566 } else {
3567 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3568 return 1;
3569 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003570 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3571 if (isinstance == -1) {
3572 return 1;
3573 }
3574 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003575 identifier name;
3576 arguments_ty args;
3577 asdl_seq* body;
3578 asdl_seq* decorator_list;
3579 expr_ty returns;
3580
3581 if (PyObject_HasAttrString(obj, "name")) {
3582 int res;
3583 tmp = PyObject_GetAttrString(obj, "name");
3584 if (tmp == NULL) goto failed;
3585 res = obj2ast_identifier(tmp, &name, arena);
3586 if (res != 0) goto failed;
3587 Py_XDECREF(tmp);
3588 tmp = NULL;
3589 } else {
3590 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3591 return 1;
3592 }
3593 if (PyObject_HasAttrString(obj, "args")) {
3594 int res;
3595 tmp = PyObject_GetAttrString(obj, "args");
3596 if (tmp == NULL) goto failed;
3597 res = obj2ast_arguments(tmp, &args, arena);
3598 if (res != 0) goto failed;
3599 Py_XDECREF(tmp);
3600 tmp = NULL;
3601 } else {
3602 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3603 return 1;
3604 }
3605 if (PyObject_HasAttrString(obj, "body")) {
3606 int res;
3607 Py_ssize_t len;
3608 Py_ssize_t i;
3609 tmp = PyObject_GetAttrString(obj, "body");
3610 if (tmp == NULL) goto failed;
3611 if (!PyList_Check(tmp)) {
3612 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3613 goto failed;
3614 }
3615 len = PyList_GET_SIZE(tmp);
3616 body = asdl_seq_new(len, arena);
3617 if (body == NULL) goto failed;
3618 for (i = 0; i < len; i++) {
3619 stmt_ty value;
3620 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3621 if (res != 0) goto failed;
3622 asdl_seq_SET(body, i, value);
3623 }
3624 Py_XDECREF(tmp);
3625 tmp = NULL;
3626 } else {
3627 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3628 return 1;
3629 }
3630 if (PyObject_HasAttrString(obj, "decorator_list")) {
3631 int res;
3632 Py_ssize_t len;
3633 Py_ssize_t i;
3634 tmp = PyObject_GetAttrString(obj, "decorator_list");
3635 if (tmp == NULL) goto failed;
3636 if (!PyList_Check(tmp)) {
3637 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3638 goto failed;
3639 }
3640 len = PyList_GET_SIZE(tmp);
3641 decorator_list = asdl_seq_new(len, arena);
3642 if (decorator_list == NULL) goto failed;
3643 for (i = 0; i < len; i++) {
3644 expr_ty value;
3645 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3646 if (res != 0) goto failed;
3647 asdl_seq_SET(decorator_list, i, value);
3648 }
3649 Py_XDECREF(tmp);
3650 tmp = NULL;
3651 } else {
3652 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3653 return 1;
3654 }
3655 if (PyObject_HasAttrString(obj, "returns")) {
3656 int res;
3657 tmp = PyObject_GetAttrString(obj, "returns");
3658 if (tmp == NULL) goto failed;
3659 res = obj2ast_expr(tmp, &returns, arena);
3660 if (res != 0) goto failed;
3661 Py_XDECREF(tmp);
3662 tmp = NULL;
3663 } else {
3664 returns = NULL;
3665 }
3666 *out = FunctionDef(name, args, body, decorator_list, returns,
3667 lineno, col_offset, arena);
3668 if (*out == NULL) goto failed;
3669 return 0;
3670 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003671 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3672 if (isinstance == -1) {
3673 return 1;
3674 }
3675 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003676 identifier name;
3677 asdl_seq* bases;
3678 asdl_seq* keywords;
3679 expr_ty starargs;
3680 expr_ty kwargs;
3681 asdl_seq* body;
3682 asdl_seq* decorator_list;
3683
3684 if (PyObject_HasAttrString(obj, "name")) {
3685 int res;
3686 tmp = PyObject_GetAttrString(obj, "name");
3687 if (tmp == NULL) goto failed;
3688 res = obj2ast_identifier(tmp, &name, arena);
3689 if (res != 0) goto failed;
3690 Py_XDECREF(tmp);
3691 tmp = NULL;
3692 } else {
3693 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3694 return 1;
3695 }
3696 if (PyObject_HasAttrString(obj, "bases")) {
3697 int res;
3698 Py_ssize_t len;
3699 Py_ssize_t i;
3700 tmp = PyObject_GetAttrString(obj, "bases");
3701 if (tmp == NULL) goto failed;
3702 if (!PyList_Check(tmp)) {
3703 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3704 goto failed;
3705 }
3706 len = PyList_GET_SIZE(tmp);
3707 bases = asdl_seq_new(len, arena);
3708 if (bases == NULL) goto failed;
3709 for (i = 0; i < len; i++) {
3710 expr_ty value;
3711 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3712 if (res != 0) goto failed;
3713 asdl_seq_SET(bases, i, value);
3714 }
3715 Py_XDECREF(tmp);
3716 tmp = NULL;
3717 } else {
3718 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3719 return 1;
3720 }
3721 if (PyObject_HasAttrString(obj, "keywords")) {
3722 int res;
3723 Py_ssize_t len;
3724 Py_ssize_t i;
3725 tmp = PyObject_GetAttrString(obj, "keywords");
3726 if (tmp == NULL) goto failed;
3727 if (!PyList_Check(tmp)) {
3728 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3729 goto failed;
3730 }
3731 len = PyList_GET_SIZE(tmp);
3732 keywords = asdl_seq_new(len, arena);
3733 if (keywords == NULL) goto failed;
3734 for (i = 0; i < len; i++) {
3735 keyword_ty value;
3736 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3737 if (res != 0) goto failed;
3738 asdl_seq_SET(keywords, i, value);
3739 }
3740 Py_XDECREF(tmp);
3741 tmp = NULL;
3742 } else {
3743 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3744 return 1;
3745 }
3746 if (PyObject_HasAttrString(obj, "starargs")) {
3747 int res;
3748 tmp = PyObject_GetAttrString(obj, "starargs");
3749 if (tmp == NULL) goto failed;
3750 res = obj2ast_expr(tmp, &starargs, arena);
3751 if (res != 0) goto failed;
3752 Py_XDECREF(tmp);
3753 tmp = NULL;
3754 } else {
3755 starargs = NULL;
3756 }
3757 if (PyObject_HasAttrString(obj, "kwargs")) {
3758 int res;
3759 tmp = PyObject_GetAttrString(obj, "kwargs");
3760 if (tmp == NULL) goto failed;
3761 res = obj2ast_expr(tmp, &kwargs, arena);
3762 if (res != 0) goto failed;
3763 Py_XDECREF(tmp);
3764 tmp = NULL;
3765 } else {
3766 kwargs = NULL;
3767 }
3768 if (PyObject_HasAttrString(obj, "body")) {
3769 int res;
3770 Py_ssize_t len;
3771 Py_ssize_t i;
3772 tmp = PyObject_GetAttrString(obj, "body");
3773 if (tmp == NULL) goto failed;
3774 if (!PyList_Check(tmp)) {
3775 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3776 goto failed;
3777 }
3778 len = PyList_GET_SIZE(tmp);
3779 body = asdl_seq_new(len, arena);
3780 if (body == NULL) goto failed;
3781 for (i = 0; i < len; i++) {
3782 stmt_ty value;
3783 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3784 if (res != 0) goto failed;
3785 asdl_seq_SET(body, i, value);
3786 }
3787 Py_XDECREF(tmp);
3788 tmp = NULL;
3789 } else {
3790 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3791 return 1;
3792 }
3793 if (PyObject_HasAttrString(obj, "decorator_list")) {
3794 int res;
3795 Py_ssize_t len;
3796 Py_ssize_t i;
3797 tmp = PyObject_GetAttrString(obj, "decorator_list");
3798 if (tmp == NULL) goto failed;
3799 if (!PyList_Check(tmp)) {
3800 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3801 goto failed;
3802 }
3803 len = PyList_GET_SIZE(tmp);
3804 decorator_list = asdl_seq_new(len, arena);
3805 if (decorator_list == NULL) goto failed;
3806 for (i = 0; i < len; i++) {
3807 expr_ty value;
3808 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3809 if (res != 0) goto failed;
3810 asdl_seq_SET(decorator_list, i, value);
3811 }
3812 Py_XDECREF(tmp);
3813 tmp = NULL;
3814 } else {
3815 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3816 return 1;
3817 }
3818 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3819 decorator_list, lineno, col_offset, arena);
3820 if (*out == NULL) goto failed;
3821 return 0;
3822 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003823 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3824 if (isinstance == -1) {
3825 return 1;
3826 }
3827 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003828 expr_ty value;
3829
3830 if (PyObject_HasAttrString(obj, "value")) {
3831 int res;
3832 tmp = PyObject_GetAttrString(obj, "value");
3833 if (tmp == NULL) goto failed;
3834 res = obj2ast_expr(tmp, &value, arena);
3835 if (res != 0) goto failed;
3836 Py_XDECREF(tmp);
3837 tmp = NULL;
3838 } else {
3839 value = NULL;
3840 }
3841 *out = Return(value, lineno, col_offset, arena);
3842 if (*out == NULL) goto failed;
3843 return 0;
3844 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003845 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3846 if (isinstance == -1) {
3847 return 1;
3848 }
3849 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003850 asdl_seq* targets;
3851
3852 if (PyObject_HasAttrString(obj, "targets")) {
3853 int res;
3854 Py_ssize_t len;
3855 Py_ssize_t i;
3856 tmp = PyObject_GetAttrString(obj, "targets");
3857 if (tmp == NULL) goto failed;
3858 if (!PyList_Check(tmp)) {
3859 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3860 goto failed;
3861 }
3862 len = PyList_GET_SIZE(tmp);
3863 targets = asdl_seq_new(len, arena);
3864 if (targets == NULL) goto failed;
3865 for (i = 0; i < len; i++) {
3866 expr_ty value;
3867 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3868 if (res != 0) goto failed;
3869 asdl_seq_SET(targets, i, value);
3870 }
3871 Py_XDECREF(tmp);
3872 tmp = NULL;
3873 } else {
3874 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3875 return 1;
3876 }
3877 *out = Delete(targets, lineno, col_offset, arena);
3878 if (*out == NULL) goto failed;
3879 return 0;
3880 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003881 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3882 if (isinstance == -1) {
3883 return 1;
3884 }
3885 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003886 asdl_seq* targets;
3887 expr_ty value;
3888
3889 if (PyObject_HasAttrString(obj, "targets")) {
3890 int res;
3891 Py_ssize_t len;
3892 Py_ssize_t i;
3893 tmp = PyObject_GetAttrString(obj, "targets");
3894 if (tmp == NULL) goto failed;
3895 if (!PyList_Check(tmp)) {
3896 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3897 goto failed;
3898 }
3899 len = PyList_GET_SIZE(tmp);
3900 targets = asdl_seq_new(len, arena);
3901 if (targets == NULL) goto failed;
3902 for (i = 0; i < len; i++) {
3903 expr_ty value;
3904 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3905 if (res != 0) goto failed;
3906 asdl_seq_SET(targets, i, value);
3907 }
3908 Py_XDECREF(tmp);
3909 tmp = NULL;
3910 } else {
3911 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3912 return 1;
3913 }
3914 if (PyObject_HasAttrString(obj, "value")) {
3915 int res;
3916 tmp = PyObject_GetAttrString(obj, "value");
3917 if (tmp == NULL) goto failed;
3918 res = obj2ast_expr(tmp, &value, arena);
3919 if (res != 0) goto failed;
3920 Py_XDECREF(tmp);
3921 tmp = NULL;
3922 } else {
3923 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3924 return 1;
3925 }
3926 *out = Assign(targets, value, lineno, col_offset, arena);
3927 if (*out == NULL) goto failed;
3928 return 0;
3929 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003930 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3931 if (isinstance == -1) {
3932 return 1;
3933 }
3934 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003935 expr_ty target;
3936 operator_ty op;
3937 expr_ty value;
3938
3939 if (PyObject_HasAttrString(obj, "target")) {
3940 int res;
3941 tmp = PyObject_GetAttrString(obj, "target");
3942 if (tmp == NULL) goto failed;
3943 res = obj2ast_expr(tmp, &target, arena);
3944 if (res != 0) goto failed;
3945 Py_XDECREF(tmp);
3946 tmp = NULL;
3947 } else {
3948 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3949 return 1;
3950 }
3951 if (PyObject_HasAttrString(obj, "op")) {
3952 int res;
3953 tmp = PyObject_GetAttrString(obj, "op");
3954 if (tmp == NULL) goto failed;
3955 res = obj2ast_operator(tmp, &op, arena);
3956 if (res != 0) goto failed;
3957 Py_XDECREF(tmp);
3958 tmp = NULL;
3959 } else {
3960 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3961 return 1;
3962 }
3963 if (PyObject_HasAttrString(obj, "value")) {
3964 int res;
3965 tmp = PyObject_GetAttrString(obj, "value");
3966 if (tmp == NULL) goto failed;
3967 res = obj2ast_expr(tmp, &value, arena);
3968 if (res != 0) goto failed;
3969 Py_XDECREF(tmp);
3970 tmp = NULL;
3971 } else {
3972 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3973 return 1;
3974 }
3975 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3976 if (*out == NULL) goto failed;
3977 return 0;
3978 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003979 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3980 if (isinstance == -1) {
3981 return 1;
3982 }
3983 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003984 expr_ty target;
3985 expr_ty iter;
3986 asdl_seq* body;
3987 asdl_seq* orelse;
3988
3989 if (PyObject_HasAttrString(obj, "target")) {
3990 int res;
3991 tmp = PyObject_GetAttrString(obj, "target");
3992 if (tmp == NULL) goto failed;
3993 res = obj2ast_expr(tmp, &target, arena);
3994 if (res != 0) goto failed;
3995 Py_XDECREF(tmp);
3996 tmp = NULL;
3997 } else {
3998 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3999 return 1;
4000 }
4001 if (PyObject_HasAttrString(obj, "iter")) {
4002 int res;
4003 tmp = PyObject_GetAttrString(obj, "iter");
4004 if (tmp == NULL) goto failed;
4005 res = obj2ast_expr(tmp, &iter, arena);
4006 if (res != 0) goto failed;
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4011 return 1;
4012 }
4013 if (PyObject_HasAttrString(obj, "body")) {
4014 int res;
4015 Py_ssize_t len;
4016 Py_ssize_t i;
4017 tmp = PyObject_GetAttrString(obj, "body");
4018 if (tmp == NULL) goto failed;
4019 if (!PyList_Check(tmp)) {
4020 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4021 goto failed;
4022 }
4023 len = PyList_GET_SIZE(tmp);
4024 body = asdl_seq_new(len, arena);
4025 if (body == NULL) goto failed;
4026 for (i = 0; i < len; i++) {
4027 stmt_ty value;
4028 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4029 if (res != 0) goto failed;
4030 asdl_seq_SET(body, i, value);
4031 }
4032 Py_XDECREF(tmp);
4033 tmp = NULL;
4034 } else {
4035 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4036 return 1;
4037 }
4038 if (PyObject_HasAttrString(obj, "orelse")) {
4039 int res;
4040 Py_ssize_t len;
4041 Py_ssize_t i;
4042 tmp = PyObject_GetAttrString(obj, "orelse");
4043 if (tmp == NULL) goto failed;
4044 if (!PyList_Check(tmp)) {
4045 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4046 goto failed;
4047 }
4048 len = PyList_GET_SIZE(tmp);
4049 orelse = asdl_seq_new(len, arena);
4050 if (orelse == NULL) goto failed;
4051 for (i = 0; i < len; i++) {
4052 stmt_ty value;
4053 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4054 if (res != 0) goto failed;
4055 asdl_seq_SET(orelse, i, value);
4056 }
4057 Py_XDECREF(tmp);
4058 tmp = NULL;
4059 } else {
4060 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4061 return 1;
4062 }
4063 *out = For(target, iter, body, orelse, lineno, col_offset,
4064 arena);
4065 if (*out == NULL) goto failed;
4066 return 0;
4067 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004068 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4069 if (isinstance == -1) {
4070 return 1;
4071 }
4072 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004073 expr_ty test;
4074 asdl_seq* body;
4075 asdl_seq* orelse;
4076
4077 if (PyObject_HasAttrString(obj, "test")) {
4078 int res;
4079 tmp = PyObject_GetAttrString(obj, "test");
4080 if (tmp == NULL) goto failed;
4081 res = obj2ast_expr(tmp, &test, arena);
4082 if (res != 0) goto failed;
4083 Py_XDECREF(tmp);
4084 tmp = NULL;
4085 } else {
4086 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4087 return 1;
4088 }
4089 if (PyObject_HasAttrString(obj, "body")) {
4090 int res;
4091 Py_ssize_t len;
4092 Py_ssize_t i;
4093 tmp = PyObject_GetAttrString(obj, "body");
4094 if (tmp == NULL) goto failed;
4095 if (!PyList_Check(tmp)) {
4096 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4097 goto failed;
4098 }
4099 len = PyList_GET_SIZE(tmp);
4100 body = asdl_seq_new(len, arena);
4101 if (body == NULL) goto failed;
4102 for (i = 0; i < len; i++) {
4103 stmt_ty value;
4104 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4105 if (res != 0) goto failed;
4106 asdl_seq_SET(body, i, value);
4107 }
4108 Py_XDECREF(tmp);
4109 tmp = NULL;
4110 } else {
4111 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4112 return 1;
4113 }
4114 if (PyObject_HasAttrString(obj, "orelse")) {
4115 int res;
4116 Py_ssize_t len;
4117 Py_ssize_t i;
4118 tmp = PyObject_GetAttrString(obj, "orelse");
4119 if (tmp == NULL) goto failed;
4120 if (!PyList_Check(tmp)) {
4121 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4122 goto failed;
4123 }
4124 len = PyList_GET_SIZE(tmp);
4125 orelse = asdl_seq_new(len, arena);
4126 if (orelse == NULL) goto failed;
4127 for (i = 0; i < len; i++) {
4128 stmt_ty value;
4129 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4130 if (res != 0) goto failed;
4131 asdl_seq_SET(orelse, i, value);
4132 }
4133 Py_XDECREF(tmp);
4134 tmp = NULL;
4135 } else {
4136 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4137 return 1;
4138 }
4139 *out = While(test, body, orelse, lineno, col_offset, arena);
4140 if (*out == NULL) goto failed;
4141 return 0;
4142 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004143 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4144 if (isinstance == -1) {
4145 return 1;
4146 }
4147 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004148 expr_ty test;
4149 asdl_seq* body;
4150 asdl_seq* orelse;
4151
4152 if (PyObject_HasAttrString(obj, "test")) {
4153 int res;
4154 tmp = PyObject_GetAttrString(obj, "test");
4155 if (tmp == NULL) goto failed;
4156 res = obj2ast_expr(tmp, &test, arena);
4157 if (res != 0) goto failed;
4158 Py_XDECREF(tmp);
4159 tmp = NULL;
4160 } else {
4161 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4162 return 1;
4163 }
4164 if (PyObject_HasAttrString(obj, "body")) {
4165 int res;
4166 Py_ssize_t len;
4167 Py_ssize_t i;
4168 tmp = PyObject_GetAttrString(obj, "body");
4169 if (tmp == NULL) goto failed;
4170 if (!PyList_Check(tmp)) {
4171 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4172 goto failed;
4173 }
4174 len = PyList_GET_SIZE(tmp);
4175 body = asdl_seq_new(len, arena);
4176 if (body == NULL) goto failed;
4177 for (i = 0; i < len; i++) {
4178 stmt_ty value;
4179 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4180 if (res != 0) goto failed;
4181 asdl_seq_SET(body, i, value);
4182 }
4183 Py_XDECREF(tmp);
4184 tmp = NULL;
4185 } else {
4186 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4187 return 1;
4188 }
4189 if (PyObject_HasAttrString(obj, "orelse")) {
4190 int res;
4191 Py_ssize_t len;
4192 Py_ssize_t i;
4193 tmp = PyObject_GetAttrString(obj, "orelse");
4194 if (tmp == NULL) goto failed;
4195 if (!PyList_Check(tmp)) {
4196 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4197 goto failed;
4198 }
4199 len = PyList_GET_SIZE(tmp);
4200 orelse = asdl_seq_new(len, arena);
4201 if (orelse == NULL) goto failed;
4202 for (i = 0; i < len; i++) {
4203 stmt_ty value;
4204 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4205 if (res != 0) goto failed;
4206 asdl_seq_SET(orelse, i, value);
4207 }
4208 Py_XDECREF(tmp);
4209 tmp = NULL;
4210 } else {
4211 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4212 return 1;
4213 }
4214 *out = If(test, body, orelse, lineno, col_offset, arena);
4215 if (*out == NULL) goto failed;
4216 return 0;
4217 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004218 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4219 if (isinstance == -1) {
4220 return 1;
4221 }
4222 if (isinstance) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004223 asdl_seq* items;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004224 asdl_seq* body;
4225
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004226 if (PyObject_HasAttrString(obj, "items")) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004227 int res;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004228 Py_ssize_t len;
4229 Py_ssize_t i;
4230 tmp = PyObject_GetAttrString(obj, "items");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004231 if (tmp == NULL) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004232 if (!PyList_Check(tmp)) {
4233 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4234 goto failed;
4235 }
4236 len = PyList_GET_SIZE(tmp);
4237 items = asdl_seq_new(len, arena);
4238 if (items == NULL) goto failed;
4239 for (i = 0; i < len; i++) {
4240 withitem_ty value;
4241 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4242 if (res != 0) goto failed;
4243 asdl_seq_SET(items, i, value);
4244 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004245 Py_XDECREF(tmp);
4246 tmp = NULL;
4247 } else {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004248 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004249 return 1;
4250 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004251 if (PyObject_HasAttrString(obj, "body")) {
4252 int res;
4253 Py_ssize_t len;
4254 Py_ssize_t i;
4255 tmp = PyObject_GetAttrString(obj, "body");
4256 if (tmp == NULL) goto failed;
4257 if (!PyList_Check(tmp)) {
4258 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4259 goto failed;
4260 }
4261 len = PyList_GET_SIZE(tmp);
4262 body = asdl_seq_new(len, arena);
4263 if (body == NULL) goto failed;
4264 for (i = 0; i < len; i++) {
4265 stmt_ty value;
4266 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4267 if (res != 0) goto failed;
4268 asdl_seq_SET(body, i, value);
4269 }
4270 Py_XDECREF(tmp);
4271 tmp = NULL;
4272 } else {
4273 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4274 return 1;
4275 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004276 *out = With(items, body, lineno, col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004277 if (*out == NULL) goto failed;
4278 return 0;
4279 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004280 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4281 if (isinstance == -1) {
4282 return 1;
4283 }
4284 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004285 expr_ty exc;
4286 expr_ty cause;
4287
4288 if (PyObject_HasAttrString(obj, "exc")) {
4289 int res;
4290 tmp = PyObject_GetAttrString(obj, "exc");
4291 if (tmp == NULL) goto failed;
4292 res = obj2ast_expr(tmp, &exc, arena);
4293 if (res != 0) goto failed;
4294 Py_XDECREF(tmp);
4295 tmp = NULL;
4296 } else {
4297 exc = NULL;
4298 }
4299 if (PyObject_HasAttrString(obj, "cause")) {
4300 int res;
4301 tmp = PyObject_GetAttrString(obj, "cause");
4302 if (tmp == NULL) goto failed;
4303 res = obj2ast_expr(tmp, &cause, arena);
4304 if (res != 0) goto failed;
4305 Py_XDECREF(tmp);
4306 tmp = NULL;
4307 } else {
4308 cause = NULL;
4309 }
4310 *out = Raise(exc, cause, lineno, col_offset, arena);
4311 if (*out == NULL) goto failed;
4312 return 0;
4313 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004314 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004315 if (isinstance == -1) {
4316 return 1;
4317 }
4318 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004319 asdl_seq* body;
4320 asdl_seq* handlers;
4321 asdl_seq* orelse;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004322 asdl_seq* finalbody;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004323
4324 if (PyObject_HasAttrString(obj, "body")) {
4325 int res;
4326 Py_ssize_t len;
4327 Py_ssize_t i;
4328 tmp = PyObject_GetAttrString(obj, "body");
4329 if (tmp == NULL) goto failed;
4330 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004331 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004332 goto failed;
4333 }
4334 len = PyList_GET_SIZE(tmp);
4335 body = asdl_seq_new(len, arena);
4336 if (body == NULL) goto failed;
4337 for (i = 0; i < len; i++) {
4338 stmt_ty value;
4339 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4340 if (res != 0) goto failed;
4341 asdl_seq_SET(body, i, value);
4342 }
4343 Py_XDECREF(tmp);
4344 tmp = NULL;
4345 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004346 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004347 return 1;
4348 }
4349 if (PyObject_HasAttrString(obj, "handlers")) {
4350 int res;
4351 Py_ssize_t len;
4352 Py_ssize_t i;
4353 tmp = PyObject_GetAttrString(obj, "handlers");
4354 if (tmp == NULL) goto failed;
4355 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004356 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004357 goto failed;
4358 }
4359 len = PyList_GET_SIZE(tmp);
4360 handlers = asdl_seq_new(len, arena);
4361 if (handlers == NULL) goto failed;
4362 for (i = 0; i < len; i++) {
4363 excepthandler_ty value;
4364 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4365 if (res != 0) goto failed;
4366 asdl_seq_SET(handlers, i, value);
4367 }
4368 Py_XDECREF(tmp);
4369 tmp = NULL;
4370 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004371 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004372 return 1;
4373 }
4374 if (PyObject_HasAttrString(obj, "orelse")) {
4375 int res;
4376 Py_ssize_t len;
4377 Py_ssize_t i;
4378 tmp = PyObject_GetAttrString(obj, "orelse");
4379 if (tmp == NULL) goto failed;
4380 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004381 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004382 goto failed;
4383 }
4384 len = PyList_GET_SIZE(tmp);
4385 orelse = asdl_seq_new(len, arena);
4386 if (orelse == NULL) goto failed;
4387 for (i = 0; i < len; i++) {
4388 stmt_ty value;
4389 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4390 if (res != 0) goto failed;
4391 asdl_seq_SET(orelse, i, value);
4392 }
4393 Py_XDECREF(tmp);
4394 tmp = NULL;
4395 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004396 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004397 return 1;
4398 }
4399 if (PyObject_HasAttrString(obj, "finalbody")) {
4400 int res;
4401 Py_ssize_t len;
4402 Py_ssize_t i;
4403 tmp = PyObject_GetAttrString(obj, "finalbody");
4404 if (tmp == NULL) goto failed;
4405 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004406 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004407 goto failed;
4408 }
4409 len = PyList_GET_SIZE(tmp);
4410 finalbody = asdl_seq_new(len, arena);
4411 if (finalbody == NULL) goto failed;
4412 for (i = 0; i < len; i++) {
4413 stmt_ty value;
4414 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4415 if (res != 0) goto failed;
4416 asdl_seq_SET(finalbody, i, value);
4417 }
4418 Py_XDECREF(tmp);
4419 tmp = NULL;
4420 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004421 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004422 return 1;
4423 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004424 *out = Try(body, handlers, orelse, finalbody, lineno,
4425 col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004426 if (*out == NULL) goto failed;
4427 return 0;
4428 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004429 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4430 if (isinstance == -1) {
4431 return 1;
4432 }
4433 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004434 expr_ty test;
4435 expr_ty msg;
4436
4437 if (PyObject_HasAttrString(obj, "test")) {
4438 int res;
4439 tmp = PyObject_GetAttrString(obj, "test");
4440 if (tmp == NULL) goto failed;
4441 res = obj2ast_expr(tmp, &test, arena);
4442 if (res != 0) goto failed;
4443 Py_XDECREF(tmp);
4444 tmp = NULL;
4445 } else {
4446 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4447 return 1;
4448 }
4449 if (PyObject_HasAttrString(obj, "msg")) {
4450 int res;
4451 tmp = PyObject_GetAttrString(obj, "msg");
4452 if (tmp == NULL) goto failed;
4453 res = obj2ast_expr(tmp, &msg, arena);
4454 if (res != 0) goto failed;
4455 Py_XDECREF(tmp);
4456 tmp = NULL;
4457 } else {
4458 msg = NULL;
4459 }
4460 *out = Assert(test, msg, lineno, col_offset, arena);
4461 if (*out == NULL) goto failed;
4462 return 0;
4463 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004464 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4465 if (isinstance == -1) {
4466 return 1;
4467 }
4468 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004469 asdl_seq* names;
4470
4471 if (PyObject_HasAttrString(obj, "names")) {
4472 int res;
4473 Py_ssize_t len;
4474 Py_ssize_t i;
4475 tmp = PyObject_GetAttrString(obj, "names");
4476 if (tmp == NULL) goto failed;
4477 if (!PyList_Check(tmp)) {
4478 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479 goto failed;
4480 }
4481 len = PyList_GET_SIZE(tmp);
4482 names = asdl_seq_new(len, arena);
4483 if (names == NULL) goto failed;
4484 for (i = 0; i < len; i++) {
4485 alias_ty value;
4486 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4487 if (res != 0) goto failed;
4488 asdl_seq_SET(names, i, value);
4489 }
4490 Py_XDECREF(tmp);
4491 tmp = NULL;
4492 } else {
4493 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4494 return 1;
4495 }
4496 *out = Import(names, lineno, col_offset, arena);
4497 if (*out == NULL) goto failed;
4498 return 0;
4499 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004500 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4501 if (isinstance == -1) {
4502 return 1;
4503 }
4504 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004505 identifier module;
4506 asdl_seq* names;
4507 int level;
4508
4509 if (PyObject_HasAttrString(obj, "module")) {
4510 int res;
4511 tmp = PyObject_GetAttrString(obj, "module");
4512 if (tmp == NULL) goto failed;
4513 res = obj2ast_identifier(tmp, &module, arena);
4514 if (res != 0) goto failed;
4515 Py_XDECREF(tmp);
4516 tmp = NULL;
4517 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004518 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004519 }
4520 if (PyObject_HasAttrString(obj, "names")) {
4521 int res;
4522 Py_ssize_t len;
4523 Py_ssize_t i;
4524 tmp = PyObject_GetAttrString(obj, "names");
4525 if (tmp == NULL) goto failed;
4526 if (!PyList_Check(tmp)) {
4527 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4528 goto failed;
4529 }
4530 len = PyList_GET_SIZE(tmp);
4531 names = asdl_seq_new(len, arena);
4532 if (names == NULL) goto failed;
4533 for (i = 0; i < len; i++) {
4534 alias_ty value;
4535 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4536 if (res != 0) goto failed;
4537 asdl_seq_SET(names, i, value);
4538 }
4539 Py_XDECREF(tmp);
4540 tmp = NULL;
4541 } else {
4542 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4543 return 1;
4544 }
4545 if (PyObject_HasAttrString(obj, "level")) {
4546 int res;
4547 tmp = PyObject_GetAttrString(obj, "level");
4548 if (tmp == NULL) goto failed;
4549 res = obj2ast_int(tmp, &level, arena);
4550 if (res != 0) goto failed;
4551 Py_XDECREF(tmp);
4552 tmp = NULL;
4553 } else {
4554 level = 0;
4555 }
4556 *out = ImportFrom(module, names, level, lineno, col_offset,
4557 arena);
4558 if (*out == NULL) goto failed;
4559 return 0;
4560 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004561 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4562 if (isinstance == -1) {
4563 return 1;
4564 }
4565 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004566 asdl_seq* names;
4567
4568 if (PyObject_HasAttrString(obj, "names")) {
4569 int res;
4570 Py_ssize_t len;
4571 Py_ssize_t i;
4572 tmp = PyObject_GetAttrString(obj, "names");
4573 if (tmp == NULL) goto failed;
4574 if (!PyList_Check(tmp)) {
4575 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4576 goto failed;
4577 }
4578 len = PyList_GET_SIZE(tmp);
4579 names = asdl_seq_new(len, arena);
4580 if (names == NULL) goto failed;
4581 for (i = 0; i < len; i++) {
4582 identifier value;
4583 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4584 if (res != 0) goto failed;
4585 asdl_seq_SET(names, i, value);
4586 }
4587 Py_XDECREF(tmp);
4588 tmp = NULL;
4589 } else {
4590 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4591 return 1;
4592 }
4593 *out = Global(names, lineno, col_offset, arena);
4594 if (*out == NULL) goto failed;
4595 return 0;
4596 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004597 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4598 if (isinstance == -1) {
4599 return 1;
4600 }
4601 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004602 asdl_seq* names;
4603
4604 if (PyObject_HasAttrString(obj, "names")) {
4605 int res;
4606 Py_ssize_t len;
4607 Py_ssize_t i;
4608 tmp = PyObject_GetAttrString(obj, "names");
4609 if (tmp == NULL) goto failed;
4610 if (!PyList_Check(tmp)) {
4611 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4612 goto failed;
4613 }
4614 len = PyList_GET_SIZE(tmp);
4615 names = asdl_seq_new(len, arena);
4616 if (names == NULL) goto failed;
4617 for (i = 0; i < len; i++) {
4618 identifier value;
4619 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4620 if (res != 0) goto failed;
4621 asdl_seq_SET(names, i, value);
4622 }
4623 Py_XDECREF(tmp);
4624 tmp = NULL;
4625 } else {
4626 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4627 return 1;
4628 }
4629 *out = Nonlocal(names, lineno, col_offset, arena);
4630 if (*out == NULL) goto failed;
4631 return 0;
4632 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004633 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4634 if (isinstance == -1) {
4635 return 1;
4636 }
4637 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004638 expr_ty value;
4639
4640 if (PyObject_HasAttrString(obj, "value")) {
4641 int res;
4642 tmp = PyObject_GetAttrString(obj, "value");
4643 if (tmp == NULL) goto failed;
4644 res = obj2ast_expr(tmp, &value, arena);
4645 if (res != 0) goto failed;
4646 Py_XDECREF(tmp);
4647 tmp = NULL;
4648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4650 return 1;
4651 }
4652 *out = Expr(value, lineno, col_offset, arena);
4653 if (*out == NULL) goto failed;
4654 return 0;
4655 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004656 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4657 if (isinstance == -1) {
4658 return 1;
4659 }
4660 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004661
4662 *out = Pass(lineno, col_offset, arena);
4663 if (*out == NULL) goto failed;
4664 return 0;
4665 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004666 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4667 if (isinstance == -1) {
4668 return 1;
4669 }
4670 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004671
4672 *out = Break(lineno, col_offset, arena);
4673 if (*out == NULL) goto failed;
4674 return 0;
4675 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004676 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4677 if (isinstance == -1) {
4678 return 1;
4679 }
4680 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004681
4682 *out = Continue(lineno, col_offset, arena);
4683 if (*out == NULL) goto failed;
4684 return 0;
4685 }
4686
Benjamin Peterson5b066812010-11-20 01:38:49 +00004687 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4688 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004689 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004690 return 1;
4691}
4692
4693int
4694obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4695{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004696 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004697
Benjamin Petersond8f65972010-11-20 04:31:07 +00004698 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004699 int lineno;
4700 int col_offset;
4701
4702 if (obj == Py_None) {
4703 *out = NULL;
4704 return 0;
4705 }
4706 if (PyObject_HasAttrString(obj, "lineno")) {
4707 int res;
4708 tmp = PyObject_GetAttrString(obj, "lineno");
4709 if (tmp == NULL) goto failed;
4710 res = obj2ast_int(tmp, &lineno, arena);
4711 if (res != 0) goto failed;
4712 Py_XDECREF(tmp);
4713 tmp = NULL;
4714 } else {
4715 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4716 return 1;
4717 }
4718 if (PyObject_HasAttrString(obj, "col_offset")) {
4719 int res;
4720 tmp = PyObject_GetAttrString(obj, "col_offset");
4721 if (tmp == NULL) goto failed;
4722 res = obj2ast_int(tmp, &col_offset, arena);
4723 if (res != 0) goto failed;
4724 Py_XDECREF(tmp);
4725 tmp = NULL;
4726 } else {
4727 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4728 return 1;
4729 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004730 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4731 if (isinstance == -1) {
4732 return 1;
4733 }
4734 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004735 boolop_ty op;
4736 asdl_seq* values;
4737
4738 if (PyObject_HasAttrString(obj, "op")) {
4739 int res;
4740 tmp = PyObject_GetAttrString(obj, "op");
4741 if (tmp == NULL) goto failed;
4742 res = obj2ast_boolop(tmp, &op, arena);
4743 if (res != 0) goto failed;
4744 Py_XDECREF(tmp);
4745 tmp = NULL;
4746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4748 return 1;
4749 }
4750 if (PyObject_HasAttrString(obj, "values")) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = PyObject_GetAttrString(obj, "values");
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4758 goto failed;
4759 }
4760 len = PyList_GET_SIZE(tmp);
4761 values = asdl_seq_new(len, arena);
4762 if (values == NULL) goto failed;
4763 for (i = 0; i < len; i++) {
4764 expr_ty value;
4765 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4766 if (res != 0) goto failed;
4767 asdl_seq_SET(values, i, value);
4768 }
4769 Py_XDECREF(tmp);
4770 tmp = NULL;
4771 } else {
4772 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4773 return 1;
4774 }
4775 *out = BoolOp(op, values, lineno, col_offset, arena);
4776 if (*out == NULL) goto failed;
4777 return 0;
4778 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004779 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4780 if (isinstance == -1) {
4781 return 1;
4782 }
4783 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004784 expr_ty left;
4785 operator_ty op;
4786 expr_ty right;
4787
4788 if (PyObject_HasAttrString(obj, "left")) {
4789 int res;
4790 tmp = PyObject_GetAttrString(obj, "left");
4791 if (tmp == NULL) goto failed;
4792 res = obj2ast_expr(tmp, &left, arena);
4793 if (res != 0) goto failed;
4794 Py_XDECREF(tmp);
4795 tmp = NULL;
4796 } else {
4797 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4798 return 1;
4799 }
4800 if (PyObject_HasAttrString(obj, "op")) {
4801 int res;
4802 tmp = PyObject_GetAttrString(obj, "op");
4803 if (tmp == NULL) goto failed;
4804 res = obj2ast_operator(tmp, &op, arena);
4805 if (res != 0) goto failed;
4806 Py_XDECREF(tmp);
4807 tmp = NULL;
4808 } else {
4809 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4810 return 1;
4811 }
4812 if (PyObject_HasAttrString(obj, "right")) {
4813 int res;
4814 tmp = PyObject_GetAttrString(obj, "right");
4815 if (tmp == NULL) goto failed;
4816 res = obj2ast_expr(tmp, &right, arena);
4817 if (res != 0) goto failed;
4818 Py_XDECREF(tmp);
4819 tmp = NULL;
4820 } else {
4821 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4822 return 1;
4823 }
4824 *out = BinOp(left, op, right, lineno, col_offset, arena);
4825 if (*out == NULL) goto failed;
4826 return 0;
4827 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004828 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4829 if (isinstance == -1) {
4830 return 1;
4831 }
4832 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004833 unaryop_ty op;
4834 expr_ty operand;
4835
4836 if (PyObject_HasAttrString(obj, "op")) {
4837 int res;
4838 tmp = PyObject_GetAttrString(obj, "op");
4839 if (tmp == NULL) goto failed;
4840 res = obj2ast_unaryop(tmp, &op, arena);
4841 if (res != 0) goto failed;
4842 Py_XDECREF(tmp);
4843 tmp = NULL;
4844 } else {
4845 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4846 return 1;
4847 }
4848 if (PyObject_HasAttrString(obj, "operand")) {
4849 int res;
4850 tmp = PyObject_GetAttrString(obj, "operand");
4851 if (tmp == NULL) goto failed;
4852 res = obj2ast_expr(tmp, &operand, arena);
4853 if (res != 0) goto failed;
4854 Py_XDECREF(tmp);
4855 tmp = NULL;
4856 } else {
4857 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4858 return 1;
4859 }
4860 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4861 if (*out == NULL) goto failed;
4862 return 0;
4863 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004864 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4865 if (isinstance == -1) {
4866 return 1;
4867 }
4868 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004869 arguments_ty args;
4870 expr_ty body;
4871
4872 if (PyObject_HasAttrString(obj, "args")) {
4873 int res;
4874 tmp = PyObject_GetAttrString(obj, "args");
4875 if (tmp == NULL) goto failed;
4876 res = obj2ast_arguments(tmp, &args, arena);
4877 if (res != 0) goto failed;
4878 Py_XDECREF(tmp);
4879 tmp = NULL;
4880 } else {
4881 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4882 return 1;
4883 }
4884 if (PyObject_HasAttrString(obj, "body")) {
4885 int res;
4886 tmp = PyObject_GetAttrString(obj, "body");
4887 if (tmp == NULL) goto failed;
4888 res = obj2ast_expr(tmp, &body, arena);
4889 if (res != 0) goto failed;
4890 Py_XDECREF(tmp);
4891 tmp = NULL;
4892 } else {
4893 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4894 return 1;
4895 }
4896 *out = Lambda(args, body, lineno, col_offset, arena);
4897 if (*out == NULL) goto failed;
4898 return 0;
4899 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004900 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4901 if (isinstance == -1) {
4902 return 1;
4903 }
4904 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004905 expr_ty test;
4906 expr_ty body;
4907 expr_ty orelse;
4908
4909 if (PyObject_HasAttrString(obj, "test")) {
4910 int res;
4911 tmp = PyObject_GetAttrString(obj, "test");
4912 if (tmp == NULL) goto failed;
4913 res = obj2ast_expr(tmp, &test, arena);
4914 if (res != 0) goto failed;
4915 Py_XDECREF(tmp);
4916 tmp = NULL;
4917 } else {
4918 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4919 return 1;
4920 }
4921 if (PyObject_HasAttrString(obj, "body")) {
4922 int res;
4923 tmp = PyObject_GetAttrString(obj, "body");
4924 if (tmp == NULL) goto failed;
4925 res = obj2ast_expr(tmp, &body, arena);
4926 if (res != 0) goto failed;
4927 Py_XDECREF(tmp);
4928 tmp = NULL;
4929 } else {
4930 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4931 return 1;
4932 }
4933 if (PyObject_HasAttrString(obj, "orelse")) {
4934 int res;
4935 tmp = PyObject_GetAttrString(obj, "orelse");
4936 if (tmp == NULL) goto failed;
4937 res = obj2ast_expr(tmp, &orelse, arena);
4938 if (res != 0) goto failed;
4939 Py_XDECREF(tmp);
4940 tmp = NULL;
4941 } else {
4942 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4943 return 1;
4944 }
4945 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4946 if (*out == NULL) goto failed;
4947 return 0;
4948 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004949 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4950 if (isinstance == -1) {
4951 return 1;
4952 }
4953 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004954 asdl_seq* keys;
4955 asdl_seq* values;
4956
4957 if (PyObject_HasAttrString(obj, "keys")) {
4958 int res;
4959 Py_ssize_t len;
4960 Py_ssize_t i;
4961 tmp = PyObject_GetAttrString(obj, "keys");
4962 if (tmp == NULL) goto failed;
4963 if (!PyList_Check(tmp)) {
4964 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4965 goto failed;
4966 }
4967 len = PyList_GET_SIZE(tmp);
4968 keys = asdl_seq_new(len, arena);
4969 if (keys == NULL) goto failed;
4970 for (i = 0; i < len; i++) {
4971 expr_ty value;
4972 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4973 if (res != 0) goto failed;
4974 asdl_seq_SET(keys, i, value);
4975 }
4976 Py_XDECREF(tmp);
4977 tmp = NULL;
4978 } else {
4979 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4980 return 1;
4981 }
4982 if (PyObject_HasAttrString(obj, "values")) {
4983 int res;
4984 Py_ssize_t len;
4985 Py_ssize_t i;
4986 tmp = PyObject_GetAttrString(obj, "values");
4987 if (tmp == NULL) goto failed;
4988 if (!PyList_Check(tmp)) {
4989 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4990 goto failed;
4991 }
4992 len = PyList_GET_SIZE(tmp);
4993 values = asdl_seq_new(len, arena);
4994 if (values == NULL) goto failed;
4995 for (i = 0; i < len; i++) {
4996 expr_ty value;
4997 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4998 if (res != 0) goto failed;
4999 asdl_seq_SET(values, i, value);
5000 }
5001 Py_XDECREF(tmp);
5002 tmp = NULL;
5003 } else {
5004 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5005 return 1;
5006 }
5007 *out = Dict(keys, values, lineno, col_offset, arena);
5008 if (*out == NULL) goto failed;
5009 return 0;
5010 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005011 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5012 if (isinstance == -1) {
5013 return 1;
5014 }
5015 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005016 asdl_seq* elts;
5017
5018 if (PyObject_HasAttrString(obj, "elts")) {
5019 int res;
5020 Py_ssize_t len;
5021 Py_ssize_t i;
5022 tmp = PyObject_GetAttrString(obj, "elts");
5023 if (tmp == NULL) goto failed;
5024 if (!PyList_Check(tmp)) {
5025 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5026 goto failed;
5027 }
5028 len = PyList_GET_SIZE(tmp);
5029 elts = asdl_seq_new(len, arena);
5030 if (elts == NULL) goto failed;
5031 for (i = 0; i < len; i++) {
5032 expr_ty value;
5033 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5034 if (res != 0) goto failed;
5035 asdl_seq_SET(elts, i, value);
5036 }
5037 Py_XDECREF(tmp);
5038 tmp = NULL;
5039 } else {
5040 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5041 return 1;
5042 }
5043 *out = Set(elts, lineno, col_offset, arena);
5044 if (*out == NULL) goto failed;
5045 return 0;
5046 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005047 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5048 if (isinstance == -1) {
5049 return 1;
5050 }
5051 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005052 expr_ty elt;
5053 asdl_seq* generators;
5054
5055 if (PyObject_HasAttrString(obj, "elt")) {
5056 int res;
5057 tmp = PyObject_GetAttrString(obj, "elt");
5058 if (tmp == NULL) goto failed;
5059 res = obj2ast_expr(tmp, &elt, arena);
5060 if (res != 0) goto failed;
5061 Py_XDECREF(tmp);
5062 tmp = NULL;
5063 } else {
5064 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5065 return 1;
5066 }
5067 if (PyObject_HasAttrString(obj, "generators")) {
5068 int res;
5069 Py_ssize_t len;
5070 Py_ssize_t i;
5071 tmp = PyObject_GetAttrString(obj, "generators");
5072 if (tmp == NULL) goto failed;
5073 if (!PyList_Check(tmp)) {
5074 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5075 goto failed;
5076 }
5077 len = PyList_GET_SIZE(tmp);
5078 generators = asdl_seq_new(len, arena);
5079 if (generators == NULL) goto failed;
5080 for (i = 0; i < len; i++) {
5081 comprehension_ty value;
5082 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5083 if (res != 0) goto failed;
5084 asdl_seq_SET(generators, i, value);
5085 }
5086 Py_XDECREF(tmp);
5087 tmp = NULL;
5088 } else {
5089 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5090 return 1;
5091 }
5092 *out = ListComp(elt, generators, lineno, col_offset, arena);
5093 if (*out == NULL) goto failed;
5094 return 0;
5095 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005096 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5097 if (isinstance == -1) {
5098 return 1;
5099 }
5100 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005101 expr_ty elt;
5102 asdl_seq* generators;
5103
5104 if (PyObject_HasAttrString(obj, "elt")) {
5105 int res;
5106 tmp = PyObject_GetAttrString(obj, "elt");
5107 if (tmp == NULL) goto failed;
5108 res = obj2ast_expr(tmp, &elt, arena);
5109 if (res != 0) goto failed;
5110 Py_XDECREF(tmp);
5111 tmp = NULL;
5112 } else {
5113 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5114 return 1;
5115 }
5116 if (PyObject_HasAttrString(obj, "generators")) {
5117 int res;
5118 Py_ssize_t len;
5119 Py_ssize_t i;
5120 tmp = PyObject_GetAttrString(obj, "generators");
5121 if (tmp == NULL) goto failed;
5122 if (!PyList_Check(tmp)) {
5123 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5124 goto failed;
5125 }
5126 len = PyList_GET_SIZE(tmp);
5127 generators = asdl_seq_new(len, arena);
5128 if (generators == NULL) goto failed;
5129 for (i = 0; i < len; i++) {
5130 comprehension_ty value;
5131 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5132 if (res != 0) goto failed;
5133 asdl_seq_SET(generators, i, value);
5134 }
5135 Py_XDECREF(tmp);
5136 tmp = NULL;
5137 } else {
5138 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5139 return 1;
5140 }
5141 *out = SetComp(elt, generators, lineno, col_offset, arena);
5142 if (*out == NULL) goto failed;
5143 return 0;
5144 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005145 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5146 if (isinstance == -1) {
5147 return 1;
5148 }
5149 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005150 expr_ty key;
5151 expr_ty value;
5152 asdl_seq* generators;
5153
5154 if (PyObject_HasAttrString(obj, "key")) {
5155 int res;
5156 tmp = PyObject_GetAttrString(obj, "key");
5157 if (tmp == NULL) goto failed;
5158 res = obj2ast_expr(tmp, &key, arena);
5159 if (res != 0) goto failed;
5160 Py_XDECREF(tmp);
5161 tmp = NULL;
5162 } else {
5163 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5164 return 1;
5165 }
5166 if (PyObject_HasAttrString(obj, "value")) {
5167 int res;
5168 tmp = PyObject_GetAttrString(obj, "value");
5169 if (tmp == NULL) goto failed;
5170 res = obj2ast_expr(tmp, &value, arena);
5171 if (res != 0) goto failed;
5172 Py_XDECREF(tmp);
5173 tmp = NULL;
5174 } else {
5175 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5176 return 1;
5177 }
5178 if (PyObject_HasAttrString(obj, "generators")) {
5179 int res;
5180 Py_ssize_t len;
5181 Py_ssize_t i;
5182 tmp = PyObject_GetAttrString(obj, "generators");
5183 if (tmp == NULL) goto failed;
5184 if (!PyList_Check(tmp)) {
5185 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5186 goto failed;
5187 }
5188 len = PyList_GET_SIZE(tmp);
5189 generators = asdl_seq_new(len, arena);
5190 if (generators == NULL) goto failed;
5191 for (i = 0; i < len; i++) {
5192 comprehension_ty value;
5193 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5194 if (res != 0) goto failed;
5195 asdl_seq_SET(generators, i, value);
5196 }
5197 Py_XDECREF(tmp);
5198 tmp = NULL;
5199 } else {
5200 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5201 return 1;
5202 }
5203 *out = DictComp(key, value, generators, lineno, col_offset,
5204 arena);
5205 if (*out == NULL) goto failed;
5206 return 0;
5207 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005208 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5209 if (isinstance == -1) {
5210 return 1;
5211 }
5212 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005213 expr_ty elt;
5214 asdl_seq* generators;
5215
5216 if (PyObject_HasAttrString(obj, "elt")) {
5217 int res;
5218 tmp = PyObject_GetAttrString(obj, "elt");
5219 if (tmp == NULL) goto failed;
5220 res = obj2ast_expr(tmp, &elt, arena);
5221 if (res != 0) goto failed;
5222 Py_XDECREF(tmp);
5223 tmp = NULL;
5224 } else {
5225 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5226 return 1;
5227 }
5228 if (PyObject_HasAttrString(obj, "generators")) {
5229 int res;
5230 Py_ssize_t len;
5231 Py_ssize_t i;
5232 tmp = PyObject_GetAttrString(obj, "generators");
5233 if (tmp == NULL) goto failed;
5234 if (!PyList_Check(tmp)) {
5235 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5236 goto failed;
5237 }
5238 len = PyList_GET_SIZE(tmp);
5239 generators = asdl_seq_new(len, arena);
5240 if (generators == NULL) goto failed;
5241 for (i = 0; i < len; i++) {
5242 comprehension_ty value;
5243 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5244 if (res != 0) goto failed;
5245 asdl_seq_SET(generators, i, value);
5246 }
5247 Py_XDECREF(tmp);
5248 tmp = NULL;
5249 } else {
5250 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5251 return 1;
5252 }
5253 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5254 if (*out == NULL) goto failed;
5255 return 0;
5256 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005257 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5258 if (isinstance == -1) {
5259 return 1;
5260 }
5261 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005262 expr_ty value;
5263
5264 if (PyObject_HasAttrString(obj, "value")) {
5265 int res;
5266 tmp = PyObject_GetAttrString(obj, "value");
5267 if (tmp == NULL) goto failed;
5268 res = obj2ast_expr(tmp, &value, arena);
5269 if (res != 0) goto failed;
5270 Py_XDECREF(tmp);
5271 tmp = NULL;
5272 } else {
5273 value = NULL;
5274 }
5275 *out = Yield(value, lineno, col_offset, arena);
5276 if (*out == NULL) goto failed;
5277 return 0;
5278 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005279 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5280 if (isinstance == -1) {
5281 return 1;
5282 }
5283 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005284 expr_ty left;
5285 asdl_int_seq* ops;
5286 asdl_seq* comparators;
5287
5288 if (PyObject_HasAttrString(obj, "left")) {
5289 int res;
5290 tmp = PyObject_GetAttrString(obj, "left");
5291 if (tmp == NULL) goto failed;
5292 res = obj2ast_expr(tmp, &left, arena);
5293 if (res != 0) goto failed;
5294 Py_XDECREF(tmp);
5295 tmp = NULL;
5296 } else {
5297 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5298 return 1;
5299 }
5300 if (PyObject_HasAttrString(obj, "ops")) {
5301 int res;
5302 Py_ssize_t len;
5303 Py_ssize_t i;
5304 tmp = PyObject_GetAttrString(obj, "ops");
5305 if (tmp == NULL) goto failed;
5306 if (!PyList_Check(tmp)) {
5307 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5308 goto failed;
5309 }
5310 len = PyList_GET_SIZE(tmp);
5311 ops = asdl_int_seq_new(len, arena);
5312 if (ops == NULL) goto failed;
5313 for (i = 0; i < len; i++) {
5314 cmpop_ty value;
5315 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5316 if (res != 0) goto failed;
5317 asdl_seq_SET(ops, i, value);
5318 }
5319 Py_XDECREF(tmp);
5320 tmp = NULL;
5321 } else {
5322 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5323 return 1;
5324 }
5325 if (PyObject_HasAttrString(obj, "comparators")) {
5326 int res;
5327 Py_ssize_t len;
5328 Py_ssize_t i;
5329 tmp = PyObject_GetAttrString(obj, "comparators");
5330 if (tmp == NULL) goto failed;
5331 if (!PyList_Check(tmp)) {
5332 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5333 goto failed;
5334 }
5335 len = PyList_GET_SIZE(tmp);
5336 comparators = asdl_seq_new(len, arena);
5337 if (comparators == NULL) goto failed;
5338 for (i = 0; i < len; i++) {
5339 expr_ty value;
5340 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5341 if (res != 0) goto failed;
5342 asdl_seq_SET(comparators, i, value);
5343 }
5344 Py_XDECREF(tmp);
5345 tmp = NULL;
5346 } else {
5347 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5348 return 1;
5349 }
5350 *out = Compare(left, ops, comparators, lineno, col_offset,
5351 arena);
5352 if (*out == NULL) goto failed;
5353 return 0;
5354 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005355 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5356 if (isinstance == -1) {
5357 return 1;
5358 }
5359 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005360 expr_ty func;
5361 asdl_seq* args;
5362 asdl_seq* keywords;
5363 expr_ty starargs;
5364 expr_ty kwargs;
5365
5366 if (PyObject_HasAttrString(obj, "func")) {
5367 int res;
5368 tmp = PyObject_GetAttrString(obj, "func");
5369 if (tmp == NULL) goto failed;
5370 res = obj2ast_expr(tmp, &func, arena);
5371 if (res != 0) goto failed;
5372 Py_XDECREF(tmp);
5373 tmp = NULL;
5374 } else {
5375 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5376 return 1;
5377 }
5378 if (PyObject_HasAttrString(obj, "args")) {
5379 int res;
5380 Py_ssize_t len;
5381 Py_ssize_t i;
5382 tmp = PyObject_GetAttrString(obj, "args");
5383 if (tmp == NULL) goto failed;
5384 if (!PyList_Check(tmp)) {
5385 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5386 goto failed;
5387 }
5388 len = PyList_GET_SIZE(tmp);
5389 args = asdl_seq_new(len, arena);
5390 if (args == NULL) goto failed;
5391 for (i = 0; i < len; i++) {
5392 expr_ty value;
5393 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5394 if (res != 0) goto failed;
5395 asdl_seq_SET(args, i, value);
5396 }
5397 Py_XDECREF(tmp);
5398 tmp = NULL;
5399 } else {
5400 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5401 return 1;
5402 }
5403 if (PyObject_HasAttrString(obj, "keywords")) {
5404 int res;
5405 Py_ssize_t len;
5406 Py_ssize_t i;
5407 tmp = PyObject_GetAttrString(obj, "keywords");
5408 if (tmp == NULL) goto failed;
5409 if (!PyList_Check(tmp)) {
5410 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5411 goto failed;
5412 }
5413 len = PyList_GET_SIZE(tmp);
5414 keywords = asdl_seq_new(len, arena);
5415 if (keywords == NULL) goto failed;
5416 for (i = 0; i < len; i++) {
5417 keyword_ty value;
5418 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5419 if (res != 0) goto failed;
5420 asdl_seq_SET(keywords, i, value);
5421 }
5422 Py_XDECREF(tmp);
5423 tmp = NULL;
5424 } else {
5425 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5426 return 1;
5427 }
5428 if (PyObject_HasAttrString(obj, "starargs")) {
5429 int res;
5430 tmp = PyObject_GetAttrString(obj, "starargs");
5431 if (tmp == NULL) goto failed;
5432 res = obj2ast_expr(tmp, &starargs, arena);
5433 if (res != 0) goto failed;
5434 Py_XDECREF(tmp);
5435 tmp = NULL;
5436 } else {
5437 starargs = NULL;
5438 }
5439 if (PyObject_HasAttrString(obj, "kwargs")) {
5440 int res;
5441 tmp = PyObject_GetAttrString(obj, "kwargs");
5442 if (tmp == NULL) goto failed;
5443 res = obj2ast_expr(tmp, &kwargs, arena);
5444 if (res != 0) goto failed;
5445 Py_XDECREF(tmp);
5446 tmp = NULL;
5447 } else {
5448 kwargs = NULL;
5449 }
5450 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5451 col_offset, arena);
5452 if (*out == NULL) goto failed;
5453 return 0;
5454 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005455 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5456 if (isinstance == -1) {
5457 return 1;
5458 }
5459 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005460 object n;
5461
5462 if (PyObject_HasAttrString(obj, "n")) {
5463 int res;
5464 tmp = PyObject_GetAttrString(obj, "n");
5465 if (tmp == NULL) goto failed;
5466 res = obj2ast_object(tmp, &n, arena);
5467 if (res != 0) goto failed;
5468 Py_XDECREF(tmp);
5469 tmp = NULL;
5470 } else {
5471 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5472 return 1;
5473 }
5474 *out = Num(n, lineno, col_offset, arena);
5475 if (*out == NULL) goto failed;
5476 return 0;
5477 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005478 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5479 if (isinstance == -1) {
5480 return 1;
5481 }
5482 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005483 string s;
5484
5485 if (PyObject_HasAttrString(obj, "s")) {
5486 int res;
5487 tmp = PyObject_GetAttrString(obj, "s");
5488 if (tmp == NULL) goto failed;
5489 res = obj2ast_string(tmp, &s, arena);
5490 if (res != 0) goto failed;
5491 Py_XDECREF(tmp);
5492 tmp = NULL;
5493 } else {
5494 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5495 return 1;
5496 }
5497 *out = Str(s, lineno, col_offset, arena);
5498 if (*out == NULL) goto failed;
5499 return 0;
5500 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005501 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5502 if (isinstance == -1) {
5503 return 1;
5504 }
5505 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005506 string s;
5507
5508 if (PyObject_HasAttrString(obj, "s")) {
5509 int res;
5510 tmp = PyObject_GetAttrString(obj, "s");
5511 if (tmp == NULL) goto failed;
5512 res = obj2ast_string(tmp, &s, arena);
5513 if (res != 0) goto failed;
5514 Py_XDECREF(tmp);
5515 tmp = NULL;
5516 } else {
5517 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5518 return 1;
5519 }
5520 *out = Bytes(s, lineno, col_offset, arena);
5521 if (*out == NULL) goto failed;
5522 return 0;
5523 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005524 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5525 if (isinstance == -1) {
5526 return 1;
5527 }
5528 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005529
5530 *out = Ellipsis(lineno, col_offset, arena);
5531 if (*out == NULL) goto failed;
5532 return 0;
5533 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005534 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5535 if (isinstance == -1) {
5536 return 1;
5537 }
5538 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005539 expr_ty value;
5540 identifier attr;
5541 expr_context_ty ctx;
5542
5543 if (PyObject_HasAttrString(obj, "value")) {
5544 int res;
5545 tmp = PyObject_GetAttrString(obj, "value");
5546 if (tmp == NULL) goto failed;
5547 res = obj2ast_expr(tmp, &value, arena);
5548 if (res != 0) goto failed;
5549 Py_XDECREF(tmp);
5550 tmp = NULL;
5551 } else {
5552 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5553 return 1;
5554 }
5555 if (PyObject_HasAttrString(obj, "attr")) {
5556 int res;
5557 tmp = PyObject_GetAttrString(obj, "attr");
5558 if (tmp == NULL) goto failed;
5559 res = obj2ast_identifier(tmp, &attr, arena);
5560 if (res != 0) goto failed;
5561 Py_XDECREF(tmp);
5562 tmp = NULL;
5563 } else {
5564 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5565 return 1;
5566 }
5567 if (PyObject_HasAttrString(obj, "ctx")) {
5568 int res;
5569 tmp = PyObject_GetAttrString(obj, "ctx");
5570 if (tmp == NULL) goto failed;
5571 res = obj2ast_expr_context(tmp, &ctx, arena);
5572 if (res != 0) goto failed;
5573 Py_XDECREF(tmp);
5574 tmp = NULL;
5575 } else {
5576 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5577 return 1;
5578 }
5579 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5580 if (*out == NULL) goto failed;
5581 return 0;
5582 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005583 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5584 if (isinstance == -1) {
5585 return 1;
5586 }
5587 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005588 expr_ty value;
5589 slice_ty slice;
5590 expr_context_ty ctx;
5591
5592 if (PyObject_HasAttrString(obj, "value")) {
5593 int res;
5594 tmp = PyObject_GetAttrString(obj, "value");
5595 if (tmp == NULL) goto failed;
5596 res = obj2ast_expr(tmp, &value, arena);
5597 if (res != 0) goto failed;
5598 Py_XDECREF(tmp);
5599 tmp = NULL;
5600 } else {
5601 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5602 return 1;
5603 }
5604 if (PyObject_HasAttrString(obj, "slice")) {
5605 int res;
5606 tmp = PyObject_GetAttrString(obj, "slice");
5607 if (tmp == NULL) goto failed;
5608 res = obj2ast_slice(tmp, &slice, arena);
5609 if (res != 0) goto failed;
5610 Py_XDECREF(tmp);
5611 tmp = NULL;
5612 } else {
5613 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5614 return 1;
5615 }
5616 if (PyObject_HasAttrString(obj, "ctx")) {
5617 int res;
5618 tmp = PyObject_GetAttrString(obj, "ctx");
5619 if (tmp == NULL) goto failed;
5620 res = obj2ast_expr_context(tmp, &ctx, arena);
5621 if (res != 0) goto failed;
5622 Py_XDECREF(tmp);
5623 tmp = NULL;
5624 } else {
5625 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5626 return 1;
5627 }
5628 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5629 if (*out == NULL) goto failed;
5630 return 0;
5631 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005632 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5633 if (isinstance == -1) {
5634 return 1;
5635 }
5636 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005637 expr_ty value;
5638 expr_context_ty ctx;
5639
5640 if (PyObject_HasAttrString(obj, "value")) {
5641 int res;
5642 tmp = PyObject_GetAttrString(obj, "value");
5643 if (tmp == NULL) goto failed;
5644 res = obj2ast_expr(tmp, &value, arena);
5645 if (res != 0) goto failed;
5646 Py_XDECREF(tmp);
5647 tmp = NULL;
5648 } else {
5649 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5650 return 1;
5651 }
5652 if (PyObject_HasAttrString(obj, "ctx")) {
5653 int res;
5654 tmp = PyObject_GetAttrString(obj, "ctx");
5655 if (tmp == NULL) goto failed;
5656 res = obj2ast_expr_context(tmp, &ctx, arena);
5657 if (res != 0) goto failed;
5658 Py_XDECREF(tmp);
5659 tmp = NULL;
5660 } else {
5661 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5662 return 1;
5663 }
5664 *out = Starred(value, ctx, lineno, col_offset, arena);
5665 if (*out == NULL) goto failed;
5666 return 0;
5667 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005668 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5669 if (isinstance == -1) {
5670 return 1;
5671 }
5672 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005673 identifier id;
5674 expr_context_ty ctx;
5675
5676 if (PyObject_HasAttrString(obj, "id")) {
5677 int res;
5678 tmp = PyObject_GetAttrString(obj, "id");
5679 if (tmp == NULL) goto failed;
5680 res = obj2ast_identifier(tmp, &id, arena);
5681 if (res != 0) goto failed;
5682 Py_XDECREF(tmp);
5683 tmp = NULL;
5684 } else {
5685 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5686 return 1;
5687 }
5688 if (PyObject_HasAttrString(obj, "ctx")) {
5689 int res;
5690 tmp = PyObject_GetAttrString(obj, "ctx");
5691 if (tmp == NULL) goto failed;
5692 res = obj2ast_expr_context(tmp, &ctx, arena);
5693 if (res != 0) goto failed;
5694 Py_XDECREF(tmp);
5695 tmp = NULL;
5696 } else {
5697 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5698 return 1;
5699 }
5700 *out = Name(id, ctx, lineno, col_offset, arena);
5701 if (*out == NULL) goto failed;
5702 return 0;
5703 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005704 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5705 if (isinstance == -1) {
5706 return 1;
5707 }
5708 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005709 asdl_seq* elts;
5710 expr_context_ty ctx;
5711
5712 if (PyObject_HasAttrString(obj, "elts")) {
5713 int res;
5714 Py_ssize_t len;
5715 Py_ssize_t i;
5716 tmp = PyObject_GetAttrString(obj, "elts");
5717 if (tmp == NULL) goto failed;
5718 if (!PyList_Check(tmp)) {
5719 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5720 goto failed;
5721 }
5722 len = PyList_GET_SIZE(tmp);
5723 elts = asdl_seq_new(len, arena);
5724 if (elts == NULL) goto failed;
5725 for (i = 0; i < len; i++) {
5726 expr_ty value;
5727 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5728 if (res != 0) goto failed;
5729 asdl_seq_SET(elts, i, value);
5730 }
5731 Py_XDECREF(tmp);
5732 tmp = NULL;
5733 } else {
5734 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5735 return 1;
5736 }
5737 if (PyObject_HasAttrString(obj, "ctx")) {
5738 int res;
5739 tmp = PyObject_GetAttrString(obj, "ctx");
5740 if (tmp == NULL) goto failed;
5741 res = obj2ast_expr_context(tmp, &ctx, arena);
5742 if (res != 0) goto failed;
5743 Py_XDECREF(tmp);
5744 tmp = NULL;
5745 } else {
5746 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5747 return 1;
5748 }
5749 *out = List(elts, ctx, lineno, col_offset, arena);
5750 if (*out == NULL) goto failed;
5751 return 0;
5752 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005753 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5754 if (isinstance == -1) {
5755 return 1;
5756 }
5757 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005758 asdl_seq* elts;
5759 expr_context_ty ctx;
5760
5761 if (PyObject_HasAttrString(obj, "elts")) {
5762 int res;
5763 Py_ssize_t len;
5764 Py_ssize_t i;
5765 tmp = PyObject_GetAttrString(obj, "elts");
5766 if (tmp == NULL) goto failed;
5767 if (!PyList_Check(tmp)) {
5768 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5769 goto failed;
5770 }
5771 len = PyList_GET_SIZE(tmp);
5772 elts = asdl_seq_new(len, arena);
5773 if (elts == NULL) goto failed;
5774 for (i = 0; i < len; i++) {
5775 expr_ty value;
5776 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5777 if (res != 0) goto failed;
5778 asdl_seq_SET(elts, i, value);
5779 }
5780 Py_XDECREF(tmp);
5781 tmp = NULL;
5782 } else {
5783 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5784 return 1;
5785 }
5786 if (PyObject_HasAttrString(obj, "ctx")) {
5787 int res;
5788 tmp = PyObject_GetAttrString(obj, "ctx");
5789 if (tmp == NULL) goto failed;
5790 res = obj2ast_expr_context(tmp, &ctx, arena);
5791 if (res != 0) goto failed;
5792 Py_XDECREF(tmp);
5793 tmp = NULL;
5794 } else {
5795 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5796 return 1;
5797 }
5798 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5799 if (*out == NULL) goto failed;
5800 return 0;
5801 }
5802
Benjamin Peterson5b066812010-11-20 01:38:49 +00005803 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5804 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005805 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005806 return 1;
5807}
5808
5809int
5810obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5811{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005812 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005813
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005814 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5815 if (isinstance == -1) {
5816 return 1;
5817 }
5818 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005819 *out = Load;
5820 return 0;
5821 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005822 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5823 if (isinstance == -1) {
5824 return 1;
5825 }
5826 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005827 *out = Store;
5828 return 0;
5829 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005830 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5831 if (isinstance == -1) {
5832 return 1;
5833 }
5834 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005835 *out = Del;
5836 return 0;
5837 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005838 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5839 if (isinstance == -1) {
5840 return 1;
5841 }
5842 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005843 *out = AugLoad;
5844 return 0;
5845 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005846 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5847 if (isinstance == -1) {
5848 return 1;
5849 }
5850 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005851 *out = AugStore;
5852 return 0;
5853 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005854 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5855 if (isinstance == -1) {
5856 return 1;
5857 }
5858 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005859 *out = Param;
5860 return 0;
5861 }
5862
Benjamin Peterson5b066812010-11-20 01:38:49 +00005863 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005864 return 1;
5865}
5866
5867int
5868obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5869{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005870 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005871
Benjamin Petersond8f65972010-11-20 04:31:07 +00005872 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005873
5874 if (obj == Py_None) {
5875 *out = NULL;
5876 return 0;
5877 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005878 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5879 if (isinstance == -1) {
5880 return 1;
5881 }
5882 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005883 expr_ty lower;
5884 expr_ty upper;
5885 expr_ty step;
5886
5887 if (PyObject_HasAttrString(obj, "lower")) {
5888 int res;
5889 tmp = PyObject_GetAttrString(obj, "lower");
5890 if (tmp == NULL) goto failed;
5891 res = obj2ast_expr(tmp, &lower, arena);
5892 if (res != 0) goto failed;
5893 Py_XDECREF(tmp);
5894 tmp = NULL;
5895 } else {
5896 lower = NULL;
5897 }
5898 if (PyObject_HasAttrString(obj, "upper")) {
5899 int res;
5900 tmp = PyObject_GetAttrString(obj, "upper");
5901 if (tmp == NULL) goto failed;
5902 res = obj2ast_expr(tmp, &upper, arena);
5903 if (res != 0) goto failed;
5904 Py_XDECREF(tmp);
5905 tmp = NULL;
5906 } else {
5907 upper = NULL;
5908 }
5909 if (PyObject_HasAttrString(obj, "step")) {
5910 int res;
5911 tmp = PyObject_GetAttrString(obj, "step");
5912 if (tmp == NULL) goto failed;
5913 res = obj2ast_expr(tmp, &step, arena);
5914 if (res != 0) goto failed;
5915 Py_XDECREF(tmp);
5916 tmp = NULL;
5917 } else {
5918 step = NULL;
5919 }
5920 *out = Slice(lower, upper, step, arena);
5921 if (*out == NULL) goto failed;
5922 return 0;
5923 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005924 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5925 if (isinstance == -1) {
5926 return 1;
5927 }
5928 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005929 asdl_seq* dims;
5930
5931 if (PyObject_HasAttrString(obj, "dims")) {
5932 int res;
5933 Py_ssize_t len;
5934 Py_ssize_t i;
5935 tmp = PyObject_GetAttrString(obj, "dims");
5936 if (tmp == NULL) goto failed;
5937 if (!PyList_Check(tmp)) {
5938 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5939 goto failed;
5940 }
5941 len = PyList_GET_SIZE(tmp);
5942 dims = asdl_seq_new(len, arena);
5943 if (dims == NULL) goto failed;
5944 for (i = 0; i < len; i++) {
5945 slice_ty value;
5946 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5947 if (res != 0) goto failed;
5948 asdl_seq_SET(dims, i, value);
5949 }
5950 Py_XDECREF(tmp);
5951 tmp = NULL;
5952 } else {
5953 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5954 return 1;
5955 }
5956 *out = ExtSlice(dims, arena);
5957 if (*out == NULL) goto failed;
5958 return 0;
5959 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005960 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5961 if (isinstance == -1) {
5962 return 1;
5963 }
5964 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005965 expr_ty value;
5966
5967 if (PyObject_HasAttrString(obj, "value")) {
5968 int res;
5969 tmp = PyObject_GetAttrString(obj, "value");
5970 if (tmp == NULL) goto failed;
5971 res = obj2ast_expr(tmp, &value, arena);
5972 if (res != 0) goto failed;
5973 Py_XDECREF(tmp);
5974 tmp = NULL;
5975 } else {
5976 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5977 return 1;
5978 }
5979 *out = Index(value, arena);
5980 if (*out == NULL) goto failed;
5981 return 0;
5982 }
5983
Benjamin Peterson5b066812010-11-20 01:38:49 +00005984 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
5985 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005986 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005987 return 1;
5988}
5989
5990int
5991obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5992{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005993 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005994
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005995 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5996 if (isinstance == -1) {
5997 return 1;
5998 }
5999 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006000 *out = And;
6001 return 0;
6002 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006003 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6004 if (isinstance == -1) {
6005 return 1;
6006 }
6007 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006008 *out = Or;
6009 return 0;
6010 }
6011
Benjamin Peterson5b066812010-11-20 01:38:49 +00006012 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006013 return 1;
6014}
6015
6016int
6017obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6018{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006019 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006020
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006021 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6022 if (isinstance == -1) {
6023 return 1;
6024 }
6025 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006026 *out = Add;
6027 return 0;
6028 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006029 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6030 if (isinstance == -1) {
6031 return 1;
6032 }
6033 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006034 *out = Sub;
6035 return 0;
6036 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006037 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6038 if (isinstance == -1) {
6039 return 1;
6040 }
6041 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006042 *out = Mult;
6043 return 0;
6044 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006045 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6046 if (isinstance == -1) {
6047 return 1;
6048 }
6049 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006050 *out = Div;
6051 return 0;
6052 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006053 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6054 if (isinstance == -1) {
6055 return 1;
6056 }
6057 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006058 *out = Mod;
6059 return 0;
6060 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006061 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6062 if (isinstance == -1) {
6063 return 1;
6064 }
6065 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006066 *out = Pow;
6067 return 0;
6068 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006069 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6070 if (isinstance == -1) {
6071 return 1;
6072 }
6073 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006074 *out = LShift;
6075 return 0;
6076 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006077 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6078 if (isinstance == -1) {
6079 return 1;
6080 }
6081 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006082 *out = RShift;
6083 return 0;
6084 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006085 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6086 if (isinstance == -1) {
6087 return 1;
6088 }
6089 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006090 *out = BitOr;
6091 return 0;
6092 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006093 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6094 if (isinstance == -1) {
6095 return 1;
6096 }
6097 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006098 *out = BitXor;
6099 return 0;
6100 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006101 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6102 if (isinstance == -1) {
6103 return 1;
6104 }
6105 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006106 *out = BitAnd;
6107 return 0;
6108 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006109 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6110 if (isinstance == -1) {
6111 return 1;
6112 }
6113 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006114 *out = FloorDiv;
6115 return 0;
6116 }
6117
Benjamin Peterson5b066812010-11-20 01:38:49 +00006118 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006119 return 1;
6120}
6121
6122int
6123obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6124{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006125 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006126
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006127 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6128 if (isinstance == -1) {
6129 return 1;
6130 }
6131 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006132 *out = Invert;
6133 return 0;
6134 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006135 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6136 if (isinstance == -1) {
6137 return 1;
6138 }
6139 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006140 *out = Not;
6141 return 0;
6142 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006143 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6144 if (isinstance == -1) {
6145 return 1;
6146 }
6147 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006148 *out = UAdd;
6149 return 0;
6150 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006151 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6152 if (isinstance == -1) {
6153 return 1;
6154 }
6155 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006156 *out = USub;
6157 return 0;
6158 }
6159
Benjamin Peterson5b066812010-11-20 01:38:49 +00006160 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006161 return 1;
6162}
6163
6164int
6165obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6166{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006167 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006168
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006169 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6170 if (isinstance == -1) {
6171 return 1;
6172 }
6173 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006174 *out = Eq;
6175 return 0;
6176 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006177 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6178 if (isinstance == -1) {
6179 return 1;
6180 }
6181 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006182 *out = NotEq;
6183 return 0;
6184 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006185 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6186 if (isinstance == -1) {
6187 return 1;
6188 }
6189 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006190 *out = Lt;
6191 return 0;
6192 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006193 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6194 if (isinstance == -1) {
6195 return 1;
6196 }
6197 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006198 *out = LtE;
6199 return 0;
6200 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006201 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6202 if (isinstance == -1) {
6203 return 1;
6204 }
6205 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006206 *out = Gt;
6207 return 0;
6208 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006209 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6210 if (isinstance == -1) {
6211 return 1;
6212 }
6213 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006214 *out = GtE;
6215 return 0;
6216 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006217 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6218 if (isinstance == -1) {
6219 return 1;
6220 }
6221 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006222 *out = Is;
6223 return 0;
6224 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006225 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6226 if (isinstance == -1) {
6227 return 1;
6228 }
6229 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006230 *out = IsNot;
6231 return 0;
6232 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006233 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6234 if (isinstance == -1) {
6235 return 1;
6236 }
6237 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006238 *out = In;
6239 return 0;
6240 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006241 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6242 if (isinstance == -1) {
6243 return 1;
6244 }
6245 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006246 *out = NotIn;
6247 return 0;
6248 }
6249
Benjamin Peterson5b066812010-11-20 01:38:49 +00006250 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006251 return 1;
6252}
6253
6254int
6255obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6256{
6257 PyObject* tmp = NULL;
6258 expr_ty target;
6259 expr_ty iter;
6260 asdl_seq* ifs;
6261
6262 if (PyObject_HasAttrString(obj, "target")) {
6263 int res;
6264 tmp = PyObject_GetAttrString(obj, "target");
6265 if (tmp == NULL) goto failed;
6266 res = obj2ast_expr(tmp, &target, arena);
6267 if (res != 0) goto failed;
6268 Py_XDECREF(tmp);
6269 tmp = NULL;
6270 } else {
6271 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6272 return 1;
6273 }
6274 if (PyObject_HasAttrString(obj, "iter")) {
6275 int res;
6276 tmp = PyObject_GetAttrString(obj, "iter");
6277 if (tmp == NULL) goto failed;
6278 res = obj2ast_expr(tmp, &iter, arena);
6279 if (res != 0) goto failed;
6280 Py_XDECREF(tmp);
6281 tmp = NULL;
6282 } else {
6283 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6284 return 1;
6285 }
6286 if (PyObject_HasAttrString(obj, "ifs")) {
6287 int res;
6288 Py_ssize_t len;
6289 Py_ssize_t i;
6290 tmp = PyObject_GetAttrString(obj, "ifs");
6291 if (tmp == NULL) goto failed;
6292 if (!PyList_Check(tmp)) {
6293 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6294 goto failed;
6295 }
6296 len = PyList_GET_SIZE(tmp);
6297 ifs = asdl_seq_new(len, arena);
6298 if (ifs == NULL) goto failed;
6299 for (i = 0; i < len; i++) {
6300 expr_ty value;
6301 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6302 if (res != 0) goto failed;
6303 asdl_seq_SET(ifs, i, value);
6304 }
6305 Py_XDECREF(tmp);
6306 tmp = NULL;
6307 } else {
6308 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6309 return 1;
6310 }
6311 *out = comprehension(target, iter, ifs, arena);
6312 return 0;
6313failed:
6314 Py_XDECREF(tmp);
6315 return 1;
6316}
6317
6318int
6319obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6320{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006321 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006322
Benjamin Petersond8f65972010-11-20 04:31:07 +00006323 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006324 int lineno;
6325 int col_offset;
6326
Neal Norwitzad74aa82008-03-31 05:14:30 +00006327 if (obj == Py_None) {
6328 *out = NULL;
6329 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006330 }
6331 if (PyObject_HasAttrString(obj, "lineno")) {
6332 int res;
6333 tmp = PyObject_GetAttrString(obj, "lineno");
6334 if (tmp == NULL) goto failed;
6335 res = obj2ast_int(tmp, &lineno, arena);
6336 if (res != 0) goto failed;
6337 Py_XDECREF(tmp);
6338 tmp = NULL;
6339 } else {
6340 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6341 return 1;
6342 }
6343 if (PyObject_HasAttrString(obj, "col_offset")) {
6344 int res;
6345 tmp = PyObject_GetAttrString(obj, "col_offset");
6346 if (tmp == NULL) goto failed;
6347 res = obj2ast_int(tmp, &col_offset, arena);
6348 if (res != 0) goto failed;
6349 Py_XDECREF(tmp);
6350 tmp = NULL;
6351 } else {
6352 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6353 return 1;
6354 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006355 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6356 if (isinstance == -1) {
6357 return 1;
6358 }
6359 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006360 expr_ty type;
6361 identifier name;
6362 asdl_seq* body;
6363
6364 if (PyObject_HasAttrString(obj, "type")) {
6365 int res;
6366 tmp = PyObject_GetAttrString(obj, "type");
6367 if (tmp == NULL) goto failed;
6368 res = obj2ast_expr(tmp, &type, arena);
6369 if (res != 0) goto failed;
6370 Py_XDECREF(tmp);
6371 tmp = NULL;
6372 } else {
6373 type = NULL;
6374 }
6375 if (PyObject_HasAttrString(obj, "name")) {
6376 int res;
6377 tmp = PyObject_GetAttrString(obj, "name");
6378 if (tmp == NULL) goto failed;
6379 res = obj2ast_identifier(tmp, &name, arena);
6380 if (res != 0) goto failed;
6381 Py_XDECREF(tmp);
6382 tmp = NULL;
6383 } else {
6384 name = NULL;
6385 }
6386 if (PyObject_HasAttrString(obj, "body")) {
6387 int res;
6388 Py_ssize_t len;
6389 Py_ssize_t i;
6390 tmp = PyObject_GetAttrString(obj, "body");
6391 if (tmp == NULL) goto failed;
6392 if (!PyList_Check(tmp)) {
6393 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6394 goto failed;
6395 }
6396 len = PyList_GET_SIZE(tmp);
6397 body = asdl_seq_new(len, arena);
6398 if (body == NULL) goto failed;
6399 for (i = 0; i < len; i++) {
6400 stmt_ty value;
6401 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6402 if (res != 0) goto failed;
6403 asdl_seq_SET(body, i, value);
6404 }
6405 Py_XDECREF(tmp);
6406 tmp = NULL;
6407 } else {
6408 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6409 return 1;
6410 }
6411 *out = ExceptHandler(type, name, body, lineno, col_offset,
6412 arena);
6413 if (*out == NULL) goto failed;
6414 return 0;
6415 }
6416
Benjamin Peterson5b066812010-11-20 01:38:49 +00006417 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6418 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006419 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006420 return 1;
6421}
6422
6423int
6424obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6425{
6426 PyObject* tmp = NULL;
6427 asdl_seq* args;
6428 identifier vararg;
6429 expr_ty varargannotation;
6430 asdl_seq* kwonlyargs;
6431 identifier kwarg;
6432 expr_ty kwargannotation;
6433 asdl_seq* defaults;
6434 asdl_seq* kw_defaults;
6435
6436 if (PyObject_HasAttrString(obj, "args")) {
6437 int res;
6438 Py_ssize_t len;
6439 Py_ssize_t i;
6440 tmp = PyObject_GetAttrString(obj, "args");
6441 if (tmp == NULL) goto failed;
6442 if (!PyList_Check(tmp)) {
6443 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6444 goto failed;
6445 }
6446 len = PyList_GET_SIZE(tmp);
6447 args = asdl_seq_new(len, arena);
6448 if (args == NULL) goto failed;
6449 for (i = 0; i < len; i++) {
6450 arg_ty value;
6451 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6452 if (res != 0) goto failed;
6453 asdl_seq_SET(args, i, value);
6454 }
6455 Py_XDECREF(tmp);
6456 tmp = NULL;
6457 } else {
6458 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6459 return 1;
6460 }
6461 if (PyObject_HasAttrString(obj, "vararg")) {
6462 int res;
6463 tmp = PyObject_GetAttrString(obj, "vararg");
6464 if (tmp == NULL) goto failed;
6465 res = obj2ast_identifier(tmp, &vararg, arena);
6466 if (res != 0) goto failed;
6467 Py_XDECREF(tmp);
6468 tmp = NULL;
6469 } else {
6470 vararg = NULL;
6471 }
6472 if (PyObject_HasAttrString(obj, "varargannotation")) {
6473 int res;
6474 tmp = PyObject_GetAttrString(obj, "varargannotation");
6475 if (tmp == NULL) goto failed;
6476 res = obj2ast_expr(tmp, &varargannotation, arena);
6477 if (res != 0) goto failed;
6478 Py_XDECREF(tmp);
6479 tmp = NULL;
6480 } else {
6481 varargannotation = NULL;
6482 }
6483 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6484 int res;
6485 Py_ssize_t len;
6486 Py_ssize_t i;
6487 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6488 if (tmp == NULL) goto failed;
6489 if (!PyList_Check(tmp)) {
6490 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6491 goto failed;
6492 }
6493 len = PyList_GET_SIZE(tmp);
6494 kwonlyargs = asdl_seq_new(len, arena);
6495 if (kwonlyargs == NULL) goto failed;
6496 for (i = 0; i < len; i++) {
6497 arg_ty value;
6498 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6499 if (res != 0) goto failed;
6500 asdl_seq_SET(kwonlyargs, i, value);
6501 }
6502 Py_XDECREF(tmp);
6503 tmp = NULL;
6504 } else {
6505 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6506 return 1;
6507 }
6508 if (PyObject_HasAttrString(obj, "kwarg")) {
6509 int res;
6510 tmp = PyObject_GetAttrString(obj, "kwarg");
6511 if (tmp == NULL) goto failed;
6512 res = obj2ast_identifier(tmp, &kwarg, arena);
6513 if (res != 0) goto failed;
6514 Py_XDECREF(tmp);
6515 tmp = NULL;
6516 } else {
6517 kwarg = NULL;
6518 }
6519 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6520 int res;
6521 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6522 if (tmp == NULL) goto failed;
6523 res = obj2ast_expr(tmp, &kwargannotation, arena);
6524 if (res != 0) goto failed;
6525 Py_XDECREF(tmp);
6526 tmp = NULL;
6527 } else {
6528 kwargannotation = NULL;
6529 }
6530 if (PyObject_HasAttrString(obj, "defaults")) {
6531 int res;
6532 Py_ssize_t len;
6533 Py_ssize_t i;
6534 tmp = PyObject_GetAttrString(obj, "defaults");
6535 if (tmp == NULL) goto failed;
6536 if (!PyList_Check(tmp)) {
6537 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6538 goto failed;
6539 }
6540 len = PyList_GET_SIZE(tmp);
6541 defaults = asdl_seq_new(len, arena);
6542 if (defaults == NULL) goto failed;
6543 for (i = 0; i < len; i++) {
6544 expr_ty value;
6545 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6546 if (res != 0) goto failed;
6547 asdl_seq_SET(defaults, i, value);
6548 }
6549 Py_XDECREF(tmp);
6550 tmp = NULL;
6551 } else {
6552 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6553 return 1;
6554 }
6555 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6556 int res;
6557 Py_ssize_t len;
6558 Py_ssize_t i;
6559 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6560 if (tmp == NULL) goto failed;
6561 if (!PyList_Check(tmp)) {
6562 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6563 goto failed;
6564 }
6565 len = PyList_GET_SIZE(tmp);
6566 kw_defaults = asdl_seq_new(len, arena);
6567 if (kw_defaults == NULL) goto failed;
6568 for (i = 0; i < len; i++) {
6569 expr_ty value;
6570 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6571 if (res != 0) goto failed;
6572 asdl_seq_SET(kw_defaults, i, value);
6573 }
6574 Py_XDECREF(tmp);
6575 tmp = NULL;
6576 } else {
6577 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6578 return 1;
6579 }
6580 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6581 kwargannotation, defaults, kw_defaults, arena);
6582 return 0;
6583failed:
6584 Py_XDECREF(tmp);
6585 return 1;
6586}
6587
6588int
6589obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6590{
6591 PyObject* tmp = NULL;
6592 identifier arg;
6593 expr_ty annotation;
6594
6595 if (PyObject_HasAttrString(obj, "arg")) {
6596 int res;
6597 tmp = PyObject_GetAttrString(obj, "arg");
6598 if (tmp == NULL) goto failed;
6599 res = obj2ast_identifier(tmp, &arg, arena);
6600 if (res != 0) goto failed;
6601 Py_XDECREF(tmp);
6602 tmp = NULL;
6603 } else {
6604 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6605 return 1;
6606 }
6607 if (PyObject_HasAttrString(obj, "annotation")) {
6608 int res;
6609 tmp = PyObject_GetAttrString(obj, "annotation");
6610 if (tmp == NULL) goto failed;
6611 res = obj2ast_expr(tmp, &annotation, arena);
6612 if (res != 0) goto failed;
6613 Py_XDECREF(tmp);
6614 tmp = NULL;
6615 } else {
6616 annotation = NULL;
6617 }
6618 *out = arg(arg, annotation, arena);
6619 return 0;
6620failed:
6621 Py_XDECREF(tmp);
6622 return 1;
6623}
6624
6625int
6626obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6627{
6628 PyObject* tmp = NULL;
6629 identifier arg;
6630 expr_ty value;
6631
6632 if (PyObject_HasAttrString(obj, "arg")) {
6633 int res;
6634 tmp = PyObject_GetAttrString(obj, "arg");
6635 if (tmp == NULL) goto failed;
6636 res = obj2ast_identifier(tmp, &arg, arena);
6637 if (res != 0) goto failed;
6638 Py_XDECREF(tmp);
6639 tmp = NULL;
6640 } else {
6641 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6642 return 1;
6643 }
6644 if (PyObject_HasAttrString(obj, "value")) {
6645 int res;
6646 tmp = PyObject_GetAttrString(obj, "value");
6647 if (tmp == NULL) goto failed;
6648 res = obj2ast_expr(tmp, &value, arena);
6649 if (res != 0) goto failed;
6650 Py_XDECREF(tmp);
6651 tmp = NULL;
6652 } else {
6653 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6654 return 1;
6655 }
6656 *out = keyword(arg, value, arena);
6657 return 0;
6658failed:
6659 Py_XDECREF(tmp);
6660 return 1;
6661}
6662
6663int
6664obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6665{
6666 PyObject* tmp = NULL;
6667 identifier name;
6668 identifier asname;
6669
6670 if (PyObject_HasAttrString(obj, "name")) {
6671 int res;
6672 tmp = PyObject_GetAttrString(obj, "name");
6673 if (tmp == NULL) goto failed;
6674 res = obj2ast_identifier(tmp, &name, arena);
6675 if (res != 0) goto failed;
6676 Py_XDECREF(tmp);
6677 tmp = NULL;
6678 } else {
6679 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6680 return 1;
6681 }
6682 if (PyObject_HasAttrString(obj, "asname")) {
6683 int res;
6684 tmp = PyObject_GetAttrString(obj, "asname");
6685 if (tmp == NULL) goto failed;
6686 res = obj2ast_identifier(tmp, &asname, arena);
6687 if (res != 0) goto failed;
6688 Py_XDECREF(tmp);
6689 tmp = NULL;
6690 } else {
6691 asname = NULL;
6692 }
6693 *out = alias(name, asname, arena);
6694 return 0;
6695failed:
6696 Py_XDECREF(tmp);
6697 return 1;
6698}
6699
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006700int
6701obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6702{
6703 PyObject* tmp = NULL;
6704 expr_ty context_expr;
6705 expr_ty optional_vars;
6706
6707 if (PyObject_HasAttrString(obj, "context_expr")) {
6708 int res;
6709 tmp = PyObject_GetAttrString(obj, "context_expr");
6710 if (tmp == NULL) goto failed;
6711 res = obj2ast_expr(tmp, &context_expr, arena);
6712 if (res != 0) goto failed;
6713 Py_XDECREF(tmp);
6714 tmp = NULL;
6715 } else {
6716 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6717 return 1;
6718 }
6719 if (PyObject_HasAttrString(obj, "optional_vars")) {
6720 int res;
6721 tmp = PyObject_GetAttrString(obj, "optional_vars");
6722 if (tmp == NULL) goto failed;
6723 res = obj2ast_expr(tmp, &optional_vars, arena);
6724 if (res != 0) goto failed;
6725 Py_XDECREF(tmp);
6726 tmp = NULL;
6727 } else {
6728 optional_vars = NULL;
6729 }
6730 *out = withitem(context_expr, optional_vars, arena);
6731 return 0;
6732failed:
6733 Py_XDECREF(tmp);
6734 return 1;
6735}
6736
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006737
Martin v. Löwis1a214512008-06-11 05:26:20 +00006738static struct PyModuleDef _astmodule = {
6739 PyModuleDef_HEAD_INIT, "_ast"
6740};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006741PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006742PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006743{
6744 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006745 if (!init_types()) return NULL;
6746 m = PyModule_Create(&_astmodule);
6747 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006748 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006749 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6750 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006751 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006752 return NULL;
Benjamin Peterson04a90b42011-05-29 11:45:29 -05006753 if (PyModule_AddStringConstant(m, "__version__", "e0e663132363") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006754 return NULL;
6755 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6756 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006757 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006758 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006759 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006760 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006761 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006762 0) return NULL;
6763 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6764 NULL;
6765 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6766 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006767 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006768 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006769 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006770 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006772 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006773 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006774 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006775 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006776 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006777 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006778 0) return NULL;
6779 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6780 NULL;
6781 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6782 NULL;
6783 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6784 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6785 NULL;
6786 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6787 NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05006788 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
6789 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006790 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006791 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006792 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006793 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006794 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006795 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006796 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006797 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006798 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006799 return NULL;
6800 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6801 NULL;
6802 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6803 NULL;
6804 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6805 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006806 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006807 return NULL;
6808 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6809 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006810 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006811 return NULL;
6812 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6813 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006814 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006815 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006816 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006817 return NULL;
6818 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6819 NULL;
6820 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6821 NULL;
6822 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6823 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006824 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006825 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006826 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006827 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006828 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006829 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006830 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006831 (PyObject*)GeneratorExp_type) < 0) return NULL;
6832 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6833 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006834 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006835 return NULL;
6836 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6837 NULL;
6838 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6839 NULL;
6840 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6841 NULL;
6842 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6843 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006844 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006845 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006846 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006847 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006848 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006849 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006850 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006851 return NULL;
6852 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6853 NULL;
6854 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6855 NULL;
6856 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6857 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006858 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006859 (PyObject*)expr_context_type) < 0) return NULL;
6860 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6861 NULL;
6862 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6863 NULL;
6864 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6865 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006866 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006867 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006868 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006869 return NULL;
6870 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6873 NULL;
6874 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6875 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006876 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006877 return NULL;
6878 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_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, "And", (PyObject*)And_type) < 0) return
6883 NULL;
6884 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006885 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006886 return NULL;
6887 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6888 NULL;
6889 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6890 NULL;
6891 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6892 NULL;
6893 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6894 NULL;
6895 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6896 NULL;
6897 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6898 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006899 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006900 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006901 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006902 return NULL;
6903 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6904 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006905 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006906 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006907 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006908 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006909 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006910 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006911 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006912 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006914 return NULL;
6915 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6916 NULL;
6917 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6918 NULL;
6919 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6920 NULL;
6921 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6924 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6925 NULL;
6926 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6927 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6928 NULL;
6929 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6930 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6931 NULL;
6932 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6933 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6934 NULL;
6935 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6936 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6937 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006938 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006939 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006940 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006941 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006942 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006943 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006944 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006945 0) return NULL;
6946 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6947 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006948 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006949 return NULL;
6950 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6951 NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006952 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6953 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006954 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006955}
6956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006957
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006958PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006959{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006960 init_types();
6961 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006962}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006963
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006964/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6965mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006966{
6967 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006968 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6969 (PyObject*)Interactive_type};
6970 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006971 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006972 assert(0 <= mode && mode <= 2);
6973
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006974 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006975
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006976 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6977 if (isinstance == -1)
6978 return NULL;
6979 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006980 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6981 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006982 return NULL;
6983 }
6984 if (obj2ast_mod(ast, &res, arena) != 0)
6985 return NULL;
6986 else
6987 return res;
6988}
6989
6990int PyAST_Check(PyObject* obj)
6991{
6992 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006993 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006994}
6995
Martin v. Löwis5b222132007-06-10 09:51:05 +00006996