blob: f6e345c9168625be7537f6754576f70648195e94 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003#include "Python.h"
4#include "Python-ast.h"
5
Neal Norwitz207c9f32008-03-31 04:42:11 +00006static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +00007static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +00009static PyTypeObject *Module_type;
10static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000011 "body",
12};
Neal Norwitz53d960c2006-02-28 22:47:29 +000013static PyTypeObject *Interactive_type;
14static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000015 "body",
16};
Neal Norwitz53d960c2006-02-28 22:47:29 +000017static PyTypeObject *Expression_type;
18static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000019 "body",
20};
Neal Norwitz53d960c2006-02-28 22:47:29 +000021static PyTypeObject *Suite_type;
22static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000023 "body",
24};
Neal Norwitz53d960c2006-02-28 22:47:29 +000025static PyTypeObject *stmt_type;
26static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000027 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000028 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000029};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000030static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000031static PyTypeObject *FunctionDef_type;
32static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000033 "name",
34 "args",
35 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000036 "decorator_list",
Neal Norwitzc1505362006-12-28 06:47:50 +000037 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000038};
Neal Norwitz53d960c2006-02-28 22:47:29 +000039static PyTypeObject *ClassDef_type;
40static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000041 "name",
42 "bases",
Guido van Rossum52cc1d82007-03-18 15:41:51 +000043 "keywords",
44 "starargs",
45 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000046 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000047 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000048};
Neal Norwitz53d960c2006-02-28 22:47:29 +000049static PyTypeObject *Return_type;
50static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000051 "value",
52};
Neal Norwitz53d960c2006-02-28 22:47:29 +000053static PyTypeObject *Delete_type;
54static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000055 "targets",
56};
Neal Norwitz53d960c2006-02-28 22:47:29 +000057static PyTypeObject *Assign_type;
58static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000059 "targets",
60 "value",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *AugAssign_type;
63static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "target",
65 "op",
66 "value",
67};
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static PyTypeObject *For_type;
69static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070 "target",
71 "iter",
72 "body",
73 "orelse",
74};
Neal Norwitz53d960c2006-02-28 22:47:29 +000075static PyTypeObject *While_type;
76static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000077 "test",
78 "body",
79 "orelse",
80};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *If_type;
82static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000083 "test",
84 "body",
85 "orelse",
86};
Neal Norwitz53d960c2006-02-28 22:47:29 +000087static PyTypeObject *With_type;
88static char *With_fields[]={
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -050089 "items",
Guido van Rossumc2e20742006-02-27 22:32:47 +000090 "body",
91};
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static PyTypeObject *Raise_type;
93static char *Raise_fields[]={
Collin Winter828f04a2007-08-31 00:04:24 +000094 "exc",
95 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000096};
Benjamin Peterson43af12b2011-05-29 11:43:10 -050097static PyTypeObject *Try_type;
98static char *Try_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000099 "body",
100 "handlers",
101 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000102 "finalbody",
103};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000104static PyTypeObject *Assert_type;
105static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106 "test",
107 "msg",
108};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000109static PyTypeObject *Import_type;
110static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000111 "names",
112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *ImportFrom_type;
114static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115 "module",
116 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000117 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000118};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000119static PyTypeObject *Global_type;
120static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000121 "names",
122};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000123static PyTypeObject *Nonlocal_type;
124static char *Nonlocal_fields[]={
125 "names",
126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *Expr_type;
128static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000129 "value",
130};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000131static PyTypeObject *Pass_type;
132static PyTypeObject *Break_type;
133static PyTypeObject *Continue_type;
134static PyTypeObject *expr_type;
135static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000136 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000137 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000138};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000139static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000140static PyTypeObject *BoolOp_type;
141static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000142 "op",
143 "values",
144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *BinOp_type;
146static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000147 "left",
148 "op",
149 "right",
150};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000151static PyTypeObject *UnaryOp_type;
152static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153 "op",
154 "operand",
155};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static PyTypeObject *Lambda_type;
157static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158 "args",
159 "body",
160};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000161static PyTypeObject *IfExp_type;
162static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000163 "test",
164 "body",
165 "orelse",
166};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000167static PyTypeObject *Dict_type;
168static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000169 "keys",
170 "values",
171};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000172static PyTypeObject *Set_type;
173static char *Set_fields[]={
174 "elts",
175};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000176static PyTypeObject *ListComp_type;
177static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000178 "elt",
179 "generators",
180};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000181static PyTypeObject *SetComp_type;
182static char *SetComp_fields[]={
183 "elt",
184 "generators",
185};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000186static PyTypeObject *DictComp_type;
187static char *DictComp_fields[]={
188 "key",
189 "value",
190 "generators",
191};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000192static PyTypeObject *GeneratorExp_type;
193static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000194 "elt",
195 "generators",
196};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000197static PyTypeObject *Yield_type;
198static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000199 "value",
200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *Compare_type;
202static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000203 "left",
204 "ops",
205 "comparators",
206};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000207static PyTypeObject *Call_type;
208static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000209 "func",
210 "args",
211 "keywords",
212 "starargs",
213 "kwargs",
214};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static PyTypeObject *Num_type;
216static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217 "n",
218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *Str_type;
220static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221 "s",
222};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000223static PyTypeObject *Bytes_type;
224static char *Bytes_fields[]={
225 "s",
226};
Georg Brandl52318d62006-09-06 07:06:08 +0000227static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000228static PyTypeObject *Attribute_type;
229static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230 "value",
231 "attr",
232 "ctx",
233};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000234static PyTypeObject *Subscript_type;
235static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000236 "value",
237 "slice",
238 "ctx",
239};
Guido van Rossum0368b722007-05-11 16:50:42 +0000240static PyTypeObject *Starred_type;
241static char *Starred_fields[]={
242 "value",
243 "ctx",
244};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000245static PyTypeObject *Name_type;
246static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000247 "id",
248 "ctx",
249};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000250static PyTypeObject *List_type;
251static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000252 "elts",
253 "ctx",
254};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000255static PyTypeObject *Tuple_type;
256static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000257 "elts",
258 "ctx",
259};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000260static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000261static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
262*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
263static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *Load_type;
265static PyTypeObject *Store_type;
266static PyTypeObject *Del_type;
267static PyTypeObject *AugLoad_type;
268static PyTypeObject *AugStore_type;
269static PyTypeObject *Param_type;
270static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000272static PyTypeObject *Slice_type;
273static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000274 "lower",
275 "upper",
276 "step",
277};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *ExtSlice_type;
279static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000280 "dims",
281};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000282static PyTypeObject *Index_type;
283static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000284 "value",
285};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000286static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287static PyObject *And_singleton, *Or_singleton;
288static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000289static PyTypeObject *And_type;
290static PyTypeObject *Or_type;
291static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000292static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
293*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
294*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
295*FloorDiv_singleton;
296static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000297static PyTypeObject *Add_type;
298static PyTypeObject *Sub_type;
299static PyTypeObject *Mult_type;
300static PyTypeObject *Div_type;
301static PyTypeObject *Mod_type;
302static PyTypeObject *Pow_type;
303static PyTypeObject *LShift_type;
304static PyTypeObject *RShift_type;
305static PyTypeObject *BitOr_type;
306static PyTypeObject *BitXor_type;
307static PyTypeObject *BitAnd_type;
308static PyTypeObject *FloorDiv_type;
309static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000310static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
311*USub_singleton;
312static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000313static PyTypeObject *Invert_type;
314static PyTypeObject *Not_type;
315static PyTypeObject *UAdd_type;
316static PyTypeObject *USub_type;
317static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000318static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
319*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
320*NotIn_singleton;
321static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000322static PyTypeObject *Eq_type;
323static PyTypeObject *NotEq_type;
324static PyTypeObject *Lt_type;
325static PyTypeObject *LtE_type;
326static PyTypeObject *Gt_type;
327static PyTypeObject *GtE_type;
328static PyTypeObject *Is_type;
329static PyTypeObject *IsNot_type;
330static PyTypeObject *In_type;
331static PyTypeObject *NotIn_type;
332static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000333static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000334static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000335 "target",
336 "iter",
337 "ifs",
338};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000339static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000340static char *excepthandler_attributes[] = {
341 "lineno",
342 "col_offset",
343};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000345static PyTypeObject *ExceptHandler_type;
346static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347 "type",
348 "name",
349 "body",
350};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000351static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000352static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000354 "args",
355 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000356 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000357 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000359 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000360 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000361 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362};
Neal Norwitzc1505362006-12-28 06:47:50 +0000363static PyTypeObject *arg_type;
364static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000365static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000366 "arg",
367 "annotation",
368};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000369static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000370static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000371static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000372 "arg",
373 "value",
374};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000375static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000376static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000377static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000378 "name",
379 "asname",
380};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500381static PyTypeObject *withitem_type;
382static PyObject* ast2obj_withitem(void*);
383static char *withitem_fields[]={
384 "context_expr",
385 "optional_vars",
386};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387
388
Neal Norwitz207c9f32008-03-31 04:42:11 +0000389static int
390ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
391{
392 Py_ssize_t i, numfields = 0;
393 int res = -1;
394 PyObject *key, *value, *fields;
395 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
396 if (!fields)
397 PyErr_Clear();
398 if (fields) {
399 numfields = PySequence_Size(fields);
400 if (numfields == -1)
401 goto cleanup;
402 }
403 res = 0; /* if no error occurs, this stays 0 to the end */
404 if (PyTuple_GET_SIZE(args) > 0) {
405 if (numfields != PyTuple_GET_SIZE(args)) {
406 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000407 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000408 Py_TYPE(self)->tp_name,
409 numfields == 0 ? "" : "either 0 or ",
410 numfields, numfields == 1 ? "" : "s");
411 res = -1;
412 goto cleanup;
413 }
414 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
415 /* cannot be reached when fields is NULL */
416 PyObject *name = PySequence_GetItem(fields, i);
417 if (!name) {
418 res = -1;
419 goto cleanup;
420 }
421 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
422 Py_DECREF(name);
423 if (res < 0)
424 goto cleanup;
425 }
426 }
427 if (kw) {
428 i = 0; /* needed by PyDict_Next */
429 while (PyDict_Next(kw, &i, &key, &value)) {
430 res = PyObject_SetAttr(self, key, value);
431 if (res < 0)
432 goto cleanup;
433 }
434 }
435 cleanup:
436 Py_XDECREF(fields);
437 return res;
438}
439
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000440/* Pickling support */
441static PyObject *
442ast_type_reduce(PyObject *self, PyObject *unused)
443{
444 PyObject *res;
445 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
446 if (dict == NULL) {
447 if (PyErr_ExceptionMatches(PyExc_AttributeError))
448 PyErr_Clear();
449 else
450 return NULL;
451 }
452 if (dict) {
453 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
454 Py_DECREF(dict);
455 return res;
456 }
457 return Py_BuildValue("O()", Py_TYPE(self));
458}
459
460static PyMethodDef ast_type_methods[] = {
461 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
462 {NULL}
463};
464
Neal Norwitz207c9f32008-03-31 04:42:11 +0000465static PyTypeObject AST_type = {
466 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000467 "_ast.AST",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000468 sizeof(PyObject),
469 0,
470 0, /* tp_dealloc */
471 0, /* tp_print */
472 0, /* tp_getattr */
473 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000474 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000475 0, /* tp_repr */
476 0, /* tp_as_number */
477 0, /* tp_as_sequence */
478 0, /* tp_as_mapping */
479 0, /* tp_hash */
480 0, /* tp_call */
481 0, /* tp_str */
482 PyObject_GenericGetAttr, /* tp_getattro */
483 PyObject_GenericSetAttr, /* tp_setattro */
484 0, /* tp_as_buffer */
485 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
486 0, /* tp_doc */
487 0, /* tp_traverse */
488 0, /* tp_clear */
489 0, /* tp_richcompare */
490 0, /* tp_weaklistoffset */
491 0, /* tp_iter */
492 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000493 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000494 0, /* tp_members */
495 0, /* tp_getset */
496 0, /* tp_base */
497 0, /* tp_dict */
498 0, /* tp_descr_get */
499 0, /* tp_descr_set */
500 0, /* tp_dictoffset */
501 (initproc)ast_type_init, /* tp_init */
502 PyType_GenericAlloc, /* tp_alloc */
503 PyType_GenericNew, /* tp_new */
504 PyObject_Del, /* tp_free */
505};
506
507
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000508static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
509{
510 PyObject *fnames, *result;
511 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000512 fnames = PyTuple_New(num_fields);
513 if (!fnames) return NULL;
514 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000515 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000516 if (!field) {
517 Py_DECREF(fnames);
518 return NULL;
519 }
520 PyTuple_SET_ITEM(fnames, i, field);
521 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000522 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000523 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000524 Py_DECREF(fnames);
525 return (PyTypeObject*)result;
526}
527
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000528static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
529{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000531 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000532 if (!l)
533 return 0;
534 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000535 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000536 if (!s) {
537 Py_DECREF(l);
538 return 0;
539 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000540 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000541 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
543 Py_DECREF(l);
544 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000545}
546
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000547/* Conversion AST -> Python */
548
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000549static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
550{
551 int i, n = asdl_seq_LEN(seq);
552 PyObject *result = PyList_New(n);
553 PyObject *value;
554 if (!result)
555 return NULL;
556 for (i = 0; i < n; i++) {
557 value = func(asdl_seq_GET(seq, i));
558 if (!value) {
559 Py_DECREF(result);
560 return NULL;
561 }
562 PyList_SET_ITEM(result, i, value);
563 }
564 return result;
565}
566
567static PyObject* ast2obj_object(void *o)
568{
569 if (!o)
570 o = Py_None;
571 Py_INCREF((PyObject*)o);
572 return (PyObject*)o;
573}
574#define ast2obj_identifier ast2obj_object
575#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500576#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000577
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000578static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000579{
Christian Heimes217cfd12007-12-02 14:31:20 +0000580 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000581}
582
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000583/* Conversion Python -> AST */
584
585static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
586{
587 if (obj == Py_None)
588 obj = NULL;
589 if (obj)
590 PyArena_AddPyObject(arena, obj);
591 Py_XINCREF(obj);
592 *out = obj;
593 return 0;
594}
595
Benjamin Peterson180e6352011-07-22 11:09:07 -0500596static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500597{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500598 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
599 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500600 return 1;
601 }
602 return obj2ast_object(obj, out, arena);
603}
604
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500605static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
606{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500607 if (!PyUnicode_CheckExact(obj)) {
608 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
609 return 1;
610 }
611 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500612}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000613
Benjamin Petersone2498412011-08-09 16:08:39 -0500614static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
615{
616 if (!PyBytes_CheckExact(obj)) {
617 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
618 return 1;
619 }
620 return obj2ast_object(obj, out, arena);
621}
622
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000623static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
624{
625 int i;
626 if (!PyLong_Check(obj)) {
627 PyObject *s = PyObject_Repr(obj);
628 if (s == NULL) return 1;
629 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000630 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000631 Py_DECREF(s);
632 return 1;
633 }
634
635 i = (int)PyLong_AsLong(obj);
636 if (i == -1 && PyErr_Occurred())
637 return 1;
638 *out = i;
639 return 0;
640}
641
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000642static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000643{
644 PyObject *empty_tuple, *d;
645 if (PyType_Ready(&AST_type) < 0)
646 return -1;
647 d = AST_type.tp_dict;
648 empty_tuple = PyTuple_New(0);
649 if (!empty_tuple ||
650 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
651 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
652 Py_XDECREF(empty_tuple);
653 return -1;
654 }
655 Py_DECREF(empty_tuple);
656 return 0;
657}
658
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000659
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660static int init_types(void)
661{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000662 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000664 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000665 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 if (!mod_type) return 0;
667 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Interactive_type = make_type("Interactive", mod_type,
671 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 Expression_type = make_type("Expression", mod_type, Expression_fields,
674 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000678 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000680 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000682 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000684 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!If_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500700 With_type = make_type("With", stmt_type, With_fields, 2);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000701 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000702 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Raise_type) return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500704 Try_type = make_type("Try", stmt_type, Try_fields, 4);
705 if (!Try_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000706 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000710 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000711 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000715 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
716 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000719 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000721 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000723 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000725 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000727 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000736 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000740 Set_type = make_type("Set", expr_type, Set_fields, 1);
741 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000744 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
745 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000746 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
747 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 GeneratorExp_type = make_type("GeneratorExp", expr_type,
749 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000753 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000759 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000761 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
762 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000763 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
764 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000769 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
770 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000771 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000777 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!expr_context_type) return 0;
779 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000780 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000788 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000790 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000792 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000794 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000796 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000798 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000800 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000804 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!slice_type) return 0;
806 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000807 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000809 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000810 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000811 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000813 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!boolop_type) return 0;
815 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000816 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000818 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000820 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000822 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000824 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!operator_type) return 0;
826 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000827 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000829 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000831 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000833 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000835 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000837 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000839 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000841 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000843 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000845 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000847 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000849 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000851 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000853 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000855 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000857 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000859 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000861 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000863 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000865 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000867 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000869 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000871 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000873 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000875 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!unaryop_type) return 0;
877 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000878 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000880 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000882 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000884 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000886 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000888 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000890 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000892 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000894 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!cmpop_type) return 0;
896 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000897 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000898 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000899 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000900 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000901 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000902 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000903 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000904 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000905 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000906 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000907 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000908 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000909 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000910 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000911 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000913 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000915 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000917 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000919 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000921 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000922 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000923 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000924 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000925 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000926 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000927 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000928 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000929 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000930 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000931 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000932 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000933 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000934 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000935 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000936 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000937 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000938 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000939 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000940 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000941 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000942 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
943 return 0;
944 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
945 ExceptHandler_fields, 3);
946 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000947 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000948 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000949 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000950 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000951 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000952 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000953 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000954 if (!alias_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500955 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
956 if (!withitem_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000957 initialized = 1;
958 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000959}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000960
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000961static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
962static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
963static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
964static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
965 arena);
966static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
967static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
968static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
969static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
970static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
971static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
972 arena);
973static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
974 arena);
975static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
976static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
977static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
978static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500979static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000982Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983{
984 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000985 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 p->kind = Module_kind;
989 p->v.Module.body = body;
990 return p;
991}
992
993mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995{
996 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000998 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 p->kind = Interactive_kind;
1001 p->v.Interactive.body = body;
1002 return p;
1003}
1004
1005mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001006Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007{
1008 mod_ty p;
1009 if (!body) {
1010 PyErr_SetString(PyExc_ValueError,
1011 "field body is required for Expression");
1012 return NULL;
1013 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001014 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001015 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 p->kind = Expression_kind;
1018 p->v.Expression.body = body;
1019 return p;
1020}
1021
1022mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001023Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024{
1025 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001026 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001027 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 p->kind = Suite_kind;
1030 p->v.Suite.body = body;
1031 return p;
1032}
1033
1034stmt_ty
1035FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001036 decorator_list, expr_ty returns, int lineno, int col_offset,
1037 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038{
1039 stmt_ty p;
1040 if (!name) {
1041 PyErr_SetString(PyExc_ValueError,
1042 "field name is required for FunctionDef");
1043 return NULL;
1044 }
1045 if (!args) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field args is required for FunctionDef");
1048 return NULL;
1049 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001050 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001051 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 p->kind = FunctionDef_kind;
1054 p->v.FunctionDef.name = name;
1055 p->v.FunctionDef.args = args;
1056 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001057 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001058 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001060 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 return p;
1062}
1063
1064stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001065ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001066 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1067 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068{
1069 stmt_ty p;
1070 if (!name) {
1071 PyErr_SetString(PyExc_ValueError,
1072 "field name is required for ClassDef");
1073 return NULL;
1074 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001075 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001076 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 p->kind = ClassDef_kind;
1079 p->v.ClassDef.name = name;
1080 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001081 p->v.ClassDef.keywords = keywords;
1082 p->v.ClassDef.starargs = starargs;
1083 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001085 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001087 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return p;
1089}
1090
1091stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001092Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
1094 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001095 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001096 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 p->kind = Return_kind;
1099 p->v.Return.value = value;
1100 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001101 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return p;
1103}
1104
1105stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107{
1108 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001109 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001110 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 p->kind = Delete_kind;
1113 p->v.Delete.targets = targets;
1114 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001115 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return p;
1117}
1118
1119stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1121 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
1123 stmt_ty p;
1124 if (!value) {
1125 PyErr_SetString(PyExc_ValueError,
1126 "field value is required for Assign");
1127 return NULL;
1128 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001130 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 p->kind = Assign_kind;
1133 p->v.Assign.targets = targets;
1134 p->v.Assign.value = value;
1135 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return p;
1138}
1139
1140stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1142 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143{
1144 stmt_ty p;
1145 if (!target) {
1146 PyErr_SetString(PyExc_ValueError,
1147 "field target is required for AugAssign");
1148 return NULL;
1149 }
1150 if (!op) {
1151 PyErr_SetString(PyExc_ValueError,
1152 "field op is required for AugAssign");
1153 return NULL;
1154 }
1155 if (!value) {
1156 PyErr_SetString(PyExc_ValueError,
1157 "field value is required for AugAssign");
1158 return NULL;
1159 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001160 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001161 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 p->kind = AugAssign_kind;
1164 p->v.AugAssign.target = target;
1165 p->v.AugAssign.op = op;
1166 p->v.AugAssign.value = value;
1167 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001168 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return p;
1170}
1171
1172stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
1176 stmt_ty p;
1177 if (!target) {
1178 PyErr_SetString(PyExc_ValueError,
1179 "field target is required for For");
1180 return NULL;
1181 }
1182 if (!iter) {
1183 PyErr_SetString(PyExc_ValueError,
1184 "field iter is required for For");
1185 return NULL;
1186 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001188 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 p->kind = For_kind;
1191 p->v.For.target = target;
1192 p->v.For.iter = iter;
1193 p->v.For.body = body;
1194 p->v.For.orelse = orelse;
1195 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001196 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return p;
1198}
1199
1200stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001201While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1202 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
1204 stmt_ty p;
1205 if (!test) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "field test is required for While");
1208 return NULL;
1209 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001211 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 p->kind = While_kind;
1214 p->v.While.test = test;
1215 p->v.While.body = body;
1216 p->v.While.orelse = orelse;
1217 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 return p;
1220}
1221
1222stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1224 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225{
1226 stmt_ty p;
1227 if (!test) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "field test is required for If");
1230 return NULL;
1231 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001233 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 p->kind = If_kind;
1236 p->v.If.test = test;
1237 p->v.If.body = body;
1238 p->v.If.orelse = orelse;
1239 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return p;
1242}
1243
1244stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001245With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1246 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001247{
1248 stmt_ty p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001249 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001250 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001251 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001252 p->kind = With_kind;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001253 p->v.With.items = items;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001254 p->v.With.body = body;
1255 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001256 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001257 return p;
1258}
1259
1260stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001261Raise(expr_ty exc, expr_ty cause, 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001268 p->v.Raise.exc = exc;
1269 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001271 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return p;
1273}
1274
1275stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001276Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1277 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278{
1279 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001280 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001281 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 return NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001283 p->kind = Try_kind;
1284 p->v.Try.body = body;
1285 p->v.Try.handlers = handlers;
1286 p->v.Try.orelse = orelse;
1287 p->v.Try.finalbody = finalbody;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001289 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 return p;
1291}
1292
1293stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001294Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295{
1296 stmt_ty p;
1297 if (!test) {
1298 PyErr_SetString(PyExc_ValueError,
1299 "field test is required for Assert");
1300 return NULL;
1301 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001302 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001303 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 p->kind = Assert_kind;
1306 p->v.Assert.test = test;
1307 p->v.Assert.msg = msg;
1308 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001309 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return p;
1311}
1312
1313stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001314Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315{
1316 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001317 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001318 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 p->kind = Import_kind;
1321 p->v.Import.names = names;
1322 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001323 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return p;
1325}
1326
1327stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001328ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1329 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 = ImportFrom_kind;
1336 p->v.ImportFrom.module = module;
1337 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001338 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001340 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return p;
1342}
1343
1344stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001345Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346{
1347 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001348 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001349 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 p->kind = Global_kind;
1352 p->v.Global.names = names;
1353 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001354 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 return p;
1356}
1357
1358stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001359Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1360{
1361 stmt_ty p;
1362 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1363 if (!p)
1364 return NULL;
1365 p->kind = Nonlocal_kind;
1366 p->v.Nonlocal.names = names;
1367 p->lineno = lineno;
1368 p->col_offset = col_offset;
1369 return p;
1370}
1371
1372stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001373Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374{
1375 stmt_ty p;
1376 if (!value) {
1377 PyErr_SetString(PyExc_ValueError,
1378 "field value is required for Expr");
1379 return NULL;
1380 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001381 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001382 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 p->kind = Expr_kind;
1385 p->v.Expr.value = value;
1386 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001387 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 return p;
1389}
1390
1391stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001392Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393{
1394 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001395 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001396 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 p->kind = Pass_kind;
1399 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001400 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 return p;
1402}
1403
1404stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001405Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406{
1407 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001408 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001409 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 p->kind = Break_kind;
1412 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001413 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 return p;
1415}
1416
1417stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001418Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419{
1420 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001421 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001422 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 p->kind = Continue_kind;
1425 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 return p;
1428}
1429
1430expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1432 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433{
1434 expr_ty p;
1435 if (!op) {
1436 PyErr_SetString(PyExc_ValueError,
1437 "field op is required for BoolOp");
1438 return NULL;
1439 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001440 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001441 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 p->kind = BoolOp_kind;
1444 p->v.BoolOp.op = op;
1445 p->v.BoolOp.values = values;
1446 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001447 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return p;
1449}
1450
1451expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001452BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1453 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
1455 expr_ty p;
1456 if (!left) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "field left is required for BinOp");
1459 return NULL;
1460 }
1461 if (!op) {
1462 PyErr_SetString(PyExc_ValueError,
1463 "field op is required for BinOp");
1464 return NULL;
1465 }
1466 if (!right) {
1467 PyErr_SetString(PyExc_ValueError,
1468 "field right is required for BinOp");
1469 return NULL;
1470 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001471 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001472 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 p->kind = BinOp_kind;
1475 p->v.BinOp.left = left;
1476 p->v.BinOp.op = op;
1477 p->v.BinOp.right = right;
1478 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 return p;
1481}
1482
1483expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001484UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1485 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
1487 expr_ty p;
1488 if (!op) {
1489 PyErr_SetString(PyExc_ValueError,
1490 "field op is required for UnaryOp");
1491 return NULL;
1492 }
1493 if (!operand) {
1494 PyErr_SetString(PyExc_ValueError,
1495 "field operand is required for UnaryOp");
1496 return NULL;
1497 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001498 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001499 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 p->kind = UnaryOp_kind;
1502 p->v.UnaryOp.op = op;
1503 p->v.UnaryOp.operand = operand;
1504 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 return p;
1507}
1508
1509expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1511 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512{
1513 expr_ty p;
1514 if (!args) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field args is required for Lambda");
1517 return NULL;
1518 }
1519 if (!body) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field body is required for Lambda");
1522 return NULL;
1523 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001524 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001525 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 p->kind = Lambda_kind;
1528 p->v.Lambda.args = args;
1529 p->v.Lambda.body = body;
1530 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001531 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 return p;
1533}
1534
1535expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001536IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1537 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001538{
1539 expr_ty p;
1540 if (!test) {
1541 PyErr_SetString(PyExc_ValueError,
1542 "field test is required for IfExp");
1543 return NULL;
1544 }
1545 if (!body) {
1546 PyErr_SetString(PyExc_ValueError,
1547 "field body is required for IfExp");
1548 return NULL;
1549 }
1550 if (!orelse) {
1551 PyErr_SetString(PyExc_ValueError,
1552 "field orelse is required for IfExp");
1553 return NULL;
1554 }
1555 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001556 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001557 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001558 p->kind = IfExp_kind;
1559 p->v.IfExp.test = test;
1560 p->v.IfExp.body = body;
1561 p->v.IfExp.orelse = orelse;
1562 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001563 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001564 return p;
1565}
1566
1567expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001568Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1569 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570{
1571 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001572 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001573 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 p->kind = Dict_kind;
1576 p->v.Dict.keys = keys;
1577 p->v.Dict.values = values;
1578 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001579 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 return p;
1581}
1582
1583expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001584Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1585{
1586 expr_ty p;
1587 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001588 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001589 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001590 p->kind = Set_kind;
1591 p->v.Set.elts = elts;
1592 p->lineno = lineno;
1593 p->col_offset = col_offset;
1594 return p;
1595}
1596
1597expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001598ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1599 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600{
1601 expr_ty p;
1602 if (!elt) {
1603 PyErr_SetString(PyExc_ValueError,
1604 "field elt is required for ListComp");
1605 return NULL;
1606 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001607 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001608 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 p->kind = ListComp_kind;
1611 p->v.ListComp.elt = elt;
1612 p->v.ListComp.generators = generators;
1613 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001614 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 return p;
1616}
1617
1618expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001619SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1620 *arena)
1621{
1622 expr_ty p;
1623 if (!elt) {
1624 PyErr_SetString(PyExc_ValueError,
1625 "field elt is required for SetComp");
1626 return NULL;
1627 }
1628 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1629 if (!p)
1630 return NULL;
1631 p->kind = SetComp_kind;
1632 p->v.SetComp.elt = elt;
1633 p->v.SetComp.generators = generators;
1634 p->lineno = lineno;
1635 p->col_offset = col_offset;
1636 return p;
1637}
1638
1639expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001640DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1641 col_offset, PyArena *arena)
1642{
1643 expr_ty p;
1644 if (!key) {
1645 PyErr_SetString(PyExc_ValueError,
1646 "field key is required for DictComp");
1647 return NULL;
1648 }
1649 if (!value) {
1650 PyErr_SetString(PyExc_ValueError,
1651 "field value is required for DictComp");
1652 return NULL;
1653 }
1654 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1655 if (!p)
1656 return NULL;
1657 p->kind = DictComp_kind;
1658 p->v.DictComp.key = key;
1659 p->v.DictComp.value = value;
1660 p->v.DictComp.generators = generators;
1661 p->lineno = lineno;
1662 p->col_offset = col_offset;
1663 return p;
1664}
1665
1666expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001667GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1668 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669{
1670 expr_ty p;
1671 if (!elt) {
1672 PyErr_SetString(PyExc_ValueError,
1673 "field elt is required for GeneratorExp");
1674 return NULL;
1675 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001676 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001677 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 p->kind = GeneratorExp_kind;
1680 p->v.GeneratorExp.elt = elt;
1681 p->v.GeneratorExp.generators = generators;
1682 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001683 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 return p;
1685}
1686
1687expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689{
1690 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001691 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001692 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 p->kind = Yield_kind;
1695 p->v.Yield.value = value;
1696 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001697 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 return p;
1699}
1700
1701expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001702Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1703 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704{
1705 expr_ty p;
1706 if (!left) {
1707 PyErr_SetString(PyExc_ValueError,
1708 "field left is required for Compare");
1709 return NULL;
1710 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001711 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001712 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 p->kind = Compare_kind;
1715 p->v.Compare.left = left;
1716 p->v.Compare.ops = ops;
1717 p->v.Compare.comparators = comparators;
1718 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001719 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 return p;
1721}
1722
1723expr_ty
1724Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001725 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726{
1727 expr_ty p;
1728 if (!func) {
1729 PyErr_SetString(PyExc_ValueError,
1730 "field func is required for Call");
1731 return NULL;
1732 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001734 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 p->kind = Call_kind;
1737 p->v.Call.func = func;
1738 p->v.Call.args = args;
1739 p->v.Call.keywords = keywords;
1740 p->v.Call.starargs = starargs;
1741 p->v.Call.kwargs = kwargs;
1742 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001743 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 return p;
1745}
1746
1747expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001748Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749{
1750 expr_ty p;
1751 if (!n) {
1752 PyErr_SetString(PyExc_ValueError,
1753 "field n is required for Num");
1754 return NULL;
1755 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001756 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001757 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 p->kind = Num_kind;
1760 p->v.Num.n = n;
1761 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001762 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 return p;
1764}
1765
1766expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001767Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768{
1769 expr_ty p;
1770 if (!s) {
1771 PyErr_SetString(PyExc_ValueError,
1772 "field s is required for Str");
1773 return NULL;
1774 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001775 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001776 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778 p->kind = Str_kind;
1779 p->v.Str.s = s;
1780 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001781 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return p;
1783}
1784
1785expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001786Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001787{
1788 expr_ty p;
1789 if (!s) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field s is required for Bytes");
1792 return NULL;
1793 }
1794 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001795 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001796 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001797 p->kind = Bytes_kind;
1798 p->v.Bytes.s = s;
1799 p->lineno = lineno;
1800 p->col_offset = col_offset;
1801 return p;
1802}
1803
1804expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001805Ellipsis(int lineno, int col_offset, PyArena *arena)
1806{
1807 expr_ty p;
1808 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001809 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001810 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001811 p->kind = Ellipsis_kind;
1812 p->lineno = lineno;
1813 p->col_offset = col_offset;
1814 return p;
1815}
1816
1817expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001818Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1819 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820{
1821 expr_ty p;
1822 if (!value) {
1823 PyErr_SetString(PyExc_ValueError,
1824 "field value is required for Attribute");
1825 return NULL;
1826 }
1827 if (!attr) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "field attr is required for Attribute");
1830 return NULL;
1831 }
1832 if (!ctx) {
1833 PyErr_SetString(PyExc_ValueError,
1834 "field ctx is required for Attribute");
1835 return NULL;
1836 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001838 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 p->kind = Attribute_kind;
1841 p->v.Attribute.value = value;
1842 p->v.Attribute.attr = attr;
1843 p->v.Attribute.ctx = ctx;
1844 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001845 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return p;
1847}
1848
1849expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001850Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1851 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852{
1853 expr_ty p;
1854 if (!value) {
1855 PyErr_SetString(PyExc_ValueError,
1856 "field value is required for Subscript");
1857 return NULL;
1858 }
1859 if (!slice) {
1860 PyErr_SetString(PyExc_ValueError,
1861 "field slice is required for Subscript");
1862 return NULL;
1863 }
1864 if (!ctx) {
1865 PyErr_SetString(PyExc_ValueError,
1866 "field ctx is required for Subscript");
1867 return NULL;
1868 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001870 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 p->kind = Subscript_kind;
1873 p->v.Subscript.value = value;
1874 p->v.Subscript.slice = slice;
1875 p->v.Subscript.ctx = ctx;
1876 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001877 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 return p;
1879}
1880
1881expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001882Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1883 *arena)
1884{
1885 expr_ty p;
1886 if (!value) {
1887 PyErr_SetString(PyExc_ValueError,
1888 "field value is required for Starred");
1889 return NULL;
1890 }
1891 if (!ctx) {
1892 PyErr_SetString(PyExc_ValueError,
1893 "field ctx is required for Starred");
1894 return NULL;
1895 }
1896 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1897 if (!p)
1898 return NULL;
1899 p->kind = Starred_kind;
1900 p->v.Starred.value = value;
1901 p->v.Starred.ctx = ctx;
1902 p->lineno = lineno;
1903 p->col_offset = col_offset;
1904 return p;
1905}
1906
1907expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001908Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1909 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910{
1911 expr_ty p;
1912 if (!id) {
1913 PyErr_SetString(PyExc_ValueError,
1914 "field id is required for Name");
1915 return NULL;
1916 }
1917 if (!ctx) {
1918 PyErr_SetString(PyExc_ValueError,
1919 "field ctx is required for Name");
1920 return NULL;
1921 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001922 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001923 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 p->kind = Name_kind;
1926 p->v.Name.id = id;
1927 p->v.Name.ctx = ctx;
1928 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001929 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 return p;
1931}
1932
1933expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001934List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1935 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936{
1937 expr_ty p;
1938 if (!ctx) {
1939 PyErr_SetString(PyExc_ValueError,
1940 "field ctx is required for List");
1941 return NULL;
1942 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001943 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001944 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 p->kind = List_kind;
1947 p->v.List.elts = elts;
1948 p->v.List.ctx = ctx;
1949 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001950 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 return p;
1952}
1953
1954expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001955Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1956 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957{
1958 expr_ty p;
1959 if (!ctx) {
1960 PyErr_SetString(PyExc_ValueError,
1961 "field ctx is required for Tuple");
1962 return NULL;
1963 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001965 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 p->kind = Tuple_kind;
1968 p->v.Tuple.elts = elts;
1969 p->v.Tuple.ctx = ctx;
1970 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001971 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return p;
1973}
1974
1975slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001976Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977{
1978 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001979 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001980 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 p->kind = Slice_kind;
1983 p->v.Slice.lower = lower;
1984 p->v.Slice.upper = upper;
1985 p->v.Slice.step = step;
1986 return p;
1987}
1988
1989slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001990ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991{
1992 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001994 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 p->kind = ExtSlice_kind;
1997 p->v.ExtSlice.dims = dims;
1998 return p;
1999}
2000
2001slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002002Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
2004 slice_ty p;
2005 if (!value) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field value is required for Index");
2008 return NULL;
2009 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002011 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 p->kind = Index_kind;
2014 p->v.Index.value = value;
2015 return p;
2016}
2017
2018comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002019comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020{
2021 comprehension_ty p;
2022 if (!target) {
2023 PyErr_SetString(PyExc_ValueError,
2024 "field target is required for comprehension");
2025 return NULL;
2026 }
2027 if (!iter) {
2028 PyErr_SetString(PyExc_ValueError,
2029 "field iter is required for comprehension");
2030 return NULL;
2031 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002032 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002033 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 p->target = target;
2036 p->iter = iter;
2037 p->ifs = ifs;
2038 return p;
2039}
2040
2041excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002042ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002043 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044{
2045 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002046 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002047 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002049 p->kind = ExceptHandler_kind;
2050 p->v.ExceptHandler.type = type;
2051 p->v.ExceptHandler.name = name;
2052 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002053 p->lineno = lineno;
2054 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 return p;
2056}
2057
2058arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002059arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2060 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2061 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062{
2063 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002064 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002065 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 p->args = args;
2068 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002069 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002070 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002072 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002074 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 return p;
2076}
2077
Neal Norwitzc1505362006-12-28 06:47:50 +00002078arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002079arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002080{
2081 arg_ty p;
2082 if (!arg) {
2083 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002084 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002085 return NULL;
2086 }
2087 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002088 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002089 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002090 p->arg = arg;
2091 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002092 return p;
2093}
2094
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002096keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097{
2098 keyword_ty p;
2099 if (!arg) {
2100 PyErr_SetString(PyExc_ValueError,
2101 "field arg is required for keyword");
2102 return NULL;
2103 }
2104 if (!value) {
2105 PyErr_SetString(PyExc_ValueError,
2106 "field value is required for keyword");
2107 return NULL;
2108 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002109 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002110 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 p->arg = arg;
2113 p->value = value;
2114 return p;
2115}
2116
2117alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002118alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119{
2120 alias_ty p;
2121 if (!name) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "field name is required for alias");
2124 return NULL;
2125 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002126 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002127 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 p->name = name;
2130 p->asname = asname;
2131 return p;
2132}
2133
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002134withitem_ty
2135withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2136{
2137 withitem_ty p;
2138 if (!context_expr) {
2139 PyErr_SetString(PyExc_ValueError,
2140 "field context_expr is required for withitem");
2141 return NULL;
2142 }
2143 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2144 if (!p)
2145 return NULL;
2146 p->context_expr = context_expr;
2147 p->optional_vars = optional_vars;
2148 return p;
2149}
2150
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002151
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002152PyObject*
2153ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002154{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002155 mod_ty o = (mod_ty)_o;
2156 PyObject *result = NULL, *value = NULL;
2157 if (!o) {
2158 Py_INCREF(Py_None);
2159 return Py_None;
2160 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 switch (o->kind) {
2163 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002164 result = PyType_GenericNew(Module_type, NULL, NULL);
2165 if (!result) goto failed;
2166 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2167 if (!value) goto failed;
2168 if (PyObject_SetAttrString(result, "body", value) == -1)
2169 goto failed;
2170 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 break;
2172 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002173 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2174 if (!result) goto failed;
2175 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2176 if (!value) goto failed;
2177 if (PyObject_SetAttrString(result, "body", value) == -1)
2178 goto failed;
2179 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 break;
2181 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002182 result = PyType_GenericNew(Expression_type, NULL, NULL);
2183 if (!result) goto failed;
2184 value = ast2obj_expr(o->v.Expression.body);
2185 if (!value) goto failed;
2186 if (PyObject_SetAttrString(result, "body", value) == -1)
2187 goto failed;
2188 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 break;
2190 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002191 result = PyType_GenericNew(Suite_type, NULL, NULL);
2192 if (!result) goto failed;
2193 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2194 if (!value) goto failed;
2195 if (PyObject_SetAttrString(result, "body", value) == -1)
2196 goto failed;
2197 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 break;
2199 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002200 return result;
2201failed:
2202 Py_XDECREF(value);
2203 Py_XDECREF(result);
2204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205}
2206
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002207PyObject*
2208ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002210 stmt_ty o = (stmt_ty)_o;
2211 PyObject *result = NULL, *value = NULL;
2212 if (!o) {
2213 Py_INCREF(Py_None);
2214 return Py_None;
2215 }
2216
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217 switch (o->kind) {
2218 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002219 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2220 if (!result) goto failed;
2221 value = ast2obj_identifier(o->v.FunctionDef.name);
2222 if (!value) goto failed;
2223 if (PyObject_SetAttrString(result, "name", value) == -1)
2224 goto failed;
2225 Py_DECREF(value);
2226 value = ast2obj_arguments(o->v.FunctionDef.args);
2227 if (!value) goto failed;
2228 if (PyObject_SetAttrString(result, "args", value) == -1)
2229 goto failed;
2230 Py_DECREF(value);
2231 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2232 if (!value) goto failed;
2233 if (PyObject_SetAttrString(result, "body", value) == -1)
2234 goto failed;
2235 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002236 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2237 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002238 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002239 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2240 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002241 goto failed;
2242 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002243 value = ast2obj_expr(o->v.FunctionDef.returns);
2244 if (!value) goto failed;
2245 if (PyObject_SetAttrString(result, "returns", value) == -1)
2246 goto failed;
2247 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 break;
2249 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002250 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2251 if (!result) goto failed;
2252 value = ast2obj_identifier(o->v.ClassDef.name);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "name", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
2257 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "bases", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002262 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2263 if (!value) goto failed;
2264 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2265 goto failed;
2266 Py_DECREF(value);
2267 value = ast2obj_expr(o->v.ClassDef.starargs);
2268 if (!value) goto failed;
2269 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2270 goto failed;
2271 Py_DECREF(value);
2272 value = ast2obj_expr(o->v.ClassDef.kwargs);
2273 if (!value) goto failed;
2274 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2275 goto failed;
2276 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002277 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "body", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002282 value = ast2obj_list(o->v.ClassDef.decorator_list,
2283 ast2obj_expr);
2284 if (!value) goto failed;
2285 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2286 -1)
2287 goto failed;
2288 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 break;
2290 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002291 result = PyType_GenericNew(Return_type, NULL, NULL);
2292 if (!result) goto failed;
2293 value = ast2obj_expr(o->v.Return.value);
2294 if (!value) goto failed;
2295 if (PyObject_SetAttrString(result, "value", value) == -1)
2296 goto failed;
2297 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 break;
2299 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002300 result = PyType_GenericNew(Delete_type, NULL, NULL);
2301 if (!result) goto failed;
2302 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2303 if (!value) goto failed;
2304 if (PyObject_SetAttrString(result, "targets", value) == -1)
2305 goto failed;
2306 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 break;
2308 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002309 result = PyType_GenericNew(Assign_type, NULL, NULL);
2310 if (!result) goto failed;
2311 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2312 if (!value) goto failed;
2313 if (PyObject_SetAttrString(result, "targets", value) == -1)
2314 goto failed;
2315 Py_DECREF(value);
2316 value = ast2obj_expr(o->v.Assign.value);
2317 if (!value) goto failed;
2318 if (PyObject_SetAttrString(result, "value", value) == -1)
2319 goto failed;
2320 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 break;
2322 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002323 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2324 if (!result) goto failed;
2325 value = ast2obj_expr(o->v.AugAssign.target);
2326 if (!value) goto failed;
2327 if (PyObject_SetAttrString(result, "target", value) == -1)
2328 goto failed;
2329 Py_DECREF(value);
2330 value = ast2obj_operator(o->v.AugAssign.op);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "op", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_expr(o->v.AugAssign.value);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "value", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002342 result = PyType_GenericNew(For_type, NULL, NULL);
2343 if (!result) goto failed;
2344 value = ast2obj_expr(o->v.For.target);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "target", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_expr(o->v.For.iter);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "iter", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
2354 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2355 if (!value) goto failed;
2356 if (PyObject_SetAttrString(result, "body", value) == -1)
2357 goto failed;
2358 Py_DECREF(value);
2359 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 break;
2365 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002366 result = PyType_GenericNew(While_type, NULL, NULL);
2367 if (!result) goto failed;
2368 value = ast2obj_expr(o->v.While.test);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "test", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
2373 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2374 if (!value) goto failed;
2375 if (PyObject_SetAttrString(result, "body", value) == -1)
2376 goto failed;
2377 Py_DECREF(value);
2378 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2379 if (!value) goto failed;
2380 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2381 goto failed;
2382 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 break;
2384 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002385 result = PyType_GenericNew(If_type, NULL, NULL);
2386 if (!result) goto failed;
2387 value = ast2obj_expr(o->v.If.test);
2388 if (!value) goto failed;
2389 if (PyObject_SetAttrString(result, "test", value) == -1)
2390 goto failed;
2391 Py_DECREF(value);
2392 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2393 if (!value) goto failed;
2394 if (PyObject_SetAttrString(result, "body", value) == -1)
2395 goto failed;
2396 Py_DECREF(value);
2397 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2398 if (!value) goto failed;
2399 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2400 goto failed;
2401 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002403 case With_kind:
2404 result = PyType_GenericNew(With_type, NULL, NULL);
2405 if (!result) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002406 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002407 if (!value) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002408 if (PyObject_SetAttrString(result, "items", value) == -1)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002409 goto failed;
2410 Py_DECREF(value);
2411 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2412 if (!value) goto failed;
2413 if (PyObject_SetAttrString(result, "body", value) == -1)
2414 goto failed;
2415 Py_DECREF(value);
2416 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002418 result = PyType_GenericNew(Raise_type, NULL, NULL);
2419 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002420 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002421 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002422 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002423 goto failed;
2424 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002425 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002427 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002428 goto failed;
2429 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002431 case Try_kind:
2432 result = PyType_GenericNew(Try_type, NULL, NULL);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002433 if (!result) goto failed;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002434 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002435 if (!value) goto failed;
2436 if (PyObject_SetAttrString(result, "body", value) == -1)
2437 goto failed;
2438 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002439 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 if (!value) goto failed;
2441 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2442 goto failed;
2443 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002444 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002445 if (!value) goto failed;
2446 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2447 goto failed;
2448 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002449 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002450 if (!value) goto failed;
2451 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2452 goto failed;
2453 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 break;
2455 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002456 result = PyType_GenericNew(Assert_type, NULL, NULL);
2457 if (!result) goto failed;
2458 value = ast2obj_expr(o->v.Assert.test);
2459 if (!value) goto failed;
2460 if (PyObject_SetAttrString(result, "test", value) == -1)
2461 goto failed;
2462 Py_DECREF(value);
2463 value = ast2obj_expr(o->v.Assert.msg);
2464 if (!value) goto failed;
2465 if (PyObject_SetAttrString(result, "msg", value) == -1)
2466 goto failed;
2467 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 break;
2469 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002470 result = PyType_GenericNew(Import_type, NULL, NULL);
2471 if (!result) goto failed;
2472 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2473 if (!value) goto failed;
2474 if (PyObject_SetAttrString(result, "names", value) == -1)
2475 goto failed;
2476 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 break;
2478 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002479 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2480 if (!result) goto failed;
2481 value = ast2obj_identifier(o->v.ImportFrom.module);
2482 if (!value) goto failed;
2483 if (PyObject_SetAttrString(result, "module", value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
2486 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2487 if (!value) goto failed;
2488 if (PyObject_SetAttrString(result, "names", value) == -1)
2489 goto failed;
2490 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002491 value = ast2obj_int(o->v.ImportFrom.level);
2492 if (!value) goto failed;
2493 if (PyObject_SetAttrString(result, "level", value) == -1)
2494 goto failed;
2495 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002498 result = PyType_GenericNew(Global_type, NULL, NULL);
2499 if (!result) goto failed;
2500 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2501 if (!value) goto failed;
2502 if (PyObject_SetAttrString(result, "names", value) == -1)
2503 goto failed;
2504 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002506 case Nonlocal_kind:
2507 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2508 if (!result) goto failed;
2509 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2510 if (!value) goto failed;
2511 if (PyObject_SetAttrString(result, "names", value) == -1)
2512 goto failed;
2513 Py_DECREF(value);
2514 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002516 result = PyType_GenericNew(Expr_type, NULL, NULL);
2517 if (!result) goto failed;
2518 value = ast2obj_expr(o->v.Expr.value);
2519 if (!value) goto failed;
2520 if (PyObject_SetAttrString(result, "value", value) == -1)
2521 goto failed;
2522 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 break;
2524 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002525 result = PyType_GenericNew(Pass_type, NULL, NULL);
2526 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 break;
2528 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002529 result = PyType_GenericNew(Break_type, NULL, NULL);
2530 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 break;
2532 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002533 result = PyType_GenericNew(Continue_type, NULL, NULL);
2534 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 break;
2536 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002537 value = ast2obj_int(o->lineno);
2538 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002539 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2540 goto failed;
2541 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002542 value = ast2obj_int(o->col_offset);
2543 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002544 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2545 goto failed;
2546 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547 return result;
2548failed:
2549 Py_XDECREF(value);
2550 Py_XDECREF(result);
2551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002554PyObject*
2555ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002557 expr_ty o = (expr_ty)_o;
2558 PyObject *result = NULL, *value = NULL;
2559 if (!o) {
2560 Py_INCREF(Py_None);
2561 return Py_None;
2562 }
2563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 switch (o->kind) {
2565 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002566 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2567 if (!result) goto failed;
2568 value = ast2obj_boolop(o->v.BoolOp.op);
2569 if (!value) goto failed;
2570 if (PyObject_SetAttrString(result, "op", value) == -1)
2571 goto failed;
2572 Py_DECREF(value);
2573 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2574 if (!value) goto failed;
2575 if (PyObject_SetAttrString(result, "values", value) == -1)
2576 goto failed;
2577 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 break;
2579 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002580 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2581 if (!result) goto failed;
2582 value = ast2obj_expr(o->v.BinOp.left);
2583 if (!value) goto failed;
2584 if (PyObject_SetAttrString(result, "left", value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
2587 value = ast2obj_operator(o->v.BinOp.op);
2588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "op", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 value = ast2obj_expr(o->v.BinOp.right);
2593 if (!value) goto failed;
2594 if (PyObject_SetAttrString(result, "right", value) == -1)
2595 goto failed;
2596 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 break;
2598 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002599 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2600 if (!result) goto failed;
2601 value = ast2obj_unaryop(o->v.UnaryOp.op);
2602 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "op", value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 value = ast2obj_expr(o->v.UnaryOp.operand);
2607 if (!value) goto failed;
2608 if (PyObject_SetAttrString(result, "operand", value) == -1)
2609 goto failed;
2610 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 break;
2612 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002613 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2614 if (!result) goto failed;
2615 value = ast2obj_arguments(o->v.Lambda.args);
2616 if (!value) goto failed;
2617 if (PyObject_SetAttrString(result, "args", value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
2620 value = ast2obj_expr(o->v.Lambda.body);
2621 if (!value) goto failed;
2622 if (PyObject_SetAttrString(result, "body", value) == -1)
2623 goto failed;
2624 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002626 case IfExp_kind:
2627 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2628 if (!result) goto failed;
2629 value = ast2obj_expr(o->v.IfExp.test);
2630 if (!value) goto failed;
2631 if (PyObject_SetAttrString(result, "test", value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 value = ast2obj_expr(o->v.IfExp.body);
2635 if (!value) goto failed;
2636 if (PyObject_SetAttrString(result, "body", value) == -1)
2637 goto failed;
2638 Py_DECREF(value);
2639 value = ast2obj_expr(o->v.IfExp.orelse);
2640 if (!value) goto failed;
2641 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2642 goto failed;
2643 Py_DECREF(value);
2644 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002646 result = PyType_GenericNew(Dict_type, NULL, NULL);
2647 if (!result) goto failed;
2648 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2649 if (!value) goto failed;
2650 if (PyObject_SetAttrString(result, "keys", value) == -1)
2651 goto failed;
2652 Py_DECREF(value);
2653 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2654 if (!value) goto failed;
2655 if (PyObject_SetAttrString(result, "values", value) == -1)
2656 goto failed;
2657 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002659 case Set_kind:
2660 result = PyType_GenericNew(Set_type, NULL, NULL);
2661 if (!result) goto failed;
2662 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2663 if (!value) goto failed;
2664 if (PyObject_SetAttrString(result, "elts", value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002669 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2670 if (!result) goto failed;
2671 value = ast2obj_expr(o->v.ListComp.elt);
2672 if (!value) goto failed;
2673 if (PyObject_SetAttrString(result, "elt", value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
2676 value = ast2obj_list(o->v.ListComp.generators,
2677 ast2obj_comprehension);
2678 if (!value) goto failed;
2679 if (PyObject_SetAttrString(result, "generators", value) == -1)
2680 goto failed;
2681 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002683 case SetComp_kind:
2684 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2685 if (!result) goto failed;
2686 value = ast2obj_expr(o->v.SetComp.elt);
2687 if (!value) goto failed;
2688 if (PyObject_SetAttrString(result, "elt", value) == -1)
2689 goto failed;
2690 Py_DECREF(value);
2691 value = ast2obj_list(o->v.SetComp.generators,
2692 ast2obj_comprehension);
2693 if (!value) goto failed;
2694 if (PyObject_SetAttrString(result, "generators", value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
2697 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002698 case DictComp_kind:
2699 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2700 if (!result) goto failed;
2701 value = ast2obj_expr(o->v.DictComp.key);
2702 if (!value) goto failed;
2703 if (PyObject_SetAttrString(result, "key", value) == -1)
2704 goto failed;
2705 Py_DECREF(value);
2706 value = ast2obj_expr(o->v.DictComp.value);
2707 if (!value) goto failed;
2708 if (PyObject_SetAttrString(result, "value", value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
2711 value = ast2obj_list(o->v.DictComp.generators,
2712 ast2obj_comprehension);
2713 if (!value) goto failed;
2714 if (PyObject_SetAttrString(result, "generators", value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002719 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2720 if (!result) goto failed;
2721 value = ast2obj_expr(o->v.GeneratorExp.elt);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "elt", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 value = ast2obj_list(o->v.GeneratorExp.generators,
2727 ast2obj_comprehension);
2728 if (!value) goto failed;
2729 if (PyObject_SetAttrString(result, "generators", value) == -1)
2730 goto failed;
2731 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 break;
2733 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002734 result = PyType_GenericNew(Yield_type, NULL, NULL);
2735 if (!result) goto failed;
2736 value = ast2obj_expr(o->v.Yield.value);
2737 if (!value) goto failed;
2738 if (PyObject_SetAttrString(result, "value", value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 break;
2742 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002743 result = PyType_GenericNew(Compare_type, NULL, NULL);
2744 if (!result) goto failed;
2745 value = ast2obj_expr(o->v.Compare.left);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "left", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002750 {
2751 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2752 value = PyList_New(n);
2753 if (!value) goto failed;
2754 for(i = 0; i < n; i++)
2755 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2756 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002757 if (!value) goto failed;
2758 if (PyObject_SetAttrString(result, "ops", value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
2761 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2762 if (!value) goto failed;
2763 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2764 goto failed;
2765 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 break;
2767 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002768 result = PyType_GenericNew(Call_type, NULL, NULL);
2769 if (!result) goto failed;
2770 value = ast2obj_expr(o->v.Call.func);
2771 if (!value) goto failed;
2772 if (PyObject_SetAttrString(result, "func", value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2776 if (!value) goto failed;
2777 if (PyObject_SetAttrString(result, "args", value) == -1)
2778 goto failed;
2779 Py_DECREF(value);
2780 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2781 if (!value) goto failed;
2782 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2783 goto failed;
2784 Py_DECREF(value);
2785 value = ast2obj_expr(o->v.Call.starargs);
2786 if (!value) goto failed;
2787 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2788 goto failed;
2789 Py_DECREF(value);
2790 value = ast2obj_expr(o->v.Call.kwargs);
2791 if (!value) goto failed;
2792 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2793 goto failed;
2794 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002797 result = PyType_GenericNew(Num_type, NULL, NULL);
2798 if (!result) goto failed;
2799 value = ast2obj_object(o->v.Num.n);
2800 if (!value) goto failed;
2801 if (PyObject_SetAttrString(result, "n", value) == -1)
2802 goto failed;
2803 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 break;
2805 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002806 result = PyType_GenericNew(Str_type, NULL, NULL);
2807 if (!result) goto failed;
2808 value = ast2obj_string(o->v.Str.s);
2809 if (!value) goto failed;
2810 if (PyObject_SetAttrString(result, "s", value) == -1)
2811 goto failed;
2812 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002814 case Bytes_kind:
2815 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2816 if (!result) goto failed;
Benjamin Petersone2498412011-08-09 16:08:39 -05002817 value = ast2obj_bytes(o->v.Bytes.s);
Thomas Wouters00e41de2007-02-23 19:56:57 +00002818 if (!value) goto failed;
2819 if (PyObject_SetAttrString(result, "s", value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
2822 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002823 case Ellipsis_kind:
2824 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2825 if (!result) goto failed;
2826 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002828 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2829 if (!result) goto failed;
2830 value = ast2obj_expr(o->v.Attribute.value);
2831 if (!value) goto failed;
2832 if (PyObject_SetAttrString(result, "value", value) == -1)
2833 goto failed;
2834 Py_DECREF(value);
2835 value = ast2obj_identifier(o->v.Attribute.attr);
2836 if (!value) goto failed;
2837 if (PyObject_SetAttrString(result, "attr", value) == -1)
2838 goto failed;
2839 Py_DECREF(value);
2840 value = ast2obj_expr_context(o->v.Attribute.ctx);
2841 if (!value) goto failed;
2842 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2843 goto failed;
2844 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 break;
2846 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002847 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2848 if (!result) goto failed;
2849 value = ast2obj_expr(o->v.Subscript.value);
2850 if (!value) goto failed;
2851 if (PyObject_SetAttrString(result, "value", value) == -1)
2852 goto failed;
2853 Py_DECREF(value);
2854 value = ast2obj_slice(o->v.Subscript.slice);
2855 if (!value) goto failed;
2856 if (PyObject_SetAttrString(result, "slice", value) == -1)
2857 goto failed;
2858 Py_DECREF(value);
2859 value = ast2obj_expr_context(o->v.Subscript.ctx);
2860 if (!value) goto failed;
2861 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2862 goto failed;
2863 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002865 case Starred_kind:
2866 result = PyType_GenericNew(Starred_type, NULL, NULL);
2867 if (!result) goto failed;
2868 value = ast2obj_expr(o->v.Starred.value);
2869 if (!value) goto failed;
2870 if (PyObject_SetAttrString(result, "value", value) == -1)
2871 goto failed;
2872 Py_DECREF(value);
2873 value = ast2obj_expr_context(o->v.Starred.ctx);
2874 if (!value) goto failed;
2875 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2876 goto failed;
2877 Py_DECREF(value);
2878 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002880 result = PyType_GenericNew(Name_type, NULL, NULL);
2881 if (!result) goto failed;
2882 value = ast2obj_identifier(o->v.Name.id);
2883 if (!value) goto failed;
2884 if (PyObject_SetAttrString(result, "id", value) == -1)
2885 goto failed;
2886 Py_DECREF(value);
2887 value = ast2obj_expr_context(o->v.Name.ctx);
2888 if (!value) goto failed;
2889 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2890 goto failed;
2891 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 break;
2893 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002894 result = PyType_GenericNew(List_type, NULL, NULL);
2895 if (!result) goto failed;
2896 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2897 if (!value) goto failed;
2898 if (PyObject_SetAttrString(result, "elts", value) == -1)
2899 goto failed;
2900 Py_DECREF(value);
2901 value = ast2obj_expr_context(o->v.List.ctx);
2902 if (!value) goto failed;
2903 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2904 goto failed;
2905 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 break;
2907 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002908 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2909 if (!result) goto failed;
2910 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2911 if (!value) goto failed;
2912 if (PyObject_SetAttrString(result, "elts", value) == -1)
2913 goto failed;
2914 Py_DECREF(value);
2915 value = ast2obj_expr_context(o->v.Tuple.ctx);
2916 if (!value) goto failed;
2917 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2918 goto failed;
2919 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 break;
2921 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002922 value = ast2obj_int(o->lineno);
2923 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002924 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2925 goto failed;
2926 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002927 value = ast2obj_int(o->col_offset);
2928 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002929 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2930 goto failed;
2931 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002932 return result;
2933failed:
2934 Py_XDECREF(value);
2935 Py_XDECREF(result);
2936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937}
2938
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002939PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941 switch(o) {
2942 case Load:
2943 Py_INCREF(Load_singleton);
2944 return Load_singleton;
2945 case Store:
2946 Py_INCREF(Store_singleton);
2947 return Store_singleton;
2948 case Del:
2949 Py_INCREF(Del_singleton);
2950 return Del_singleton;
2951 case AugLoad:
2952 Py_INCREF(AugLoad_singleton);
2953 return AugLoad_singleton;
2954 case AugStore:
2955 Py_INCREF(AugStore_singleton);
2956 return AugStore_singleton;
2957 case Param:
2958 Py_INCREF(Param_singleton);
2959 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002960 default:
2961 /* should never happen, but just in case ... */
2962 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2963 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002966PyObject*
2967ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002969 slice_ty o = (slice_ty)_o;
2970 PyObject *result = NULL, *value = NULL;
2971 if (!o) {
2972 Py_INCREF(Py_None);
2973 return Py_None;
2974 }
2975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002978 result = PyType_GenericNew(Slice_type, NULL, NULL);
2979 if (!result) goto failed;
2980 value = ast2obj_expr(o->v.Slice.lower);
2981 if (!value) goto failed;
2982 if (PyObject_SetAttrString(result, "lower", value) == -1)
2983 goto failed;
2984 Py_DECREF(value);
2985 value = ast2obj_expr(o->v.Slice.upper);
2986 if (!value) goto failed;
2987 if (PyObject_SetAttrString(result, "upper", value) == -1)
2988 goto failed;
2989 Py_DECREF(value);
2990 value = ast2obj_expr(o->v.Slice.step);
2991 if (!value) goto failed;
2992 if (PyObject_SetAttrString(result, "step", value) == -1)
2993 goto failed;
2994 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 break;
2996 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002997 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2998 if (!result) goto failed;
2999 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3000 if (!value) goto failed;
3001 if (PyObject_SetAttrString(result, "dims", value) == -1)
3002 goto failed;
3003 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 break;
3005 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003006 result = PyType_GenericNew(Index_type, NULL, NULL);
3007 if (!result) goto failed;
3008 value = ast2obj_expr(o->v.Index.value);
3009 if (!value) goto failed;
3010 if (PyObject_SetAttrString(result, "value", value) == -1)
3011 goto failed;
3012 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 break;
3014 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003015 return result;
3016failed:
3017 Py_XDECREF(value);
3018 Py_XDECREF(result);
3019 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020}
3021
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003022PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003024 switch(o) {
3025 case And:
3026 Py_INCREF(And_singleton);
3027 return And_singleton;
3028 case Or:
3029 Py_INCREF(Or_singleton);
3030 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003031 default:
3032 /* should never happen, but just in case ... */
3033 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003036}
3037PyObject* ast2obj_operator(operator_ty o)
3038{
3039 switch(o) {
3040 case Add:
3041 Py_INCREF(Add_singleton);
3042 return Add_singleton;
3043 case Sub:
3044 Py_INCREF(Sub_singleton);
3045 return Sub_singleton;
3046 case Mult:
3047 Py_INCREF(Mult_singleton);
3048 return Mult_singleton;
3049 case Div:
3050 Py_INCREF(Div_singleton);
3051 return Div_singleton;
3052 case Mod:
3053 Py_INCREF(Mod_singleton);
3054 return Mod_singleton;
3055 case Pow:
3056 Py_INCREF(Pow_singleton);
3057 return Pow_singleton;
3058 case LShift:
3059 Py_INCREF(LShift_singleton);
3060 return LShift_singleton;
3061 case RShift:
3062 Py_INCREF(RShift_singleton);
3063 return RShift_singleton;
3064 case BitOr:
3065 Py_INCREF(BitOr_singleton);
3066 return BitOr_singleton;
3067 case BitXor:
3068 Py_INCREF(BitXor_singleton);
3069 return BitXor_singleton;
3070 case BitAnd:
3071 Py_INCREF(BitAnd_singleton);
3072 return BitAnd_singleton;
3073 case FloorDiv:
3074 Py_INCREF(FloorDiv_singleton);
3075 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003076 default:
3077 /* should never happen, but just in case ... */
3078 PyErr_Format(PyExc_SystemError, "unknown operator found");
3079 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003080 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003081}
3082PyObject* ast2obj_unaryop(unaryop_ty o)
3083{
3084 switch(o) {
3085 case Invert:
3086 Py_INCREF(Invert_singleton);
3087 return Invert_singleton;
3088 case Not:
3089 Py_INCREF(Not_singleton);
3090 return Not_singleton;
3091 case UAdd:
3092 Py_INCREF(UAdd_singleton);
3093 return UAdd_singleton;
3094 case USub:
3095 Py_INCREF(USub_singleton);
3096 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003097 default:
3098 /* should never happen, but just in case ... */
3099 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3100 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003101 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003102}
3103PyObject* ast2obj_cmpop(cmpop_ty o)
3104{
3105 switch(o) {
3106 case Eq:
3107 Py_INCREF(Eq_singleton);
3108 return Eq_singleton;
3109 case NotEq:
3110 Py_INCREF(NotEq_singleton);
3111 return NotEq_singleton;
3112 case Lt:
3113 Py_INCREF(Lt_singleton);
3114 return Lt_singleton;
3115 case LtE:
3116 Py_INCREF(LtE_singleton);
3117 return LtE_singleton;
3118 case Gt:
3119 Py_INCREF(Gt_singleton);
3120 return Gt_singleton;
3121 case GtE:
3122 Py_INCREF(GtE_singleton);
3123 return GtE_singleton;
3124 case Is:
3125 Py_INCREF(Is_singleton);
3126 return Is_singleton;
3127 case IsNot:
3128 Py_INCREF(IsNot_singleton);
3129 return IsNot_singleton;
3130 case In:
3131 Py_INCREF(In_singleton);
3132 return In_singleton;
3133 case NotIn:
3134 Py_INCREF(NotIn_singleton);
3135 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003136 default:
3137 /* should never happen, but just in case ... */
3138 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3139 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003140 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003141}
3142PyObject*
3143ast2obj_comprehension(void* _o)
3144{
3145 comprehension_ty o = (comprehension_ty)_o;
3146 PyObject *result = NULL, *value = NULL;
3147 if (!o) {
3148 Py_INCREF(Py_None);
3149 return Py_None;
3150 }
3151
3152 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3153 if (!result) return NULL;
3154 value = ast2obj_expr(o->target);
3155 if (!value) goto failed;
3156 if (PyObject_SetAttrString(result, "target", value) == -1)
3157 goto failed;
3158 Py_DECREF(value);
3159 value = ast2obj_expr(o->iter);
3160 if (!value) goto failed;
3161 if (PyObject_SetAttrString(result, "iter", value) == -1)
3162 goto failed;
3163 Py_DECREF(value);
3164 value = ast2obj_list(o->ifs, ast2obj_expr);
3165 if (!value) goto failed;
3166 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003169 return result;
3170failed:
3171 Py_XDECREF(value);
3172 Py_XDECREF(result);
3173 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174}
3175
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003176PyObject*
3177ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003179 excepthandler_ty o = (excepthandler_ty)_o;
3180 PyObject *result = NULL, *value = NULL;
3181 if (!o) {
3182 Py_INCREF(Py_None);
3183 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003185
Neal Norwitzad74aa82008-03-31 05:14:30 +00003186 switch (o->kind) {
3187 case ExceptHandler_kind:
3188 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3189 if (!result) goto failed;
3190 value = ast2obj_expr(o->v.ExceptHandler.type);
3191 if (!value) goto failed;
3192 if (PyObject_SetAttrString(result, "type", value) == -1)
3193 goto failed;
3194 Py_DECREF(value);
3195 value = ast2obj_identifier(o->v.ExceptHandler.name);
3196 if (!value) goto failed;
3197 if (PyObject_SetAttrString(result, "name", value) == -1)
3198 goto failed;
3199 Py_DECREF(value);
3200 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3201 if (!value) goto failed;
3202 if (PyObject_SetAttrString(result, "body", value) == -1)
3203 goto failed;
3204 Py_DECREF(value);
3205 break;
3206 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003207 value = ast2obj_int(o->lineno);
3208 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003209 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003210 goto failed;
3211 Py_DECREF(value);
3212 value = ast2obj_int(o->col_offset);
3213 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003214 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003215 goto failed;
3216 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003217 return result;
3218failed:
3219 Py_XDECREF(value);
3220 Py_XDECREF(result);
3221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222}
3223
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003224PyObject*
3225ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003227 arguments_ty o = (arguments_ty)_o;
3228 PyObject *result = NULL, *value = NULL;
3229 if (!o) {
3230 Py_INCREF(Py_None);
3231 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003233
3234 result = PyType_GenericNew(arguments_type, NULL, NULL);
3235 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003236 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003237 if (!value) goto failed;
3238 if (PyObject_SetAttrString(result, "args", value) == -1)
3239 goto failed;
3240 Py_DECREF(value);
3241 value = ast2obj_identifier(o->vararg);
3242 if (!value) goto failed;
3243 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3244 goto failed;
3245 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003246 value = ast2obj_expr(o->varargannotation);
3247 if (!value) goto failed;
3248 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3249 goto failed;
3250 Py_DECREF(value);
3251 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003252 if (!value) goto failed;
3253 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3254 goto failed;
3255 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003256 value = ast2obj_identifier(o->kwarg);
3257 if (!value) goto failed;
3258 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3259 goto failed;
3260 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003261 value = ast2obj_expr(o->kwargannotation);
3262 if (!value) goto failed;
3263 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3264 goto failed;
3265 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003266 value = ast2obj_list(o->defaults, ast2obj_expr);
3267 if (!value) goto failed;
3268 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3269 goto failed;
3270 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003271 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3272 if (!value) goto failed;
3273 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3274 goto failed;
3275 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003276 return result;
3277failed:
3278 Py_XDECREF(value);
3279 Py_XDECREF(result);
3280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281}
3282
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003283PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003284ast2obj_arg(void* _o)
3285{
3286 arg_ty o = (arg_ty)_o;
3287 PyObject *result = NULL, *value = NULL;
3288 if (!o) {
3289 Py_INCREF(Py_None);
3290 return Py_None;
3291 }
3292
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003293 result = PyType_GenericNew(arg_type, NULL, NULL);
3294 if (!result) return NULL;
3295 value = ast2obj_identifier(o->arg);
3296 if (!value) goto failed;
3297 if (PyObject_SetAttrString(result, "arg", value) == -1)
3298 goto failed;
3299 Py_DECREF(value);
3300 value = ast2obj_expr(o->annotation);
3301 if (!value) goto failed;
3302 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3303 goto failed;
3304 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003305 return result;
3306failed:
3307 Py_XDECREF(value);
3308 Py_XDECREF(result);
3309 return NULL;
3310}
3311
3312PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003313ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003315 keyword_ty o = (keyword_ty)_o;
3316 PyObject *result = NULL, *value = NULL;
3317 if (!o) {
3318 Py_INCREF(Py_None);
3319 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003321
3322 result = PyType_GenericNew(keyword_type, NULL, NULL);
3323 if (!result) return NULL;
3324 value = ast2obj_identifier(o->arg);
3325 if (!value) goto failed;
3326 if (PyObject_SetAttrString(result, "arg", value) == -1)
3327 goto failed;
3328 Py_DECREF(value);
3329 value = ast2obj_expr(o->value);
3330 if (!value) goto failed;
3331 if (PyObject_SetAttrString(result, "value", value) == -1)
3332 goto failed;
3333 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003334 return result;
3335failed:
3336 Py_XDECREF(value);
3337 Py_XDECREF(result);
3338 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339}
3340
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003341PyObject*
3342ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003344 alias_ty o = (alias_ty)_o;
3345 PyObject *result = NULL, *value = NULL;
3346 if (!o) {
3347 Py_INCREF(Py_None);
3348 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003350
3351 result = PyType_GenericNew(alias_type, NULL, NULL);
3352 if (!result) return NULL;
3353 value = ast2obj_identifier(o->name);
3354 if (!value) goto failed;
3355 if (PyObject_SetAttrString(result, "name", value) == -1)
3356 goto failed;
3357 Py_DECREF(value);
3358 value = ast2obj_identifier(o->asname);
3359 if (!value) goto failed;
3360 if (PyObject_SetAttrString(result, "asname", value) == -1)
3361 goto failed;
3362 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003363 return result;
3364failed:
3365 Py_XDECREF(value);
3366 Py_XDECREF(result);
3367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368}
3369
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003370PyObject*
3371ast2obj_withitem(void* _o)
3372{
3373 withitem_ty o = (withitem_ty)_o;
3374 PyObject *result = NULL, *value = NULL;
3375 if (!o) {
3376 Py_INCREF(Py_None);
3377 return Py_None;
3378 }
3379
3380 result = PyType_GenericNew(withitem_type, NULL, NULL);
3381 if (!result) return NULL;
3382 value = ast2obj_expr(o->context_expr);
3383 if (!value) goto failed;
3384 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3385 goto failed;
3386 Py_DECREF(value);
3387 value = ast2obj_expr(o->optional_vars);
3388 if (!value) goto failed;
3389 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3390 goto failed;
3391 Py_DECREF(value);
3392 return result;
3393failed:
3394 Py_XDECREF(value);
3395 Py_XDECREF(result);
3396 return NULL;
3397}
3398
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003400int
3401obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3402{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003403 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003404
Benjamin Petersond8f65972010-11-20 04:31:07 +00003405 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003406
3407 if (obj == Py_None) {
3408 *out = NULL;
3409 return 0;
3410 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003411 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3412 if (isinstance == -1) {
3413 return 1;
3414 }
3415 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003416 asdl_seq* body;
3417
3418 if (PyObject_HasAttrString(obj, "body")) {
3419 int res;
3420 Py_ssize_t len;
3421 Py_ssize_t i;
3422 tmp = PyObject_GetAttrString(obj, "body");
3423 if (tmp == NULL) goto failed;
3424 if (!PyList_Check(tmp)) {
3425 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3426 goto failed;
3427 }
3428 len = PyList_GET_SIZE(tmp);
3429 body = asdl_seq_new(len, arena);
3430 if (body == NULL) goto failed;
3431 for (i = 0; i < len; i++) {
3432 stmt_ty value;
3433 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3434 if (res != 0) goto failed;
3435 asdl_seq_SET(body, i, value);
3436 }
3437 Py_XDECREF(tmp);
3438 tmp = NULL;
3439 } else {
3440 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3441 return 1;
3442 }
3443 *out = Module(body, arena);
3444 if (*out == NULL) goto failed;
3445 return 0;
3446 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003447 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3448 if (isinstance == -1) {
3449 return 1;
3450 }
3451 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003452 asdl_seq* body;
3453
3454 if (PyObject_HasAttrString(obj, "body")) {
3455 int res;
3456 Py_ssize_t len;
3457 Py_ssize_t i;
3458 tmp = PyObject_GetAttrString(obj, "body");
3459 if (tmp == NULL) goto failed;
3460 if (!PyList_Check(tmp)) {
3461 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3462 goto failed;
3463 }
3464 len = PyList_GET_SIZE(tmp);
3465 body = asdl_seq_new(len, arena);
3466 if (body == NULL) goto failed;
3467 for (i = 0; i < len; i++) {
3468 stmt_ty value;
3469 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3470 if (res != 0) goto failed;
3471 asdl_seq_SET(body, i, value);
3472 }
3473 Py_XDECREF(tmp);
3474 tmp = NULL;
3475 } else {
3476 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3477 return 1;
3478 }
3479 *out = Interactive(body, arena);
3480 if (*out == NULL) goto failed;
3481 return 0;
3482 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003483 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3484 if (isinstance == -1) {
3485 return 1;
3486 }
3487 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003488 expr_ty body;
3489
3490 if (PyObject_HasAttrString(obj, "body")) {
3491 int res;
3492 tmp = PyObject_GetAttrString(obj, "body");
3493 if (tmp == NULL) goto failed;
3494 res = obj2ast_expr(tmp, &body, arena);
3495 if (res != 0) goto failed;
3496 Py_XDECREF(tmp);
3497 tmp = NULL;
3498 } else {
3499 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3500 return 1;
3501 }
3502 *out = Expression(body, arena);
3503 if (*out == NULL) goto failed;
3504 return 0;
3505 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003506 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3507 if (isinstance == -1) {
3508 return 1;
3509 }
3510 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003511 asdl_seq* body;
3512
3513 if (PyObject_HasAttrString(obj, "body")) {
3514 int res;
3515 Py_ssize_t len;
3516 Py_ssize_t i;
3517 tmp = PyObject_GetAttrString(obj, "body");
3518 if (tmp == NULL) goto failed;
3519 if (!PyList_Check(tmp)) {
3520 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3521 goto failed;
3522 }
3523 len = PyList_GET_SIZE(tmp);
3524 body = asdl_seq_new(len, arena);
3525 if (body == NULL) goto failed;
3526 for (i = 0; i < len; i++) {
3527 stmt_ty value;
3528 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3529 if (res != 0) goto failed;
3530 asdl_seq_SET(body, i, value);
3531 }
3532 Py_XDECREF(tmp);
3533 tmp = NULL;
3534 } else {
3535 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3536 return 1;
3537 }
3538 *out = Suite(body, arena);
3539 if (*out == NULL) goto failed;
3540 return 0;
3541 }
3542
Benjamin Peterson5b066812010-11-20 01:38:49 +00003543 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3544 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003545 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003546 return 1;
3547}
3548
3549int
3550obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3551{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003552 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003553
Benjamin Petersond8f65972010-11-20 04:31:07 +00003554 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003555 int lineno;
3556 int col_offset;
3557
3558 if (obj == Py_None) {
3559 *out = NULL;
3560 return 0;
3561 }
3562 if (PyObject_HasAttrString(obj, "lineno")) {
3563 int res;
3564 tmp = PyObject_GetAttrString(obj, "lineno");
3565 if (tmp == NULL) goto failed;
3566 res = obj2ast_int(tmp, &lineno, arena);
3567 if (res != 0) goto failed;
3568 Py_XDECREF(tmp);
3569 tmp = NULL;
3570 } else {
3571 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3572 return 1;
3573 }
3574 if (PyObject_HasAttrString(obj, "col_offset")) {
3575 int res;
3576 tmp = PyObject_GetAttrString(obj, "col_offset");
3577 if (tmp == NULL) goto failed;
3578 res = obj2ast_int(tmp, &col_offset, arena);
3579 if (res != 0) goto failed;
3580 Py_XDECREF(tmp);
3581 tmp = NULL;
3582 } else {
3583 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3584 return 1;
3585 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003586 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3587 if (isinstance == -1) {
3588 return 1;
3589 }
3590 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003591 identifier name;
3592 arguments_ty args;
3593 asdl_seq* body;
3594 asdl_seq* decorator_list;
3595 expr_ty returns;
3596
3597 if (PyObject_HasAttrString(obj, "name")) {
3598 int res;
3599 tmp = PyObject_GetAttrString(obj, "name");
3600 if (tmp == NULL) goto failed;
3601 res = obj2ast_identifier(tmp, &name, arena);
3602 if (res != 0) goto failed;
3603 Py_XDECREF(tmp);
3604 tmp = NULL;
3605 } else {
3606 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3607 return 1;
3608 }
3609 if (PyObject_HasAttrString(obj, "args")) {
3610 int res;
3611 tmp = PyObject_GetAttrString(obj, "args");
3612 if (tmp == NULL) goto failed;
3613 res = obj2ast_arguments(tmp, &args, arena);
3614 if (res != 0) goto failed;
3615 Py_XDECREF(tmp);
3616 tmp = NULL;
3617 } else {
3618 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3619 return 1;
3620 }
3621 if (PyObject_HasAttrString(obj, "body")) {
3622 int res;
3623 Py_ssize_t len;
3624 Py_ssize_t i;
3625 tmp = PyObject_GetAttrString(obj, "body");
3626 if (tmp == NULL) goto failed;
3627 if (!PyList_Check(tmp)) {
3628 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3629 goto failed;
3630 }
3631 len = PyList_GET_SIZE(tmp);
3632 body = asdl_seq_new(len, arena);
3633 if (body == NULL) goto failed;
3634 for (i = 0; i < len; i++) {
3635 stmt_ty value;
3636 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3637 if (res != 0) goto failed;
3638 asdl_seq_SET(body, i, value);
3639 }
3640 Py_XDECREF(tmp);
3641 tmp = NULL;
3642 } else {
3643 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3644 return 1;
3645 }
3646 if (PyObject_HasAttrString(obj, "decorator_list")) {
3647 int res;
3648 Py_ssize_t len;
3649 Py_ssize_t i;
3650 tmp = PyObject_GetAttrString(obj, "decorator_list");
3651 if (tmp == NULL) goto failed;
3652 if (!PyList_Check(tmp)) {
3653 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3654 goto failed;
3655 }
3656 len = PyList_GET_SIZE(tmp);
3657 decorator_list = asdl_seq_new(len, arena);
3658 if (decorator_list == NULL) goto failed;
3659 for (i = 0; i < len; i++) {
3660 expr_ty value;
3661 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3662 if (res != 0) goto failed;
3663 asdl_seq_SET(decorator_list, i, value);
3664 }
3665 Py_XDECREF(tmp);
3666 tmp = NULL;
3667 } else {
3668 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3669 return 1;
3670 }
3671 if (PyObject_HasAttrString(obj, "returns")) {
3672 int res;
3673 tmp = PyObject_GetAttrString(obj, "returns");
3674 if (tmp == NULL) goto failed;
3675 res = obj2ast_expr(tmp, &returns, arena);
3676 if (res != 0) goto failed;
3677 Py_XDECREF(tmp);
3678 tmp = NULL;
3679 } else {
3680 returns = NULL;
3681 }
3682 *out = FunctionDef(name, args, body, decorator_list, returns,
3683 lineno, col_offset, arena);
3684 if (*out == NULL) goto failed;
3685 return 0;
3686 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003687 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3688 if (isinstance == -1) {
3689 return 1;
3690 }
3691 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003692 identifier name;
3693 asdl_seq* bases;
3694 asdl_seq* keywords;
3695 expr_ty starargs;
3696 expr_ty kwargs;
3697 asdl_seq* body;
3698 asdl_seq* decorator_list;
3699
3700 if (PyObject_HasAttrString(obj, "name")) {
3701 int res;
3702 tmp = PyObject_GetAttrString(obj, "name");
3703 if (tmp == NULL) goto failed;
3704 res = obj2ast_identifier(tmp, &name, arena);
3705 if (res != 0) goto failed;
3706 Py_XDECREF(tmp);
3707 tmp = NULL;
3708 } else {
3709 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3710 return 1;
3711 }
3712 if (PyObject_HasAttrString(obj, "bases")) {
3713 int res;
3714 Py_ssize_t len;
3715 Py_ssize_t i;
3716 tmp = PyObject_GetAttrString(obj, "bases");
3717 if (tmp == NULL) goto failed;
3718 if (!PyList_Check(tmp)) {
3719 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3720 goto failed;
3721 }
3722 len = PyList_GET_SIZE(tmp);
3723 bases = asdl_seq_new(len, arena);
3724 if (bases == NULL) goto failed;
3725 for (i = 0; i < len; i++) {
3726 expr_ty value;
3727 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3728 if (res != 0) goto failed;
3729 asdl_seq_SET(bases, i, value);
3730 }
3731 Py_XDECREF(tmp);
3732 tmp = NULL;
3733 } else {
3734 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3735 return 1;
3736 }
3737 if (PyObject_HasAttrString(obj, "keywords")) {
3738 int res;
3739 Py_ssize_t len;
3740 Py_ssize_t i;
3741 tmp = PyObject_GetAttrString(obj, "keywords");
3742 if (tmp == NULL) goto failed;
3743 if (!PyList_Check(tmp)) {
3744 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3745 goto failed;
3746 }
3747 len = PyList_GET_SIZE(tmp);
3748 keywords = asdl_seq_new(len, arena);
3749 if (keywords == NULL) goto failed;
3750 for (i = 0; i < len; i++) {
3751 keyword_ty value;
3752 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3753 if (res != 0) goto failed;
3754 asdl_seq_SET(keywords, i, value);
3755 }
3756 Py_XDECREF(tmp);
3757 tmp = NULL;
3758 } else {
3759 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3760 return 1;
3761 }
3762 if (PyObject_HasAttrString(obj, "starargs")) {
3763 int res;
3764 tmp = PyObject_GetAttrString(obj, "starargs");
3765 if (tmp == NULL) goto failed;
3766 res = obj2ast_expr(tmp, &starargs, arena);
3767 if (res != 0) goto failed;
3768 Py_XDECREF(tmp);
3769 tmp = NULL;
3770 } else {
3771 starargs = NULL;
3772 }
3773 if (PyObject_HasAttrString(obj, "kwargs")) {
3774 int res;
3775 tmp = PyObject_GetAttrString(obj, "kwargs");
3776 if (tmp == NULL) goto failed;
3777 res = obj2ast_expr(tmp, &kwargs, arena);
3778 if (res != 0) goto failed;
3779 Py_XDECREF(tmp);
3780 tmp = NULL;
3781 } else {
3782 kwargs = NULL;
3783 }
3784 if (PyObject_HasAttrString(obj, "body")) {
3785 int res;
3786 Py_ssize_t len;
3787 Py_ssize_t i;
3788 tmp = PyObject_GetAttrString(obj, "body");
3789 if (tmp == NULL) goto failed;
3790 if (!PyList_Check(tmp)) {
3791 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3792 goto failed;
3793 }
3794 len = PyList_GET_SIZE(tmp);
3795 body = asdl_seq_new(len, arena);
3796 if (body == NULL) goto failed;
3797 for (i = 0; i < len; i++) {
3798 stmt_ty value;
3799 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3800 if (res != 0) goto failed;
3801 asdl_seq_SET(body, i, value);
3802 }
3803 Py_XDECREF(tmp);
3804 tmp = NULL;
3805 } else {
3806 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3807 return 1;
3808 }
3809 if (PyObject_HasAttrString(obj, "decorator_list")) {
3810 int res;
3811 Py_ssize_t len;
3812 Py_ssize_t i;
3813 tmp = PyObject_GetAttrString(obj, "decorator_list");
3814 if (tmp == NULL) goto failed;
3815 if (!PyList_Check(tmp)) {
3816 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3817 goto failed;
3818 }
3819 len = PyList_GET_SIZE(tmp);
3820 decorator_list = asdl_seq_new(len, arena);
3821 if (decorator_list == NULL) goto failed;
3822 for (i = 0; i < len; i++) {
3823 expr_ty value;
3824 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3825 if (res != 0) goto failed;
3826 asdl_seq_SET(decorator_list, i, value);
3827 }
3828 Py_XDECREF(tmp);
3829 tmp = NULL;
3830 } else {
3831 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3832 return 1;
3833 }
3834 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3835 decorator_list, lineno, col_offset, arena);
3836 if (*out == NULL) goto failed;
3837 return 0;
3838 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003839 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3840 if (isinstance == -1) {
3841 return 1;
3842 }
3843 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003844 expr_ty value;
3845
3846 if (PyObject_HasAttrString(obj, "value")) {
3847 int res;
3848 tmp = PyObject_GetAttrString(obj, "value");
3849 if (tmp == NULL) goto failed;
3850 res = obj2ast_expr(tmp, &value, arena);
3851 if (res != 0) goto failed;
3852 Py_XDECREF(tmp);
3853 tmp = NULL;
3854 } else {
3855 value = NULL;
3856 }
3857 *out = Return(value, lineno, col_offset, arena);
3858 if (*out == NULL) goto failed;
3859 return 0;
3860 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003861 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3862 if (isinstance == -1) {
3863 return 1;
3864 }
3865 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003866 asdl_seq* targets;
3867
3868 if (PyObject_HasAttrString(obj, "targets")) {
3869 int res;
3870 Py_ssize_t len;
3871 Py_ssize_t i;
3872 tmp = PyObject_GetAttrString(obj, "targets");
3873 if (tmp == NULL) goto failed;
3874 if (!PyList_Check(tmp)) {
3875 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3876 goto failed;
3877 }
3878 len = PyList_GET_SIZE(tmp);
3879 targets = asdl_seq_new(len, arena);
3880 if (targets == NULL) goto failed;
3881 for (i = 0; i < len; i++) {
3882 expr_ty value;
3883 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3884 if (res != 0) goto failed;
3885 asdl_seq_SET(targets, i, value);
3886 }
3887 Py_XDECREF(tmp);
3888 tmp = NULL;
3889 } else {
3890 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3891 return 1;
3892 }
3893 *out = Delete(targets, lineno, col_offset, arena);
3894 if (*out == NULL) goto failed;
3895 return 0;
3896 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003897 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3898 if (isinstance == -1) {
3899 return 1;
3900 }
3901 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003902 asdl_seq* targets;
3903 expr_ty value;
3904
3905 if (PyObject_HasAttrString(obj, "targets")) {
3906 int res;
3907 Py_ssize_t len;
3908 Py_ssize_t i;
3909 tmp = PyObject_GetAttrString(obj, "targets");
3910 if (tmp == NULL) goto failed;
3911 if (!PyList_Check(tmp)) {
3912 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3913 goto failed;
3914 }
3915 len = PyList_GET_SIZE(tmp);
3916 targets = asdl_seq_new(len, arena);
3917 if (targets == NULL) goto failed;
3918 for (i = 0; i < len; i++) {
3919 expr_ty value;
3920 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3921 if (res != 0) goto failed;
3922 asdl_seq_SET(targets, i, value);
3923 }
3924 Py_XDECREF(tmp);
3925 tmp = NULL;
3926 } else {
3927 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3928 return 1;
3929 }
3930 if (PyObject_HasAttrString(obj, "value")) {
3931 int res;
3932 tmp = PyObject_GetAttrString(obj, "value");
3933 if (tmp == NULL) goto failed;
3934 res = obj2ast_expr(tmp, &value, arena);
3935 if (res != 0) goto failed;
3936 Py_XDECREF(tmp);
3937 tmp = NULL;
3938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3940 return 1;
3941 }
3942 *out = Assign(targets, value, lineno, col_offset, arena);
3943 if (*out == NULL) goto failed;
3944 return 0;
3945 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003946 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3947 if (isinstance == -1) {
3948 return 1;
3949 }
3950 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003951 expr_ty target;
3952 operator_ty op;
3953 expr_ty value;
3954
3955 if (PyObject_HasAttrString(obj, "target")) {
3956 int res;
3957 tmp = PyObject_GetAttrString(obj, "target");
3958 if (tmp == NULL) goto failed;
3959 res = obj2ast_expr(tmp, &target, arena);
3960 if (res != 0) goto failed;
3961 Py_XDECREF(tmp);
3962 tmp = NULL;
3963 } else {
3964 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3965 return 1;
3966 }
3967 if (PyObject_HasAttrString(obj, "op")) {
3968 int res;
3969 tmp = PyObject_GetAttrString(obj, "op");
3970 if (tmp == NULL) goto failed;
3971 res = obj2ast_operator(tmp, &op, arena);
3972 if (res != 0) goto failed;
3973 Py_XDECREF(tmp);
3974 tmp = NULL;
3975 } else {
3976 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3977 return 1;
3978 }
3979 if (PyObject_HasAttrString(obj, "value")) {
3980 int res;
3981 tmp = PyObject_GetAttrString(obj, "value");
3982 if (tmp == NULL) goto failed;
3983 res = obj2ast_expr(tmp, &value, arena);
3984 if (res != 0) goto failed;
3985 Py_XDECREF(tmp);
3986 tmp = NULL;
3987 } else {
3988 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3989 return 1;
3990 }
3991 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3992 if (*out == NULL) goto failed;
3993 return 0;
3994 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003995 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3996 if (isinstance == -1) {
3997 return 1;
3998 }
3999 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004000 expr_ty target;
4001 expr_ty iter;
4002 asdl_seq* body;
4003 asdl_seq* orelse;
4004
4005 if (PyObject_HasAttrString(obj, "target")) {
4006 int res;
4007 tmp = PyObject_GetAttrString(obj, "target");
4008 if (tmp == NULL) goto failed;
4009 res = obj2ast_expr(tmp, &target, arena);
4010 if (res != 0) goto failed;
4011 Py_XDECREF(tmp);
4012 tmp = NULL;
4013 } else {
4014 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4015 return 1;
4016 }
4017 if (PyObject_HasAttrString(obj, "iter")) {
4018 int res;
4019 tmp = PyObject_GetAttrString(obj, "iter");
4020 if (tmp == NULL) goto failed;
4021 res = obj2ast_expr(tmp, &iter, arena);
4022 if (res != 0) goto failed;
4023 Py_XDECREF(tmp);
4024 tmp = NULL;
4025 } else {
4026 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4027 return 1;
4028 }
4029 if (PyObject_HasAttrString(obj, "body")) {
4030 int res;
4031 Py_ssize_t len;
4032 Py_ssize_t i;
4033 tmp = PyObject_GetAttrString(obj, "body");
4034 if (tmp == NULL) goto failed;
4035 if (!PyList_Check(tmp)) {
4036 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4037 goto failed;
4038 }
4039 len = PyList_GET_SIZE(tmp);
4040 body = asdl_seq_new(len, arena);
4041 if (body == NULL) goto failed;
4042 for (i = 0; i < len; i++) {
4043 stmt_ty value;
4044 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4045 if (res != 0) goto failed;
4046 asdl_seq_SET(body, i, value);
4047 }
4048 Py_XDECREF(tmp);
4049 tmp = NULL;
4050 } else {
4051 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4052 return 1;
4053 }
4054 if (PyObject_HasAttrString(obj, "orelse")) {
4055 int res;
4056 Py_ssize_t len;
4057 Py_ssize_t i;
4058 tmp = PyObject_GetAttrString(obj, "orelse");
4059 if (tmp == NULL) goto failed;
4060 if (!PyList_Check(tmp)) {
4061 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4062 goto failed;
4063 }
4064 len = PyList_GET_SIZE(tmp);
4065 orelse = asdl_seq_new(len, arena);
4066 if (orelse == NULL) goto failed;
4067 for (i = 0; i < len; i++) {
4068 stmt_ty value;
4069 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4070 if (res != 0) goto failed;
4071 asdl_seq_SET(orelse, i, value);
4072 }
4073 Py_XDECREF(tmp);
4074 tmp = NULL;
4075 } else {
4076 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4077 return 1;
4078 }
4079 *out = For(target, iter, body, orelse, lineno, col_offset,
4080 arena);
4081 if (*out == NULL) goto failed;
4082 return 0;
4083 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004084 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4085 if (isinstance == -1) {
4086 return 1;
4087 }
4088 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004089 expr_ty test;
4090 asdl_seq* body;
4091 asdl_seq* orelse;
4092
4093 if (PyObject_HasAttrString(obj, "test")) {
4094 int res;
4095 tmp = PyObject_GetAttrString(obj, "test");
4096 if (tmp == NULL) goto failed;
4097 res = obj2ast_expr(tmp, &test, arena);
4098 if (res != 0) goto failed;
4099 Py_XDECREF(tmp);
4100 tmp = NULL;
4101 } else {
4102 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4103 return 1;
4104 }
4105 if (PyObject_HasAttrString(obj, "body")) {
4106 int res;
4107 Py_ssize_t len;
4108 Py_ssize_t i;
4109 tmp = PyObject_GetAttrString(obj, "body");
4110 if (tmp == NULL) goto failed;
4111 if (!PyList_Check(tmp)) {
4112 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4113 goto failed;
4114 }
4115 len = PyList_GET_SIZE(tmp);
4116 body = asdl_seq_new(len, arena);
4117 if (body == NULL) goto failed;
4118 for (i = 0; i < len; i++) {
4119 stmt_ty value;
4120 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4121 if (res != 0) goto failed;
4122 asdl_seq_SET(body, i, value);
4123 }
4124 Py_XDECREF(tmp);
4125 tmp = NULL;
4126 } else {
4127 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4128 return 1;
4129 }
4130 if (PyObject_HasAttrString(obj, "orelse")) {
4131 int res;
4132 Py_ssize_t len;
4133 Py_ssize_t i;
4134 tmp = PyObject_GetAttrString(obj, "orelse");
4135 if (tmp == NULL) goto failed;
4136 if (!PyList_Check(tmp)) {
4137 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4138 goto failed;
4139 }
4140 len = PyList_GET_SIZE(tmp);
4141 orelse = asdl_seq_new(len, arena);
4142 if (orelse == NULL) goto failed;
4143 for (i = 0; i < len; i++) {
4144 stmt_ty value;
4145 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4146 if (res != 0) goto failed;
4147 asdl_seq_SET(orelse, i, value);
4148 }
4149 Py_XDECREF(tmp);
4150 tmp = NULL;
4151 } else {
4152 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4153 return 1;
4154 }
4155 *out = While(test, body, orelse, lineno, col_offset, arena);
4156 if (*out == NULL) goto failed;
4157 return 0;
4158 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004159 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4160 if (isinstance == -1) {
4161 return 1;
4162 }
4163 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004164 expr_ty test;
4165 asdl_seq* body;
4166 asdl_seq* orelse;
4167
4168 if (PyObject_HasAttrString(obj, "test")) {
4169 int res;
4170 tmp = PyObject_GetAttrString(obj, "test");
4171 if (tmp == NULL) goto failed;
4172 res = obj2ast_expr(tmp, &test, arena);
4173 if (res != 0) goto failed;
4174 Py_XDECREF(tmp);
4175 tmp = NULL;
4176 } else {
4177 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4178 return 1;
4179 }
4180 if (PyObject_HasAttrString(obj, "body")) {
4181 int res;
4182 Py_ssize_t len;
4183 Py_ssize_t i;
4184 tmp = PyObject_GetAttrString(obj, "body");
4185 if (tmp == NULL) goto failed;
4186 if (!PyList_Check(tmp)) {
4187 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4188 goto failed;
4189 }
4190 len = PyList_GET_SIZE(tmp);
4191 body = asdl_seq_new(len, arena);
4192 if (body == NULL) goto failed;
4193 for (i = 0; i < len; i++) {
4194 stmt_ty value;
4195 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4196 if (res != 0) goto failed;
4197 asdl_seq_SET(body, i, value);
4198 }
4199 Py_XDECREF(tmp);
4200 tmp = NULL;
4201 } else {
4202 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4203 return 1;
4204 }
4205 if (PyObject_HasAttrString(obj, "orelse")) {
4206 int res;
4207 Py_ssize_t len;
4208 Py_ssize_t i;
4209 tmp = PyObject_GetAttrString(obj, "orelse");
4210 if (tmp == NULL) goto failed;
4211 if (!PyList_Check(tmp)) {
4212 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4213 goto failed;
4214 }
4215 len = PyList_GET_SIZE(tmp);
4216 orelse = asdl_seq_new(len, arena);
4217 if (orelse == NULL) goto failed;
4218 for (i = 0; i < len; i++) {
4219 stmt_ty value;
4220 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4221 if (res != 0) goto failed;
4222 asdl_seq_SET(orelse, i, value);
4223 }
4224 Py_XDECREF(tmp);
4225 tmp = NULL;
4226 } else {
4227 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4228 return 1;
4229 }
4230 *out = If(test, body, orelse, lineno, col_offset, arena);
4231 if (*out == NULL) goto failed;
4232 return 0;
4233 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004234 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4235 if (isinstance == -1) {
4236 return 1;
4237 }
4238 if (isinstance) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004239 asdl_seq* items;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004240 asdl_seq* body;
4241
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004242 if (PyObject_HasAttrString(obj, "items")) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004243 int res;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004244 Py_ssize_t len;
4245 Py_ssize_t i;
4246 tmp = PyObject_GetAttrString(obj, "items");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004247 if (tmp == NULL) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004248 if (!PyList_Check(tmp)) {
4249 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4250 goto failed;
4251 }
4252 len = PyList_GET_SIZE(tmp);
4253 items = asdl_seq_new(len, arena);
4254 if (items == NULL) goto failed;
4255 for (i = 0; i < len; i++) {
4256 withitem_ty value;
4257 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4258 if (res != 0) goto failed;
4259 asdl_seq_SET(items, i, value);
4260 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004261 Py_XDECREF(tmp);
4262 tmp = NULL;
4263 } else {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004265 return 1;
4266 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004267 if (PyObject_HasAttrString(obj, "body")) {
4268 int res;
4269 Py_ssize_t len;
4270 Py_ssize_t i;
4271 tmp = PyObject_GetAttrString(obj, "body");
4272 if (tmp == NULL) goto failed;
4273 if (!PyList_Check(tmp)) {
4274 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4275 goto failed;
4276 }
4277 len = PyList_GET_SIZE(tmp);
4278 body = asdl_seq_new(len, arena);
4279 if (body == NULL) goto failed;
4280 for (i = 0; i < len; i++) {
4281 stmt_ty value;
4282 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4283 if (res != 0) goto failed;
4284 asdl_seq_SET(body, i, value);
4285 }
4286 Py_XDECREF(tmp);
4287 tmp = NULL;
4288 } else {
4289 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4290 return 1;
4291 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004292 *out = With(items, body, lineno, col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004293 if (*out == NULL) goto failed;
4294 return 0;
4295 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004296 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4297 if (isinstance == -1) {
4298 return 1;
4299 }
4300 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004301 expr_ty exc;
4302 expr_ty cause;
4303
4304 if (PyObject_HasAttrString(obj, "exc")) {
4305 int res;
4306 tmp = PyObject_GetAttrString(obj, "exc");
4307 if (tmp == NULL) goto failed;
4308 res = obj2ast_expr(tmp, &exc, arena);
4309 if (res != 0) goto failed;
4310 Py_XDECREF(tmp);
4311 tmp = NULL;
4312 } else {
4313 exc = NULL;
4314 }
4315 if (PyObject_HasAttrString(obj, "cause")) {
4316 int res;
4317 tmp = PyObject_GetAttrString(obj, "cause");
4318 if (tmp == NULL) goto failed;
4319 res = obj2ast_expr(tmp, &cause, arena);
4320 if (res != 0) goto failed;
4321 Py_XDECREF(tmp);
4322 tmp = NULL;
4323 } else {
4324 cause = NULL;
4325 }
4326 *out = Raise(exc, cause, lineno, col_offset, arena);
4327 if (*out == NULL) goto failed;
4328 return 0;
4329 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004330 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004331 if (isinstance == -1) {
4332 return 1;
4333 }
4334 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004335 asdl_seq* body;
4336 asdl_seq* handlers;
4337 asdl_seq* orelse;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004338 asdl_seq* finalbody;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004339
4340 if (PyObject_HasAttrString(obj, "body")) {
4341 int res;
4342 Py_ssize_t len;
4343 Py_ssize_t i;
4344 tmp = PyObject_GetAttrString(obj, "body");
4345 if (tmp == NULL) goto failed;
4346 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004347 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 +00004348 goto failed;
4349 }
4350 len = PyList_GET_SIZE(tmp);
4351 body = asdl_seq_new(len, arena);
4352 if (body == NULL) goto failed;
4353 for (i = 0; i < len; i++) {
4354 stmt_ty value;
4355 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4356 if (res != 0) goto failed;
4357 asdl_seq_SET(body, i, value);
4358 }
4359 Py_XDECREF(tmp);
4360 tmp = NULL;
4361 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004362 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004363 return 1;
4364 }
4365 if (PyObject_HasAttrString(obj, "handlers")) {
4366 int res;
4367 Py_ssize_t len;
4368 Py_ssize_t i;
4369 tmp = PyObject_GetAttrString(obj, "handlers");
4370 if (tmp == NULL) goto failed;
4371 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004372 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 +00004373 goto failed;
4374 }
4375 len = PyList_GET_SIZE(tmp);
4376 handlers = asdl_seq_new(len, arena);
4377 if (handlers == NULL) goto failed;
4378 for (i = 0; i < len; i++) {
4379 excepthandler_ty value;
4380 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4381 if (res != 0) goto failed;
4382 asdl_seq_SET(handlers, i, value);
4383 }
4384 Py_XDECREF(tmp);
4385 tmp = NULL;
4386 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004387 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004388 return 1;
4389 }
4390 if (PyObject_HasAttrString(obj, "orelse")) {
4391 int res;
4392 Py_ssize_t len;
4393 Py_ssize_t i;
4394 tmp = PyObject_GetAttrString(obj, "orelse");
4395 if (tmp == NULL) goto failed;
4396 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004397 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 +00004398 goto failed;
4399 }
4400 len = PyList_GET_SIZE(tmp);
4401 orelse = asdl_seq_new(len, arena);
4402 if (orelse == NULL) goto failed;
4403 for (i = 0; i < len; i++) {
4404 stmt_ty value;
4405 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4406 if (res != 0) goto failed;
4407 asdl_seq_SET(orelse, i, value);
4408 }
4409 Py_XDECREF(tmp);
4410 tmp = NULL;
4411 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004412 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004413 return 1;
4414 }
4415 if (PyObject_HasAttrString(obj, "finalbody")) {
4416 int res;
4417 Py_ssize_t len;
4418 Py_ssize_t i;
4419 tmp = PyObject_GetAttrString(obj, "finalbody");
4420 if (tmp == NULL) goto failed;
4421 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004422 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 +00004423 goto failed;
4424 }
4425 len = PyList_GET_SIZE(tmp);
4426 finalbody = asdl_seq_new(len, arena);
4427 if (finalbody == NULL) goto failed;
4428 for (i = 0; i < len; i++) {
4429 stmt_ty value;
4430 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4431 if (res != 0) goto failed;
4432 asdl_seq_SET(finalbody, i, value);
4433 }
4434 Py_XDECREF(tmp);
4435 tmp = NULL;
4436 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004437 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004438 return 1;
4439 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004440 *out = Try(body, handlers, orelse, finalbody, lineno,
4441 col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004442 if (*out == NULL) goto failed;
4443 return 0;
4444 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004445 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4446 if (isinstance == -1) {
4447 return 1;
4448 }
4449 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004450 expr_ty test;
4451 expr_ty msg;
4452
4453 if (PyObject_HasAttrString(obj, "test")) {
4454 int res;
4455 tmp = PyObject_GetAttrString(obj, "test");
4456 if (tmp == NULL) goto failed;
4457 res = obj2ast_expr(tmp, &test, arena);
4458 if (res != 0) goto failed;
4459 Py_XDECREF(tmp);
4460 tmp = NULL;
4461 } else {
4462 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4463 return 1;
4464 }
4465 if (PyObject_HasAttrString(obj, "msg")) {
4466 int res;
4467 tmp = PyObject_GetAttrString(obj, "msg");
4468 if (tmp == NULL) goto failed;
4469 res = obj2ast_expr(tmp, &msg, arena);
4470 if (res != 0) goto failed;
4471 Py_XDECREF(tmp);
4472 tmp = NULL;
4473 } else {
4474 msg = NULL;
4475 }
4476 *out = Assert(test, msg, lineno, col_offset, arena);
4477 if (*out == NULL) goto failed;
4478 return 0;
4479 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004480 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4481 if (isinstance == -1) {
4482 return 1;
4483 }
4484 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004485 asdl_seq* names;
4486
4487 if (PyObject_HasAttrString(obj, "names")) {
4488 int res;
4489 Py_ssize_t len;
4490 Py_ssize_t i;
4491 tmp = PyObject_GetAttrString(obj, "names");
4492 if (tmp == NULL) goto failed;
4493 if (!PyList_Check(tmp)) {
4494 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4495 goto failed;
4496 }
4497 len = PyList_GET_SIZE(tmp);
4498 names = asdl_seq_new(len, arena);
4499 if (names == NULL) goto failed;
4500 for (i = 0; i < len; i++) {
4501 alias_ty value;
4502 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4503 if (res != 0) goto failed;
4504 asdl_seq_SET(names, i, value);
4505 }
4506 Py_XDECREF(tmp);
4507 tmp = NULL;
4508 } else {
4509 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4510 return 1;
4511 }
4512 *out = Import(names, lineno, col_offset, arena);
4513 if (*out == NULL) goto failed;
4514 return 0;
4515 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004516 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4517 if (isinstance == -1) {
4518 return 1;
4519 }
4520 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004521 identifier module;
4522 asdl_seq* names;
4523 int level;
4524
4525 if (PyObject_HasAttrString(obj, "module")) {
4526 int res;
4527 tmp = PyObject_GetAttrString(obj, "module");
4528 if (tmp == NULL) goto failed;
4529 res = obj2ast_identifier(tmp, &module, arena);
4530 if (res != 0) goto failed;
4531 Py_XDECREF(tmp);
4532 tmp = NULL;
4533 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004534 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004535 }
4536 if (PyObject_HasAttrString(obj, "names")) {
4537 int res;
4538 Py_ssize_t len;
4539 Py_ssize_t i;
4540 tmp = PyObject_GetAttrString(obj, "names");
4541 if (tmp == NULL) goto failed;
4542 if (!PyList_Check(tmp)) {
4543 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4544 goto failed;
4545 }
4546 len = PyList_GET_SIZE(tmp);
4547 names = asdl_seq_new(len, arena);
4548 if (names == NULL) goto failed;
4549 for (i = 0; i < len; i++) {
4550 alias_ty value;
4551 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4552 if (res != 0) goto failed;
4553 asdl_seq_SET(names, i, value);
4554 }
4555 Py_XDECREF(tmp);
4556 tmp = NULL;
4557 } else {
4558 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4559 return 1;
4560 }
4561 if (PyObject_HasAttrString(obj, "level")) {
4562 int res;
4563 tmp = PyObject_GetAttrString(obj, "level");
4564 if (tmp == NULL) goto failed;
4565 res = obj2ast_int(tmp, &level, arena);
4566 if (res != 0) goto failed;
4567 Py_XDECREF(tmp);
4568 tmp = NULL;
4569 } else {
4570 level = 0;
4571 }
4572 *out = ImportFrom(module, names, level, lineno, col_offset,
4573 arena);
4574 if (*out == NULL) goto failed;
4575 return 0;
4576 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004577 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4578 if (isinstance == -1) {
4579 return 1;
4580 }
4581 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004582 asdl_seq* names;
4583
4584 if (PyObject_HasAttrString(obj, "names")) {
4585 int res;
4586 Py_ssize_t len;
4587 Py_ssize_t i;
4588 tmp = PyObject_GetAttrString(obj, "names");
4589 if (tmp == NULL) goto failed;
4590 if (!PyList_Check(tmp)) {
4591 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4592 goto failed;
4593 }
4594 len = PyList_GET_SIZE(tmp);
4595 names = asdl_seq_new(len, arena);
4596 if (names == NULL) goto failed;
4597 for (i = 0; i < len; i++) {
4598 identifier value;
4599 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4600 if (res != 0) goto failed;
4601 asdl_seq_SET(names, i, value);
4602 }
4603 Py_XDECREF(tmp);
4604 tmp = NULL;
4605 } else {
4606 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4607 return 1;
4608 }
4609 *out = Global(names, lineno, col_offset, arena);
4610 if (*out == NULL) goto failed;
4611 return 0;
4612 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004613 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4614 if (isinstance == -1) {
4615 return 1;
4616 }
4617 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004618 asdl_seq* names;
4619
4620 if (PyObject_HasAttrString(obj, "names")) {
4621 int res;
4622 Py_ssize_t len;
4623 Py_ssize_t i;
4624 tmp = PyObject_GetAttrString(obj, "names");
4625 if (tmp == NULL) goto failed;
4626 if (!PyList_Check(tmp)) {
4627 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4628 goto failed;
4629 }
4630 len = PyList_GET_SIZE(tmp);
4631 names = asdl_seq_new(len, arena);
4632 if (names == NULL) goto failed;
4633 for (i = 0; i < len; i++) {
4634 identifier value;
4635 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4636 if (res != 0) goto failed;
4637 asdl_seq_SET(names, i, value);
4638 }
4639 Py_XDECREF(tmp);
4640 tmp = NULL;
4641 } else {
4642 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4643 return 1;
4644 }
4645 *out = Nonlocal(names, lineno, col_offset, arena);
4646 if (*out == NULL) goto failed;
4647 return 0;
4648 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004649 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4650 if (isinstance == -1) {
4651 return 1;
4652 }
4653 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004654 expr_ty value;
4655
4656 if (PyObject_HasAttrString(obj, "value")) {
4657 int res;
4658 tmp = PyObject_GetAttrString(obj, "value");
4659 if (tmp == NULL) goto failed;
4660 res = obj2ast_expr(tmp, &value, arena);
4661 if (res != 0) goto failed;
4662 Py_XDECREF(tmp);
4663 tmp = NULL;
4664 } else {
4665 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4666 return 1;
4667 }
4668 *out = Expr(value, lineno, col_offset, arena);
4669 if (*out == NULL) goto failed;
4670 return 0;
4671 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004672 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4673 if (isinstance == -1) {
4674 return 1;
4675 }
4676 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004677
4678 *out = Pass(lineno, col_offset, arena);
4679 if (*out == NULL) goto failed;
4680 return 0;
4681 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004682 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4683 if (isinstance == -1) {
4684 return 1;
4685 }
4686 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004687
4688 *out = Break(lineno, col_offset, arena);
4689 if (*out == NULL) goto failed;
4690 return 0;
4691 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004692 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4693 if (isinstance == -1) {
4694 return 1;
4695 }
4696 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004697
4698 *out = Continue(lineno, col_offset, arena);
4699 if (*out == NULL) goto failed;
4700 return 0;
4701 }
4702
Benjamin Peterson5b066812010-11-20 01:38:49 +00004703 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4704 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004705 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004706 return 1;
4707}
4708
4709int
4710obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4711{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004712 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004713
Benjamin Petersond8f65972010-11-20 04:31:07 +00004714 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004715 int lineno;
4716 int col_offset;
4717
4718 if (obj == Py_None) {
4719 *out = NULL;
4720 return 0;
4721 }
4722 if (PyObject_HasAttrString(obj, "lineno")) {
4723 int res;
4724 tmp = PyObject_GetAttrString(obj, "lineno");
4725 if (tmp == NULL) goto failed;
4726 res = obj2ast_int(tmp, &lineno, arena);
4727 if (res != 0) goto failed;
4728 Py_XDECREF(tmp);
4729 tmp = NULL;
4730 } else {
4731 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4732 return 1;
4733 }
4734 if (PyObject_HasAttrString(obj, "col_offset")) {
4735 int res;
4736 tmp = PyObject_GetAttrString(obj, "col_offset");
4737 if (tmp == NULL) goto failed;
4738 res = obj2ast_int(tmp, &col_offset, arena);
4739 if (res != 0) goto failed;
4740 Py_XDECREF(tmp);
4741 tmp = NULL;
4742 } else {
4743 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4744 return 1;
4745 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004746 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4747 if (isinstance == -1) {
4748 return 1;
4749 }
4750 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004751 boolop_ty op;
4752 asdl_seq* values;
4753
4754 if (PyObject_HasAttrString(obj, "op")) {
4755 int res;
4756 tmp = PyObject_GetAttrString(obj, "op");
4757 if (tmp == NULL) goto failed;
4758 res = obj2ast_boolop(tmp, &op, arena);
4759 if (res != 0) goto failed;
4760 Py_XDECREF(tmp);
4761 tmp = NULL;
4762 } else {
4763 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4764 return 1;
4765 }
4766 if (PyObject_HasAttrString(obj, "values")) {
4767 int res;
4768 Py_ssize_t len;
4769 Py_ssize_t i;
4770 tmp = PyObject_GetAttrString(obj, "values");
4771 if (tmp == NULL) goto failed;
4772 if (!PyList_Check(tmp)) {
4773 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4774 goto failed;
4775 }
4776 len = PyList_GET_SIZE(tmp);
4777 values = asdl_seq_new(len, arena);
4778 if (values == NULL) goto failed;
4779 for (i = 0; i < len; i++) {
4780 expr_ty value;
4781 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4782 if (res != 0) goto failed;
4783 asdl_seq_SET(values, i, value);
4784 }
4785 Py_XDECREF(tmp);
4786 tmp = NULL;
4787 } else {
4788 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4789 return 1;
4790 }
4791 *out = BoolOp(op, values, lineno, col_offset, arena);
4792 if (*out == NULL) goto failed;
4793 return 0;
4794 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004795 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4796 if (isinstance == -1) {
4797 return 1;
4798 }
4799 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004800 expr_ty left;
4801 operator_ty op;
4802 expr_ty right;
4803
4804 if (PyObject_HasAttrString(obj, "left")) {
4805 int res;
4806 tmp = PyObject_GetAttrString(obj, "left");
4807 if (tmp == NULL) goto failed;
4808 res = obj2ast_expr(tmp, &left, arena);
4809 if (res != 0) goto failed;
4810 Py_XDECREF(tmp);
4811 tmp = NULL;
4812 } else {
4813 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4814 return 1;
4815 }
4816 if (PyObject_HasAttrString(obj, "op")) {
4817 int res;
4818 tmp = PyObject_GetAttrString(obj, "op");
4819 if (tmp == NULL) goto failed;
4820 res = obj2ast_operator(tmp, &op, arena);
4821 if (res != 0) goto failed;
4822 Py_XDECREF(tmp);
4823 tmp = NULL;
4824 } else {
4825 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4826 return 1;
4827 }
4828 if (PyObject_HasAttrString(obj, "right")) {
4829 int res;
4830 tmp = PyObject_GetAttrString(obj, "right");
4831 if (tmp == NULL) goto failed;
4832 res = obj2ast_expr(tmp, &right, arena);
4833 if (res != 0) goto failed;
4834 Py_XDECREF(tmp);
4835 tmp = NULL;
4836 } else {
4837 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4838 return 1;
4839 }
4840 *out = BinOp(left, op, right, lineno, col_offset, arena);
4841 if (*out == NULL) goto failed;
4842 return 0;
4843 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004844 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4845 if (isinstance == -1) {
4846 return 1;
4847 }
4848 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004849 unaryop_ty op;
4850 expr_ty operand;
4851
4852 if (PyObject_HasAttrString(obj, "op")) {
4853 int res;
4854 tmp = PyObject_GetAttrString(obj, "op");
4855 if (tmp == NULL) goto failed;
4856 res = obj2ast_unaryop(tmp, &op, arena);
4857 if (res != 0) goto failed;
4858 Py_XDECREF(tmp);
4859 tmp = NULL;
4860 } else {
4861 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4862 return 1;
4863 }
4864 if (PyObject_HasAttrString(obj, "operand")) {
4865 int res;
4866 tmp = PyObject_GetAttrString(obj, "operand");
4867 if (tmp == NULL) goto failed;
4868 res = obj2ast_expr(tmp, &operand, arena);
4869 if (res != 0) goto failed;
4870 Py_XDECREF(tmp);
4871 tmp = NULL;
4872 } else {
4873 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4874 return 1;
4875 }
4876 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4877 if (*out == NULL) goto failed;
4878 return 0;
4879 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004880 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4881 if (isinstance == -1) {
4882 return 1;
4883 }
4884 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004885 arguments_ty args;
4886 expr_ty body;
4887
4888 if (PyObject_HasAttrString(obj, "args")) {
4889 int res;
4890 tmp = PyObject_GetAttrString(obj, "args");
4891 if (tmp == NULL) goto failed;
4892 res = obj2ast_arguments(tmp, &args, arena);
4893 if (res != 0) goto failed;
4894 Py_XDECREF(tmp);
4895 tmp = NULL;
4896 } else {
4897 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4898 return 1;
4899 }
4900 if (PyObject_HasAttrString(obj, "body")) {
4901 int res;
4902 tmp = PyObject_GetAttrString(obj, "body");
4903 if (tmp == NULL) goto failed;
4904 res = obj2ast_expr(tmp, &body, arena);
4905 if (res != 0) goto failed;
4906 Py_XDECREF(tmp);
4907 tmp = NULL;
4908 } else {
4909 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4910 return 1;
4911 }
4912 *out = Lambda(args, body, lineno, col_offset, arena);
4913 if (*out == NULL) goto failed;
4914 return 0;
4915 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004916 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4917 if (isinstance == -1) {
4918 return 1;
4919 }
4920 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004921 expr_ty test;
4922 expr_ty body;
4923 expr_ty orelse;
4924
4925 if (PyObject_HasAttrString(obj, "test")) {
4926 int res;
4927 tmp = PyObject_GetAttrString(obj, "test");
4928 if (tmp == NULL) goto failed;
4929 res = obj2ast_expr(tmp, &test, arena);
4930 if (res != 0) goto failed;
4931 Py_XDECREF(tmp);
4932 tmp = NULL;
4933 } else {
4934 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4935 return 1;
4936 }
4937 if (PyObject_HasAttrString(obj, "body")) {
4938 int res;
4939 tmp = PyObject_GetAttrString(obj, "body");
4940 if (tmp == NULL) goto failed;
4941 res = obj2ast_expr(tmp, &body, arena);
4942 if (res != 0) goto failed;
4943 Py_XDECREF(tmp);
4944 tmp = NULL;
4945 } else {
4946 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4947 return 1;
4948 }
4949 if (PyObject_HasAttrString(obj, "orelse")) {
4950 int res;
4951 tmp = PyObject_GetAttrString(obj, "orelse");
4952 if (tmp == NULL) goto failed;
4953 res = obj2ast_expr(tmp, &orelse, arena);
4954 if (res != 0) goto failed;
4955 Py_XDECREF(tmp);
4956 tmp = NULL;
4957 } else {
4958 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4959 return 1;
4960 }
4961 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4962 if (*out == NULL) goto failed;
4963 return 0;
4964 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004965 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4966 if (isinstance == -1) {
4967 return 1;
4968 }
4969 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004970 asdl_seq* keys;
4971 asdl_seq* values;
4972
4973 if (PyObject_HasAttrString(obj, "keys")) {
4974 int res;
4975 Py_ssize_t len;
4976 Py_ssize_t i;
4977 tmp = PyObject_GetAttrString(obj, "keys");
4978 if (tmp == NULL) goto failed;
4979 if (!PyList_Check(tmp)) {
4980 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4981 goto failed;
4982 }
4983 len = PyList_GET_SIZE(tmp);
4984 keys = asdl_seq_new(len, arena);
4985 if (keys == NULL) goto failed;
4986 for (i = 0; i < len; i++) {
4987 expr_ty value;
4988 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4989 if (res != 0) goto failed;
4990 asdl_seq_SET(keys, i, value);
4991 }
4992 Py_XDECREF(tmp);
4993 tmp = NULL;
4994 } else {
4995 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4996 return 1;
4997 }
4998 if (PyObject_HasAttrString(obj, "values")) {
4999 int res;
5000 Py_ssize_t len;
5001 Py_ssize_t i;
5002 tmp = PyObject_GetAttrString(obj, "values");
5003 if (tmp == NULL) goto failed;
5004 if (!PyList_Check(tmp)) {
5005 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5006 goto failed;
5007 }
5008 len = PyList_GET_SIZE(tmp);
5009 values = asdl_seq_new(len, arena);
5010 if (values == NULL) goto failed;
5011 for (i = 0; i < len; i++) {
5012 expr_ty value;
5013 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5014 if (res != 0) goto failed;
5015 asdl_seq_SET(values, i, value);
5016 }
5017 Py_XDECREF(tmp);
5018 tmp = NULL;
5019 } else {
5020 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5021 return 1;
5022 }
5023 *out = Dict(keys, values, lineno, col_offset, arena);
5024 if (*out == NULL) goto failed;
5025 return 0;
5026 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005027 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5028 if (isinstance == -1) {
5029 return 1;
5030 }
5031 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005032 asdl_seq* elts;
5033
5034 if (PyObject_HasAttrString(obj, "elts")) {
5035 int res;
5036 Py_ssize_t len;
5037 Py_ssize_t i;
5038 tmp = PyObject_GetAttrString(obj, "elts");
5039 if (tmp == NULL) goto failed;
5040 if (!PyList_Check(tmp)) {
5041 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5042 goto failed;
5043 }
5044 len = PyList_GET_SIZE(tmp);
5045 elts = asdl_seq_new(len, arena);
5046 if (elts == NULL) goto failed;
5047 for (i = 0; i < len; i++) {
5048 expr_ty value;
5049 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5050 if (res != 0) goto failed;
5051 asdl_seq_SET(elts, i, value);
5052 }
5053 Py_XDECREF(tmp);
5054 tmp = NULL;
5055 } else {
5056 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5057 return 1;
5058 }
5059 *out = Set(elts, lineno, col_offset, arena);
5060 if (*out == NULL) goto failed;
5061 return 0;
5062 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005063 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5064 if (isinstance == -1) {
5065 return 1;
5066 }
5067 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005068 expr_ty elt;
5069 asdl_seq* generators;
5070
5071 if (PyObject_HasAttrString(obj, "elt")) {
5072 int res;
5073 tmp = PyObject_GetAttrString(obj, "elt");
5074 if (tmp == NULL) goto failed;
5075 res = obj2ast_expr(tmp, &elt, arena);
5076 if (res != 0) goto failed;
5077 Py_XDECREF(tmp);
5078 tmp = NULL;
5079 } else {
5080 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5081 return 1;
5082 }
5083 if (PyObject_HasAttrString(obj, "generators")) {
5084 int res;
5085 Py_ssize_t len;
5086 Py_ssize_t i;
5087 tmp = PyObject_GetAttrString(obj, "generators");
5088 if (tmp == NULL) goto failed;
5089 if (!PyList_Check(tmp)) {
5090 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5091 goto failed;
5092 }
5093 len = PyList_GET_SIZE(tmp);
5094 generators = asdl_seq_new(len, arena);
5095 if (generators == NULL) goto failed;
5096 for (i = 0; i < len; i++) {
5097 comprehension_ty value;
5098 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5099 if (res != 0) goto failed;
5100 asdl_seq_SET(generators, i, value);
5101 }
5102 Py_XDECREF(tmp);
5103 tmp = NULL;
5104 } else {
5105 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5106 return 1;
5107 }
5108 *out = ListComp(elt, generators, lineno, col_offset, arena);
5109 if (*out == NULL) goto failed;
5110 return 0;
5111 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005112 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5113 if (isinstance == -1) {
5114 return 1;
5115 }
5116 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005117 expr_ty elt;
5118 asdl_seq* generators;
5119
5120 if (PyObject_HasAttrString(obj, "elt")) {
5121 int res;
5122 tmp = PyObject_GetAttrString(obj, "elt");
5123 if (tmp == NULL) goto failed;
5124 res = obj2ast_expr(tmp, &elt, arena);
5125 if (res != 0) goto failed;
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5130 return 1;
5131 }
5132 if (PyObject_HasAttrString(obj, "generators")) {
5133 int res;
5134 Py_ssize_t len;
5135 Py_ssize_t i;
5136 tmp = PyObject_GetAttrString(obj, "generators");
5137 if (tmp == NULL) goto failed;
5138 if (!PyList_Check(tmp)) {
5139 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5140 goto failed;
5141 }
5142 len = PyList_GET_SIZE(tmp);
5143 generators = asdl_seq_new(len, arena);
5144 if (generators == NULL) goto failed;
5145 for (i = 0; i < len; i++) {
5146 comprehension_ty value;
5147 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5148 if (res != 0) goto failed;
5149 asdl_seq_SET(generators, i, value);
5150 }
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5155 return 1;
5156 }
5157 *out = SetComp(elt, generators, lineno, col_offset, arena);
5158 if (*out == NULL) goto failed;
5159 return 0;
5160 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005161 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5162 if (isinstance == -1) {
5163 return 1;
5164 }
5165 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005166 expr_ty key;
5167 expr_ty value;
5168 asdl_seq* generators;
5169
5170 if (PyObject_HasAttrString(obj, "key")) {
5171 int res;
5172 tmp = PyObject_GetAttrString(obj, "key");
5173 if (tmp == NULL) goto failed;
5174 res = obj2ast_expr(tmp, &key, arena);
5175 if (res != 0) goto failed;
5176 Py_XDECREF(tmp);
5177 tmp = NULL;
5178 } else {
5179 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5180 return 1;
5181 }
5182 if (PyObject_HasAttrString(obj, "value")) {
5183 int res;
5184 tmp = PyObject_GetAttrString(obj, "value");
5185 if (tmp == NULL) goto failed;
5186 res = obj2ast_expr(tmp, &value, arena);
5187 if (res != 0) goto failed;
5188 Py_XDECREF(tmp);
5189 tmp = NULL;
5190 } else {
5191 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5192 return 1;
5193 }
5194 if (PyObject_HasAttrString(obj, "generators")) {
5195 int res;
5196 Py_ssize_t len;
5197 Py_ssize_t i;
5198 tmp = PyObject_GetAttrString(obj, "generators");
5199 if (tmp == NULL) goto failed;
5200 if (!PyList_Check(tmp)) {
5201 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5202 goto failed;
5203 }
5204 len = PyList_GET_SIZE(tmp);
5205 generators = asdl_seq_new(len, arena);
5206 if (generators == NULL) goto failed;
5207 for (i = 0; i < len; i++) {
5208 comprehension_ty value;
5209 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5210 if (res != 0) goto failed;
5211 asdl_seq_SET(generators, i, value);
5212 }
5213 Py_XDECREF(tmp);
5214 tmp = NULL;
5215 } else {
5216 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5217 return 1;
5218 }
5219 *out = DictComp(key, value, generators, lineno, col_offset,
5220 arena);
5221 if (*out == NULL) goto failed;
5222 return 0;
5223 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005224 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5225 if (isinstance == -1) {
5226 return 1;
5227 }
5228 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005229 expr_ty elt;
5230 asdl_seq* generators;
5231
5232 if (PyObject_HasAttrString(obj, "elt")) {
5233 int res;
5234 tmp = PyObject_GetAttrString(obj, "elt");
5235 if (tmp == NULL) goto failed;
5236 res = obj2ast_expr(tmp, &elt, arena);
5237 if (res != 0) goto failed;
5238 Py_XDECREF(tmp);
5239 tmp = NULL;
5240 } else {
5241 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5242 return 1;
5243 }
5244 if (PyObject_HasAttrString(obj, "generators")) {
5245 int res;
5246 Py_ssize_t len;
5247 Py_ssize_t i;
5248 tmp = PyObject_GetAttrString(obj, "generators");
5249 if (tmp == NULL) goto failed;
5250 if (!PyList_Check(tmp)) {
5251 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5252 goto failed;
5253 }
5254 len = PyList_GET_SIZE(tmp);
5255 generators = asdl_seq_new(len, arena);
5256 if (generators == NULL) goto failed;
5257 for (i = 0; i < len; i++) {
5258 comprehension_ty value;
5259 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5260 if (res != 0) goto failed;
5261 asdl_seq_SET(generators, i, value);
5262 }
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5267 return 1;
5268 }
5269 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5270 if (*out == NULL) goto failed;
5271 return 0;
5272 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005273 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5274 if (isinstance == -1) {
5275 return 1;
5276 }
5277 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005278 expr_ty value;
5279
5280 if (PyObject_HasAttrString(obj, "value")) {
5281 int res;
5282 tmp = PyObject_GetAttrString(obj, "value");
5283 if (tmp == NULL) goto failed;
5284 res = obj2ast_expr(tmp, &value, arena);
5285 if (res != 0) goto failed;
5286 Py_XDECREF(tmp);
5287 tmp = NULL;
5288 } else {
5289 value = NULL;
5290 }
5291 *out = Yield(value, lineno, col_offset, arena);
5292 if (*out == NULL) goto failed;
5293 return 0;
5294 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005295 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5296 if (isinstance == -1) {
5297 return 1;
5298 }
5299 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005300 expr_ty left;
5301 asdl_int_seq* ops;
5302 asdl_seq* comparators;
5303
5304 if (PyObject_HasAttrString(obj, "left")) {
5305 int res;
5306 tmp = PyObject_GetAttrString(obj, "left");
5307 if (tmp == NULL) goto failed;
5308 res = obj2ast_expr(tmp, &left, arena);
5309 if (res != 0) goto failed;
5310 Py_XDECREF(tmp);
5311 tmp = NULL;
5312 } else {
5313 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5314 return 1;
5315 }
5316 if (PyObject_HasAttrString(obj, "ops")) {
5317 int res;
5318 Py_ssize_t len;
5319 Py_ssize_t i;
5320 tmp = PyObject_GetAttrString(obj, "ops");
5321 if (tmp == NULL) goto failed;
5322 if (!PyList_Check(tmp)) {
5323 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5324 goto failed;
5325 }
5326 len = PyList_GET_SIZE(tmp);
5327 ops = asdl_int_seq_new(len, arena);
5328 if (ops == NULL) goto failed;
5329 for (i = 0; i < len; i++) {
5330 cmpop_ty value;
5331 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5332 if (res != 0) goto failed;
5333 asdl_seq_SET(ops, i, value);
5334 }
5335 Py_XDECREF(tmp);
5336 tmp = NULL;
5337 } else {
5338 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5339 return 1;
5340 }
5341 if (PyObject_HasAttrString(obj, "comparators")) {
5342 int res;
5343 Py_ssize_t len;
5344 Py_ssize_t i;
5345 tmp = PyObject_GetAttrString(obj, "comparators");
5346 if (tmp == NULL) goto failed;
5347 if (!PyList_Check(tmp)) {
5348 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5349 goto failed;
5350 }
5351 len = PyList_GET_SIZE(tmp);
5352 comparators = asdl_seq_new(len, arena);
5353 if (comparators == NULL) goto failed;
5354 for (i = 0; i < len; i++) {
5355 expr_ty value;
5356 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5357 if (res != 0) goto failed;
5358 asdl_seq_SET(comparators, i, value);
5359 }
5360 Py_XDECREF(tmp);
5361 tmp = NULL;
5362 } else {
5363 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5364 return 1;
5365 }
5366 *out = Compare(left, ops, comparators, lineno, col_offset,
5367 arena);
5368 if (*out == NULL) goto failed;
5369 return 0;
5370 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005371 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5372 if (isinstance == -1) {
5373 return 1;
5374 }
5375 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005376 expr_ty func;
5377 asdl_seq* args;
5378 asdl_seq* keywords;
5379 expr_ty starargs;
5380 expr_ty kwargs;
5381
5382 if (PyObject_HasAttrString(obj, "func")) {
5383 int res;
5384 tmp = PyObject_GetAttrString(obj, "func");
5385 if (tmp == NULL) goto failed;
5386 res = obj2ast_expr(tmp, &func, arena);
5387 if (res != 0) goto failed;
5388 Py_XDECREF(tmp);
5389 tmp = NULL;
5390 } else {
5391 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5392 return 1;
5393 }
5394 if (PyObject_HasAttrString(obj, "args")) {
5395 int res;
5396 Py_ssize_t len;
5397 Py_ssize_t i;
5398 tmp = PyObject_GetAttrString(obj, "args");
5399 if (tmp == NULL) goto failed;
5400 if (!PyList_Check(tmp)) {
5401 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5402 goto failed;
5403 }
5404 len = PyList_GET_SIZE(tmp);
5405 args = asdl_seq_new(len, arena);
5406 if (args == NULL) goto failed;
5407 for (i = 0; i < len; i++) {
5408 expr_ty value;
5409 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5410 if (res != 0) goto failed;
5411 asdl_seq_SET(args, i, value);
5412 }
5413 Py_XDECREF(tmp);
5414 tmp = NULL;
5415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5417 return 1;
5418 }
5419 if (PyObject_HasAttrString(obj, "keywords")) {
5420 int res;
5421 Py_ssize_t len;
5422 Py_ssize_t i;
5423 tmp = PyObject_GetAttrString(obj, "keywords");
5424 if (tmp == NULL) goto failed;
5425 if (!PyList_Check(tmp)) {
5426 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5427 goto failed;
5428 }
5429 len = PyList_GET_SIZE(tmp);
5430 keywords = asdl_seq_new(len, arena);
5431 if (keywords == NULL) goto failed;
5432 for (i = 0; i < len; i++) {
5433 keyword_ty value;
5434 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5435 if (res != 0) goto failed;
5436 asdl_seq_SET(keywords, i, value);
5437 }
5438 Py_XDECREF(tmp);
5439 tmp = NULL;
5440 } else {
5441 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5442 return 1;
5443 }
5444 if (PyObject_HasAttrString(obj, "starargs")) {
5445 int res;
5446 tmp = PyObject_GetAttrString(obj, "starargs");
5447 if (tmp == NULL) goto failed;
5448 res = obj2ast_expr(tmp, &starargs, arena);
5449 if (res != 0) goto failed;
5450 Py_XDECREF(tmp);
5451 tmp = NULL;
5452 } else {
5453 starargs = NULL;
5454 }
5455 if (PyObject_HasAttrString(obj, "kwargs")) {
5456 int res;
5457 tmp = PyObject_GetAttrString(obj, "kwargs");
5458 if (tmp == NULL) goto failed;
5459 res = obj2ast_expr(tmp, &kwargs, arena);
5460 if (res != 0) goto failed;
5461 Py_XDECREF(tmp);
5462 tmp = NULL;
5463 } else {
5464 kwargs = NULL;
5465 }
5466 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5467 col_offset, arena);
5468 if (*out == NULL) goto failed;
5469 return 0;
5470 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005471 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5472 if (isinstance == -1) {
5473 return 1;
5474 }
5475 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005476 object n;
5477
5478 if (PyObject_HasAttrString(obj, "n")) {
5479 int res;
5480 tmp = PyObject_GetAttrString(obj, "n");
5481 if (tmp == NULL) goto failed;
5482 res = obj2ast_object(tmp, &n, arena);
5483 if (res != 0) goto failed;
5484 Py_XDECREF(tmp);
5485 tmp = NULL;
5486 } else {
5487 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5488 return 1;
5489 }
5490 *out = Num(n, lineno, col_offset, arena);
5491 if (*out == NULL) goto failed;
5492 return 0;
5493 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005494 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5495 if (isinstance == -1) {
5496 return 1;
5497 }
5498 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005499 string s;
5500
5501 if (PyObject_HasAttrString(obj, "s")) {
5502 int res;
5503 tmp = PyObject_GetAttrString(obj, "s");
5504 if (tmp == NULL) goto failed;
5505 res = obj2ast_string(tmp, &s, arena);
5506 if (res != 0) goto failed;
5507 Py_XDECREF(tmp);
5508 tmp = NULL;
5509 } else {
5510 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5511 return 1;
5512 }
5513 *out = Str(s, lineno, col_offset, arena);
5514 if (*out == NULL) goto failed;
5515 return 0;
5516 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005517 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5518 if (isinstance == -1) {
5519 return 1;
5520 }
5521 if (isinstance) {
Benjamin Petersone2498412011-08-09 16:08:39 -05005522 bytes s;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005523
5524 if (PyObject_HasAttrString(obj, "s")) {
5525 int res;
5526 tmp = PyObject_GetAttrString(obj, "s");
5527 if (tmp == NULL) goto failed;
Benjamin Petersone2498412011-08-09 16:08:39 -05005528 res = obj2ast_bytes(tmp, &s, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005529 if (res != 0) goto failed;
5530 Py_XDECREF(tmp);
5531 tmp = NULL;
5532 } else {
5533 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5534 return 1;
5535 }
5536 *out = Bytes(s, lineno, col_offset, arena);
5537 if (*out == NULL) goto failed;
5538 return 0;
5539 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005540 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5541 if (isinstance == -1) {
5542 return 1;
5543 }
5544 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005545
5546 *out = Ellipsis(lineno, col_offset, arena);
5547 if (*out == NULL) goto failed;
5548 return 0;
5549 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005550 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5551 if (isinstance == -1) {
5552 return 1;
5553 }
5554 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005555 expr_ty value;
5556 identifier attr;
5557 expr_context_ty ctx;
5558
5559 if (PyObject_HasAttrString(obj, "value")) {
5560 int res;
5561 tmp = PyObject_GetAttrString(obj, "value");
5562 if (tmp == NULL) goto failed;
5563 res = obj2ast_expr(tmp, &value, arena);
5564 if (res != 0) goto failed;
5565 Py_XDECREF(tmp);
5566 tmp = NULL;
5567 } else {
5568 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5569 return 1;
5570 }
5571 if (PyObject_HasAttrString(obj, "attr")) {
5572 int res;
5573 tmp = PyObject_GetAttrString(obj, "attr");
5574 if (tmp == NULL) goto failed;
5575 res = obj2ast_identifier(tmp, &attr, arena);
5576 if (res != 0) goto failed;
5577 Py_XDECREF(tmp);
5578 tmp = NULL;
5579 } else {
5580 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5581 return 1;
5582 }
5583 if (PyObject_HasAttrString(obj, "ctx")) {
5584 int res;
5585 tmp = PyObject_GetAttrString(obj, "ctx");
5586 if (tmp == NULL) goto failed;
5587 res = obj2ast_expr_context(tmp, &ctx, arena);
5588 if (res != 0) goto failed;
5589 Py_XDECREF(tmp);
5590 tmp = NULL;
5591 } else {
5592 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5593 return 1;
5594 }
5595 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5596 if (*out == NULL) goto failed;
5597 return 0;
5598 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005599 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5600 if (isinstance == -1) {
5601 return 1;
5602 }
5603 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005604 expr_ty value;
5605 slice_ty slice;
5606 expr_context_ty ctx;
5607
5608 if (PyObject_HasAttrString(obj, "value")) {
5609 int res;
5610 tmp = PyObject_GetAttrString(obj, "value");
5611 if (tmp == NULL) goto failed;
5612 res = obj2ast_expr(tmp, &value, arena);
5613 if (res != 0) goto failed;
5614 Py_XDECREF(tmp);
5615 tmp = NULL;
5616 } else {
5617 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5618 return 1;
5619 }
5620 if (PyObject_HasAttrString(obj, "slice")) {
5621 int res;
5622 tmp = PyObject_GetAttrString(obj, "slice");
5623 if (tmp == NULL) goto failed;
5624 res = obj2ast_slice(tmp, &slice, arena);
5625 if (res != 0) goto failed;
5626 Py_XDECREF(tmp);
5627 tmp = NULL;
5628 } else {
5629 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5630 return 1;
5631 }
5632 if (PyObject_HasAttrString(obj, "ctx")) {
5633 int res;
5634 tmp = PyObject_GetAttrString(obj, "ctx");
5635 if (tmp == NULL) goto failed;
5636 res = obj2ast_expr_context(tmp, &ctx, arena);
5637 if (res != 0) goto failed;
5638 Py_XDECREF(tmp);
5639 tmp = NULL;
5640 } else {
5641 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5642 return 1;
5643 }
5644 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5645 if (*out == NULL) goto failed;
5646 return 0;
5647 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005648 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5649 if (isinstance == -1) {
5650 return 1;
5651 }
5652 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005653 expr_ty value;
5654 expr_context_ty ctx;
5655
5656 if (PyObject_HasAttrString(obj, "value")) {
5657 int res;
5658 tmp = PyObject_GetAttrString(obj, "value");
5659 if (tmp == NULL) goto failed;
5660 res = obj2ast_expr(tmp, &value, arena);
5661 if (res != 0) goto failed;
5662 Py_XDECREF(tmp);
5663 tmp = NULL;
5664 } else {
5665 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5666 return 1;
5667 }
5668 if (PyObject_HasAttrString(obj, "ctx")) {
5669 int res;
5670 tmp = PyObject_GetAttrString(obj, "ctx");
5671 if (tmp == NULL) goto failed;
5672 res = obj2ast_expr_context(tmp, &ctx, arena);
5673 if (res != 0) goto failed;
5674 Py_XDECREF(tmp);
5675 tmp = NULL;
5676 } else {
5677 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5678 return 1;
5679 }
5680 *out = Starred(value, ctx, lineno, col_offset, arena);
5681 if (*out == NULL) goto failed;
5682 return 0;
5683 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005684 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5685 if (isinstance == -1) {
5686 return 1;
5687 }
5688 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005689 identifier id;
5690 expr_context_ty ctx;
5691
5692 if (PyObject_HasAttrString(obj, "id")) {
5693 int res;
5694 tmp = PyObject_GetAttrString(obj, "id");
5695 if (tmp == NULL) goto failed;
5696 res = obj2ast_identifier(tmp, &id, arena);
5697 if (res != 0) goto failed;
5698 Py_XDECREF(tmp);
5699 tmp = NULL;
5700 } else {
5701 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5702 return 1;
5703 }
5704 if (PyObject_HasAttrString(obj, "ctx")) {
5705 int res;
5706 tmp = PyObject_GetAttrString(obj, "ctx");
5707 if (tmp == NULL) goto failed;
5708 res = obj2ast_expr_context(tmp, &ctx, arena);
5709 if (res != 0) goto failed;
5710 Py_XDECREF(tmp);
5711 tmp = NULL;
5712 } else {
5713 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5714 return 1;
5715 }
5716 *out = Name(id, ctx, lineno, col_offset, arena);
5717 if (*out == NULL) goto failed;
5718 return 0;
5719 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005720 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5721 if (isinstance == -1) {
5722 return 1;
5723 }
5724 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005725 asdl_seq* elts;
5726 expr_context_ty ctx;
5727
5728 if (PyObject_HasAttrString(obj, "elts")) {
5729 int res;
5730 Py_ssize_t len;
5731 Py_ssize_t i;
5732 tmp = PyObject_GetAttrString(obj, "elts");
5733 if (tmp == NULL) goto failed;
5734 if (!PyList_Check(tmp)) {
5735 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5736 goto failed;
5737 }
5738 len = PyList_GET_SIZE(tmp);
5739 elts = asdl_seq_new(len, arena);
5740 if (elts == NULL) goto failed;
5741 for (i = 0; i < len; i++) {
5742 expr_ty value;
5743 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5744 if (res != 0) goto failed;
5745 asdl_seq_SET(elts, i, value);
5746 }
5747 Py_XDECREF(tmp);
5748 tmp = NULL;
5749 } else {
5750 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5751 return 1;
5752 }
5753 if (PyObject_HasAttrString(obj, "ctx")) {
5754 int res;
5755 tmp = PyObject_GetAttrString(obj, "ctx");
5756 if (tmp == NULL) goto failed;
5757 res = obj2ast_expr_context(tmp, &ctx, arena);
5758 if (res != 0) goto failed;
5759 Py_XDECREF(tmp);
5760 tmp = NULL;
5761 } else {
5762 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5763 return 1;
5764 }
5765 *out = List(elts, ctx, lineno, col_offset, arena);
5766 if (*out == NULL) goto failed;
5767 return 0;
5768 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005769 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5770 if (isinstance == -1) {
5771 return 1;
5772 }
5773 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005774 asdl_seq* elts;
5775 expr_context_ty ctx;
5776
5777 if (PyObject_HasAttrString(obj, "elts")) {
5778 int res;
5779 Py_ssize_t len;
5780 Py_ssize_t i;
5781 tmp = PyObject_GetAttrString(obj, "elts");
5782 if (tmp == NULL) goto failed;
5783 if (!PyList_Check(tmp)) {
5784 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5785 goto failed;
5786 }
5787 len = PyList_GET_SIZE(tmp);
5788 elts = asdl_seq_new(len, arena);
5789 if (elts == NULL) goto failed;
5790 for (i = 0; i < len; i++) {
5791 expr_ty value;
5792 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5793 if (res != 0) goto failed;
5794 asdl_seq_SET(elts, i, value);
5795 }
5796 Py_XDECREF(tmp);
5797 tmp = NULL;
5798 } else {
5799 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5800 return 1;
5801 }
5802 if (PyObject_HasAttrString(obj, "ctx")) {
5803 int res;
5804 tmp = PyObject_GetAttrString(obj, "ctx");
5805 if (tmp == NULL) goto failed;
5806 res = obj2ast_expr_context(tmp, &ctx, arena);
5807 if (res != 0) goto failed;
5808 Py_XDECREF(tmp);
5809 tmp = NULL;
5810 } else {
5811 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5812 return 1;
5813 }
5814 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5815 if (*out == NULL) goto failed;
5816 return 0;
5817 }
5818
Benjamin Peterson5b066812010-11-20 01:38:49 +00005819 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5820 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005821 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005822 return 1;
5823}
5824
5825int
5826obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5827{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005828 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005829
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005830 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5831 if (isinstance == -1) {
5832 return 1;
5833 }
5834 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005835 *out = Load;
5836 return 0;
5837 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005838 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5839 if (isinstance == -1) {
5840 return 1;
5841 }
5842 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005843 *out = Store;
5844 return 0;
5845 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005846 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5847 if (isinstance == -1) {
5848 return 1;
5849 }
5850 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005851 *out = Del;
5852 return 0;
5853 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005854 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5855 if (isinstance == -1) {
5856 return 1;
5857 }
5858 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005859 *out = AugLoad;
5860 return 0;
5861 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005862 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5863 if (isinstance == -1) {
5864 return 1;
5865 }
5866 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005867 *out = AugStore;
5868 return 0;
5869 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005870 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5871 if (isinstance == -1) {
5872 return 1;
5873 }
5874 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005875 *out = Param;
5876 return 0;
5877 }
5878
Benjamin Peterson5b066812010-11-20 01:38:49 +00005879 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005880 return 1;
5881}
5882
5883int
5884obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5885{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005886 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005887
Benjamin Petersond8f65972010-11-20 04:31:07 +00005888 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005889
5890 if (obj == Py_None) {
5891 *out = NULL;
5892 return 0;
5893 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005894 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5895 if (isinstance == -1) {
5896 return 1;
5897 }
5898 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005899 expr_ty lower;
5900 expr_ty upper;
5901 expr_ty step;
5902
5903 if (PyObject_HasAttrString(obj, "lower")) {
5904 int res;
5905 tmp = PyObject_GetAttrString(obj, "lower");
5906 if (tmp == NULL) goto failed;
5907 res = obj2ast_expr(tmp, &lower, arena);
5908 if (res != 0) goto failed;
5909 Py_XDECREF(tmp);
5910 tmp = NULL;
5911 } else {
5912 lower = NULL;
5913 }
5914 if (PyObject_HasAttrString(obj, "upper")) {
5915 int res;
5916 tmp = PyObject_GetAttrString(obj, "upper");
5917 if (tmp == NULL) goto failed;
5918 res = obj2ast_expr(tmp, &upper, arena);
5919 if (res != 0) goto failed;
5920 Py_XDECREF(tmp);
5921 tmp = NULL;
5922 } else {
5923 upper = NULL;
5924 }
5925 if (PyObject_HasAttrString(obj, "step")) {
5926 int res;
5927 tmp = PyObject_GetAttrString(obj, "step");
5928 if (tmp == NULL) goto failed;
5929 res = obj2ast_expr(tmp, &step, arena);
5930 if (res != 0) goto failed;
5931 Py_XDECREF(tmp);
5932 tmp = NULL;
5933 } else {
5934 step = NULL;
5935 }
5936 *out = Slice(lower, upper, step, arena);
5937 if (*out == NULL) goto failed;
5938 return 0;
5939 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005940 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5941 if (isinstance == -1) {
5942 return 1;
5943 }
5944 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005945 asdl_seq* dims;
5946
5947 if (PyObject_HasAttrString(obj, "dims")) {
5948 int res;
5949 Py_ssize_t len;
5950 Py_ssize_t i;
5951 tmp = PyObject_GetAttrString(obj, "dims");
5952 if (tmp == NULL) goto failed;
5953 if (!PyList_Check(tmp)) {
5954 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5955 goto failed;
5956 }
5957 len = PyList_GET_SIZE(tmp);
5958 dims = asdl_seq_new(len, arena);
5959 if (dims == NULL) goto failed;
5960 for (i = 0; i < len; i++) {
5961 slice_ty value;
5962 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5963 if (res != 0) goto failed;
5964 asdl_seq_SET(dims, i, value);
5965 }
5966 Py_XDECREF(tmp);
5967 tmp = NULL;
5968 } else {
5969 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5970 return 1;
5971 }
5972 *out = ExtSlice(dims, arena);
5973 if (*out == NULL) goto failed;
5974 return 0;
5975 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005976 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5977 if (isinstance == -1) {
5978 return 1;
5979 }
5980 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005981 expr_ty value;
5982
5983 if (PyObject_HasAttrString(obj, "value")) {
5984 int res;
5985 tmp = PyObject_GetAttrString(obj, "value");
5986 if (tmp == NULL) goto failed;
5987 res = obj2ast_expr(tmp, &value, arena);
5988 if (res != 0) goto failed;
5989 Py_XDECREF(tmp);
5990 tmp = NULL;
5991 } else {
5992 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5993 return 1;
5994 }
5995 *out = Index(value, arena);
5996 if (*out == NULL) goto failed;
5997 return 0;
5998 }
5999
Benjamin Peterson5b066812010-11-20 01:38:49 +00006000 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6001 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006002 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006003 return 1;
6004}
6005
6006int
6007obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6008{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006009 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006010
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006011 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6012 if (isinstance == -1) {
6013 return 1;
6014 }
6015 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006016 *out = And;
6017 return 0;
6018 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006019 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6020 if (isinstance == -1) {
6021 return 1;
6022 }
6023 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006024 *out = Or;
6025 return 0;
6026 }
6027
Benjamin Peterson5b066812010-11-20 01:38:49 +00006028 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006029 return 1;
6030}
6031
6032int
6033obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6034{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006035 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006036
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006037 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6038 if (isinstance == -1) {
6039 return 1;
6040 }
6041 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006042 *out = Add;
6043 return 0;
6044 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006045 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6046 if (isinstance == -1) {
6047 return 1;
6048 }
6049 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006050 *out = Sub;
6051 return 0;
6052 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006053 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6054 if (isinstance == -1) {
6055 return 1;
6056 }
6057 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006058 *out = Mult;
6059 return 0;
6060 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006061 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6062 if (isinstance == -1) {
6063 return 1;
6064 }
6065 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006066 *out = Div;
6067 return 0;
6068 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006069 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6070 if (isinstance == -1) {
6071 return 1;
6072 }
6073 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006074 *out = Mod;
6075 return 0;
6076 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006077 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6078 if (isinstance == -1) {
6079 return 1;
6080 }
6081 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006082 *out = Pow;
6083 return 0;
6084 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006085 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6086 if (isinstance == -1) {
6087 return 1;
6088 }
6089 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006090 *out = LShift;
6091 return 0;
6092 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006093 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6094 if (isinstance == -1) {
6095 return 1;
6096 }
6097 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006098 *out = RShift;
6099 return 0;
6100 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006101 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6102 if (isinstance == -1) {
6103 return 1;
6104 }
6105 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006106 *out = BitOr;
6107 return 0;
6108 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006109 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6110 if (isinstance == -1) {
6111 return 1;
6112 }
6113 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006114 *out = BitXor;
6115 return 0;
6116 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006117 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6118 if (isinstance == -1) {
6119 return 1;
6120 }
6121 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006122 *out = BitAnd;
6123 return 0;
6124 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006125 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6126 if (isinstance == -1) {
6127 return 1;
6128 }
6129 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006130 *out = FloorDiv;
6131 return 0;
6132 }
6133
Benjamin Peterson5b066812010-11-20 01:38:49 +00006134 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006135 return 1;
6136}
6137
6138int
6139obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6140{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006141 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006142
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006143 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6144 if (isinstance == -1) {
6145 return 1;
6146 }
6147 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006148 *out = Invert;
6149 return 0;
6150 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006151 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6152 if (isinstance == -1) {
6153 return 1;
6154 }
6155 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006156 *out = Not;
6157 return 0;
6158 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006159 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6160 if (isinstance == -1) {
6161 return 1;
6162 }
6163 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006164 *out = UAdd;
6165 return 0;
6166 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006167 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6168 if (isinstance == -1) {
6169 return 1;
6170 }
6171 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006172 *out = USub;
6173 return 0;
6174 }
6175
Benjamin Peterson5b066812010-11-20 01:38:49 +00006176 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006177 return 1;
6178}
6179
6180int
6181obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6182{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006183 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006184
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006185 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6186 if (isinstance == -1) {
6187 return 1;
6188 }
6189 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006190 *out = Eq;
6191 return 0;
6192 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006193 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6194 if (isinstance == -1) {
6195 return 1;
6196 }
6197 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006198 *out = NotEq;
6199 return 0;
6200 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006201 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6202 if (isinstance == -1) {
6203 return 1;
6204 }
6205 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006206 *out = Lt;
6207 return 0;
6208 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006209 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6210 if (isinstance == -1) {
6211 return 1;
6212 }
6213 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006214 *out = LtE;
6215 return 0;
6216 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006217 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6218 if (isinstance == -1) {
6219 return 1;
6220 }
6221 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006222 *out = Gt;
6223 return 0;
6224 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006225 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6226 if (isinstance == -1) {
6227 return 1;
6228 }
6229 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006230 *out = GtE;
6231 return 0;
6232 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006233 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6234 if (isinstance == -1) {
6235 return 1;
6236 }
6237 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006238 *out = Is;
6239 return 0;
6240 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006241 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6242 if (isinstance == -1) {
6243 return 1;
6244 }
6245 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006246 *out = IsNot;
6247 return 0;
6248 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006249 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6250 if (isinstance == -1) {
6251 return 1;
6252 }
6253 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006254 *out = In;
6255 return 0;
6256 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006257 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6258 if (isinstance == -1) {
6259 return 1;
6260 }
6261 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006262 *out = NotIn;
6263 return 0;
6264 }
6265
Benjamin Peterson5b066812010-11-20 01:38:49 +00006266 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006267 return 1;
6268}
6269
6270int
6271obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6272{
6273 PyObject* tmp = NULL;
6274 expr_ty target;
6275 expr_ty iter;
6276 asdl_seq* ifs;
6277
6278 if (PyObject_HasAttrString(obj, "target")) {
6279 int res;
6280 tmp = PyObject_GetAttrString(obj, "target");
6281 if (tmp == NULL) goto failed;
6282 res = obj2ast_expr(tmp, &target, arena);
6283 if (res != 0) goto failed;
6284 Py_XDECREF(tmp);
6285 tmp = NULL;
6286 } else {
6287 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6288 return 1;
6289 }
6290 if (PyObject_HasAttrString(obj, "iter")) {
6291 int res;
6292 tmp = PyObject_GetAttrString(obj, "iter");
6293 if (tmp == NULL) goto failed;
6294 res = obj2ast_expr(tmp, &iter, arena);
6295 if (res != 0) goto failed;
6296 Py_XDECREF(tmp);
6297 tmp = NULL;
6298 } else {
6299 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6300 return 1;
6301 }
6302 if (PyObject_HasAttrString(obj, "ifs")) {
6303 int res;
6304 Py_ssize_t len;
6305 Py_ssize_t i;
6306 tmp = PyObject_GetAttrString(obj, "ifs");
6307 if (tmp == NULL) goto failed;
6308 if (!PyList_Check(tmp)) {
6309 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6310 goto failed;
6311 }
6312 len = PyList_GET_SIZE(tmp);
6313 ifs = asdl_seq_new(len, arena);
6314 if (ifs == NULL) goto failed;
6315 for (i = 0; i < len; i++) {
6316 expr_ty value;
6317 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6318 if (res != 0) goto failed;
6319 asdl_seq_SET(ifs, i, value);
6320 }
6321 Py_XDECREF(tmp);
6322 tmp = NULL;
6323 } else {
6324 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6325 return 1;
6326 }
6327 *out = comprehension(target, iter, ifs, arena);
6328 return 0;
6329failed:
6330 Py_XDECREF(tmp);
6331 return 1;
6332}
6333
6334int
6335obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6336{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006337 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006338
Benjamin Petersond8f65972010-11-20 04:31:07 +00006339 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006340 int lineno;
6341 int col_offset;
6342
Neal Norwitzad74aa82008-03-31 05:14:30 +00006343 if (obj == Py_None) {
6344 *out = NULL;
6345 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006346 }
6347 if (PyObject_HasAttrString(obj, "lineno")) {
6348 int res;
6349 tmp = PyObject_GetAttrString(obj, "lineno");
6350 if (tmp == NULL) goto failed;
6351 res = obj2ast_int(tmp, &lineno, arena);
6352 if (res != 0) goto failed;
6353 Py_XDECREF(tmp);
6354 tmp = NULL;
6355 } else {
6356 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6357 return 1;
6358 }
6359 if (PyObject_HasAttrString(obj, "col_offset")) {
6360 int res;
6361 tmp = PyObject_GetAttrString(obj, "col_offset");
6362 if (tmp == NULL) goto failed;
6363 res = obj2ast_int(tmp, &col_offset, arena);
6364 if (res != 0) goto failed;
6365 Py_XDECREF(tmp);
6366 tmp = NULL;
6367 } else {
6368 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6369 return 1;
6370 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006371 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6372 if (isinstance == -1) {
6373 return 1;
6374 }
6375 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006376 expr_ty type;
6377 identifier name;
6378 asdl_seq* body;
6379
6380 if (PyObject_HasAttrString(obj, "type")) {
6381 int res;
6382 tmp = PyObject_GetAttrString(obj, "type");
6383 if (tmp == NULL) goto failed;
6384 res = obj2ast_expr(tmp, &type, arena);
6385 if (res != 0) goto failed;
6386 Py_XDECREF(tmp);
6387 tmp = NULL;
6388 } else {
6389 type = NULL;
6390 }
6391 if (PyObject_HasAttrString(obj, "name")) {
6392 int res;
6393 tmp = PyObject_GetAttrString(obj, "name");
6394 if (tmp == NULL) goto failed;
6395 res = obj2ast_identifier(tmp, &name, arena);
6396 if (res != 0) goto failed;
6397 Py_XDECREF(tmp);
6398 tmp = NULL;
6399 } else {
6400 name = NULL;
6401 }
6402 if (PyObject_HasAttrString(obj, "body")) {
6403 int res;
6404 Py_ssize_t len;
6405 Py_ssize_t i;
6406 tmp = PyObject_GetAttrString(obj, "body");
6407 if (tmp == NULL) goto failed;
6408 if (!PyList_Check(tmp)) {
6409 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6410 goto failed;
6411 }
6412 len = PyList_GET_SIZE(tmp);
6413 body = asdl_seq_new(len, arena);
6414 if (body == NULL) goto failed;
6415 for (i = 0; i < len; i++) {
6416 stmt_ty value;
6417 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6418 if (res != 0) goto failed;
6419 asdl_seq_SET(body, i, value);
6420 }
6421 Py_XDECREF(tmp);
6422 tmp = NULL;
6423 } else {
6424 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6425 return 1;
6426 }
6427 *out = ExceptHandler(type, name, body, lineno, col_offset,
6428 arena);
6429 if (*out == NULL) goto failed;
6430 return 0;
6431 }
6432
Benjamin Peterson5b066812010-11-20 01:38:49 +00006433 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6434 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006435 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006436 return 1;
6437}
6438
6439int
6440obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6441{
6442 PyObject* tmp = NULL;
6443 asdl_seq* args;
6444 identifier vararg;
6445 expr_ty varargannotation;
6446 asdl_seq* kwonlyargs;
6447 identifier kwarg;
6448 expr_ty kwargannotation;
6449 asdl_seq* defaults;
6450 asdl_seq* kw_defaults;
6451
6452 if (PyObject_HasAttrString(obj, "args")) {
6453 int res;
6454 Py_ssize_t len;
6455 Py_ssize_t i;
6456 tmp = PyObject_GetAttrString(obj, "args");
6457 if (tmp == NULL) goto failed;
6458 if (!PyList_Check(tmp)) {
6459 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6460 goto failed;
6461 }
6462 len = PyList_GET_SIZE(tmp);
6463 args = asdl_seq_new(len, arena);
6464 if (args == NULL) goto failed;
6465 for (i = 0; i < len; i++) {
6466 arg_ty value;
6467 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6468 if (res != 0) goto failed;
6469 asdl_seq_SET(args, i, value);
6470 }
6471 Py_XDECREF(tmp);
6472 tmp = NULL;
6473 } else {
6474 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6475 return 1;
6476 }
6477 if (PyObject_HasAttrString(obj, "vararg")) {
6478 int res;
6479 tmp = PyObject_GetAttrString(obj, "vararg");
6480 if (tmp == NULL) goto failed;
6481 res = obj2ast_identifier(tmp, &vararg, arena);
6482 if (res != 0) goto failed;
6483 Py_XDECREF(tmp);
6484 tmp = NULL;
6485 } else {
6486 vararg = NULL;
6487 }
6488 if (PyObject_HasAttrString(obj, "varargannotation")) {
6489 int res;
6490 tmp = PyObject_GetAttrString(obj, "varargannotation");
6491 if (tmp == NULL) goto failed;
6492 res = obj2ast_expr(tmp, &varargannotation, arena);
6493 if (res != 0) goto failed;
6494 Py_XDECREF(tmp);
6495 tmp = NULL;
6496 } else {
6497 varargannotation = NULL;
6498 }
6499 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6500 int res;
6501 Py_ssize_t len;
6502 Py_ssize_t i;
6503 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6504 if (tmp == NULL) goto failed;
6505 if (!PyList_Check(tmp)) {
6506 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6507 goto failed;
6508 }
6509 len = PyList_GET_SIZE(tmp);
6510 kwonlyargs = asdl_seq_new(len, arena);
6511 if (kwonlyargs == NULL) goto failed;
6512 for (i = 0; i < len; i++) {
6513 arg_ty value;
6514 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6515 if (res != 0) goto failed;
6516 asdl_seq_SET(kwonlyargs, i, value);
6517 }
6518 Py_XDECREF(tmp);
6519 tmp = NULL;
6520 } else {
6521 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6522 return 1;
6523 }
6524 if (PyObject_HasAttrString(obj, "kwarg")) {
6525 int res;
6526 tmp = PyObject_GetAttrString(obj, "kwarg");
6527 if (tmp == NULL) goto failed;
6528 res = obj2ast_identifier(tmp, &kwarg, arena);
6529 if (res != 0) goto failed;
6530 Py_XDECREF(tmp);
6531 tmp = NULL;
6532 } else {
6533 kwarg = NULL;
6534 }
6535 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6536 int res;
6537 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6538 if (tmp == NULL) goto failed;
6539 res = obj2ast_expr(tmp, &kwargannotation, arena);
6540 if (res != 0) goto failed;
6541 Py_XDECREF(tmp);
6542 tmp = NULL;
6543 } else {
6544 kwargannotation = NULL;
6545 }
6546 if (PyObject_HasAttrString(obj, "defaults")) {
6547 int res;
6548 Py_ssize_t len;
6549 Py_ssize_t i;
6550 tmp = PyObject_GetAttrString(obj, "defaults");
6551 if (tmp == NULL) goto failed;
6552 if (!PyList_Check(tmp)) {
6553 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6554 goto failed;
6555 }
6556 len = PyList_GET_SIZE(tmp);
6557 defaults = asdl_seq_new(len, arena);
6558 if (defaults == NULL) goto failed;
6559 for (i = 0; i < len; i++) {
6560 expr_ty value;
6561 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6562 if (res != 0) goto failed;
6563 asdl_seq_SET(defaults, i, value);
6564 }
6565 Py_XDECREF(tmp);
6566 tmp = NULL;
6567 } else {
6568 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6569 return 1;
6570 }
6571 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6572 int res;
6573 Py_ssize_t len;
6574 Py_ssize_t i;
6575 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6576 if (tmp == NULL) goto failed;
6577 if (!PyList_Check(tmp)) {
6578 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6579 goto failed;
6580 }
6581 len = PyList_GET_SIZE(tmp);
6582 kw_defaults = asdl_seq_new(len, arena);
6583 if (kw_defaults == NULL) goto failed;
6584 for (i = 0; i < len; i++) {
6585 expr_ty value;
6586 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6587 if (res != 0) goto failed;
6588 asdl_seq_SET(kw_defaults, i, value);
6589 }
6590 Py_XDECREF(tmp);
6591 tmp = NULL;
6592 } else {
6593 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6594 return 1;
6595 }
6596 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6597 kwargannotation, defaults, kw_defaults, arena);
6598 return 0;
6599failed:
6600 Py_XDECREF(tmp);
6601 return 1;
6602}
6603
6604int
6605obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6606{
6607 PyObject* tmp = NULL;
6608 identifier arg;
6609 expr_ty annotation;
6610
6611 if (PyObject_HasAttrString(obj, "arg")) {
6612 int res;
6613 tmp = PyObject_GetAttrString(obj, "arg");
6614 if (tmp == NULL) goto failed;
6615 res = obj2ast_identifier(tmp, &arg, arena);
6616 if (res != 0) goto failed;
6617 Py_XDECREF(tmp);
6618 tmp = NULL;
6619 } else {
6620 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6621 return 1;
6622 }
6623 if (PyObject_HasAttrString(obj, "annotation")) {
6624 int res;
6625 tmp = PyObject_GetAttrString(obj, "annotation");
6626 if (tmp == NULL) goto failed;
6627 res = obj2ast_expr(tmp, &annotation, arena);
6628 if (res != 0) goto failed;
6629 Py_XDECREF(tmp);
6630 tmp = NULL;
6631 } else {
6632 annotation = NULL;
6633 }
6634 *out = arg(arg, annotation, arena);
6635 return 0;
6636failed:
6637 Py_XDECREF(tmp);
6638 return 1;
6639}
6640
6641int
6642obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6643{
6644 PyObject* tmp = NULL;
6645 identifier arg;
6646 expr_ty value;
6647
6648 if (PyObject_HasAttrString(obj, "arg")) {
6649 int res;
6650 tmp = PyObject_GetAttrString(obj, "arg");
6651 if (tmp == NULL) goto failed;
6652 res = obj2ast_identifier(tmp, &arg, arena);
6653 if (res != 0) goto failed;
6654 Py_XDECREF(tmp);
6655 tmp = NULL;
6656 } else {
6657 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6658 return 1;
6659 }
6660 if (PyObject_HasAttrString(obj, "value")) {
6661 int res;
6662 tmp = PyObject_GetAttrString(obj, "value");
6663 if (tmp == NULL) goto failed;
6664 res = obj2ast_expr(tmp, &value, arena);
6665 if (res != 0) goto failed;
6666 Py_XDECREF(tmp);
6667 tmp = NULL;
6668 } else {
6669 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6670 return 1;
6671 }
6672 *out = keyword(arg, value, arena);
6673 return 0;
6674failed:
6675 Py_XDECREF(tmp);
6676 return 1;
6677}
6678
6679int
6680obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6681{
6682 PyObject* tmp = NULL;
6683 identifier name;
6684 identifier asname;
6685
6686 if (PyObject_HasAttrString(obj, "name")) {
6687 int res;
6688 tmp = PyObject_GetAttrString(obj, "name");
6689 if (tmp == NULL) goto failed;
6690 res = obj2ast_identifier(tmp, &name, arena);
6691 if (res != 0) goto failed;
6692 Py_XDECREF(tmp);
6693 tmp = NULL;
6694 } else {
6695 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6696 return 1;
6697 }
6698 if (PyObject_HasAttrString(obj, "asname")) {
6699 int res;
6700 tmp = PyObject_GetAttrString(obj, "asname");
6701 if (tmp == NULL) goto failed;
6702 res = obj2ast_identifier(tmp, &asname, arena);
6703 if (res != 0) goto failed;
6704 Py_XDECREF(tmp);
6705 tmp = NULL;
6706 } else {
6707 asname = NULL;
6708 }
6709 *out = alias(name, asname, arena);
6710 return 0;
6711failed:
6712 Py_XDECREF(tmp);
6713 return 1;
6714}
6715
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006716int
6717obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6718{
6719 PyObject* tmp = NULL;
6720 expr_ty context_expr;
6721 expr_ty optional_vars;
6722
6723 if (PyObject_HasAttrString(obj, "context_expr")) {
6724 int res;
6725 tmp = PyObject_GetAttrString(obj, "context_expr");
6726 if (tmp == NULL) goto failed;
6727 res = obj2ast_expr(tmp, &context_expr, arena);
6728 if (res != 0) goto failed;
6729 Py_XDECREF(tmp);
6730 tmp = NULL;
6731 } else {
6732 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6733 return 1;
6734 }
6735 if (PyObject_HasAttrString(obj, "optional_vars")) {
6736 int res;
6737 tmp = PyObject_GetAttrString(obj, "optional_vars");
6738 if (tmp == NULL) goto failed;
6739 res = obj2ast_expr(tmp, &optional_vars, arena);
6740 if (res != 0) goto failed;
6741 Py_XDECREF(tmp);
6742 tmp = NULL;
6743 } else {
6744 optional_vars = NULL;
6745 }
6746 *out = withitem(context_expr, optional_vars, arena);
6747 return 0;
6748failed:
6749 Py_XDECREF(tmp);
6750 return 1;
6751}
6752
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006753
Martin v. Löwis1a214512008-06-11 05:26:20 +00006754static struct PyModuleDef _astmodule = {
6755 PyModuleDef_HEAD_INIT, "_ast"
6756};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006757PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006758PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006759{
6760 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006761 if (!init_types()) return NULL;
6762 m = PyModule_Create(&_astmodule);
6763 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006764 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006765 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6766 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006767 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006768 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006769 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6770 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_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, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006774 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006775 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006776 0) return NULL;
6777 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6778 NULL;
6779 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6780 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006781 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006782 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006783 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006784 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006785 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006786 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006787 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006788 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006789 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006790 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006791 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006792 0) return NULL;
6793 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6794 NULL;
6795 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6796 NULL;
6797 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6798 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6799 NULL;
6800 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6801 NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05006802 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
6803 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006804 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006805 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006806 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006807 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006808 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006809 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006810 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006811 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006812 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006813 return NULL;
6814 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6815 NULL;
6816 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6817 NULL;
6818 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6819 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006820 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006821 return NULL;
6822 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6823 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006824 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006825 return NULL;
6826 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6827 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006828 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_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, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006831 return NULL;
6832 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6833 NULL;
6834 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6835 NULL;
6836 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6837 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006838 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006839 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006840 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006841 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006842 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006843 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006844 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006845 (PyObject*)GeneratorExp_type) < 0) return NULL;
6846 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6847 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006848 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006849 return NULL;
6850 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6851 NULL;
6852 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6853 NULL;
6854 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6855 NULL;
6856 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6857 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006858 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006859 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006860 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006861 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006862 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006863 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006864 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006865 return NULL;
6866 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6867 NULL;
6868 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6871 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006872 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006873 (PyObject*)expr_context_type) < 0) return NULL;
6874 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6875 NULL;
6876 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6877 NULL;
6878 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6879 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006880 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006881 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006882 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006883 return NULL;
6884 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6885 NULL;
6886 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6887 NULL;
6888 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6889 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006890 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006891 return NULL;
6892 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6893 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006894 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006895 return NULL;
6896 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6897 NULL;
6898 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006899 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006900 return NULL;
6901 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6904 NULL;
6905 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6908 NULL;
6909 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6910 NULL;
6911 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6912 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006914 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006915 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006916 return NULL;
6917 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6918 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006919 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006920 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006921 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006922 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006923 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006924 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006925 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006926 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006927 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006928 return NULL;
6929 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6930 NULL;
6931 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6932 NULL;
6933 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6934 NULL;
6935 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6936 NULL;
6937 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6938 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6939 NULL;
6940 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6941 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6942 NULL;
6943 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6944 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6945 NULL;
6946 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6947 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6948 NULL;
6949 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6950 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6951 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006952 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006953 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006954 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006955 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006956 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006957 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006958 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006959 0) return NULL;
6960 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6961 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006962 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006963 return NULL;
6964 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6965 NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006966 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6967 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006968 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006969}
6970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006971
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006972PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006973{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006974 init_types();
6975 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006976}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006977
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006978/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6979mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006980{
6981 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006982 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6983 (PyObject*)Interactive_type};
6984 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006985 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006986 assert(0 <= mode && mode <= 2);
6987
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006988 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006989
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006990 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6991 if (isinstance == -1)
6992 return NULL;
6993 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006994 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6995 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006996 return NULL;
6997 }
6998 if (obj2ast_mod(ast, &res, arena) != 0)
6999 return NULL;
7000 else
7001 return res;
7002}
7003
7004int PyAST_Check(PyObject* obj)
7005{
7006 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00007007 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007008}
7009
Martin v. Löwis5b222132007-06-10 09:51:05 +00007010