blob: 96c6bf83fc7e5fc4555e293ef3651b01119d7acb [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
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000576
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000577static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000578{
Christian Heimes217cfd12007-12-02 14:31:20 +0000579 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000580}
581
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000582/* Conversion Python -> AST */
583
584static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
585{
586 if (obj == Py_None)
587 obj = NULL;
588 if (obj)
589 PyArena_AddPyObject(arena, obj);
590 Py_XINCREF(obj);
591 *out = obj;
592 return 0;
593}
594
595#define obj2ast_identifier obj2ast_object
596#define obj2ast_string obj2ast_object
597
598static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
599{
600 int i;
601 if (!PyLong_Check(obj)) {
602 PyObject *s = PyObject_Repr(obj);
603 if (s == NULL) return 1;
604 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000605 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000606 Py_DECREF(s);
607 return 1;
608 }
609
610 i = (int)PyLong_AsLong(obj);
611 if (i == -1 && PyErr_Occurred())
612 return 1;
613 *out = i;
614 return 0;
615}
616
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000617static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000618{
619 PyObject *empty_tuple, *d;
620 if (PyType_Ready(&AST_type) < 0)
621 return -1;
622 d = AST_type.tp_dict;
623 empty_tuple = PyTuple_New(0);
624 if (!empty_tuple ||
625 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
626 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
627 Py_XDECREF(empty_tuple);
628 return -1;
629 }
630 Py_DECREF(empty_tuple);
631 return 0;
632}
633
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000634
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000635static int init_types(void)
636{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000638 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000639 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000640 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000641 if (!mod_type) return 0;
642 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000643 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000644 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000645 Interactive_type = make_type("Interactive", mod_type,
646 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 Expression_type = make_type("Expression", mod_type, Expression_fields,
649 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000653 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000655 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000657 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000659 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000665 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000667 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000671 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000674 if (!If_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500675 With_type = make_type("With", stmt_type, With_fields, 2);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000676 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000677 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678 if (!Raise_type) return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500679 Try_type = make_type("Try", stmt_type, Try_fields, 4);
680 if (!Try_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000683 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000684 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000686 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000690 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
691 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000694 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000696 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000698 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000700 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000702 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000705 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000707 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000711 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000715 Set_type = make_type("Set", expr_type, Set_fields, 1);
716 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000719 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
720 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000721 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
722 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 GeneratorExp_type = make_type("GeneratorExp", expr_type,
724 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000736 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
737 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000738 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
739 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000740 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000744 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
745 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000752 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!expr_context_type) return 0;
754 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000755 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000759 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000761 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000763 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000767 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000769 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000775 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000779 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!slice_type) return 0;
781 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000784 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000788 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!boolop_type) return 0;
790 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000791 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000795 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000799 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!operator_type) return 0;
801 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000802 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000814 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000816 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000818 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000820 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000850 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!unaryop_type) return 0;
852 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000853 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000855 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000865 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000867 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000869 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!cmpop_type) return 0;
871 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000872 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000874 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000876 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000878 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000880 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000884 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000886 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000888 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000890 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000892 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000894 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000896 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000898 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000900 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000902 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000904 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000906 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000908 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000912 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000913 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000915 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000917 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
918 return 0;
919 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
920 ExceptHandler_fields, 3);
921 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000922 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000924 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000925 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000926 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000928 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!alias_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500930 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
931 if (!withitem_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000932 initialized = 1;
933 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000934}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000935
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000936static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
937static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
938static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
939static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
940 arena);
941static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
942static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
943static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
944static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
945static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
946static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
947 arena);
948static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
949 arena);
950static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
951static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
952static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
953static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500954static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000957Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958{
959 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000960 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000961 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 p->kind = Module_kind;
964 p->v.Module.body = body;
965 return p;
966}
967
968mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000969Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970{
971 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000972 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000973 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 p->kind = Interactive_kind;
976 p->v.Interactive.body = body;
977 return p;
978}
979
980mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982{
983 mod_ty p;
984 if (!body) {
985 PyErr_SetString(PyExc_ValueError,
986 "field body is required for Expression");
987 return NULL;
988 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000989 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000990 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 p->kind = Expression_kind;
993 p->v.Expression.body = body;
994 return p;
995}
996
997mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999{
1000 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001001 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001002 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 p->kind = Suite_kind;
1005 p->v.Suite.body = body;
1006 return p;
1007}
1008
1009stmt_ty
1010FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001011 decorator_list, expr_ty returns, int lineno, int col_offset,
1012 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
1014 stmt_ty p;
1015 if (!name) {
1016 PyErr_SetString(PyExc_ValueError,
1017 "field name is required for FunctionDef");
1018 return NULL;
1019 }
1020 if (!args) {
1021 PyErr_SetString(PyExc_ValueError,
1022 "field args is required for FunctionDef");
1023 return NULL;
1024 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001025 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001026 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 p->kind = FunctionDef_kind;
1029 p->v.FunctionDef.name = name;
1030 p->v.FunctionDef.args = args;
1031 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001032 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001033 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001035 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 return p;
1037}
1038
1039stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001040ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001041 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1042 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 stmt_ty p;
1045 if (!name) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field name is required for ClassDef");
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 = ClassDef_kind;
1054 p->v.ClassDef.name = name;
1055 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001056 p->v.ClassDef.keywords = keywords;
1057 p->v.ClassDef.starargs = starargs;
1058 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001060 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001062 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 return p;
1064}
1065
1066stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001067Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068{
1069 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001070 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001071 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 p->kind = Return_kind;
1074 p->v.Return.value = value;
1075 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001076 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 return p;
1078}
1079
1080stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001081Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082{
1083 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001084 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001085 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 p->kind = Delete_kind;
1088 p->v.Delete.targets = targets;
1089 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001090 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 return p;
1092}
1093
1094stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001095Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1096 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097{
1098 stmt_ty p;
1099 if (!value) {
1100 PyErr_SetString(PyExc_ValueError,
1101 "field value is required for Assign");
1102 return NULL;
1103 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001104 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001105 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 p->kind = Assign_kind;
1108 p->v.Assign.targets = targets;
1109 p->v.Assign.value = value;
1110 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001111 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 return p;
1113}
1114
1115stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001116AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1117 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118{
1119 stmt_ty p;
1120 if (!target) {
1121 PyErr_SetString(PyExc_ValueError,
1122 "field target is required for AugAssign");
1123 return NULL;
1124 }
1125 if (!op) {
1126 PyErr_SetString(PyExc_ValueError,
1127 "field op is required for AugAssign");
1128 return NULL;
1129 }
1130 if (!value) {
1131 PyErr_SetString(PyExc_ValueError,
1132 "field value is required for AugAssign");
1133 return NULL;
1134 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001135 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001136 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 p->kind = AugAssign_kind;
1139 p->v.AugAssign.target = target;
1140 p->v.AugAssign.op = op;
1141 p->v.AugAssign.value = value;
1142 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001143 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return p;
1145}
1146
1147stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001149 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150{
1151 stmt_ty p;
1152 if (!target) {
1153 PyErr_SetString(PyExc_ValueError,
1154 "field target is required for For");
1155 return NULL;
1156 }
1157 if (!iter) {
1158 PyErr_SetString(PyExc_ValueError,
1159 "field iter is required for For");
1160 return NULL;
1161 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001163 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 p->kind = For_kind;
1166 p->v.For.target = target;
1167 p->v.For.iter = iter;
1168 p->v.For.body = body;
1169 p->v.For.orelse = orelse;
1170 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001171 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 return p;
1173}
1174
1175stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001176While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1177 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178{
1179 stmt_ty p;
1180 if (!test) {
1181 PyErr_SetString(PyExc_ValueError,
1182 "field test is required for While");
1183 return NULL;
1184 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001185 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001186 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 p->kind = While_kind;
1189 p->v.While.test = test;
1190 p->v.While.body = body;
1191 p->v.While.orelse = orelse;
1192 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001193 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 return p;
1195}
1196
1197stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001198If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1199 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200{
1201 stmt_ty p;
1202 if (!test) {
1203 PyErr_SetString(PyExc_ValueError,
1204 "field test is required for If");
1205 return NULL;
1206 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001208 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 p->kind = If_kind;
1211 p->v.If.test = test;
1212 p->v.If.body = body;
1213 p->v.If.orelse = orelse;
1214 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001215 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return p;
1217}
1218
1219stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001220With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1221 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001222{
1223 stmt_ty p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001224 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001225 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001226 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001227 p->kind = With_kind;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001228 p->v.With.items = items;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001229 p->v.With.body = body;
1230 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001231 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001232 return p;
1233}
1234
1235stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001236Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237{
1238 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001240 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001243 p->v.Raise.exc = exc;
1244 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 return p;
1248}
1249
1250stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001251Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1252 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253{
1254 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001255 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001256 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001258 p->kind = Try_kind;
1259 p->v.Try.body = body;
1260 p->v.Try.handlers = handlers;
1261 p->v.Try.orelse = orelse;
1262 p->v.Try.finalbody = finalbody;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001264 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 return p;
1266}
1267
1268stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001269Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270{
1271 stmt_ty p;
1272 if (!test) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "field test is required for Assert");
1275 return NULL;
1276 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001278 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 p->kind = Assert_kind;
1281 p->v.Assert.test = test;
1282 p->v.Assert.msg = msg;
1283 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 return p;
1286}
1287
1288stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001289Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290{
1291 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001292 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001293 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 p->kind = Import_kind;
1296 p->v.Import.names = names;
1297 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001298 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 return p;
1300}
1301
1302stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001303ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1304 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305{
1306 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001307 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001308 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 p->kind = ImportFrom_kind;
1311 p->v.ImportFrom.module = module;
1312 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001313 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001315 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return p;
1317}
1318
1319stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001320Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321{
1322 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001324 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 p->kind = Global_kind;
1327 p->v.Global.names = names;
1328 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 return p;
1331}
1332
1333stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001334Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1335{
1336 stmt_ty p;
1337 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1338 if (!p)
1339 return NULL;
1340 p->kind = Nonlocal_kind;
1341 p->v.Nonlocal.names = names;
1342 p->lineno = lineno;
1343 p->col_offset = col_offset;
1344 return p;
1345}
1346
1347stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001348Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349{
1350 stmt_ty p;
1351 if (!value) {
1352 PyErr_SetString(PyExc_ValueError,
1353 "field value is required for Expr");
1354 return NULL;
1355 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001356 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001357 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 p->kind = Expr_kind;
1360 p->v.Expr.value = value;
1361 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001362 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 return p;
1364}
1365
1366stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001367Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368{
1369 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001370 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001371 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 p->kind = Pass_kind;
1374 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001375 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 return p;
1377}
1378
1379stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001380Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381{
1382 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001383 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001384 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 p->kind = Break_kind;
1387 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001388 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 return p;
1390}
1391
1392stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001393Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394{
1395 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001396 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001397 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 p->kind = Continue_kind;
1400 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001401 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return p;
1403}
1404
1405expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1407 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408{
1409 expr_ty p;
1410 if (!op) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field op is required for BoolOp");
1413 return NULL;
1414 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001415 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001416 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 p->kind = BoolOp_kind;
1419 p->v.BoolOp.op = op;
1420 p->v.BoolOp.values = values;
1421 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001422 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return p;
1424}
1425
1426expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1428 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429{
1430 expr_ty p;
1431 if (!left) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "field left is required for BinOp");
1434 return NULL;
1435 }
1436 if (!op) {
1437 PyErr_SetString(PyExc_ValueError,
1438 "field op is required for BinOp");
1439 return NULL;
1440 }
1441 if (!right) {
1442 PyErr_SetString(PyExc_ValueError,
1443 "field right is required for BinOp");
1444 return NULL;
1445 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001446 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001447 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 p->kind = BinOp_kind;
1450 p->v.BinOp.left = left;
1451 p->v.BinOp.op = op;
1452 p->v.BinOp.right = right;
1453 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001454 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 return p;
1456}
1457
1458expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001459UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1460 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461{
1462 expr_ty p;
1463 if (!op) {
1464 PyErr_SetString(PyExc_ValueError,
1465 "field op is required for UnaryOp");
1466 return NULL;
1467 }
1468 if (!operand) {
1469 PyErr_SetString(PyExc_ValueError,
1470 "field operand is required for UnaryOp");
1471 return NULL;
1472 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001473 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001474 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 p->kind = UnaryOp_kind;
1477 p->v.UnaryOp.op = op;
1478 p->v.UnaryOp.operand = operand;
1479 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001480 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 return p;
1482}
1483
1484expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1486 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487{
1488 expr_ty p;
1489 if (!args) {
1490 PyErr_SetString(PyExc_ValueError,
1491 "field args is required for Lambda");
1492 return NULL;
1493 }
1494 if (!body) {
1495 PyErr_SetString(PyExc_ValueError,
1496 "field body is required for Lambda");
1497 return NULL;
1498 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001499 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001500 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 p->kind = Lambda_kind;
1503 p->v.Lambda.args = args;
1504 p->v.Lambda.body = body;
1505 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 return p;
1508}
1509
1510expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1512 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001513{
1514 expr_ty p;
1515 if (!test) {
1516 PyErr_SetString(PyExc_ValueError,
1517 "field test is required for IfExp");
1518 return NULL;
1519 }
1520 if (!body) {
1521 PyErr_SetString(PyExc_ValueError,
1522 "field body is required for IfExp");
1523 return NULL;
1524 }
1525 if (!orelse) {
1526 PyErr_SetString(PyExc_ValueError,
1527 "field orelse is required for IfExp");
1528 return NULL;
1529 }
1530 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001531 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001532 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001533 p->kind = IfExp_kind;
1534 p->v.IfExp.test = test;
1535 p->v.IfExp.body = body;
1536 p->v.IfExp.orelse = orelse;
1537 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001538 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001539 return p;
1540}
1541
1542expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001543Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1544 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545{
1546 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001547 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001548 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 p->kind = Dict_kind;
1551 p->v.Dict.keys = keys;
1552 p->v.Dict.values = values;
1553 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001554 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555 return p;
1556}
1557
1558expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001559Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1560{
1561 expr_ty p;
1562 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001563 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001564 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001565 p->kind = Set_kind;
1566 p->v.Set.elts = elts;
1567 p->lineno = lineno;
1568 p->col_offset = col_offset;
1569 return p;
1570}
1571
1572expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001573ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1574 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575{
1576 expr_ty p;
1577 if (!elt) {
1578 PyErr_SetString(PyExc_ValueError,
1579 "field elt is required for ListComp");
1580 return NULL;
1581 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001582 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001583 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 p->kind = ListComp_kind;
1586 p->v.ListComp.elt = elt;
1587 p->v.ListComp.generators = generators;
1588 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 return p;
1591}
1592
1593expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001594SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1595 *arena)
1596{
1597 expr_ty p;
1598 if (!elt) {
1599 PyErr_SetString(PyExc_ValueError,
1600 "field elt is required for SetComp");
1601 return NULL;
1602 }
1603 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1604 if (!p)
1605 return NULL;
1606 p->kind = SetComp_kind;
1607 p->v.SetComp.elt = elt;
1608 p->v.SetComp.generators = generators;
1609 p->lineno = lineno;
1610 p->col_offset = col_offset;
1611 return p;
1612}
1613
1614expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001615DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1616 col_offset, PyArena *arena)
1617{
1618 expr_ty p;
1619 if (!key) {
1620 PyErr_SetString(PyExc_ValueError,
1621 "field key is required for DictComp");
1622 return NULL;
1623 }
1624 if (!value) {
1625 PyErr_SetString(PyExc_ValueError,
1626 "field value is required for DictComp");
1627 return NULL;
1628 }
1629 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1630 if (!p)
1631 return NULL;
1632 p->kind = DictComp_kind;
1633 p->v.DictComp.key = key;
1634 p->v.DictComp.value = value;
1635 p->v.DictComp.generators = generators;
1636 p->lineno = lineno;
1637 p->col_offset = col_offset;
1638 return p;
1639}
1640
1641expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001642GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1643 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644{
1645 expr_ty p;
1646 if (!elt) {
1647 PyErr_SetString(PyExc_ValueError,
1648 "field elt is required for GeneratorExp");
1649 return NULL;
1650 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001651 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001652 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 p->kind = GeneratorExp_kind;
1655 p->v.GeneratorExp.elt = elt;
1656 p->v.GeneratorExp.generators = generators;
1657 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001658 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 return p;
1660}
1661
1662expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001663Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664{
1665 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001666 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001667 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 p->kind = Yield_kind;
1670 p->v.Yield.value = value;
1671 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001672 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 return p;
1674}
1675
1676expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001677Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1678 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679{
1680 expr_ty p;
1681 if (!left) {
1682 PyErr_SetString(PyExc_ValueError,
1683 "field left is required for Compare");
1684 return NULL;
1685 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001686 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001687 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 p->kind = Compare_kind;
1690 p->v.Compare.left = left;
1691 p->v.Compare.ops = ops;
1692 p->v.Compare.comparators = comparators;
1693 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 return p;
1696}
1697
1698expr_ty
1699Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001700 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701{
1702 expr_ty p;
1703 if (!func) {
1704 PyErr_SetString(PyExc_ValueError,
1705 "field func is required for Call");
1706 return NULL;
1707 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001708 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001709 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 p->kind = Call_kind;
1712 p->v.Call.func = func;
1713 p->v.Call.args = args;
1714 p->v.Call.keywords = keywords;
1715 p->v.Call.starargs = starargs;
1716 p->v.Call.kwargs = kwargs;
1717 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001718 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 return p;
1720}
1721
1722expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001723Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724{
1725 expr_ty p;
1726 if (!n) {
1727 PyErr_SetString(PyExc_ValueError,
1728 "field n is required for Num");
1729 return NULL;
1730 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001731 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001732 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 p->kind = Num_kind;
1735 p->v.Num.n = n;
1736 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001737 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 return p;
1739}
1740
1741expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001742Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743{
1744 expr_ty p;
1745 if (!s) {
1746 PyErr_SetString(PyExc_ValueError,
1747 "field s is required for Str");
1748 return NULL;
1749 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001750 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001751 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 p->kind = Str_kind;
1754 p->v.Str.s = s;
1755 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001756 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 return p;
1758}
1759
1760expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001761Bytes(string s, int lineno, int col_offset, PyArena *arena)
1762{
1763 expr_ty p;
1764 if (!s) {
1765 PyErr_SetString(PyExc_ValueError,
1766 "field s is required for Bytes");
1767 return NULL;
1768 }
1769 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001770 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001771 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001772 p->kind = Bytes_kind;
1773 p->v.Bytes.s = s;
1774 p->lineno = lineno;
1775 p->col_offset = col_offset;
1776 return p;
1777}
1778
1779expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001780Ellipsis(int lineno, int col_offset, PyArena *arena)
1781{
1782 expr_ty p;
1783 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001784 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001785 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001786 p->kind = Ellipsis_kind;
1787 p->lineno = lineno;
1788 p->col_offset = col_offset;
1789 return p;
1790}
1791
1792expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001793Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1794 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795{
1796 expr_ty p;
1797 if (!value) {
1798 PyErr_SetString(PyExc_ValueError,
1799 "field value is required for Attribute");
1800 return NULL;
1801 }
1802 if (!attr) {
1803 PyErr_SetString(PyExc_ValueError,
1804 "field attr is required for Attribute");
1805 return NULL;
1806 }
1807 if (!ctx) {
1808 PyErr_SetString(PyExc_ValueError,
1809 "field ctx is required for Attribute");
1810 return NULL;
1811 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001813 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 p->kind = Attribute_kind;
1816 p->v.Attribute.value = value;
1817 p->v.Attribute.attr = attr;
1818 p->v.Attribute.ctx = ctx;
1819 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001820 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 return p;
1822}
1823
1824expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001825Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1826 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827{
1828 expr_ty p;
1829 if (!value) {
1830 PyErr_SetString(PyExc_ValueError,
1831 "field value is required for Subscript");
1832 return NULL;
1833 }
1834 if (!slice) {
1835 PyErr_SetString(PyExc_ValueError,
1836 "field slice is required for Subscript");
1837 return NULL;
1838 }
1839 if (!ctx) {
1840 PyErr_SetString(PyExc_ValueError,
1841 "field ctx is required for Subscript");
1842 return NULL;
1843 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001845 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 p->kind = Subscript_kind;
1848 p->v.Subscript.value = value;
1849 p->v.Subscript.slice = slice;
1850 p->v.Subscript.ctx = ctx;
1851 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001852 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 return p;
1854}
1855
1856expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001857Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1858 *arena)
1859{
1860 expr_ty p;
1861 if (!value) {
1862 PyErr_SetString(PyExc_ValueError,
1863 "field value is required for Starred");
1864 return NULL;
1865 }
1866 if (!ctx) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "field ctx is required for Starred");
1869 return NULL;
1870 }
1871 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1872 if (!p)
1873 return NULL;
1874 p->kind = Starred_kind;
1875 p->v.Starred.value = value;
1876 p->v.Starred.ctx = ctx;
1877 p->lineno = lineno;
1878 p->col_offset = col_offset;
1879 return p;
1880}
1881
1882expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001883Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1884 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885{
1886 expr_ty p;
1887 if (!id) {
1888 PyErr_SetString(PyExc_ValueError,
1889 "field id is required for Name");
1890 return NULL;
1891 }
1892 if (!ctx) {
1893 PyErr_SetString(PyExc_ValueError,
1894 "field ctx is required for Name");
1895 return NULL;
1896 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001898 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 p->kind = Name_kind;
1901 p->v.Name.id = id;
1902 p->v.Name.ctx = ctx;
1903 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001904 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 return p;
1906}
1907
1908expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001909List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1910 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911{
1912 expr_ty p;
1913 if (!ctx) {
1914 PyErr_SetString(PyExc_ValueError,
1915 "field ctx is required for List");
1916 return NULL;
1917 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001919 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 p->kind = List_kind;
1922 p->v.List.elts = elts;
1923 p->v.List.ctx = ctx;
1924 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001925 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 return p;
1927}
1928
1929expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001930Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1931 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932{
1933 expr_ty p;
1934 if (!ctx) {
1935 PyErr_SetString(PyExc_ValueError,
1936 "field ctx is required for Tuple");
1937 return NULL;
1938 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001940 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 p->kind = Tuple_kind;
1943 p->v.Tuple.elts = elts;
1944 p->v.Tuple.ctx = ctx;
1945 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001946 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 return p;
1948}
1949
1950slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001951Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952{
1953 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001955 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 p->kind = Slice_kind;
1958 p->v.Slice.lower = lower;
1959 p->v.Slice.upper = upper;
1960 p->v.Slice.step = step;
1961 return p;
1962}
1963
1964slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001965ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966{
1967 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001968 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001969 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 p->kind = ExtSlice_kind;
1972 p->v.ExtSlice.dims = dims;
1973 return p;
1974}
1975
1976slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001977Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978{
1979 slice_ty p;
1980 if (!value) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field value is required for Index");
1983 return NULL;
1984 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 p->kind = Index_kind;
1989 p->v.Index.value = value;
1990 return p;
1991}
1992
1993comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001994comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995{
1996 comprehension_ty p;
1997 if (!target) {
1998 PyErr_SetString(PyExc_ValueError,
1999 "field target is required for comprehension");
2000 return NULL;
2001 }
2002 if (!iter) {
2003 PyErr_SetString(PyExc_ValueError,
2004 "field iter is required for comprehension");
2005 return NULL;
2006 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002007 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002008 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 p->target = target;
2011 p->iter = iter;
2012 p->ifs = ifs;
2013 return p;
2014}
2015
2016excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002017ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002018 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019{
2020 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002022 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002024 p->kind = ExceptHandler_kind;
2025 p->v.ExceptHandler.type = type;
2026 p->v.ExceptHandler.name = name;
2027 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002028 p->lineno = lineno;
2029 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 return p;
2031}
2032
2033arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002034arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2035 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2036 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037{
2038 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002040 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 p->args = args;
2043 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002044 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002045 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002047 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002049 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 return p;
2051}
2052
Neal Norwitzc1505362006-12-28 06:47:50 +00002053arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002054arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002055{
2056 arg_ty p;
2057 if (!arg) {
2058 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002059 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002060 return NULL;
2061 }
2062 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002063 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002064 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002065 p->arg = arg;
2066 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002067 return p;
2068}
2069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002071keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072{
2073 keyword_ty p;
2074 if (!arg) {
2075 PyErr_SetString(PyExc_ValueError,
2076 "field arg is required for keyword");
2077 return NULL;
2078 }
2079 if (!value) {
2080 PyErr_SetString(PyExc_ValueError,
2081 "field value is required for keyword");
2082 return NULL;
2083 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002084 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002085 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 p->arg = arg;
2088 p->value = value;
2089 return p;
2090}
2091
2092alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094{
2095 alias_ty p;
2096 if (!name) {
2097 PyErr_SetString(PyExc_ValueError,
2098 "field name is required for alias");
2099 return NULL;
2100 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002101 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002102 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 p->name = name;
2105 p->asname = asname;
2106 return p;
2107}
2108
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002109withitem_ty
2110withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2111{
2112 withitem_ty p;
2113 if (!context_expr) {
2114 PyErr_SetString(PyExc_ValueError,
2115 "field context_expr is required for withitem");
2116 return NULL;
2117 }
2118 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2119 if (!p)
2120 return NULL;
2121 p->context_expr = context_expr;
2122 p->optional_vars = optional_vars;
2123 return p;
2124}
2125
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002126
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002127PyObject*
2128ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002129{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002130 mod_ty o = (mod_ty)_o;
2131 PyObject *result = NULL, *value = NULL;
2132 if (!o) {
2133 Py_INCREF(Py_None);
2134 return Py_None;
2135 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 switch (o->kind) {
2138 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002139 result = PyType_GenericNew(Module_type, NULL, NULL);
2140 if (!result) goto failed;
2141 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2142 if (!value) goto failed;
2143 if (PyObject_SetAttrString(result, "body", value) == -1)
2144 goto failed;
2145 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 break;
2147 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002148 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2149 if (!result) goto failed;
2150 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2151 if (!value) goto failed;
2152 if (PyObject_SetAttrString(result, "body", value) == -1)
2153 goto failed;
2154 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 break;
2156 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002157 result = PyType_GenericNew(Expression_type, NULL, NULL);
2158 if (!result) goto failed;
2159 value = ast2obj_expr(o->v.Expression.body);
2160 if (!value) goto failed;
2161 if (PyObject_SetAttrString(result, "body", value) == -1)
2162 goto failed;
2163 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 break;
2165 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002166 result = PyType_GenericNew(Suite_type, NULL, NULL);
2167 if (!result) goto failed;
2168 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2169 if (!value) goto failed;
2170 if (PyObject_SetAttrString(result, "body", value) == -1)
2171 goto failed;
2172 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 break;
2174 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002175 return result;
2176failed:
2177 Py_XDECREF(value);
2178 Py_XDECREF(result);
2179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180}
2181
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002182PyObject*
2183ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002185 stmt_ty o = (stmt_ty)_o;
2186 PyObject *result = NULL, *value = NULL;
2187 if (!o) {
2188 Py_INCREF(Py_None);
2189 return Py_None;
2190 }
2191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 switch (o->kind) {
2193 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002194 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2195 if (!result) goto failed;
2196 value = ast2obj_identifier(o->v.FunctionDef.name);
2197 if (!value) goto failed;
2198 if (PyObject_SetAttrString(result, "name", value) == -1)
2199 goto failed;
2200 Py_DECREF(value);
2201 value = ast2obj_arguments(o->v.FunctionDef.args);
2202 if (!value) goto failed;
2203 if (PyObject_SetAttrString(result, "args", value) == -1)
2204 goto failed;
2205 Py_DECREF(value);
2206 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2207 if (!value) goto failed;
2208 if (PyObject_SetAttrString(result, "body", value) == -1)
2209 goto failed;
2210 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002211 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2212 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002213 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002214 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2215 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002216 goto failed;
2217 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002218 value = ast2obj_expr(o->v.FunctionDef.returns);
2219 if (!value) goto failed;
2220 if (PyObject_SetAttrString(result, "returns", value) == -1)
2221 goto failed;
2222 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 break;
2224 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002225 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2226 if (!result) goto failed;
2227 value = ast2obj_identifier(o->v.ClassDef.name);
2228 if (!value) goto failed;
2229 if (PyObject_SetAttrString(result, "name", value) == -1)
2230 goto failed;
2231 Py_DECREF(value);
2232 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2233 if (!value) goto failed;
2234 if (PyObject_SetAttrString(result, "bases", value) == -1)
2235 goto failed;
2236 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002237 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2238 if (!value) goto failed;
2239 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2240 goto failed;
2241 Py_DECREF(value);
2242 value = ast2obj_expr(o->v.ClassDef.starargs);
2243 if (!value) goto failed;
2244 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2245 goto failed;
2246 Py_DECREF(value);
2247 value = ast2obj_expr(o->v.ClassDef.kwargs);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002252 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "body", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002257 value = ast2obj_list(o->v.ClassDef.decorator_list,
2258 ast2obj_expr);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2261 -1)
2262 goto failed;
2263 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 break;
2265 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002266 result = PyType_GenericNew(Return_type, NULL, NULL);
2267 if (!result) goto failed;
2268 value = ast2obj_expr(o->v.Return.value);
2269 if (!value) goto failed;
2270 if (PyObject_SetAttrString(result, "value", value) == -1)
2271 goto failed;
2272 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 break;
2274 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002275 result = PyType_GenericNew(Delete_type, NULL, NULL);
2276 if (!result) goto failed;
2277 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "targets", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 break;
2283 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 result = PyType_GenericNew(Assign_type, NULL, NULL);
2285 if (!result) goto failed;
2286 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2287 if (!value) goto failed;
2288 if (PyObject_SetAttrString(result, "targets", value) == -1)
2289 goto failed;
2290 Py_DECREF(value);
2291 value = ast2obj_expr(o->v.Assign.value);
2292 if (!value) goto failed;
2293 if (PyObject_SetAttrString(result, "value", value) == -1)
2294 goto failed;
2295 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 break;
2297 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002298 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2299 if (!result) goto failed;
2300 value = ast2obj_expr(o->v.AugAssign.target);
2301 if (!value) goto failed;
2302 if (PyObject_SetAttrString(result, "target", value) == -1)
2303 goto failed;
2304 Py_DECREF(value);
2305 value = ast2obj_operator(o->v.AugAssign.op);
2306 if (!value) goto failed;
2307 if (PyObject_SetAttrString(result, "op", value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 value = ast2obj_expr(o->v.AugAssign.value);
2311 if (!value) goto failed;
2312 if (PyObject_SetAttrString(result, "value", value) == -1)
2313 goto failed;
2314 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002317 result = PyType_GenericNew(For_type, NULL, NULL);
2318 if (!result) goto failed;
2319 value = ast2obj_expr(o->v.For.target);
2320 if (!value) goto failed;
2321 if (PyObject_SetAttrString(result, "target", value) == -1)
2322 goto failed;
2323 Py_DECREF(value);
2324 value = ast2obj_expr(o->v.For.iter);
2325 if (!value) goto failed;
2326 if (PyObject_SetAttrString(result, "iter", value) == -1)
2327 goto failed;
2328 Py_DECREF(value);
2329 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2330 if (!value) goto failed;
2331 if (PyObject_SetAttrString(result, "body", value) == -1)
2332 goto failed;
2333 Py_DECREF(value);
2334 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2335 if (!value) goto failed;
2336 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2337 goto failed;
2338 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 break;
2340 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002341 result = PyType_GenericNew(While_type, NULL, NULL);
2342 if (!result) goto failed;
2343 value = ast2obj_expr(o->v.While.test);
2344 if (!value) goto failed;
2345 if (PyObject_SetAttrString(result, "test", value) == -1)
2346 goto failed;
2347 Py_DECREF(value);
2348 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2349 if (!value) goto failed;
2350 if (PyObject_SetAttrString(result, "body", value) == -1)
2351 goto failed;
2352 Py_DECREF(value);
2353 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2354 if (!value) goto failed;
2355 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2356 goto failed;
2357 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358 break;
2359 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002360 result = PyType_GenericNew(If_type, NULL, NULL);
2361 if (!result) goto failed;
2362 value = ast2obj_expr(o->v.If.test);
2363 if (!value) goto failed;
2364 if (PyObject_SetAttrString(result, "test", value) == -1)
2365 goto failed;
2366 Py_DECREF(value);
2367 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2368 if (!value) goto failed;
2369 if (PyObject_SetAttrString(result, "body", value) == -1)
2370 goto failed;
2371 Py_DECREF(value);
2372 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002378 case With_kind:
2379 result = PyType_GenericNew(With_type, NULL, NULL);
2380 if (!result) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002381 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002382 if (!value) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002383 if (PyObject_SetAttrString(result, "items", value) == -1)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002384 goto failed;
2385 Py_DECREF(value);
2386 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "body", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
2391 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002393 result = PyType_GenericNew(Raise_type, NULL, NULL);
2394 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002395 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002396 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002397 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002398 goto failed;
2399 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002400 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002401 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002402 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002403 goto failed;
2404 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002406 case Try_kind:
2407 result = PyType_GenericNew(Try_type, NULL, NULL);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002408 if (!result) goto failed;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002409 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002410 if (!value) goto failed;
2411 if (PyObject_SetAttrString(result, "body", value) == -1)
2412 goto failed;
2413 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002414 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002415 if (!value) goto failed;
2416 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2417 goto failed;
2418 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002419 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002424 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002425 if (!value) goto failed;
2426 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 break;
2430 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002431 result = PyType_GenericNew(Assert_type, NULL, NULL);
2432 if (!result) goto failed;
2433 value = ast2obj_expr(o->v.Assert.test);
2434 if (!value) goto failed;
2435 if (PyObject_SetAttrString(result, "test", value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
2438 value = ast2obj_expr(o->v.Assert.msg);
2439 if (!value) goto failed;
2440 if (PyObject_SetAttrString(result, "msg", value) == -1)
2441 goto failed;
2442 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443 break;
2444 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002445 result = PyType_GenericNew(Import_type, NULL, NULL);
2446 if (!result) goto failed;
2447 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2448 if (!value) goto failed;
2449 if (PyObject_SetAttrString(result, "names", value) == -1)
2450 goto failed;
2451 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 break;
2453 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002454 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2455 if (!result) goto failed;
2456 value = ast2obj_identifier(o->v.ImportFrom.module);
2457 if (!value) goto failed;
2458 if (PyObject_SetAttrString(result, "module", value) == -1)
2459 goto failed;
2460 Py_DECREF(value);
2461 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2462 if (!value) goto failed;
2463 if (PyObject_SetAttrString(result, "names", value) == -1)
2464 goto failed;
2465 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002466 value = ast2obj_int(o->v.ImportFrom.level);
2467 if (!value) goto failed;
2468 if (PyObject_SetAttrString(result, "level", value) == -1)
2469 goto failed;
2470 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002473 result = PyType_GenericNew(Global_type, NULL, NULL);
2474 if (!result) goto failed;
2475 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2476 if (!value) goto failed;
2477 if (PyObject_SetAttrString(result, "names", value) == -1)
2478 goto failed;
2479 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002481 case Nonlocal_kind:
2482 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2483 if (!result) goto failed;
2484 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2485 if (!value) goto failed;
2486 if (PyObject_SetAttrString(result, "names", value) == -1)
2487 goto failed;
2488 Py_DECREF(value);
2489 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002491 result = PyType_GenericNew(Expr_type, NULL, NULL);
2492 if (!result) goto failed;
2493 value = ast2obj_expr(o->v.Expr.value);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "value", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 break;
2499 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002500 result = PyType_GenericNew(Pass_type, NULL, NULL);
2501 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 break;
2503 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002504 result = PyType_GenericNew(Break_type, NULL, NULL);
2505 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 break;
2507 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002508 result = PyType_GenericNew(Continue_type, NULL, NULL);
2509 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 break;
2511 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002512 value = ast2obj_int(o->lineno);
2513 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002514 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2515 goto failed;
2516 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002517 value = ast2obj_int(o->col_offset);
2518 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002519 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2520 goto failed;
2521 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002522 return result;
2523failed:
2524 Py_XDECREF(value);
2525 Py_XDECREF(result);
2526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527}
2528
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002529PyObject*
2530ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002532 expr_ty o = (expr_ty)_o;
2533 PyObject *result = NULL, *value = NULL;
2534 if (!o) {
2535 Py_INCREF(Py_None);
2536 return Py_None;
2537 }
2538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 switch (o->kind) {
2540 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002541 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2542 if (!result) goto failed;
2543 value = ast2obj_boolop(o->v.BoolOp.op);
2544 if (!value) goto failed;
2545 if (PyObject_SetAttrString(result, "op", value) == -1)
2546 goto failed;
2547 Py_DECREF(value);
2548 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2549 if (!value) goto failed;
2550 if (PyObject_SetAttrString(result, "values", value) == -1)
2551 goto failed;
2552 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 break;
2554 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002555 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2556 if (!result) goto failed;
2557 value = ast2obj_expr(o->v.BinOp.left);
2558 if (!value) goto failed;
2559 if (PyObject_SetAttrString(result, "left", value) == -1)
2560 goto failed;
2561 Py_DECREF(value);
2562 value = ast2obj_operator(o->v.BinOp.op);
2563 if (!value) goto failed;
2564 if (PyObject_SetAttrString(result, "op", value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 value = ast2obj_expr(o->v.BinOp.right);
2568 if (!value) goto failed;
2569 if (PyObject_SetAttrString(result, "right", value) == -1)
2570 goto failed;
2571 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 break;
2573 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002574 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2575 if (!result) goto failed;
2576 value = ast2obj_unaryop(o->v.UnaryOp.op);
2577 if (!value) goto failed;
2578 if (PyObject_SetAttrString(result, "op", value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 value = ast2obj_expr(o->v.UnaryOp.operand);
2582 if (!value) goto failed;
2583 if (PyObject_SetAttrString(result, "operand", value) == -1)
2584 goto failed;
2585 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 break;
2587 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002588 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2589 if (!result) goto failed;
2590 value = ast2obj_arguments(o->v.Lambda.args);
2591 if (!value) goto failed;
2592 if (PyObject_SetAttrString(result, "args", value) == -1)
2593 goto failed;
2594 Py_DECREF(value);
2595 value = ast2obj_expr(o->v.Lambda.body);
2596 if (!value) goto failed;
2597 if (PyObject_SetAttrString(result, "body", value) == -1)
2598 goto failed;
2599 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002601 case IfExp_kind:
2602 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2603 if (!result) goto failed;
2604 value = ast2obj_expr(o->v.IfExp.test);
2605 if (!value) goto failed;
2606 if (PyObject_SetAttrString(result, "test", value) == -1)
2607 goto failed;
2608 Py_DECREF(value);
2609 value = ast2obj_expr(o->v.IfExp.body);
2610 if (!value) goto failed;
2611 if (PyObject_SetAttrString(result, "body", value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 value = ast2obj_expr(o->v.IfExp.orelse);
2615 if (!value) goto failed;
2616 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2617 goto failed;
2618 Py_DECREF(value);
2619 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002621 result = PyType_GenericNew(Dict_type, NULL, NULL);
2622 if (!result) goto failed;
2623 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2624 if (!value) goto failed;
2625 if (PyObject_SetAttrString(result, "keys", value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2629 if (!value) goto failed;
2630 if (PyObject_SetAttrString(result, "values", value) == -1)
2631 goto failed;
2632 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002634 case Set_kind:
2635 result = PyType_GenericNew(Set_type, NULL, NULL);
2636 if (!result) goto failed;
2637 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2638 if (!value) goto failed;
2639 if (PyObject_SetAttrString(result, "elts", value) == -1)
2640 goto failed;
2641 Py_DECREF(value);
2642 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002644 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2645 if (!result) goto failed;
2646 value = ast2obj_expr(o->v.ListComp.elt);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "elt", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 value = ast2obj_list(o->v.ListComp.generators,
2652 ast2obj_comprehension);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "generators", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002658 case SetComp_kind:
2659 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2660 if (!result) goto failed;
2661 value = ast2obj_expr(o->v.SetComp.elt);
2662 if (!value) goto failed;
2663 if (PyObject_SetAttrString(result, "elt", value) == -1)
2664 goto failed;
2665 Py_DECREF(value);
2666 value = ast2obj_list(o->v.SetComp.generators,
2667 ast2obj_comprehension);
2668 if (!value) goto failed;
2669 if (PyObject_SetAttrString(result, "generators", value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
2672 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002673 case DictComp_kind:
2674 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_expr(o->v.DictComp.key);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "key", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 value = ast2obj_expr(o->v.DictComp.value);
2682 if (!value) goto failed;
2683 if (PyObject_SetAttrString(result, "value", value) == -1)
2684 goto failed;
2685 Py_DECREF(value);
2686 value = ast2obj_list(o->v.DictComp.generators,
2687 ast2obj_comprehension);
2688 if (!value) goto failed;
2689 if (PyObject_SetAttrString(result, "generators", value) == -1)
2690 goto failed;
2691 Py_DECREF(value);
2692 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002694 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2695 if (!result) goto failed;
2696 value = ast2obj_expr(o->v.GeneratorExp.elt);
2697 if (!value) goto failed;
2698 if (PyObject_SetAttrString(result, "elt", value) == -1)
2699 goto failed;
2700 Py_DECREF(value);
2701 value = ast2obj_list(o->v.GeneratorExp.generators,
2702 ast2obj_comprehension);
2703 if (!value) goto failed;
2704 if (PyObject_SetAttrString(result, "generators", value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 break;
2708 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002709 result = PyType_GenericNew(Yield_type, NULL, NULL);
2710 if (!result) goto failed;
2711 value = ast2obj_expr(o->v.Yield.value);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "value", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 break;
2717 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002718 result = PyType_GenericNew(Compare_type, NULL, NULL);
2719 if (!result) goto failed;
2720 value = ast2obj_expr(o->v.Compare.left);
2721 if (!value) goto failed;
2722 if (PyObject_SetAttrString(result, "left", value) == -1)
2723 goto failed;
2724 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002725 {
2726 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2727 value = PyList_New(n);
2728 if (!value) goto failed;
2729 for(i = 0; i < n; i++)
2730 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2731 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002732 if (!value) goto failed;
2733 if (PyObject_SetAttrString(result, "ops", value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2737 if (!value) goto failed;
2738 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 break;
2742 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002743 result = PyType_GenericNew(Call_type, NULL, NULL);
2744 if (!result) goto failed;
2745 value = ast2obj_expr(o->v.Call.func);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "func", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
2750 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2751 if (!value) goto failed;
2752 if (PyObject_SetAttrString(result, "args", value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
2755 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2756 if (!value) goto failed;
2757 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
2760 value = ast2obj_expr(o->v.Call.starargs);
2761 if (!value) goto failed;
2762 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
2765 value = ast2obj_expr(o->v.Call.kwargs);
2766 if (!value) goto failed;
2767 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002772 result = PyType_GenericNew(Num_type, NULL, NULL);
2773 if (!result) goto failed;
2774 value = ast2obj_object(o->v.Num.n);
2775 if (!value) goto failed;
2776 if (PyObject_SetAttrString(result, "n", value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 break;
2780 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002781 result = PyType_GenericNew(Str_type, NULL, NULL);
2782 if (!result) goto failed;
2783 value = ast2obj_string(o->v.Str.s);
2784 if (!value) goto failed;
2785 if (PyObject_SetAttrString(result, "s", value) == -1)
2786 goto failed;
2787 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002789 case Bytes_kind:
2790 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2791 if (!result) goto failed;
2792 value = ast2obj_string(o->v.Bytes.s);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "s", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002798 case Ellipsis_kind:
2799 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2800 if (!result) goto failed;
2801 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002803 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2804 if (!result) goto failed;
2805 value = ast2obj_expr(o->v.Attribute.value);
2806 if (!value) goto failed;
2807 if (PyObject_SetAttrString(result, "value", value) == -1)
2808 goto failed;
2809 Py_DECREF(value);
2810 value = ast2obj_identifier(o->v.Attribute.attr);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "attr", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
2815 value = ast2obj_expr_context(o->v.Attribute.ctx);
2816 if (!value) goto failed;
2817 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 break;
2821 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002822 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2823 if (!result) goto failed;
2824 value = ast2obj_expr(o->v.Subscript.value);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "value", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 value = ast2obj_slice(o->v.Subscript.slice);
2830 if (!value) goto failed;
2831 if (PyObject_SetAttrString(result, "slice", value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 value = ast2obj_expr_context(o->v.Subscript.ctx);
2835 if (!value) goto failed;
2836 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2837 goto failed;
2838 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002840 case Starred_kind:
2841 result = PyType_GenericNew(Starred_type, NULL, NULL);
2842 if (!result) goto failed;
2843 value = ast2obj_expr(o->v.Starred.value);
2844 if (!value) goto failed;
2845 if (PyObject_SetAttrString(result, "value", value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
2848 value = ast2obj_expr_context(o->v.Starred.ctx);
2849 if (!value) goto failed;
2850 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002855 result = PyType_GenericNew(Name_type, NULL, NULL);
2856 if (!result) goto failed;
2857 value = ast2obj_identifier(o->v.Name.id);
2858 if (!value) goto failed;
2859 if (PyObject_SetAttrString(result, "id", value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 value = ast2obj_expr_context(o->v.Name.ctx);
2863 if (!value) goto failed;
2864 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2865 goto failed;
2866 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 break;
2868 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002869 result = PyType_GenericNew(List_type, NULL, NULL);
2870 if (!result) goto failed;
2871 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2872 if (!value) goto failed;
2873 if (PyObject_SetAttrString(result, "elts", value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
2876 value = ast2obj_expr_context(o->v.List.ctx);
2877 if (!value) goto failed;
2878 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2879 goto failed;
2880 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 break;
2882 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002883 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2884 if (!result) goto failed;
2885 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2886 if (!value) goto failed;
2887 if (PyObject_SetAttrString(result, "elts", value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_expr_context(o->v.Tuple.ctx);
2891 if (!value) goto failed;
2892 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2893 goto failed;
2894 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 break;
2896 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002897 value = ast2obj_int(o->lineno);
2898 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002899 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2900 goto failed;
2901 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002902 value = ast2obj_int(o->col_offset);
2903 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002904 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2905 goto failed;
2906 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002907 return result;
2908failed:
2909 Py_XDECREF(value);
2910 Py_XDECREF(result);
2911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912}
2913
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002914PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002916 switch(o) {
2917 case Load:
2918 Py_INCREF(Load_singleton);
2919 return Load_singleton;
2920 case Store:
2921 Py_INCREF(Store_singleton);
2922 return Store_singleton;
2923 case Del:
2924 Py_INCREF(Del_singleton);
2925 return Del_singleton;
2926 case AugLoad:
2927 Py_INCREF(AugLoad_singleton);
2928 return AugLoad_singleton;
2929 case AugStore:
2930 Py_INCREF(AugStore_singleton);
2931 return AugStore_singleton;
2932 case Param:
2933 Py_INCREF(Param_singleton);
2934 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002935 default:
2936 /* should never happen, but just in case ... */
2937 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941PyObject*
2942ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002944 slice_ty o = (slice_ty)_o;
2945 PyObject *result = NULL, *value = NULL;
2946 if (!o) {
2947 Py_INCREF(Py_None);
2948 return Py_None;
2949 }
2950
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002953 result = PyType_GenericNew(Slice_type, NULL, NULL);
2954 if (!result) goto failed;
2955 value = ast2obj_expr(o->v.Slice.lower);
2956 if (!value) goto failed;
2957 if (PyObject_SetAttrString(result, "lower", value) == -1)
2958 goto failed;
2959 Py_DECREF(value);
2960 value = ast2obj_expr(o->v.Slice.upper);
2961 if (!value) goto failed;
2962 if (PyObject_SetAttrString(result, "upper", value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
2965 value = ast2obj_expr(o->v.Slice.step);
2966 if (!value) goto failed;
2967 if (PyObject_SetAttrString(result, "step", value) == -1)
2968 goto failed;
2969 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970 break;
2971 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002972 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2973 if (!result) goto failed;
2974 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2975 if (!value) goto failed;
2976 if (PyObject_SetAttrString(result, "dims", value) == -1)
2977 goto failed;
2978 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 break;
2980 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002981 result = PyType_GenericNew(Index_type, NULL, NULL);
2982 if (!result) goto failed;
2983 value = ast2obj_expr(o->v.Index.value);
2984 if (!value) goto failed;
2985 if (PyObject_SetAttrString(result, "value", value) == -1)
2986 goto failed;
2987 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 break;
2989 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002990 return result;
2991failed:
2992 Py_XDECREF(value);
2993 Py_XDECREF(result);
2994 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995}
2996
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002997PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002999 switch(o) {
3000 case And:
3001 Py_INCREF(And_singleton);
3002 return And_singleton;
3003 case Or:
3004 Py_INCREF(Or_singleton);
3005 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003006 default:
3007 /* should never happen, but just in case ... */
3008 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003011}
3012PyObject* ast2obj_operator(operator_ty o)
3013{
3014 switch(o) {
3015 case Add:
3016 Py_INCREF(Add_singleton);
3017 return Add_singleton;
3018 case Sub:
3019 Py_INCREF(Sub_singleton);
3020 return Sub_singleton;
3021 case Mult:
3022 Py_INCREF(Mult_singleton);
3023 return Mult_singleton;
3024 case Div:
3025 Py_INCREF(Div_singleton);
3026 return Div_singleton;
3027 case Mod:
3028 Py_INCREF(Mod_singleton);
3029 return Mod_singleton;
3030 case Pow:
3031 Py_INCREF(Pow_singleton);
3032 return Pow_singleton;
3033 case LShift:
3034 Py_INCREF(LShift_singleton);
3035 return LShift_singleton;
3036 case RShift:
3037 Py_INCREF(RShift_singleton);
3038 return RShift_singleton;
3039 case BitOr:
3040 Py_INCREF(BitOr_singleton);
3041 return BitOr_singleton;
3042 case BitXor:
3043 Py_INCREF(BitXor_singleton);
3044 return BitXor_singleton;
3045 case BitAnd:
3046 Py_INCREF(BitAnd_singleton);
3047 return BitAnd_singleton;
3048 case FloorDiv:
3049 Py_INCREF(FloorDiv_singleton);
3050 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003051 default:
3052 /* should never happen, but just in case ... */
3053 PyErr_Format(PyExc_SystemError, "unknown operator found");
3054 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003055 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003056}
3057PyObject* ast2obj_unaryop(unaryop_ty o)
3058{
3059 switch(o) {
3060 case Invert:
3061 Py_INCREF(Invert_singleton);
3062 return Invert_singleton;
3063 case Not:
3064 Py_INCREF(Not_singleton);
3065 return Not_singleton;
3066 case UAdd:
3067 Py_INCREF(UAdd_singleton);
3068 return UAdd_singleton;
3069 case USub:
3070 Py_INCREF(USub_singleton);
3071 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003072 default:
3073 /* should never happen, but just in case ... */
3074 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3075 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003076 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003077}
3078PyObject* ast2obj_cmpop(cmpop_ty o)
3079{
3080 switch(o) {
3081 case Eq:
3082 Py_INCREF(Eq_singleton);
3083 return Eq_singleton;
3084 case NotEq:
3085 Py_INCREF(NotEq_singleton);
3086 return NotEq_singleton;
3087 case Lt:
3088 Py_INCREF(Lt_singleton);
3089 return Lt_singleton;
3090 case LtE:
3091 Py_INCREF(LtE_singleton);
3092 return LtE_singleton;
3093 case Gt:
3094 Py_INCREF(Gt_singleton);
3095 return Gt_singleton;
3096 case GtE:
3097 Py_INCREF(GtE_singleton);
3098 return GtE_singleton;
3099 case Is:
3100 Py_INCREF(Is_singleton);
3101 return Is_singleton;
3102 case IsNot:
3103 Py_INCREF(IsNot_singleton);
3104 return IsNot_singleton;
3105 case In:
3106 Py_INCREF(In_singleton);
3107 return In_singleton;
3108 case NotIn:
3109 Py_INCREF(NotIn_singleton);
3110 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003111 default:
3112 /* should never happen, but just in case ... */
3113 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3114 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003115 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003116}
3117PyObject*
3118ast2obj_comprehension(void* _o)
3119{
3120 comprehension_ty o = (comprehension_ty)_o;
3121 PyObject *result = NULL, *value = NULL;
3122 if (!o) {
3123 Py_INCREF(Py_None);
3124 return Py_None;
3125 }
3126
3127 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3128 if (!result) return NULL;
3129 value = ast2obj_expr(o->target);
3130 if (!value) goto failed;
3131 if (PyObject_SetAttrString(result, "target", value) == -1)
3132 goto failed;
3133 Py_DECREF(value);
3134 value = ast2obj_expr(o->iter);
3135 if (!value) goto failed;
3136 if (PyObject_SetAttrString(result, "iter", value) == -1)
3137 goto failed;
3138 Py_DECREF(value);
3139 value = ast2obj_list(o->ifs, ast2obj_expr);
3140 if (!value) goto failed;
3141 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3142 goto failed;
3143 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003144 return result;
3145failed:
3146 Py_XDECREF(value);
3147 Py_XDECREF(result);
3148 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149}
3150
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003151PyObject*
3152ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003154 excepthandler_ty o = (excepthandler_ty)_o;
3155 PyObject *result = NULL, *value = NULL;
3156 if (!o) {
3157 Py_INCREF(Py_None);
3158 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003160
Neal Norwitzad74aa82008-03-31 05:14:30 +00003161 switch (o->kind) {
3162 case ExceptHandler_kind:
3163 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3164 if (!result) goto failed;
3165 value = ast2obj_expr(o->v.ExceptHandler.type);
3166 if (!value) goto failed;
3167 if (PyObject_SetAttrString(result, "type", value) == -1)
3168 goto failed;
3169 Py_DECREF(value);
3170 value = ast2obj_identifier(o->v.ExceptHandler.name);
3171 if (!value) goto failed;
3172 if (PyObject_SetAttrString(result, "name", value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3176 if (!value) goto failed;
3177 if (PyObject_SetAttrString(result, "body", value) == -1)
3178 goto failed;
3179 Py_DECREF(value);
3180 break;
3181 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003182 value = ast2obj_int(o->lineno);
3183 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003184 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003185 goto failed;
3186 Py_DECREF(value);
3187 value = ast2obj_int(o->col_offset);
3188 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003189 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003190 goto failed;
3191 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003192 return result;
3193failed:
3194 Py_XDECREF(value);
3195 Py_XDECREF(result);
3196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197}
3198
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003199PyObject*
3200ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003202 arguments_ty o = (arguments_ty)_o;
3203 PyObject *result = NULL, *value = NULL;
3204 if (!o) {
3205 Py_INCREF(Py_None);
3206 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003208
3209 result = PyType_GenericNew(arguments_type, NULL, NULL);
3210 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003211 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003212 if (!value) goto failed;
3213 if (PyObject_SetAttrString(result, "args", value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 value = ast2obj_identifier(o->vararg);
3217 if (!value) goto failed;
3218 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3219 goto failed;
3220 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003221 value = ast2obj_expr(o->varargannotation);
3222 if (!value) goto failed;
3223 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3224 goto failed;
3225 Py_DECREF(value);
3226 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003227 if (!value) goto failed;
3228 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3229 goto failed;
3230 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003231 value = ast2obj_identifier(o->kwarg);
3232 if (!value) goto failed;
3233 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3234 goto failed;
3235 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003236 value = ast2obj_expr(o->kwargannotation);
3237 if (!value) goto failed;
3238 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3239 goto failed;
3240 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003241 value = ast2obj_list(o->defaults, ast2obj_expr);
3242 if (!value) goto failed;
3243 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3244 goto failed;
3245 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003246 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3247 if (!value) goto failed;
3248 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3249 goto failed;
3250 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003251 return result;
3252failed:
3253 Py_XDECREF(value);
3254 Py_XDECREF(result);
3255 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256}
3257
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003258PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003259ast2obj_arg(void* _o)
3260{
3261 arg_ty o = (arg_ty)_o;
3262 PyObject *result = NULL, *value = NULL;
3263 if (!o) {
3264 Py_INCREF(Py_None);
3265 return Py_None;
3266 }
3267
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003268 result = PyType_GenericNew(arg_type, NULL, NULL);
3269 if (!result) return NULL;
3270 value = ast2obj_identifier(o->arg);
3271 if (!value) goto failed;
3272 if (PyObject_SetAttrString(result, "arg", value) == -1)
3273 goto failed;
3274 Py_DECREF(value);
3275 value = ast2obj_expr(o->annotation);
3276 if (!value) goto failed;
3277 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3278 goto failed;
3279 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003280 return result;
3281failed:
3282 Py_XDECREF(value);
3283 Py_XDECREF(result);
3284 return NULL;
3285}
3286
3287PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003288ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003290 keyword_ty o = (keyword_ty)_o;
3291 PyObject *result = NULL, *value = NULL;
3292 if (!o) {
3293 Py_INCREF(Py_None);
3294 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003296
3297 result = PyType_GenericNew(keyword_type, NULL, NULL);
3298 if (!result) return NULL;
3299 value = ast2obj_identifier(o->arg);
3300 if (!value) goto failed;
3301 if (PyObject_SetAttrString(result, "arg", value) == -1)
3302 goto failed;
3303 Py_DECREF(value);
3304 value = ast2obj_expr(o->value);
3305 if (!value) goto failed;
3306 if (PyObject_SetAttrString(result, "value", value) == -1)
3307 goto failed;
3308 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003309 return result;
3310failed:
3311 Py_XDECREF(value);
3312 Py_XDECREF(result);
3313 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314}
3315
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003316PyObject*
3317ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003319 alias_ty o = (alias_ty)_o;
3320 PyObject *result = NULL, *value = NULL;
3321 if (!o) {
3322 Py_INCREF(Py_None);
3323 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003325
3326 result = PyType_GenericNew(alias_type, NULL, NULL);
3327 if (!result) return NULL;
3328 value = ast2obj_identifier(o->name);
3329 if (!value) goto failed;
3330 if (PyObject_SetAttrString(result, "name", value) == -1)
3331 goto failed;
3332 Py_DECREF(value);
3333 value = ast2obj_identifier(o->asname);
3334 if (!value) goto failed;
3335 if (PyObject_SetAttrString(result, "asname", value) == -1)
3336 goto failed;
3337 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003338 return result;
3339failed:
3340 Py_XDECREF(value);
3341 Py_XDECREF(result);
3342 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343}
3344
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003345PyObject*
3346ast2obj_withitem(void* _o)
3347{
3348 withitem_ty o = (withitem_ty)_o;
3349 PyObject *result = NULL, *value = NULL;
3350 if (!o) {
3351 Py_INCREF(Py_None);
3352 return Py_None;
3353 }
3354
3355 result = PyType_GenericNew(withitem_type, NULL, NULL);
3356 if (!result) return NULL;
3357 value = ast2obj_expr(o->context_expr);
3358 if (!value) goto failed;
3359 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3360 goto failed;
3361 Py_DECREF(value);
3362 value = ast2obj_expr(o->optional_vars);
3363 if (!value) goto failed;
3364 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3365 goto failed;
3366 Py_DECREF(value);
3367 return result;
3368failed:
3369 Py_XDECREF(value);
3370 Py_XDECREF(result);
3371 return NULL;
3372}
3373
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003375int
3376obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3377{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003378 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003379
Benjamin Petersond8f65972010-11-20 04:31:07 +00003380 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003381
3382 if (obj == Py_None) {
3383 *out = NULL;
3384 return 0;
3385 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003386 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3387 if (isinstance == -1) {
3388 return 1;
3389 }
3390 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003391 asdl_seq* body;
3392
3393 if (PyObject_HasAttrString(obj, "body")) {
3394 int res;
3395 Py_ssize_t len;
3396 Py_ssize_t i;
3397 tmp = PyObject_GetAttrString(obj, "body");
3398 if (tmp == NULL) goto failed;
3399 if (!PyList_Check(tmp)) {
3400 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3401 goto failed;
3402 }
3403 len = PyList_GET_SIZE(tmp);
3404 body = asdl_seq_new(len, arena);
3405 if (body == NULL) goto failed;
3406 for (i = 0; i < len; i++) {
3407 stmt_ty value;
3408 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3409 if (res != 0) goto failed;
3410 asdl_seq_SET(body, i, value);
3411 }
3412 Py_XDECREF(tmp);
3413 tmp = NULL;
3414 } else {
3415 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3416 return 1;
3417 }
3418 *out = Module(body, arena);
3419 if (*out == NULL) goto failed;
3420 return 0;
3421 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003422 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3423 if (isinstance == -1) {
3424 return 1;
3425 }
3426 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003427 asdl_seq* body;
3428
3429 if (PyObject_HasAttrString(obj, "body")) {
3430 int res;
3431 Py_ssize_t len;
3432 Py_ssize_t i;
3433 tmp = PyObject_GetAttrString(obj, "body");
3434 if (tmp == NULL) goto failed;
3435 if (!PyList_Check(tmp)) {
3436 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3437 goto failed;
3438 }
3439 len = PyList_GET_SIZE(tmp);
3440 body = asdl_seq_new(len, arena);
3441 if (body == NULL) goto failed;
3442 for (i = 0; i < len; i++) {
3443 stmt_ty value;
3444 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3445 if (res != 0) goto failed;
3446 asdl_seq_SET(body, i, value);
3447 }
3448 Py_XDECREF(tmp);
3449 tmp = NULL;
3450 } else {
3451 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3452 return 1;
3453 }
3454 *out = Interactive(body, arena);
3455 if (*out == NULL) goto failed;
3456 return 0;
3457 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003458 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3459 if (isinstance == -1) {
3460 return 1;
3461 }
3462 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003463 expr_ty body;
3464
3465 if (PyObject_HasAttrString(obj, "body")) {
3466 int res;
3467 tmp = PyObject_GetAttrString(obj, "body");
3468 if (tmp == NULL) goto failed;
3469 res = obj2ast_expr(tmp, &body, arena);
3470 if (res != 0) goto failed;
3471 Py_XDECREF(tmp);
3472 tmp = NULL;
3473 } else {
3474 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3475 return 1;
3476 }
3477 *out = Expression(body, arena);
3478 if (*out == NULL) goto failed;
3479 return 0;
3480 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003481 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3482 if (isinstance == -1) {
3483 return 1;
3484 }
3485 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003486 asdl_seq* body;
3487
3488 if (PyObject_HasAttrString(obj, "body")) {
3489 int res;
3490 Py_ssize_t len;
3491 Py_ssize_t i;
3492 tmp = PyObject_GetAttrString(obj, "body");
3493 if (tmp == NULL) goto failed;
3494 if (!PyList_Check(tmp)) {
3495 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3496 goto failed;
3497 }
3498 len = PyList_GET_SIZE(tmp);
3499 body = asdl_seq_new(len, arena);
3500 if (body == NULL) goto failed;
3501 for (i = 0; i < len; i++) {
3502 stmt_ty value;
3503 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3504 if (res != 0) goto failed;
3505 asdl_seq_SET(body, i, value);
3506 }
3507 Py_XDECREF(tmp);
3508 tmp = NULL;
3509 } else {
3510 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3511 return 1;
3512 }
3513 *out = Suite(body, arena);
3514 if (*out == NULL) goto failed;
3515 return 0;
3516 }
3517
Benjamin Peterson5b066812010-11-20 01:38:49 +00003518 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3519 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003520 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003521 return 1;
3522}
3523
3524int
3525obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3526{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003527 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003528
Benjamin Petersond8f65972010-11-20 04:31:07 +00003529 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003530 int lineno;
3531 int col_offset;
3532
3533 if (obj == Py_None) {
3534 *out = NULL;
3535 return 0;
3536 }
3537 if (PyObject_HasAttrString(obj, "lineno")) {
3538 int res;
3539 tmp = PyObject_GetAttrString(obj, "lineno");
3540 if (tmp == NULL) goto failed;
3541 res = obj2ast_int(tmp, &lineno, arena);
3542 if (res != 0) goto failed;
3543 Py_XDECREF(tmp);
3544 tmp = NULL;
3545 } else {
3546 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3547 return 1;
3548 }
3549 if (PyObject_HasAttrString(obj, "col_offset")) {
3550 int res;
3551 tmp = PyObject_GetAttrString(obj, "col_offset");
3552 if (tmp == NULL) goto failed;
3553 res = obj2ast_int(tmp, &col_offset, arena);
3554 if (res != 0) goto failed;
3555 Py_XDECREF(tmp);
3556 tmp = NULL;
3557 } else {
3558 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3559 return 1;
3560 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003561 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3562 if (isinstance == -1) {
3563 return 1;
3564 }
3565 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003566 identifier name;
3567 arguments_ty args;
3568 asdl_seq* body;
3569 asdl_seq* decorator_list;
3570 expr_ty returns;
3571
3572 if (PyObject_HasAttrString(obj, "name")) {
3573 int res;
3574 tmp = PyObject_GetAttrString(obj, "name");
3575 if (tmp == NULL) goto failed;
3576 res = obj2ast_identifier(tmp, &name, arena);
3577 if (res != 0) goto failed;
3578 Py_XDECREF(tmp);
3579 tmp = NULL;
3580 } else {
3581 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3582 return 1;
3583 }
3584 if (PyObject_HasAttrString(obj, "args")) {
3585 int res;
3586 tmp = PyObject_GetAttrString(obj, "args");
3587 if (tmp == NULL) goto failed;
3588 res = obj2ast_arguments(tmp, &args, arena);
3589 if (res != 0) goto failed;
3590 Py_XDECREF(tmp);
3591 tmp = NULL;
3592 } else {
3593 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3594 return 1;
3595 }
3596 if (PyObject_HasAttrString(obj, "body")) {
3597 int res;
3598 Py_ssize_t len;
3599 Py_ssize_t i;
3600 tmp = PyObject_GetAttrString(obj, "body");
3601 if (tmp == NULL) goto failed;
3602 if (!PyList_Check(tmp)) {
3603 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3604 goto failed;
3605 }
3606 len = PyList_GET_SIZE(tmp);
3607 body = asdl_seq_new(len, arena);
3608 if (body == NULL) goto failed;
3609 for (i = 0; i < len; i++) {
3610 stmt_ty value;
3611 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3612 if (res != 0) goto failed;
3613 asdl_seq_SET(body, i, value);
3614 }
3615 Py_XDECREF(tmp);
3616 tmp = NULL;
3617 } else {
3618 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3619 return 1;
3620 }
3621 if (PyObject_HasAttrString(obj, "decorator_list")) {
3622 int res;
3623 Py_ssize_t len;
3624 Py_ssize_t i;
3625 tmp = PyObject_GetAttrString(obj, "decorator_list");
3626 if (tmp == NULL) goto failed;
3627 if (!PyList_Check(tmp)) {
3628 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3629 goto failed;
3630 }
3631 len = PyList_GET_SIZE(tmp);
3632 decorator_list = asdl_seq_new(len, arena);
3633 if (decorator_list == NULL) goto failed;
3634 for (i = 0; i < len; i++) {
3635 expr_ty value;
3636 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3637 if (res != 0) goto failed;
3638 asdl_seq_SET(decorator_list, i, value);
3639 }
3640 Py_XDECREF(tmp);
3641 tmp = NULL;
3642 } else {
3643 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3644 return 1;
3645 }
3646 if (PyObject_HasAttrString(obj, "returns")) {
3647 int res;
3648 tmp = PyObject_GetAttrString(obj, "returns");
3649 if (tmp == NULL) goto failed;
3650 res = obj2ast_expr(tmp, &returns, arena);
3651 if (res != 0) goto failed;
3652 Py_XDECREF(tmp);
3653 tmp = NULL;
3654 } else {
3655 returns = NULL;
3656 }
3657 *out = FunctionDef(name, args, body, decorator_list, returns,
3658 lineno, col_offset, arena);
3659 if (*out == NULL) goto failed;
3660 return 0;
3661 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003662 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3663 if (isinstance == -1) {
3664 return 1;
3665 }
3666 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003667 identifier name;
3668 asdl_seq* bases;
3669 asdl_seq* keywords;
3670 expr_ty starargs;
3671 expr_ty kwargs;
3672 asdl_seq* body;
3673 asdl_seq* decorator_list;
3674
3675 if (PyObject_HasAttrString(obj, "name")) {
3676 int res;
3677 tmp = PyObject_GetAttrString(obj, "name");
3678 if (tmp == NULL) goto failed;
3679 res = obj2ast_identifier(tmp, &name, arena);
3680 if (res != 0) goto failed;
3681 Py_XDECREF(tmp);
3682 tmp = NULL;
3683 } else {
3684 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3685 return 1;
3686 }
3687 if (PyObject_HasAttrString(obj, "bases")) {
3688 int res;
3689 Py_ssize_t len;
3690 Py_ssize_t i;
3691 tmp = PyObject_GetAttrString(obj, "bases");
3692 if (tmp == NULL) goto failed;
3693 if (!PyList_Check(tmp)) {
3694 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3695 goto failed;
3696 }
3697 len = PyList_GET_SIZE(tmp);
3698 bases = asdl_seq_new(len, arena);
3699 if (bases == NULL) goto failed;
3700 for (i = 0; i < len; i++) {
3701 expr_ty value;
3702 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3703 if (res != 0) goto failed;
3704 asdl_seq_SET(bases, i, value);
3705 }
3706 Py_XDECREF(tmp);
3707 tmp = NULL;
3708 } else {
3709 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3710 return 1;
3711 }
3712 if (PyObject_HasAttrString(obj, "keywords")) {
3713 int res;
3714 Py_ssize_t len;
3715 Py_ssize_t i;
3716 tmp = PyObject_GetAttrString(obj, "keywords");
3717 if (tmp == NULL) goto failed;
3718 if (!PyList_Check(tmp)) {
3719 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3720 goto failed;
3721 }
3722 len = PyList_GET_SIZE(tmp);
3723 keywords = asdl_seq_new(len, arena);
3724 if (keywords == NULL) goto failed;
3725 for (i = 0; i < len; i++) {
3726 keyword_ty value;
3727 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3728 if (res != 0) goto failed;
3729 asdl_seq_SET(keywords, i, value);
3730 }
3731 Py_XDECREF(tmp);
3732 tmp = NULL;
3733 } else {
3734 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3735 return 1;
3736 }
3737 if (PyObject_HasAttrString(obj, "starargs")) {
3738 int res;
3739 tmp = PyObject_GetAttrString(obj, "starargs");
3740 if (tmp == NULL) goto failed;
3741 res = obj2ast_expr(tmp, &starargs, arena);
3742 if (res != 0) goto failed;
3743 Py_XDECREF(tmp);
3744 tmp = NULL;
3745 } else {
3746 starargs = NULL;
3747 }
3748 if (PyObject_HasAttrString(obj, "kwargs")) {
3749 int res;
3750 tmp = PyObject_GetAttrString(obj, "kwargs");
3751 if (tmp == NULL) goto failed;
3752 res = obj2ast_expr(tmp, &kwargs, arena);
3753 if (res != 0) goto failed;
3754 Py_XDECREF(tmp);
3755 tmp = NULL;
3756 } else {
3757 kwargs = NULL;
3758 }
3759 if (PyObject_HasAttrString(obj, "body")) {
3760 int res;
3761 Py_ssize_t len;
3762 Py_ssize_t i;
3763 tmp = PyObject_GetAttrString(obj, "body");
3764 if (tmp == NULL) goto failed;
3765 if (!PyList_Check(tmp)) {
3766 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3767 goto failed;
3768 }
3769 len = PyList_GET_SIZE(tmp);
3770 body = asdl_seq_new(len, arena);
3771 if (body == NULL) goto failed;
3772 for (i = 0; i < len; i++) {
3773 stmt_ty value;
3774 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3775 if (res != 0) goto failed;
3776 asdl_seq_SET(body, i, value);
3777 }
3778 Py_XDECREF(tmp);
3779 tmp = NULL;
3780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3782 return 1;
3783 }
3784 if (PyObject_HasAttrString(obj, "decorator_list")) {
3785 int res;
3786 Py_ssize_t len;
3787 Py_ssize_t i;
3788 tmp = PyObject_GetAttrString(obj, "decorator_list");
3789 if (tmp == NULL) goto failed;
3790 if (!PyList_Check(tmp)) {
3791 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3792 goto failed;
3793 }
3794 len = PyList_GET_SIZE(tmp);
3795 decorator_list = asdl_seq_new(len, arena);
3796 if (decorator_list == NULL) goto failed;
3797 for (i = 0; i < len; i++) {
3798 expr_ty value;
3799 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3800 if (res != 0) goto failed;
3801 asdl_seq_SET(decorator_list, i, value);
3802 }
3803 Py_XDECREF(tmp);
3804 tmp = NULL;
3805 } else {
3806 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3807 return 1;
3808 }
3809 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3810 decorator_list, lineno, col_offset, arena);
3811 if (*out == NULL) goto failed;
3812 return 0;
3813 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003814 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3815 if (isinstance == -1) {
3816 return 1;
3817 }
3818 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003819 expr_ty value;
3820
3821 if (PyObject_HasAttrString(obj, "value")) {
3822 int res;
3823 tmp = PyObject_GetAttrString(obj, "value");
3824 if (tmp == NULL) goto failed;
3825 res = obj2ast_expr(tmp, &value, arena);
3826 if (res != 0) goto failed;
3827 Py_XDECREF(tmp);
3828 tmp = NULL;
3829 } else {
3830 value = NULL;
3831 }
3832 *out = Return(value, lineno, col_offset, arena);
3833 if (*out == NULL) goto failed;
3834 return 0;
3835 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003836 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3837 if (isinstance == -1) {
3838 return 1;
3839 }
3840 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003841 asdl_seq* targets;
3842
3843 if (PyObject_HasAttrString(obj, "targets")) {
3844 int res;
3845 Py_ssize_t len;
3846 Py_ssize_t i;
3847 tmp = PyObject_GetAttrString(obj, "targets");
3848 if (tmp == NULL) goto failed;
3849 if (!PyList_Check(tmp)) {
3850 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3851 goto failed;
3852 }
3853 len = PyList_GET_SIZE(tmp);
3854 targets = asdl_seq_new(len, arena);
3855 if (targets == NULL) goto failed;
3856 for (i = 0; i < len; i++) {
3857 expr_ty value;
3858 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3859 if (res != 0) goto failed;
3860 asdl_seq_SET(targets, i, value);
3861 }
3862 Py_XDECREF(tmp);
3863 tmp = NULL;
3864 } else {
3865 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3866 return 1;
3867 }
3868 *out = Delete(targets, lineno, col_offset, arena);
3869 if (*out == NULL) goto failed;
3870 return 0;
3871 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003872 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3873 if (isinstance == -1) {
3874 return 1;
3875 }
3876 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003877 asdl_seq* targets;
3878 expr_ty value;
3879
3880 if (PyObject_HasAttrString(obj, "targets")) {
3881 int res;
3882 Py_ssize_t len;
3883 Py_ssize_t i;
3884 tmp = PyObject_GetAttrString(obj, "targets");
3885 if (tmp == NULL) goto failed;
3886 if (!PyList_Check(tmp)) {
3887 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3888 goto failed;
3889 }
3890 len = PyList_GET_SIZE(tmp);
3891 targets = asdl_seq_new(len, arena);
3892 if (targets == NULL) goto failed;
3893 for (i = 0; i < len; i++) {
3894 expr_ty value;
3895 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3896 if (res != 0) goto failed;
3897 asdl_seq_SET(targets, i, value);
3898 }
3899 Py_XDECREF(tmp);
3900 tmp = NULL;
3901 } else {
3902 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3903 return 1;
3904 }
3905 if (PyObject_HasAttrString(obj, "value")) {
3906 int res;
3907 tmp = PyObject_GetAttrString(obj, "value");
3908 if (tmp == NULL) goto failed;
3909 res = obj2ast_expr(tmp, &value, arena);
3910 if (res != 0) goto failed;
3911 Py_XDECREF(tmp);
3912 tmp = NULL;
3913 } else {
3914 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3915 return 1;
3916 }
3917 *out = Assign(targets, value, lineno, col_offset, arena);
3918 if (*out == NULL) goto failed;
3919 return 0;
3920 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003921 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3922 if (isinstance == -1) {
3923 return 1;
3924 }
3925 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003926 expr_ty target;
3927 operator_ty op;
3928 expr_ty value;
3929
3930 if (PyObject_HasAttrString(obj, "target")) {
3931 int res;
3932 tmp = PyObject_GetAttrString(obj, "target");
3933 if (tmp == NULL) goto failed;
3934 res = obj2ast_expr(tmp, &target, arena);
3935 if (res != 0) goto failed;
3936 Py_XDECREF(tmp);
3937 tmp = NULL;
3938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3940 return 1;
3941 }
3942 if (PyObject_HasAttrString(obj, "op")) {
3943 int res;
3944 tmp = PyObject_GetAttrString(obj, "op");
3945 if (tmp == NULL) goto failed;
3946 res = obj2ast_operator(tmp, &op, arena);
3947 if (res != 0) goto failed;
3948 Py_XDECREF(tmp);
3949 tmp = NULL;
3950 } else {
3951 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3952 return 1;
3953 }
3954 if (PyObject_HasAttrString(obj, "value")) {
3955 int res;
3956 tmp = PyObject_GetAttrString(obj, "value");
3957 if (tmp == NULL) goto failed;
3958 res = obj2ast_expr(tmp, &value, arena);
3959 if (res != 0) goto failed;
3960 Py_XDECREF(tmp);
3961 tmp = NULL;
3962 } else {
3963 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3964 return 1;
3965 }
3966 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3967 if (*out == NULL) goto failed;
3968 return 0;
3969 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003970 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3971 if (isinstance == -1) {
3972 return 1;
3973 }
3974 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003975 expr_ty target;
3976 expr_ty iter;
3977 asdl_seq* body;
3978 asdl_seq* orelse;
3979
3980 if (PyObject_HasAttrString(obj, "target")) {
3981 int res;
3982 tmp = PyObject_GetAttrString(obj, "target");
3983 if (tmp == NULL) goto failed;
3984 res = obj2ast_expr(tmp, &target, arena);
3985 if (res != 0) goto failed;
3986 Py_XDECREF(tmp);
3987 tmp = NULL;
3988 } else {
3989 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3990 return 1;
3991 }
3992 if (PyObject_HasAttrString(obj, "iter")) {
3993 int res;
3994 tmp = PyObject_GetAttrString(obj, "iter");
3995 if (tmp == NULL) goto failed;
3996 res = obj2ast_expr(tmp, &iter, arena);
3997 if (res != 0) goto failed;
3998 Py_XDECREF(tmp);
3999 tmp = NULL;
4000 } else {
4001 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4002 return 1;
4003 }
4004 if (PyObject_HasAttrString(obj, "body")) {
4005 int res;
4006 Py_ssize_t len;
4007 Py_ssize_t i;
4008 tmp = PyObject_GetAttrString(obj, "body");
4009 if (tmp == NULL) goto failed;
4010 if (!PyList_Check(tmp)) {
4011 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4012 goto failed;
4013 }
4014 len = PyList_GET_SIZE(tmp);
4015 body = asdl_seq_new(len, arena);
4016 if (body == NULL) goto failed;
4017 for (i = 0; i < len; i++) {
4018 stmt_ty value;
4019 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4020 if (res != 0) goto failed;
4021 asdl_seq_SET(body, i, value);
4022 }
4023 Py_XDECREF(tmp);
4024 tmp = NULL;
4025 } else {
4026 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4027 return 1;
4028 }
4029 if (PyObject_HasAttrString(obj, "orelse")) {
4030 int res;
4031 Py_ssize_t len;
4032 Py_ssize_t i;
4033 tmp = PyObject_GetAttrString(obj, "orelse");
4034 if (tmp == NULL) goto failed;
4035 if (!PyList_Check(tmp)) {
4036 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4037 goto failed;
4038 }
4039 len = PyList_GET_SIZE(tmp);
4040 orelse = asdl_seq_new(len, arena);
4041 if (orelse == 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(orelse, i, value);
4047 }
4048 Py_XDECREF(tmp);
4049 tmp = NULL;
4050 } else {
4051 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4052 return 1;
4053 }
4054 *out = For(target, iter, body, orelse, lineno, col_offset,
4055 arena);
4056 if (*out == NULL) goto failed;
4057 return 0;
4058 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004059 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4060 if (isinstance == -1) {
4061 return 1;
4062 }
4063 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004064 expr_ty test;
4065 asdl_seq* body;
4066 asdl_seq* orelse;
4067
4068 if (PyObject_HasAttrString(obj, "test")) {
4069 int res;
4070 tmp = PyObject_GetAttrString(obj, "test");
4071 if (tmp == NULL) goto failed;
4072 res = obj2ast_expr(tmp, &test, arena);
4073 if (res != 0) goto failed;
4074 Py_XDECREF(tmp);
4075 tmp = NULL;
4076 } else {
4077 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4078 return 1;
4079 }
4080 if (PyObject_HasAttrString(obj, "body")) {
4081 int res;
4082 Py_ssize_t len;
4083 Py_ssize_t i;
4084 tmp = PyObject_GetAttrString(obj, "body");
4085 if (tmp == NULL) goto failed;
4086 if (!PyList_Check(tmp)) {
4087 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4088 goto failed;
4089 }
4090 len = PyList_GET_SIZE(tmp);
4091 body = asdl_seq_new(len, arena);
4092 if (body == NULL) goto failed;
4093 for (i = 0; i < len; i++) {
4094 stmt_ty value;
4095 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4096 if (res != 0) goto failed;
4097 asdl_seq_SET(body, i, value);
4098 }
4099 Py_XDECREF(tmp);
4100 tmp = NULL;
4101 } else {
4102 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4103 return 1;
4104 }
4105 if (PyObject_HasAttrString(obj, "orelse")) {
4106 int res;
4107 Py_ssize_t len;
4108 Py_ssize_t i;
4109 tmp = PyObject_GetAttrString(obj, "orelse");
4110 if (tmp == NULL) goto failed;
4111 if (!PyList_Check(tmp)) {
4112 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4113 goto failed;
4114 }
4115 len = PyList_GET_SIZE(tmp);
4116 orelse = asdl_seq_new(len, arena);
4117 if (orelse == 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(orelse, i, value);
4123 }
4124 Py_XDECREF(tmp);
4125 tmp = NULL;
4126 } else {
4127 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4128 return 1;
4129 }
4130 *out = While(test, body, orelse, lineno, col_offset, arena);
4131 if (*out == NULL) goto failed;
4132 return 0;
4133 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004134 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4135 if (isinstance == -1) {
4136 return 1;
4137 }
4138 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004139 expr_ty test;
4140 asdl_seq* body;
4141 asdl_seq* orelse;
4142
4143 if (PyObject_HasAttrString(obj, "test")) {
4144 int res;
4145 tmp = PyObject_GetAttrString(obj, "test");
4146 if (tmp == NULL) goto failed;
4147 res = obj2ast_expr(tmp, &test, arena);
4148 if (res != 0) goto failed;
4149 Py_XDECREF(tmp);
4150 tmp = NULL;
4151 } else {
4152 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4153 return 1;
4154 }
4155 if (PyObject_HasAttrString(obj, "body")) {
4156 int res;
4157 Py_ssize_t len;
4158 Py_ssize_t i;
4159 tmp = PyObject_GetAttrString(obj, "body");
4160 if (tmp == NULL) goto failed;
4161 if (!PyList_Check(tmp)) {
4162 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4163 goto failed;
4164 }
4165 len = PyList_GET_SIZE(tmp);
4166 body = asdl_seq_new(len, arena);
4167 if (body == NULL) goto failed;
4168 for (i = 0; i < len; i++) {
4169 stmt_ty value;
4170 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4171 if (res != 0) goto failed;
4172 asdl_seq_SET(body, i, value);
4173 }
4174 Py_XDECREF(tmp);
4175 tmp = NULL;
4176 } else {
4177 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4178 return 1;
4179 }
4180 if (PyObject_HasAttrString(obj, "orelse")) {
4181 int res;
4182 Py_ssize_t len;
4183 Py_ssize_t i;
4184 tmp = PyObject_GetAttrString(obj, "orelse");
4185 if (tmp == NULL) goto failed;
4186 if (!PyList_Check(tmp)) {
4187 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4188 goto failed;
4189 }
4190 len = PyList_GET_SIZE(tmp);
4191 orelse = asdl_seq_new(len, arena);
4192 if (orelse == 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(orelse, i, value);
4198 }
4199 Py_XDECREF(tmp);
4200 tmp = NULL;
4201 } else {
4202 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4203 return 1;
4204 }
4205 *out = If(test, body, orelse, lineno, col_offset, arena);
4206 if (*out == NULL) goto failed;
4207 return 0;
4208 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004209 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4210 if (isinstance == -1) {
4211 return 1;
4212 }
4213 if (isinstance) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004214 asdl_seq* items;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004215 asdl_seq* body;
4216
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004217 if (PyObject_HasAttrString(obj, "items")) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004218 int res;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004219 Py_ssize_t len;
4220 Py_ssize_t i;
4221 tmp = PyObject_GetAttrString(obj, "items");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004222 if (tmp == NULL) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004223 if (!PyList_Check(tmp)) {
4224 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4225 goto failed;
4226 }
4227 len = PyList_GET_SIZE(tmp);
4228 items = asdl_seq_new(len, arena);
4229 if (items == NULL) goto failed;
4230 for (i = 0; i < len; i++) {
4231 withitem_ty value;
4232 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4233 if (res != 0) goto failed;
4234 asdl_seq_SET(items, i, value);
4235 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004236 Py_XDECREF(tmp);
4237 tmp = NULL;
4238 } else {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004239 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004240 return 1;
4241 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004242 if (PyObject_HasAttrString(obj, "body")) {
4243 int res;
4244 Py_ssize_t len;
4245 Py_ssize_t i;
4246 tmp = PyObject_GetAttrString(obj, "body");
4247 if (tmp == NULL) goto failed;
4248 if (!PyList_Check(tmp)) {
4249 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4250 goto failed;
4251 }
4252 len = PyList_GET_SIZE(tmp);
4253 body = asdl_seq_new(len, arena);
4254 if (body == NULL) goto failed;
4255 for (i = 0; i < len; i++) {
4256 stmt_ty value;
4257 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4258 if (res != 0) goto failed;
4259 asdl_seq_SET(body, i, value);
4260 }
4261 Py_XDECREF(tmp);
4262 tmp = NULL;
4263 } else {
4264 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4265 return 1;
4266 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004267 *out = With(items, body, lineno, col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004268 if (*out == NULL) goto failed;
4269 return 0;
4270 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004271 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4272 if (isinstance == -1) {
4273 return 1;
4274 }
4275 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004276 expr_ty exc;
4277 expr_ty cause;
4278
4279 if (PyObject_HasAttrString(obj, "exc")) {
4280 int res;
4281 tmp = PyObject_GetAttrString(obj, "exc");
4282 if (tmp == NULL) goto failed;
4283 res = obj2ast_expr(tmp, &exc, arena);
4284 if (res != 0) goto failed;
4285 Py_XDECREF(tmp);
4286 tmp = NULL;
4287 } else {
4288 exc = NULL;
4289 }
4290 if (PyObject_HasAttrString(obj, "cause")) {
4291 int res;
4292 tmp = PyObject_GetAttrString(obj, "cause");
4293 if (tmp == NULL) goto failed;
4294 res = obj2ast_expr(tmp, &cause, arena);
4295 if (res != 0) goto failed;
4296 Py_XDECREF(tmp);
4297 tmp = NULL;
4298 } else {
4299 cause = NULL;
4300 }
4301 *out = Raise(exc, cause, lineno, col_offset, arena);
4302 if (*out == NULL) goto failed;
4303 return 0;
4304 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004305 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004306 if (isinstance == -1) {
4307 return 1;
4308 }
4309 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004310 asdl_seq* body;
4311 asdl_seq* handlers;
4312 asdl_seq* orelse;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004313 asdl_seq* finalbody;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004314
4315 if (PyObject_HasAttrString(obj, "body")) {
4316 int res;
4317 Py_ssize_t len;
4318 Py_ssize_t i;
4319 tmp = PyObject_GetAttrString(obj, "body");
4320 if (tmp == NULL) goto failed;
4321 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004322 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 +00004323 goto failed;
4324 }
4325 len = PyList_GET_SIZE(tmp);
4326 body = asdl_seq_new(len, arena);
4327 if (body == NULL) goto failed;
4328 for (i = 0; i < len; i++) {
4329 stmt_ty value;
4330 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4331 if (res != 0) goto failed;
4332 asdl_seq_SET(body, i, value);
4333 }
4334 Py_XDECREF(tmp);
4335 tmp = NULL;
4336 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004337 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004338 return 1;
4339 }
4340 if (PyObject_HasAttrString(obj, "handlers")) {
4341 int res;
4342 Py_ssize_t len;
4343 Py_ssize_t i;
4344 tmp = PyObject_GetAttrString(obj, "handlers");
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 \"handlers\" 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 handlers = asdl_seq_new(len, arena);
4352 if (handlers == NULL) goto failed;
4353 for (i = 0; i < len; i++) {
4354 excepthandler_ty value;
4355 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4356 if (res != 0) goto failed;
4357 asdl_seq_SET(handlers, 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 \"handlers\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004363 return 1;
4364 }
4365 if (PyObject_HasAttrString(obj, "orelse")) {
4366 int res;
4367 Py_ssize_t len;
4368 Py_ssize_t i;
4369 tmp = PyObject_GetAttrString(obj, "orelse");
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 \"orelse\" 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 orelse = asdl_seq_new(len, arena);
4377 if (orelse == NULL) goto failed;
4378 for (i = 0; i < len; i++) {
4379 stmt_ty value;
4380 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4381 if (res != 0) goto failed;
4382 asdl_seq_SET(orelse, 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 \"orelse\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004388 return 1;
4389 }
4390 if (PyObject_HasAttrString(obj, "finalbody")) {
4391 int res;
4392 Py_ssize_t len;
4393 Py_ssize_t i;
4394 tmp = PyObject_GetAttrString(obj, "finalbody");
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 \"finalbody\" 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 finalbody = asdl_seq_new(len, arena);
4402 if (finalbody == 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(finalbody, 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 \"finalbody\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004413 return 1;
4414 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004415 *out = Try(body, handlers, orelse, finalbody, lineno,
4416 col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004417 if (*out == NULL) goto failed;
4418 return 0;
4419 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004420 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4421 if (isinstance == -1) {
4422 return 1;
4423 }
4424 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004425 expr_ty test;
4426 expr_ty msg;
4427
4428 if (PyObject_HasAttrString(obj, "test")) {
4429 int res;
4430 tmp = PyObject_GetAttrString(obj, "test");
4431 if (tmp == NULL) goto failed;
4432 res = obj2ast_expr(tmp, &test, arena);
4433 if (res != 0) goto failed;
4434 Py_XDECREF(tmp);
4435 tmp = NULL;
4436 } else {
4437 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4438 return 1;
4439 }
4440 if (PyObject_HasAttrString(obj, "msg")) {
4441 int res;
4442 tmp = PyObject_GetAttrString(obj, "msg");
4443 if (tmp == NULL) goto failed;
4444 res = obj2ast_expr(tmp, &msg, arena);
4445 if (res != 0) goto failed;
4446 Py_XDECREF(tmp);
4447 tmp = NULL;
4448 } else {
4449 msg = NULL;
4450 }
4451 *out = Assert(test, msg, lineno, col_offset, arena);
4452 if (*out == NULL) goto failed;
4453 return 0;
4454 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004455 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4456 if (isinstance == -1) {
4457 return 1;
4458 }
4459 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004460 asdl_seq* names;
4461
4462 if (PyObject_HasAttrString(obj, "names")) {
4463 int res;
4464 Py_ssize_t len;
4465 Py_ssize_t i;
4466 tmp = PyObject_GetAttrString(obj, "names");
4467 if (tmp == NULL) goto failed;
4468 if (!PyList_Check(tmp)) {
4469 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4470 goto failed;
4471 }
4472 len = PyList_GET_SIZE(tmp);
4473 names = asdl_seq_new(len, arena);
4474 if (names == NULL) goto failed;
4475 for (i = 0; i < len; i++) {
4476 alias_ty value;
4477 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4478 if (res != 0) goto failed;
4479 asdl_seq_SET(names, i, value);
4480 }
4481 Py_XDECREF(tmp);
4482 tmp = NULL;
4483 } else {
4484 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4485 return 1;
4486 }
4487 *out = Import(names, lineno, col_offset, arena);
4488 if (*out == NULL) goto failed;
4489 return 0;
4490 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004491 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4492 if (isinstance == -1) {
4493 return 1;
4494 }
4495 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004496 identifier module;
4497 asdl_seq* names;
4498 int level;
4499
4500 if (PyObject_HasAttrString(obj, "module")) {
4501 int res;
4502 tmp = PyObject_GetAttrString(obj, "module");
4503 if (tmp == NULL) goto failed;
4504 res = obj2ast_identifier(tmp, &module, arena);
4505 if (res != 0) goto failed;
4506 Py_XDECREF(tmp);
4507 tmp = NULL;
4508 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004509 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004510 }
4511 if (PyObject_HasAttrString(obj, "names")) {
4512 int res;
4513 Py_ssize_t len;
4514 Py_ssize_t i;
4515 tmp = PyObject_GetAttrString(obj, "names");
4516 if (tmp == NULL) goto failed;
4517 if (!PyList_Check(tmp)) {
4518 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4519 goto failed;
4520 }
4521 len = PyList_GET_SIZE(tmp);
4522 names = asdl_seq_new(len, arena);
4523 if (names == NULL) goto failed;
4524 for (i = 0; i < len; i++) {
4525 alias_ty value;
4526 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4527 if (res != 0) goto failed;
4528 asdl_seq_SET(names, i, value);
4529 }
4530 Py_XDECREF(tmp);
4531 tmp = NULL;
4532 } else {
4533 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4534 return 1;
4535 }
4536 if (PyObject_HasAttrString(obj, "level")) {
4537 int res;
4538 tmp = PyObject_GetAttrString(obj, "level");
4539 if (tmp == NULL) goto failed;
4540 res = obj2ast_int(tmp, &level, arena);
4541 if (res != 0) goto failed;
4542 Py_XDECREF(tmp);
4543 tmp = NULL;
4544 } else {
4545 level = 0;
4546 }
4547 *out = ImportFrom(module, names, level, lineno, col_offset,
4548 arena);
4549 if (*out == NULL) goto failed;
4550 return 0;
4551 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004552 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4553 if (isinstance == -1) {
4554 return 1;
4555 }
4556 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004557 asdl_seq* names;
4558
4559 if (PyObject_HasAttrString(obj, "names")) {
4560 int res;
4561 Py_ssize_t len;
4562 Py_ssize_t i;
4563 tmp = PyObject_GetAttrString(obj, "names");
4564 if (tmp == NULL) goto failed;
4565 if (!PyList_Check(tmp)) {
4566 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4567 goto failed;
4568 }
4569 len = PyList_GET_SIZE(tmp);
4570 names = asdl_seq_new(len, arena);
4571 if (names == NULL) goto failed;
4572 for (i = 0; i < len; i++) {
4573 identifier value;
4574 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4575 if (res != 0) goto failed;
4576 asdl_seq_SET(names, i, value);
4577 }
4578 Py_XDECREF(tmp);
4579 tmp = NULL;
4580 } else {
4581 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4582 return 1;
4583 }
4584 *out = Global(names, lineno, col_offset, arena);
4585 if (*out == NULL) goto failed;
4586 return 0;
4587 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004588 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4589 if (isinstance == -1) {
4590 return 1;
4591 }
4592 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004593 asdl_seq* names;
4594
4595 if (PyObject_HasAttrString(obj, "names")) {
4596 int res;
4597 Py_ssize_t len;
4598 Py_ssize_t i;
4599 tmp = PyObject_GetAttrString(obj, "names");
4600 if (tmp == NULL) goto failed;
4601 if (!PyList_Check(tmp)) {
4602 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4603 goto failed;
4604 }
4605 len = PyList_GET_SIZE(tmp);
4606 names = asdl_seq_new(len, arena);
4607 if (names == NULL) goto failed;
4608 for (i = 0; i < len; i++) {
4609 identifier value;
4610 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4611 if (res != 0) goto failed;
4612 asdl_seq_SET(names, i, value);
4613 }
4614 Py_XDECREF(tmp);
4615 tmp = NULL;
4616 } else {
4617 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4618 return 1;
4619 }
4620 *out = Nonlocal(names, lineno, col_offset, arena);
4621 if (*out == NULL) goto failed;
4622 return 0;
4623 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004624 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4625 if (isinstance == -1) {
4626 return 1;
4627 }
4628 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004629 expr_ty value;
4630
4631 if (PyObject_HasAttrString(obj, "value")) {
4632 int res;
4633 tmp = PyObject_GetAttrString(obj, "value");
4634 if (tmp == NULL) goto failed;
4635 res = obj2ast_expr(tmp, &value, arena);
4636 if (res != 0) goto failed;
4637 Py_XDECREF(tmp);
4638 tmp = NULL;
4639 } else {
4640 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4641 return 1;
4642 }
4643 *out = Expr(value, lineno, col_offset, arena);
4644 if (*out == NULL) goto failed;
4645 return 0;
4646 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004647 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4648 if (isinstance == -1) {
4649 return 1;
4650 }
4651 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004652
4653 *out = Pass(lineno, col_offset, arena);
4654 if (*out == NULL) goto failed;
4655 return 0;
4656 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004657 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4658 if (isinstance == -1) {
4659 return 1;
4660 }
4661 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004662
4663 *out = Break(lineno, col_offset, arena);
4664 if (*out == NULL) goto failed;
4665 return 0;
4666 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004667 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4668 if (isinstance == -1) {
4669 return 1;
4670 }
4671 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004672
4673 *out = Continue(lineno, col_offset, arena);
4674 if (*out == NULL) goto failed;
4675 return 0;
4676 }
4677
Benjamin Peterson5b066812010-11-20 01:38:49 +00004678 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4679 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004680 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004681 return 1;
4682}
4683
4684int
4685obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4686{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004687 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004688
Benjamin Petersond8f65972010-11-20 04:31:07 +00004689 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004690 int lineno;
4691 int col_offset;
4692
4693 if (obj == Py_None) {
4694 *out = NULL;
4695 return 0;
4696 }
4697 if (PyObject_HasAttrString(obj, "lineno")) {
4698 int res;
4699 tmp = PyObject_GetAttrString(obj, "lineno");
4700 if (tmp == NULL) goto failed;
4701 res = obj2ast_int(tmp, &lineno, arena);
4702 if (res != 0) goto failed;
4703 Py_XDECREF(tmp);
4704 tmp = NULL;
4705 } else {
4706 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4707 return 1;
4708 }
4709 if (PyObject_HasAttrString(obj, "col_offset")) {
4710 int res;
4711 tmp = PyObject_GetAttrString(obj, "col_offset");
4712 if (tmp == NULL) goto failed;
4713 res = obj2ast_int(tmp, &col_offset, arena);
4714 if (res != 0) goto failed;
4715 Py_XDECREF(tmp);
4716 tmp = NULL;
4717 } else {
4718 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4719 return 1;
4720 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004721 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4722 if (isinstance == -1) {
4723 return 1;
4724 }
4725 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004726 boolop_ty op;
4727 asdl_seq* values;
4728
4729 if (PyObject_HasAttrString(obj, "op")) {
4730 int res;
4731 tmp = PyObject_GetAttrString(obj, "op");
4732 if (tmp == NULL) goto failed;
4733 res = obj2ast_boolop(tmp, &op, arena);
4734 if (res != 0) goto failed;
4735 Py_XDECREF(tmp);
4736 tmp = NULL;
4737 } else {
4738 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4739 return 1;
4740 }
4741 if (PyObject_HasAttrString(obj, "values")) {
4742 int res;
4743 Py_ssize_t len;
4744 Py_ssize_t i;
4745 tmp = PyObject_GetAttrString(obj, "values");
4746 if (tmp == NULL) goto failed;
4747 if (!PyList_Check(tmp)) {
4748 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4749 goto failed;
4750 }
4751 len = PyList_GET_SIZE(tmp);
4752 values = asdl_seq_new(len, arena);
4753 if (values == NULL) goto failed;
4754 for (i = 0; i < len; i++) {
4755 expr_ty value;
4756 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4757 if (res != 0) goto failed;
4758 asdl_seq_SET(values, i, value);
4759 }
4760 Py_XDECREF(tmp);
4761 tmp = NULL;
4762 } else {
4763 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4764 return 1;
4765 }
4766 *out = BoolOp(op, values, lineno, col_offset, arena);
4767 if (*out == NULL) goto failed;
4768 return 0;
4769 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004770 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4771 if (isinstance == -1) {
4772 return 1;
4773 }
4774 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004775 expr_ty left;
4776 operator_ty op;
4777 expr_ty right;
4778
4779 if (PyObject_HasAttrString(obj, "left")) {
4780 int res;
4781 tmp = PyObject_GetAttrString(obj, "left");
4782 if (tmp == NULL) goto failed;
4783 res = obj2ast_expr(tmp, &left, arena);
4784 if (res != 0) goto failed;
4785 Py_XDECREF(tmp);
4786 tmp = NULL;
4787 } else {
4788 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4789 return 1;
4790 }
4791 if (PyObject_HasAttrString(obj, "op")) {
4792 int res;
4793 tmp = PyObject_GetAttrString(obj, "op");
4794 if (tmp == NULL) goto failed;
4795 res = obj2ast_operator(tmp, &op, arena);
4796 if (res != 0) goto failed;
4797 Py_XDECREF(tmp);
4798 tmp = NULL;
4799 } else {
4800 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4801 return 1;
4802 }
4803 if (PyObject_HasAttrString(obj, "right")) {
4804 int res;
4805 tmp = PyObject_GetAttrString(obj, "right");
4806 if (tmp == NULL) goto failed;
4807 res = obj2ast_expr(tmp, &right, arena);
4808 if (res != 0) goto failed;
4809 Py_XDECREF(tmp);
4810 tmp = NULL;
4811 } else {
4812 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4813 return 1;
4814 }
4815 *out = BinOp(left, op, right, lineno, col_offset, arena);
4816 if (*out == NULL) goto failed;
4817 return 0;
4818 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004819 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4820 if (isinstance == -1) {
4821 return 1;
4822 }
4823 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004824 unaryop_ty op;
4825 expr_ty operand;
4826
4827 if (PyObject_HasAttrString(obj, "op")) {
4828 int res;
4829 tmp = PyObject_GetAttrString(obj, "op");
4830 if (tmp == NULL) goto failed;
4831 res = obj2ast_unaryop(tmp, &op, arena);
4832 if (res != 0) goto failed;
4833 Py_XDECREF(tmp);
4834 tmp = NULL;
4835 } else {
4836 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4837 return 1;
4838 }
4839 if (PyObject_HasAttrString(obj, "operand")) {
4840 int res;
4841 tmp = PyObject_GetAttrString(obj, "operand");
4842 if (tmp == NULL) goto failed;
4843 res = obj2ast_expr(tmp, &operand, arena);
4844 if (res != 0) goto failed;
4845 Py_XDECREF(tmp);
4846 tmp = NULL;
4847 } else {
4848 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4849 return 1;
4850 }
4851 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4852 if (*out == NULL) goto failed;
4853 return 0;
4854 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004855 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4856 if (isinstance == -1) {
4857 return 1;
4858 }
4859 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004860 arguments_ty args;
4861 expr_ty body;
4862
4863 if (PyObject_HasAttrString(obj, "args")) {
4864 int res;
4865 tmp = PyObject_GetAttrString(obj, "args");
4866 if (tmp == NULL) goto failed;
4867 res = obj2ast_arguments(tmp, &args, arena);
4868 if (res != 0) goto failed;
4869 Py_XDECREF(tmp);
4870 tmp = NULL;
4871 } else {
4872 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4873 return 1;
4874 }
4875 if (PyObject_HasAttrString(obj, "body")) {
4876 int res;
4877 tmp = PyObject_GetAttrString(obj, "body");
4878 if (tmp == NULL) goto failed;
4879 res = obj2ast_expr(tmp, &body, arena);
4880 if (res != 0) goto failed;
4881 Py_XDECREF(tmp);
4882 tmp = NULL;
4883 } else {
4884 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4885 return 1;
4886 }
4887 *out = Lambda(args, body, lineno, col_offset, arena);
4888 if (*out == NULL) goto failed;
4889 return 0;
4890 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004891 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4892 if (isinstance == -1) {
4893 return 1;
4894 }
4895 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004896 expr_ty test;
4897 expr_ty body;
4898 expr_ty orelse;
4899
4900 if (PyObject_HasAttrString(obj, "test")) {
4901 int res;
4902 tmp = PyObject_GetAttrString(obj, "test");
4903 if (tmp == NULL) goto failed;
4904 res = obj2ast_expr(tmp, &test, arena);
4905 if (res != 0) goto failed;
4906 Py_XDECREF(tmp);
4907 tmp = NULL;
4908 } else {
4909 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4910 return 1;
4911 }
4912 if (PyObject_HasAttrString(obj, "body")) {
4913 int res;
4914 tmp = PyObject_GetAttrString(obj, "body");
4915 if (tmp == NULL) goto failed;
4916 res = obj2ast_expr(tmp, &body, arena);
4917 if (res != 0) goto failed;
4918 Py_XDECREF(tmp);
4919 tmp = NULL;
4920 } else {
4921 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4922 return 1;
4923 }
4924 if (PyObject_HasAttrString(obj, "orelse")) {
4925 int res;
4926 tmp = PyObject_GetAttrString(obj, "orelse");
4927 if (tmp == NULL) goto failed;
4928 res = obj2ast_expr(tmp, &orelse, arena);
4929 if (res != 0) goto failed;
4930 Py_XDECREF(tmp);
4931 tmp = NULL;
4932 } else {
4933 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4934 return 1;
4935 }
4936 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4937 if (*out == NULL) goto failed;
4938 return 0;
4939 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004940 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4941 if (isinstance == -1) {
4942 return 1;
4943 }
4944 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004945 asdl_seq* keys;
4946 asdl_seq* values;
4947
4948 if (PyObject_HasAttrString(obj, "keys")) {
4949 int res;
4950 Py_ssize_t len;
4951 Py_ssize_t i;
4952 tmp = PyObject_GetAttrString(obj, "keys");
4953 if (tmp == NULL) goto failed;
4954 if (!PyList_Check(tmp)) {
4955 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4956 goto failed;
4957 }
4958 len = PyList_GET_SIZE(tmp);
4959 keys = asdl_seq_new(len, arena);
4960 if (keys == NULL) goto failed;
4961 for (i = 0; i < len; i++) {
4962 expr_ty value;
4963 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4964 if (res != 0) goto failed;
4965 asdl_seq_SET(keys, i, value);
4966 }
4967 Py_XDECREF(tmp);
4968 tmp = NULL;
4969 } else {
4970 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4971 return 1;
4972 }
4973 if (PyObject_HasAttrString(obj, "values")) {
4974 int res;
4975 Py_ssize_t len;
4976 Py_ssize_t i;
4977 tmp = PyObject_GetAttrString(obj, "values");
4978 if (tmp == NULL) goto failed;
4979 if (!PyList_Check(tmp)) {
4980 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4981 goto failed;
4982 }
4983 len = PyList_GET_SIZE(tmp);
4984 values = asdl_seq_new(len, arena);
4985 if (values == 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(values, i, value);
4991 }
4992 Py_XDECREF(tmp);
4993 tmp = NULL;
4994 } else {
4995 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4996 return 1;
4997 }
4998 *out = Dict(keys, values, lineno, col_offset, arena);
4999 if (*out == NULL) goto failed;
5000 return 0;
5001 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005002 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5003 if (isinstance == -1) {
5004 return 1;
5005 }
5006 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005007 asdl_seq* elts;
5008
5009 if (PyObject_HasAttrString(obj, "elts")) {
5010 int res;
5011 Py_ssize_t len;
5012 Py_ssize_t i;
5013 tmp = PyObject_GetAttrString(obj, "elts");
5014 if (tmp == NULL) goto failed;
5015 if (!PyList_Check(tmp)) {
5016 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5017 goto failed;
5018 }
5019 len = PyList_GET_SIZE(tmp);
5020 elts = asdl_seq_new(len, arena);
5021 if (elts == NULL) goto failed;
5022 for (i = 0; i < len; i++) {
5023 expr_ty value;
5024 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5025 if (res != 0) goto failed;
5026 asdl_seq_SET(elts, i, value);
5027 }
5028 Py_XDECREF(tmp);
5029 tmp = NULL;
5030 } else {
5031 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5032 return 1;
5033 }
5034 *out = Set(elts, lineno, col_offset, arena);
5035 if (*out == NULL) goto failed;
5036 return 0;
5037 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005038 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5039 if (isinstance == -1) {
5040 return 1;
5041 }
5042 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005043 expr_ty elt;
5044 asdl_seq* generators;
5045
5046 if (PyObject_HasAttrString(obj, "elt")) {
5047 int res;
5048 tmp = PyObject_GetAttrString(obj, "elt");
5049 if (tmp == NULL) goto failed;
5050 res = obj2ast_expr(tmp, &elt, arena);
5051 if (res != 0) goto failed;
5052 Py_XDECREF(tmp);
5053 tmp = NULL;
5054 } else {
5055 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5056 return 1;
5057 }
5058 if (PyObject_HasAttrString(obj, "generators")) {
5059 int res;
5060 Py_ssize_t len;
5061 Py_ssize_t i;
5062 tmp = PyObject_GetAttrString(obj, "generators");
5063 if (tmp == NULL) goto failed;
5064 if (!PyList_Check(tmp)) {
5065 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5066 goto failed;
5067 }
5068 len = PyList_GET_SIZE(tmp);
5069 generators = asdl_seq_new(len, arena);
5070 if (generators == NULL) goto failed;
5071 for (i = 0; i < len; i++) {
5072 comprehension_ty value;
5073 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5074 if (res != 0) goto failed;
5075 asdl_seq_SET(generators, i, value);
5076 }
5077 Py_XDECREF(tmp);
5078 tmp = NULL;
5079 } else {
5080 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5081 return 1;
5082 }
5083 *out = ListComp(elt, generators, lineno, col_offset, arena);
5084 if (*out == NULL) goto failed;
5085 return 0;
5086 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005087 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5088 if (isinstance == -1) {
5089 return 1;
5090 }
5091 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005092 expr_ty elt;
5093 asdl_seq* generators;
5094
5095 if (PyObject_HasAttrString(obj, "elt")) {
5096 int res;
5097 tmp = PyObject_GetAttrString(obj, "elt");
5098 if (tmp == NULL) goto failed;
5099 res = obj2ast_expr(tmp, &elt, arena);
5100 if (res != 0) goto failed;
5101 Py_XDECREF(tmp);
5102 tmp = NULL;
5103 } else {
5104 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5105 return 1;
5106 }
5107 if (PyObject_HasAttrString(obj, "generators")) {
5108 int res;
5109 Py_ssize_t len;
5110 Py_ssize_t i;
5111 tmp = PyObject_GetAttrString(obj, "generators");
5112 if (tmp == NULL) goto failed;
5113 if (!PyList_Check(tmp)) {
5114 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5115 goto failed;
5116 }
5117 len = PyList_GET_SIZE(tmp);
5118 generators = asdl_seq_new(len, arena);
5119 if (generators == NULL) goto failed;
5120 for (i = 0; i < len; i++) {
5121 comprehension_ty value;
5122 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5123 if (res != 0) goto failed;
5124 asdl_seq_SET(generators, i, value);
5125 }
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5130 return 1;
5131 }
5132 *out = SetComp(elt, generators, lineno, col_offset, arena);
5133 if (*out == NULL) goto failed;
5134 return 0;
5135 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005136 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5137 if (isinstance == -1) {
5138 return 1;
5139 }
5140 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005141 expr_ty key;
5142 expr_ty value;
5143 asdl_seq* generators;
5144
5145 if (PyObject_HasAttrString(obj, "key")) {
5146 int res;
5147 tmp = PyObject_GetAttrString(obj, "key");
5148 if (tmp == NULL) goto failed;
5149 res = obj2ast_expr(tmp, &key, arena);
5150 if (res != 0) goto failed;
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "value")) {
5158 int res;
5159 tmp = PyObject_GetAttrString(obj, "value");
5160 if (tmp == NULL) goto failed;
5161 res = obj2ast_expr(tmp, &value, arena);
5162 if (res != 0) goto failed;
5163 Py_XDECREF(tmp);
5164 tmp = NULL;
5165 } else {
5166 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5167 return 1;
5168 }
5169 if (PyObject_HasAttrString(obj, "generators")) {
5170 int res;
5171 Py_ssize_t len;
5172 Py_ssize_t i;
5173 tmp = PyObject_GetAttrString(obj, "generators");
5174 if (tmp == NULL) goto failed;
5175 if (!PyList_Check(tmp)) {
5176 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5177 goto failed;
5178 }
5179 len = PyList_GET_SIZE(tmp);
5180 generators = asdl_seq_new(len, arena);
5181 if (generators == NULL) goto failed;
5182 for (i = 0; i < len; i++) {
5183 comprehension_ty value;
5184 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5185 if (res != 0) goto failed;
5186 asdl_seq_SET(generators, i, value);
5187 }
5188 Py_XDECREF(tmp);
5189 tmp = NULL;
5190 } else {
5191 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5192 return 1;
5193 }
5194 *out = DictComp(key, value, generators, lineno, col_offset,
5195 arena);
5196 if (*out == NULL) goto failed;
5197 return 0;
5198 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005199 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5200 if (isinstance == -1) {
5201 return 1;
5202 }
5203 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005204 expr_ty elt;
5205 asdl_seq* generators;
5206
5207 if (PyObject_HasAttrString(obj, "elt")) {
5208 int res;
5209 tmp = PyObject_GetAttrString(obj, "elt");
5210 if (tmp == NULL) goto failed;
5211 res = obj2ast_expr(tmp, &elt, arena);
5212 if (res != 0) goto failed;
5213 Py_XDECREF(tmp);
5214 tmp = NULL;
5215 } else {
5216 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5217 return 1;
5218 }
5219 if (PyObject_HasAttrString(obj, "generators")) {
5220 int res;
5221 Py_ssize_t len;
5222 Py_ssize_t i;
5223 tmp = PyObject_GetAttrString(obj, "generators");
5224 if (tmp == NULL) goto failed;
5225 if (!PyList_Check(tmp)) {
5226 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5227 goto failed;
5228 }
5229 len = PyList_GET_SIZE(tmp);
5230 generators = asdl_seq_new(len, arena);
5231 if (generators == NULL) goto failed;
5232 for (i = 0; i < len; i++) {
5233 comprehension_ty value;
5234 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5235 if (res != 0) goto failed;
5236 asdl_seq_SET(generators, i, value);
5237 }
5238 Py_XDECREF(tmp);
5239 tmp = NULL;
5240 } else {
5241 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5242 return 1;
5243 }
5244 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5245 if (*out == NULL) goto failed;
5246 return 0;
5247 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005248 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5249 if (isinstance == -1) {
5250 return 1;
5251 }
5252 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005253 expr_ty value;
5254
5255 if (PyObject_HasAttrString(obj, "value")) {
5256 int res;
5257 tmp = PyObject_GetAttrString(obj, "value");
5258 if (tmp == NULL) goto failed;
5259 res = obj2ast_expr(tmp, &value, arena);
5260 if (res != 0) goto failed;
5261 Py_XDECREF(tmp);
5262 tmp = NULL;
5263 } else {
5264 value = NULL;
5265 }
5266 *out = Yield(value, lineno, col_offset, arena);
5267 if (*out == NULL) goto failed;
5268 return 0;
5269 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005270 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5271 if (isinstance == -1) {
5272 return 1;
5273 }
5274 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005275 expr_ty left;
5276 asdl_int_seq* ops;
5277 asdl_seq* comparators;
5278
5279 if (PyObject_HasAttrString(obj, "left")) {
5280 int res;
5281 tmp = PyObject_GetAttrString(obj, "left");
5282 if (tmp == NULL) goto failed;
5283 res = obj2ast_expr(tmp, &left, arena);
5284 if (res != 0) goto failed;
5285 Py_XDECREF(tmp);
5286 tmp = NULL;
5287 } else {
5288 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5289 return 1;
5290 }
5291 if (PyObject_HasAttrString(obj, "ops")) {
5292 int res;
5293 Py_ssize_t len;
5294 Py_ssize_t i;
5295 tmp = PyObject_GetAttrString(obj, "ops");
5296 if (tmp == NULL) goto failed;
5297 if (!PyList_Check(tmp)) {
5298 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5299 goto failed;
5300 }
5301 len = PyList_GET_SIZE(tmp);
5302 ops = asdl_int_seq_new(len, arena);
5303 if (ops == NULL) goto failed;
5304 for (i = 0; i < len; i++) {
5305 cmpop_ty value;
5306 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5307 if (res != 0) goto failed;
5308 asdl_seq_SET(ops, i, value);
5309 }
5310 Py_XDECREF(tmp);
5311 tmp = NULL;
5312 } else {
5313 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5314 return 1;
5315 }
5316 if (PyObject_HasAttrString(obj, "comparators")) {
5317 int res;
5318 Py_ssize_t len;
5319 Py_ssize_t i;
5320 tmp = PyObject_GetAttrString(obj, "comparators");
5321 if (tmp == NULL) goto failed;
5322 if (!PyList_Check(tmp)) {
5323 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5324 goto failed;
5325 }
5326 len = PyList_GET_SIZE(tmp);
5327 comparators = asdl_seq_new(len, arena);
5328 if (comparators == NULL) goto failed;
5329 for (i = 0; i < len; i++) {
5330 expr_ty value;
5331 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5332 if (res != 0) goto failed;
5333 asdl_seq_SET(comparators, i, value);
5334 }
5335 Py_XDECREF(tmp);
5336 tmp = NULL;
5337 } else {
5338 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5339 return 1;
5340 }
5341 *out = Compare(left, ops, comparators, lineno, col_offset,
5342 arena);
5343 if (*out == NULL) goto failed;
5344 return 0;
5345 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005346 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5347 if (isinstance == -1) {
5348 return 1;
5349 }
5350 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005351 expr_ty func;
5352 asdl_seq* args;
5353 asdl_seq* keywords;
5354 expr_ty starargs;
5355 expr_ty kwargs;
5356
5357 if (PyObject_HasAttrString(obj, "func")) {
5358 int res;
5359 tmp = PyObject_GetAttrString(obj, "func");
5360 if (tmp == NULL) goto failed;
5361 res = obj2ast_expr(tmp, &func, arena);
5362 if (res != 0) goto failed;
5363 Py_XDECREF(tmp);
5364 tmp = NULL;
5365 } else {
5366 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5367 return 1;
5368 }
5369 if (PyObject_HasAttrString(obj, "args")) {
5370 int res;
5371 Py_ssize_t len;
5372 Py_ssize_t i;
5373 tmp = PyObject_GetAttrString(obj, "args");
5374 if (tmp == NULL) goto failed;
5375 if (!PyList_Check(tmp)) {
5376 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5377 goto failed;
5378 }
5379 len = PyList_GET_SIZE(tmp);
5380 args = asdl_seq_new(len, arena);
5381 if (args == NULL) goto failed;
5382 for (i = 0; i < len; i++) {
5383 expr_ty value;
5384 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5385 if (res != 0) goto failed;
5386 asdl_seq_SET(args, i, value);
5387 }
5388 Py_XDECREF(tmp);
5389 tmp = NULL;
5390 } else {
5391 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5392 return 1;
5393 }
5394 if (PyObject_HasAttrString(obj, "keywords")) {
5395 int res;
5396 Py_ssize_t len;
5397 Py_ssize_t i;
5398 tmp = PyObject_GetAttrString(obj, "keywords");
5399 if (tmp == NULL) goto failed;
5400 if (!PyList_Check(tmp)) {
5401 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5402 goto failed;
5403 }
5404 len = PyList_GET_SIZE(tmp);
5405 keywords = asdl_seq_new(len, arena);
5406 if (keywords == NULL) goto failed;
5407 for (i = 0; i < len; i++) {
5408 keyword_ty value;
5409 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5410 if (res != 0) goto failed;
5411 asdl_seq_SET(keywords, i, value);
5412 }
5413 Py_XDECREF(tmp);
5414 tmp = NULL;
5415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5417 return 1;
5418 }
5419 if (PyObject_HasAttrString(obj, "starargs")) {
5420 int res;
5421 tmp = PyObject_GetAttrString(obj, "starargs");
5422 if (tmp == NULL) goto failed;
5423 res = obj2ast_expr(tmp, &starargs, arena);
5424 if (res != 0) goto failed;
5425 Py_XDECREF(tmp);
5426 tmp = NULL;
5427 } else {
5428 starargs = NULL;
5429 }
5430 if (PyObject_HasAttrString(obj, "kwargs")) {
5431 int res;
5432 tmp = PyObject_GetAttrString(obj, "kwargs");
5433 if (tmp == NULL) goto failed;
5434 res = obj2ast_expr(tmp, &kwargs, arena);
5435 if (res != 0) goto failed;
5436 Py_XDECREF(tmp);
5437 tmp = NULL;
5438 } else {
5439 kwargs = NULL;
5440 }
5441 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5442 col_offset, arena);
5443 if (*out == NULL) goto failed;
5444 return 0;
5445 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005446 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5447 if (isinstance == -1) {
5448 return 1;
5449 }
5450 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005451 object n;
5452
5453 if (PyObject_HasAttrString(obj, "n")) {
5454 int res;
5455 tmp = PyObject_GetAttrString(obj, "n");
5456 if (tmp == NULL) goto failed;
5457 res = obj2ast_object(tmp, &n, arena);
5458 if (res != 0) goto failed;
5459 Py_XDECREF(tmp);
5460 tmp = NULL;
5461 } else {
5462 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5463 return 1;
5464 }
5465 *out = Num(n, lineno, col_offset, arena);
5466 if (*out == NULL) goto failed;
5467 return 0;
5468 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005469 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5470 if (isinstance == -1) {
5471 return 1;
5472 }
5473 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005474 string s;
5475
5476 if (PyObject_HasAttrString(obj, "s")) {
5477 int res;
5478 tmp = PyObject_GetAttrString(obj, "s");
5479 if (tmp == NULL) goto failed;
5480 res = obj2ast_string(tmp, &s, arena);
5481 if (res != 0) goto failed;
5482 Py_XDECREF(tmp);
5483 tmp = NULL;
5484 } else {
5485 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5486 return 1;
5487 }
5488 *out = Str(s, lineno, col_offset, arena);
5489 if (*out == NULL) goto failed;
5490 return 0;
5491 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005492 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5493 if (isinstance == -1) {
5494 return 1;
5495 }
5496 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005497 string s;
5498
5499 if (PyObject_HasAttrString(obj, "s")) {
5500 int res;
5501 tmp = PyObject_GetAttrString(obj, "s");
5502 if (tmp == NULL) goto failed;
5503 res = obj2ast_string(tmp, &s, arena);
5504 if (res != 0) goto failed;
5505 Py_XDECREF(tmp);
5506 tmp = NULL;
5507 } else {
5508 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5509 return 1;
5510 }
5511 *out = Bytes(s, lineno, col_offset, arena);
5512 if (*out == NULL) goto failed;
5513 return 0;
5514 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005515 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5516 if (isinstance == -1) {
5517 return 1;
5518 }
5519 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005520
5521 *out = Ellipsis(lineno, col_offset, arena);
5522 if (*out == NULL) goto failed;
5523 return 0;
5524 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005525 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5526 if (isinstance == -1) {
5527 return 1;
5528 }
5529 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005530 expr_ty value;
5531 identifier attr;
5532 expr_context_ty ctx;
5533
5534 if (PyObject_HasAttrString(obj, "value")) {
5535 int res;
5536 tmp = PyObject_GetAttrString(obj, "value");
5537 if (tmp == NULL) goto failed;
5538 res = obj2ast_expr(tmp, &value, arena);
5539 if (res != 0) goto failed;
5540 Py_XDECREF(tmp);
5541 tmp = NULL;
5542 } else {
5543 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5544 return 1;
5545 }
5546 if (PyObject_HasAttrString(obj, "attr")) {
5547 int res;
5548 tmp = PyObject_GetAttrString(obj, "attr");
5549 if (tmp == NULL) goto failed;
5550 res = obj2ast_identifier(tmp, &attr, arena);
5551 if (res != 0) goto failed;
5552 Py_XDECREF(tmp);
5553 tmp = NULL;
5554 } else {
5555 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5556 return 1;
5557 }
5558 if (PyObject_HasAttrString(obj, "ctx")) {
5559 int res;
5560 tmp = PyObject_GetAttrString(obj, "ctx");
5561 if (tmp == NULL) goto failed;
5562 res = obj2ast_expr_context(tmp, &ctx, arena);
5563 if (res != 0) goto failed;
5564 Py_XDECREF(tmp);
5565 tmp = NULL;
5566 } else {
5567 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5568 return 1;
5569 }
5570 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5571 if (*out == NULL) goto failed;
5572 return 0;
5573 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005574 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5575 if (isinstance == -1) {
5576 return 1;
5577 }
5578 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005579 expr_ty value;
5580 slice_ty slice;
5581 expr_context_ty ctx;
5582
5583 if (PyObject_HasAttrString(obj, "value")) {
5584 int res;
5585 tmp = PyObject_GetAttrString(obj, "value");
5586 if (tmp == NULL) goto failed;
5587 res = obj2ast_expr(tmp, &value, arena);
5588 if (res != 0) goto failed;
5589 Py_XDECREF(tmp);
5590 tmp = NULL;
5591 } else {
5592 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5593 return 1;
5594 }
5595 if (PyObject_HasAttrString(obj, "slice")) {
5596 int res;
5597 tmp = PyObject_GetAttrString(obj, "slice");
5598 if (tmp == NULL) goto failed;
5599 res = obj2ast_slice(tmp, &slice, arena);
5600 if (res != 0) goto failed;
5601 Py_XDECREF(tmp);
5602 tmp = NULL;
5603 } else {
5604 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5605 return 1;
5606 }
5607 if (PyObject_HasAttrString(obj, "ctx")) {
5608 int res;
5609 tmp = PyObject_GetAttrString(obj, "ctx");
5610 if (tmp == NULL) goto failed;
5611 res = obj2ast_expr_context(tmp, &ctx, arena);
5612 if (res != 0) goto failed;
5613 Py_XDECREF(tmp);
5614 tmp = NULL;
5615 } else {
5616 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5617 return 1;
5618 }
5619 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5620 if (*out == NULL) goto failed;
5621 return 0;
5622 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005623 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5624 if (isinstance == -1) {
5625 return 1;
5626 }
5627 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005628 expr_ty value;
5629 expr_context_ty ctx;
5630
5631 if (PyObject_HasAttrString(obj, "value")) {
5632 int res;
5633 tmp = PyObject_GetAttrString(obj, "value");
5634 if (tmp == NULL) goto failed;
5635 res = obj2ast_expr(tmp, &value, arena);
5636 if (res != 0) goto failed;
5637 Py_XDECREF(tmp);
5638 tmp = NULL;
5639 } else {
5640 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5641 return 1;
5642 }
5643 if (PyObject_HasAttrString(obj, "ctx")) {
5644 int res;
5645 tmp = PyObject_GetAttrString(obj, "ctx");
5646 if (tmp == NULL) goto failed;
5647 res = obj2ast_expr_context(tmp, &ctx, arena);
5648 if (res != 0) goto failed;
5649 Py_XDECREF(tmp);
5650 tmp = NULL;
5651 } else {
5652 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5653 return 1;
5654 }
5655 *out = Starred(value, ctx, lineno, col_offset, arena);
5656 if (*out == NULL) goto failed;
5657 return 0;
5658 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005659 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5660 if (isinstance == -1) {
5661 return 1;
5662 }
5663 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005664 identifier id;
5665 expr_context_ty ctx;
5666
5667 if (PyObject_HasAttrString(obj, "id")) {
5668 int res;
5669 tmp = PyObject_GetAttrString(obj, "id");
5670 if (tmp == NULL) goto failed;
5671 res = obj2ast_identifier(tmp, &id, arena);
5672 if (res != 0) goto failed;
5673 Py_XDECREF(tmp);
5674 tmp = NULL;
5675 } else {
5676 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5677 return 1;
5678 }
5679 if (PyObject_HasAttrString(obj, "ctx")) {
5680 int res;
5681 tmp = PyObject_GetAttrString(obj, "ctx");
5682 if (tmp == NULL) goto failed;
5683 res = obj2ast_expr_context(tmp, &ctx, arena);
5684 if (res != 0) goto failed;
5685 Py_XDECREF(tmp);
5686 tmp = NULL;
5687 } else {
5688 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5689 return 1;
5690 }
5691 *out = Name(id, ctx, lineno, col_offset, arena);
5692 if (*out == NULL) goto failed;
5693 return 0;
5694 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005695 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5696 if (isinstance == -1) {
5697 return 1;
5698 }
5699 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005700 asdl_seq* elts;
5701 expr_context_ty ctx;
5702
5703 if (PyObject_HasAttrString(obj, "elts")) {
5704 int res;
5705 Py_ssize_t len;
5706 Py_ssize_t i;
5707 tmp = PyObject_GetAttrString(obj, "elts");
5708 if (tmp == NULL) goto failed;
5709 if (!PyList_Check(tmp)) {
5710 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5711 goto failed;
5712 }
5713 len = PyList_GET_SIZE(tmp);
5714 elts = asdl_seq_new(len, arena);
5715 if (elts == NULL) goto failed;
5716 for (i = 0; i < len; i++) {
5717 expr_ty value;
5718 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5719 if (res != 0) goto failed;
5720 asdl_seq_SET(elts, i, value);
5721 }
5722 Py_XDECREF(tmp);
5723 tmp = NULL;
5724 } else {
5725 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5726 return 1;
5727 }
5728 if (PyObject_HasAttrString(obj, "ctx")) {
5729 int res;
5730 tmp = PyObject_GetAttrString(obj, "ctx");
5731 if (tmp == NULL) goto failed;
5732 res = obj2ast_expr_context(tmp, &ctx, arena);
5733 if (res != 0) goto failed;
5734 Py_XDECREF(tmp);
5735 tmp = NULL;
5736 } else {
5737 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5738 return 1;
5739 }
5740 *out = List(elts, ctx, lineno, col_offset, arena);
5741 if (*out == NULL) goto failed;
5742 return 0;
5743 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005744 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5745 if (isinstance == -1) {
5746 return 1;
5747 }
5748 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005749 asdl_seq* elts;
5750 expr_context_ty ctx;
5751
5752 if (PyObject_HasAttrString(obj, "elts")) {
5753 int res;
5754 Py_ssize_t len;
5755 Py_ssize_t i;
5756 tmp = PyObject_GetAttrString(obj, "elts");
5757 if (tmp == NULL) goto failed;
5758 if (!PyList_Check(tmp)) {
5759 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5760 goto failed;
5761 }
5762 len = PyList_GET_SIZE(tmp);
5763 elts = asdl_seq_new(len, arena);
5764 if (elts == NULL) goto failed;
5765 for (i = 0; i < len; i++) {
5766 expr_ty value;
5767 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5768 if (res != 0) goto failed;
5769 asdl_seq_SET(elts, i, value);
5770 }
5771 Py_XDECREF(tmp);
5772 tmp = NULL;
5773 } else {
5774 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5775 return 1;
5776 }
5777 if (PyObject_HasAttrString(obj, "ctx")) {
5778 int res;
5779 tmp = PyObject_GetAttrString(obj, "ctx");
5780 if (tmp == NULL) goto failed;
5781 res = obj2ast_expr_context(tmp, &ctx, arena);
5782 if (res != 0) goto failed;
5783 Py_XDECREF(tmp);
5784 tmp = NULL;
5785 } else {
5786 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5787 return 1;
5788 }
5789 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5790 if (*out == NULL) goto failed;
5791 return 0;
5792 }
5793
Benjamin Peterson5b066812010-11-20 01:38:49 +00005794 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5795 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005796 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005797 return 1;
5798}
5799
5800int
5801obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5802{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005803 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005804
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005805 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5806 if (isinstance == -1) {
5807 return 1;
5808 }
5809 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005810 *out = Load;
5811 return 0;
5812 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005813 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5814 if (isinstance == -1) {
5815 return 1;
5816 }
5817 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005818 *out = Store;
5819 return 0;
5820 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005821 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5822 if (isinstance == -1) {
5823 return 1;
5824 }
5825 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005826 *out = Del;
5827 return 0;
5828 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005829 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5830 if (isinstance == -1) {
5831 return 1;
5832 }
5833 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005834 *out = AugLoad;
5835 return 0;
5836 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005837 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5838 if (isinstance == -1) {
5839 return 1;
5840 }
5841 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005842 *out = AugStore;
5843 return 0;
5844 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005845 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5846 if (isinstance == -1) {
5847 return 1;
5848 }
5849 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005850 *out = Param;
5851 return 0;
5852 }
5853
Benjamin Peterson5b066812010-11-20 01:38:49 +00005854 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005855 return 1;
5856}
5857
5858int
5859obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5860{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005861 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005862
Benjamin Petersond8f65972010-11-20 04:31:07 +00005863 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005864
5865 if (obj == Py_None) {
5866 *out = NULL;
5867 return 0;
5868 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005869 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5870 if (isinstance == -1) {
5871 return 1;
5872 }
5873 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005874 expr_ty lower;
5875 expr_ty upper;
5876 expr_ty step;
5877
5878 if (PyObject_HasAttrString(obj, "lower")) {
5879 int res;
5880 tmp = PyObject_GetAttrString(obj, "lower");
5881 if (tmp == NULL) goto failed;
5882 res = obj2ast_expr(tmp, &lower, arena);
5883 if (res != 0) goto failed;
5884 Py_XDECREF(tmp);
5885 tmp = NULL;
5886 } else {
5887 lower = NULL;
5888 }
5889 if (PyObject_HasAttrString(obj, "upper")) {
5890 int res;
5891 tmp = PyObject_GetAttrString(obj, "upper");
5892 if (tmp == NULL) goto failed;
5893 res = obj2ast_expr(tmp, &upper, arena);
5894 if (res != 0) goto failed;
5895 Py_XDECREF(tmp);
5896 tmp = NULL;
5897 } else {
5898 upper = NULL;
5899 }
5900 if (PyObject_HasAttrString(obj, "step")) {
5901 int res;
5902 tmp = PyObject_GetAttrString(obj, "step");
5903 if (tmp == NULL) goto failed;
5904 res = obj2ast_expr(tmp, &step, arena);
5905 if (res != 0) goto failed;
5906 Py_XDECREF(tmp);
5907 tmp = NULL;
5908 } else {
5909 step = NULL;
5910 }
5911 *out = Slice(lower, upper, step, arena);
5912 if (*out == NULL) goto failed;
5913 return 0;
5914 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005915 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5916 if (isinstance == -1) {
5917 return 1;
5918 }
5919 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005920 asdl_seq* dims;
5921
5922 if (PyObject_HasAttrString(obj, "dims")) {
5923 int res;
5924 Py_ssize_t len;
5925 Py_ssize_t i;
5926 tmp = PyObject_GetAttrString(obj, "dims");
5927 if (tmp == NULL) goto failed;
5928 if (!PyList_Check(tmp)) {
5929 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5930 goto failed;
5931 }
5932 len = PyList_GET_SIZE(tmp);
5933 dims = asdl_seq_new(len, arena);
5934 if (dims == NULL) goto failed;
5935 for (i = 0; i < len; i++) {
5936 slice_ty value;
5937 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5938 if (res != 0) goto failed;
5939 asdl_seq_SET(dims, i, value);
5940 }
5941 Py_XDECREF(tmp);
5942 tmp = NULL;
5943 } else {
5944 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5945 return 1;
5946 }
5947 *out = ExtSlice(dims, arena);
5948 if (*out == NULL) goto failed;
5949 return 0;
5950 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005951 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5952 if (isinstance == -1) {
5953 return 1;
5954 }
5955 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005956 expr_ty value;
5957
5958 if (PyObject_HasAttrString(obj, "value")) {
5959 int res;
5960 tmp = PyObject_GetAttrString(obj, "value");
5961 if (tmp == NULL) goto failed;
5962 res = obj2ast_expr(tmp, &value, arena);
5963 if (res != 0) goto failed;
5964 Py_XDECREF(tmp);
5965 tmp = NULL;
5966 } else {
5967 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5968 return 1;
5969 }
5970 *out = Index(value, arena);
5971 if (*out == NULL) goto failed;
5972 return 0;
5973 }
5974
Benjamin Peterson5b066812010-11-20 01:38:49 +00005975 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
5976 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005977 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005978 return 1;
5979}
5980
5981int
5982obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5983{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005984 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005985
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005986 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5987 if (isinstance == -1) {
5988 return 1;
5989 }
5990 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005991 *out = And;
5992 return 0;
5993 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005994 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5995 if (isinstance == -1) {
5996 return 1;
5997 }
5998 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005999 *out = Or;
6000 return 0;
6001 }
6002
Benjamin Peterson5b066812010-11-20 01:38:49 +00006003 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006004 return 1;
6005}
6006
6007int
6008obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6009{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006010 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006011
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006012 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6013 if (isinstance == -1) {
6014 return 1;
6015 }
6016 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006017 *out = Add;
6018 return 0;
6019 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006020 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6021 if (isinstance == -1) {
6022 return 1;
6023 }
6024 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006025 *out = Sub;
6026 return 0;
6027 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006028 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6029 if (isinstance == -1) {
6030 return 1;
6031 }
6032 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006033 *out = Mult;
6034 return 0;
6035 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006036 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6037 if (isinstance == -1) {
6038 return 1;
6039 }
6040 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006041 *out = Div;
6042 return 0;
6043 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006044 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6045 if (isinstance == -1) {
6046 return 1;
6047 }
6048 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006049 *out = Mod;
6050 return 0;
6051 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006052 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6053 if (isinstance == -1) {
6054 return 1;
6055 }
6056 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006057 *out = Pow;
6058 return 0;
6059 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006060 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6061 if (isinstance == -1) {
6062 return 1;
6063 }
6064 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006065 *out = LShift;
6066 return 0;
6067 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006068 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6069 if (isinstance == -1) {
6070 return 1;
6071 }
6072 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006073 *out = RShift;
6074 return 0;
6075 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006076 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6077 if (isinstance == -1) {
6078 return 1;
6079 }
6080 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006081 *out = BitOr;
6082 return 0;
6083 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006084 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6085 if (isinstance == -1) {
6086 return 1;
6087 }
6088 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006089 *out = BitXor;
6090 return 0;
6091 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006092 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6093 if (isinstance == -1) {
6094 return 1;
6095 }
6096 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006097 *out = BitAnd;
6098 return 0;
6099 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006100 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6101 if (isinstance == -1) {
6102 return 1;
6103 }
6104 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006105 *out = FloorDiv;
6106 return 0;
6107 }
6108
Benjamin Peterson5b066812010-11-20 01:38:49 +00006109 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006110 return 1;
6111}
6112
6113int
6114obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6115{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006116 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006117
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006118 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6119 if (isinstance == -1) {
6120 return 1;
6121 }
6122 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006123 *out = Invert;
6124 return 0;
6125 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006126 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6127 if (isinstance == -1) {
6128 return 1;
6129 }
6130 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006131 *out = Not;
6132 return 0;
6133 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006134 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6135 if (isinstance == -1) {
6136 return 1;
6137 }
6138 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006139 *out = UAdd;
6140 return 0;
6141 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006142 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6143 if (isinstance == -1) {
6144 return 1;
6145 }
6146 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006147 *out = USub;
6148 return 0;
6149 }
6150
Benjamin Peterson5b066812010-11-20 01:38:49 +00006151 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006152 return 1;
6153}
6154
6155int
6156obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6157{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006158 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006159
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006160 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6161 if (isinstance == -1) {
6162 return 1;
6163 }
6164 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006165 *out = Eq;
6166 return 0;
6167 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006168 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6169 if (isinstance == -1) {
6170 return 1;
6171 }
6172 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006173 *out = NotEq;
6174 return 0;
6175 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006176 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6177 if (isinstance == -1) {
6178 return 1;
6179 }
6180 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006181 *out = Lt;
6182 return 0;
6183 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006184 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6185 if (isinstance == -1) {
6186 return 1;
6187 }
6188 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006189 *out = LtE;
6190 return 0;
6191 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006192 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6193 if (isinstance == -1) {
6194 return 1;
6195 }
6196 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006197 *out = Gt;
6198 return 0;
6199 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006200 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6201 if (isinstance == -1) {
6202 return 1;
6203 }
6204 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006205 *out = GtE;
6206 return 0;
6207 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006208 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6209 if (isinstance == -1) {
6210 return 1;
6211 }
6212 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006213 *out = Is;
6214 return 0;
6215 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006216 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6217 if (isinstance == -1) {
6218 return 1;
6219 }
6220 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006221 *out = IsNot;
6222 return 0;
6223 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006224 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6225 if (isinstance == -1) {
6226 return 1;
6227 }
6228 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006229 *out = In;
6230 return 0;
6231 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006232 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6233 if (isinstance == -1) {
6234 return 1;
6235 }
6236 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006237 *out = NotIn;
6238 return 0;
6239 }
6240
Benjamin Peterson5b066812010-11-20 01:38:49 +00006241 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006242 return 1;
6243}
6244
6245int
6246obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6247{
6248 PyObject* tmp = NULL;
6249 expr_ty target;
6250 expr_ty iter;
6251 asdl_seq* ifs;
6252
6253 if (PyObject_HasAttrString(obj, "target")) {
6254 int res;
6255 tmp = PyObject_GetAttrString(obj, "target");
6256 if (tmp == NULL) goto failed;
6257 res = obj2ast_expr(tmp, &target, arena);
6258 if (res != 0) goto failed;
6259 Py_XDECREF(tmp);
6260 tmp = NULL;
6261 } else {
6262 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6263 return 1;
6264 }
6265 if (PyObject_HasAttrString(obj, "iter")) {
6266 int res;
6267 tmp = PyObject_GetAttrString(obj, "iter");
6268 if (tmp == NULL) goto failed;
6269 res = obj2ast_expr(tmp, &iter, arena);
6270 if (res != 0) goto failed;
6271 Py_XDECREF(tmp);
6272 tmp = NULL;
6273 } else {
6274 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6275 return 1;
6276 }
6277 if (PyObject_HasAttrString(obj, "ifs")) {
6278 int res;
6279 Py_ssize_t len;
6280 Py_ssize_t i;
6281 tmp = PyObject_GetAttrString(obj, "ifs");
6282 if (tmp == NULL) goto failed;
6283 if (!PyList_Check(tmp)) {
6284 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6285 goto failed;
6286 }
6287 len = PyList_GET_SIZE(tmp);
6288 ifs = asdl_seq_new(len, arena);
6289 if (ifs == NULL) goto failed;
6290 for (i = 0; i < len; i++) {
6291 expr_ty value;
6292 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6293 if (res != 0) goto failed;
6294 asdl_seq_SET(ifs, i, value);
6295 }
6296 Py_XDECREF(tmp);
6297 tmp = NULL;
6298 } else {
6299 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6300 return 1;
6301 }
6302 *out = comprehension(target, iter, ifs, arena);
6303 return 0;
6304failed:
6305 Py_XDECREF(tmp);
6306 return 1;
6307}
6308
6309int
6310obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6311{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006312 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006313
Benjamin Petersond8f65972010-11-20 04:31:07 +00006314 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006315 int lineno;
6316 int col_offset;
6317
Neal Norwitzad74aa82008-03-31 05:14:30 +00006318 if (obj == Py_None) {
6319 *out = NULL;
6320 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006321 }
6322 if (PyObject_HasAttrString(obj, "lineno")) {
6323 int res;
6324 tmp = PyObject_GetAttrString(obj, "lineno");
6325 if (tmp == NULL) goto failed;
6326 res = obj2ast_int(tmp, &lineno, arena);
6327 if (res != 0) goto failed;
6328 Py_XDECREF(tmp);
6329 tmp = NULL;
6330 } else {
6331 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6332 return 1;
6333 }
6334 if (PyObject_HasAttrString(obj, "col_offset")) {
6335 int res;
6336 tmp = PyObject_GetAttrString(obj, "col_offset");
6337 if (tmp == NULL) goto failed;
6338 res = obj2ast_int(tmp, &col_offset, arena);
6339 if (res != 0) goto failed;
6340 Py_XDECREF(tmp);
6341 tmp = NULL;
6342 } else {
6343 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6344 return 1;
6345 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006346 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6347 if (isinstance == -1) {
6348 return 1;
6349 }
6350 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006351 expr_ty type;
6352 identifier name;
6353 asdl_seq* body;
6354
6355 if (PyObject_HasAttrString(obj, "type")) {
6356 int res;
6357 tmp = PyObject_GetAttrString(obj, "type");
6358 if (tmp == NULL) goto failed;
6359 res = obj2ast_expr(tmp, &type, arena);
6360 if (res != 0) goto failed;
6361 Py_XDECREF(tmp);
6362 tmp = NULL;
6363 } else {
6364 type = NULL;
6365 }
6366 if (PyObject_HasAttrString(obj, "name")) {
6367 int res;
6368 tmp = PyObject_GetAttrString(obj, "name");
6369 if (tmp == NULL) goto failed;
6370 res = obj2ast_identifier(tmp, &name, arena);
6371 if (res != 0) goto failed;
6372 Py_XDECREF(tmp);
6373 tmp = NULL;
6374 } else {
6375 name = NULL;
6376 }
6377 if (PyObject_HasAttrString(obj, "body")) {
6378 int res;
6379 Py_ssize_t len;
6380 Py_ssize_t i;
6381 tmp = PyObject_GetAttrString(obj, "body");
6382 if (tmp == NULL) goto failed;
6383 if (!PyList_Check(tmp)) {
6384 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6385 goto failed;
6386 }
6387 len = PyList_GET_SIZE(tmp);
6388 body = asdl_seq_new(len, arena);
6389 if (body == NULL) goto failed;
6390 for (i = 0; i < len; i++) {
6391 stmt_ty value;
6392 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6393 if (res != 0) goto failed;
6394 asdl_seq_SET(body, i, value);
6395 }
6396 Py_XDECREF(tmp);
6397 tmp = NULL;
6398 } else {
6399 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6400 return 1;
6401 }
6402 *out = ExceptHandler(type, name, body, lineno, col_offset,
6403 arena);
6404 if (*out == NULL) goto failed;
6405 return 0;
6406 }
6407
Benjamin Peterson5b066812010-11-20 01:38:49 +00006408 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6409 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006410 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006411 return 1;
6412}
6413
6414int
6415obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6416{
6417 PyObject* tmp = NULL;
6418 asdl_seq* args;
6419 identifier vararg;
6420 expr_ty varargannotation;
6421 asdl_seq* kwonlyargs;
6422 identifier kwarg;
6423 expr_ty kwargannotation;
6424 asdl_seq* defaults;
6425 asdl_seq* kw_defaults;
6426
6427 if (PyObject_HasAttrString(obj, "args")) {
6428 int res;
6429 Py_ssize_t len;
6430 Py_ssize_t i;
6431 tmp = PyObject_GetAttrString(obj, "args");
6432 if (tmp == NULL) goto failed;
6433 if (!PyList_Check(tmp)) {
6434 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6435 goto failed;
6436 }
6437 len = PyList_GET_SIZE(tmp);
6438 args = asdl_seq_new(len, arena);
6439 if (args == NULL) goto failed;
6440 for (i = 0; i < len; i++) {
6441 arg_ty value;
6442 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6443 if (res != 0) goto failed;
6444 asdl_seq_SET(args, i, value);
6445 }
6446 Py_XDECREF(tmp);
6447 tmp = NULL;
6448 } else {
6449 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6450 return 1;
6451 }
6452 if (PyObject_HasAttrString(obj, "vararg")) {
6453 int res;
6454 tmp = PyObject_GetAttrString(obj, "vararg");
6455 if (tmp == NULL) goto failed;
6456 res = obj2ast_identifier(tmp, &vararg, arena);
6457 if (res != 0) goto failed;
6458 Py_XDECREF(tmp);
6459 tmp = NULL;
6460 } else {
6461 vararg = NULL;
6462 }
6463 if (PyObject_HasAttrString(obj, "varargannotation")) {
6464 int res;
6465 tmp = PyObject_GetAttrString(obj, "varargannotation");
6466 if (tmp == NULL) goto failed;
6467 res = obj2ast_expr(tmp, &varargannotation, arena);
6468 if (res != 0) goto failed;
6469 Py_XDECREF(tmp);
6470 tmp = NULL;
6471 } else {
6472 varargannotation = NULL;
6473 }
6474 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6475 int res;
6476 Py_ssize_t len;
6477 Py_ssize_t i;
6478 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6479 if (tmp == NULL) goto failed;
6480 if (!PyList_Check(tmp)) {
6481 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6482 goto failed;
6483 }
6484 len = PyList_GET_SIZE(tmp);
6485 kwonlyargs = asdl_seq_new(len, arena);
6486 if (kwonlyargs == NULL) goto failed;
6487 for (i = 0; i < len; i++) {
6488 arg_ty value;
6489 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6490 if (res != 0) goto failed;
6491 asdl_seq_SET(kwonlyargs, i, value);
6492 }
6493 Py_XDECREF(tmp);
6494 tmp = NULL;
6495 } else {
6496 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6497 return 1;
6498 }
6499 if (PyObject_HasAttrString(obj, "kwarg")) {
6500 int res;
6501 tmp = PyObject_GetAttrString(obj, "kwarg");
6502 if (tmp == NULL) goto failed;
6503 res = obj2ast_identifier(tmp, &kwarg, arena);
6504 if (res != 0) goto failed;
6505 Py_XDECREF(tmp);
6506 tmp = NULL;
6507 } else {
6508 kwarg = NULL;
6509 }
6510 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6511 int res;
6512 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6513 if (tmp == NULL) goto failed;
6514 res = obj2ast_expr(tmp, &kwargannotation, arena);
6515 if (res != 0) goto failed;
6516 Py_XDECREF(tmp);
6517 tmp = NULL;
6518 } else {
6519 kwargannotation = NULL;
6520 }
6521 if (PyObject_HasAttrString(obj, "defaults")) {
6522 int res;
6523 Py_ssize_t len;
6524 Py_ssize_t i;
6525 tmp = PyObject_GetAttrString(obj, "defaults");
6526 if (tmp == NULL) goto failed;
6527 if (!PyList_Check(tmp)) {
6528 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6529 goto failed;
6530 }
6531 len = PyList_GET_SIZE(tmp);
6532 defaults = asdl_seq_new(len, arena);
6533 if (defaults == NULL) goto failed;
6534 for (i = 0; i < len; i++) {
6535 expr_ty value;
6536 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6537 if (res != 0) goto failed;
6538 asdl_seq_SET(defaults, i, value);
6539 }
6540 Py_XDECREF(tmp);
6541 tmp = NULL;
6542 } else {
6543 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6544 return 1;
6545 }
6546 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6547 int res;
6548 Py_ssize_t len;
6549 Py_ssize_t i;
6550 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6551 if (tmp == NULL) goto failed;
6552 if (!PyList_Check(tmp)) {
6553 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6554 goto failed;
6555 }
6556 len = PyList_GET_SIZE(tmp);
6557 kw_defaults = asdl_seq_new(len, arena);
6558 if (kw_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(kw_defaults, i, value);
6564 }
6565 Py_XDECREF(tmp);
6566 tmp = NULL;
6567 } else {
6568 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6569 return 1;
6570 }
6571 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6572 kwargannotation, defaults, kw_defaults, arena);
6573 return 0;
6574failed:
6575 Py_XDECREF(tmp);
6576 return 1;
6577}
6578
6579int
6580obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6581{
6582 PyObject* tmp = NULL;
6583 identifier arg;
6584 expr_ty annotation;
6585
6586 if (PyObject_HasAttrString(obj, "arg")) {
6587 int res;
6588 tmp = PyObject_GetAttrString(obj, "arg");
6589 if (tmp == NULL) goto failed;
6590 res = obj2ast_identifier(tmp, &arg, arena);
6591 if (res != 0) goto failed;
6592 Py_XDECREF(tmp);
6593 tmp = NULL;
6594 } else {
6595 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6596 return 1;
6597 }
6598 if (PyObject_HasAttrString(obj, "annotation")) {
6599 int res;
6600 tmp = PyObject_GetAttrString(obj, "annotation");
6601 if (tmp == NULL) goto failed;
6602 res = obj2ast_expr(tmp, &annotation, arena);
6603 if (res != 0) goto failed;
6604 Py_XDECREF(tmp);
6605 tmp = NULL;
6606 } else {
6607 annotation = NULL;
6608 }
6609 *out = arg(arg, annotation, arena);
6610 return 0;
6611failed:
6612 Py_XDECREF(tmp);
6613 return 1;
6614}
6615
6616int
6617obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6618{
6619 PyObject* tmp = NULL;
6620 identifier arg;
6621 expr_ty value;
6622
6623 if (PyObject_HasAttrString(obj, "arg")) {
6624 int res;
6625 tmp = PyObject_GetAttrString(obj, "arg");
6626 if (tmp == NULL) goto failed;
6627 res = obj2ast_identifier(tmp, &arg, arena);
6628 if (res != 0) goto failed;
6629 Py_XDECREF(tmp);
6630 tmp = NULL;
6631 } else {
6632 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6633 return 1;
6634 }
6635 if (PyObject_HasAttrString(obj, "value")) {
6636 int res;
6637 tmp = PyObject_GetAttrString(obj, "value");
6638 if (tmp == NULL) goto failed;
6639 res = obj2ast_expr(tmp, &value, arena);
6640 if (res != 0) goto failed;
6641 Py_XDECREF(tmp);
6642 tmp = NULL;
6643 } else {
6644 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6645 return 1;
6646 }
6647 *out = keyword(arg, value, arena);
6648 return 0;
6649failed:
6650 Py_XDECREF(tmp);
6651 return 1;
6652}
6653
6654int
6655obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6656{
6657 PyObject* tmp = NULL;
6658 identifier name;
6659 identifier asname;
6660
6661 if (PyObject_HasAttrString(obj, "name")) {
6662 int res;
6663 tmp = PyObject_GetAttrString(obj, "name");
6664 if (tmp == NULL) goto failed;
6665 res = obj2ast_identifier(tmp, &name, arena);
6666 if (res != 0) goto failed;
6667 Py_XDECREF(tmp);
6668 tmp = NULL;
6669 } else {
6670 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6671 return 1;
6672 }
6673 if (PyObject_HasAttrString(obj, "asname")) {
6674 int res;
6675 tmp = PyObject_GetAttrString(obj, "asname");
6676 if (tmp == NULL) goto failed;
6677 res = obj2ast_identifier(tmp, &asname, arena);
6678 if (res != 0) goto failed;
6679 Py_XDECREF(tmp);
6680 tmp = NULL;
6681 } else {
6682 asname = NULL;
6683 }
6684 *out = alias(name, asname, arena);
6685 return 0;
6686failed:
6687 Py_XDECREF(tmp);
6688 return 1;
6689}
6690
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006691int
6692obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6693{
6694 PyObject* tmp = NULL;
6695 expr_ty context_expr;
6696 expr_ty optional_vars;
6697
6698 if (PyObject_HasAttrString(obj, "context_expr")) {
6699 int res;
6700 tmp = PyObject_GetAttrString(obj, "context_expr");
6701 if (tmp == NULL) goto failed;
6702 res = obj2ast_expr(tmp, &context_expr, arena);
6703 if (res != 0) goto failed;
6704 Py_XDECREF(tmp);
6705 tmp = NULL;
6706 } else {
6707 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6708 return 1;
6709 }
6710 if (PyObject_HasAttrString(obj, "optional_vars")) {
6711 int res;
6712 tmp = PyObject_GetAttrString(obj, "optional_vars");
6713 if (tmp == NULL) goto failed;
6714 res = obj2ast_expr(tmp, &optional_vars, arena);
6715 if (res != 0) goto failed;
6716 Py_XDECREF(tmp);
6717 tmp = NULL;
6718 } else {
6719 optional_vars = NULL;
6720 }
6721 *out = withitem(context_expr, optional_vars, arena);
6722 return 0;
6723failed:
6724 Py_XDECREF(tmp);
6725 return 1;
6726}
6727
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006728
Martin v. Löwis1a214512008-06-11 05:26:20 +00006729static struct PyModuleDef _astmodule = {
6730 PyModuleDef_HEAD_INIT, "_ast"
6731};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006732PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006733PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006734{
6735 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006736 if (!init_types()) return NULL;
6737 m = PyModule_Create(&_astmodule);
6738 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006739 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006740 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6741 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006742 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006743 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006744 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6745 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006746 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006747 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006748 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006749 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006750 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006751 0) return NULL;
6752 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6753 NULL;
6754 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6755 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006756 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006757 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006758 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006759 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006760 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006761 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006762 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006763 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006764 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006765 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006766 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006767 0) return NULL;
6768 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6769 NULL;
6770 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6771 NULL;
6772 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6773 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6774 NULL;
6775 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6776 NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05006777 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
6778 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006779 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006780 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006781 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006782 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006783 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006784 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006785 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006786 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006787 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006788 return NULL;
6789 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6790 NULL;
6791 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6792 NULL;
6793 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6794 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006795 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006796 return NULL;
6797 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6798 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006799 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006800 return NULL;
6801 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6802 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006803 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006804 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006805 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006806 return NULL;
6807 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6808 NULL;
6809 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6810 NULL;
6811 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6812 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006813 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006814 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006815 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006816 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006817 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006818 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006819 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006820 (PyObject*)GeneratorExp_type) < 0) return NULL;
6821 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6822 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006823 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006824 return NULL;
6825 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6826 NULL;
6827 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6828 NULL;
6829 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6830 NULL;
6831 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6832 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006833 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006834 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006835 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006836 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006837 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006838 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006839 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006840 return NULL;
6841 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6842 NULL;
6843 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6844 NULL;
6845 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6846 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006847 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006848 (PyObject*)expr_context_type) < 0) return NULL;
6849 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6850 NULL;
6851 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6852 NULL;
6853 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6854 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006855 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006856 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006857 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006858 return NULL;
6859 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6860 NULL;
6861 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6862 NULL;
6863 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6864 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006865 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006866 return NULL;
6867 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6868 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006869 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006870 return NULL;
6871 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6872 NULL;
6873 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006874 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006875 return NULL;
6876 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6877 NULL;
6878 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6879 NULL;
6880 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6881 NULL;
6882 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6883 NULL;
6884 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6885 NULL;
6886 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6887 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006888 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006889 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006890 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006891 return NULL;
6892 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6893 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006894 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006895 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006896 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006897 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006898 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006899 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006900 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006901 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006902 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006903 return NULL;
6904 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6905 NULL;
6906 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6907 NULL;
6908 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6909 NULL;
6910 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6911 NULL;
6912 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6913 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6914 NULL;
6915 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6916 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6917 NULL;
6918 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6919 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6920 NULL;
6921 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6922 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6923 NULL;
6924 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6925 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6926 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006927 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006928 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006929 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006930 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006931 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006932 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006933 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006934 0) return NULL;
6935 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6936 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006937 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006938 return NULL;
6939 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6940 NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006941 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6942 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006943 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006944}
6945
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006946
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006947PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006948{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006949 init_types();
6950 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006951}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006952
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006953/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6954mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006955{
6956 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006957 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6958 (PyObject*)Interactive_type};
6959 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006960 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006961 assert(0 <= mode && mode <= 2);
6962
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006963 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006964
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006965 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6966 if (isinstance == -1)
6967 return NULL;
6968 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006969 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6970 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006971 return NULL;
6972 }
6973 if (obj2ast_mod(ast, &res, arena) != 0)
6974 return NULL;
6975 else
6976 return res;
6977}
6978
6979int PyAST_Check(PyObject* obj)
6980{
6981 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006982 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006983}
6984
Martin v. Löwis5b222132007-06-10 09:51:05 +00006985