blob: 68b109722b49cdfa3200ed15c2b15f1ff60fa741 [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
Benjamin Peterson180e6352011-07-22 11:09:07 -0500595static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500596{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500597 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
598 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500599 return 1;
600 }
601 return obj2ast_object(obj, out, arena);
602}
603
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500604static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
605{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500606 if (!PyUnicode_CheckExact(obj)) {
607 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
608 return 1;
609 }
610 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500611}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000612
613static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
614{
615 int i;
616 if (!PyLong_Check(obj)) {
617 PyObject *s = PyObject_Repr(obj);
618 if (s == NULL) return 1;
619 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000620 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000621 Py_DECREF(s);
622 return 1;
623 }
624
625 i = (int)PyLong_AsLong(obj);
626 if (i == -1 && PyErr_Occurred())
627 return 1;
628 *out = i;
629 return 0;
630}
631
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000632static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000633{
634 PyObject *empty_tuple, *d;
635 if (PyType_Ready(&AST_type) < 0)
636 return -1;
637 d = AST_type.tp_dict;
638 empty_tuple = PyTuple_New(0);
639 if (!empty_tuple ||
640 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
641 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
642 Py_XDECREF(empty_tuple);
643 return -1;
644 }
645 Py_DECREF(empty_tuple);
646 return 0;
647}
648
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000649
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650static int init_types(void)
651{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000654 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000655 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!mod_type) return 0;
657 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660 Interactive_type = make_type("Interactive", mod_type,
661 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663 Expression_type = make_type("Expression", mod_type, Expression_fields,
664 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000668 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000670 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000671 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000672 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000674 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!If_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500690 With_type = make_type("With", stmt_type, With_fields, 2);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000691 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000692 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!Raise_type) return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500694 Try_type = make_type("Try", stmt_type, Try_fields, 4);
695 if (!Try_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000701 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000705 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
706 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000707 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000709 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000711 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000713 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000715 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000717 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000718 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000726 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000730 Set_type = make_type("Set", expr_type, Set_fields, 1);
731 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000734 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
735 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000736 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
737 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 GeneratorExp_type = make_type("GeneratorExp", expr_type,
739 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000741 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000743 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000745 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000746 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000747 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000748 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000749 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000751 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
752 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000753 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
754 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000759 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
760 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000761 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000763 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000767 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!expr_context_type) return 0;
769 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000770 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000772 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000774 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000776 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000778 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000780 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000782 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000784 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000786 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000788 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000790 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000792 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000794 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!slice_type) return 0;
796 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000799 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000803 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!boolop_type) return 0;
805 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000814 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!operator_type) return 0;
816 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000817 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000819 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000821 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000823 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000825 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000827 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000829 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000831 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000833 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000835 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000837 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000839 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000841 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000845 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000847 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000849 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000851 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000853 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000855 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000865 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!unaryop_type) return 0;
867 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000868 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000870 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000872 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000874 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000876 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000878 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000880 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000884 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!cmpop_type) return 0;
886 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000887 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000889 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000890 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000891 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000893 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000895 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000897 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000898 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000899 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000900 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000901 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000902 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000903 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000904 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000905 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000906 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000907 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000908 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000909 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000910 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000911 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000913 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000915 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000917 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000919 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000921 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000922 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000923 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000924 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000925 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000926 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000927 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000928 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000930 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000931 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000932 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
933 return 0;
934 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
935 ExceptHandler_fields, 3);
936 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000937 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000938 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000939 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000940 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000941 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000942 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000943 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000944 if (!alias_type) return 0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500945 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
946 if (!withitem_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000947 initialized = 1;
948 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000949}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000950
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000951static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
952static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
953static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
954static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
955 arena);
956static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
957static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
958static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
959static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
960static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
961static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
962 arena);
963static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
964 arena);
965static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
966static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
967static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
968static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500969static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000972Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
974 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000975 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000976 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 p->kind = Module_kind;
979 p->v.Module.body = body;
980 return p;
981}
982
983mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000984Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985{
986 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000988 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 p->kind = Interactive_kind;
991 p->v.Interactive.body = body;
992 return p;
993}
994
995mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997{
998 mod_ty p;
999 if (!body) {
1000 PyErr_SetString(PyExc_ValueError,
1001 "field body is required for Expression");
1002 return NULL;
1003 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001004 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001005 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 p->kind = Expression_kind;
1008 p->v.Expression.body = body;
1009 return p;
1010}
1011
1012mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014{
1015 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001017 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 p->kind = Suite_kind;
1020 p->v.Suite.body = body;
1021 return p;
1022}
1023
1024stmt_ty
1025FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001026 decorator_list, expr_ty returns, int lineno, int col_offset,
1027 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028{
1029 stmt_ty p;
1030 if (!name) {
1031 PyErr_SetString(PyExc_ValueError,
1032 "field name is required for FunctionDef");
1033 return NULL;
1034 }
1035 if (!args) {
1036 PyErr_SetString(PyExc_ValueError,
1037 "field args is required for FunctionDef");
1038 return NULL;
1039 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001040 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001041 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 p->kind = FunctionDef_kind;
1044 p->v.FunctionDef.name = name;
1045 p->v.FunctionDef.args = args;
1046 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001047 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001048 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001050 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 return p;
1052}
1053
1054stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001055ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001056 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1057 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058{
1059 stmt_ty p;
1060 if (!name) {
1061 PyErr_SetString(PyExc_ValueError,
1062 "field name is required for ClassDef");
1063 return NULL;
1064 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001065 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001066 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 p->kind = ClassDef_kind;
1069 p->v.ClassDef.name = name;
1070 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001071 p->v.ClassDef.keywords = keywords;
1072 p->v.ClassDef.starargs = starargs;
1073 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001075 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001077 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 return p;
1079}
1080
1081stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001082Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083{
1084 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001085 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001086 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 p->kind = Return_kind;
1089 p->v.Return.value = value;
1090 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001091 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 return p;
1093}
1094
1095stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001096Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097{
1098 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001099 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001100 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 p->kind = Delete_kind;
1103 p->v.Delete.targets = targets;
1104 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001105 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 return p;
1107}
1108
1109stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001110Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1111 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112{
1113 stmt_ty p;
1114 if (!value) {
1115 PyErr_SetString(PyExc_ValueError,
1116 "field value is required for Assign");
1117 return NULL;
1118 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001119 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001120 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 p->kind = Assign_kind;
1123 p->v.Assign.targets = targets;
1124 p->v.Assign.value = value;
1125 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001126 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 return p;
1128}
1129
1130stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001131AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1132 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133{
1134 stmt_ty p;
1135 if (!target) {
1136 PyErr_SetString(PyExc_ValueError,
1137 "field target is required for AugAssign");
1138 return NULL;
1139 }
1140 if (!op) {
1141 PyErr_SetString(PyExc_ValueError,
1142 "field op is required for AugAssign");
1143 return NULL;
1144 }
1145 if (!value) {
1146 PyErr_SetString(PyExc_ValueError,
1147 "field value is required for AugAssign");
1148 return NULL;
1149 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001150 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001151 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 p->kind = AugAssign_kind;
1154 p->v.AugAssign.target = target;
1155 p->v.AugAssign.op = op;
1156 p->v.AugAssign.value = value;
1157 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001158 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return p;
1160}
1161
1162stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001164 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165{
1166 stmt_ty p;
1167 if (!target) {
1168 PyErr_SetString(PyExc_ValueError,
1169 "field target is required for For");
1170 return NULL;
1171 }
1172 if (!iter) {
1173 PyErr_SetString(PyExc_ValueError,
1174 "field iter is required for For");
1175 return NULL;
1176 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001177 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001178 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 p->kind = For_kind;
1181 p->v.For.target = target;
1182 p->v.For.iter = iter;
1183 p->v.For.body = body;
1184 p->v.For.orelse = orelse;
1185 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001186 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return p;
1188}
1189
1190stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001191While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1192 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193{
1194 stmt_ty p;
1195 if (!test) {
1196 PyErr_SetString(PyExc_ValueError,
1197 "field test is required for While");
1198 return NULL;
1199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001201 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 p->kind = While_kind;
1204 p->v.While.test = test;
1205 p->v.While.body = body;
1206 p->v.While.orelse = orelse;
1207 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return p;
1210}
1211
1212stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001213If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1214 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
1216 stmt_ty p;
1217 if (!test) {
1218 PyErr_SetString(PyExc_ValueError,
1219 "field test is required for If");
1220 return NULL;
1221 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001222 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001223 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 p->kind = If_kind;
1226 p->v.If.test = test;
1227 p->v.If.body = body;
1228 p->v.If.orelse = orelse;
1229 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001230 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 return p;
1232}
1233
1234stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001235With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1236 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001237{
1238 stmt_ty p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001239 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001240 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001241 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001242 p->kind = With_kind;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001243 p->v.With.items = items;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001244 p->v.With.body = body;
1245 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001247 return p;
1248}
1249
1250stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001251Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252{
1253 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001255 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001258 p->v.Raise.exc = exc;
1259 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001261 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 return p;
1263}
1264
1265stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001266Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1267 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268{
1269 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001270 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001271 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001273 p->kind = Try_kind;
1274 p->v.Try.body = body;
1275 p->v.Try.handlers = handlers;
1276 p->v.Try.orelse = orelse;
1277 p->v.Try.finalbody = finalbody;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return p;
1281}
1282
1283stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285{
1286 stmt_ty p;
1287 if (!test) {
1288 PyErr_SetString(PyExc_ValueError,
1289 "field test is required for Assert");
1290 return NULL;
1291 }
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 = Assert_kind;
1296 p->v.Assert.test = test;
1297 p->v.Assert.msg = msg;
1298 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001299 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 return p;
1301}
1302
1303stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001304Import(asdl_seq * names, int lineno, int 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 = Import_kind;
1311 p->v.Import.names = names;
1312 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001313 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 return p;
1315}
1316
1317stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001318ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1319 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320{
1321 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001323 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 p->kind = ImportFrom_kind;
1326 p->v.ImportFrom.module = module;
1327 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001328 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001330 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 return p;
1332}
1333
1334stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001335Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336{
1337 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001338 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001339 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 p->kind = Global_kind;
1342 p->v.Global.names = names;
1343 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001344 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 return p;
1346}
1347
1348stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001349Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1350{
1351 stmt_ty p;
1352 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1353 if (!p)
1354 return NULL;
1355 p->kind = Nonlocal_kind;
1356 p->v.Nonlocal.names = names;
1357 p->lineno = lineno;
1358 p->col_offset = col_offset;
1359 return p;
1360}
1361
1362stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364{
1365 stmt_ty p;
1366 if (!value) {
1367 PyErr_SetString(PyExc_ValueError,
1368 "field value is required for Expr");
1369 return NULL;
1370 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001372 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 p->kind = Expr_kind;
1375 p->v.Expr.value = value;
1376 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001377 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 return p;
1379}
1380
1381stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001382Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383{
1384 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001385 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001386 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 p->kind = Pass_kind;
1389 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001390 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 return p;
1392}
1393
1394stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001395Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396{
1397 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001398 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001399 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 p->kind = Break_kind;
1402 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001403 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 return p;
1405}
1406
1407stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001408Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409{
1410 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001411 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001412 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 p->kind = Continue_kind;
1415 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return p;
1418}
1419
1420expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1422 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423{
1424 expr_ty p;
1425 if (!op) {
1426 PyErr_SetString(PyExc_ValueError,
1427 "field op is required for BoolOp");
1428 return NULL;
1429 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001430 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001431 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 p->kind = BoolOp_kind;
1434 p->v.BoolOp.op = op;
1435 p->v.BoolOp.values = values;
1436 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001437 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return p;
1439}
1440
1441expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001442BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1443 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444{
1445 expr_ty p;
1446 if (!left) {
1447 PyErr_SetString(PyExc_ValueError,
1448 "field left is required for BinOp");
1449 return NULL;
1450 }
1451 if (!op) {
1452 PyErr_SetString(PyExc_ValueError,
1453 "field op is required for BinOp");
1454 return NULL;
1455 }
1456 if (!right) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "field right is required for BinOp");
1459 return NULL;
1460 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001461 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001462 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 p->kind = BinOp_kind;
1465 p->v.BinOp.left = left;
1466 p->v.BinOp.op = op;
1467 p->v.BinOp.right = right;
1468 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001469 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 return p;
1471}
1472
1473expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001474UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1475 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476{
1477 expr_ty p;
1478 if (!op) {
1479 PyErr_SetString(PyExc_ValueError,
1480 "field op is required for UnaryOp");
1481 return NULL;
1482 }
1483 if (!operand) {
1484 PyErr_SetString(PyExc_ValueError,
1485 "field operand is required for UnaryOp");
1486 return NULL;
1487 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001489 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 p->kind = UnaryOp_kind;
1492 p->v.UnaryOp.op = op;
1493 p->v.UnaryOp.operand = operand;
1494 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001495 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 return p;
1497}
1498
1499expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1501 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502{
1503 expr_ty p;
1504 if (!args) {
1505 PyErr_SetString(PyExc_ValueError,
1506 "field args is required for Lambda");
1507 return NULL;
1508 }
1509 if (!body) {
1510 PyErr_SetString(PyExc_ValueError,
1511 "field body is required for Lambda");
1512 return NULL;
1513 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001514 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001515 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 p->kind = Lambda_kind;
1518 p->v.Lambda.args = args;
1519 p->v.Lambda.body = body;
1520 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001521 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 return p;
1523}
1524
1525expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001526IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1527 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001528{
1529 expr_ty p;
1530 if (!test) {
1531 PyErr_SetString(PyExc_ValueError,
1532 "field test is required for IfExp");
1533 return NULL;
1534 }
1535 if (!body) {
1536 PyErr_SetString(PyExc_ValueError,
1537 "field body is required for IfExp");
1538 return NULL;
1539 }
1540 if (!orelse) {
1541 PyErr_SetString(PyExc_ValueError,
1542 "field orelse is required for IfExp");
1543 return NULL;
1544 }
1545 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001546 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001547 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001548 p->kind = IfExp_kind;
1549 p->v.IfExp.test = test;
1550 p->v.IfExp.body = body;
1551 p->v.IfExp.orelse = orelse;
1552 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001554 return p;
1555}
1556
1557expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1559 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560{
1561 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001563 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 p->kind = Dict_kind;
1566 p->v.Dict.keys = keys;
1567 p->v.Dict.values = values;
1568 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001569 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 return p;
1571}
1572
1573expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001574Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1575{
1576 expr_ty p;
1577 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001578 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001579 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001580 p->kind = Set_kind;
1581 p->v.Set.elts = elts;
1582 p->lineno = lineno;
1583 p->col_offset = col_offset;
1584 return p;
1585}
1586
1587expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001588ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1589 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
1591 expr_ty p;
1592 if (!elt) {
1593 PyErr_SetString(PyExc_ValueError,
1594 "field elt is required for ListComp");
1595 return NULL;
1596 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001597 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001598 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 p->kind = ListComp_kind;
1601 p->v.ListComp.elt = elt;
1602 p->v.ListComp.generators = generators;
1603 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001604 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 return p;
1606}
1607
1608expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001609SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1610 *arena)
1611{
1612 expr_ty p;
1613 if (!elt) {
1614 PyErr_SetString(PyExc_ValueError,
1615 "field elt is required for SetComp");
1616 return NULL;
1617 }
1618 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1619 if (!p)
1620 return NULL;
1621 p->kind = SetComp_kind;
1622 p->v.SetComp.elt = elt;
1623 p->v.SetComp.generators = generators;
1624 p->lineno = lineno;
1625 p->col_offset = col_offset;
1626 return p;
1627}
1628
1629expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001630DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1631 col_offset, PyArena *arena)
1632{
1633 expr_ty p;
1634 if (!key) {
1635 PyErr_SetString(PyExc_ValueError,
1636 "field key is required for DictComp");
1637 return NULL;
1638 }
1639 if (!value) {
1640 PyErr_SetString(PyExc_ValueError,
1641 "field value is required for DictComp");
1642 return NULL;
1643 }
1644 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1645 if (!p)
1646 return NULL;
1647 p->kind = DictComp_kind;
1648 p->v.DictComp.key = key;
1649 p->v.DictComp.value = value;
1650 p->v.DictComp.generators = generators;
1651 p->lineno = lineno;
1652 p->col_offset = col_offset;
1653 return p;
1654}
1655
1656expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001657GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1658 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659{
1660 expr_ty p;
1661 if (!elt) {
1662 PyErr_SetString(PyExc_ValueError,
1663 "field elt is required for GeneratorExp");
1664 return NULL;
1665 }
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 = GeneratorExp_kind;
1670 p->v.GeneratorExp.elt = elt;
1671 p->v.GeneratorExp.generators = generators;
1672 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 return p;
1675}
1676
1677expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001678Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679{
1680 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001681 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001682 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 p->kind = Yield_kind;
1685 p->v.Yield.value = value;
1686 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001687 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 return p;
1689}
1690
1691expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001692Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1693 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694{
1695 expr_ty p;
1696 if (!left) {
1697 PyErr_SetString(PyExc_ValueError,
1698 "field left is required for Compare");
1699 return NULL;
1700 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001701 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001702 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 p->kind = Compare_kind;
1705 p->v.Compare.left = left;
1706 p->v.Compare.ops = ops;
1707 p->v.Compare.comparators = comparators;
1708 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 return p;
1711}
1712
1713expr_ty
1714Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001715 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716{
1717 expr_ty p;
1718 if (!func) {
1719 PyErr_SetString(PyExc_ValueError,
1720 "field func is required for Call");
1721 return NULL;
1722 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001723 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001724 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 p->kind = Call_kind;
1727 p->v.Call.func = func;
1728 p->v.Call.args = args;
1729 p->v.Call.keywords = keywords;
1730 p->v.Call.starargs = starargs;
1731 p->v.Call.kwargs = kwargs;
1732 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001733 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return p;
1735}
1736
1737expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001738Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739{
1740 expr_ty p;
1741 if (!n) {
1742 PyErr_SetString(PyExc_ValueError,
1743 "field n is required for Num");
1744 return NULL;
1745 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001746 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001747 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 p->kind = Num_kind;
1750 p->v.Num.n = n;
1751 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 return p;
1754}
1755
1756expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001757Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758{
1759 expr_ty p;
1760 if (!s) {
1761 PyErr_SetString(PyExc_ValueError,
1762 "field s is required for Str");
1763 return NULL;
1764 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001765 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001766 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 p->kind = Str_kind;
1769 p->v.Str.s = s;
1770 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001771 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772 return p;
1773}
1774
1775expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001776Bytes(string s, int lineno, int col_offset, PyArena *arena)
1777{
1778 expr_ty p;
1779 if (!s) {
1780 PyErr_SetString(PyExc_ValueError,
1781 "field s is required for Bytes");
1782 return NULL;
1783 }
1784 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001785 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001786 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001787 p->kind = Bytes_kind;
1788 p->v.Bytes.s = s;
1789 p->lineno = lineno;
1790 p->col_offset = col_offset;
1791 return p;
1792}
1793
1794expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001795Ellipsis(int lineno, int col_offset, PyArena *arena)
1796{
1797 expr_ty p;
1798 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001799 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001800 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001801 p->kind = Ellipsis_kind;
1802 p->lineno = lineno;
1803 p->col_offset = col_offset;
1804 return p;
1805}
1806
1807expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001808Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1809 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810{
1811 expr_ty p;
1812 if (!value) {
1813 PyErr_SetString(PyExc_ValueError,
1814 "field value is required for Attribute");
1815 return NULL;
1816 }
1817 if (!attr) {
1818 PyErr_SetString(PyExc_ValueError,
1819 "field attr is required for Attribute");
1820 return NULL;
1821 }
1822 if (!ctx) {
1823 PyErr_SetString(PyExc_ValueError,
1824 "field ctx is required for Attribute");
1825 return NULL;
1826 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001827 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001828 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 p->kind = Attribute_kind;
1831 p->v.Attribute.value = value;
1832 p->v.Attribute.attr = attr;
1833 p->v.Attribute.ctx = ctx;
1834 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001835 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 return p;
1837}
1838
1839expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001840Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1841 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842{
1843 expr_ty p;
1844 if (!value) {
1845 PyErr_SetString(PyExc_ValueError,
1846 "field value is required for Subscript");
1847 return NULL;
1848 }
1849 if (!slice) {
1850 PyErr_SetString(PyExc_ValueError,
1851 "field slice is required for Subscript");
1852 return NULL;
1853 }
1854 if (!ctx) {
1855 PyErr_SetString(PyExc_ValueError,
1856 "field ctx is required for Subscript");
1857 return NULL;
1858 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001860 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 p->kind = Subscript_kind;
1863 p->v.Subscript.value = value;
1864 p->v.Subscript.slice = slice;
1865 p->v.Subscript.ctx = ctx;
1866 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001867 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 return p;
1869}
1870
1871expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001872Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1873 *arena)
1874{
1875 expr_ty p;
1876 if (!value) {
1877 PyErr_SetString(PyExc_ValueError,
1878 "field value is required for Starred");
1879 return NULL;
1880 }
1881 if (!ctx) {
1882 PyErr_SetString(PyExc_ValueError,
1883 "field ctx is required for Starred");
1884 return NULL;
1885 }
1886 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1887 if (!p)
1888 return NULL;
1889 p->kind = Starred_kind;
1890 p->v.Starred.value = value;
1891 p->v.Starred.ctx = ctx;
1892 p->lineno = lineno;
1893 p->col_offset = col_offset;
1894 return p;
1895}
1896
1897expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001898Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1899 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900{
1901 expr_ty p;
1902 if (!id) {
1903 PyErr_SetString(PyExc_ValueError,
1904 "field id is required for Name");
1905 return NULL;
1906 }
1907 if (!ctx) {
1908 PyErr_SetString(PyExc_ValueError,
1909 "field ctx is required for Name");
1910 return NULL;
1911 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001913 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 p->kind = Name_kind;
1916 p->v.Name.id = id;
1917 p->v.Name.ctx = ctx;
1918 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001919 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 return p;
1921}
1922
1923expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001924List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1925 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926{
1927 expr_ty p;
1928 if (!ctx) {
1929 PyErr_SetString(PyExc_ValueError,
1930 "field ctx is required for List");
1931 return NULL;
1932 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001933 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001934 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 p->kind = List_kind;
1937 p->v.List.elts = elts;
1938 p->v.List.ctx = ctx;
1939 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001940 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 return p;
1942}
1943
1944expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001945Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1946 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947{
1948 expr_ty p;
1949 if (!ctx) {
1950 PyErr_SetString(PyExc_ValueError,
1951 "field ctx is required for Tuple");
1952 return NULL;
1953 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954 p = (expr_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 = Tuple_kind;
1958 p->v.Tuple.elts = elts;
1959 p->v.Tuple.ctx = ctx;
1960 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001961 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 return p;
1963}
1964
1965slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967{
1968 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001969 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001970 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 p->kind = Slice_kind;
1973 p->v.Slice.lower = lower;
1974 p->v.Slice.upper = upper;
1975 p->v.Slice.step = step;
1976 return p;
1977}
1978
1979slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001980ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
1982 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001983 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001984 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 p->kind = ExtSlice_kind;
1987 p->v.ExtSlice.dims = dims;
1988 return p;
1989}
1990
1991slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001992Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993{
1994 slice_ty p;
1995 if (!value) {
1996 PyErr_SetString(PyExc_ValueError,
1997 "field value is required for Index");
1998 return NULL;
1999 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002000 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002001 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 p->kind = Index_kind;
2004 p->v.Index.value = value;
2005 return p;
2006}
2007
2008comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010{
2011 comprehension_ty p;
2012 if (!target) {
2013 PyErr_SetString(PyExc_ValueError,
2014 "field target is required for comprehension");
2015 return NULL;
2016 }
2017 if (!iter) {
2018 PyErr_SetString(PyExc_ValueError,
2019 "field iter is required for comprehension");
2020 return NULL;
2021 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002023 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 p->target = target;
2026 p->iter = iter;
2027 p->ifs = ifs;
2028 return p;
2029}
2030
2031excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002032ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002033 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034{
2035 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002036 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002037 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002039 p->kind = ExceptHandler_kind;
2040 p->v.ExceptHandler.type = type;
2041 p->v.ExceptHandler.name = name;
2042 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002043 p->lineno = lineno;
2044 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 return p;
2046}
2047
2048arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002049arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2050 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2051 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052{
2053 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002054 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002055 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 p->args = args;
2058 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002059 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002060 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002062 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002064 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 return p;
2066}
2067
Neal Norwitzc1505362006-12-28 06:47:50 +00002068arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002069arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002070{
2071 arg_ty p;
2072 if (!arg) {
2073 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002074 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002075 return NULL;
2076 }
2077 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002078 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002079 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002080 p->arg = arg;
2081 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002082 return p;
2083}
2084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002086keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087{
2088 keyword_ty p;
2089 if (!arg) {
2090 PyErr_SetString(PyExc_ValueError,
2091 "field arg is required for keyword");
2092 return NULL;
2093 }
2094 if (!value) {
2095 PyErr_SetString(PyExc_ValueError,
2096 "field value is required for keyword");
2097 return NULL;
2098 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002100 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 p->arg = arg;
2103 p->value = value;
2104 return p;
2105}
2106
2107alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109{
2110 alias_ty p;
2111 if (!name) {
2112 PyErr_SetString(PyExc_ValueError,
2113 "field name is required for alias");
2114 return NULL;
2115 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002116 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002117 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 p->name = name;
2120 p->asname = asname;
2121 return p;
2122}
2123
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002124withitem_ty
2125withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2126{
2127 withitem_ty p;
2128 if (!context_expr) {
2129 PyErr_SetString(PyExc_ValueError,
2130 "field context_expr is required for withitem");
2131 return NULL;
2132 }
2133 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2134 if (!p)
2135 return NULL;
2136 p->context_expr = context_expr;
2137 p->optional_vars = optional_vars;
2138 return p;
2139}
2140
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002141
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002142PyObject*
2143ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002144{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 mod_ty o = (mod_ty)_o;
2146 PyObject *result = NULL, *value = NULL;
2147 if (!o) {
2148 Py_INCREF(Py_None);
2149 return Py_None;
2150 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 switch (o->kind) {
2153 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002154 result = PyType_GenericNew(Module_type, NULL, NULL);
2155 if (!result) goto failed;
2156 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2157 if (!value) goto failed;
2158 if (PyObject_SetAttrString(result, "body", value) == -1)
2159 goto failed;
2160 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 break;
2162 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002163 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2164 if (!result) goto failed;
2165 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2166 if (!value) goto failed;
2167 if (PyObject_SetAttrString(result, "body", value) == -1)
2168 goto failed;
2169 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 break;
2171 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002172 result = PyType_GenericNew(Expression_type, NULL, NULL);
2173 if (!result) goto failed;
2174 value = ast2obj_expr(o->v.Expression.body);
2175 if (!value) goto failed;
2176 if (PyObject_SetAttrString(result, "body", value) == -1)
2177 goto failed;
2178 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 break;
2180 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002181 result = PyType_GenericNew(Suite_type, NULL, NULL);
2182 if (!result) goto failed;
2183 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2184 if (!value) goto failed;
2185 if (PyObject_SetAttrString(result, "body", value) == -1)
2186 goto failed;
2187 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 break;
2189 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002190 return result;
2191failed:
2192 Py_XDECREF(value);
2193 Py_XDECREF(result);
2194 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195}
2196
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002197PyObject*
2198ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002200 stmt_ty o = (stmt_ty)_o;
2201 PyObject *result = NULL, *value = NULL;
2202 if (!o) {
2203 Py_INCREF(Py_None);
2204 return Py_None;
2205 }
2206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 switch (o->kind) {
2208 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002209 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2210 if (!result) goto failed;
2211 value = ast2obj_identifier(o->v.FunctionDef.name);
2212 if (!value) goto failed;
2213 if (PyObject_SetAttrString(result, "name", value) == -1)
2214 goto failed;
2215 Py_DECREF(value);
2216 value = ast2obj_arguments(o->v.FunctionDef.args);
2217 if (!value) goto failed;
2218 if (PyObject_SetAttrString(result, "args", value) == -1)
2219 goto failed;
2220 Py_DECREF(value);
2221 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2222 if (!value) goto failed;
2223 if (PyObject_SetAttrString(result, "body", value) == -1)
2224 goto failed;
2225 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002226 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2227 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002228 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002229 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2230 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002231 goto failed;
2232 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002233 value = ast2obj_expr(o->v.FunctionDef.returns);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "returns", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 break;
2239 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002240 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2241 if (!result) goto failed;
2242 value = ast2obj_identifier(o->v.ClassDef.name);
2243 if (!value) goto failed;
2244 if (PyObject_SetAttrString(result, "name", value) == -1)
2245 goto failed;
2246 Py_DECREF(value);
2247 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "bases", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002252 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
2257 value = ast2obj_expr(o->v.ClassDef.starargs);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
2262 value = ast2obj_expr(o->v.ClassDef.kwargs);
2263 if (!value) goto failed;
2264 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2265 goto failed;
2266 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002267 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2268 if (!value) goto failed;
2269 if (PyObject_SetAttrString(result, "body", value) == -1)
2270 goto failed;
2271 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002272 value = ast2obj_list(o->v.ClassDef.decorator_list,
2273 ast2obj_expr);
2274 if (!value) goto failed;
2275 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2276 -1)
2277 goto failed;
2278 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 break;
2280 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002281 result = PyType_GenericNew(Return_type, NULL, NULL);
2282 if (!result) goto failed;
2283 value = ast2obj_expr(o->v.Return.value);
2284 if (!value) goto failed;
2285 if (PyObject_SetAttrString(result, "value", value) == -1)
2286 goto failed;
2287 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 break;
2289 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002290 result = PyType_GenericNew(Delete_type, NULL, NULL);
2291 if (!result) goto failed;
2292 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2293 if (!value) goto failed;
2294 if (PyObject_SetAttrString(result, "targets", value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 break;
2298 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002299 result = PyType_GenericNew(Assign_type, NULL, NULL);
2300 if (!result) goto failed;
2301 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2302 if (!value) goto failed;
2303 if (PyObject_SetAttrString(result, "targets", value) == -1)
2304 goto failed;
2305 Py_DECREF(value);
2306 value = ast2obj_expr(o->v.Assign.value);
2307 if (!value) goto failed;
2308 if (PyObject_SetAttrString(result, "value", value) == -1)
2309 goto failed;
2310 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 break;
2312 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002313 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2314 if (!result) goto failed;
2315 value = ast2obj_expr(o->v.AugAssign.target);
2316 if (!value) goto failed;
2317 if (PyObject_SetAttrString(result, "target", value) == -1)
2318 goto failed;
2319 Py_DECREF(value);
2320 value = ast2obj_operator(o->v.AugAssign.op);
2321 if (!value) goto failed;
2322 if (PyObject_SetAttrString(result, "op", value) == -1)
2323 goto failed;
2324 Py_DECREF(value);
2325 value = ast2obj_expr(o->v.AugAssign.value);
2326 if (!value) goto failed;
2327 if (PyObject_SetAttrString(result, "value", value) == -1)
2328 goto failed;
2329 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002332 result = PyType_GenericNew(For_type, NULL, NULL);
2333 if (!result) goto failed;
2334 value = ast2obj_expr(o->v.For.target);
2335 if (!value) goto failed;
2336 if (PyObject_SetAttrString(result, "target", value) == -1)
2337 goto failed;
2338 Py_DECREF(value);
2339 value = ast2obj_expr(o->v.For.iter);
2340 if (!value) goto failed;
2341 if (PyObject_SetAttrString(result, "iter", value) == -1)
2342 goto failed;
2343 Py_DECREF(value);
2344 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "body", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 break;
2355 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002356 result = PyType_GenericNew(While_type, NULL, NULL);
2357 if (!result) goto failed;
2358 value = ast2obj_expr(o->v.While.test);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "test", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2364 if (!value) goto failed;
2365 if (PyObject_SetAttrString(result, "body", value) == -1)
2366 goto failed;
2367 Py_DECREF(value);
2368 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 break;
2374 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002375 result = PyType_GenericNew(If_type, NULL, NULL);
2376 if (!result) goto failed;
2377 value = ast2obj_expr(o->v.If.test);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "test", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2383 if (!value) goto failed;
2384 if (PyObject_SetAttrString(result, "body", value) == -1)
2385 goto failed;
2386 Py_DECREF(value);
2387 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2388 if (!value) goto failed;
2389 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2390 goto failed;
2391 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002393 case With_kind:
2394 result = PyType_GenericNew(With_type, NULL, NULL);
2395 if (!result) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002396 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002397 if (!value) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002398 if (PyObject_SetAttrString(result, "items", value) == -1)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002399 goto failed;
2400 Py_DECREF(value);
2401 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2402 if (!value) goto failed;
2403 if (PyObject_SetAttrString(result, "body", value) == -1)
2404 goto failed;
2405 Py_DECREF(value);
2406 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002408 result = PyType_GenericNew(Raise_type, NULL, NULL);
2409 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002410 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002411 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002412 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002413 goto failed;
2414 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002415 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002416 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002417 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002418 goto failed;
2419 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002421 case Try_kind:
2422 result = PyType_GenericNew(Try_type, NULL, NULL);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002423 if (!result) goto failed;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002424 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002425 if (!value) goto failed;
2426 if (PyObject_SetAttrString(result, "body", value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002429 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002430 if (!value) goto failed;
2431 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2432 goto failed;
2433 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002434 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002435 if (!value) goto failed;
2436 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2437 goto failed;
2438 Py_DECREF(value);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002439 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 if (!value) goto failed;
2441 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2442 goto failed;
2443 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 break;
2445 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002446 result = PyType_GenericNew(Assert_type, NULL, NULL);
2447 if (!result) goto failed;
2448 value = ast2obj_expr(o->v.Assert.test);
2449 if (!value) goto failed;
2450 if (PyObject_SetAttrString(result, "test", value) == -1)
2451 goto failed;
2452 Py_DECREF(value);
2453 value = ast2obj_expr(o->v.Assert.msg);
2454 if (!value) goto failed;
2455 if (PyObject_SetAttrString(result, "msg", value) == -1)
2456 goto failed;
2457 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 break;
2459 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002460 result = PyType_GenericNew(Import_type, NULL, NULL);
2461 if (!result) goto failed;
2462 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2463 if (!value) goto failed;
2464 if (PyObject_SetAttrString(result, "names", value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 break;
2468 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002469 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2470 if (!result) goto failed;
2471 value = ast2obj_identifier(o->v.ImportFrom.module);
2472 if (!value) goto failed;
2473 if (PyObject_SetAttrString(result, "module", value) == -1)
2474 goto failed;
2475 Py_DECREF(value);
2476 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2477 if (!value) goto failed;
2478 if (PyObject_SetAttrString(result, "names", value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002481 value = ast2obj_int(o->v.ImportFrom.level);
2482 if (!value) goto failed;
2483 if (PyObject_SetAttrString(result, "level", value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002488 result = PyType_GenericNew(Global_type, NULL, NULL);
2489 if (!result) goto failed;
2490 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2491 if (!value) goto failed;
2492 if (PyObject_SetAttrString(result, "names", value) == -1)
2493 goto failed;
2494 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002496 case Nonlocal_kind:
2497 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2498 if (!result) goto failed;
2499 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2500 if (!value) goto failed;
2501 if (PyObject_SetAttrString(result, "names", value) == -1)
2502 goto failed;
2503 Py_DECREF(value);
2504 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002506 result = PyType_GenericNew(Expr_type, NULL, NULL);
2507 if (!result) goto failed;
2508 value = ast2obj_expr(o->v.Expr.value);
2509 if (!value) goto failed;
2510 if (PyObject_SetAttrString(result, "value", value) == -1)
2511 goto failed;
2512 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 break;
2514 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002515 result = PyType_GenericNew(Pass_type, NULL, NULL);
2516 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 break;
2518 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002519 result = PyType_GenericNew(Break_type, NULL, NULL);
2520 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 break;
2522 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002523 result = PyType_GenericNew(Continue_type, NULL, NULL);
2524 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 break;
2526 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002527 value = ast2obj_int(o->lineno);
2528 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002529 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2530 goto failed;
2531 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002532 value = ast2obj_int(o->col_offset);
2533 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002534 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2535 goto failed;
2536 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002537 return result;
2538failed:
2539 Py_XDECREF(value);
2540 Py_XDECREF(result);
2541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542}
2543
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002544PyObject*
2545ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547 expr_ty o = (expr_ty)_o;
2548 PyObject *result = NULL, *value = NULL;
2549 if (!o) {
2550 Py_INCREF(Py_None);
2551 return Py_None;
2552 }
2553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 switch (o->kind) {
2555 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002556 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2557 if (!result) goto failed;
2558 value = ast2obj_boolop(o->v.BoolOp.op);
2559 if (!value) goto failed;
2560 if (PyObject_SetAttrString(result, "op", value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
2563 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2564 if (!value) goto failed;
2565 if (PyObject_SetAttrString(result, "values", value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 break;
2569 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002570 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2571 if (!result) goto failed;
2572 value = ast2obj_expr(o->v.BinOp.left);
2573 if (!value) goto failed;
2574 if (PyObject_SetAttrString(result, "left", value) == -1)
2575 goto failed;
2576 Py_DECREF(value);
2577 value = ast2obj_operator(o->v.BinOp.op);
2578 if (!value) goto failed;
2579 if (PyObject_SetAttrString(result, "op", value) == -1)
2580 goto failed;
2581 Py_DECREF(value);
2582 value = ast2obj_expr(o->v.BinOp.right);
2583 if (!value) goto failed;
2584 if (PyObject_SetAttrString(result, "right", value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 break;
2588 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002589 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2590 if (!result) goto failed;
2591 value = ast2obj_unaryop(o->v.UnaryOp.op);
2592 if (!value) goto failed;
2593 if (PyObject_SetAttrString(result, "op", value) == -1)
2594 goto failed;
2595 Py_DECREF(value);
2596 value = ast2obj_expr(o->v.UnaryOp.operand);
2597 if (!value) goto failed;
2598 if (PyObject_SetAttrString(result, "operand", value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 break;
2602 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002603 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2604 if (!result) goto failed;
2605 value = ast2obj_arguments(o->v.Lambda.args);
2606 if (!value) goto failed;
2607 if (PyObject_SetAttrString(result, "args", value) == -1)
2608 goto failed;
2609 Py_DECREF(value);
2610 value = ast2obj_expr(o->v.Lambda.body);
2611 if (!value) goto failed;
2612 if (PyObject_SetAttrString(result, "body", value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002616 case IfExp_kind:
2617 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2618 if (!result) goto failed;
2619 value = ast2obj_expr(o->v.IfExp.test);
2620 if (!value) goto failed;
2621 if (PyObject_SetAttrString(result, "test", value) == -1)
2622 goto failed;
2623 Py_DECREF(value);
2624 value = ast2obj_expr(o->v.IfExp.body);
2625 if (!value) goto failed;
2626 if (PyObject_SetAttrString(result, "body", value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
2629 value = ast2obj_expr(o->v.IfExp.orelse);
2630 if (!value) goto failed;
2631 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002636 result = PyType_GenericNew(Dict_type, NULL, NULL);
2637 if (!result) goto failed;
2638 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "keys", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
2643 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2644 if (!value) goto failed;
2645 if (PyObject_SetAttrString(result, "values", value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002649 case Set_kind:
2650 result = PyType_GenericNew(Set_type, NULL, NULL);
2651 if (!result) goto failed;
2652 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "elts", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002659 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2660 if (!result) goto failed;
2661 value = ast2obj_expr(o->v.ListComp.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.ListComp.generators,
2667 ast2obj_comprehension);
2668 if (!value) goto failed;
2669 if (PyObject_SetAttrString(result, "generators", value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002673 case SetComp_kind:
2674 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_expr(o->v.SetComp.elt);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "elt", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 value = ast2obj_list(o->v.SetComp.generators,
2682 ast2obj_comprehension);
2683 if (!value) goto failed;
2684 if (PyObject_SetAttrString(result, "generators", value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002688 case DictComp_kind:
2689 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2690 if (!result) goto failed;
2691 value = ast2obj_expr(o->v.DictComp.key);
2692 if (!value) goto failed;
2693 if (PyObject_SetAttrString(result, "key", value) == -1)
2694 goto failed;
2695 Py_DECREF(value);
2696 value = ast2obj_expr(o->v.DictComp.value);
2697 if (!value) goto failed;
2698 if (PyObject_SetAttrString(result, "value", value) == -1)
2699 goto failed;
2700 Py_DECREF(value);
2701 value = ast2obj_list(o->v.DictComp.generators,
2702 ast2obj_comprehension);
2703 if (!value) goto failed;
2704 if (PyObject_SetAttrString(result, "generators", value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
2707 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002709 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2710 if (!result) goto failed;
2711 value = ast2obj_expr(o->v.GeneratorExp.elt);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "elt", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
2716 value = ast2obj_list(o->v.GeneratorExp.generators,
2717 ast2obj_comprehension);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "generators", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 break;
2723 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002724 result = PyType_GenericNew(Yield_type, NULL, NULL);
2725 if (!result) goto failed;
2726 value = ast2obj_expr(o->v.Yield.value);
2727 if (!value) goto failed;
2728 if (PyObject_SetAttrString(result, "value", value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 break;
2732 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002733 result = PyType_GenericNew(Compare_type, NULL, NULL);
2734 if (!result) goto failed;
2735 value = ast2obj_expr(o->v.Compare.left);
2736 if (!value) goto failed;
2737 if (PyObject_SetAttrString(result, "left", value) == -1)
2738 goto failed;
2739 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002740 {
2741 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2742 value = PyList_New(n);
2743 if (!value) goto failed;
2744 for(i = 0; i < n; i++)
2745 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2746 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002747 if (!value) goto failed;
2748 if (PyObject_SetAttrString(result, "ops", value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2752 if (!value) goto failed;
2753 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 break;
2757 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002758 result = PyType_GenericNew(Call_type, NULL, NULL);
2759 if (!result) goto failed;
2760 value = ast2obj_expr(o->v.Call.func);
2761 if (!value) goto failed;
2762 if (PyObject_SetAttrString(result, "func", value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
2765 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2766 if (!value) goto failed;
2767 if (PyObject_SetAttrString(result, "args", value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
2770 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2771 if (!value) goto failed;
2772 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 value = ast2obj_expr(o->v.Call.starargs);
2776 if (!value) goto failed;
2777 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2778 goto failed;
2779 Py_DECREF(value);
2780 value = ast2obj_expr(o->v.Call.kwargs);
2781 if (!value) goto failed;
2782 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2783 goto failed;
2784 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002787 result = PyType_GenericNew(Num_type, NULL, NULL);
2788 if (!result) goto failed;
2789 value = ast2obj_object(o->v.Num.n);
2790 if (!value) goto failed;
2791 if (PyObject_SetAttrString(result, "n", value) == -1)
2792 goto failed;
2793 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 break;
2795 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002796 result = PyType_GenericNew(Str_type, NULL, NULL);
2797 if (!result) goto failed;
2798 value = ast2obj_string(o->v.Str.s);
2799 if (!value) goto failed;
2800 if (PyObject_SetAttrString(result, "s", value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002804 case Bytes_kind:
2805 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2806 if (!result) goto failed;
2807 value = ast2obj_string(o->v.Bytes.s);
2808 if (!value) goto failed;
2809 if (PyObject_SetAttrString(result, "s", value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
2812 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002813 case Ellipsis_kind:
2814 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2815 if (!result) goto failed;
2816 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002818 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2819 if (!result) goto failed;
2820 value = ast2obj_expr(o->v.Attribute.value);
2821 if (!value) goto failed;
2822 if (PyObject_SetAttrString(result, "value", value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
2825 value = ast2obj_identifier(o->v.Attribute.attr);
2826 if (!value) goto failed;
2827 if (PyObject_SetAttrString(result, "attr", value) == -1)
2828 goto failed;
2829 Py_DECREF(value);
2830 value = ast2obj_expr_context(o->v.Attribute.ctx);
2831 if (!value) goto failed;
2832 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2833 goto failed;
2834 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 break;
2836 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002837 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2838 if (!result) goto failed;
2839 value = ast2obj_expr(o->v.Subscript.value);
2840 if (!value) goto failed;
2841 if (PyObject_SetAttrString(result, "value", value) == -1)
2842 goto failed;
2843 Py_DECREF(value);
2844 value = ast2obj_slice(o->v.Subscript.slice);
2845 if (!value) goto failed;
2846 if (PyObject_SetAttrString(result, "slice", value) == -1)
2847 goto failed;
2848 Py_DECREF(value);
2849 value = ast2obj_expr_context(o->v.Subscript.ctx);
2850 if (!value) goto failed;
2851 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2852 goto failed;
2853 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002855 case Starred_kind:
2856 result = PyType_GenericNew(Starred_type, NULL, NULL);
2857 if (!result) goto failed;
2858 value = ast2obj_expr(o->v.Starred.value);
2859 if (!value) goto failed;
2860 if (PyObject_SetAttrString(result, "value", value) == -1)
2861 goto failed;
2862 Py_DECREF(value);
2863 value = ast2obj_expr_context(o->v.Starred.ctx);
2864 if (!value) goto failed;
2865 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2866 goto failed;
2867 Py_DECREF(value);
2868 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002870 result = PyType_GenericNew(Name_type, NULL, NULL);
2871 if (!result) goto failed;
2872 value = ast2obj_identifier(o->v.Name.id);
2873 if (!value) goto failed;
2874 if (PyObject_SetAttrString(result, "id", value) == -1)
2875 goto failed;
2876 Py_DECREF(value);
2877 value = ast2obj_expr_context(o->v.Name.ctx);
2878 if (!value) goto failed;
2879 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2880 goto failed;
2881 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 break;
2883 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002884 result = PyType_GenericNew(List_type, NULL, NULL);
2885 if (!result) goto failed;
2886 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2887 if (!value) goto failed;
2888 if (PyObject_SetAttrString(result, "elts", value) == -1)
2889 goto failed;
2890 Py_DECREF(value);
2891 value = ast2obj_expr_context(o->v.List.ctx);
2892 if (!value) goto failed;
2893 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 break;
2897 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002898 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2899 if (!result) goto failed;
2900 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2901 if (!value) goto failed;
2902 if (PyObject_SetAttrString(result, "elts", value) == -1)
2903 goto failed;
2904 Py_DECREF(value);
2905 value = ast2obj_expr_context(o->v.Tuple.ctx);
2906 if (!value) goto failed;
2907 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2908 goto failed;
2909 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 break;
2911 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002912 value = ast2obj_int(o->lineno);
2913 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002914 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2915 goto failed;
2916 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002917 value = ast2obj_int(o->col_offset);
2918 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002919 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2920 goto failed;
2921 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002922 return result;
2923failed:
2924 Py_XDECREF(value);
2925 Py_XDECREF(result);
2926 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927}
2928
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002929PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002931 switch(o) {
2932 case Load:
2933 Py_INCREF(Load_singleton);
2934 return Load_singleton;
2935 case Store:
2936 Py_INCREF(Store_singleton);
2937 return Store_singleton;
2938 case Del:
2939 Py_INCREF(Del_singleton);
2940 return Del_singleton;
2941 case AugLoad:
2942 Py_INCREF(AugLoad_singleton);
2943 return AugLoad_singleton;
2944 case AugStore:
2945 Py_INCREF(AugStore_singleton);
2946 return AugStore_singleton;
2947 case Param:
2948 Py_INCREF(Param_singleton);
2949 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002950 default:
2951 /* should never happen, but just in case ... */
2952 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002956PyObject*
2957ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002959 slice_ty o = (slice_ty)_o;
2960 PyObject *result = NULL, *value = NULL;
2961 if (!o) {
2962 Py_INCREF(Py_None);
2963 return Py_None;
2964 }
2965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002968 result = PyType_GenericNew(Slice_type, NULL, NULL);
2969 if (!result) goto failed;
2970 value = ast2obj_expr(o->v.Slice.lower);
2971 if (!value) goto failed;
2972 if (PyObject_SetAttrString(result, "lower", value) == -1)
2973 goto failed;
2974 Py_DECREF(value);
2975 value = ast2obj_expr(o->v.Slice.upper);
2976 if (!value) goto failed;
2977 if (PyObject_SetAttrString(result, "upper", value) == -1)
2978 goto failed;
2979 Py_DECREF(value);
2980 value = ast2obj_expr(o->v.Slice.step);
2981 if (!value) goto failed;
2982 if (PyObject_SetAttrString(result, "step", value) == -1)
2983 goto failed;
2984 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 break;
2986 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002987 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2988 if (!result) goto failed;
2989 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2990 if (!value) goto failed;
2991 if (PyObject_SetAttrString(result, "dims", value) == -1)
2992 goto failed;
2993 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 break;
2995 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002996 result = PyType_GenericNew(Index_type, NULL, NULL);
2997 if (!result) goto failed;
2998 value = ast2obj_expr(o->v.Index.value);
2999 if (!value) goto failed;
3000 if (PyObject_SetAttrString(result, "value", value) == -1)
3001 goto failed;
3002 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 break;
3004 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003005 return result;
3006failed:
3007 Py_XDECREF(value);
3008 Py_XDECREF(result);
3009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010}
3011
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003012PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003014 switch(o) {
3015 case And:
3016 Py_INCREF(And_singleton);
3017 return And_singleton;
3018 case Or:
3019 Py_INCREF(Or_singleton);
3020 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003021 default:
3022 /* should never happen, but just in case ... */
3023 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003026}
3027PyObject* ast2obj_operator(operator_ty o)
3028{
3029 switch(o) {
3030 case Add:
3031 Py_INCREF(Add_singleton);
3032 return Add_singleton;
3033 case Sub:
3034 Py_INCREF(Sub_singleton);
3035 return Sub_singleton;
3036 case Mult:
3037 Py_INCREF(Mult_singleton);
3038 return Mult_singleton;
3039 case Div:
3040 Py_INCREF(Div_singleton);
3041 return Div_singleton;
3042 case Mod:
3043 Py_INCREF(Mod_singleton);
3044 return Mod_singleton;
3045 case Pow:
3046 Py_INCREF(Pow_singleton);
3047 return Pow_singleton;
3048 case LShift:
3049 Py_INCREF(LShift_singleton);
3050 return LShift_singleton;
3051 case RShift:
3052 Py_INCREF(RShift_singleton);
3053 return RShift_singleton;
3054 case BitOr:
3055 Py_INCREF(BitOr_singleton);
3056 return BitOr_singleton;
3057 case BitXor:
3058 Py_INCREF(BitXor_singleton);
3059 return BitXor_singleton;
3060 case BitAnd:
3061 Py_INCREF(BitAnd_singleton);
3062 return BitAnd_singleton;
3063 case FloorDiv:
3064 Py_INCREF(FloorDiv_singleton);
3065 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003066 default:
3067 /* should never happen, but just in case ... */
3068 PyErr_Format(PyExc_SystemError, "unknown operator found");
3069 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003070 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003071}
3072PyObject* ast2obj_unaryop(unaryop_ty o)
3073{
3074 switch(o) {
3075 case Invert:
3076 Py_INCREF(Invert_singleton);
3077 return Invert_singleton;
3078 case Not:
3079 Py_INCREF(Not_singleton);
3080 return Not_singleton;
3081 case UAdd:
3082 Py_INCREF(UAdd_singleton);
3083 return UAdd_singleton;
3084 case USub:
3085 Py_INCREF(USub_singleton);
3086 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003087 default:
3088 /* should never happen, but just in case ... */
3089 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3090 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003091 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003092}
3093PyObject* ast2obj_cmpop(cmpop_ty o)
3094{
3095 switch(o) {
3096 case Eq:
3097 Py_INCREF(Eq_singleton);
3098 return Eq_singleton;
3099 case NotEq:
3100 Py_INCREF(NotEq_singleton);
3101 return NotEq_singleton;
3102 case Lt:
3103 Py_INCREF(Lt_singleton);
3104 return Lt_singleton;
3105 case LtE:
3106 Py_INCREF(LtE_singleton);
3107 return LtE_singleton;
3108 case Gt:
3109 Py_INCREF(Gt_singleton);
3110 return Gt_singleton;
3111 case GtE:
3112 Py_INCREF(GtE_singleton);
3113 return GtE_singleton;
3114 case Is:
3115 Py_INCREF(Is_singleton);
3116 return Is_singleton;
3117 case IsNot:
3118 Py_INCREF(IsNot_singleton);
3119 return IsNot_singleton;
3120 case In:
3121 Py_INCREF(In_singleton);
3122 return In_singleton;
3123 case NotIn:
3124 Py_INCREF(NotIn_singleton);
3125 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003126 default:
3127 /* should never happen, but just in case ... */
3128 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3129 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003130 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003131}
3132PyObject*
3133ast2obj_comprehension(void* _o)
3134{
3135 comprehension_ty o = (comprehension_ty)_o;
3136 PyObject *result = NULL, *value = NULL;
3137 if (!o) {
3138 Py_INCREF(Py_None);
3139 return Py_None;
3140 }
3141
3142 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3143 if (!result) return NULL;
3144 value = ast2obj_expr(o->target);
3145 if (!value) goto failed;
3146 if (PyObject_SetAttrString(result, "target", value) == -1)
3147 goto failed;
3148 Py_DECREF(value);
3149 value = ast2obj_expr(o->iter);
3150 if (!value) goto failed;
3151 if (PyObject_SetAttrString(result, "iter", value) == -1)
3152 goto failed;
3153 Py_DECREF(value);
3154 value = ast2obj_list(o->ifs, ast2obj_expr);
3155 if (!value) goto failed;
3156 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3157 goto failed;
3158 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003159 return result;
3160failed:
3161 Py_XDECREF(value);
3162 Py_XDECREF(result);
3163 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164}
3165
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003166PyObject*
3167ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003169 excepthandler_ty o = (excepthandler_ty)_o;
3170 PyObject *result = NULL, *value = NULL;
3171 if (!o) {
3172 Py_INCREF(Py_None);
3173 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003175
Neal Norwitzad74aa82008-03-31 05:14:30 +00003176 switch (o->kind) {
3177 case ExceptHandler_kind:
3178 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3179 if (!result) goto failed;
3180 value = ast2obj_expr(o->v.ExceptHandler.type);
3181 if (!value) goto failed;
3182 if (PyObject_SetAttrString(result, "type", value) == -1)
3183 goto failed;
3184 Py_DECREF(value);
3185 value = ast2obj_identifier(o->v.ExceptHandler.name);
3186 if (!value) goto failed;
3187 if (PyObject_SetAttrString(result, "name", value) == -1)
3188 goto failed;
3189 Py_DECREF(value);
3190 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3191 if (!value) goto failed;
3192 if (PyObject_SetAttrString(result, "body", value) == -1)
3193 goto failed;
3194 Py_DECREF(value);
3195 break;
3196 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003197 value = ast2obj_int(o->lineno);
3198 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003199 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003200 goto failed;
3201 Py_DECREF(value);
3202 value = ast2obj_int(o->col_offset);
3203 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003204 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003205 goto failed;
3206 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003207 return result;
3208failed:
3209 Py_XDECREF(value);
3210 Py_XDECREF(result);
3211 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212}
3213
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003214PyObject*
3215ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003217 arguments_ty o = (arguments_ty)_o;
3218 PyObject *result = NULL, *value = NULL;
3219 if (!o) {
3220 Py_INCREF(Py_None);
3221 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003223
3224 result = PyType_GenericNew(arguments_type, NULL, NULL);
3225 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003226 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003227 if (!value) goto failed;
3228 if (PyObject_SetAttrString(result, "args", value) == -1)
3229 goto failed;
3230 Py_DECREF(value);
3231 value = ast2obj_identifier(o->vararg);
3232 if (!value) goto failed;
3233 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3234 goto failed;
3235 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003236 value = ast2obj_expr(o->varargannotation);
3237 if (!value) goto failed;
3238 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3239 goto failed;
3240 Py_DECREF(value);
3241 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003242 if (!value) goto failed;
3243 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3244 goto failed;
3245 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003246 value = ast2obj_identifier(o->kwarg);
3247 if (!value) goto failed;
3248 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3249 goto failed;
3250 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003251 value = ast2obj_expr(o->kwargannotation);
3252 if (!value) goto failed;
3253 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3254 goto failed;
3255 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003256 value = ast2obj_list(o->defaults, ast2obj_expr);
3257 if (!value) goto failed;
3258 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3259 goto failed;
3260 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003261 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3262 if (!value) goto failed;
3263 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3264 goto failed;
3265 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003266 return result;
3267failed:
3268 Py_XDECREF(value);
3269 Py_XDECREF(result);
3270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271}
3272
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003273PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003274ast2obj_arg(void* _o)
3275{
3276 arg_ty o = (arg_ty)_o;
3277 PyObject *result = NULL, *value = NULL;
3278 if (!o) {
3279 Py_INCREF(Py_None);
3280 return Py_None;
3281 }
3282
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003283 result = PyType_GenericNew(arg_type, NULL, NULL);
3284 if (!result) return NULL;
3285 value = ast2obj_identifier(o->arg);
3286 if (!value) goto failed;
3287 if (PyObject_SetAttrString(result, "arg", value) == -1)
3288 goto failed;
3289 Py_DECREF(value);
3290 value = ast2obj_expr(o->annotation);
3291 if (!value) goto failed;
3292 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3293 goto failed;
3294 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003295 return result;
3296failed:
3297 Py_XDECREF(value);
3298 Py_XDECREF(result);
3299 return NULL;
3300}
3301
3302PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003303ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003305 keyword_ty o = (keyword_ty)_o;
3306 PyObject *result = NULL, *value = NULL;
3307 if (!o) {
3308 Py_INCREF(Py_None);
3309 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003311
3312 result = PyType_GenericNew(keyword_type, NULL, NULL);
3313 if (!result) return NULL;
3314 value = ast2obj_identifier(o->arg);
3315 if (!value) goto failed;
3316 if (PyObject_SetAttrString(result, "arg", value) == -1)
3317 goto failed;
3318 Py_DECREF(value);
3319 value = ast2obj_expr(o->value);
3320 if (!value) goto failed;
3321 if (PyObject_SetAttrString(result, "value", value) == -1)
3322 goto failed;
3323 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003324 return result;
3325failed:
3326 Py_XDECREF(value);
3327 Py_XDECREF(result);
3328 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329}
3330
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003331PyObject*
3332ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003334 alias_ty o = (alias_ty)_o;
3335 PyObject *result = NULL, *value = NULL;
3336 if (!o) {
3337 Py_INCREF(Py_None);
3338 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003340
3341 result = PyType_GenericNew(alias_type, NULL, NULL);
3342 if (!result) return NULL;
3343 value = ast2obj_identifier(o->name);
3344 if (!value) goto failed;
3345 if (PyObject_SetAttrString(result, "name", value) == -1)
3346 goto failed;
3347 Py_DECREF(value);
3348 value = ast2obj_identifier(o->asname);
3349 if (!value) goto failed;
3350 if (PyObject_SetAttrString(result, "asname", value) == -1)
3351 goto failed;
3352 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003353 return result;
3354failed:
3355 Py_XDECREF(value);
3356 Py_XDECREF(result);
3357 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358}
3359
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003360PyObject*
3361ast2obj_withitem(void* _o)
3362{
3363 withitem_ty o = (withitem_ty)_o;
3364 PyObject *result = NULL, *value = NULL;
3365 if (!o) {
3366 Py_INCREF(Py_None);
3367 return Py_None;
3368 }
3369
3370 result = PyType_GenericNew(withitem_type, NULL, NULL);
3371 if (!result) return NULL;
3372 value = ast2obj_expr(o->context_expr);
3373 if (!value) goto failed;
3374 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
3375 goto failed;
3376 Py_DECREF(value);
3377 value = ast2obj_expr(o->optional_vars);
3378 if (!value) goto failed;
3379 if (PyObject_SetAttrString(result, "optional_vars", value) == -1)
3380 goto failed;
3381 Py_DECREF(value);
3382 return result;
3383failed:
3384 Py_XDECREF(value);
3385 Py_XDECREF(result);
3386 return NULL;
3387}
3388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003390int
3391obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3392{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003393 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003394
Benjamin Petersond8f65972010-11-20 04:31:07 +00003395 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003396
3397 if (obj == Py_None) {
3398 *out = NULL;
3399 return 0;
3400 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003401 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3402 if (isinstance == -1) {
3403 return 1;
3404 }
3405 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003406 asdl_seq* body;
3407
3408 if (PyObject_HasAttrString(obj, "body")) {
3409 int res;
3410 Py_ssize_t len;
3411 Py_ssize_t i;
3412 tmp = PyObject_GetAttrString(obj, "body");
3413 if (tmp == NULL) goto failed;
3414 if (!PyList_Check(tmp)) {
3415 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3416 goto failed;
3417 }
3418 len = PyList_GET_SIZE(tmp);
3419 body = asdl_seq_new(len, arena);
3420 if (body == NULL) goto failed;
3421 for (i = 0; i < len; i++) {
3422 stmt_ty value;
3423 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3424 if (res != 0) goto failed;
3425 asdl_seq_SET(body, i, value);
3426 }
3427 Py_XDECREF(tmp);
3428 tmp = NULL;
3429 } else {
3430 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3431 return 1;
3432 }
3433 *out = Module(body, arena);
3434 if (*out == NULL) goto failed;
3435 return 0;
3436 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003437 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3438 if (isinstance == -1) {
3439 return 1;
3440 }
3441 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003442 asdl_seq* body;
3443
3444 if (PyObject_HasAttrString(obj, "body")) {
3445 int res;
3446 Py_ssize_t len;
3447 Py_ssize_t i;
3448 tmp = PyObject_GetAttrString(obj, "body");
3449 if (tmp == NULL) goto failed;
3450 if (!PyList_Check(tmp)) {
3451 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3452 goto failed;
3453 }
3454 len = PyList_GET_SIZE(tmp);
3455 body = asdl_seq_new(len, arena);
3456 if (body == NULL) goto failed;
3457 for (i = 0; i < len; i++) {
3458 stmt_ty value;
3459 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3460 if (res != 0) goto failed;
3461 asdl_seq_SET(body, i, value);
3462 }
3463 Py_XDECREF(tmp);
3464 tmp = NULL;
3465 } else {
3466 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3467 return 1;
3468 }
3469 *out = Interactive(body, arena);
3470 if (*out == NULL) goto failed;
3471 return 0;
3472 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003473 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3474 if (isinstance == -1) {
3475 return 1;
3476 }
3477 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003478 expr_ty body;
3479
3480 if (PyObject_HasAttrString(obj, "body")) {
3481 int res;
3482 tmp = PyObject_GetAttrString(obj, "body");
3483 if (tmp == NULL) goto failed;
3484 res = obj2ast_expr(tmp, &body, arena);
3485 if (res != 0) goto failed;
3486 Py_XDECREF(tmp);
3487 tmp = NULL;
3488 } else {
3489 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3490 return 1;
3491 }
3492 *out = Expression(body, arena);
3493 if (*out == NULL) goto failed;
3494 return 0;
3495 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003496 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3497 if (isinstance == -1) {
3498 return 1;
3499 }
3500 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003501 asdl_seq* body;
3502
3503 if (PyObject_HasAttrString(obj, "body")) {
3504 int res;
3505 Py_ssize_t len;
3506 Py_ssize_t i;
3507 tmp = PyObject_GetAttrString(obj, "body");
3508 if (tmp == NULL) goto failed;
3509 if (!PyList_Check(tmp)) {
3510 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3511 goto failed;
3512 }
3513 len = PyList_GET_SIZE(tmp);
3514 body = asdl_seq_new(len, arena);
3515 if (body == NULL) goto failed;
3516 for (i = 0; i < len; i++) {
3517 stmt_ty value;
3518 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3519 if (res != 0) goto failed;
3520 asdl_seq_SET(body, i, value);
3521 }
3522 Py_XDECREF(tmp);
3523 tmp = NULL;
3524 } else {
3525 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3526 return 1;
3527 }
3528 *out = Suite(body, arena);
3529 if (*out == NULL) goto failed;
3530 return 0;
3531 }
3532
Benjamin Peterson5b066812010-11-20 01:38:49 +00003533 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3534 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003535 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003536 return 1;
3537}
3538
3539int
3540obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3541{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003542 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003543
Benjamin Petersond8f65972010-11-20 04:31:07 +00003544 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003545 int lineno;
3546 int col_offset;
3547
3548 if (obj == Py_None) {
3549 *out = NULL;
3550 return 0;
3551 }
3552 if (PyObject_HasAttrString(obj, "lineno")) {
3553 int res;
3554 tmp = PyObject_GetAttrString(obj, "lineno");
3555 if (tmp == NULL) goto failed;
3556 res = obj2ast_int(tmp, &lineno, arena);
3557 if (res != 0) goto failed;
3558 Py_XDECREF(tmp);
3559 tmp = NULL;
3560 } else {
3561 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3562 return 1;
3563 }
3564 if (PyObject_HasAttrString(obj, "col_offset")) {
3565 int res;
3566 tmp = PyObject_GetAttrString(obj, "col_offset");
3567 if (tmp == NULL) goto failed;
3568 res = obj2ast_int(tmp, &col_offset, arena);
3569 if (res != 0) goto failed;
3570 Py_XDECREF(tmp);
3571 tmp = NULL;
3572 } else {
3573 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3574 return 1;
3575 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003576 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3577 if (isinstance == -1) {
3578 return 1;
3579 }
3580 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003581 identifier name;
3582 arguments_ty args;
3583 asdl_seq* body;
3584 asdl_seq* decorator_list;
3585 expr_ty returns;
3586
3587 if (PyObject_HasAttrString(obj, "name")) {
3588 int res;
3589 tmp = PyObject_GetAttrString(obj, "name");
3590 if (tmp == NULL) goto failed;
3591 res = obj2ast_identifier(tmp, &name, arena);
3592 if (res != 0) goto failed;
3593 Py_XDECREF(tmp);
3594 tmp = NULL;
3595 } else {
3596 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3597 return 1;
3598 }
3599 if (PyObject_HasAttrString(obj, "args")) {
3600 int res;
3601 tmp = PyObject_GetAttrString(obj, "args");
3602 if (tmp == NULL) goto failed;
3603 res = obj2ast_arguments(tmp, &args, arena);
3604 if (res != 0) goto failed;
3605 Py_XDECREF(tmp);
3606 tmp = NULL;
3607 } else {
3608 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3609 return 1;
3610 }
3611 if (PyObject_HasAttrString(obj, "body")) {
3612 int res;
3613 Py_ssize_t len;
3614 Py_ssize_t i;
3615 tmp = PyObject_GetAttrString(obj, "body");
3616 if (tmp == NULL) goto failed;
3617 if (!PyList_Check(tmp)) {
3618 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3619 goto failed;
3620 }
3621 len = PyList_GET_SIZE(tmp);
3622 body = asdl_seq_new(len, arena);
3623 if (body == NULL) goto failed;
3624 for (i = 0; i < len; i++) {
3625 stmt_ty value;
3626 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3627 if (res != 0) goto failed;
3628 asdl_seq_SET(body, i, value);
3629 }
3630 Py_XDECREF(tmp);
3631 tmp = NULL;
3632 } else {
3633 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3634 return 1;
3635 }
3636 if (PyObject_HasAttrString(obj, "decorator_list")) {
3637 int res;
3638 Py_ssize_t len;
3639 Py_ssize_t i;
3640 tmp = PyObject_GetAttrString(obj, "decorator_list");
3641 if (tmp == NULL) goto failed;
3642 if (!PyList_Check(tmp)) {
3643 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3644 goto failed;
3645 }
3646 len = PyList_GET_SIZE(tmp);
3647 decorator_list = asdl_seq_new(len, arena);
3648 if (decorator_list == NULL) goto failed;
3649 for (i = 0; i < len; i++) {
3650 expr_ty value;
3651 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3652 if (res != 0) goto failed;
3653 asdl_seq_SET(decorator_list, i, value);
3654 }
3655 Py_XDECREF(tmp);
3656 tmp = NULL;
3657 } else {
3658 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3659 return 1;
3660 }
3661 if (PyObject_HasAttrString(obj, "returns")) {
3662 int res;
3663 tmp = PyObject_GetAttrString(obj, "returns");
3664 if (tmp == NULL) goto failed;
3665 res = obj2ast_expr(tmp, &returns, arena);
3666 if (res != 0) goto failed;
3667 Py_XDECREF(tmp);
3668 tmp = NULL;
3669 } else {
3670 returns = NULL;
3671 }
3672 *out = FunctionDef(name, args, body, decorator_list, returns,
3673 lineno, col_offset, arena);
3674 if (*out == NULL) goto failed;
3675 return 0;
3676 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003677 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3678 if (isinstance == -1) {
3679 return 1;
3680 }
3681 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003682 identifier name;
3683 asdl_seq* bases;
3684 asdl_seq* keywords;
3685 expr_ty starargs;
3686 expr_ty kwargs;
3687 asdl_seq* body;
3688 asdl_seq* decorator_list;
3689
3690 if (PyObject_HasAttrString(obj, "name")) {
3691 int res;
3692 tmp = PyObject_GetAttrString(obj, "name");
3693 if (tmp == NULL) goto failed;
3694 res = obj2ast_identifier(tmp, &name, arena);
3695 if (res != 0) goto failed;
3696 Py_XDECREF(tmp);
3697 tmp = NULL;
3698 } else {
3699 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3700 return 1;
3701 }
3702 if (PyObject_HasAttrString(obj, "bases")) {
3703 int res;
3704 Py_ssize_t len;
3705 Py_ssize_t i;
3706 tmp = PyObject_GetAttrString(obj, "bases");
3707 if (tmp == NULL) goto failed;
3708 if (!PyList_Check(tmp)) {
3709 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3710 goto failed;
3711 }
3712 len = PyList_GET_SIZE(tmp);
3713 bases = asdl_seq_new(len, arena);
3714 if (bases == NULL) goto failed;
3715 for (i = 0; i < len; i++) {
3716 expr_ty value;
3717 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3718 if (res != 0) goto failed;
3719 asdl_seq_SET(bases, i, value);
3720 }
3721 Py_XDECREF(tmp);
3722 tmp = NULL;
3723 } else {
3724 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3725 return 1;
3726 }
3727 if (PyObject_HasAttrString(obj, "keywords")) {
3728 int res;
3729 Py_ssize_t len;
3730 Py_ssize_t i;
3731 tmp = PyObject_GetAttrString(obj, "keywords");
3732 if (tmp == NULL) goto failed;
3733 if (!PyList_Check(tmp)) {
3734 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3735 goto failed;
3736 }
3737 len = PyList_GET_SIZE(tmp);
3738 keywords = asdl_seq_new(len, arena);
3739 if (keywords == NULL) goto failed;
3740 for (i = 0; i < len; i++) {
3741 keyword_ty value;
3742 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3743 if (res != 0) goto failed;
3744 asdl_seq_SET(keywords, i, value);
3745 }
3746 Py_XDECREF(tmp);
3747 tmp = NULL;
3748 } else {
3749 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3750 return 1;
3751 }
3752 if (PyObject_HasAttrString(obj, "starargs")) {
3753 int res;
3754 tmp = PyObject_GetAttrString(obj, "starargs");
3755 if (tmp == NULL) goto failed;
3756 res = obj2ast_expr(tmp, &starargs, arena);
3757 if (res != 0) goto failed;
3758 Py_XDECREF(tmp);
3759 tmp = NULL;
3760 } else {
3761 starargs = NULL;
3762 }
3763 if (PyObject_HasAttrString(obj, "kwargs")) {
3764 int res;
3765 tmp = PyObject_GetAttrString(obj, "kwargs");
3766 if (tmp == NULL) goto failed;
3767 res = obj2ast_expr(tmp, &kwargs, arena);
3768 if (res != 0) goto failed;
3769 Py_XDECREF(tmp);
3770 tmp = NULL;
3771 } else {
3772 kwargs = NULL;
3773 }
3774 if (PyObject_HasAttrString(obj, "body")) {
3775 int res;
3776 Py_ssize_t len;
3777 Py_ssize_t i;
3778 tmp = PyObject_GetAttrString(obj, "body");
3779 if (tmp == NULL) goto failed;
3780 if (!PyList_Check(tmp)) {
3781 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3782 goto failed;
3783 }
3784 len = PyList_GET_SIZE(tmp);
3785 body = asdl_seq_new(len, arena);
3786 if (body == NULL) goto failed;
3787 for (i = 0; i < len; i++) {
3788 stmt_ty value;
3789 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3790 if (res != 0) goto failed;
3791 asdl_seq_SET(body, i, value);
3792 }
3793 Py_XDECREF(tmp);
3794 tmp = NULL;
3795 } else {
3796 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3797 return 1;
3798 }
3799 if (PyObject_HasAttrString(obj, "decorator_list")) {
3800 int res;
3801 Py_ssize_t len;
3802 Py_ssize_t i;
3803 tmp = PyObject_GetAttrString(obj, "decorator_list");
3804 if (tmp == NULL) goto failed;
3805 if (!PyList_Check(tmp)) {
3806 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3807 goto failed;
3808 }
3809 len = PyList_GET_SIZE(tmp);
3810 decorator_list = asdl_seq_new(len, arena);
3811 if (decorator_list == NULL) goto failed;
3812 for (i = 0; i < len; i++) {
3813 expr_ty value;
3814 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3815 if (res != 0) goto failed;
3816 asdl_seq_SET(decorator_list, i, value);
3817 }
3818 Py_XDECREF(tmp);
3819 tmp = NULL;
3820 } else {
3821 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3822 return 1;
3823 }
3824 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3825 decorator_list, lineno, col_offset, arena);
3826 if (*out == NULL) goto failed;
3827 return 0;
3828 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003829 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3830 if (isinstance == -1) {
3831 return 1;
3832 }
3833 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003834 expr_ty value;
3835
3836 if (PyObject_HasAttrString(obj, "value")) {
3837 int res;
3838 tmp = PyObject_GetAttrString(obj, "value");
3839 if (tmp == NULL) goto failed;
3840 res = obj2ast_expr(tmp, &value, arena);
3841 if (res != 0) goto failed;
3842 Py_XDECREF(tmp);
3843 tmp = NULL;
3844 } else {
3845 value = NULL;
3846 }
3847 *out = Return(value, lineno, col_offset, arena);
3848 if (*out == NULL) goto failed;
3849 return 0;
3850 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003851 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3852 if (isinstance == -1) {
3853 return 1;
3854 }
3855 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003856 asdl_seq* targets;
3857
3858 if (PyObject_HasAttrString(obj, "targets")) {
3859 int res;
3860 Py_ssize_t len;
3861 Py_ssize_t i;
3862 tmp = PyObject_GetAttrString(obj, "targets");
3863 if (tmp == NULL) goto failed;
3864 if (!PyList_Check(tmp)) {
3865 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3866 goto failed;
3867 }
3868 len = PyList_GET_SIZE(tmp);
3869 targets = asdl_seq_new(len, arena);
3870 if (targets == NULL) goto failed;
3871 for (i = 0; i < len; i++) {
3872 expr_ty value;
3873 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3874 if (res != 0) goto failed;
3875 asdl_seq_SET(targets, i, value);
3876 }
3877 Py_XDECREF(tmp);
3878 tmp = NULL;
3879 } else {
3880 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3881 return 1;
3882 }
3883 *out = Delete(targets, lineno, col_offset, arena);
3884 if (*out == NULL) goto failed;
3885 return 0;
3886 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003887 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3888 if (isinstance == -1) {
3889 return 1;
3890 }
3891 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003892 asdl_seq* targets;
3893 expr_ty value;
3894
3895 if (PyObject_HasAttrString(obj, "targets")) {
3896 int res;
3897 Py_ssize_t len;
3898 Py_ssize_t i;
3899 tmp = PyObject_GetAttrString(obj, "targets");
3900 if (tmp == NULL) goto failed;
3901 if (!PyList_Check(tmp)) {
3902 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3903 goto failed;
3904 }
3905 len = PyList_GET_SIZE(tmp);
3906 targets = asdl_seq_new(len, arena);
3907 if (targets == NULL) goto failed;
3908 for (i = 0; i < len; i++) {
3909 expr_ty value;
3910 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3911 if (res != 0) goto failed;
3912 asdl_seq_SET(targets, i, value);
3913 }
3914 Py_XDECREF(tmp);
3915 tmp = NULL;
3916 } else {
3917 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3918 return 1;
3919 }
3920 if (PyObject_HasAttrString(obj, "value")) {
3921 int res;
3922 tmp = PyObject_GetAttrString(obj, "value");
3923 if (tmp == NULL) goto failed;
3924 res = obj2ast_expr(tmp, &value, arena);
3925 if (res != 0) goto failed;
3926 Py_XDECREF(tmp);
3927 tmp = NULL;
3928 } else {
3929 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3930 return 1;
3931 }
3932 *out = Assign(targets, value, lineno, col_offset, arena);
3933 if (*out == NULL) goto failed;
3934 return 0;
3935 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003936 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3937 if (isinstance == -1) {
3938 return 1;
3939 }
3940 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003941 expr_ty target;
3942 operator_ty op;
3943 expr_ty value;
3944
3945 if (PyObject_HasAttrString(obj, "target")) {
3946 int res;
3947 tmp = PyObject_GetAttrString(obj, "target");
3948 if (tmp == NULL) goto failed;
3949 res = obj2ast_expr(tmp, &target, arena);
3950 if (res != 0) goto failed;
3951 Py_XDECREF(tmp);
3952 tmp = NULL;
3953 } else {
3954 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3955 return 1;
3956 }
3957 if (PyObject_HasAttrString(obj, "op")) {
3958 int res;
3959 tmp = PyObject_GetAttrString(obj, "op");
3960 if (tmp == NULL) goto failed;
3961 res = obj2ast_operator(tmp, &op, arena);
3962 if (res != 0) goto failed;
3963 Py_XDECREF(tmp);
3964 tmp = NULL;
3965 } else {
3966 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3967 return 1;
3968 }
3969 if (PyObject_HasAttrString(obj, "value")) {
3970 int res;
3971 tmp = PyObject_GetAttrString(obj, "value");
3972 if (tmp == NULL) goto failed;
3973 res = obj2ast_expr(tmp, &value, arena);
3974 if (res != 0) goto failed;
3975 Py_XDECREF(tmp);
3976 tmp = NULL;
3977 } else {
3978 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3979 return 1;
3980 }
3981 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3982 if (*out == NULL) goto failed;
3983 return 0;
3984 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003985 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3986 if (isinstance == -1) {
3987 return 1;
3988 }
3989 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003990 expr_ty target;
3991 expr_ty iter;
3992 asdl_seq* body;
3993 asdl_seq* orelse;
3994
3995 if (PyObject_HasAttrString(obj, "target")) {
3996 int res;
3997 tmp = PyObject_GetAttrString(obj, "target");
3998 if (tmp == NULL) goto failed;
3999 res = obj2ast_expr(tmp, &target, arena);
4000 if (res != 0) goto failed;
4001 Py_XDECREF(tmp);
4002 tmp = NULL;
4003 } else {
4004 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4005 return 1;
4006 }
4007 if (PyObject_HasAttrString(obj, "iter")) {
4008 int res;
4009 tmp = PyObject_GetAttrString(obj, "iter");
4010 if (tmp == NULL) goto failed;
4011 res = obj2ast_expr(tmp, &iter, arena);
4012 if (res != 0) goto failed;
4013 Py_XDECREF(tmp);
4014 tmp = NULL;
4015 } else {
4016 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4017 return 1;
4018 }
4019 if (PyObject_HasAttrString(obj, "body")) {
4020 int res;
4021 Py_ssize_t len;
4022 Py_ssize_t i;
4023 tmp = PyObject_GetAttrString(obj, "body");
4024 if (tmp == NULL) goto failed;
4025 if (!PyList_Check(tmp)) {
4026 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4027 goto failed;
4028 }
4029 len = PyList_GET_SIZE(tmp);
4030 body = asdl_seq_new(len, arena);
4031 if (body == NULL) goto failed;
4032 for (i = 0; i < len; i++) {
4033 stmt_ty value;
4034 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4035 if (res != 0) goto failed;
4036 asdl_seq_SET(body, i, value);
4037 }
4038 Py_XDECREF(tmp);
4039 tmp = NULL;
4040 } else {
4041 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4042 return 1;
4043 }
4044 if (PyObject_HasAttrString(obj, "orelse")) {
4045 int res;
4046 Py_ssize_t len;
4047 Py_ssize_t i;
4048 tmp = PyObject_GetAttrString(obj, "orelse");
4049 if (tmp == NULL) goto failed;
4050 if (!PyList_Check(tmp)) {
4051 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4052 goto failed;
4053 }
4054 len = PyList_GET_SIZE(tmp);
4055 orelse = asdl_seq_new(len, arena);
4056 if (orelse == NULL) goto failed;
4057 for (i = 0; i < len; i++) {
4058 stmt_ty value;
4059 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4060 if (res != 0) goto failed;
4061 asdl_seq_SET(orelse, i, value);
4062 }
4063 Py_XDECREF(tmp);
4064 tmp = NULL;
4065 } else {
4066 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4067 return 1;
4068 }
4069 *out = For(target, iter, body, orelse, lineno, col_offset,
4070 arena);
4071 if (*out == NULL) goto failed;
4072 return 0;
4073 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004074 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4075 if (isinstance == -1) {
4076 return 1;
4077 }
4078 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004079 expr_ty test;
4080 asdl_seq* body;
4081 asdl_seq* orelse;
4082
4083 if (PyObject_HasAttrString(obj, "test")) {
4084 int res;
4085 tmp = PyObject_GetAttrString(obj, "test");
4086 if (tmp == NULL) goto failed;
4087 res = obj2ast_expr(tmp, &test, arena);
4088 if (res != 0) goto failed;
4089 Py_XDECREF(tmp);
4090 tmp = NULL;
4091 } else {
4092 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4093 return 1;
4094 }
4095 if (PyObject_HasAttrString(obj, "body")) {
4096 int res;
4097 Py_ssize_t len;
4098 Py_ssize_t i;
4099 tmp = PyObject_GetAttrString(obj, "body");
4100 if (tmp == NULL) goto failed;
4101 if (!PyList_Check(tmp)) {
4102 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4103 goto failed;
4104 }
4105 len = PyList_GET_SIZE(tmp);
4106 body = asdl_seq_new(len, arena);
4107 if (body == NULL) goto failed;
4108 for (i = 0; i < len; i++) {
4109 stmt_ty value;
4110 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4111 if (res != 0) goto failed;
4112 asdl_seq_SET(body, i, value);
4113 }
4114 Py_XDECREF(tmp);
4115 tmp = NULL;
4116 } else {
4117 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4118 return 1;
4119 }
4120 if (PyObject_HasAttrString(obj, "orelse")) {
4121 int res;
4122 Py_ssize_t len;
4123 Py_ssize_t i;
4124 tmp = PyObject_GetAttrString(obj, "orelse");
4125 if (tmp == NULL) goto failed;
4126 if (!PyList_Check(tmp)) {
4127 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4128 goto failed;
4129 }
4130 len = PyList_GET_SIZE(tmp);
4131 orelse = asdl_seq_new(len, arena);
4132 if (orelse == NULL) goto failed;
4133 for (i = 0; i < len; i++) {
4134 stmt_ty value;
4135 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4136 if (res != 0) goto failed;
4137 asdl_seq_SET(orelse, i, value);
4138 }
4139 Py_XDECREF(tmp);
4140 tmp = NULL;
4141 } else {
4142 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4143 return 1;
4144 }
4145 *out = While(test, body, orelse, lineno, col_offset, arena);
4146 if (*out == NULL) goto failed;
4147 return 0;
4148 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004149 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4150 if (isinstance == -1) {
4151 return 1;
4152 }
4153 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004154 expr_ty test;
4155 asdl_seq* body;
4156 asdl_seq* orelse;
4157
4158 if (PyObject_HasAttrString(obj, "test")) {
4159 int res;
4160 tmp = PyObject_GetAttrString(obj, "test");
4161 if (tmp == NULL) goto failed;
4162 res = obj2ast_expr(tmp, &test, arena);
4163 if (res != 0) goto failed;
4164 Py_XDECREF(tmp);
4165 tmp = NULL;
4166 } else {
4167 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4168 return 1;
4169 }
4170 if (PyObject_HasAttrString(obj, "body")) {
4171 int res;
4172 Py_ssize_t len;
4173 Py_ssize_t i;
4174 tmp = PyObject_GetAttrString(obj, "body");
4175 if (tmp == NULL) goto failed;
4176 if (!PyList_Check(tmp)) {
4177 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4178 goto failed;
4179 }
4180 len = PyList_GET_SIZE(tmp);
4181 body = asdl_seq_new(len, arena);
4182 if (body == NULL) goto failed;
4183 for (i = 0; i < len; i++) {
4184 stmt_ty value;
4185 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4186 if (res != 0) goto failed;
4187 asdl_seq_SET(body, i, value);
4188 }
4189 Py_XDECREF(tmp);
4190 tmp = NULL;
4191 } else {
4192 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4193 return 1;
4194 }
4195 if (PyObject_HasAttrString(obj, "orelse")) {
4196 int res;
4197 Py_ssize_t len;
4198 Py_ssize_t i;
4199 tmp = PyObject_GetAttrString(obj, "orelse");
4200 if (tmp == NULL) goto failed;
4201 if (!PyList_Check(tmp)) {
4202 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4203 goto failed;
4204 }
4205 len = PyList_GET_SIZE(tmp);
4206 orelse = asdl_seq_new(len, arena);
4207 if (orelse == NULL) goto failed;
4208 for (i = 0; i < len; i++) {
4209 stmt_ty value;
4210 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4211 if (res != 0) goto failed;
4212 asdl_seq_SET(orelse, i, value);
4213 }
4214 Py_XDECREF(tmp);
4215 tmp = NULL;
4216 } else {
4217 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4218 return 1;
4219 }
4220 *out = If(test, body, orelse, lineno, col_offset, arena);
4221 if (*out == NULL) goto failed;
4222 return 0;
4223 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004224 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4225 if (isinstance == -1) {
4226 return 1;
4227 }
4228 if (isinstance) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004229 asdl_seq* items;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004230 asdl_seq* body;
4231
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004232 if (PyObject_HasAttrString(obj, "items")) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004233 int res;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004234 Py_ssize_t len;
4235 Py_ssize_t i;
4236 tmp = PyObject_GetAttrString(obj, "items");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004237 if (tmp == NULL) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004238 if (!PyList_Check(tmp)) {
4239 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4240 goto failed;
4241 }
4242 len = PyList_GET_SIZE(tmp);
4243 items = asdl_seq_new(len, arena);
4244 if (items == NULL) goto failed;
4245 for (i = 0; i < len; i++) {
4246 withitem_ty value;
4247 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4248 if (res != 0) goto failed;
4249 asdl_seq_SET(items, i, value);
4250 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004251 Py_XDECREF(tmp);
4252 tmp = NULL;
4253 } else {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004254 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004255 return 1;
4256 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004257 if (PyObject_HasAttrString(obj, "body")) {
4258 int res;
4259 Py_ssize_t len;
4260 Py_ssize_t i;
4261 tmp = PyObject_GetAttrString(obj, "body");
4262 if (tmp == NULL) goto failed;
4263 if (!PyList_Check(tmp)) {
4264 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4265 goto failed;
4266 }
4267 len = PyList_GET_SIZE(tmp);
4268 body = asdl_seq_new(len, arena);
4269 if (body == NULL) goto failed;
4270 for (i = 0; i < len; i++) {
4271 stmt_ty value;
4272 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4273 if (res != 0) goto failed;
4274 asdl_seq_SET(body, i, value);
4275 }
4276 Py_XDECREF(tmp);
4277 tmp = NULL;
4278 } else {
4279 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4280 return 1;
4281 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004282 *out = With(items, body, lineno, col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004283 if (*out == NULL) goto failed;
4284 return 0;
4285 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004286 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4287 if (isinstance == -1) {
4288 return 1;
4289 }
4290 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004291 expr_ty exc;
4292 expr_ty cause;
4293
4294 if (PyObject_HasAttrString(obj, "exc")) {
4295 int res;
4296 tmp = PyObject_GetAttrString(obj, "exc");
4297 if (tmp == NULL) goto failed;
4298 res = obj2ast_expr(tmp, &exc, arena);
4299 if (res != 0) goto failed;
4300 Py_XDECREF(tmp);
4301 tmp = NULL;
4302 } else {
4303 exc = NULL;
4304 }
4305 if (PyObject_HasAttrString(obj, "cause")) {
4306 int res;
4307 tmp = PyObject_GetAttrString(obj, "cause");
4308 if (tmp == NULL) goto failed;
4309 res = obj2ast_expr(tmp, &cause, arena);
4310 if (res != 0) goto failed;
4311 Py_XDECREF(tmp);
4312 tmp = NULL;
4313 } else {
4314 cause = NULL;
4315 }
4316 *out = Raise(exc, cause, lineno, col_offset, arena);
4317 if (*out == NULL) goto failed;
4318 return 0;
4319 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004320 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004321 if (isinstance == -1) {
4322 return 1;
4323 }
4324 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004325 asdl_seq* body;
4326 asdl_seq* handlers;
4327 asdl_seq* orelse;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004328 asdl_seq* finalbody;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004329
4330 if (PyObject_HasAttrString(obj, "body")) {
4331 int res;
4332 Py_ssize_t len;
4333 Py_ssize_t i;
4334 tmp = PyObject_GetAttrString(obj, "body");
4335 if (tmp == NULL) goto failed;
4336 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004337 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 +00004338 goto failed;
4339 }
4340 len = PyList_GET_SIZE(tmp);
4341 body = asdl_seq_new(len, arena);
4342 if (body == NULL) goto failed;
4343 for (i = 0; i < len; i++) {
4344 stmt_ty value;
4345 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4346 if (res != 0) goto failed;
4347 asdl_seq_SET(body, i, value);
4348 }
4349 Py_XDECREF(tmp);
4350 tmp = NULL;
4351 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004352 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004353 return 1;
4354 }
4355 if (PyObject_HasAttrString(obj, "handlers")) {
4356 int res;
4357 Py_ssize_t len;
4358 Py_ssize_t i;
4359 tmp = PyObject_GetAttrString(obj, "handlers");
4360 if (tmp == NULL) goto failed;
4361 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004362 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 +00004363 goto failed;
4364 }
4365 len = PyList_GET_SIZE(tmp);
4366 handlers = asdl_seq_new(len, arena);
4367 if (handlers == NULL) goto failed;
4368 for (i = 0; i < len; i++) {
4369 excepthandler_ty value;
4370 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4371 if (res != 0) goto failed;
4372 asdl_seq_SET(handlers, i, value);
4373 }
4374 Py_XDECREF(tmp);
4375 tmp = NULL;
4376 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004377 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004378 return 1;
4379 }
4380 if (PyObject_HasAttrString(obj, "orelse")) {
4381 int res;
4382 Py_ssize_t len;
4383 Py_ssize_t i;
4384 tmp = PyObject_GetAttrString(obj, "orelse");
4385 if (tmp == NULL) goto failed;
4386 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004387 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 +00004388 goto failed;
4389 }
4390 len = PyList_GET_SIZE(tmp);
4391 orelse = asdl_seq_new(len, arena);
4392 if (orelse == NULL) goto failed;
4393 for (i = 0; i < len; i++) {
4394 stmt_ty value;
4395 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4396 if (res != 0) goto failed;
4397 asdl_seq_SET(orelse, i, value);
4398 }
4399 Py_XDECREF(tmp);
4400 tmp = NULL;
4401 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004402 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004403 return 1;
4404 }
4405 if (PyObject_HasAttrString(obj, "finalbody")) {
4406 int res;
4407 Py_ssize_t len;
4408 Py_ssize_t i;
4409 tmp = PyObject_GetAttrString(obj, "finalbody");
4410 if (tmp == NULL) goto failed;
4411 if (!PyList_Check(tmp)) {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004412 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 +00004413 goto failed;
4414 }
4415 len = PyList_GET_SIZE(tmp);
4416 finalbody = asdl_seq_new(len, arena);
4417 if (finalbody == NULL) goto failed;
4418 for (i = 0; i < len; i++) {
4419 stmt_ty value;
4420 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4421 if (res != 0) goto failed;
4422 asdl_seq_SET(finalbody, i, value);
4423 }
4424 Py_XDECREF(tmp);
4425 tmp = NULL;
4426 } else {
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004427 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004428 return 1;
4429 }
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004430 *out = Try(body, handlers, orelse, finalbody, lineno,
4431 col_offset, arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004432 if (*out == NULL) goto failed;
4433 return 0;
4434 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004435 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4436 if (isinstance == -1) {
4437 return 1;
4438 }
4439 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004440 expr_ty test;
4441 expr_ty msg;
4442
4443 if (PyObject_HasAttrString(obj, "test")) {
4444 int res;
4445 tmp = PyObject_GetAttrString(obj, "test");
4446 if (tmp == NULL) goto failed;
4447 res = obj2ast_expr(tmp, &test, arena);
4448 if (res != 0) goto failed;
4449 Py_XDECREF(tmp);
4450 tmp = NULL;
4451 } else {
4452 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4453 return 1;
4454 }
4455 if (PyObject_HasAttrString(obj, "msg")) {
4456 int res;
4457 tmp = PyObject_GetAttrString(obj, "msg");
4458 if (tmp == NULL) goto failed;
4459 res = obj2ast_expr(tmp, &msg, arena);
4460 if (res != 0) goto failed;
4461 Py_XDECREF(tmp);
4462 tmp = NULL;
4463 } else {
4464 msg = NULL;
4465 }
4466 *out = Assert(test, msg, lineno, col_offset, arena);
4467 if (*out == NULL) goto failed;
4468 return 0;
4469 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004470 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4471 if (isinstance == -1) {
4472 return 1;
4473 }
4474 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004475 asdl_seq* names;
4476
4477 if (PyObject_HasAttrString(obj, "names")) {
4478 int res;
4479 Py_ssize_t len;
4480 Py_ssize_t i;
4481 tmp = PyObject_GetAttrString(obj, "names");
4482 if (tmp == NULL) goto failed;
4483 if (!PyList_Check(tmp)) {
4484 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4485 goto failed;
4486 }
4487 len = PyList_GET_SIZE(tmp);
4488 names = asdl_seq_new(len, arena);
4489 if (names == NULL) goto failed;
4490 for (i = 0; i < len; i++) {
4491 alias_ty value;
4492 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4493 if (res != 0) goto failed;
4494 asdl_seq_SET(names, i, value);
4495 }
4496 Py_XDECREF(tmp);
4497 tmp = NULL;
4498 } else {
4499 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4500 return 1;
4501 }
4502 *out = Import(names, lineno, col_offset, arena);
4503 if (*out == NULL) goto failed;
4504 return 0;
4505 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004506 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4507 if (isinstance == -1) {
4508 return 1;
4509 }
4510 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004511 identifier module;
4512 asdl_seq* names;
4513 int level;
4514
4515 if (PyObject_HasAttrString(obj, "module")) {
4516 int res;
4517 tmp = PyObject_GetAttrString(obj, "module");
4518 if (tmp == NULL) goto failed;
4519 res = obj2ast_identifier(tmp, &module, arena);
4520 if (res != 0) goto failed;
4521 Py_XDECREF(tmp);
4522 tmp = NULL;
4523 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004524 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004525 }
4526 if (PyObject_HasAttrString(obj, "names")) {
4527 int res;
4528 Py_ssize_t len;
4529 Py_ssize_t i;
4530 tmp = PyObject_GetAttrString(obj, "names");
4531 if (tmp == NULL) goto failed;
4532 if (!PyList_Check(tmp)) {
4533 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4534 goto failed;
4535 }
4536 len = PyList_GET_SIZE(tmp);
4537 names = asdl_seq_new(len, arena);
4538 if (names == NULL) goto failed;
4539 for (i = 0; i < len; i++) {
4540 alias_ty value;
4541 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4542 if (res != 0) goto failed;
4543 asdl_seq_SET(names, i, value);
4544 }
4545 Py_XDECREF(tmp);
4546 tmp = NULL;
4547 } else {
4548 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4549 return 1;
4550 }
4551 if (PyObject_HasAttrString(obj, "level")) {
4552 int res;
4553 tmp = PyObject_GetAttrString(obj, "level");
4554 if (tmp == NULL) goto failed;
4555 res = obj2ast_int(tmp, &level, arena);
4556 if (res != 0) goto failed;
4557 Py_XDECREF(tmp);
4558 tmp = NULL;
4559 } else {
4560 level = 0;
4561 }
4562 *out = ImportFrom(module, names, level, lineno, col_offset,
4563 arena);
4564 if (*out == NULL) goto failed;
4565 return 0;
4566 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004567 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4568 if (isinstance == -1) {
4569 return 1;
4570 }
4571 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004572 asdl_seq* names;
4573
4574 if (PyObject_HasAttrString(obj, "names")) {
4575 int res;
4576 Py_ssize_t len;
4577 Py_ssize_t i;
4578 tmp = PyObject_GetAttrString(obj, "names");
4579 if (tmp == NULL) goto failed;
4580 if (!PyList_Check(tmp)) {
4581 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4582 goto failed;
4583 }
4584 len = PyList_GET_SIZE(tmp);
4585 names = asdl_seq_new(len, arena);
4586 if (names == NULL) goto failed;
4587 for (i = 0; i < len; i++) {
4588 identifier value;
4589 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4590 if (res != 0) goto failed;
4591 asdl_seq_SET(names, i, value);
4592 }
4593 Py_XDECREF(tmp);
4594 tmp = NULL;
4595 } else {
4596 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4597 return 1;
4598 }
4599 *out = Global(names, lineno, col_offset, arena);
4600 if (*out == NULL) goto failed;
4601 return 0;
4602 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004603 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4604 if (isinstance == -1) {
4605 return 1;
4606 }
4607 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004608 asdl_seq* names;
4609
4610 if (PyObject_HasAttrString(obj, "names")) {
4611 int res;
4612 Py_ssize_t len;
4613 Py_ssize_t i;
4614 tmp = PyObject_GetAttrString(obj, "names");
4615 if (tmp == NULL) goto failed;
4616 if (!PyList_Check(tmp)) {
4617 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4618 goto failed;
4619 }
4620 len = PyList_GET_SIZE(tmp);
4621 names = asdl_seq_new(len, arena);
4622 if (names == NULL) goto failed;
4623 for (i = 0; i < len; i++) {
4624 identifier value;
4625 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4626 if (res != 0) goto failed;
4627 asdl_seq_SET(names, i, value);
4628 }
4629 Py_XDECREF(tmp);
4630 tmp = NULL;
4631 } else {
4632 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4633 return 1;
4634 }
4635 *out = Nonlocal(names, lineno, col_offset, arena);
4636 if (*out == NULL) goto failed;
4637 return 0;
4638 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004639 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4640 if (isinstance == -1) {
4641 return 1;
4642 }
4643 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004644 expr_ty value;
4645
4646 if (PyObject_HasAttrString(obj, "value")) {
4647 int res;
4648 tmp = PyObject_GetAttrString(obj, "value");
4649 if (tmp == NULL) goto failed;
4650 res = obj2ast_expr(tmp, &value, arena);
4651 if (res != 0) goto failed;
4652 Py_XDECREF(tmp);
4653 tmp = NULL;
4654 } else {
4655 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4656 return 1;
4657 }
4658 *out = Expr(value, lineno, col_offset, arena);
4659 if (*out == NULL) goto failed;
4660 return 0;
4661 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004662 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4663 if (isinstance == -1) {
4664 return 1;
4665 }
4666 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004667
4668 *out = Pass(lineno, col_offset, arena);
4669 if (*out == NULL) goto failed;
4670 return 0;
4671 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004672 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4673 if (isinstance == -1) {
4674 return 1;
4675 }
4676 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004677
4678 *out = Break(lineno, col_offset, arena);
4679 if (*out == NULL) goto failed;
4680 return 0;
4681 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004682 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4683 if (isinstance == -1) {
4684 return 1;
4685 }
4686 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004687
4688 *out = Continue(lineno, col_offset, arena);
4689 if (*out == NULL) goto failed;
4690 return 0;
4691 }
4692
Benjamin Peterson5b066812010-11-20 01:38:49 +00004693 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4694 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004695 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004696 return 1;
4697}
4698
4699int
4700obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4701{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004702 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004703
Benjamin Petersond8f65972010-11-20 04:31:07 +00004704 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004705 int lineno;
4706 int col_offset;
4707
4708 if (obj == Py_None) {
4709 *out = NULL;
4710 return 0;
4711 }
4712 if (PyObject_HasAttrString(obj, "lineno")) {
4713 int res;
4714 tmp = PyObject_GetAttrString(obj, "lineno");
4715 if (tmp == NULL) goto failed;
4716 res = obj2ast_int(tmp, &lineno, arena);
4717 if (res != 0) goto failed;
4718 Py_XDECREF(tmp);
4719 tmp = NULL;
4720 } else {
4721 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4722 return 1;
4723 }
4724 if (PyObject_HasAttrString(obj, "col_offset")) {
4725 int res;
4726 tmp = PyObject_GetAttrString(obj, "col_offset");
4727 if (tmp == NULL) goto failed;
4728 res = obj2ast_int(tmp, &col_offset, arena);
4729 if (res != 0) goto failed;
4730 Py_XDECREF(tmp);
4731 tmp = NULL;
4732 } else {
4733 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4734 return 1;
4735 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004736 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4737 if (isinstance == -1) {
4738 return 1;
4739 }
4740 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004741 boolop_ty op;
4742 asdl_seq* values;
4743
4744 if (PyObject_HasAttrString(obj, "op")) {
4745 int res;
4746 tmp = PyObject_GetAttrString(obj, "op");
4747 if (tmp == NULL) goto failed;
4748 res = obj2ast_boolop(tmp, &op, arena);
4749 if (res != 0) goto failed;
4750 Py_XDECREF(tmp);
4751 tmp = NULL;
4752 } else {
4753 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4754 return 1;
4755 }
4756 if (PyObject_HasAttrString(obj, "values")) {
4757 int res;
4758 Py_ssize_t len;
4759 Py_ssize_t i;
4760 tmp = PyObject_GetAttrString(obj, "values");
4761 if (tmp == NULL) goto failed;
4762 if (!PyList_Check(tmp)) {
4763 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4764 goto failed;
4765 }
4766 len = PyList_GET_SIZE(tmp);
4767 values = asdl_seq_new(len, arena);
4768 if (values == NULL) goto failed;
4769 for (i = 0; i < len; i++) {
4770 expr_ty value;
4771 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4772 if (res != 0) goto failed;
4773 asdl_seq_SET(values, i, value);
4774 }
4775 Py_XDECREF(tmp);
4776 tmp = NULL;
4777 } else {
4778 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4779 return 1;
4780 }
4781 *out = BoolOp(op, values, lineno, col_offset, arena);
4782 if (*out == NULL) goto failed;
4783 return 0;
4784 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004785 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4786 if (isinstance == -1) {
4787 return 1;
4788 }
4789 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004790 expr_ty left;
4791 operator_ty op;
4792 expr_ty right;
4793
4794 if (PyObject_HasAttrString(obj, "left")) {
4795 int res;
4796 tmp = PyObject_GetAttrString(obj, "left");
4797 if (tmp == NULL) goto failed;
4798 res = obj2ast_expr(tmp, &left, arena);
4799 if (res != 0) goto failed;
4800 Py_XDECREF(tmp);
4801 tmp = NULL;
4802 } else {
4803 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4804 return 1;
4805 }
4806 if (PyObject_HasAttrString(obj, "op")) {
4807 int res;
4808 tmp = PyObject_GetAttrString(obj, "op");
4809 if (tmp == NULL) goto failed;
4810 res = obj2ast_operator(tmp, &op, arena);
4811 if (res != 0) goto failed;
4812 Py_XDECREF(tmp);
4813 tmp = NULL;
4814 } else {
4815 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4816 return 1;
4817 }
4818 if (PyObject_HasAttrString(obj, "right")) {
4819 int res;
4820 tmp = PyObject_GetAttrString(obj, "right");
4821 if (tmp == NULL) goto failed;
4822 res = obj2ast_expr(tmp, &right, arena);
4823 if (res != 0) goto failed;
4824 Py_XDECREF(tmp);
4825 tmp = NULL;
4826 } else {
4827 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4828 return 1;
4829 }
4830 *out = BinOp(left, op, right, lineno, col_offset, arena);
4831 if (*out == NULL) goto failed;
4832 return 0;
4833 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004834 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4835 if (isinstance == -1) {
4836 return 1;
4837 }
4838 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004839 unaryop_ty op;
4840 expr_ty operand;
4841
4842 if (PyObject_HasAttrString(obj, "op")) {
4843 int res;
4844 tmp = PyObject_GetAttrString(obj, "op");
4845 if (tmp == NULL) goto failed;
4846 res = obj2ast_unaryop(tmp, &op, arena);
4847 if (res != 0) goto failed;
4848 Py_XDECREF(tmp);
4849 tmp = NULL;
4850 } else {
4851 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4852 return 1;
4853 }
4854 if (PyObject_HasAttrString(obj, "operand")) {
4855 int res;
4856 tmp = PyObject_GetAttrString(obj, "operand");
4857 if (tmp == NULL) goto failed;
4858 res = obj2ast_expr(tmp, &operand, arena);
4859 if (res != 0) goto failed;
4860 Py_XDECREF(tmp);
4861 tmp = NULL;
4862 } else {
4863 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4864 return 1;
4865 }
4866 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4867 if (*out == NULL) goto failed;
4868 return 0;
4869 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004870 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4871 if (isinstance == -1) {
4872 return 1;
4873 }
4874 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004875 arguments_ty args;
4876 expr_ty body;
4877
4878 if (PyObject_HasAttrString(obj, "args")) {
4879 int res;
4880 tmp = PyObject_GetAttrString(obj, "args");
4881 if (tmp == NULL) goto failed;
4882 res = obj2ast_arguments(tmp, &args, arena);
4883 if (res != 0) goto failed;
4884 Py_XDECREF(tmp);
4885 tmp = NULL;
4886 } else {
4887 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4888 return 1;
4889 }
4890 if (PyObject_HasAttrString(obj, "body")) {
4891 int res;
4892 tmp = PyObject_GetAttrString(obj, "body");
4893 if (tmp == NULL) goto failed;
4894 res = obj2ast_expr(tmp, &body, arena);
4895 if (res != 0) goto failed;
4896 Py_XDECREF(tmp);
4897 tmp = NULL;
4898 } else {
4899 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4900 return 1;
4901 }
4902 *out = Lambda(args, body, lineno, col_offset, arena);
4903 if (*out == NULL) goto failed;
4904 return 0;
4905 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004906 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4907 if (isinstance == -1) {
4908 return 1;
4909 }
4910 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004911 expr_ty test;
4912 expr_ty body;
4913 expr_ty orelse;
4914
4915 if (PyObject_HasAttrString(obj, "test")) {
4916 int res;
4917 tmp = PyObject_GetAttrString(obj, "test");
4918 if (tmp == NULL) goto failed;
4919 res = obj2ast_expr(tmp, &test, arena);
4920 if (res != 0) goto failed;
4921 Py_XDECREF(tmp);
4922 tmp = NULL;
4923 } else {
4924 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4925 return 1;
4926 }
4927 if (PyObject_HasAttrString(obj, "body")) {
4928 int res;
4929 tmp = PyObject_GetAttrString(obj, "body");
4930 if (tmp == NULL) goto failed;
4931 res = obj2ast_expr(tmp, &body, arena);
4932 if (res != 0) goto failed;
4933 Py_XDECREF(tmp);
4934 tmp = NULL;
4935 } else {
4936 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4937 return 1;
4938 }
4939 if (PyObject_HasAttrString(obj, "orelse")) {
4940 int res;
4941 tmp = PyObject_GetAttrString(obj, "orelse");
4942 if (tmp == NULL) goto failed;
4943 res = obj2ast_expr(tmp, &orelse, arena);
4944 if (res != 0) goto failed;
4945 Py_XDECREF(tmp);
4946 tmp = NULL;
4947 } else {
4948 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4949 return 1;
4950 }
4951 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4952 if (*out == NULL) goto failed;
4953 return 0;
4954 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004955 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4956 if (isinstance == -1) {
4957 return 1;
4958 }
4959 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004960 asdl_seq* keys;
4961 asdl_seq* values;
4962
4963 if (PyObject_HasAttrString(obj, "keys")) {
4964 int res;
4965 Py_ssize_t len;
4966 Py_ssize_t i;
4967 tmp = PyObject_GetAttrString(obj, "keys");
4968 if (tmp == NULL) goto failed;
4969 if (!PyList_Check(tmp)) {
4970 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4971 goto failed;
4972 }
4973 len = PyList_GET_SIZE(tmp);
4974 keys = asdl_seq_new(len, arena);
4975 if (keys == NULL) goto failed;
4976 for (i = 0; i < len; i++) {
4977 expr_ty value;
4978 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4979 if (res != 0) goto failed;
4980 asdl_seq_SET(keys, i, value);
4981 }
4982 Py_XDECREF(tmp);
4983 tmp = NULL;
4984 } else {
4985 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4986 return 1;
4987 }
4988 if (PyObject_HasAttrString(obj, "values")) {
4989 int res;
4990 Py_ssize_t len;
4991 Py_ssize_t i;
4992 tmp = PyObject_GetAttrString(obj, "values");
4993 if (tmp == NULL) goto failed;
4994 if (!PyList_Check(tmp)) {
4995 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4996 goto failed;
4997 }
4998 len = PyList_GET_SIZE(tmp);
4999 values = asdl_seq_new(len, arena);
5000 if (values == NULL) goto failed;
5001 for (i = 0; i < len; i++) {
5002 expr_ty value;
5003 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5004 if (res != 0) goto failed;
5005 asdl_seq_SET(values, i, value);
5006 }
5007 Py_XDECREF(tmp);
5008 tmp = NULL;
5009 } else {
5010 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5011 return 1;
5012 }
5013 *out = Dict(keys, values, lineno, col_offset, arena);
5014 if (*out == NULL) goto failed;
5015 return 0;
5016 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005017 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5018 if (isinstance == -1) {
5019 return 1;
5020 }
5021 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005022 asdl_seq* elts;
5023
5024 if (PyObject_HasAttrString(obj, "elts")) {
5025 int res;
5026 Py_ssize_t len;
5027 Py_ssize_t i;
5028 tmp = PyObject_GetAttrString(obj, "elts");
5029 if (tmp == NULL) goto failed;
5030 if (!PyList_Check(tmp)) {
5031 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5032 goto failed;
5033 }
5034 len = PyList_GET_SIZE(tmp);
5035 elts = asdl_seq_new(len, arena);
5036 if (elts == NULL) goto failed;
5037 for (i = 0; i < len; i++) {
5038 expr_ty value;
5039 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5040 if (res != 0) goto failed;
5041 asdl_seq_SET(elts, i, value);
5042 }
5043 Py_XDECREF(tmp);
5044 tmp = NULL;
5045 } else {
5046 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5047 return 1;
5048 }
5049 *out = Set(elts, lineno, col_offset, arena);
5050 if (*out == NULL) goto failed;
5051 return 0;
5052 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005053 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5054 if (isinstance == -1) {
5055 return 1;
5056 }
5057 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005058 expr_ty elt;
5059 asdl_seq* generators;
5060
5061 if (PyObject_HasAttrString(obj, "elt")) {
5062 int res;
5063 tmp = PyObject_GetAttrString(obj, "elt");
5064 if (tmp == NULL) goto failed;
5065 res = obj2ast_expr(tmp, &elt, arena);
5066 if (res != 0) goto failed;
5067 Py_XDECREF(tmp);
5068 tmp = NULL;
5069 } else {
5070 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5071 return 1;
5072 }
5073 if (PyObject_HasAttrString(obj, "generators")) {
5074 int res;
5075 Py_ssize_t len;
5076 Py_ssize_t i;
5077 tmp = PyObject_GetAttrString(obj, "generators");
5078 if (tmp == NULL) goto failed;
5079 if (!PyList_Check(tmp)) {
5080 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5081 goto failed;
5082 }
5083 len = PyList_GET_SIZE(tmp);
5084 generators = asdl_seq_new(len, arena);
5085 if (generators == NULL) goto failed;
5086 for (i = 0; i < len; i++) {
5087 comprehension_ty value;
5088 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5089 if (res != 0) goto failed;
5090 asdl_seq_SET(generators, i, value);
5091 }
5092 Py_XDECREF(tmp);
5093 tmp = NULL;
5094 } else {
5095 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5096 return 1;
5097 }
5098 *out = ListComp(elt, generators, lineno, col_offset, arena);
5099 if (*out == NULL) goto failed;
5100 return 0;
5101 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005102 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5103 if (isinstance == -1) {
5104 return 1;
5105 }
5106 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005107 expr_ty elt;
5108 asdl_seq* generators;
5109
5110 if (PyObject_HasAttrString(obj, "elt")) {
5111 int res;
5112 tmp = PyObject_GetAttrString(obj, "elt");
5113 if (tmp == NULL) goto failed;
5114 res = obj2ast_expr(tmp, &elt, arena);
5115 if (res != 0) goto failed;
5116 Py_XDECREF(tmp);
5117 tmp = NULL;
5118 } else {
5119 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5120 return 1;
5121 }
5122 if (PyObject_HasAttrString(obj, "generators")) {
5123 int res;
5124 Py_ssize_t len;
5125 Py_ssize_t i;
5126 tmp = PyObject_GetAttrString(obj, "generators");
5127 if (tmp == NULL) goto failed;
5128 if (!PyList_Check(tmp)) {
5129 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5130 goto failed;
5131 }
5132 len = PyList_GET_SIZE(tmp);
5133 generators = asdl_seq_new(len, arena);
5134 if (generators == NULL) goto failed;
5135 for (i = 0; i < len; i++) {
5136 comprehension_ty value;
5137 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5138 if (res != 0) goto failed;
5139 asdl_seq_SET(generators, i, value);
5140 }
5141 Py_XDECREF(tmp);
5142 tmp = NULL;
5143 } else {
5144 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5145 return 1;
5146 }
5147 *out = SetComp(elt, generators, lineno, col_offset, arena);
5148 if (*out == NULL) goto failed;
5149 return 0;
5150 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005151 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5152 if (isinstance == -1) {
5153 return 1;
5154 }
5155 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005156 expr_ty key;
5157 expr_ty value;
5158 asdl_seq* generators;
5159
5160 if (PyObject_HasAttrString(obj, "key")) {
5161 int res;
5162 tmp = PyObject_GetAttrString(obj, "key");
5163 if (tmp == NULL) goto failed;
5164 res = obj2ast_expr(tmp, &key, arena);
5165 if (res != 0) goto failed;
5166 Py_XDECREF(tmp);
5167 tmp = NULL;
5168 } else {
5169 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5170 return 1;
5171 }
5172 if (PyObject_HasAttrString(obj, "value")) {
5173 int res;
5174 tmp = PyObject_GetAttrString(obj, "value");
5175 if (tmp == NULL) goto failed;
5176 res = obj2ast_expr(tmp, &value, arena);
5177 if (res != 0) goto failed;
5178 Py_XDECREF(tmp);
5179 tmp = NULL;
5180 } else {
5181 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5182 return 1;
5183 }
5184 if (PyObject_HasAttrString(obj, "generators")) {
5185 int res;
5186 Py_ssize_t len;
5187 Py_ssize_t i;
5188 tmp = PyObject_GetAttrString(obj, "generators");
5189 if (tmp == NULL) goto failed;
5190 if (!PyList_Check(tmp)) {
5191 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5192 goto failed;
5193 }
5194 len = PyList_GET_SIZE(tmp);
5195 generators = asdl_seq_new(len, arena);
5196 if (generators == NULL) goto failed;
5197 for (i = 0; i < len; i++) {
5198 comprehension_ty value;
5199 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5200 if (res != 0) goto failed;
5201 asdl_seq_SET(generators, i, value);
5202 }
5203 Py_XDECREF(tmp);
5204 tmp = NULL;
5205 } else {
5206 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5207 return 1;
5208 }
5209 *out = DictComp(key, value, generators, lineno, col_offset,
5210 arena);
5211 if (*out == NULL) goto failed;
5212 return 0;
5213 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005214 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5215 if (isinstance == -1) {
5216 return 1;
5217 }
5218 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005219 expr_ty elt;
5220 asdl_seq* generators;
5221
5222 if (PyObject_HasAttrString(obj, "elt")) {
5223 int res;
5224 tmp = PyObject_GetAttrString(obj, "elt");
5225 if (tmp == NULL) goto failed;
5226 res = obj2ast_expr(tmp, &elt, arena);
5227 if (res != 0) goto failed;
5228 Py_XDECREF(tmp);
5229 tmp = NULL;
5230 } else {
5231 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5232 return 1;
5233 }
5234 if (PyObject_HasAttrString(obj, "generators")) {
5235 int res;
5236 Py_ssize_t len;
5237 Py_ssize_t i;
5238 tmp = PyObject_GetAttrString(obj, "generators");
5239 if (tmp == NULL) goto failed;
5240 if (!PyList_Check(tmp)) {
5241 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5242 goto failed;
5243 }
5244 len = PyList_GET_SIZE(tmp);
5245 generators = asdl_seq_new(len, arena);
5246 if (generators == NULL) goto failed;
5247 for (i = 0; i < len; i++) {
5248 comprehension_ty value;
5249 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5250 if (res != 0) goto failed;
5251 asdl_seq_SET(generators, i, value);
5252 }
5253 Py_XDECREF(tmp);
5254 tmp = NULL;
5255 } else {
5256 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5257 return 1;
5258 }
5259 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5260 if (*out == NULL) goto failed;
5261 return 0;
5262 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005263 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5264 if (isinstance == -1) {
5265 return 1;
5266 }
5267 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005268 expr_ty value;
5269
5270 if (PyObject_HasAttrString(obj, "value")) {
5271 int res;
5272 tmp = PyObject_GetAttrString(obj, "value");
5273 if (tmp == NULL) goto failed;
5274 res = obj2ast_expr(tmp, &value, arena);
5275 if (res != 0) goto failed;
5276 Py_XDECREF(tmp);
5277 tmp = NULL;
5278 } else {
5279 value = NULL;
5280 }
5281 *out = Yield(value, lineno, col_offset, arena);
5282 if (*out == NULL) goto failed;
5283 return 0;
5284 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005285 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5286 if (isinstance == -1) {
5287 return 1;
5288 }
5289 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005290 expr_ty left;
5291 asdl_int_seq* ops;
5292 asdl_seq* comparators;
5293
5294 if (PyObject_HasAttrString(obj, "left")) {
5295 int res;
5296 tmp = PyObject_GetAttrString(obj, "left");
5297 if (tmp == NULL) goto failed;
5298 res = obj2ast_expr(tmp, &left, arena);
5299 if (res != 0) goto failed;
5300 Py_XDECREF(tmp);
5301 tmp = NULL;
5302 } else {
5303 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5304 return 1;
5305 }
5306 if (PyObject_HasAttrString(obj, "ops")) {
5307 int res;
5308 Py_ssize_t len;
5309 Py_ssize_t i;
5310 tmp = PyObject_GetAttrString(obj, "ops");
5311 if (tmp == NULL) goto failed;
5312 if (!PyList_Check(tmp)) {
5313 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5314 goto failed;
5315 }
5316 len = PyList_GET_SIZE(tmp);
5317 ops = asdl_int_seq_new(len, arena);
5318 if (ops == NULL) goto failed;
5319 for (i = 0; i < len; i++) {
5320 cmpop_ty value;
5321 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5322 if (res != 0) goto failed;
5323 asdl_seq_SET(ops, i, value);
5324 }
5325 Py_XDECREF(tmp);
5326 tmp = NULL;
5327 } else {
5328 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5329 return 1;
5330 }
5331 if (PyObject_HasAttrString(obj, "comparators")) {
5332 int res;
5333 Py_ssize_t len;
5334 Py_ssize_t i;
5335 tmp = PyObject_GetAttrString(obj, "comparators");
5336 if (tmp == NULL) goto failed;
5337 if (!PyList_Check(tmp)) {
5338 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5339 goto failed;
5340 }
5341 len = PyList_GET_SIZE(tmp);
5342 comparators = asdl_seq_new(len, arena);
5343 if (comparators == NULL) goto failed;
5344 for (i = 0; i < len; i++) {
5345 expr_ty value;
5346 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5347 if (res != 0) goto failed;
5348 asdl_seq_SET(comparators, i, value);
5349 }
5350 Py_XDECREF(tmp);
5351 tmp = NULL;
5352 } else {
5353 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5354 return 1;
5355 }
5356 *out = Compare(left, ops, comparators, lineno, col_offset,
5357 arena);
5358 if (*out == NULL) goto failed;
5359 return 0;
5360 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005361 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5362 if (isinstance == -1) {
5363 return 1;
5364 }
5365 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005366 expr_ty func;
5367 asdl_seq* args;
5368 asdl_seq* keywords;
5369 expr_ty starargs;
5370 expr_ty kwargs;
5371
5372 if (PyObject_HasAttrString(obj, "func")) {
5373 int res;
5374 tmp = PyObject_GetAttrString(obj, "func");
5375 if (tmp == NULL) goto failed;
5376 res = obj2ast_expr(tmp, &func, arena);
5377 if (res != 0) goto failed;
5378 Py_XDECREF(tmp);
5379 tmp = NULL;
5380 } else {
5381 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5382 return 1;
5383 }
5384 if (PyObject_HasAttrString(obj, "args")) {
5385 int res;
5386 Py_ssize_t len;
5387 Py_ssize_t i;
5388 tmp = PyObject_GetAttrString(obj, "args");
5389 if (tmp == NULL) goto failed;
5390 if (!PyList_Check(tmp)) {
5391 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5392 goto failed;
5393 }
5394 len = PyList_GET_SIZE(tmp);
5395 args = asdl_seq_new(len, arena);
5396 if (args == NULL) goto failed;
5397 for (i = 0; i < len; i++) {
5398 expr_ty value;
5399 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5400 if (res != 0) goto failed;
5401 asdl_seq_SET(args, i, value);
5402 }
5403 Py_XDECREF(tmp);
5404 tmp = NULL;
5405 } else {
5406 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5407 return 1;
5408 }
5409 if (PyObject_HasAttrString(obj, "keywords")) {
5410 int res;
5411 Py_ssize_t len;
5412 Py_ssize_t i;
5413 tmp = PyObject_GetAttrString(obj, "keywords");
5414 if (tmp == NULL) goto failed;
5415 if (!PyList_Check(tmp)) {
5416 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5417 goto failed;
5418 }
5419 len = PyList_GET_SIZE(tmp);
5420 keywords = asdl_seq_new(len, arena);
5421 if (keywords == NULL) goto failed;
5422 for (i = 0; i < len; i++) {
5423 keyword_ty value;
5424 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5425 if (res != 0) goto failed;
5426 asdl_seq_SET(keywords, i, value);
5427 }
5428 Py_XDECREF(tmp);
5429 tmp = NULL;
5430 } else {
5431 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5432 return 1;
5433 }
5434 if (PyObject_HasAttrString(obj, "starargs")) {
5435 int res;
5436 tmp = PyObject_GetAttrString(obj, "starargs");
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_expr(tmp, &starargs, arena);
5439 if (res != 0) goto failed;
5440 Py_XDECREF(tmp);
5441 tmp = NULL;
5442 } else {
5443 starargs = NULL;
5444 }
5445 if (PyObject_HasAttrString(obj, "kwargs")) {
5446 int res;
5447 tmp = PyObject_GetAttrString(obj, "kwargs");
5448 if (tmp == NULL) goto failed;
5449 res = obj2ast_expr(tmp, &kwargs, arena);
5450 if (res != 0) goto failed;
5451 Py_XDECREF(tmp);
5452 tmp = NULL;
5453 } else {
5454 kwargs = NULL;
5455 }
5456 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5457 col_offset, arena);
5458 if (*out == NULL) goto failed;
5459 return 0;
5460 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005461 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5462 if (isinstance == -1) {
5463 return 1;
5464 }
5465 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005466 object n;
5467
5468 if (PyObject_HasAttrString(obj, "n")) {
5469 int res;
5470 tmp = PyObject_GetAttrString(obj, "n");
5471 if (tmp == NULL) goto failed;
5472 res = obj2ast_object(tmp, &n, arena);
5473 if (res != 0) goto failed;
5474 Py_XDECREF(tmp);
5475 tmp = NULL;
5476 } else {
5477 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5478 return 1;
5479 }
5480 *out = Num(n, lineno, col_offset, arena);
5481 if (*out == NULL) goto failed;
5482 return 0;
5483 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005484 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5485 if (isinstance == -1) {
5486 return 1;
5487 }
5488 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005489 string s;
5490
5491 if (PyObject_HasAttrString(obj, "s")) {
5492 int res;
5493 tmp = PyObject_GetAttrString(obj, "s");
5494 if (tmp == NULL) goto failed;
5495 res = obj2ast_string(tmp, &s, arena);
5496 if (res != 0) goto failed;
5497 Py_XDECREF(tmp);
5498 tmp = NULL;
5499 } else {
5500 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5501 return 1;
5502 }
5503 *out = Str(s, lineno, col_offset, arena);
5504 if (*out == NULL) goto failed;
5505 return 0;
5506 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005507 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5508 if (isinstance == -1) {
5509 return 1;
5510 }
5511 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005512 string s;
5513
5514 if (PyObject_HasAttrString(obj, "s")) {
5515 int res;
5516 tmp = PyObject_GetAttrString(obj, "s");
5517 if (tmp == NULL) goto failed;
5518 res = obj2ast_string(tmp, &s, arena);
5519 if (res != 0) goto failed;
5520 Py_XDECREF(tmp);
5521 tmp = NULL;
5522 } else {
5523 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5524 return 1;
5525 }
5526 *out = Bytes(s, lineno, col_offset, arena);
5527 if (*out == NULL) goto failed;
5528 return 0;
5529 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005530 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5531 if (isinstance == -1) {
5532 return 1;
5533 }
5534 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005535
5536 *out = Ellipsis(lineno, col_offset, arena);
5537 if (*out == NULL) goto failed;
5538 return 0;
5539 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005540 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5541 if (isinstance == -1) {
5542 return 1;
5543 }
5544 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005545 expr_ty value;
5546 identifier attr;
5547 expr_context_ty ctx;
5548
5549 if (PyObject_HasAttrString(obj, "value")) {
5550 int res;
5551 tmp = PyObject_GetAttrString(obj, "value");
5552 if (tmp == NULL) goto failed;
5553 res = obj2ast_expr(tmp, &value, arena);
5554 if (res != 0) goto failed;
5555 Py_XDECREF(tmp);
5556 tmp = NULL;
5557 } else {
5558 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5559 return 1;
5560 }
5561 if (PyObject_HasAttrString(obj, "attr")) {
5562 int res;
5563 tmp = PyObject_GetAttrString(obj, "attr");
5564 if (tmp == NULL) goto failed;
5565 res = obj2ast_identifier(tmp, &attr, arena);
5566 if (res != 0) goto failed;
5567 Py_XDECREF(tmp);
5568 tmp = NULL;
5569 } else {
5570 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5571 return 1;
5572 }
5573 if (PyObject_HasAttrString(obj, "ctx")) {
5574 int res;
5575 tmp = PyObject_GetAttrString(obj, "ctx");
5576 if (tmp == NULL) goto failed;
5577 res = obj2ast_expr_context(tmp, &ctx, arena);
5578 if (res != 0) goto failed;
5579 Py_XDECREF(tmp);
5580 tmp = NULL;
5581 } else {
5582 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5583 return 1;
5584 }
5585 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5586 if (*out == NULL) goto failed;
5587 return 0;
5588 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005589 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5590 if (isinstance == -1) {
5591 return 1;
5592 }
5593 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005594 expr_ty value;
5595 slice_ty slice;
5596 expr_context_ty ctx;
5597
5598 if (PyObject_HasAttrString(obj, "value")) {
5599 int res;
5600 tmp = PyObject_GetAttrString(obj, "value");
5601 if (tmp == NULL) goto failed;
5602 res = obj2ast_expr(tmp, &value, arena);
5603 if (res != 0) goto failed;
5604 Py_XDECREF(tmp);
5605 tmp = NULL;
5606 } else {
5607 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5608 return 1;
5609 }
5610 if (PyObject_HasAttrString(obj, "slice")) {
5611 int res;
5612 tmp = PyObject_GetAttrString(obj, "slice");
5613 if (tmp == NULL) goto failed;
5614 res = obj2ast_slice(tmp, &slice, arena);
5615 if (res != 0) goto failed;
5616 Py_XDECREF(tmp);
5617 tmp = NULL;
5618 } else {
5619 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5620 return 1;
5621 }
5622 if (PyObject_HasAttrString(obj, "ctx")) {
5623 int res;
5624 tmp = PyObject_GetAttrString(obj, "ctx");
5625 if (tmp == NULL) goto failed;
5626 res = obj2ast_expr_context(tmp, &ctx, arena);
5627 if (res != 0) goto failed;
5628 Py_XDECREF(tmp);
5629 tmp = NULL;
5630 } else {
5631 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5632 return 1;
5633 }
5634 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5635 if (*out == NULL) goto failed;
5636 return 0;
5637 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005638 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5639 if (isinstance == -1) {
5640 return 1;
5641 }
5642 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005643 expr_ty value;
5644 expr_context_ty ctx;
5645
5646 if (PyObject_HasAttrString(obj, "value")) {
5647 int res;
5648 tmp = PyObject_GetAttrString(obj, "value");
5649 if (tmp == NULL) goto failed;
5650 res = obj2ast_expr(tmp, &value, arena);
5651 if (res != 0) goto failed;
5652 Py_XDECREF(tmp);
5653 tmp = NULL;
5654 } else {
5655 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5656 return 1;
5657 }
5658 if (PyObject_HasAttrString(obj, "ctx")) {
5659 int res;
5660 tmp = PyObject_GetAttrString(obj, "ctx");
5661 if (tmp == NULL) goto failed;
5662 res = obj2ast_expr_context(tmp, &ctx, arena);
5663 if (res != 0) goto failed;
5664 Py_XDECREF(tmp);
5665 tmp = NULL;
5666 } else {
5667 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5668 return 1;
5669 }
5670 *out = Starred(value, ctx, lineno, col_offset, arena);
5671 if (*out == NULL) goto failed;
5672 return 0;
5673 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005674 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5675 if (isinstance == -1) {
5676 return 1;
5677 }
5678 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005679 identifier id;
5680 expr_context_ty ctx;
5681
5682 if (PyObject_HasAttrString(obj, "id")) {
5683 int res;
5684 tmp = PyObject_GetAttrString(obj, "id");
5685 if (tmp == NULL) goto failed;
5686 res = obj2ast_identifier(tmp, &id, arena);
5687 if (res != 0) goto failed;
5688 Py_XDECREF(tmp);
5689 tmp = NULL;
5690 } else {
5691 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5692 return 1;
5693 }
5694 if (PyObject_HasAttrString(obj, "ctx")) {
5695 int res;
5696 tmp = PyObject_GetAttrString(obj, "ctx");
5697 if (tmp == NULL) goto failed;
5698 res = obj2ast_expr_context(tmp, &ctx, arena);
5699 if (res != 0) goto failed;
5700 Py_XDECREF(tmp);
5701 tmp = NULL;
5702 } else {
5703 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5704 return 1;
5705 }
5706 *out = Name(id, ctx, lineno, col_offset, arena);
5707 if (*out == NULL) goto failed;
5708 return 0;
5709 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005710 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5711 if (isinstance == -1) {
5712 return 1;
5713 }
5714 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005715 asdl_seq* elts;
5716 expr_context_ty ctx;
5717
5718 if (PyObject_HasAttrString(obj, "elts")) {
5719 int res;
5720 Py_ssize_t len;
5721 Py_ssize_t i;
5722 tmp = PyObject_GetAttrString(obj, "elts");
5723 if (tmp == NULL) goto failed;
5724 if (!PyList_Check(tmp)) {
5725 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5726 goto failed;
5727 }
5728 len = PyList_GET_SIZE(tmp);
5729 elts = asdl_seq_new(len, arena);
5730 if (elts == NULL) goto failed;
5731 for (i = 0; i < len; i++) {
5732 expr_ty value;
5733 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5734 if (res != 0) goto failed;
5735 asdl_seq_SET(elts, i, value);
5736 }
5737 Py_XDECREF(tmp);
5738 tmp = NULL;
5739 } else {
5740 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5741 return 1;
5742 }
5743 if (PyObject_HasAttrString(obj, "ctx")) {
5744 int res;
5745 tmp = PyObject_GetAttrString(obj, "ctx");
5746 if (tmp == NULL) goto failed;
5747 res = obj2ast_expr_context(tmp, &ctx, arena);
5748 if (res != 0) goto failed;
5749 Py_XDECREF(tmp);
5750 tmp = NULL;
5751 } else {
5752 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5753 return 1;
5754 }
5755 *out = List(elts, ctx, lineno, col_offset, arena);
5756 if (*out == NULL) goto failed;
5757 return 0;
5758 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005759 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5760 if (isinstance == -1) {
5761 return 1;
5762 }
5763 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005764 asdl_seq* elts;
5765 expr_context_ty ctx;
5766
5767 if (PyObject_HasAttrString(obj, "elts")) {
5768 int res;
5769 Py_ssize_t len;
5770 Py_ssize_t i;
5771 tmp = PyObject_GetAttrString(obj, "elts");
5772 if (tmp == NULL) goto failed;
5773 if (!PyList_Check(tmp)) {
5774 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5775 goto failed;
5776 }
5777 len = PyList_GET_SIZE(tmp);
5778 elts = asdl_seq_new(len, arena);
5779 if (elts == NULL) goto failed;
5780 for (i = 0; i < len; i++) {
5781 expr_ty value;
5782 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5783 if (res != 0) goto failed;
5784 asdl_seq_SET(elts, i, value);
5785 }
5786 Py_XDECREF(tmp);
5787 tmp = NULL;
5788 } else {
5789 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5790 return 1;
5791 }
5792 if (PyObject_HasAttrString(obj, "ctx")) {
5793 int res;
5794 tmp = PyObject_GetAttrString(obj, "ctx");
5795 if (tmp == NULL) goto failed;
5796 res = obj2ast_expr_context(tmp, &ctx, arena);
5797 if (res != 0) goto failed;
5798 Py_XDECREF(tmp);
5799 tmp = NULL;
5800 } else {
5801 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5802 return 1;
5803 }
5804 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5805 if (*out == NULL) goto failed;
5806 return 0;
5807 }
5808
Benjamin Peterson5b066812010-11-20 01:38:49 +00005809 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5810 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005811 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005812 return 1;
5813}
5814
5815int
5816obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5817{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005818 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005819
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005820 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5821 if (isinstance == -1) {
5822 return 1;
5823 }
5824 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005825 *out = Load;
5826 return 0;
5827 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005828 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5829 if (isinstance == -1) {
5830 return 1;
5831 }
5832 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005833 *out = Store;
5834 return 0;
5835 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005836 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5837 if (isinstance == -1) {
5838 return 1;
5839 }
5840 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005841 *out = Del;
5842 return 0;
5843 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005844 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5845 if (isinstance == -1) {
5846 return 1;
5847 }
5848 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005849 *out = AugLoad;
5850 return 0;
5851 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005852 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5853 if (isinstance == -1) {
5854 return 1;
5855 }
5856 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005857 *out = AugStore;
5858 return 0;
5859 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005860 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5861 if (isinstance == -1) {
5862 return 1;
5863 }
5864 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005865 *out = Param;
5866 return 0;
5867 }
5868
Benjamin Peterson5b066812010-11-20 01:38:49 +00005869 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005870 return 1;
5871}
5872
5873int
5874obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5875{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005876 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005877
Benjamin Petersond8f65972010-11-20 04:31:07 +00005878 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005879
5880 if (obj == Py_None) {
5881 *out = NULL;
5882 return 0;
5883 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005884 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5885 if (isinstance == -1) {
5886 return 1;
5887 }
5888 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005889 expr_ty lower;
5890 expr_ty upper;
5891 expr_ty step;
5892
5893 if (PyObject_HasAttrString(obj, "lower")) {
5894 int res;
5895 tmp = PyObject_GetAttrString(obj, "lower");
5896 if (tmp == NULL) goto failed;
5897 res = obj2ast_expr(tmp, &lower, arena);
5898 if (res != 0) goto failed;
5899 Py_XDECREF(tmp);
5900 tmp = NULL;
5901 } else {
5902 lower = NULL;
5903 }
5904 if (PyObject_HasAttrString(obj, "upper")) {
5905 int res;
5906 tmp = PyObject_GetAttrString(obj, "upper");
5907 if (tmp == NULL) goto failed;
5908 res = obj2ast_expr(tmp, &upper, arena);
5909 if (res != 0) goto failed;
5910 Py_XDECREF(tmp);
5911 tmp = NULL;
5912 } else {
5913 upper = NULL;
5914 }
5915 if (PyObject_HasAttrString(obj, "step")) {
5916 int res;
5917 tmp = PyObject_GetAttrString(obj, "step");
5918 if (tmp == NULL) goto failed;
5919 res = obj2ast_expr(tmp, &step, arena);
5920 if (res != 0) goto failed;
5921 Py_XDECREF(tmp);
5922 tmp = NULL;
5923 } else {
5924 step = NULL;
5925 }
5926 *out = Slice(lower, upper, step, arena);
5927 if (*out == NULL) goto failed;
5928 return 0;
5929 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005930 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5931 if (isinstance == -1) {
5932 return 1;
5933 }
5934 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005935 asdl_seq* dims;
5936
5937 if (PyObject_HasAttrString(obj, "dims")) {
5938 int res;
5939 Py_ssize_t len;
5940 Py_ssize_t i;
5941 tmp = PyObject_GetAttrString(obj, "dims");
5942 if (tmp == NULL) goto failed;
5943 if (!PyList_Check(tmp)) {
5944 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5945 goto failed;
5946 }
5947 len = PyList_GET_SIZE(tmp);
5948 dims = asdl_seq_new(len, arena);
5949 if (dims == NULL) goto failed;
5950 for (i = 0; i < len; i++) {
5951 slice_ty value;
5952 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5953 if (res != 0) goto failed;
5954 asdl_seq_SET(dims, i, value);
5955 }
5956 Py_XDECREF(tmp);
5957 tmp = NULL;
5958 } else {
5959 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5960 return 1;
5961 }
5962 *out = ExtSlice(dims, arena);
5963 if (*out == NULL) goto failed;
5964 return 0;
5965 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005966 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5967 if (isinstance == -1) {
5968 return 1;
5969 }
5970 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005971 expr_ty value;
5972
5973 if (PyObject_HasAttrString(obj, "value")) {
5974 int res;
5975 tmp = PyObject_GetAttrString(obj, "value");
5976 if (tmp == NULL) goto failed;
5977 res = obj2ast_expr(tmp, &value, arena);
5978 if (res != 0) goto failed;
5979 Py_XDECREF(tmp);
5980 tmp = NULL;
5981 } else {
5982 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5983 return 1;
5984 }
5985 *out = Index(value, arena);
5986 if (*out == NULL) goto failed;
5987 return 0;
5988 }
5989
Benjamin Peterson5b066812010-11-20 01:38:49 +00005990 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
5991 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005992 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005993 return 1;
5994}
5995
5996int
5997obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5998{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005999 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006000
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006001 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6002 if (isinstance == -1) {
6003 return 1;
6004 }
6005 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006006 *out = And;
6007 return 0;
6008 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006009 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6010 if (isinstance == -1) {
6011 return 1;
6012 }
6013 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006014 *out = Or;
6015 return 0;
6016 }
6017
Benjamin Peterson5b066812010-11-20 01:38:49 +00006018 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006019 return 1;
6020}
6021
6022int
6023obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6024{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006025 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006026
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006027 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6028 if (isinstance == -1) {
6029 return 1;
6030 }
6031 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006032 *out = Add;
6033 return 0;
6034 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006035 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6036 if (isinstance == -1) {
6037 return 1;
6038 }
6039 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006040 *out = Sub;
6041 return 0;
6042 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006043 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6044 if (isinstance == -1) {
6045 return 1;
6046 }
6047 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006048 *out = Mult;
6049 return 0;
6050 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006051 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6052 if (isinstance == -1) {
6053 return 1;
6054 }
6055 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006056 *out = Div;
6057 return 0;
6058 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006059 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6060 if (isinstance == -1) {
6061 return 1;
6062 }
6063 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006064 *out = Mod;
6065 return 0;
6066 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006067 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6068 if (isinstance == -1) {
6069 return 1;
6070 }
6071 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006072 *out = Pow;
6073 return 0;
6074 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006075 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6076 if (isinstance == -1) {
6077 return 1;
6078 }
6079 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006080 *out = LShift;
6081 return 0;
6082 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006083 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6084 if (isinstance == -1) {
6085 return 1;
6086 }
6087 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006088 *out = RShift;
6089 return 0;
6090 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006091 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6092 if (isinstance == -1) {
6093 return 1;
6094 }
6095 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006096 *out = BitOr;
6097 return 0;
6098 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006099 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6100 if (isinstance == -1) {
6101 return 1;
6102 }
6103 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006104 *out = BitXor;
6105 return 0;
6106 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006107 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6108 if (isinstance == -1) {
6109 return 1;
6110 }
6111 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006112 *out = BitAnd;
6113 return 0;
6114 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006115 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6116 if (isinstance == -1) {
6117 return 1;
6118 }
6119 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006120 *out = FloorDiv;
6121 return 0;
6122 }
6123
Benjamin Peterson5b066812010-11-20 01:38:49 +00006124 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006125 return 1;
6126}
6127
6128int
6129obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6130{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006131 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006132
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006133 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6134 if (isinstance == -1) {
6135 return 1;
6136 }
6137 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006138 *out = Invert;
6139 return 0;
6140 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006141 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6142 if (isinstance == -1) {
6143 return 1;
6144 }
6145 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006146 *out = Not;
6147 return 0;
6148 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006149 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6150 if (isinstance == -1) {
6151 return 1;
6152 }
6153 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006154 *out = UAdd;
6155 return 0;
6156 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006157 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6158 if (isinstance == -1) {
6159 return 1;
6160 }
6161 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006162 *out = USub;
6163 return 0;
6164 }
6165
Benjamin Peterson5b066812010-11-20 01:38:49 +00006166 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006167 return 1;
6168}
6169
6170int
6171obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6172{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006173 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006174
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006175 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6176 if (isinstance == -1) {
6177 return 1;
6178 }
6179 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006180 *out = Eq;
6181 return 0;
6182 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006183 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6184 if (isinstance == -1) {
6185 return 1;
6186 }
6187 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006188 *out = NotEq;
6189 return 0;
6190 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006191 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6192 if (isinstance == -1) {
6193 return 1;
6194 }
6195 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006196 *out = Lt;
6197 return 0;
6198 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006199 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6200 if (isinstance == -1) {
6201 return 1;
6202 }
6203 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006204 *out = LtE;
6205 return 0;
6206 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006207 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6208 if (isinstance == -1) {
6209 return 1;
6210 }
6211 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006212 *out = Gt;
6213 return 0;
6214 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006215 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6216 if (isinstance == -1) {
6217 return 1;
6218 }
6219 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006220 *out = GtE;
6221 return 0;
6222 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006223 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6224 if (isinstance == -1) {
6225 return 1;
6226 }
6227 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006228 *out = Is;
6229 return 0;
6230 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006231 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6232 if (isinstance == -1) {
6233 return 1;
6234 }
6235 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006236 *out = IsNot;
6237 return 0;
6238 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006239 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6240 if (isinstance == -1) {
6241 return 1;
6242 }
6243 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006244 *out = In;
6245 return 0;
6246 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006247 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6248 if (isinstance == -1) {
6249 return 1;
6250 }
6251 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006252 *out = NotIn;
6253 return 0;
6254 }
6255
Benjamin Peterson5b066812010-11-20 01:38:49 +00006256 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006257 return 1;
6258}
6259
6260int
6261obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6262{
6263 PyObject* tmp = NULL;
6264 expr_ty target;
6265 expr_ty iter;
6266 asdl_seq* ifs;
6267
6268 if (PyObject_HasAttrString(obj, "target")) {
6269 int res;
6270 tmp = PyObject_GetAttrString(obj, "target");
6271 if (tmp == NULL) goto failed;
6272 res = obj2ast_expr(tmp, &target, arena);
6273 if (res != 0) goto failed;
6274 Py_XDECREF(tmp);
6275 tmp = NULL;
6276 } else {
6277 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6278 return 1;
6279 }
6280 if (PyObject_HasAttrString(obj, "iter")) {
6281 int res;
6282 tmp = PyObject_GetAttrString(obj, "iter");
6283 if (tmp == NULL) goto failed;
6284 res = obj2ast_expr(tmp, &iter, arena);
6285 if (res != 0) goto failed;
6286 Py_XDECREF(tmp);
6287 tmp = NULL;
6288 } else {
6289 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6290 return 1;
6291 }
6292 if (PyObject_HasAttrString(obj, "ifs")) {
6293 int res;
6294 Py_ssize_t len;
6295 Py_ssize_t i;
6296 tmp = PyObject_GetAttrString(obj, "ifs");
6297 if (tmp == NULL) goto failed;
6298 if (!PyList_Check(tmp)) {
6299 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6300 goto failed;
6301 }
6302 len = PyList_GET_SIZE(tmp);
6303 ifs = asdl_seq_new(len, arena);
6304 if (ifs == NULL) goto failed;
6305 for (i = 0; i < len; i++) {
6306 expr_ty value;
6307 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6308 if (res != 0) goto failed;
6309 asdl_seq_SET(ifs, i, value);
6310 }
6311 Py_XDECREF(tmp);
6312 tmp = NULL;
6313 } else {
6314 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6315 return 1;
6316 }
6317 *out = comprehension(target, iter, ifs, arena);
6318 return 0;
6319failed:
6320 Py_XDECREF(tmp);
6321 return 1;
6322}
6323
6324int
6325obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6326{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006327 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006328
Benjamin Petersond8f65972010-11-20 04:31:07 +00006329 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006330 int lineno;
6331 int col_offset;
6332
Neal Norwitzad74aa82008-03-31 05:14:30 +00006333 if (obj == Py_None) {
6334 *out = NULL;
6335 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006336 }
6337 if (PyObject_HasAttrString(obj, "lineno")) {
6338 int res;
6339 tmp = PyObject_GetAttrString(obj, "lineno");
6340 if (tmp == NULL) goto failed;
6341 res = obj2ast_int(tmp, &lineno, arena);
6342 if (res != 0) goto failed;
6343 Py_XDECREF(tmp);
6344 tmp = NULL;
6345 } else {
6346 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6347 return 1;
6348 }
6349 if (PyObject_HasAttrString(obj, "col_offset")) {
6350 int res;
6351 tmp = PyObject_GetAttrString(obj, "col_offset");
6352 if (tmp == NULL) goto failed;
6353 res = obj2ast_int(tmp, &col_offset, arena);
6354 if (res != 0) goto failed;
6355 Py_XDECREF(tmp);
6356 tmp = NULL;
6357 } else {
6358 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6359 return 1;
6360 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006361 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6362 if (isinstance == -1) {
6363 return 1;
6364 }
6365 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006366 expr_ty type;
6367 identifier name;
6368 asdl_seq* body;
6369
6370 if (PyObject_HasAttrString(obj, "type")) {
6371 int res;
6372 tmp = PyObject_GetAttrString(obj, "type");
6373 if (tmp == NULL) goto failed;
6374 res = obj2ast_expr(tmp, &type, arena);
6375 if (res != 0) goto failed;
6376 Py_XDECREF(tmp);
6377 tmp = NULL;
6378 } else {
6379 type = NULL;
6380 }
6381 if (PyObject_HasAttrString(obj, "name")) {
6382 int res;
6383 tmp = PyObject_GetAttrString(obj, "name");
6384 if (tmp == NULL) goto failed;
6385 res = obj2ast_identifier(tmp, &name, arena);
6386 if (res != 0) goto failed;
6387 Py_XDECREF(tmp);
6388 tmp = NULL;
6389 } else {
6390 name = NULL;
6391 }
6392 if (PyObject_HasAttrString(obj, "body")) {
6393 int res;
6394 Py_ssize_t len;
6395 Py_ssize_t i;
6396 tmp = PyObject_GetAttrString(obj, "body");
6397 if (tmp == NULL) goto failed;
6398 if (!PyList_Check(tmp)) {
6399 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6400 goto failed;
6401 }
6402 len = PyList_GET_SIZE(tmp);
6403 body = asdl_seq_new(len, arena);
6404 if (body == NULL) goto failed;
6405 for (i = 0; i < len; i++) {
6406 stmt_ty value;
6407 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6408 if (res != 0) goto failed;
6409 asdl_seq_SET(body, i, value);
6410 }
6411 Py_XDECREF(tmp);
6412 tmp = NULL;
6413 } else {
6414 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6415 return 1;
6416 }
6417 *out = ExceptHandler(type, name, body, lineno, col_offset,
6418 arena);
6419 if (*out == NULL) goto failed;
6420 return 0;
6421 }
6422
Benjamin Peterson5b066812010-11-20 01:38:49 +00006423 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6424 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006425 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006426 return 1;
6427}
6428
6429int
6430obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6431{
6432 PyObject* tmp = NULL;
6433 asdl_seq* args;
6434 identifier vararg;
6435 expr_ty varargannotation;
6436 asdl_seq* kwonlyargs;
6437 identifier kwarg;
6438 expr_ty kwargannotation;
6439 asdl_seq* defaults;
6440 asdl_seq* kw_defaults;
6441
6442 if (PyObject_HasAttrString(obj, "args")) {
6443 int res;
6444 Py_ssize_t len;
6445 Py_ssize_t i;
6446 tmp = PyObject_GetAttrString(obj, "args");
6447 if (tmp == NULL) goto failed;
6448 if (!PyList_Check(tmp)) {
6449 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6450 goto failed;
6451 }
6452 len = PyList_GET_SIZE(tmp);
6453 args = asdl_seq_new(len, arena);
6454 if (args == NULL) goto failed;
6455 for (i = 0; i < len; i++) {
6456 arg_ty value;
6457 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6458 if (res != 0) goto failed;
6459 asdl_seq_SET(args, i, value);
6460 }
6461 Py_XDECREF(tmp);
6462 tmp = NULL;
6463 } else {
6464 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6465 return 1;
6466 }
6467 if (PyObject_HasAttrString(obj, "vararg")) {
6468 int res;
6469 tmp = PyObject_GetAttrString(obj, "vararg");
6470 if (tmp == NULL) goto failed;
6471 res = obj2ast_identifier(tmp, &vararg, arena);
6472 if (res != 0) goto failed;
6473 Py_XDECREF(tmp);
6474 tmp = NULL;
6475 } else {
6476 vararg = NULL;
6477 }
6478 if (PyObject_HasAttrString(obj, "varargannotation")) {
6479 int res;
6480 tmp = PyObject_GetAttrString(obj, "varargannotation");
6481 if (tmp == NULL) goto failed;
6482 res = obj2ast_expr(tmp, &varargannotation, arena);
6483 if (res != 0) goto failed;
6484 Py_XDECREF(tmp);
6485 tmp = NULL;
6486 } else {
6487 varargannotation = NULL;
6488 }
6489 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6490 int res;
6491 Py_ssize_t len;
6492 Py_ssize_t i;
6493 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6494 if (tmp == NULL) goto failed;
6495 if (!PyList_Check(tmp)) {
6496 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6497 goto failed;
6498 }
6499 len = PyList_GET_SIZE(tmp);
6500 kwonlyargs = asdl_seq_new(len, arena);
6501 if (kwonlyargs == NULL) goto failed;
6502 for (i = 0; i < len; i++) {
6503 arg_ty value;
6504 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6505 if (res != 0) goto failed;
6506 asdl_seq_SET(kwonlyargs, i, value);
6507 }
6508 Py_XDECREF(tmp);
6509 tmp = NULL;
6510 } else {
6511 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6512 return 1;
6513 }
6514 if (PyObject_HasAttrString(obj, "kwarg")) {
6515 int res;
6516 tmp = PyObject_GetAttrString(obj, "kwarg");
6517 if (tmp == NULL) goto failed;
6518 res = obj2ast_identifier(tmp, &kwarg, arena);
6519 if (res != 0) goto failed;
6520 Py_XDECREF(tmp);
6521 tmp = NULL;
6522 } else {
6523 kwarg = NULL;
6524 }
6525 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6526 int res;
6527 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6528 if (tmp == NULL) goto failed;
6529 res = obj2ast_expr(tmp, &kwargannotation, arena);
6530 if (res != 0) goto failed;
6531 Py_XDECREF(tmp);
6532 tmp = NULL;
6533 } else {
6534 kwargannotation = NULL;
6535 }
6536 if (PyObject_HasAttrString(obj, "defaults")) {
6537 int res;
6538 Py_ssize_t len;
6539 Py_ssize_t i;
6540 tmp = PyObject_GetAttrString(obj, "defaults");
6541 if (tmp == NULL) goto failed;
6542 if (!PyList_Check(tmp)) {
6543 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6544 goto failed;
6545 }
6546 len = PyList_GET_SIZE(tmp);
6547 defaults = asdl_seq_new(len, arena);
6548 if (defaults == NULL) goto failed;
6549 for (i = 0; i < len; i++) {
6550 expr_ty value;
6551 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6552 if (res != 0) goto failed;
6553 asdl_seq_SET(defaults, i, value);
6554 }
6555 Py_XDECREF(tmp);
6556 tmp = NULL;
6557 } else {
6558 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6559 return 1;
6560 }
6561 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6562 int res;
6563 Py_ssize_t len;
6564 Py_ssize_t i;
6565 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6566 if (tmp == NULL) goto failed;
6567 if (!PyList_Check(tmp)) {
6568 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6569 goto failed;
6570 }
6571 len = PyList_GET_SIZE(tmp);
6572 kw_defaults = asdl_seq_new(len, arena);
6573 if (kw_defaults == NULL) goto failed;
6574 for (i = 0; i < len; i++) {
6575 expr_ty value;
6576 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6577 if (res != 0) goto failed;
6578 asdl_seq_SET(kw_defaults, i, value);
6579 }
6580 Py_XDECREF(tmp);
6581 tmp = NULL;
6582 } else {
6583 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6584 return 1;
6585 }
6586 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6587 kwargannotation, defaults, kw_defaults, arena);
6588 return 0;
6589failed:
6590 Py_XDECREF(tmp);
6591 return 1;
6592}
6593
6594int
6595obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6596{
6597 PyObject* tmp = NULL;
6598 identifier arg;
6599 expr_ty annotation;
6600
6601 if (PyObject_HasAttrString(obj, "arg")) {
6602 int res;
6603 tmp = PyObject_GetAttrString(obj, "arg");
6604 if (tmp == NULL) goto failed;
6605 res = obj2ast_identifier(tmp, &arg, arena);
6606 if (res != 0) goto failed;
6607 Py_XDECREF(tmp);
6608 tmp = NULL;
6609 } else {
6610 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6611 return 1;
6612 }
6613 if (PyObject_HasAttrString(obj, "annotation")) {
6614 int res;
6615 tmp = PyObject_GetAttrString(obj, "annotation");
6616 if (tmp == NULL) goto failed;
6617 res = obj2ast_expr(tmp, &annotation, arena);
6618 if (res != 0) goto failed;
6619 Py_XDECREF(tmp);
6620 tmp = NULL;
6621 } else {
6622 annotation = NULL;
6623 }
6624 *out = arg(arg, annotation, arena);
6625 return 0;
6626failed:
6627 Py_XDECREF(tmp);
6628 return 1;
6629}
6630
6631int
6632obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6633{
6634 PyObject* tmp = NULL;
6635 identifier arg;
6636 expr_ty value;
6637
6638 if (PyObject_HasAttrString(obj, "arg")) {
6639 int res;
6640 tmp = PyObject_GetAttrString(obj, "arg");
6641 if (tmp == NULL) goto failed;
6642 res = obj2ast_identifier(tmp, &arg, arena);
6643 if (res != 0) goto failed;
6644 Py_XDECREF(tmp);
6645 tmp = NULL;
6646 } else {
6647 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6648 return 1;
6649 }
6650 if (PyObject_HasAttrString(obj, "value")) {
6651 int res;
6652 tmp = PyObject_GetAttrString(obj, "value");
6653 if (tmp == NULL) goto failed;
6654 res = obj2ast_expr(tmp, &value, arena);
6655 if (res != 0) goto failed;
6656 Py_XDECREF(tmp);
6657 tmp = NULL;
6658 } else {
6659 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6660 return 1;
6661 }
6662 *out = keyword(arg, value, arena);
6663 return 0;
6664failed:
6665 Py_XDECREF(tmp);
6666 return 1;
6667}
6668
6669int
6670obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6671{
6672 PyObject* tmp = NULL;
6673 identifier name;
6674 identifier asname;
6675
6676 if (PyObject_HasAttrString(obj, "name")) {
6677 int res;
6678 tmp = PyObject_GetAttrString(obj, "name");
6679 if (tmp == NULL) goto failed;
6680 res = obj2ast_identifier(tmp, &name, arena);
6681 if (res != 0) goto failed;
6682 Py_XDECREF(tmp);
6683 tmp = NULL;
6684 } else {
6685 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6686 return 1;
6687 }
6688 if (PyObject_HasAttrString(obj, "asname")) {
6689 int res;
6690 tmp = PyObject_GetAttrString(obj, "asname");
6691 if (tmp == NULL) goto failed;
6692 res = obj2ast_identifier(tmp, &asname, arena);
6693 if (res != 0) goto failed;
6694 Py_XDECREF(tmp);
6695 tmp = NULL;
6696 } else {
6697 asname = NULL;
6698 }
6699 *out = alias(name, asname, arena);
6700 return 0;
6701failed:
6702 Py_XDECREF(tmp);
6703 return 1;
6704}
6705
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006706int
6707obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6708{
6709 PyObject* tmp = NULL;
6710 expr_ty context_expr;
6711 expr_ty optional_vars;
6712
6713 if (PyObject_HasAttrString(obj, "context_expr")) {
6714 int res;
6715 tmp = PyObject_GetAttrString(obj, "context_expr");
6716 if (tmp == NULL) goto failed;
6717 res = obj2ast_expr(tmp, &context_expr, arena);
6718 if (res != 0) goto failed;
6719 Py_XDECREF(tmp);
6720 tmp = NULL;
6721 } else {
6722 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
6723 return 1;
6724 }
6725 if (PyObject_HasAttrString(obj, "optional_vars")) {
6726 int res;
6727 tmp = PyObject_GetAttrString(obj, "optional_vars");
6728 if (tmp == NULL) goto failed;
6729 res = obj2ast_expr(tmp, &optional_vars, arena);
6730 if (res != 0) goto failed;
6731 Py_XDECREF(tmp);
6732 tmp = NULL;
6733 } else {
6734 optional_vars = NULL;
6735 }
6736 *out = withitem(context_expr, optional_vars, arena);
6737 return 0;
6738failed:
6739 Py_XDECREF(tmp);
6740 return 1;
6741}
6742
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006743
Martin v. Löwis1a214512008-06-11 05:26:20 +00006744static struct PyModuleDef _astmodule = {
6745 PyModuleDef_HEAD_INIT, "_ast"
6746};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006747PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006748PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006749{
6750 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006751 if (!init_types()) return NULL;
6752 m = PyModule_Create(&_astmodule);
6753 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006754 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006755 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6756 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006757 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006758 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006759 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6760 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006761 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006762 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006763 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006764 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006765 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006766 0) return NULL;
6767 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6768 NULL;
6769 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6770 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006772 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006773 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006774 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006775 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006776 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006777 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006778 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006779 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_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, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006782 0) return NULL;
6783 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6784 NULL;
6785 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6786 NULL;
6787 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6788 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6789 NULL;
6790 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6791 NULL;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05006792 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return
6793 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006794 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006795 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006796 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006797 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006798 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006799 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006800 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006801 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006802 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006803 return NULL;
6804 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6805 NULL;
6806 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6807 NULL;
6808 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6809 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006810 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006811 return NULL;
6812 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6813 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006814 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006815 return NULL;
6816 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6817 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006818 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006819 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006820 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006821 return NULL;
6822 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6823 NULL;
6824 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6825 NULL;
6826 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6827 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006828 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006829 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006830 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006831 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006832 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006833 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006834 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006835 (PyObject*)GeneratorExp_type) < 0) return NULL;
6836 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6837 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006838 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006839 return NULL;
6840 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6841 NULL;
6842 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6843 NULL;
6844 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6845 NULL;
6846 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6847 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006848 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006849 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006850 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006851 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006852 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006853 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006854 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006855 return NULL;
6856 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6857 NULL;
6858 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6859 NULL;
6860 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6861 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006862 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006863 (PyObject*)expr_context_type) < 0) return NULL;
6864 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6865 NULL;
6866 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6867 NULL;
6868 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6869 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006870 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006871 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006872 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006873 return NULL;
6874 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6875 NULL;
6876 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6877 NULL;
6878 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6879 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006880 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006881 return NULL;
6882 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6883 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006884 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006885 return NULL;
6886 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6887 NULL;
6888 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006889 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006890 return NULL;
6891 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6892 NULL;
6893 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6894 NULL;
6895 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6896 NULL;
6897 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6898 NULL;
6899 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6902 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006903 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006904 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006905 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006906 return NULL;
6907 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6908 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006909 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006910 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006911 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006912 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006914 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006915 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006916 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006917 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006918 return NULL;
6919 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6920 NULL;
6921 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6924 NULL;
6925 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6926 NULL;
6927 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6928 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6929 NULL;
6930 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6931 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6932 NULL;
6933 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6934 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6935 NULL;
6936 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6937 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6938 NULL;
6939 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6940 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6941 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006942 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006943 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006944 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006945 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006946 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006947 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006948 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006949 0) return NULL;
6950 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6951 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006952 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006953 return NULL;
6954 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6955 NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006956 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6957 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006958 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006959}
6960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006961
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006962PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006963{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006964 init_types();
6965 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006966}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006967
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006968/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6969mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006970{
6971 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006972 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6973 (PyObject*)Interactive_type};
6974 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006975 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006976 assert(0 <= mode && mode <= 2);
6977
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006978 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006979
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006980 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6981 if (isinstance == -1)
6982 return NULL;
6983 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006984 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6985 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006986 return NULL;
6987 }
6988 if (obj2ast_mod(ast, &res, arena) != 0)
6989 return NULL;
6990 else
6991 return res;
6992}
6993
6994int PyAST_Check(PyObject* obj)
6995{
6996 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006997 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006998}
6999
Martin v. Löwis5b222132007-06-10 09:51:05 +00007000