blob: 43dcf6a508186472aa87db96382fc9a745b39a7e [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson2193d2b2011-07-22 10:50:23 -05005 __version__ .
Thomas Wouterscf297e42007-02-23 15:07:44 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Neal Norwitz207c9f32008-03-31 04:42:11 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000045 "decorator_list",
Neal Norwitzc1505362006-12-28 06:47:50 +000046 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Neal Norwitz53d960c2006-02-28 22:47:29 +000048static PyTypeObject *ClassDef_type;
49static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000050 "name",
51 "bases",
Guido van Rossum52cc1d82007-03-18 15:41:51 +000052 "keywords",
53 "starargs",
54 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000055 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000056 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000057};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Return_type;
59static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "value",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Delete_type;
63static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Assign_type;
67static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000068 "targets",
69 "value",
70};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *AugAssign_type;
72static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000073 "target",
74 "op",
75 "value",
76};
Neal Norwitz53d960c2006-02-28 22:47:29 +000077static PyTypeObject *For_type;
78static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000079 "target",
80 "iter",
81 "body",
82 "orelse",
83};
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static PyTypeObject *While_type;
85static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000086 "test",
87 "body",
88 "orelse",
89};
Neal Norwitz53d960c2006-02-28 22:47:29 +000090static PyTypeObject *If_type;
91static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000092 "test",
93 "body",
94 "orelse",
95};
Neal Norwitz53d960c2006-02-28 22:47:29 +000096static PyTypeObject *With_type;
97static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +000098 "context_expr",
99 "optional_vars",
100 "body",
101};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000102static PyTypeObject *Raise_type;
103static char *Raise_fields[]={
Collin Winter828f04a2007-08-31 00:04:24 +0000104 "exc",
105 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static PyTypeObject *TryExcept_type;
108static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000109 "body",
110 "handlers",
111 "orelse",
112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *TryFinally_type;
114static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115 "body",
116 "finalbody",
117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *Assert_type;
119static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120 "test",
121 "msg",
122};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000123static PyTypeObject *Import_type;
124static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000125 "names",
126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *ImportFrom_type;
128static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000129 "module",
130 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000131 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000133static PyTypeObject *Global_type;
134static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135 "names",
136};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000137static PyTypeObject *Nonlocal_type;
138static char *Nonlocal_fields[]={
139 "names",
140};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000141static PyTypeObject *Expr_type;
142static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000143 "value",
144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *Pass_type;
146static PyTypeObject *Break_type;
147static PyTypeObject *Continue_type;
148static PyTypeObject *expr_type;
149static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000150 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000151 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000152};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *BoolOp_type;
155static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000156 "op",
157 "values",
158};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BinOp_type;
160static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "left",
162 "op",
163 "right",
164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *UnaryOp_type;
166static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167 "op",
168 "operand",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *Lambda_type;
171static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "args",
173 "body",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *IfExp_type;
176static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000177 "test",
178 "body",
179 "orelse",
180};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static PyTypeObject *Dict_type;
182static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000183 "keys",
184 "values",
185};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000186static PyTypeObject *Set_type;
187static char *Set_fields[]={
188 "elts",
189};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000190static PyTypeObject *ListComp_type;
191static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000192 "elt",
193 "generators",
194};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000195static PyTypeObject *SetComp_type;
196static char *SetComp_fields[]={
197 "elt",
198 "generators",
199};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000200static PyTypeObject *DictComp_type;
201static char *DictComp_fields[]={
202 "key",
203 "value",
204 "generators",
205};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000206static PyTypeObject *GeneratorExp_type;
207static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000208 "elt",
209 "generators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Yield_type;
212static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "value",
214};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static PyTypeObject *Compare_type;
216static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217 "left",
218 "ops",
219 "comparators",
220};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000221static PyTypeObject *Call_type;
222static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000223 "func",
224 "args",
225 "keywords",
226 "starargs",
227 "kwargs",
228};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000229static PyTypeObject *Num_type;
230static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000231 "n",
232};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000233static PyTypeObject *Str_type;
234static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000235 "s",
236};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000237static PyTypeObject *Bytes_type;
238static char *Bytes_fields[]={
239 "s",
240};
Georg Brandl52318d62006-09-06 07:06:08 +0000241static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000242static PyTypeObject *Attribute_type;
243static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000244 "value",
245 "attr",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *Subscript_type;
249static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "value",
251 "slice",
252 "ctx",
253};
Guido van Rossum0368b722007-05-11 16:50:42 +0000254static PyTypeObject *Starred_type;
255static char *Starred_fields[]={
256 "value",
257 "ctx",
258};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000259static PyTypeObject *Name_type;
260static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000261 "id",
262 "ctx",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *List_type;
265static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000266 "elts",
267 "ctx",
268};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000269static PyTypeObject *Tuple_type;
270static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271 "elts",
272 "ctx",
273};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000274static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000275static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
276*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
277static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *Load_type;
279static PyTypeObject *Store_type;
280static PyTypeObject *Del_type;
281static PyTypeObject *AugLoad_type;
282static PyTypeObject *AugStore_type;
283static PyTypeObject *Param_type;
284static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000285static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000286static PyTypeObject *Slice_type;
287static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000288 "lower",
289 "upper",
290 "step",
291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *ExtSlice_type;
293static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000294 "dims",
295};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Index_type;
297static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298 "value",
299};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301static PyObject *And_singleton, *Or_singleton;
302static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000303static PyTypeObject *And_type;
304static PyTypeObject *Or_type;
305static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000306static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
307*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
308*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
309*FloorDiv_singleton;
310static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000311static PyTypeObject *Add_type;
312static PyTypeObject *Sub_type;
313static PyTypeObject *Mult_type;
314static PyTypeObject *Div_type;
315static PyTypeObject *Mod_type;
316static PyTypeObject *Pow_type;
317static PyTypeObject *LShift_type;
318static PyTypeObject *RShift_type;
319static PyTypeObject *BitOr_type;
320static PyTypeObject *BitXor_type;
321static PyTypeObject *BitAnd_type;
322static PyTypeObject *FloorDiv_type;
323static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000324static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
325*USub_singleton;
326static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Invert_type;
328static PyTypeObject *Not_type;
329static PyTypeObject *UAdd_type;
330static PyTypeObject *USub_type;
331static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
333*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
334*NotIn_singleton;
335static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000336static PyTypeObject *Eq_type;
337static PyTypeObject *NotEq_type;
338static PyTypeObject *Lt_type;
339static PyTypeObject *LtE_type;
340static PyTypeObject *Gt_type;
341static PyTypeObject *GtE_type;
342static PyTypeObject *Is_type;
343static PyTypeObject *IsNot_type;
344static PyTypeObject *In_type;
345static PyTypeObject *NotIn_type;
346static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000349 "target",
350 "iter",
351 "ifs",
352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000354static char *excepthandler_attributes[] = {
355 "lineno",
356 "col_offset",
357};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000359static PyTypeObject *ExceptHandler_type;
360static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "type",
362 "name",
363 "body",
364};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000365static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000366static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000367static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000368 "args",
369 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000370 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000371 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000372 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000373 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000375 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000376};
Neal Norwitzc1505362006-12-28 06:47:50 +0000377static PyTypeObject *arg_type;
378static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000379static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000380 "arg",
381 "annotation",
382};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000383static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000384static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000385static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000386 "arg",
387 "value",
388};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000389static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000390static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000392 "name",
393 "asname",
394};
395
396
Neal Norwitz207c9f32008-03-31 04:42:11 +0000397static int
398ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
399{
400 Py_ssize_t i, numfields = 0;
401 int res = -1;
402 PyObject *key, *value, *fields;
403 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
404 if (!fields)
405 PyErr_Clear();
406 if (fields) {
407 numfields = PySequence_Size(fields);
408 if (numfields == -1)
409 goto cleanup;
410 }
411 res = 0; /* if no error occurs, this stays 0 to the end */
412 if (PyTuple_GET_SIZE(args) > 0) {
413 if (numfields != PyTuple_GET_SIZE(args)) {
414 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000415 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000416 Py_TYPE(self)->tp_name,
417 numfields == 0 ? "" : "either 0 or ",
418 numfields, numfields == 1 ? "" : "s");
419 res = -1;
420 goto cleanup;
421 }
422 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
423 /* cannot be reached when fields is NULL */
424 PyObject *name = PySequence_GetItem(fields, i);
425 if (!name) {
426 res = -1;
427 goto cleanup;
428 }
429 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
430 Py_DECREF(name);
431 if (res < 0)
432 goto cleanup;
433 }
434 }
435 if (kw) {
436 i = 0; /* needed by PyDict_Next */
437 while (PyDict_Next(kw, &i, &key, &value)) {
438 res = PyObject_SetAttr(self, key, value);
439 if (res < 0)
440 goto cleanup;
441 }
442 }
443 cleanup:
444 Py_XDECREF(fields);
445 return res;
446}
447
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000448/* Pickling support */
449static PyObject *
450ast_type_reduce(PyObject *self, PyObject *unused)
451{
452 PyObject *res;
453 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
454 if (dict == NULL) {
455 if (PyErr_ExceptionMatches(PyExc_AttributeError))
456 PyErr_Clear();
457 else
458 return NULL;
459 }
460 if (dict) {
461 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
462 Py_DECREF(dict);
463 return res;
464 }
465 return Py_BuildValue("O()", Py_TYPE(self));
466}
467
468static PyMethodDef ast_type_methods[] = {
469 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
470 {NULL}
471};
472
Neal Norwitz207c9f32008-03-31 04:42:11 +0000473static PyTypeObject AST_type = {
474 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000475 "_ast.AST",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000476 sizeof(PyObject),
477 0,
478 0, /* tp_dealloc */
479 0, /* tp_print */
480 0, /* tp_getattr */
481 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000482 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000483 0, /* tp_repr */
484 0, /* tp_as_number */
485 0, /* tp_as_sequence */
486 0, /* tp_as_mapping */
487 0, /* tp_hash */
488 0, /* tp_call */
489 0, /* tp_str */
490 PyObject_GenericGetAttr, /* tp_getattro */
491 PyObject_GenericSetAttr, /* tp_setattro */
492 0, /* tp_as_buffer */
493 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
494 0, /* tp_doc */
495 0, /* tp_traverse */
496 0, /* tp_clear */
497 0, /* tp_richcompare */
498 0, /* tp_weaklistoffset */
499 0, /* tp_iter */
500 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000501 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000502 0, /* tp_members */
503 0, /* tp_getset */
504 0, /* tp_base */
505 0, /* tp_dict */
506 0, /* tp_descr_get */
507 0, /* tp_descr_set */
508 0, /* tp_dictoffset */
509 (initproc)ast_type_init, /* tp_init */
510 PyType_GenericAlloc, /* tp_alloc */
511 PyType_GenericNew, /* tp_new */
512 PyObject_Del, /* tp_free */
513};
514
515
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000516static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
517{
518 PyObject *fnames, *result;
519 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000520 fnames = PyTuple_New(num_fields);
521 if (!fnames) return NULL;
522 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000523 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000524 if (!field) {
525 Py_DECREF(fnames);
526 return NULL;
527 }
528 PyTuple_SET_ITEM(fnames, i, field);
529 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000530 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000531 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000532 Py_DECREF(fnames);
533 return (PyTypeObject*)result;
534}
535
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000536static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
537{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000539 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000540 if (!l)
541 return 0;
542 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000543 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000544 if (!s) {
545 Py_DECREF(l);
546 return 0;
547 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000548 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000549 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
551 Py_DECREF(l);
552 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000553}
554
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000555/* Conversion AST -> Python */
556
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000557static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
558{
559 int i, n = asdl_seq_LEN(seq);
560 PyObject *result = PyList_New(n);
561 PyObject *value;
562 if (!result)
563 return NULL;
564 for (i = 0; i < n; i++) {
565 value = func(asdl_seq_GET(seq, i));
566 if (!value) {
567 Py_DECREF(result);
568 return NULL;
569 }
570 PyList_SET_ITEM(result, i, value);
571 }
572 return result;
573}
574
575static PyObject* ast2obj_object(void *o)
576{
577 if (!o)
578 o = Py_None;
579 Py_INCREF((PyObject*)o);
580 return (PyObject*)o;
581}
582#define ast2obj_identifier ast2obj_object
583#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000584
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000585static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000586{
Christian Heimes217cfd12007-12-02 14:31:20 +0000587 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000588}
589
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000590/* Conversion Python -> AST */
591
592static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
593{
594 if (obj == Py_None)
595 obj = NULL;
596 if (obj)
597 PyArena_AddPyObject(arena, obj);
598 Py_XINCREF(obj);
599 *out = obj;
600 return 0;
601}
602
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500603static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
604 const char *name)
605{
606 if (!PyUnicode_CheckExact(name)) {
607 PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
608 return 1;
609 }
610 return obj2ast_object(obj, out, arena);
611}
612
613static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
614{
615 return obj2ast_stringlike(obj, out, arena, "identifier");
616}
617
618static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
619{
620 return obj2ast_stringlike(obj, out, arena, "string");
621}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000622
623static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
624{
625 int i;
626 if (!PyLong_Check(obj)) {
627 PyObject *s = PyObject_Repr(obj);
628 if (s == NULL) return 1;
629 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000630 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000631 Py_DECREF(s);
632 return 1;
633 }
634
635 i = (int)PyLong_AsLong(obj);
636 if (i == -1 && PyErr_Occurred())
637 return 1;
638 *out = i;
639 return 0;
640}
641
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000642static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000643{
644 PyObject *empty_tuple, *d;
645 if (PyType_Ready(&AST_type) < 0)
646 return -1;
647 d = AST_type.tp_dict;
648 empty_tuple = PyTuple_New(0);
649 if (!empty_tuple ||
650 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
651 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
652 Py_XDECREF(empty_tuple);
653 return -1;
654 }
655 Py_DECREF(empty_tuple);
656 return 0;
657}
658
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000659
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660static int init_types(void)
661{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000662 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000664 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000665 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 if (!mod_type) return 0;
667 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Interactive_type = make_type("Interactive", mod_type,
671 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 Expression_type = make_type("Expression", mod_type, Expression_fields,
674 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000678 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000680 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000682 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000684 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000700 With_type = make_type("With", stmt_type, With_fields, 3);
701 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000702 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000706 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
707 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000714 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000716 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000718 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
719 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000722 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000724 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000726 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000728 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000730 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000737 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000739 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000741 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000743 Set_type = make_type("Set", expr_type, Set_fields, 1);
744 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000745 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000746 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000747 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
748 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000749 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
750 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 GeneratorExp_type = make_type("GeneratorExp", expr_type,
752 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000754 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000760 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000764 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
765 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000766 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
767 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000768 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000770 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000772 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
773 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000776 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000778 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000780 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!expr_context_type) return 0;
782 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000783 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000785 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000787 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000789 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000791 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000795 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000799 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000803 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000805 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000807 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!slice_type) return 0;
809 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000814 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000816 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!boolop_type) return 0;
818 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000819 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000821 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000823 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000825 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000827 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!operator_type) return 0;
829 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000850 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000852 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000854 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000856 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000858 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000860 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000862 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000866 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000868 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000870 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000872 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000874 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000876 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000878 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!unaryop_type) return 0;
880 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000881 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000883 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000885 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000887 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000889 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000890 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000891 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000893 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000895 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000897 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000898 if (!cmpop_type) return 0;
899 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000900 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000902 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000904 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000906 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000908 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000912 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000914 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000916 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000917 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000918 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000919 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000920 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000922 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000924 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000926 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000928 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000930 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000931 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000932 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000933 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000934 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000935 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000936 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000937 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000938 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000939 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000940 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000941 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000942 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000943 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000944 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000945 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
946 return 0;
947 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
948 ExceptHandler_fields, 3);
949 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000950 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000951 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000952 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000953 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000954 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000955 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000956 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000957 if (!alias_type) return 0;
958 initialized = 1;
959 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000960}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000961
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000962static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
963static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
964static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
965static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
966 arena);
967static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
968static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
969static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
970static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
971static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
972static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
973 arena);
974static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
975 arena);
976static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
977static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
978static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
979static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000982Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983{
984 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000985 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 p->kind = Module_kind;
989 p->v.Module.body = body;
990 return p;
991}
992
993mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995{
996 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000997 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000998 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 p->kind = Interactive_kind;
1001 p->v.Interactive.body = body;
1002 return p;
1003}
1004
1005mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001006Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007{
1008 mod_ty p;
1009 if (!body) {
1010 PyErr_SetString(PyExc_ValueError,
1011 "field body is required for Expression");
1012 return NULL;
1013 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001014 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001015 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 p->kind = Expression_kind;
1018 p->v.Expression.body = body;
1019 return p;
1020}
1021
1022mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001023Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024{
1025 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001026 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001027 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 p->kind = Suite_kind;
1030 p->v.Suite.body = body;
1031 return p;
1032}
1033
1034stmt_ty
1035FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001036 decorator_list, expr_ty returns, int lineno, int col_offset,
1037 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038{
1039 stmt_ty p;
1040 if (!name) {
1041 PyErr_SetString(PyExc_ValueError,
1042 "field name is required for FunctionDef");
1043 return NULL;
1044 }
1045 if (!args) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field args is required for FunctionDef");
1048 return NULL;
1049 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001050 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001051 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 p->kind = FunctionDef_kind;
1054 p->v.FunctionDef.name = name;
1055 p->v.FunctionDef.args = args;
1056 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001057 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001058 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001060 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 return p;
1062}
1063
1064stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001065ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001066 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1067 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068{
1069 stmt_ty p;
1070 if (!name) {
1071 PyErr_SetString(PyExc_ValueError,
1072 "field name is required for ClassDef");
1073 return NULL;
1074 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001075 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001076 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 p->kind = ClassDef_kind;
1079 p->v.ClassDef.name = name;
1080 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001081 p->v.ClassDef.keywords = keywords;
1082 p->v.ClassDef.starargs = starargs;
1083 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001085 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001087 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return p;
1089}
1090
1091stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001092Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
1094 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001095 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001096 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 p->kind = Return_kind;
1099 p->v.Return.value = value;
1100 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001101 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return p;
1103}
1104
1105stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107{
1108 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001109 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001110 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 p->kind = Delete_kind;
1113 p->v.Delete.targets = targets;
1114 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001115 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return p;
1117}
1118
1119stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1121 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
1123 stmt_ty p;
1124 if (!value) {
1125 PyErr_SetString(PyExc_ValueError,
1126 "field value is required for Assign");
1127 return NULL;
1128 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001130 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 p->kind = Assign_kind;
1133 p->v.Assign.targets = targets;
1134 p->v.Assign.value = value;
1135 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return p;
1138}
1139
1140stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1142 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143{
1144 stmt_ty p;
1145 if (!target) {
1146 PyErr_SetString(PyExc_ValueError,
1147 "field target is required for AugAssign");
1148 return NULL;
1149 }
1150 if (!op) {
1151 PyErr_SetString(PyExc_ValueError,
1152 "field op is required for AugAssign");
1153 return NULL;
1154 }
1155 if (!value) {
1156 PyErr_SetString(PyExc_ValueError,
1157 "field value is required for AugAssign");
1158 return NULL;
1159 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001160 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001161 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 p->kind = AugAssign_kind;
1164 p->v.AugAssign.target = target;
1165 p->v.AugAssign.op = op;
1166 p->v.AugAssign.value = value;
1167 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001168 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return p;
1170}
1171
1172stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
1176 stmt_ty p;
1177 if (!target) {
1178 PyErr_SetString(PyExc_ValueError,
1179 "field target is required for For");
1180 return NULL;
1181 }
1182 if (!iter) {
1183 PyErr_SetString(PyExc_ValueError,
1184 "field iter is required for For");
1185 return NULL;
1186 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001188 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 p->kind = For_kind;
1191 p->v.For.target = target;
1192 p->v.For.iter = iter;
1193 p->v.For.body = body;
1194 p->v.For.orelse = orelse;
1195 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001196 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return p;
1198}
1199
1200stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001201While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1202 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
1204 stmt_ty p;
1205 if (!test) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "field test is required for While");
1208 return NULL;
1209 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001211 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 p->kind = While_kind;
1214 p->v.While.test = test;
1215 p->v.While.body = body;
1216 p->v.While.orelse = orelse;
1217 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 return p;
1220}
1221
1222stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1224 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225{
1226 stmt_ty p;
1227 if (!test) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "field test is required for If");
1230 return NULL;
1231 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001233 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 p->kind = If_kind;
1236 p->v.If.test = test;
1237 p->v.If.body = body;
1238 p->v.If.orelse = orelse;
1239 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return p;
1242}
1243
1244stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001245With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001247{
1248 stmt_ty p;
1249 if (!context_expr) {
1250 PyErr_SetString(PyExc_ValueError,
1251 "field context_expr is required for With");
1252 return NULL;
1253 }
1254 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001255 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001256 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001257 p->kind = With_kind;
1258 p->v.With.context_expr = context_expr;
1259 p->v.With.optional_vars = optional_vars;
1260 p->v.With.body = body;
1261 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001262 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001263 return p;
1264}
1265
1266stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001267Raise(expr_ty exc, expr_ty cause, 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001274 p->v.Raise.exc = exc;
1275 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001277 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 return p;
1279}
1280
1281stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001282TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001283 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284{
1285 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001286 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001287 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 p->kind = TryExcept_kind;
1290 p->v.TryExcept.body = body;
1291 p->v.TryExcept.handlers = handlers;
1292 p->v.TryExcept.orelse = orelse;
1293 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001294 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 return p;
1296}
1297
1298stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001299TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1300 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301{
1302 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001303 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001304 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 p->kind = TryFinally_kind;
1307 p->v.TryFinally.body = body;
1308 p->v.TryFinally.finalbody = finalbody;
1309 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001310 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return p;
1312}
1313
1314stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001315Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316{
1317 stmt_ty p;
1318 if (!test) {
1319 PyErr_SetString(PyExc_ValueError,
1320 "field test is required for Assert");
1321 return NULL;
1322 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001324 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 p->kind = Assert_kind;
1327 p->v.Assert.test = test;
1328 p->v.Assert.msg = msg;
1329 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 +00001335Import(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 = Import_kind;
1342 p->v.Import.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
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001349ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1350 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351{
1352 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001353 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001354 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 p->kind = ImportFrom_kind;
1357 p->v.ImportFrom.module = module;
1358 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001359 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001361 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 return p;
1363}
1364
1365stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001366Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367{
1368 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001369 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001370 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 p->kind = Global_kind;
1373 p->v.Global.names = names;
1374 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001375 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 return p;
1377}
1378
1379stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001380Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1381{
1382 stmt_ty p;
1383 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1384 if (!p)
1385 return NULL;
1386 p->kind = Nonlocal_kind;
1387 p->v.Nonlocal.names = names;
1388 p->lineno = lineno;
1389 p->col_offset = col_offset;
1390 return p;
1391}
1392
1393stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001394Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395{
1396 stmt_ty p;
1397 if (!value) {
1398 PyErr_SetString(PyExc_ValueError,
1399 "field value is required for Expr");
1400 return NULL;
1401 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001402 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001403 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 p->kind = Expr_kind;
1406 p->v.Expr.value = value;
1407 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001408 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 return p;
1410}
1411
1412stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001413Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414{
1415 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001417 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 p->kind = Pass_kind;
1420 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return p;
1423}
1424
1425stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427{
1428 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001429 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001430 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 p->kind = Break_kind;
1433 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001434 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 return p;
1436}
1437
1438stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001439Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440{
1441 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001443 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 p->kind = Continue_kind;
1446 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001447 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return p;
1449}
1450
1451expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001452BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1453 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
1455 expr_ty p;
1456 if (!op) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "field op is required for BoolOp");
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 = BoolOp_kind;
1465 p->v.BoolOp.op = op;
1466 p->v.BoolOp.values = values;
1467 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 return p;
1470}
1471
1472expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001473BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1474 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475{
1476 expr_ty p;
1477 if (!left) {
1478 PyErr_SetString(PyExc_ValueError,
1479 "field left is required for BinOp");
1480 return NULL;
1481 }
1482 if (!op) {
1483 PyErr_SetString(PyExc_ValueError,
1484 "field op is required for BinOp");
1485 return NULL;
1486 }
1487 if (!right) {
1488 PyErr_SetString(PyExc_ValueError,
1489 "field right is required for BinOp");
1490 return NULL;
1491 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001492 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001493 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 p->kind = BinOp_kind;
1496 p->v.BinOp.left = left;
1497 p->v.BinOp.op = op;
1498 p->v.BinOp.right = right;
1499 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 return p;
1502}
1503
1504expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1506 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507{
1508 expr_ty p;
1509 if (!op) {
1510 PyErr_SetString(PyExc_ValueError,
1511 "field op is required for UnaryOp");
1512 return NULL;
1513 }
1514 if (!operand) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field operand is required for UnaryOp");
1517 return NULL;
1518 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001519 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001520 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 p->kind = UnaryOp_kind;
1523 p->v.UnaryOp.op = op;
1524 p->v.UnaryOp.operand = operand;
1525 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001526 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 return p;
1528}
1529
1530expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001531Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1532 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533{
1534 expr_ty p;
1535 if (!args) {
1536 PyErr_SetString(PyExc_ValueError,
1537 "field args is required for Lambda");
1538 return NULL;
1539 }
1540 if (!body) {
1541 PyErr_SetString(PyExc_ValueError,
1542 "field body is required for Lambda");
1543 return NULL;
1544 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001545 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001546 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 p->kind = Lambda_kind;
1549 p->v.Lambda.args = args;
1550 p->v.Lambda.body = body;
1551 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001552 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 return p;
1554}
1555
1556expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001557IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1558 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001559{
1560 expr_ty p;
1561 if (!test) {
1562 PyErr_SetString(PyExc_ValueError,
1563 "field test is required for IfExp");
1564 return NULL;
1565 }
1566 if (!body) {
1567 PyErr_SetString(PyExc_ValueError,
1568 "field body is required for IfExp");
1569 return NULL;
1570 }
1571 if (!orelse) {
1572 PyErr_SetString(PyExc_ValueError,
1573 "field orelse is required for IfExp");
1574 return NULL;
1575 }
1576 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001577 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001578 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001579 p->kind = IfExp_kind;
1580 p->v.IfExp.test = test;
1581 p->v.IfExp.body = body;
1582 p->v.IfExp.orelse = orelse;
1583 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001584 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001585 return p;
1586}
1587
1588expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1590 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591{
1592 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001593 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001594 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 p->kind = Dict_kind;
1597 p->v.Dict.keys = keys;
1598 p->v.Dict.values = values;
1599 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001600 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601 return p;
1602}
1603
1604expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001605Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1606{
1607 expr_ty p;
1608 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001609 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001610 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001611 p->kind = Set_kind;
1612 p->v.Set.elts = elts;
1613 p->lineno = lineno;
1614 p->col_offset = col_offset;
1615 return p;
1616}
1617
1618expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001619ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1620 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621{
1622 expr_ty p;
1623 if (!elt) {
1624 PyErr_SetString(PyExc_ValueError,
1625 "field elt is required for ListComp");
1626 return NULL;
1627 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001628 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001629 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 p->kind = ListComp_kind;
1632 p->v.ListComp.elt = elt;
1633 p->v.ListComp.generators = generators;
1634 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 return p;
1637}
1638
1639expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001640SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1641 *arena)
1642{
1643 expr_ty p;
1644 if (!elt) {
1645 PyErr_SetString(PyExc_ValueError,
1646 "field elt is required for SetComp");
1647 return NULL;
1648 }
1649 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1650 if (!p)
1651 return NULL;
1652 p->kind = SetComp_kind;
1653 p->v.SetComp.elt = elt;
1654 p->v.SetComp.generators = generators;
1655 p->lineno = lineno;
1656 p->col_offset = col_offset;
1657 return p;
1658}
1659
1660expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001661DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1662 col_offset, PyArena *arena)
1663{
1664 expr_ty p;
1665 if (!key) {
1666 PyErr_SetString(PyExc_ValueError,
1667 "field key is required for DictComp");
1668 return NULL;
1669 }
1670 if (!value) {
1671 PyErr_SetString(PyExc_ValueError,
1672 "field value is required for DictComp");
1673 return NULL;
1674 }
1675 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1676 if (!p)
1677 return NULL;
1678 p->kind = DictComp_kind;
1679 p->v.DictComp.key = key;
1680 p->v.DictComp.value = value;
1681 p->v.DictComp.generators = generators;
1682 p->lineno = lineno;
1683 p->col_offset = col_offset;
1684 return p;
1685}
1686
1687expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1689 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690{
1691 expr_ty p;
1692 if (!elt) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "field elt is required for GeneratorExp");
1695 return NULL;
1696 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001697 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001698 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 p->kind = GeneratorExp_kind;
1701 p->v.GeneratorExp.elt = elt;
1702 p->v.GeneratorExp.generators = generators;
1703 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001704 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 return p;
1706}
1707
1708expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
1711 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001712 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001713 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 p->kind = Yield_kind;
1716 p->v.Yield.value = value;
1717 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001718 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719 return p;
1720}
1721
1722expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001723Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1724 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725{
1726 expr_ty p;
1727 if (!left) {
1728 PyErr_SetString(PyExc_ValueError,
1729 "field left is required for Compare");
1730 return NULL;
1731 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001732 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001733 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 p->kind = Compare_kind;
1736 p->v.Compare.left = left;
1737 p->v.Compare.ops = ops;
1738 p->v.Compare.comparators = comparators;
1739 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001740 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return p;
1742}
1743
1744expr_ty
1745Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747{
1748 expr_ty p;
1749 if (!func) {
1750 PyErr_SetString(PyExc_ValueError,
1751 "field func is required for Call");
1752 return NULL;
1753 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001754 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001755 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 p->kind = Call_kind;
1758 p->v.Call.func = func;
1759 p->v.Call.args = args;
1760 p->v.Call.keywords = keywords;
1761 p->v.Call.starargs = starargs;
1762 p->v.Call.kwargs = kwargs;
1763 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001764 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 return p;
1766}
1767
1768expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001769Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770{
1771 expr_ty p;
1772 if (!n) {
1773 PyErr_SetString(PyExc_ValueError,
1774 "field n is required for Num");
1775 return NULL;
1776 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001777 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001778 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 p->kind = Num_kind;
1781 p->v.Num.n = n;
1782 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001783 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 return p;
1785}
1786
1787expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001788Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789{
1790 expr_ty p;
1791 if (!s) {
1792 PyErr_SetString(PyExc_ValueError,
1793 "field s is required for Str");
1794 return NULL;
1795 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001797 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 p->kind = Str_kind;
1800 p->v.Str.s = s;
1801 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001802 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 return p;
1804}
1805
1806expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001807Bytes(string s, int lineno, int col_offset, PyArena *arena)
1808{
1809 expr_ty p;
1810 if (!s) {
1811 PyErr_SetString(PyExc_ValueError,
1812 "field s is required for Bytes");
1813 return NULL;
1814 }
1815 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001816 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001817 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001818 p->kind = Bytes_kind;
1819 p->v.Bytes.s = s;
1820 p->lineno = lineno;
1821 p->col_offset = col_offset;
1822 return p;
1823}
1824
1825expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001826Ellipsis(int lineno, int col_offset, PyArena *arena)
1827{
1828 expr_ty p;
1829 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001830 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001831 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001832 p->kind = Ellipsis_kind;
1833 p->lineno = lineno;
1834 p->col_offset = col_offset;
1835 return p;
1836}
1837
1838expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001839Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1840 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841{
1842 expr_ty p;
1843 if (!value) {
1844 PyErr_SetString(PyExc_ValueError,
1845 "field value is required for Attribute");
1846 return NULL;
1847 }
1848 if (!attr) {
1849 PyErr_SetString(PyExc_ValueError,
1850 "field attr is required for Attribute");
1851 return NULL;
1852 }
1853 if (!ctx) {
1854 PyErr_SetString(PyExc_ValueError,
1855 "field ctx is required for Attribute");
1856 return NULL;
1857 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001859 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 p->kind = Attribute_kind;
1862 p->v.Attribute.value = value;
1863 p->v.Attribute.attr = attr;
1864 p->v.Attribute.ctx = ctx;
1865 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001866 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return p;
1868}
1869
1870expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001871Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1872 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873{
1874 expr_ty p;
1875 if (!value) {
1876 PyErr_SetString(PyExc_ValueError,
1877 "field value is required for Subscript");
1878 return NULL;
1879 }
1880 if (!slice) {
1881 PyErr_SetString(PyExc_ValueError,
1882 "field slice is required for Subscript");
1883 return NULL;
1884 }
1885 if (!ctx) {
1886 PyErr_SetString(PyExc_ValueError,
1887 "field ctx is required for Subscript");
1888 return NULL;
1889 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001891 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 p->kind = Subscript_kind;
1894 p->v.Subscript.value = value;
1895 p->v.Subscript.slice = slice;
1896 p->v.Subscript.ctx = ctx;
1897 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001898 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 return p;
1900}
1901
1902expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001903Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1904 *arena)
1905{
1906 expr_ty p;
1907 if (!value) {
1908 PyErr_SetString(PyExc_ValueError,
1909 "field value is required for Starred");
1910 return NULL;
1911 }
1912 if (!ctx) {
1913 PyErr_SetString(PyExc_ValueError,
1914 "field ctx is required for Starred");
1915 return NULL;
1916 }
1917 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1918 if (!p)
1919 return NULL;
1920 p->kind = Starred_kind;
1921 p->v.Starred.value = value;
1922 p->v.Starred.ctx = ctx;
1923 p->lineno = lineno;
1924 p->col_offset = col_offset;
1925 return p;
1926}
1927
1928expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001929Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1930 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931{
1932 expr_ty p;
1933 if (!id) {
1934 PyErr_SetString(PyExc_ValueError,
1935 "field id is required for Name");
1936 return NULL;
1937 }
1938 if (!ctx) {
1939 PyErr_SetString(PyExc_ValueError,
1940 "field ctx is required for Name");
1941 return NULL;
1942 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001943 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001944 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 p->kind = Name_kind;
1947 p->v.Name.id = id;
1948 p->v.Name.ctx = ctx;
1949 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001950 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 return p;
1952}
1953
1954expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001955List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1956 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957{
1958 expr_ty p;
1959 if (!ctx) {
1960 PyErr_SetString(PyExc_ValueError,
1961 "field ctx is required for List");
1962 return NULL;
1963 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001964 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001965 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 p->kind = List_kind;
1968 p->v.List.elts = elts;
1969 p->v.List.ctx = ctx;
1970 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001971 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return p;
1973}
1974
1975expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001976Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1977 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978{
1979 expr_ty p;
1980 if (!ctx) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field ctx is required for Tuple");
1983 return NULL;
1984 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 p->kind = Tuple_kind;
1989 p->v.Tuple.elts = elts;
1990 p->v.Tuple.ctx = ctx;
1991 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001992 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 return p;
1994}
1995
1996slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001997Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998{
1999 slice_ty p;
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 = Slice_kind;
2004 p->v.Slice.lower = lower;
2005 p->v.Slice.upper = upper;
2006 p->v.Slice.step = step;
2007 return p;
2008}
2009
2010slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002011ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012{
2013 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002014 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002015 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 p->kind = ExtSlice_kind;
2018 p->v.ExtSlice.dims = dims;
2019 return p;
2020}
2021
2022slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002023Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024{
2025 slice_ty p;
2026 if (!value) {
2027 PyErr_SetString(PyExc_ValueError,
2028 "field value is required for Index");
2029 return NULL;
2030 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002031 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002032 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 p->kind = Index_kind;
2035 p->v.Index.value = value;
2036 return p;
2037}
2038
2039comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002040comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041{
2042 comprehension_ty p;
2043 if (!target) {
2044 PyErr_SetString(PyExc_ValueError,
2045 "field target is required for comprehension");
2046 return NULL;
2047 }
2048 if (!iter) {
2049 PyErr_SetString(PyExc_ValueError,
2050 "field iter is required for comprehension");
2051 return NULL;
2052 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002053 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002054 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 p->target = target;
2057 p->iter = iter;
2058 p->ifs = ifs;
2059 return p;
2060}
2061
2062excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002063ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002064 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065{
2066 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002068 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002070 p->kind = ExceptHandler_kind;
2071 p->v.ExceptHandler.type = type;
2072 p->v.ExceptHandler.name = name;
2073 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002074 p->lineno = lineno;
2075 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 return p;
2077}
2078
2079arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002080arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2081 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2082 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083{
2084 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002085 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002086 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 p->args = args;
2089 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002090 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002091 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002093 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002095 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 return p;
2097}
2098
Neal Norwitzc1505362006-12-28 06:47:50 +00002099arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002100arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002101{
2102 arg_ty p;
2103 if (!arg) {
2104 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002105 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002106 return NULL;
2107 }
2108 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002109 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002110 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002111 p->arg = arg;
2112 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002113 return p;
2114}
2115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002117keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118{
2119 keyword_ty p;
2120 if (!arg) {
2121 PyErr_SetString(PyExc_ValueError,
2122 "field arg is required for keyword");
2123 return NULL;
2124 }
2125 if (!value) {
2126 PyErr_SetString(PyExc_ValueError,
2127 "field value is required for keyword");
2128 return NULL;
2129 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002130 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002131 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 p->arg = arg;
2134 p->value = value;
2135 return p;
2136}
2137
2138alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002139alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140{
2141 alias_ty p;
2142 if (!name) {
2143 PyErr_SetString(PyExc_ValueError,
2144 "field name is required for alias");
2145 return NULL;
2146 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002147 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002148 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 p->name = name;
2151 p->asname = asname;
2152 return p;
2153}
2154
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002155
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002156PyObject*
2157ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002158{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002159 mod_ty o = (mod_ty)_o;
2160 PyObject *result = NULL, *value = NULL;
2161 if (!o) {
2162 Py_INCREF(Py_None);
2163 return Py_None;
2164 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002165
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 switch (o->kind) {
2167 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002168 result = PyType_GenericNew(Module_type, NULL, NULL);
2169 if (!result) goto failed;
2170 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2171 if (!value) goto failed;
2172 if (PyObject_SetAttrString(result, "body", value) == -1)
2173 goto failed;
2174 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 break;
2176 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002177 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2178 if (!result) goto failed;
2179 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2180 if (!value) goto failed;
2181 if (PyObject_SetAttrString(result, "body", value) == -1)
2182 goto failed;
2183 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 break;
2185 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002186 result = PyType_GenericNew(Expression_type, NULL, NULL);
2187 if (!result) goto failed;
2188 value = ast2obj_expr(o->v.Expression.body);
2189 if (!value) goto failed;
2190 if (PyObject_SetAttrString(result, "body", value) == -1)
2191 goto failed;
2192 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 break;
2194 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002195 result = PyType_GenericNew(Suite_type, NULL, NULL);
2196 if (!result) goto failed;
2197 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2198 if (!value) goto failed;
2199 if (PyObject_SetAttrString(result, "body", value) == -1)
2200 goto failed;
2201 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 break;
2203 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002204 return result;
2205failed:
2206 Py_XDECREF(value);
2207 Py_XDECREF(result);
2208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209}
2210
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002211PyObject*
2212ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002214 stmt_ty o = (stmt_ty)_o;
2215 PyObject *result = NULL, *value = NULL;
2216 if (!o) {
2217 Py_INCREF(Py_None);
2218 return Py_None;
2219 }
2220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 switch (o->kind) {
2222 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002223 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2224 if (!result) goto failed;
2225 value = ast2obj_identifier(o->v.FunctionDef.name);
2226 if (!value) goto failed;
2227 if (PyObject_SetAttrString(result, "name", value) == -1)
2228 goto failed;
2229 Py_DECREF(value);
2230 value = ast2obj_arguments(o->v.FunctionDef.args);
2231 if (!value) goto failed;
2232 if (PyObject_SetAttrString(result, "args", value) == -1)
2233 goto failed;
2234 Py_DECREF(value);
2235 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2236 if (!value) goto failed;
2237 if (PyObject_SetAttrString(result, "body", value) == -1)
2238 goto failed;
2239 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002240 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2241 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002242 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002243 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2244 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002245 goto failed;
2246 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002247 value = ast2obj_expr(o->v.FunctionDef.returns);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "returns", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 break;
2253 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002254 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2255 if (!result) goto failed;
2256 value = ast2obj_identifier(o->v.ClassDef.name);
2257 if (!value) goto failed;
2258 if (PyObject_SetAttrString(result, "name", value) == -1)
2259 goto failed;
2260 Py_DECREF(value);
2261 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2262 if (!value) goto failed;
2263 if (PyObject_SetAttrString(result, "bases", value) == -1)
2264 goto failed;
2265 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002266 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2267 if (!value) goto failed;
2268 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2269 goto failed;
2270 Py_DECREF(value);
2271 value = ast2obj_expr(o->v.ClassDef.starargs);
2272 if (!value) goto failed;
2273 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2274 goto failed;
2275 Py_DECREF(value);
2276 value = ast2obj_expr(o->v.ClassDef.kwargs);
2277 if (!value) goto failed;
2278 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2279 goto failed;
2280 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002281 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2282 if (!value) goto failed;
2283 if (PyObject_SetAttrString(result, "body", value) == -1)
2284 goto failed;
2285 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002286 value = ast2obj_list(o->v.ClassDef.decorator_list,
2287 ast2obj_expr);
2288 if (!value) goto failed;
2289 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2290 -1)
2291 goto failed;
2292 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 break;
2294 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002295 result = PyType_GenericNew(Return_type, NULL, NULL);
2296 if (!result) goto failed;
2297 value = ast2obj_expr(o->v.Return.value);
2298 if (!value) goto failed;
2299 if (PyObject_SetAttrString(result, "value", value) == -1)
2300 goto failed;
2301 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 break;
2303 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002304 result = PyType_GenericNew(Delete_type, NULL, NULL);
2305 if (!result) goto failed;
2306 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2307 if (!value) goto failed;
2308 if (PyObject_SetAttrString(result, "targets", value) == -1)
2309 goto failed;
2310 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 break;
2312 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002313 result = PyType_GenericNew(Assign_type, NULL, NULL);
2314 if (!result) goto failed;
2315 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2316 if (!value) goto failed;
2317 if (PyObject_SetAttrString(result, "targets", value) == -1)
2318 goto failed;
2319 Py_DECREF(value);
2320 value = ast2obj_expr(o->v.Assign.value);
2321 if (!value) goto failed;
2322 if (PyObject_SetAttrString(result, "value", value) == -1)
2323 goto failed;
2324 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 break;
2326 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002327 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2328 if (!result) goto failed;
2329 value = ast2obj_expr(o->v.AugAssign.target);
2330 if (!value) goto failed;
2331 if (PyObject_SetAttrString(result, "target", value) == -1)
2332 goto failed;
2333 Py_DECREF(value);
2334 value = ast2obj_operator(o->v.AugAssign.op);
2335 if (!value) goto failed;
2336 if (PyObject_SetAttrString(result, "op", value) == -1)
2337 goto failed;
2338 Py_DECREF(value);
2339 value = ast2obj_expr(o->v.AugAssign.value);
2340 if (!value) goto failed;
2341 if (PyObject_SetAttrString(result, "value", value) == -1)
2342 goto failed;
2343 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002346 result = PyType_GenericNew(For_type, NULL, NULL);
2347 if (!result) goto failed;
2348 value = ast2obj_expr(o->v.For.target);
2349 if (!value) goto failed;
2350 if (PyObject_SetAttrString(result, "target", value) == -1)
2351 goto failed;
2352 Py_DECREF(value);
2353 value = ast2obj_expr(o->v.For.iter);
2354 if (!value) goto failed;
2355 if (PyObject_SetAttrString(result, "iter", value) == -1)
2356 goto failed;
2357 Py_DECREF(value);
2358 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "body", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2364 if (!value) goto failed;
2365 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2366 goto failed;
2367 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 break;
2369 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002370 result = PyType_GenericNew(While_type, NULL, NULL);
2371 if (!result) goto failed;
2372 value = ast2obj_expr(o->v.While.test);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "test", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
2377 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "body", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2383 if (!value) goto failed;
2384 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2385 goto failed;
2386 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 break;
2388 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002389 result = PyType_GenericNew(If_type, NULL, NULL);
2390 if (!result) goto failed;
2391 value = ast2obj_expr(o->v.If.test);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "test", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2397 if (!value) goto failed;
2398 if (PyObject_SetAttrString(result, "body", value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
2401 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2402 if (!value) goto failed;
2403 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2404 goto failed;
2405 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002407 case With_kind:
2408 result = PyType_GenericNew(With_type, NULL, NULL);
2409 if (!result) goto failed;
2410 value = ast2obj_expr(o->v.With.context_expr);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
2415 value = ast2obj_expr(o->v.With.optional_vars);
2416 if (!value) goto failed;
2417 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2418 -1)
2419 goto failed;
2420 Py_DECREF(value);
2421 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2422 if (!value) goto failed;
2423 if (PyObject_SetAttrString(result, "body", value) == -1)
2424 goto failed;
2425 Py_DECREF(value);
2426 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002428 result = PyType_GenericNew(Raise_type, NULL, NULL);
2429 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002430 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002431 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002432 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002433 goto failed;
2434 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002435 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002436 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002437 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002438 goto failed;
2439 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 break;
2441 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002442 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2443 if (!result) goto failed;
2444 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2445 if (!value) goto failed;
2446 if (PyObject_SetAttrString(result, "body", value) == -1)
2447 goto failed;
2448 Py_DECREF(value);
2449 value = ast2obj_list(o->v.TryExcept.handlers,
2450 ast2obj_excepthandler);
2451 if (!value) goto failed;
2452 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
2455 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2456 if (!value) goto failed;
2457 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2458 goto failed;
2459 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 break;
2461 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002462 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2463 if (!result) goto failed;
2464 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2465 if (!value) goto failed;
2466 if (PyObject_SetAttrString(result, "body", value) == -1)
2467 goto failed;
2468 Py_DECREF(value);
2469 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2470 if (!value) goto failed;
2471 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 break;
2475 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002476 result = PyType_GenericNew(Assert_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_expr(o->v.Assert.test);
2479 if (!value) goto failed;
2480 if (PyObject_SetAttrString(result, "test", value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 value = ast2obj_expr(o->v.Assert.msg);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "msg", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 break;
2489 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002490 result = PyType_GenericNew(Import_type, NULL, NULL);
2491 if (!result) goto failed;
2492 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2493 if (!value) goto failed;
2494 if (PyObject_SetAttrString(result, "names", value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 break;
2498 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002499 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2500 if (!result) goto failed;
2501 value = ast2obj_identifier(o->v.ImportFrom.module);
2502 if (!value) goto failed;
2503 if (PyObject_SetAttrString(result, "module", value) == -1)
2504 goto failed;
2505 Py_DECREF(value);
2506 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2507 if (!value) goto failed;
2508 if (PyObject_SetAttrString(result, "names", value) == -1)
2509 goto failed;
2510 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002511 value = ast2obj_int(o->v.ImportFrom.level);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "level", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 result = PyType_GenericNew(Global_type, NULL, NULL);
2519 if (!result) goto failed;
2520 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2521 if (!value) goto failed;
2522 if (PyObject_SetAttrString(result, "names", value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002526 case Nonlocal_kind:
2527 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2528 if (!result) goto failed;
2529 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2530 if (!value) goto failed;
2531 if (PyObject_SetAttrString(result, "names", value) == -1)
2532 goto failed;
2533 Py_DECREF(value);
2534 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002536 result = PyType_GenericNew(Expr_type, NULL, NULL);
2537 if (!result) goto failed;
2538 value = ast2obj_expr(o->v.Expr.value);
2539 if (!value) goto failed;
2540 if (PyObject_SetAttrString(result, "value", value) == -1)
2541 goto failed;
2542 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 break;
2544 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002545 result = PyType_GenericNew(Pass_type, NULL, NULL);
2546 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 break;
2548 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002549 result = PyType_GenericNew(Break_type, NULL, NULL);
2550 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 break;
2552 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002553 result = PyType_GenericNew(Continue_type, NULL, NULL);
2554 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 break;
2556 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002557 value = ast2obj_int(o->lineno);
2558 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002559 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2560 goto failed;
2561 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002562 value = ast2obj_int(o->col_offset);
2563 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002564 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2565 goto failed;
2566 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002567 return result;
2568failed:
2569 Py_XDECREF(value);
2570 Py_XDECREF(result);
2571 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572}
2573
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002574PyObject*
2575ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002577 expr_ty o = (expr_ty)_o;
2578 PyObject *result = NULL, *value = NULL;
2579 if (!o) {
2580 Py_INCREF(Py_None);
2581 return Py_None;
2582 }
2583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 switch (o->kind) {
2585 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002586 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2587 if (!result) goto failed;
2588 value = ast2obj_boolop(o->v.BoolOp.op);
2589 if (!value) goto failed;
2590 if (PyObject_SetAttrString(result, "op", value) == -1)
2591 goto failed;
2592 Py_DECREF(value);
2593 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2594 if (!value) goto failed;
2595 if (PyObject_SetAttrString(result, "values", value) == -1)
2596 goto failed;
2597 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 break;
2599 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002600 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2601 if (!result) goto failed;
2602 value = ast2obj_expr(o->v.BinOp.left);
2603 if (!value) goto failed;
2604 if (PyObject_SetAttrString(result, "left", value) == -1)
2605 goto failed;
2606 Py_DECREF(value);
2607 value = ast2obj_operator(o->v.BinOp.op);
2608 if (!value) goto failed;
2609 if (PyObject_SetAttrString(result, "op", value) == -1)
2610 goto failed;
2611 Py_DECREF(value);
2612 value = ast2obj_expr(o->v.BinOp.right);
2613 if (!value) goto failed;
2614 if (PyObject_SetAttrString(result, "right", value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 break;
2618 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002619 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2620 if (!result) goto failed;
2621 value = ast2obj_unaryop(o->v.UnaryOp.op);
2622 if (!value) goto failed;
2623 if (PyObject_SetAttrString(result, "op", value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
2626 value = ast2obj_expr(o->v.UnaryOp.operand);
2627 if (!value) goto failed;
2628 if (PyObject_SetAttrString(result, "operand", value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 break;
2632 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002633 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2634 if (!result) goto failed;
2635 value = ast2obj_arguments(o->v.Lambda.args);
2636 if (!value) goto failed;
2637 if (PyObject_SetAttrString(result, "args", value) == -1)
2638 goto failed;
2639 Py_DECREF(value);
2640 value = ast2obj_expr(o->v.Lambda.body);
2641 if (!value) goto failed;
2642 if (PyObject_SetAttrString(result, "body", value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002646 case IfExp_kind:
2647 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2648 if (!result) goto failed;
2649 value = ast2obj_expr(o->v.IfExp.test);
2650 if (!value) goto failed;
2651 if (PyObject_SetAttrString(result, "test", value) == -1)
2652 goto failed;
2653 Py_DECREF(value);
2654 value = ast2obj_expr(o->v.IfExp.body);
2655 if (!value) goto failed;
2656 if (PyObject_SetAttrString(result, "body", value) == -1)
2657 goto failed;
2658 Py_DECREF(value);
2659 value = ast2obj_expr(o->v.IfExp.orelse);
2660 if (!value) goto failed;
2661 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002666 result = PyType_GenericNew(Dict_type, NULL, NULL);
2667 if (!result) goto failed;
2668 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2669 if (!value) goto failed;
2670 if (PyObject_SetAttrString(result, "keys", value) == -1)
2671 goto failed;
2672 Py_DECREF(value);
2673 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2674 if (!value) goto failed;
2675 if (PyObject_SetAttrString(result, "values", value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002679 case Set_kind:
2680 result = PyType_GenericNew(Set_type, NULL, NULL);
2681 if (!result) goto failed;
2682 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2683 if (!value) goto failed;
2684 if (PyObject_SetAttrString(result, "elts", value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002689 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2690 if (!result) goto failed;
2691 value = ast2obj_expr(o->v.ListComp.elt);
2692 if (!value) goto failed;
2693 if (PyObject_SetAttrString(result, "elt", value) == -1)
2694 goto failed;
2695 Py_DECREF(value);
2696 value = ast2obj_list(o->v.ListComp.generators,
2697 ast2obj_comprehension);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "generators", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002703 case SetComp_kind:
2704 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2705 if (!result) goto failed;
2706 value = ast2obj_expr(o->v.SetComp.elt);
2707 if (!value) goto failed;
2708 if (PyObject_SetAttrString(result, "elt", value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
2711 value = ast2obj_list(o->v.SetComp.generators,
2712 ast2obj_comprehension);
2713 if (!value) goto failed;
2714 if (PyObject_SetAttrString(result, "generators", value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002718 case DictComp_kind:
2719 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2720 if (!result) goto failed;
2721 value = ast2obj_expr(o->v.DictComp.key);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "key", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 value = ast2obj_expr(o->v.DictComp.value);
2727 if (!value) goto failed;
2728 if (PyObject_SetAttrString(result, "value", value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
2731 value = ast2obj_list(o->v.DictComp.generators,
2732 ast2obj_comprehension);
2733 if (!value) goto failed;
2734 if (PyObject_SetAttrString(result, "generators", value) == -1)
2735 goto failed;
2736 Py_DECREF(value);
2737 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002739 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2740 if (!result) goto failed;
2741 value = ast2obj_expr(o->v.GeneratorExp.elt);
2742 if (!value) goto failed;
2743 if (PyObject_SetAttrString(result, "elt", value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 value = ast2obj_list(o->v.GeneratorExp.generators,
2747 ast2obj_comprehension);
2748 if (!value) goto failed;
2749 if (PyObject_SetAttrString(result, "generators", value) == -1)
2750 goto failed;
2751 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 break;
2753 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002754 result = PyType_GenericNew(Yield_type, NULL, NULL);
2755 if (!result) goto failed;
2756 value = ast2obj_expr(o->v.Yield.value);
2757 if (!value) goto failed;
2758 if (PyObject_SetAttrString(result, "value", value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 break;
2762 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002763 result = PyType_GenericNew(Compare_type, NULL, NULL);
2764 if (!result) goto failed;
2765 value = ast2obj_expr(o->v.Compare.left);
2766 if (!value) goto failed;
2767 if (PyObject_SetAttrString(result, "left", value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002770 {
2771 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2772 value = PyList_New(n);
2773 if (!value) goto failed;
2774 for(i = 0; i < n; i++)
2775 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2776 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002777 if (!value) goto failed;
2778 if (PyObject_SetAttrString(result, "ops", value) == -1)
2779 goto failed;
2780 Py_DECREF(value);
2781 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2782 if (!value) goto failed;
2783 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2784 goto failed;
2785 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 break;
2787 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002788 result = PyType_GenericNew(Call_type, NULL, NULL);
2789 if (!result) goto failed;
2790 value = ast2obj_expr(o->v.Call.func);
2791 if (!value) goto failed;
2792 if (PyObject_SetAttrString(result, "func", value) == -1)
2793 goto failed;
2794 Py_DECREF(value);
2795 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2796 if (!value) goto failed;
2797 if (PyObject_SetAttrString(result, "args", value) == -1)
2798 goto failed;
2799 Py_DECREF(value);
2800 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2801 if (!value) goto failed;
2802 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2803 goto failed;
2804 Py_DECREF(value);
2805 value = ast2obj_expr(o->v.Call.starargs);
2806 if (!value) goto failed;
2807 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2808 goto failed;
2809 Py_DECREF(value);
2810 value = ast2obj_expr(o->v.Call.kwargs);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002817 result = PyType_GenericNew(Num_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_object(o->v.Num.n);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "n", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 break;
2825 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002826 result = PyType_GenericNew(Str_type, NULL, NULL);
2827 if (!result) goto failed;
2828 value = ast2obj_string(o->v.Str.s);
2829 if (!value) goto failed;
2830 if (PyObject_SetAttrString(result, "s", value) == -1)
2831 goto failed;
2832 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002834 case Bytes_kind:
2835 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2836 if (!result) goto failed;
2837 value = ast2obj_string(o->v.Bytes.s);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "s", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002843 case Ellipsis_kind:
2844 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2845 if (!result) goto failed;
2846 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002848 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2849 if (!result) goto failed;
2850 value = ast2obj_expr(o->v.Attribute.value);
2851 if (!value) goto failed;
2852 if (PyObject_SetAttrString(result, "value", value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 value = ast2obj_identifier(o->v.Attribute.attr);
2856 if (!value) goto failed;
2857 if (PyObject_SetAttrString(result, "attr", value) == -1)
2858 goto failed;
2859 Py_DECREF(value);
2860 value = ast2obj_expr_context(o->v.Attribute.ctx);
2861 if (!value) goto failed;
2862 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 break;
2866 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002867 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2868 if (!result) goto failed;
2869 value = ast2obj_expr(o->v.Subscript.value);
2870 if (!value) goto failed;
2871 if (PyObject_SetAttrString(result, "value", value) == -1)
2872 goto failed;
2873 Py_DECREF(value);
2874 value = ast2obj_slice(o->v.Subscript.slice);
2875 if (!value) goto failed;
2876 if (PyObject_SetAttrString(result, "slice", value) == -1)
2877 goto failed;
2878 Py_DECREF(value);
2879 value = ast2obj_expr_context(o->v.Subscript.ctx);
2880 if (!value) goto failed;
2881 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002885 case Starred_kind:
2886 result = PyType_GenericNew(Starred_type, NULL, NULL);
2887 if (!result) goto failed;
2888 value = ast2obj_expr(o->v.Starred.value);
2889 if (!value) goto failed;
2890 if (PyObject_SetAttrString(result, "value", value) == -1)
2891 goto failed;
2892 Py_DECREF(value);
2893 value = ast2obj_expr_context(o->v.Starred.ctx);
2894 if (!value) goto failed;
2895 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2896 goto failed;
2897 Py_DECREF(value);
2898 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002900 result = PyType_GenericNew(Name_type, NULL, NULL);
2901 if (!result) goto failed;
2902 value = ast2obj_identifier(o->v.Name.id);
2903 if (!value) goto failed;
2904 if (PyObject_SetAttrString(result, "id", value) == -1)
2905 goto failed;
2906 Py_DECREF(value);
2907 value = ast2obj_expr_context(o->v.Name.ctx);
2908 if (!value) goto failed;
2909 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2910 goto failed;
2911 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 break;
2913 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002914 result = PyType_GenericNew(List_type, NULL, NULL);
2915 if (!result) goto failed;
2916 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2917 if (!value) goto failed;
2918 if (PyObject_SetAttrString(result, "elts", value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
2921 value = ast2obj_expr_context(o->v.List.ctx);
2922 if (!value) goto failed;
2923 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 break;
2927 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002928 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2929 if (!result) goto failed;
2930 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2931 if (!value) goto failed;
2932 if (PyObject_SetAttrString(result, "elts", value) == -1)
2933 goto failed;
2934 Py_DECREF(value);
2935 value = ast2obj_expr_context(o->v.Tuple.ctx);
2936 if (!value) goto failed;
2937 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2938 goto failed;
2939 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 break;
2941 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002942 value = ast2obj_int(o->lineno);
2943 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002944 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2945 goto failed;
2946 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002947 value = ast2obj_int(o->col_offset);
2948 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002949 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2950 goto failed;
2951 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002952 return result;
2953failed:
2954 Py_XDECREF(value);
2955 Py_XDECREF(result);
2956 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957}
2958
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002959PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002961 switch(o) {
2962 case Load:
2963 Py_INCREF(Load_singleton);
2964 return Load_singleton;
2965 case Store:
2966 Py_INCREF(Store_singleton);
2967 return Store_singleton;
2968 case Del:
2969 Py_INCREF(Del_singleton);
2970 return Del_singleton;
2971 case AugLoad:
2972 Py_INCREF(AugLoad_singleton);
2973 return AugLoad_singleton;
2974 case AugStore:
2975 Py_INCREF(AugStore_singleton);
2976 return AugStore_singleton;
2977 case Param:
2978 Py_INCREF(Param_singleton);
2979 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002980 default:
2981 /* should never happen, but just in case ... */
2982 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002986PyObject*
2987ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002989 slice_ty o = (slice_ty)_o;
2990 PyObject *result = NULL, *value = NULL;
2991 if (!o) {
2992 Py_INCREF(Py_None);
2993 return Py_None;
2994 }
2995
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002998 result = PyType_GenericNew(Slice_type, NULL, NULL);
2999 if (!result) goto failed;
3000 value = ast2obj_expr(o->v.Slice.lower);
3001 if (!value) goto failed;
3002 if (PyObject_SetAttrString(result, "lower", value) == -1)
3003 goto failed;
3004 Py_DECREF(value);
3005 value = ast2obj_expr(o->v.Slice.upper);
3006 if (!value) goto failed;
3007 if (PyObject_SetAttrString(result, "upper", value) == -1)
3008 goto failed;
3009 Py_DECREF(value);
3010 value = ast2obj_expr(o->v.Slice.step);
3011 if (!value) goto failed;
3012 if (PyObject_SetAttrString(result, "step", value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 break;
3016 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003017 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3020 if (!value) goto failed;
3021 if (PyObject_SetAttrString(result, "dims", value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 break;
3025 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003026 result = PyType_GenericNew(Index_type, NULL, NULL);
3027 if (!result) goto failed;
3028 value = ast2obj_expr(o->v.Index.value);
3029 if (!value) goto failed;
3030 if (PyObject_SetAttrString(result, "value", value) == -1)
3031 goto failed;
3032 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 break;
3034 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003035 return result;
3036failed:
3037 Py_XDECREF(value);
3038 Py_XDECREF(result);
3039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040}
3041
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003042PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003044 switch(o) {
3045 case And:
3046 Py_INCREF(And_singleton);
3047 return And_singleton;
3048 case Or:
3049 Py_INCREF(Or_singleton);
3050 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003051 default:
3052 /* should never happen, but just in case ... */
3053 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003056}
3057PyObject* ast2obj_operator(operator_ty o)
3058{
3059 switch(o) {
3060 case Add:
3061 Py_INCREF(Add_singleton);
3062 return Add_singleton;
3063 case Sub:
3064 Py_INCREF(Sub_singleton);
3065 return Sub_singleton;
3066 case Mult:
3067 Py_INCREF(Mult_singleton);
3068 return Mult_singleton;
3069 case Div:
3070 Py_INCREF(Div_singleton);
3071 return Div_singleton;
3072 case Mod:
3073 Py_INCREF(Mod_singleton);
3074 return Mod_singleton;
3075 case Pow:
3076 Py_INCREF(Pow_singleton);
3077 return Pow_singleton;
3078 case LShift:
3079 Py_INCREF(LShift_singleton);
3080 return LShift_singleton;
3081 case RShift:
3082 Py_INCREF(RShift_singleton);
3083 return RShift_singleton;
3084 case BitOr:
3085 Py_INCREF(BitOr_singleton);
3086 return BitOr_singleton;
3087 case BitXor:
3088 Py_INCREF(BitXor_singleton);
3089 return BitXor_singleton;
3090 case BitAnd:
3091 Py_INCREF(BitAnd_singleton);
3092 return BitAnd_singleton;
3093 case FloorDiv:
3094 Py_INCREF(FloorDiv_singleton);
3095 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003096 default:
3097 /* should never happen, but just in case ... */
3098 PyErr_Format(PyExc_SystemError, "unknown operator found");
3099 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003100 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003101}
3102PyObject* ast2obj_unaryop(unaryop_ty o)
3103{
3104 switch(o) {
3105 case Invert:
3106 Py_INCREF(Invert_singleton);
3107 return Invert_singleton;
3108 case Not:
3109 Py_INCREF(Not_singleton);
3110 return Not_singleton;
3111 case UAdd:
3112 Py_INCREF(UAdd_singleton);
3113 return UAdd_singleton;
3114 case USub:
3115 Py_INCREF(USub_singleton);
3116 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003117 default:
3118 /* should never happen, but just in case ... */
3119 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3120 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003121 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003122}
3123PyObject* ast2obj_cmpop(cmpop_ty o)
3124{
3125 switch(o) {
3126 case Eq:
3127 Py_INCREF(Eq_singleton);
3128 return Eq_singleton;
3129 case NotEq:
3130 Py_INCREF(NotEq_singleton);
3131 return NotEq_singleton;
3132 case Lt:
3133 Py_INCREF(Lt_singleton);
3134 return Lt_singleton;
3135 case LtE:
3136 Py_INCREF(LtE_singleton);
3137 return LtE_singleton;
3138 case Gt:
3139 Py_INCREF(Gt_singleton);
3140 return Gt_singleton;
3141 case GtE:
3142 Py_INCREF(GtE_singleton);
3143 return GtE_singleton;
3144 case Is:
3145 Py_INCREF(Is_singleton);
3146 return Is_singleton;
3147 case IsNot:
3148 Py_INCREF(IsNot_singleton);
3149 return IsNot_singleton;
3150 case In:
3151 Py_INCREF(In_singleton);
3152 return In_singleton;
3153 case NotIn:
3154 Py_INCREF(NotIn_singleton);
3155 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003156 default:
3157 /* should never happen, but just in case ... */
3158 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3159 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003160 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003161}
3162PyObject*
3163ast2obj_comprehension(void* _o)
3164{
3165 comprehension_ty o = (comprehension_ty)_o;
3166 PyObject *result = NULL, *value = NULL;
3167 if (!o) {
3168 Py_INCREF(Py_None);
3169 return Py_None;
3170 }
3171
3172 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3173 if (!result) return NULL;
3174 value = ast2obj_expr(o->target);
3175 if (!value) goto failed;
3176 if (PyObject_SetAttrString(result, "target", value) == -1)
3177 goto failed;
3178 Py_DECREF(value);
3179 value = ast2obj_expr(o->iter);
3180 if (!value) goto failed;
3181 if (PyObject_SetAttrString(result, "iter", value) == -1)
3182 goto failed;
3183 Py_DECREF(value);
3184 value = ast2obj_list(o->ifs, ast2obj_expr);
3185 if (!value) goto failed;
3186 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3187 goto failed;
3188 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003189 return result;
3190failed:
3191 Py_XDECREF(value);
3192 Py_XDECREF(result);
3193 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194}
3195
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003196PyObject*
3197ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003199 excepthandler_ty o = (excepthandler_ty)_o;
3200 PyObject *result = NULL, *value = NULL;
3201 if (!o) {
3202 Py_INCREF(Py_None);
3203 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003205
Neal Norwitzad74aa82008-03-31 05:14:30 +00003206 switch (o->kind) {
3207 case ExceptHandler_kind:
3208 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3209 if (!result) goto failed;
3210 value = ast2obj_expr(o->v.ExceptHandler.type);
3211 if (!value) goto failed;
3212 if (PyObject_SetAttrString(result, "type", value) == -1)
3213 goto failed;
3214 Py_DECREF(value);
3215 value = ast2obj_identifier(o->v.ExceptHandler.name);
3216 if (!value) goto failed;
3217 if (PyObject_SetAttrString(result, "name", value) == -1)
3218 goto failed;
3219 Py_DECREF(value);
3220 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3221 if (!value) goto failed;
3222 if (PyObject_SetAttrString(result, "body", value) == -1)
3223 goto failed;
3224 Py_DECREF(value);
3225 break;
3226 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003227 value = ast2obj_int(o->lineno);
3228 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003229 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003230 goto failed;
3231 Py_DECREF(value);
3232 value = ast2obj_int(o->col_offset);
3233 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003234 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003235 goto failed;
3236 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003237 return result;
3238failed:
3239 Py_XDECREF(value);
3240 Py_XDECREF(result);
3241 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242}
3243
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003244PyObject*
3245ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003247 arguments_ty o = (arguments_ty)_o;
3248 PyObject *result = NULL, *value = NULL;
3249 if (!o) {
3250 Py_INCREF(Py_None);
3251 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003253
3254 result = PyType_GenericNew(arguments_type, NULL, NULL);
3255 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003256 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003257 if (!value) goto failed;
3258 if (PyObject_SetAttrString(result, "args", value) == -1)
3259 goto failed;
3260 Py_DECREF(value);
3261 value = ast2obj_identifier(o->vararg);
3262 if (!value) goto failed;
3263 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3264 goto failed;
3265 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003266 value = ast2obj_expr(o->varargannotation);
3267 if (!value) goto failed;
3268 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3269 goto failed;
3270 Py_DECREF(value);
3271 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003272 if (!value) goto failed;
3273 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3274 goto failed;
3275 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003276 value = ast2obj_identifier(o->kwarg);
3277 if (!value) goto failed;
3278 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3279 goto failed;
3280 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003281 value = ast2obj_expr(o->kwargannotation);
3282 if (!value) goto failed;
3283 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3284 goto failed;
3285 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003286 value = ast2obj_list(o->defaults, ast2obj_expr);
3287 if (!value) goto failed;
3288 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3289 goto failed;
3290 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003291 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3292 if (!value) goto failed;
3293 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3294 goto failed;
3295 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003296 return result;
3297failed:
3298 Py_XDECREF(value);
3299 Py_XDECREF(result);
3300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301}
3302
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003303PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003304ast2obj_arg(void* _o)
3305{
3306 arg_ty o = (arg_ty)_o;
3307 PyObject *result = NULL, *value = NULL;
3308 if (!o) {
3309 Py_INCREF(Py_None);
3310 return Py_None;
3311 }
3312
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003313 result = PyType_GenericNew(arg_type, NULL, NULL);
3314 if (!result) return NULL;
3315 value = ast2obj_identifier(o->arg);
3316 if (!value) goto failed;
3317 if (PyObject_SetAttrString(result, "arg", value) == -1)
3318 goto failed;
3319 Py_DECREF(value);
3320 value = ast2obj_expr(o->annotation);
3321 if (!value) goto failed;
3322 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3323 goto failed;
3324 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003325 return result;
3326failed:
3327 Py_XDECREF(value);
3328 Py_XDECREF(result);
3329 return NULL;
3330}
3331
3332PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003333ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003335 keyword_ty o = (keyword_ty)_o;
3336 PyObject *result = NULL, *value = NULL;
3337 if (!o) {
3338 Py_INCREF(Py_None);
3339 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003341
3342 result = PyType_GenericNew(keyword_type, NULL, NULL);
3343 if (!result) return NULL;
3344 value = ast2obj_identifier(o->arg);
3345 if (!value) goto failed;
3346 if (PyObject_SetAttrString(result, "arg", value) == -1)
3347 goto failed;
3348 Py_DECREF(value);
3349 value = ast2obj_expr(o->value);
3350 if (!value) goto failed;
3351 if (PyObject_SetAttrString(result, "value", value) == -1)
3352 goto failed;
3353 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003354 return result;
3355failed:
3356 Py_XDECREF(value);
3357 Py_XDECREF(result);
3358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359}
3360
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003361PyObject*
3362ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003364 alias_ty o = (alias_ty)_o;
3365 PyObject *result = NULL, *value = NULL;
3366 if (!o) {
3367 Py_INCREF(Py_None);
3368 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003370
3371 result = PyType_GenericNew(alias_type, NULL, NULL);
3372 if (!result) return NULL;
3373 value = ast2obj_identifier(o->name);
3374 if (!value) goto failed;
3375 if (PyObject_SetAttrString(result, "name", value) == -1)
3376 goto failed;
3377 Py_DECREF(value);
3378 value = ast2obj_identifier(o->asname);
3379 if (!value) goto failed;
3380 if (PyObject_SetAttrString(result, "asname", value) == -1)
3381 goto failed;
3382 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003383 return result;
3384failed:
3385 Py_XDECREF(value);
3386 Py_XDECREF(result);
3387 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388}
3389
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003391int
3392obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3393{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003394 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003395
Benjamin Petersond8f65972010-11-20 04:31:07 +00003396 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003397
3398 if (obj == Py_None) {
3399 *out = NULL;
3400 return 0;
3401 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003402 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3403 if (isinstance == -1) {
3404 return 1;
3405 }
3406 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003407 asdl_seq* body;
3408
3409 if (PyObject_HasAttrString(obj, "body")) {
3410 int res;
3411 Py_ssize_t len;
3412 Py_ssize_t i;
3413 tmp = PyObject_GetAttrString(obj, "body");
3414 if (tmp == NULL) goto failed;
3415 if (!PyList_Check(tmp)) {
3416 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3417 goto failed;
3418 }
3419 len = PyList_GET_SIZE(tmp);
3420 body = asdl_seq_new(len, arena);
3421 if (body == NULL) goto failed;
3422 for (i = 0; i < len; i++) {
3423 stmt_ty value;
3424 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3425 if (res != 0) goto failed;
3426 asdl_seq_SET(body, i, value);
3427 }
3428 Py_XDECREF(tmp);
3429 tmp = NULL;
3430 } else {
3431 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3432 return 1;
3433 }
3434 *out = Module(body, arena);
3435 if (*out == NULL) goto failed;
3436 return 0;
3437 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003438 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3439 if (isinstance == -1) {
3440 return 1;
3441 }
3442 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003443 asdl_seq* body;
3444
3445 if (PyObject_HasAttrString(obj, "body")) {
3446 int res;
3447 Py_ssize_t len;
3448 Py_ssize_t i;
3449 tmp = PyObject_GetAttrString(obj, "body");
3450 if (tmp == NULL) goto failed;
3451 if (!PyList_Check(tmp)) {
3452 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3453 goto failed;
3454 }
3455 len = PyList_GET_SIZE(tmp);
3456 body = asdl_seq_new(len, arena);
3457 if (body == NULL) goto failed;
3458 for (i = 0; i < len; i++) {
3459 stmt_ty value;
3460 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3461 if (res != 0) goto failed;
3462 asdl_seq_SET(body, i, value);
3463 }
3464 Py_XDECREF(tmp);
3465 tmp = NULL;
3466 } else {
3467 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3468 return 1;
3469 }
3470 *out = Interactive(body, arena);
3471 if (*out == NULL) goto failed;
3472 return 0;
3473 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003474 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3475 if (isinstance == -1) {
3476 return 1;
3477 }
3478 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003479 expr_ty body;
3480
3481 if (PyObject_HasAttrString(obj, "body")) {
3482 int res;
3483 tmp = PyObject_GetAttrString(obj, "body");
3484 if (tmp == NULL) goto failed;
3485 res = obj2ast_expr(tmp, &body, arena);
3486 if (res != 0) goto failed;
3487 Py_XDECREF(tmp);
3488 tmp = NULL;
3489 } else {
3490 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3491 return 1;
3492 }
3493 *out = Expression(body, arena);
3494 if (*out == NULL) goto failed;
3495 return 0;
3496 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003497 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3498 if (isinstance == -1) {
3499 return 1;
3500 }
3501 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003502 asdl_seq* body;
3503
3504 if (PyObject_HasAttrString(obj, "body")) {
3505 int res;
3506 Py_ssize_t len;
3507 Py_ssize_t i;
3508 tmp = PyObject_GetAttrString(obj, "body");
3509 if (tmp == NULL) goto failed;
3510 if (!PyList_Check(tmp)) {
3511 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3512 goto failed;
3513 }
3514 len = PyList_GET_SIZE(tmp);
3515 body = asdl_seq_new(len, arena);
3516 if (body == NULL) goto failed;
3517 for (i = 0; i < len; i++) {
3518 stmt_ty value;
3519 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3520 if (res != 0) goto failed;
3521 asdl_seq_SET(body, i, value);
3522 }
3523 Py_XDECREF(tmp);
3524 tmp = NULL;
3525 } else {
3526 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3527 return 1;
3528 }
3529 *out = Suite(body, arena);
3530 if (*out == NULL) goto failed;
3531 return 0;
3532 }
3533
Benjamin Peterson5b066812010-11-20 01:38:49 +00003534 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3535 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003536 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003537 return 1;
3538}
3539
3540int
3541obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3542{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003543 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003544
Benjamin Petersond8f65972010-11-20 04:31:07 +00003545 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003546 int lineno;
3547 int col_offset;
3548
3549 if (obj == Py_None) {
3550 *out = NULL;
3551 return 0;
3552 }
3553 if (PyObject_HasAttrString(obj, "lineno")) {
3554 int res;
3555 tmp = PyObject_GetAttrString(obj, "lineno");
3556 if (tmp == NULL) goto failed;
3557 res = obj2ast_int(tmp, &lineno, arena);
3558 if (res != 0) goto failed;
3559 Py_XDECREF(tmp);
3560 tmp = NULL;
3561 } else {
3562 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3563 return 1;
3564 }
3565 if (PyObject_HasAttrString(obj, "col_offset")) {
3566 int res;
3567 tmp = PyObject_GetAttrString(obj, "col_offset");
3568 if (tmp == NULL) goto failed;
3569 res = obj2ast_int(tmp, &col_offset, arena);
3570 if (res != 0) goto failed;
3571 Py_XDECREF(tmp);
3572 tmp = NULL;
3573 } else {
3574 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3575 return 1;
3576 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003577 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3578 if (isinstance == -1) {
3579 return 1;
3580 }
3581 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003582 identifier name;
3583 arguments_ty args;
3584 asdl_seq* body;
3585 asdl_seq* decorator_list;
3586 expr_ty returns;
3587
3588 if (PyObject_HasAttrString(obj, "name")) {
3589 int res;
3590 tmp = PyObject_GetAttrString(obj, "name");
3591 if (tmp == NULL) goto failed;
3592 res = obj2ast_identifier(tmp, &name, arena);
3593 if (res != 0) goto failed;
3594 Py_XDECREF(tmp);
3595 tmp = NULL;
3596 } else {
3597 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3598 return 1;
3599 }
3600 if (PyObject_HasAttrString(obj, "args")) {
3601 int res;
3602 tmp = PyObject_GetAttrString(obj, "args");
3603 if (tmp == NULL) goto failed;
3604 res = obj2ast_arguments(tmp, &args, arena);
3605 if (res != 0) goto failed;
3606 Py_XDECREF(tmp);
3607 tmp = NULL;
3608 } else {
3609 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3610 return 1;
3611 }
3612 if (PyObject_HasAttrString(obj, "body")) {
3613 int res;
3614 Py_ssize_t len;
3615 Py_ssize_t i;
3616 tmp = PyObject_GetAttrString(obj, "body");
3617 if (tmp == NULL) goto failed;
3618 if (!PyList_Check(tmp)) {
3619 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3620 goto failed;
3621 }
3622 len = PyList_GET_SIZE(tmp);
3623 body = asdl_seq_new(len, arena);
3624 if (body == NULL) goto failed;
3625 for (i = 0; i < len; i++) {
3626 stmt_ty value;
3627 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3628 if (res != 0) goto failed;
3629 asdl_seq_SET(body, i, value);
3630 }
3631 Py_XDECREF(tmp);
3632 tmp = NULL;
3633 } else {
3634 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3635 return 1;
3636 }
3637 if (PyObject_HasAttrString(obj, "decorator_list")) {
3638 int res;
3639 Py_ssize_t len;
3640 Py_ssize_t i;
3641 tmp = PyObject_GetAttrString(obj, "decorator_list");
3642 if (tmp == NULL) goto failed;
3643 if (!PyList_Check(tmp)) {
3644 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3645 goto failed;
3646 }
3647 len = PyList_GET_SIZE(tmp);
3648 decorator_list = asdl_seq_new(len, arena);
3649 if (decorator_list == NULL) goto failed;
3650 for (i = 0; i < len; i++) {
3651 expr_ty value;
3652 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3653 if (res != 0) goto failed;
3654 asdl_seq_SET(decorator_list, i, value);
3655 }
3656 Py_XDECREF(tmp);
3657 tmp = NULL;
3658 } else {
3659 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3660 return 1;
3661 }
3662 if (PyObject_HasAttrString(obj, "returns")) {
3663 int res;
3664 tmp = PyObject_GetAttrString(obj, "returns");
3665 if (tmp == NULL) goto failed;
3666 res = obj2ast_expr(tmp, &returns, arena);
3667 if (res != 0) goto failed;
3668 Py_XDECREF(tmp);
3669 tmp = NULL;
3670 } else {
3671 returns = NULL;
3672 }
3673 *out = FunctionDef(name, args, body, decorator_list, returns,
3674 lineno, col_offset, arena);
3675 if (*out == NULL) goto failed;
3676 return 0;
3677 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003678 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3679 if (isinstance == -1) {
3680 return 1;
3681 }
3682 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003683 identifier name;
3684 asdl_seq* bases;
3685 asdl_seq* keywords;
3686 expr_ty starargs;
3687 expr_ty kwargs;
3688 asdl_seq* body;
3689 asdl_seq* decorator_list;
3690
3691 if (PyObject_HasAttrString(obj, "name")) {
3692 int res;
3693 tmp = PyObject_GetAttrString(obj, "name");
3694 if (tmp == NULL) goto failed;
3695 res = obj2ast_identifier(tmp, &name, arena);
3696 if (res != 0) goto failed;
3697 Py_XDECREF(tmp);
3698 tmp = NULL;
3699 } else {
3700 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3701 return 1;
3702 }
3703 if (PyObject_HasAttrString(obj, "bases")) {
3704 int res;
3705 Py_ssize_t len;
3706 Py_ssize_t i;
3707 tmp = PyObject_GetAttrString(obj, "bases");
3708 if (tmp == NULL) goto failed;
3709 if (!PyList_Check(tmp)) {
3710 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3711 goto failed;
3712 }
3713 len = PyList_GET_SIZE(tmp);
3714 bases = asdl_seq_new(len, arena);
3715 if (bases == NULL) goto failed;
3716 for (i = 0; i < len; i++) {
3717 expr_ty value;
3718 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3719 if (res != 0) goto failed;
3720 asdl_seq_SET(bases, i, value);
3721 }
3722 Py_XDECREF(tmp);
3723 tmp = NULL;
3724 } else {
3725 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3726 return 1;
3727 }
3728 if (PyObject_HasAttrString(obj, "keywords")) {
3729 int res;
3730 Py_ssize_t len;
3731 Py_ssize_t i;
3732 tmp = PyObject_GetAttrString(obj, "keywords");
3733 if (tmp == NULL) goto failed;
3734 if (!PyList_Check(tmp)) {
3735 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3736 goto failed;
3737 }
3738 len = PyList_GET_SIZE(tmp);
3739 keywords = asdl_seq_new(len, arena);
3740 if (keywords == NULL) goto failed;
3741 for (i = 0; i < len; i++) {
3742 keyword_ty value;
3743 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3744 if (res != 0) goto failed;
3745 asdl_seq_SET(keywords, i, value);
3746 }
3747 Py_XDECREF(tmp);
3748 tmp = NULL;
3749 } else {
3750 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3751 return 1;
3752 }
3753 if (PyObject_HasAttrString(obj, "starargs")) {
3754 int res;
3755 tmp = PyObject_GetAttrString(obj, "starargs");
3756 if (tmp == NULL) goto failed;
3757 res = obj2ast_expr(tmp, &starargs, arena);
3758 if (res != 0) goto failed;
3759 Py_XDECREF(tmp);
3760 tmp = NULL;
3761 } else {
3762 starargs = NULL;
3763 }
3764 if (PyObject_HasAttrString(obj, "kwargs")) {
3765 int res;
3766 tmp = PyObject_GetAttrString(obj, "kwargs");
3767 if (tmp == NULL) goto failed;
3768 res = obj2ast_expr(tmp, &kwargs, arena);
3769 if (res != 0) goto failed;
3770 Py_XDECREF(tmp);
3771 tmp = NULL;
3772 } else {
3773 kwargs = NULL;
3774 }
3775 if (PyObject_HasAttrString(obj, "body")) {
3776 int res;
3777 Py_ssize_t len;
3778 Py_ssize_t i;
3779 tmp = PyObject_GetAttrString(obj, "body");
3780 if (tmp == NULL) goto failed;
3781 if (!PyList_Check(tmp)) {
3782 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3783 goto failed;
3784 }
3785 len = PyList_GET_SIZE(tmp);
3786 body = asdl_seq_new(len, arena);
3787 if (body == NULL) goto failed;
3788 for (i = 0; i < len; i++) {
3789 stmt_ty value;
3790 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3791 if (res != 0) goto failed;
3792 asdl_seq_SET(body, i, value);
3793 }
3794 Py_XDECREF(tmp);
3795 tmp = NULL;
3796 } else {
3797 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3798 return 1;
3799 }
3800 if (PyObject_HasAttrString(obj, "decorator_list")) {
3801 int res;
3802 Py_ssize_t len;
3803 Py_ssize_t i;
3804 tmp = PyObject_GetAttrString(obj, "decorator_list");
3805 if (tmp == NULL) goto failed;
3806 if (!PyList_Check(tmp)) {
3807 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3808 goto failed;
3809 }
3810 len = PyList_GET_SIZE(tmp);
3811 decorator_list = asdl_seq_new(len, arena);
3812 if (decorator_list == NULL) goto failed;
3813 for (i = 0; i < len; i++) {
3814 expr_ty value;
3815 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3816 if (res != 0) goto failed;
3817 asdl_seq_SET(decorator_list, i, value);
3818 }
3819 Py_XDECREF(tmp);
3820 tmp = NULL;
3821 } else {
3822 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3823 return 1;
3824 }
3825 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3826 decorator_list, lineno, col_offset, arena);
3827 if (*out == NULL) goto failed;
3828 return 0;
3829 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003830 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3831 if (isinstance == -1) {
3832 return 1;
3833 }
3834 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003835 expr_ty value;
3836
3837 if (PyObject_HasAttrString(obj, "value")) {
3838 int res;
3839 tmp = PyObject_GetAttrString(obj, "value");
3840 if (tmp == NULL) goto failed;
3841 res = obj2ast_expr(tmp, &value, arena);
3842 if (res != 0) goto failed;
3843 Py_XDECREF(tmp);
3844 tmp = NULL;
3845 } else {
3846 value = NULL;
3847 }
3848 *out = Return(value, lineno, col_offset, arena);
3849 if (*out == NULL) goto failed;
3850 return 0;
3851 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003852 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3853 if (isinstance == -1) {
3854 return 1;
3855 }
3856 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003857 asdl_seq* targets;
3858
3859 if (PyObject_HasAttrString(obj, "targets")) {
3860 int res;
3861 Py_ssize_t len;
3862 Py_ssize_t i;
3863 tmp = PyObject_GetAttrString(obj, "targets");
3864 if (tmp == NULL) goto failed;
3865 if (!PyList_Check(tmp)) {
3866 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3867 goto failed;
3868 }
3869 len = PyList_GET_SIZE(tmp);
3870 targets = asdl_seq_new(len, arena);
3871 if (targets == NULL) goto failed;
3872 for (i = 0; i < len; i++) {
3873 expr_ty value;
3874 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3875 if (res != 0) goto failed;
3876 asdl_seq_SET(targets, i, value);
3877 }
3878 Py_XDECREF(tmp);
3879 tmp = NULL;
3880 } else {
3881 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3882 return 1;
3883 }
3884 *out = Delete(targets, lineno, col_offset, arena);
3885 if (*out == NULL) goto failed;
3886 return 0;
3887 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003888 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3889 if (isinstance == -1) {
3890 return 1;
3891 }
3892 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003893 asdl_seq* targets;
3894 expr_ty value;
3895
3896 if (PyObject_HasAttrString(obj, "targets")) {
3897 int res;
3898 Py_ssize_t len;
3899 Py_ssize_t i;
3900 tmp = PyObject_GetAttrString(obj, "targets");
3901 if (tmp == NULL) goto failed;
3902 if (!PyList_Check(tmp)) {
3903 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3904 goto failed;
3905 }
3906 len = PyList_GET_SIZE(tmp);
3907 targets = asdl_seq_new(len, arena);
3908 if (targets == NULL) goto failed;
3909 for (i = 0; i < len; i++) {
3910 expr_ty value;
3911 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3912 if (res != 0) goto failed;
3913 asdl_seq_SET(targets, i, value);
3914 }
3915 Py_XDECREF(tmp);
3916 tmp = NULL;
3917 } else {
3918 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3919 return 1;
3920 }
3921 if (PyObject_HasAttrString(obj, "value")) {
3922 int res;
3923 tmp = PyObject_GetAttrString(obj, "value");
3924 if (tmp == NULL) goto failed;
3925 res = obj2ast_expr(tmp, &value, arena);
3926 if (res != 0) goto failed;
3927 Py_XDECREF(tmp);
3928 tmp = NULL;
3929 } else {
3930 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3931 return 1;
3932 }
3933 *out = Assign(targets, value, lineno, col_offset, arena);
3934 if (*out == NULL) goto failed;
3935 return 0;
3936 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003937 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3938 if (isinstance == -1) {
3939 return 1;
3940 }
3941 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003942 expr_ty target;
3943 operator_ty op;
3944 expr_ty value;
3945
3946 if (PyObject_HasAttrString(obj, "target")) {
3947 int res;
3948 tmp = PyObject_GetAttrString(obj, "target");
3949 if (tmp == NULL) goto failed;
3950 res = obj2ast_expr(tmp, &target, arena);
3951 if (res != 0) goto failed;
3952 Py_XDECREF(tmp);
3953 tmp = NULL;
3954 } else {
3955 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3956 return 1;
3957 }
3958 if (PyObject_HasAttrString(obj, "op")) {
3959 int res;
3960 tmp = PyObject_GetAttrString(obj, "op");
3961 if (tmp == NULL) goto failed;
3962 res = obj2ast_operator(tmp, &op, arena);
3963 if (res != 0) goto failed;
3964 Py_XDECREF(tmp);
3965 tmp = NULL;
3966 } else {
3967 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3968 return 1;
3969 }
3970 if (PyObject_HasAttrString(obj, "value")) {
3971 int res;
3972 tmp = PyObject_GetAttrString(obj, "value");
3973 if (tmp == NULL) goto failed;
3974 res = obj2ast_expr(tmp, &value, arena);
3975 if (res != 0) goto failed;
3976 Py_XDECREF(tmp);
3977 tmp = NULL;
3978 } else {
3979 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3980 return 1;
3981 }
3982 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3983 if (*out == NULL) goto failed;
3984 return 0;
3985 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003986 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3987 if (isinstance == -1) {
3988 return 1;
3989 }
3990 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003991 expr_ty target;
3992 expr_ty iter;
3993 asdl_seq* body;
3994 asdl_seq* orelse;
3995
3996 if (PyObject_HasAttrString(obj, "target")) {
3997 int res;
3998 tmp = PyObject_GetAttrString(obj, "target");
3999 if (tmp == NULL) goto failed;
4000 res = obj2ast_expr(tmp, &target, arena);
4001 if (res != 0) goto failed;
4002 Py_XDECREF(tmp);
4003 tmp = NULL;
4004 } else {
4005 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4006 return 1;
4007 }
4008 if (PyObject_HasAttrString(obj, "iter")) {
4009 int res;
4010 tmp = PyObject_GetAttrString(obj, "iter");
4011 if (tmp == NULL) goto failed;
4012 res = obj2ast_expr(tmp, &iter, arena);
4013 if (res != 0) goto failed;
4014 Py_XDECREF(tmp);
4015 tmp = NULL;
4016 } else {
4017 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4018 return 1;
4019 }
4020 if (PyObject_HasAttrString(obj, "body")) {
4021 int res;
4022 Py_ssize_t len;
4023 Py_ssize_t i;
4024 tmp = PyObject_GetAttrString(obj, "body");
4025 if (tmp == NULL) goto failed;
4026 if (!PyList_Check(tmp)) {
4027 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4028 goto failed;
4029 }
4030 len = PyList_GET_SIZE(tmp);
4031 body = asdl_seq_new(len, arena);
4032 if (body == NULL) goto failed;
4033 for (i = 0; i < len; i++) {
4034 stmt_ty value;
4035 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4036 if (res != 0) goto failed;
4037 asdl_seq_SET(body, i, value);
4038 }
4039 Py_XDECREF(tmp);
4040 tmp = NULL;
4041 } else {
4042 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4043 return 1;
4044 }
4045 if (PyObject_HasAttrString(obj, "orelse")) {
4046 int res;
4047 Py_ssize_t len;
4048 Py_ssize_t i;
4049 tmp = PyObject_GetAttrString(obj, "orelse");
4050 if (tmp == NULL) goto failed;
4051 if (!PyList_Check(tmp)) {
4052 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4053 goto failed;
4054 }
4055 len = PyList_GET_SIZE(tmp);
4056 orelse = asdl_seq_new(len, arena);
4057 if (orelse == NULL) goto failed;
4058 for (i = 0; i < len; i++) {
4059 stmt_ty value;
4060 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4061 if (res != 0) goto failed;
4062 asdl_seq_SET(orelse, i, value);
4063 }
4064 Py_XDECREF(tmp);
4065 tmp = NULL;
4066 } else {
4067 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4068 return 1;
4069 }
4070 *out = For(target, iter, body, orelse, lineno, col_offset,
4071 arena);
4072 if (*out == NULL) goto failed;
4073 return 0;
4074 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004075 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4076 if (isinstance == -1) {
4077 return 1;
4078 }
4079 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004080 expr_ty test;
4081 asdl_seq* body;
4082 asdl_seq* orelse;
4083
4084 if (PyObject_HasAttrString(obj, "test")) {
4085 int res;
4086 tmp = PyObject_GetAttrString(obj, "test");
4087 if (tmp == NULL) goto failed;
4088 res = obj2ast_expr(tmp, &test, arena);
4089 if (res != 0) goto failed;
4090 Py_XDECREF(tmp);
4091 tmp = NULL;
4092 } else {
4093 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4094 return 1;
4095 }
4096 if (PyObject_HasAttrString(obj, "body")) {
4097 int res;
4098 Py_ssize_t len;
4099 Py_ssize_t i;
4100 tmp = PyObject_GetAttrString(obj, "body");
4101 if (tmp == NULL) goto failed;
4102 if (!PyList_Check(tmp)) {
4103 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4104 goto failed;
4105 }
4106 len = PyList_GET_SIZE(tmp);
4107 body = asdl_seq_new(len, arena);
4108 if (body == NULL) goto failed;
4109 for (i = 0; i < len; i++) {
4110 stmt_ty value;
4111 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4112 if (res != 0) goto failed;
4113 asdl_seq_SET(body, i, value);
4114 }
4115 Py_XDECREF(tmp);
4116 tmp = NULL;
4117 } else {
4118 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4119 return 1;
4120 }
4121 if (PyObject_HasAttrString(obj, "orelse")) {
4122 int res;
4123 Py_ssize_t len;
4124 Py_ssize_t i;
4125 tmp = PyObject_GetAttrString(obj, "orelse");
4126 if (tmp == NULL) goto failed;
4127 if (!PyList_Check(tmp)) {
4128 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4129 goto failed;
4130 }
4131 len = PyList_GET_SIZE(tmp);
4132 orelse = asdl_seq_new(len, arena);
4133 if (orelse == NULL) goto failed;
4134 for (i = 0; i < len; i++) {
4135 stmt_ty value;
4136 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4137 if (res != 0) goto failed;
4138 asdl_seq_SET(orelse, i, value);
4139 }
4140 Py_XDECREF(tmp);
4141 tmp = NULL;
4142 } else {
4143 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4144 return 1;
4145 }
4146 *out = While(test, body, orelse, lineno, col_offset, arena);
4147 if (*out == NULL) goto failed;
4148 return 0;
4149 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004150 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4151 if (isinstance == -1) {
4152 return 1;
4153 }
4154 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004155 expr_ty test;
4156 asdl_seq* body;
4157 asdl_seq* orelse;
4158
4159 if (PyObject_HasAttrString(obj, "test")) {
4160 int res;
4161 tmp = PyObject_GetAttrString(obj, "test");
4162 if (tmp == NULL) goto failed;
4163 res = obj2ast_expr(tmp, &test, arena);
4164 if (res != 0) goto failed;
4165 Py_XDECREF(tmp);
4166 tmp = NULL;
4167 } else {
4168 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4169 return 1;
4170 }
4171 if (PyObject_HasAttrString(obj, "body")) {
4172 int res;
4173 Py_ssize_t len;
4174 Py_ssize_t i;
4175 tmp = PyObject_GetAttrString(obj, "body");
4176 if (tmp == NULL) goto failed;
4177 if (!PyList_Check(tmp)) {
4178 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4179 goto failed;
4180 }
4181 len = PyList_GET_SIZE(tmp);
4182 body = asdl_seq_new(len, arena);
4183 if (body == NULL) goto failed;
4184 for (i = 0; i < len; i++) {
4185 stmt_ty value;
4186 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4187 if (res != 0) goto failed;
4188 asdl_seq_SET(body, i, value);
4189 }
4190 Py_XDECREF(tmp);
4191 tmp = NULL;
4192 } else {
4193 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4194 return 1;
4195 }
4196 if (PyObject_HasAttrString(obj, "orelse")) {
4197 int res;
4198 Py_ssize_t len;
4199 Py_ssize_t i;
4200 tmp = PyObject_GetAttrString(obj, "orelse");
4201 if (tmp == NULL) goto failed;
4202 if (!PyList_Check(tmp)) {
4203 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4204 goto failed;
4205 }
4206 len = PyList_GET_SIZE(tmp);
4207 orelse = asdl_seq_new(len, arena);
4208 if (orelse == NULL) goto failed;
4209 for (i = 0; i < len; i++) {
4210 stmt_ty value;
4211 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4212 if (res != 0) goto failed;
4213 asdl_seq_SET(orelse, i, value);
4214 }
4215 Py_XDECREF(tmp);
4216 tmp = NULL;
4217 } else {
4218 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4219 return 1;
4220 }
4221 *out = If(test, body, orelse, lineno, col_offset, arena);
4222 if (*out == NULL) goto failed;
4223 return 0;
4224 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004225 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4226 if (isinstance == -1) {
4227 return 1;
4228 }
4229 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004230 expr_ty context_expr;
4231 expr_ty optional_vars;
4232 asdl_seq* body;
4233
4234 if (PyObject_HasAttrString(obj, "context_expr")) {
4235 int res;
4236 tmp = PyObject_GetAttrString(obj, "context_expr");
4237 if (tmp == NULL) goto failed;
4238 res = obj2ast_expr(tmp, &context_expr, arena);
4239 if (res != 0) goto failed;
4240 Py_XDECREF(tmp);
4241 tmp = NULL;
4242 } else {
4243 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4244 return 1;
4245 }
4246 if (PyObject_HasAttrString(obj, "optional_vars")) {
4247 int res;
4248 tmp = PyObject_GetAttrString(obj, "optional_vars");
4249 if (tmp == NULL) goto failed;
4250 res = obj2ast_expr(tmp, &optional_vars, arena);
4251 if (res != 0) goto failed;
4252 Py_XDECREF(tmp);
4253 tmp = NULL;
4254 } else {
4255 optional_vars = NULL;
4256 }
4257 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 }
4282 *out = With(context_expr, optional_vars, body, lineno,
4283 col_offset, arena);
4284 if (*out == NULL) goto failed;
4285 return 0;
4286 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004287 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4288 if (isinstance == -1) {
4289 return 1;
4290 }
4291 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004292 expr_ty exc;
4293 expr_ty cause;
4294
4295 if (PyObject_HasAttrString(obj, "exc")) {
4296 int res;
4297 tmp = PyObject_GetAttrString(obj, "exc");
4298 if (tmp == NULL) goto failed;
4299 res = obj2ast_expr(tmp, &exc, arena);
4300 if (res != 0) goto failed;
4301 Py_XDECREF(tmp);
4302 tmp = NULL;
4303 } else {
4304 exc = NULL;
4305 }
4306 if (PyObject_HasAttrString(obj, "cause")) {
4307 int res;
4308 tmp = PyObject_GetAttrString(obj, "cause");
4309 if (tmp == NULL) goto failed;
4310 res = obj2ast_expr(tmp, &cause, arena);
4311 if (res != 0) goto failed;
4312 Py_XDECREF(tmp);
4313 tmp = NULL;
4314 } else {
4315 cause = NULL;
4316 }
4317 *out = Raise(exc, cause, lineno, col_offset, arena);
4318 if (*out == NULL) goto failed;
4319 return 0;
4320 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004321 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4322 if (isinstance == -1) {
4323 return 1;
4324 }
4325 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004326 asdl_seq* body;
4327 asdl_seq* handlers;
4328 asdl_seq* orelse;
4329
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)) {
4337 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4338 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 {
4352 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4353 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)) {
4362 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4363 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 {
4377 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4378 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)) {
4387 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4388 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 {
4402 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4403 return 1;
4404 }
4405 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4406 arena);
4407 if (*out == NULL) goto failed;
4408 return 0;
4409 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004410 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4411 if (isinstance == -1) {
4412 return 1;
4413 }
4414 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004415 asdl_seq* body;
4416 asdl_seq* finalbody;
4417
4418 if (PyObject_HasAttrString(obj, "body")) {
4419 int res;
4420 Py_ssize_t len;
4421 Py_ssize_t i;
4422 tmp = PyObject_GetAttrString(obj, "body");
4423 if (tmp == NULL) goto failed;
4424 if (!PyList_Check(tmp)) {
4425 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4426 goto failed;
4427 }
4428 len = PyList_GET_SIZE(tmp);
4429 body = asdl_seq_new(len, arena);
4430 if (body == NULL) goto failed;
4431 for (i = 0; i < len; i++) {
4432 stmt_ty value;
4433 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4434 if (res != 0) goto failed;
4435 asdl_seq_SET(body, i, value);
4436 }
4437 Py_XDECREF(tmp);
4438 tmp = NULL;
4439 } else {
4440 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4441 return 1;
4442 }
4443 if (PyObject_HasAttrString(obj, "finalbody")) {
4444 int res;
4445 Py_ssize_t len;
4446 Py_ssize_t i;
4447 tmp = PyObject_GetAttrString(obj, "finalbody");
4448 if (tmp == NULL) goto failed;
4449 if (!PyList_Check(tmp)) {
4450 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451 goto failed;
4452 }
4453 len = PyList_GET_SIZE(tmp);
4454 finalbody = asdl_seq_new(len, arena);
4455 if (finalbody == NULL) goto failed;
4456 for (i = 0; i < len; i++) {
4457 stmt_ty value;
4458 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4459 if (res != 0) goto failed;
4460 asdl_seq_SET(finalbody, i, value);
4461 }
4462 Py_XDECREF(tmp);
4463 tmp = NULL;
4464 } else {
4465 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4466 return 1;
4467 }
4468 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4469 if (*out == NULL) goto failed;
4470 return 0;
4471 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004472 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4473 if (isinstance == -1) {
4474 return 1;
4475 }
4476 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004477 expr_ty test;
4478 expr_ty msg;
4479
4480 if (PyObject_HasAttrString(obj, "test")) {
4481 int res;
4482 tmp = PyObject_GetAttrString(obj, "test");
4483 if (tmp == NULL) goto failed;
4484 res = obj2ast_expr(tmp, &test, arena);
4485 if (res != 0) goto failed;
4486 Py_XDECREF(tmp);
4487 tmp = NULL;
4488 } else {
4489 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4490 return 1;
4491 }
4492 if (PyObject_HasAttrString(obj, "msg")) {
4493 int res;
4494 tmp = PyObject_GetAttrString(obj, "msg");
4495 if (tmp == NULL) goto failed;
4496 res = obj2ast_expr(tmp, &msg, arena);
4497 if (res != 0) goto failed;
4498 Py_XDECREF(tmp);
4499 tmp = NULL;
4500 } else {
4501 msg = NULL;
4502 }
4503 *out = Assert(test, msg, lineno, col_offset, arena);
4504 if (*out == NULL) goto failed;
4505 return 0;
4506 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004507 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4508 if (isinstance == -1) {
4509 return 1;
4510 }
4511 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004512 asdl_seq* names;
4513
4514 if (PyObject_HasAttrString(obj, "names")) {
4515 int res;
4516 Py_ssize_t len;
4517 Py_ssize_t i;
4518 tmp = PyObject_GetAttrString(obj, "names");
4519 if (tmp == NULL) goto failed;
4520 if (!PyList_Check(tmp)) {
4521 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4522 goto failed;
4523 }
4524 len = PyList_GET_SIZE(tmp);
4525 names = asdl_seq_new(len, arena);
4526 if (names == NULL) goto failed;
4527 for (i = 0; i < len; i++) {
4528 alias_ty value;
4529 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4530 if (res != 0) goto failed;
4531 asdl_seq_SET(names, i, value);
4532 }
4533 Py_XDECREF(tmp);
4534 tmp = NULL;
4535 } else {
4536 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4537 return 1;
4538 }
4539 *out = Import(names, lineno, col_offset, arena);
4540 if (*out == NULL) goto failed;
4541 return 0;
4542 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004543 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4544 if (isinstance == -1) {
4545 return 1;
4546 }
4547 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004548 identifier module;
4549 asdl_seq* names;
4550 int level;
4551
4552 if (PyObject_HasAttrString(obj, "module")) {
4553 int res;
4554 tmp = PyObject_GetAttrString(obj, "module");
4555 if (tmp == NULL) goto failed;
4556 res = obj2ast_identifier(tmp, &module, arena);
4557 if (res != 0) goto failed;
4558 Py_XDECREF(tmp);
4559 tmp = NULL;
4560 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004561 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004562 }
4563 if (PyObject_HasAttrString(obj, "names")) {
4564 int res;
4565 Py_ssize_t len;
4566 Py_ssize_t i;
4567 tmp = PyObject_GetAttrString(obj, "names");
4568 if (tmp == NULL) goto failed;
4569 if (!PyList_Check(tmp)) {
4570 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4571 goto failed;
4572 }
4573 len = PyList_GET_SIZE(tmp);
4574 names = asdl_seq_new(len, arena);
4575 if (names == NULL) goto failed;
4576 for (i = 0; i < len; i++) {
4577 alias_ty value;
4578 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4579 if (res != 0) goto failed;
4580 asdl_seq_SET(names, i, value);
4581 }
4582 Py_XDECREF(tmp);
4583 tmp = NULL;
4584 } else {
4585 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4586 return 1;
4587 }
4588 if (PyObject_HasAttrString(obj, "level")) {
4589 int res;
4590 tmp = PyObject_GetAttrString(obj, "level");
4591 if (tmp == NULL) goto failed;
4592 res = obj2ast_int(tmp, &level, arena);
4593 if (res != 0) goto failed;
4594 Py_XDECREF(tmp);
4595 tmp = NULL;
4596 } else {
4597 level = 0;
4598 }
4599 *out = ImportFrom(module, names, level, lineno, col_offset,
4600 arena);
4601 if (*out == NULL) goto failed;
4602 return 0;
4603 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004604 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4605 if (isinstance == -1) {
4606 return 1;
4607 }
4608 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004609 asdl_seq* names;
4610
4611 if (PyObject_HasAttrString(obj, "names")) {
4612 int res;
4613 Py_ssize_t len;
4614 Py_ssize_t i;
4615 tmp = PyObject_GetAttrString(obj, "names");
4616 if (tmp == NULL) goto failed;
4617 if (!PyList_Check(tmp)) {
4618 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4619 goto failed;
4620 }
4621 len = PyList_GET_SIZE(tmp);
4622 names = asdl_seq_new(len, arena);
4623 if (names == NULL) goto failed;
4624 for (i = 0; i < len; i++) {
4625 identifier value;
4626 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4627 if (res != 0) goto failed;
4628 asdl_seq_SET(names, i, value);
4629 }
4630 Py_XDECREF(tmp);
4631 tmp = NULL;
4632 } else {
4633 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4634 return 1;
4635 }
4636 *out = Global(names, lineno, col_offset, arena);
4637 if (*out == NULL) goto failed;
4638 return 0;
4639 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004640 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4641 if (isinstance == -1) {
4642 return 1;
4643 }
4644 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004645 asdl_seq* names;
4646
4647 if (PyObject_HasAttrString(obj, "names")) {
4648 int res;
4649 Py_ssize_t len;
4650 Py_ssize_t i;
4651 tmp = PyObject_GetAttrString(obj, "names");
4652 if (tmp == NULL) goto failed;
4653 if (!PyList_Check(tmp)) {
4654 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4655 goto failed;
4656 }
4657 len = PyList_GET_SIZE(tmp);
4658 names = asdl_seq_new(len, arena);
4659 if (names == NULL) goto failed;
4660 for (i = 0; i < len; i++) {
4661 identifier value;
4662 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4663 if (res != 0) goto failed;
4664 asdl_seq_SET(names, i, value);
4665 }
4666 Py_XDECREF(tmp);
4667 tmp = NULL;
4668 } else {
4669 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4670 return 1;
4671 }
4672 *out = Nonlocal(names, lineno, col_offset, arena);
4673 if (*out == NULL) goto failed;
4674 return 0;
4675 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004676 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4677 if (isinstance == -1) {
4678 return 1;
4679 }
4680 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004681 expr_ty value;
4682
4683 if (PyObject_HasAttrString(obj, "value")) {
4684 int res;
4685 tmp = PyObject_GetAttrString(obj, "value");
4686 if (tmp == NULL) goto failed;
4687 res = obj2ast_expr(tmp, &value, arena);
4688 if (res != 0) goto failed;
4689 Py_XDECREF(tmp);
4690 tmp = NULL;
4691 } else {
4692 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4693 return 1;
4694 }
4695 *out = Expr(value, lineno, col_offset, arena);
4696 if (*out == NULL) goto failed;
4697 return 0;
4698 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004699 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4700 if (isinstance == -1) {
4701 return 1;
4702 }
4703 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004704
4705 *out = Pass(lineno, col_offset, arena);
4706 if (*out == NULL) goto failed;
4707 return 0;
4708 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004709 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4710 if (isinstance == -1) {
4711 return 1;
4712 }
4713 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004714
4715 *out = Break(lineno, col_offset, arena);
4716 if (*out == NULL) goto failed;
4717 return 0;
4718 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004719 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4720 if (isinstance == -1) {
4721 return 1;
4722 }
4723 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004724
4725 *out = Continue(lineno, col_offset, arena);
4726 if (*out == NULL) goto failed;
4727 return 0;
4728 }
4729
Benjamin Peterson5b066812010-11-20 01:38:49 +00004730 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4731 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004732 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004733 return 1;
4734}
4735
4736int
4737obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4738{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004739 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004740
Benjamin Petersond8f65972010-11-20 04:31:07 +00004741 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004742 int lineno;
4743 int col_offset;
4744
4745 if (obj == Py_None) {
4746 *out = NULL;
4747 return 0;
4748 }
4749 if (PyObject_HasAttrString(obj, "lineno")) {
4750 int res;
4751 tmp = PyObject_GetAttrString(obj, "lineno");
4752 if (tmp == NULL) goto failed;
4753 res = obj2ast_int(tmp, &lineno, arena);
4754 if (res != 0) goto failed;
4755 Py_XDECREF(tmp);
4756 tmp = NULL;
4757 } else {
4758 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4759 return 1;
4760 }
4761 if (PyObject_HasAttrString(obj, "col_offset")) {
4762 int res;
4763 tmp = PyObject_GetAttrString(obj, "col_offset");
4764 if (tmp == NULL) goto failed;
4765 res = obj2ast_int(tmp, &col_offset, arena);
4766 if (res != 0) goto failed;
4767 Py_XDECREF(tmp);
4768 tmp = NULL;
4769 } else {
4770 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4771 return 1;
4772 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004773 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4774 if (isinstance == -1) {
4775 return 1;
4776 }
4777 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004778 boolop_ty op;
4779 asdl_seq* values;
4780
4781 if (PyObject_HasAttrString(obj, "op")) {
4782 int res;
4783 tmp = PyObject_GetAttrString(obj, "op");
4784 if (tmp == NULL) goto failed;
4785 res = obj2ast_boolop(tmp, &op, arena);
4786 if (res != 0) goto failed;
4787 Py_XDECREF(tmp);
4788 tmp = NULL;
4789 } else {
4790 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4791 return 1;
4792 }
4793 if (PyObject_HasAttrString(obj, "values")) {
4794 int res;
4795 Py_ssize_t len;
4796 Py_ssize_t i;
4797 tmp = PyObject_GetAttrString(obj, "values");
4798 if (tmp == NULL) goto failed;
4799 if (!PyList_Check(tmp)) {
4800 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4801 goto failed;
4802 }
4803 len = PyList_GET_SIZE(tmp);
4804 values = asdl_seq_new(len, arena);
4805 if (values == NULL) goto failed;
4806 for (i = 0; i < len; i++) {
4807 expr_ty value;
4808 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4809 if (res != 0) goto failed;
4810 asdl_seq_SET(values, i, value);
4811 }
4812 Py_XDECREF(tmp);
4813 tmp = NULL;
4814 } else {
4815 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4816 return 1;
4817 }
4818 *out = BoolOp(op, values, lineno, col_offset, arena);
4819 if (*out == NULL) goto failed;
4820 return 0;
4821 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004822 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4823 if (isinstance == -1) {
4824 return 1;
4825 }
4826 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004827 expr_ty left;
4828 operator_ty op;
4829 expr_ty right;
4830
4831 if (PyObject_HasAttrString(obj, "left")) {
4832 int res;
4833 tmp = PyObject_GetAttrString(obj, "left");
4834 if (tmp == NULL) goto failed;
4835 res = obj2ast_expr(tmp, &left, arena);
4836 if (res != 0) goto failed;
4837 Py_XDECREF(tmp);
4838 tmp = NULL;
4839 } else {
4840 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4841 return 1;
4842 }
4843 if (PyObject_HasAttrString(obj, "op")) {
4844 int res;
4845 tmp = PyObject_GetAttrString(obj, "op");
4846 if (tmp == NULL) goto failed;
4847 res = obj2ast_operator(tmp, &op, arena);
4848 if (res != 0) goto failed;
4849 Py_XDECREF(tmp);
4850 tmp = NULL;
4851 } else {
4852 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4853 return 1;
4854 }
4855 if (PyObject_HasAttrString(obj, "right")) {
4856 int res;
4857 tmp = PyObject_GetAttrString(obj, "right");
4858 if (tmp == NULL) goto failed;
4859 res = obj2ast_expr(tmp, &right, arena);
4860 if (res != 0) goto failed;
4861 Py_XDECREF(tmp);
4862 tmp = NULL;
4863 } else {
4864 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4865 return 1;
4866 }
4867 *out = BinOp(left, op, right, lineno, col_offset, arena);
4868 if (*out == NULL) goto failed;
4869 return 0;
4870 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004871 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4872 if (isinstance == -1) {
4873 return 1;
4874 }
4875 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004876 unaryop_ty op;
4877 expr_ty operand;
4878
4879 if (PyObject_HasAttrString(obj, "op")) {
4880 int res;
4881 tmp = PyObject_GetAttrString(obj, "op");
4882 if (tmp == NULL) goto failed;
4883 res = obj2ast_unaryop(tmp, &op, arena);
4884 if (res != 0) goto failed;
4885 Py_XDECREF(tmp);
4886 tmp = NULL;
4887 } else {
4888 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4889 return 1;
4890 }
4891 if (PyObject_HasAttrString(obj, "operand")) {
4892 int res;
4893 tmp = PyObject_GetAttrString(obj, "operand");
4894 if (tmp == NULL) goto failed;
4895 res = obj2ast_expr(tmp, &operand, arena);
4896 if (res != 0) goto failed;
4897 Py_XDECREF(tmp);
4898 tmp = NULL;
4899 } else {
4900 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4901 return 1;
4902 }
4903 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4904 if (*out == NULL) goto failed;
4905 return 0;
4906 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004907 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4908 if (isinstance == -1) {
4909 return 1;
4910 }
4911 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004912 arguments_ty args;
4913 expr_ty body;
4914
4915 if (PyObject_HasAttrString(obj, "args")) {
4916 int res;
4917 tmp = PyObject_GetAttrString(obj, "args");
4918 if (tmp == NULL) goto failed;
4919 res = obj2ast_arguments(tmp, &args, arena);
4920 if (res != 0) goto failed;
4921 Py_XDECREF(tmp);
4922 tmp = NULL;
4923 } else {
4924 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
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 Lambda");
4937 return 1;
4938 }
4939 *out = Lambda(args, body, lineno, col_offset, arena);
4940 if (*out == NULL) goto failed;
4941 return 0;
4942 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004943 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4944 if (isinstance == -1) {
4945 return 1;
4946 }
4947 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004948 expr_ty test;
4949 expr_ty body;
4950 expr_ty orelse;
4951
4952 if (PyObject_HasAttrString(obj, "test")) {
4953 int res;
4954 tmp = PyObject_GetAttrString(obj, "test");
4955 if (tmp == NULL) goto failed;
4956 res = obj2ast_expr(tmp, &test, arena);
4957 if (res != 0) goto failed;
4958 Py_XDECREF(tmp);
4959 tmp = NULL;
4960 } else {
4961 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4962 return 1;
4963 }
4964 if (PyObject_HasAttrString(obj, "body")) {
4965 int res;
4966 tmp = PyObject_GetAttrString(obj, "body");
4967 if (tmp == NULL) goto failed;
4968 res = obj2ast_expr(tmp, &body, arena);
4969 if (res != 0) goto failed;
4970 Py_XDECREF(tmp);
4971 tmp = NULL;
4972 } else {
4973 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4974 return 1;
4975 }
4976 if (PyObject_HasAttrString(obj, "orelse")) {
4977 int res;
4978 tmp = PyObject_GetAttrString(obj, "orelse");
4979 if (tmp == NULL) goto failed;
4980 res = obj2ast_expr(tmp, &orelse, arena);
4981 if (res != 0) goto failed;
4982 Py_XDECREF(tmp);
4983 tmp = NULL;
4984 } else {
4985 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4986 return 1;
4987 }
4988 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4989 if (*out == NULL) goto failed;
4990 return 0;
4991 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004992 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4993 if (isinstance == -1) {
4994 return 1;
4995 }
4996 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004997 asdl_seq* keys;
4998 asdl_seq* values;
4999
5000 if (PyObject_HasAttrString(obj, "keys")) {
5001 int res;
5002 Py_ssize_t len;
5003 Py_ssize_t i;
5004 tmp = PyObject_GetAttrString(obj, "keys");
5005 if (tmp == NULL) goto failed;
5006 if (!PyList_Check(tmp)) {
5007 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5008 goto failed;
5009 }
5010 len = PyList_GET_SIZE(tmp);
5011 keys = asdl_seq_new(len, arena);
5012 if (keys == NULL) goto failed;
5013 for (i = 0; i < len; i++) {
5014 expr_ty value;
5015 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5016 if (res != 0) goto failed;
5017 asdl_seq_SET(keys, i, value);
5018 }
5019 Py_XDECREF(tmp);
5020 tmp = NULL;
5021 } else {
5022 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5023 return 1;
5024 }
5025 if (PyObject_HasAttrString(obj, "values")) {
5026 int res;
5027 Py_ssize_t len;
5028 Py_ssize_t i;
5029 tmp = PyObject_GetAttrString(obj, "values");
5030 if (tmp == NULL) goto failed;
5031 if (!PyList_Check(tmp)) {
5032 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5033 goto failed;
5034 }
5035 len = PyList_GET_SIZE(tmp);
5036 values = asdl_seq_new(len, arena);
5037 if (values == NULL) goto failed;
5038 for (i = 0; i < len; i++) {
5039 expr_ty value;
5040 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5041 if (res != 0) goto failed;
5042 asdl_seq_SET(values, i, value);
5043 }
5044 Py_XDECREF(tmp);
5045 tmp = NULL;
5046 } else {
5047 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5048 return 1;
5049 }
5050 *out = Dict(keys, values, lineno, col_offset, arena);
5051 if (*out == NULL) goto failed;
5052 return 0;
5053 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005054 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5055 if (isinstance == -1) {
5056 return 1;
5057 }
5058 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005059 asdl_seq* elts;
5060
5061 if (PyObject_HasAttrString(obj, "elts")) {
5062 int res;
5063 Py_ssize_t len;
5064 Py_ssize_t i;
5065 tmp = PyObject_GetAttrString(obj, "elts");
5066 if (tmp == NULL) goto failed;
5067 if (!PyList_Check(tmp)) {
5068 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5069 goto failed;
5070 }
5071 len = PyList_GET_SIZE(tmp);
5072 elts = asdl_seq_new(len, arena);
5073 if (elts == NULL) goto failed;
5074 for (i = 0; i < len; i++) {
5075 expr_ty value;
5076 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5077 if (res != 0) goto failed;
5078 asdl_seq_SET(elts, i, value);
5079 }
5080 Py_XDECREF(tmp);
5081 tmp = NULL;
5082 } else {
5083 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5084 return 1;
5085 }
5086 *out = Set(elts, lineno, col_offset, arena);
5087 if (*out == NULL) goto failed;
5088 return 0;
5089 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005090 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5091 if (isinstance == -1) {
5092 return 1;
5093 }
5094 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005095 expr_ty elt;
5096 asdl_seq* generators;
5097
5098 if (PyObject_HasAttrString(obj, "elt")) {
5099 int res;
5100 tmp = PyObject_GetAttrString(obj, "elt");
5101 if (tmp == NULL) goto failed;
5102 res = obj2ast_expr(tmp, &elt, arena);
5103 if (res != 0) goto failed;
5104 Py_XDECREF(tmp);
5105 tmp = NULL;
5106 } else {
5107 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5108 return 1;
5109 }
5110 if (PyObject_HasAttrString(obj, "generators")) {
5111 int res;
5112 Py_ssize_t len;
5113 Py_ssize_t i;
5114 tmp = PyObject_GetAttrString(obj, "generators");
5115 if (tmp == NULL) goto failed;
5116 if (!PyList_Check(tmp)) {
5117 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5118 goto failed;
5119 }
5120 len = PyList_GET_SIZE(tmp);
5121 generators = asdl_seq_new(len, arena);
5122 if (generators == NULL) goto failed;
5123 for (i = 0; i < len; i++) {
5124 comprehension_ty value;
5125 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5126 if (res != 0) goto failed;
5127 asdl_seq_SET(generators, i, value);
5128 }
5129 Py_XDECREF(tmp);
5130 tmp = NULL;
5131 } else {
5132 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5133 return 1;
5134 }
5135 *out = ListComp(elt, generators, lineno, col_offset, arena);
5136 if (*out == NULL) goto failed;
5137 return 0;
5138 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005139 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5140 if (isinstance == -1) {
5141 return 1;
5142 }
5143 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005144 expr_ty elt;
5145 asdl_seq* generators;
5146
5147 if (PyObject_HasAttrString(obj, "elt")) {
5148 int res;
5149 tmp = PyObject_GetAttrString(obj, "elt");
5150 if (tmp == NULL) goto failed;
5151 res = obj2ast_expr(tmp, &elt, arena);
5152 if (res != 0) goto failed;
5153 Py_XDECREF(tmp);
5154 tmp = NULL;
5155 } else {
5156 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5157 return 1;
5158 }
5159 if (PyObject_HasAttrString(obj, "generators")) {
5160 int res;
5161 Py_ssize_t len;
5162 Py_ssize_t i;
5163 tmp = PyObject_GetAttrString(obj, "generators");
5164 if (tmp == NULL) goto failed;
5165 if (!PyList_Check(tmp)) {
5166 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5167 goto failed;
5168 }
5169 len = PyList_GET_SIZE(tmp);
5170 generators = asdl_seq_new(len, arena);
5171 if (generators == NULL) goto failed;
5172 for (i = 0; i < len; i++) {
5173 comprehension_ty value;
5174 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5175 if (res != 0) goto failed;
5176 asdl_seq_SET(generators, i, value);
5177 }
5178 Py_XDECREF(tmp);
5179 tmp = NULL;
5180 } else {
5181 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5182 return 1;
5183 }
5184 *out = SetComp(elt, generators, lineno, col_offset, arena);
5185 if (*out == NULL) goto failed;
5186 return 0;
5187 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005188 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5189 if (isinstance == -1) {
5190 return 1;
5191 }
5192 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005193 expr_ty key;
5194 expr_ty value;
5195 asdl_seq* generators;
5196
5197 if (PyObject_HasAttrString(obj, "key")) {
5198 int res;
5199 tmp = PyObject_GetAttrString(obj, "key");
5200 if (tmp == NULL) goto failed;
5201 res = obj2ast_expr(tmp, &key, arena);
5202 if (res != 0) goto failed;
5203 Py_XDECREF(tmp);
5204 tmp = NULL;
5205 } else {
5206 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5207 return 1;
5208 }
5209 if (PyObject_HasAttrString(obj, "value")) {
5210 int res;
5211 tmp = PyObject_GetAttrString(obj, "value");
5212 if (tmp == NULL) goto failed;
5213 res = obj2ast_expr(tmp, &value, arena);
5214 if (res != 0) goto failed;
5215 Py_XDECREF(tmp);
5216 tmp = NULL;
5217 } else {
5218 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5219 return 1;
5220 }
5221 if (PyObject_HasAttrString(obj, "generators")) {
5222 int res;
5223 Py_ssize_t len;
5224 Py_ssize_t i;
5225 tmp = PyObject_GetAttrString(obj, "generators");
5226 if (tmp == NULL) goto failed;
5227 if (!PyList_Check(tmp)) {
5228 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5229 goto failed;
5230 }
5231 len = PyList_GET_SIZE(tmp);
5232 generators = asdl_seq_new(len, arena);
5233 if (generators == NULL) goto failed;
5234 for (i = 0; i < len; i++) {
5235 comprehension_ty value;
5236 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5237 if (res != 0) goto failed;
5238 asdl_seq_SET(generators, i, value);
5239 }
5240 Py_XDECREF(tmp);
5241 tmp = NULL;
5242 } else {
5243 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5244 return 1;
5245 }
5246 *out = DictComp(key, value, generators, lineno, col_offset,
5247 arena);
5248 if (*out == NULL) goto failed;
5249 return 0;
5250 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005251 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5252 if (isinstance == -1) {
5253 return 1;
5254 }
5255 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005256 expr_ty elt;
5257 asdl_seq* generators;
5258
5259 if (PyObject_HasAttrString(obj, "elt")) {
5260 int res;
5261 tmp = PyObject_GetAttrString(obj, "elt");
5262 if (tmp == NULL) goto failed;
5263 res = obj2ast_expr(tmp, &elt, arena);
5264 if (res != 0) goto failed;
5265 Py_XDECREF(tmp);
5266 tmp = NULL;
5267 } else {
5268 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5269 return 1;
5270 }
5271 if (PyObject_HasAttrString(obj, "generators")) {
5272 int res;
5273 Py_ssize_t len;
5274 Py_ssize_t i;
5275 tmp = PyObject_GetAttrString(obj, "generators");
5276 if (tmp == NULL) goto failed;
5277 if (!PyList_Check(tmp)) {
5278 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5279 goto failed;
5280 }
5281 len = PyList_GET_SIZE(tmp);
5282 generators = asdl_seq_new(len, arena);
5283 if (generators == NULL) goto failed;
5284 for (i = 0; i < len; i++) {
5285 comprehension_ty value;
5286 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5287 if (res != 0) goto failed;
5288 asdl_seq_SET(generators, i, value);
5289 }
5290 Py_XDECREF(tmp);
5291 tmp = NULL;
5292 } else {
5293 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5294 return 1;
5295 }
5296 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5297 if (*out == NULL) goto failed;
5298 return 0;
5299 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005300 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5301 if (isinstance == -1) {
5302 return 1;
5303 }
5304 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005305 expr_ty value;
5306
5307 if (PyObject_HasAttrString(obj, "value")) {
5308 int res;
5309 tmp = PyObject_GetAttrString(obj, "value");
5310 if (tmp == NULL) goto failed;
5311 res = obj2ast_expr(tmp, &value, arena);
5312 if (res != 0) goto failed;
5313 Py_XDECREF(tmp);
5314 tmp = NULL;
5315 } else {
5316 value = NULL;
5317 }
5318 *out = Yield(value, lineno, col_offset, arena);
5319 if (*out == NULL) goto failed;
5320 return 0;
5321 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005322 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5323 if (isinstance == -1) {
5324 return 1;
5325 }
5326 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005327 expr_ty left;
5328 asdl_int_seq* ops;
5329 asdl_seq* comparators;
5330
5331 if (PyObject_HasAttrString(obj, "left")) {
5332 int res;
5333 tmp = PyObject_GetAttrString(obj, "left");
5334 if (tmp == NULL) goto failed;
5335 res = obj2ast_expr(tmp, &left, arena);
5336 if (res != 0) goto failed;
5337 Py_XDECREF(tmp);
5338 tmp = NULL;
5339 } else {
5340 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5341 return 1;
5342 }
5343 if (PyObject_HasAttrString(obj, "ops")) {
5344 int res;
5345 Py_ssize_t len;
5346 Py_ssize_t i;
5347 tmp = PyObject_GetAttrString(obj, "ops");
5348 if (tmp == NULL) goto failed;
5349 if (!PyList_Check(tmp)) {
5350 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5351 goto failed;
5352 }
5353 len = PyList_GET_SIZE(tmp);
5354 ops = asdl_int_seq_new(len, arena);
5355 if (ops == NULL) goto failed;
5356 for (i = 0; i < len; i++) {
5357 cmpop_ty value;
5358 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5359 if (res != 0) goto failed;
5360 asdl_seq_SET(ops, i, value);
5361 }
5362 Py_XDECREF(tmp);
5363 tmp = NULL;
5364 } else {
5365 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5366 return 1;
5367 }
5368 if (PyObject_HasAttrString(obj, "comparators")) {
5369 int res;
5370 Py_ssize_t len;
5371 Py_ssize_t i;
5372 tmp = PyObject_GetAttrString(obj, "comparators");
5373 if (tmp == NULL) goto failed;
5374 if (!PyList_Check(tmp)) {
5375 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5376 goto failed;
5377 }
5378 len = PyList_GET_SIZE(tmp);
5379 comparators = asdl_seq_new(len, arena);
5380 if (comparators == NULL) goto failed;
5381 for (i = 0; i < len; i++) {
5382 expr_ty value;
5383 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5384 if (res != 0) goto failed;
5385 asdl_seq_SET(comparators, i, value);
5386 }
5387 Py_XDECREF(tmp);
5388 tmp = NULL;
5389 } else {
5390 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5391 return 1;
5392 }
5393 *out = Compare(left, ops, comparators, lineno, col_offset,
5394 arena);
5395 if (*out == NULL) goto failed;
5396 return 0;
5397 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005398 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5399 if (isinstance == -1) {
5400 return 1;
5401 }
5402 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005403 expr_ty func;
5404 asdl_seq* args;
5405 asdl_seq* keywords;
5406 expr_ty starargs;
5407 expr_ty kwargs;
5408
5409 if (PyObject_HasAttrString(obj, "func")) {
5410 int res;
5411 tmp = PyObject_GetAttrString(obj, "func");
5412 if (tmp == NULL) goto failed;
5413 res = obj2ast_expr(tmp, &func, arena);
5414 if (res != 0) goto failed;
5415 Py_XDECREF(tmp);
5416 tmp = NULL;
5417 } else {
5418 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5419 return 1;
5420 }
5421 if (PyObject_HasAttrString(obj, "args")) {
5422 int res;
5423 Py_ssize_t len;
5424 Py_ssize_t i;
5425 tmp = PyObject_GetAttrString(obj, "args");
5426 if (tmp == NULL) goto failed;
5427 if (!PyList_Check(tmp)) {
5428 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5429 goto failed;
5430 }
5431 len = PyList_GET_SIZE(tmp);
5432 args = asdl_seq_new(len, arena);
5433 if (args == NULL) goto failed;
5434 for (i = 0; i < len; i++) {
5435 expr_ty value;
5436 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5437 if (res != 0) goto failed;
5438 asdl_seq_SET(args, i, value);
5439 }
5440 Py_XDECREF(tmp);
5441 tmp = NULL;
5442 } else {
5443 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5444 return 1;
5445 }
5446 if (PyObject_HasAttrString(obj, "keywords")) {
5447 int res;
5448 Py_ssize_t len;
5449 Py_ssize_t i;
5450 tmp = PyObject_GetAttrString(obj, "keywords");
5451 if (tmp == NULL) goto failed;
5452 if (!PyList_Check(tmp)) {
5453 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5454 goto failed;
5455 }
5456 len = PyList_GET_SIZE(tmp);
5457 keywords = asdl_seq_new(len, arena);
5458 if (keywords == NULL) goto failed;
5459 for (i = 0; i < len; i++) {
5460 keyword_ty value;
5461 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5462 if (res != 0) goto failed;
5463 asdl_seq_SET(keywords, i, value);
5464 }
5465 Py_XDECREF(tmp);
5466 tmp = NULL;
5467 } else {
5468 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5469 return 1;
5470 }
5471 if (PyObject_HasAttrString(obj, "starargs")) {
5472 int res;
5473 tmp = PyObject_GetAttrString(obj, "starargs");
5474 if (tmp == NULL) goto failed;
5475 res = obj2ast_expr(tmp, &starargs, arena);
5476 if (res != 0) goto failed;
5477 Py_XDECREF(tmp);
5478 tmp = NULL;
5479 } else {
5480 starargs = NULL;
5481 }
5482 if (PyObject_HasAttrString(obj, "kwargs")) {
5483 int res;
5484 tmp = PyObject_GetAttrString(obj, "kwargs");
5485 if (tmp == NULL) goto failed;
5486 res = obj2ast_expr(tmp, &kwargs, arena);
5487 if (res != 0) goto failed;
5488 Py_XDECREF(tmp);
5489 tmp = NULL;
5490 } else {
5491 kwargs = NULL;
5492 }
5493 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5494 col_offset, arena);
5495 if (*out == NULL) goto failed;
5496 return 0;
5497 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005498 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5499 if (isinstance == -1) {
5500 return 1;
5501 }
5502 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005503 object n;
5504
5505 if (PyObject_HasAttrString(obj, "n")) {
5506 int res;
5507 tmp = PyObject_GetAttrString(obj, "n");
5508 if (tmp == NULL) goto failed;
5509 res = obj2ast_object(tmp, &n, arena);
5510 if (res != 0) goto failed;
5511 Py_XDECREF(tmp);
5512 tmp = NULL;
5513 } else {
5514 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5515 return 1;
5516 }
5517 *out = Num(n, lineno, col_offset, arena);
5518 if (*out == NULL) goto failed;
5519 return 0;
5520 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005521 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5522 if (isinstance == -1) {
5523 return 1;
5524 }
5525 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005526 string s;
5527
5528 if (PyObject_HasAttrString(obj, "s")) {
5529 int res;
5530 tmp = PyObject_GetAttrString(obj, "s");
5531 if (tmp == NULL) goto failed;
5532 res = obj2ast_string(tmp, &s, arena);
5533 if (res != 0) goto failed;
5534 Py_XDECREF(tmp);
5535 tmp = NULL;
5536 } else {
5537 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5538 return 1;
5539 }
5540 *out = Str(s, lineno, col_offset, arena);
5541 if (*out == NULL) goto failed;
5542 return 0;
5543 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005544 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5545 if (isinstance == -1) {
5546 return 1;
5547 }
5548 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005549 string s;
5550
5551 if (PyObject_HasAttrString(obj, "s")) {
5552 int res;
5553 tmp = PyObject_GetAttrString(obj, "s");
5554 if (tmp == NULL) goto failed;
5555 res = obj2ast_string(tmp, &s, arena);
5556 if (res != 0) goto failed;
5557 Py_XDECREF(tmp);
5558 tmp = NULL;
5559 } else {
5560 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5561 return 1;
5562 }
5563 *out = Bytes(s, lineno, col_offset, arena);
5564 if (*out == NULL) goto failed;
5565 return 0;
5566 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005567 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5568 if (isinstance == -1) {
5569 return 1;
5570 }
5571 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005572
5573 *out = Ellipsis(lineno, col_offset, arena);
5574 if (*out == NULL) goto failed;
5575 return 0;
5576 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005577 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5578 if (isinstance == -1) {
5579 return 1;
5580 }
5581 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005582 expr_ty value;
5583 identifier attr;
5584 expr_context_ty ctx;
5585
5586 if (PyObject_HasAttrString(obj, "value")) {
5587 int res;
5588 tmp = PyObject_GetAttrString(obj, "value");
5589 if (tmp == NULL) goto failed;
5590 res = obj2ast_expr(tmp, &value, arena);
5591 if (res != 0) goto failed;
5592 Py_XDECREF(tmp);
5593 tmp = NULL;
5594 } else {
5595 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5596 return 1;
5597 }
5598 if (PyObject_HasAttrString(obj, "attr")) {
5599 int res;
5600 tmp = PyObject_GetAttrString(obj, "attr");
5601 if (tmp == NULL) goto failed;
5602 res = obj2ast_identifier(tmp, &attr, arena);
5603 if (res != 0) goto failed;
5604 Py_XDECREF(tmp);
5605 tmp = NULL;
5606 } else {
5607 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5608 return 1;
5609 }
5610 if (PyObject_HasAttrString(obj, "ctx")) {
5611 int res;
5612 tmp = PyObject_GetAttrString(obj, "ctx");
5613 if (tmp == NULL) goto failed;
5614 res = obj2ast_expr_context(tmp, &ctx, arena);
5615 if (res != 0) goto failed;
5616 Py_XDECREF(tmp);
5617 tmp = NULL;
5618 } else {
5619 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5620 return 1;
5621 }
5622 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5623 if (*out == NULL) goto failed;
5624 return 0;
5625 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005626 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5627 if (isinstance == -1) {
5628 return 1;
5629 }
5630 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005631 expr_ty value;
5632 slice_ty slice;
5633 expr_context_ty ctx;
5634
5635 if (PyObject_HasAttrString(obj, "value")) {
5636 int res;
5637 tmp = PyObject_GetAttrString(obj, "value");
5638 if (tmp == NULL) goto failed;
5639 res = obj2ast_expr(tmp, &value, arena);
5640 if (res != 0) goto failed;
5641 Py_XDECREF(tmp);
5642 tmp = NULL;
5643 } else {
5644 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5645 return 1;
5646 }
5647 if (PyObject_HasAttrString(obj, "slice")) {
5648 int res;
5649 tmp = PyObject_GetAttrString(obj, "slice");
5650 if (tmp == NULL) goto failed;
5651 res = obj2ast_slice(tmp, &slice, arena);
5652 if (res != 0) goto failed;
5653 Py_XDECREF(tmp);
5654 tmp = NULL;
5655 } else {
5656 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5657 return 1;
5658 }
5659 if (PyObject_HasAttrString(obj, "ctx")) {
5660 int res;
5661 tmp = PyObject_GetAttrString(obj, "ctx");
5662 if (tmp == NULL) goto failed;
5663 res = obj2ast_expr_context(tmp, &ctx, arena);
5664 if (res != 0) goto failed;
5665 Py_XDECREF(tmp);
5666 tmp = NULL;
5667 } else {
5668 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5669 return 1;
5670 }
5671 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5672 if (*out == NULL) goto failed;
5673 return 0;
5674 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005675 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5676 if (isinstance == -1) {
5677 return 1;
5678 }
5679 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005680 expr_ty value;
5681 expr_context_ty ctx;
5682
5683 if (PyObject_HasAttrString(obj, "value")) {
5684 int res;
5685 tmp = PyObject_GetAttrString(obj, "value");
5686 if (tmp == NULL) goto failed;
5687 res = obj2ast_expr(tmp, &value, arena);
5688 if (res != 0) goto failed;
5689 Py_XDECREF(tmp);
5690 tmp = NULL;
5691 } else {
5692 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5693 return 1;
5694 }
5695 if (PyObject_HasAttrString(obj, "ctx")) {
5696 int res;
5697 tmp = PyObject_GetAttrString(obj, "ctx");
5698 if (tmp == NULL) goto failed;
5699 res = obj2ast_expr_context(tmp, &ctx, arena);
5700 if (res != 0) goto failed;
5701 Py_XDECREF(tmp);
5702 tmp = NULL;
5703 } else {
5704 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5705 return 1;
5706 }
5707 *out = Starred(value, ctx, lineno, col_offset, arena);
5708 if (*out == NULL) goto failed;
5709 return 0;
5710 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005711 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5712 if (isinstance == -1) {
5713 return 1;
5714 }
5715 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005716 identifier id;
5717 expr_context_ty ctx;
5718
5719 if (PyObject_HasAttrString(obj, "id")) {
5720 int res;
5721 tmp = PyObject_GetAttrString(obj, "id");
5722 if (tmp == NULL) goto failed;
5723 res = obj2ast_identifier(tmp, &id, arena);
5724 if (res != 0) goto failed;
5725 Py_XDECREF(tmp);
5726 tmp = NULL;
5727 } else {
5728 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5729 return 1;
5730 }
5731 if (PyObject_HasAttrString(obj, "ctx")) {
5732 int res;
5733 tmp = PyObject_GetAttrString(obj, "ctx");
5734 if (tmp == NULL) goto failed;
5735 res = obj2ast_expr_context(tmp, &ctx, arena);
5736 if (res != 0) goto failed;
5737 Py_XDECREF(tmp);
5738 tmp = NULL;
5739 } else {
5740 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5741 return 1;
5742 }
5743 *out = Name(id, ctx, lineno, col_offset, arena);
5744 if (*out == NULL) goto failed;
5745 return 0;
5746 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005747 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5748 if (isinstance == -1) {
5749 return 1;
5750 }
5751 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005752 asdl_seq* elts;
5753 expr_context_ty ctx;
5754
5755 if (PyObject_HasAttrString(obj, "elts")) {
5756 int res;
5757 Py_ssize_t len;
5758 Py_ssize_t i;
5759 tmp = PyObject_GetAttrString(obj, "elts");
5760 if (tmp == NULL) goto failed;
5761 if (!PyList_Check(tmp)) {
5762 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5763 goto failed;
5764 }
5765 len = PyList_GET_SIZE(tmp);
5766 elts = asdl_seq_new(len, arena);
5767 if (elts == NULL) goto failed;
5768 for (i = 0; i < len; i++) {
5769 expr_ty value;
5770 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5771 if (res != 0) goto failed;
5772 asdl_seq_SET(elts, i, value);
5773 }
5774 Py_XDECREF(tmp);
5775 tmp = NULL;
5776 } else {
5777 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5778 return 1;
5779 }
5780 if (PyObject_HasAttrString(obj, "ctx")) {
5781 int res;
5782 tmp = PyObject_GetAttrString(obj, "ctx");
5783 if (tmp == NULL) goto failed;
5784 res = obj2ast_expr_context(tmp, &ctx, arena);
5785 if (res != 0) goto failed;
5786 Py_XDECREF(tmp);
5787 tmp = NULL;
5788 } else {
5789 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5790 return 1;
5791 }
5792 *out = List(elts, ctx, lineno, col_offset, arena);
5793 if (*out == NULL) goto failed;
5794 return 0;
5795 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005796 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5797 if (isinstance == -1) {
5798 return 1;
5799 }
5800 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005801 asdl_seq* elts;
5802 expr_context_ty ctx;
5803
5804 if (PyObject_HasAttrString(obj, "elts")) {
5805 int res;
5806 Py_ssize_t len;
5807 Py_ssize_t i;
5808 tmp = PyObject_GetAttrString(obj, "elts");
5809 if (tmp == NULL) goto failed;
5810 if (!PyList_Check(tmp)) {
5811 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5812 goto failed;
5813 }
5814 len = PyList_GET_SIZE(tmp);
5815 elts = asdl_seq_new(len, arena);
5816 if (elts == NULL) goto failed;
5817 for (i = 0; i < len; i++) {
5818 expr_ty value;
5819 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5820 if (res != 0) goto failed;
5821 asdl_seq_SET(elts, i, value);
5822 }
5823 Py_XDECREF(tmp);
5824 tmp = NULL;
5825 } else {
5826 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5827 return 1;
5828 }
5829 if (PyObject_HasAttrString(obj, "ctx")) {
5830 int res;
5831 tmp = PyObject_GetAttrString(obj, "ctx");
5832 if (tmp == NULL) goto failed;
5833 res = obj2ast_expr_context(tmp, &ctx, arena);
5834 if (res != 0) goto failed;
5835 Py_XDECREF(tmp);
5836 tmp = NULL;
5837 } else {
5838 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5839 return 1;
5840 }
5841 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5842 if (*out == NULL) goto failed;
5843 return 0;
5844 }
5845
Benjamin Peterson5b066812010-11-20 01:38:49 +00005846 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5847 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005848 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005849 return 1;
5850}
5851
5852int
5853obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5854{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005855 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005856
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005857 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5858 if (isinstance == -1) {
5859 return 1;
5860 }
5861 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005862 *out = Load;
5863 return 0;
5864 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005865 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5866 if (isinstance == -1) {
5867 return 1;
5868 }
5869 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005870 *out = Store;
5871 return 0;
5872 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005873 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5874 if (isinstance == -1) {
5875 return 1;
5876 }
5877 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005878 *out = Del;
5879 return 0;
5880 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005881 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5882 if (isinstance == -1) {
5883 return 1;
5884 }
5885 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005886 *out = AugLoad;
5887 return 0;
5888 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005889 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5890 if (isinstance == -1) {
5891 return 1;
5892 }
5893 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005894 *out = AugStore;
5895 return 0;
5896 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005897 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5898 if (isinstance == -1) {
5899 return 1;
5900 }
5901 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005902 *out = Param;
5903 return 0;
5904 }
5905
Benjamin Peterson5b066812010-11-20 01:38:49 +00005906 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005907 return 1;
5908}
5909
5910int
5911obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5912{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005913 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005914
Benjamin Petersond8f65972010-11-20 04:31:07 +00005915 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005916
5917 if (obj == Py_None) {
5918 *out = NULL;
5919 return 0;
5920 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005921 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5922 if (isinstance == -1) {
5923 return 1;
5924 }
5925 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005926 expr_ty lower;
5927 expr_ty upper;
5928 expr_ty step;
5929
5930 if (PyObject_HasAttrString(obj, "lower")) {
5931 int res;
5932 tmp = PyObject_GetAttrString(obj, "lower");
5933 if (tmp == NULL) goto failed;
5934 res = obj2ast_expr(tmp, &lower, arena);
5935 if (res != 0) goto failed;
5936 Py_XDECREF(tmp);
5937 tmp = NULL;
5938 } else {
5939 lower = NULL;
5940 }
5941 if (PyObject_HasAttrString(obj, "upper")) {
5942 int res;
5943 tmp = PyObject_GetAttrString(obj, "upper");
5944 if (tmp == NULL) goto failed;
5945 res = obj2ast_expr(tmp, &upper, arena);
5946 if (res != 0) goto failed;
5947 Py_XDECREF(tmp);
5948 tmp = NULL;
5949 } else {
5950 upper = NULL;
5951 }
5952 if (PyObject_HasAttrString(obj, "step")) {
5953 int res;
5954 tmp = PyObject_GetAttrString(obj, "step");
5955 if (tmp == NULL) goto failed;
5956 res = obj2ast_expr(tmp, &step, arena);
5957 if (res != 0) goto failed;
5958 Py_XDECREF(tmp);
5959 tmp = NULL;
5960 } else {
5961 step = NULL;
5962 }
5963 *out = Slice(lower, upper, step, arena);
5964 if (*out == NULL) goto failed;
5965 return 0;
5966 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005967 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5968 if (isinstance == -1) {
5969 return 1;
5970 }
5971 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005972 asdl_seq* dims;
5973
5974 if (PyObject_HasAttrString(obj, "dims")) {
5975 int res;
5976 Py_ssize_t len;
5977 Py_ssize_t i;
5978 tmp = PyObject_GetAttrString(obj, "dims");
5979 if (tmp == NULL) goto failed;
5980 if (!PyList_Check(tmp)) {
5981 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5982 goto failed;
5983 }
5984 len = PyList_GET_SIZE(tmp);
5985 dims = asdl_seq_new(len, arena);
5986 if (dims == NULL) goto failed;
5987 for (i = 0; i < len; i++) {
5988 slice_ty value;
5989 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5990 if (res != 0) goto failed;
5991 asdl_seq_SET(dims, i, value);
5992 }
5993 Py_XDECREF(tmp);
5994 tmp = NULL;
5995 } else {
5996 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5997 return 1;
5998 }
5999 *out = ExtSlice(dims, arena);
6000 if (*out == NULL) goto failed;
6001 return 0;
6002 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006003 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6004 if (isinstance == -1) {
6005 return 1;
6006 }
6007 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006008 expr_ty value;
6009
6010 if (PyObject_HasAttrString(obj, "value")) {
6011 int res;
6012 tmp = PyObject_GetAttrString(obj, "value");
6013 if (tmp == NULL) goto failed;
6014 res = obj2ast_expr(tmp, &value, arena);
6015 if (res != 0) goto failed;
6016 Py_XDECREF(tmp);
6017 tmp = NULL;
6018 } else {
6019 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6020 return 1;
6021 }
6022 *out = Index(value, arena);
6023 if (*out == NULL) goto failed;
6024 return 0;
6025 }
6026
Benjamin Peterson5b066812010-11-20 01:38:49 +00006027 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6028 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006029 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006030 return 1;
6031}
6032
6033int
6034obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6035{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006036 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006037
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006038 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6039 if (isinstance == -1) {
6040 return 1;
6041 }
6042 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006043 *out = And;
6044 return 0;
6045 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006046 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6047 if (isinstance == -1) {
6048 return 1;
6049 }
6050 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006051 *out = Or;
6052 return 0;
6053 }
6054
Benjamin Peterson5b066812010-11-20 01:38:49 +00006055 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006056 return 1;
6057}
6058
6059int
6060obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6061{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006062 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006063
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006064 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6065 if (isinstance == -1) {
6066 return 1;
6067 }
6068 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006069 *out = Add;
6070 return 0;
6071 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006072 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6073 if (isinstance == -1) {
6074 return 1;
6075 }
6076 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006077 *out = Sub;
6078 return 0;
6079 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006080 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6081 if (isinstance == -1) {
6082 return 1;
6083 }
6084 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006085 *out = Mult;
6086 return 0;
6087 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006088 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6089 if (isinstance == -1) {
6090 return 1;
6091 }
6092 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006093 *out = Div;
6094 return 0;
6095 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006096 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6097 if (isinstance == -1) {
6098 return 1;
6099 }
6100 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006101 *out = Mod;
6102 return 0;
6103 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006104 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6105 if (isinstance == -1) {
6106 return 1;
6107 }
6108 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006109 *out = Pow;
6110 return 0;
6111 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006112 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6113 if (isinstance == -1) {
6114 return 1;
6115 }
6116 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006117 *out = LShift;
6118 return 0;
6119 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006120 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6121 if (isinstance == -1) {
6122 return 1;
6123 }
6124 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006125 *out = RShift;
6126 return 0;
6127 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006128 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6129 if (isinstance == -1) {
6130 return 1;
6131 }
6132 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006133 *out = BitOr;
6134 return 0;
6135 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006136 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6137 if (isinstance == -1) {
6138 return 1;
6139 }
6140 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006141 *out = BitXor;
6142 return 0;
6143 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006144 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6145 if (isinstance == -1) {
6146 return 1;
6147 }
6148 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006149 *out = BitAnd;
6150 return 0;
6151 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006152 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6153 if (isinstance == -1) {
6154 return 1;
6155 }
6156 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006157 *out = FloorDiv;
6158 return 0;
6159 }
6160
Benjamin Peterson5b066812010-11-20 01:38:49 +00006161 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006162 return 1;
6163}
6164
6165int
6166obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6167{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006168 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006169
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006170 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006175 *out = Invert;
6176 return 0;
6177 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006178 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6179 if (isinstance == -1) {
6180 return 1;
6181 }
6182 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006183 *out = Not;
6184 return 0;
6185 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006186 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006191 *out = UAdd;
6192 return 0;
6193 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006194 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6195 if (isinstance == -1) {
6196 return 1;
6197 }
6198 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006199 *out = USub;
6200 return 0;
6201 }
6202
Benjamin Peterson5b066812010-11-20 01:38:49 +00006203 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006204 return 1;
6205}
6206
6207int
6208obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6209{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006210 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006211
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006212 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6213 if (isinstance == -1) {
6214 return 1;
6215 }
6216 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006217 *out = Eq;
6218 return 0;
6219 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006220 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6221 if (isinstance == -1) {
6222 return 1;
6223 }
6224 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006225 *out = NotEq;
6226 return 0;
6227 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006228 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6229 if (isinstance == -1) {
6230 return 1;
6231 }
6232 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006233 *out = Lt;
6234 return 0;
6235 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006236 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6237 if (isinstance == -1) {
6238 return 1;
6239 }
6240 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006241 *out = LtE;
6242 return 0;
6243 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006244 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6245 if (isinstance == -1) {
6246 return 1;
6247 }
6248 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006249 *out = Gt;
6250 return 0;
6251 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006252 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6253 if (isinstance == -1) {
6254 return 1;
6255 }
6256 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006257 *out = GtE;
6258 return 0;
6259 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006260 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6261 if (isinstance == -1) {
6262 return 1;
6263 }
6264 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006265 *out = Is;
6266 return 0;
6267 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006268 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6269 if (isinstance == -1) {
6270 return 1;
6271 }
6272 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006273 *out = IsNot;
6274 return 0;
6275 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006276 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6277 if (isinstance == -1) {
6278 return 1;
6279 }
6280 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006281 *out = In;
6282 return 0;
6283 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006284 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6285 if (isinstance == -1) {
6286 return 1;
6287 }
6288 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006289 *out = NotIn;
6290 return 0;
6291 }
6292
Benjamin Peterson5b066812010-11-20 01:38:49 +00006293 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006294 return 1;
6295}
6296
6297int
6298obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6299{
6300 PyObject* tmp = NULL;
6301 expr_ty target;
6302 expr_ty iter;
6303 asdl_seq* ifs;
6304
6305 if (PyObject_HasAttrString(obj, "target")) {
6306 int res;
6307 tmp = PyObject_GetAttrString(obj, "target");
6308 if (tmp == NULL) goto failed;
6309 res = obj2ast_expr(tmp, &target, arena);
6310 if (res != 0) goto failed;
6311 Py_XDECREF(tmp);
6312 tmp = NULL;
6313 } else {
6314 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6315 return 1;
6316 }
6317 if (PyObject_HasAttrString(obj, "iter")) {
6318 int res;
6319 tmp = PyObject_GetAttrString(obj, "iter");
6320 if (tmp == NULL) goto failed;
6321 res = obj2ast_expr(tmp, &iter, arena);
6322 if (res != 0) goto failed;
6323 Py_XDECREF(tmp);
6324 tmp = NULL;
6325 } else {
6326 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6327 return 1;
6328 }
6329 if (PyObject_HasAttrString(obj, "ifs")) {
6330 int res;
6331 Py_ssize_t len;
6332 Py_ssize_t i;
6333 tmp = PyObject_GetAttrString(obj, "ifs");
6334 if (tmp == NULL) goto failed;
6335 if (!PyList_Check(tmp)) {
6336 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6337 goto failed;
6338 }
6339 len = PyList_GET_SIZE(tmp);
6340 ifs = asdl_seq_new(len, arena);
6341 if (ifs == NULL) goto failed;
6342 for (i = 0; i < len; i++) {
6343 expr_ty value;
6344 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6345 if (res != 0) goto failed;
6346 asdl_seq_SET(ifs, i, value);
6347 }
6348 Py_XDECREF(tmp);
6349 tmp = NULL;
6350 } else {
6351 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6352 return 1;
6353 }
6354 *out = comprehension(target, iter, ifs, arena);
6355 return 0;
6356failed:
6357 Py_XDECREF(tmp);
6358 return 1;
6359}
6360
6361int
6362obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6363{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006364 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006365
Benjamin Petersond8f65972010-11-20 04:31:07 +00006366 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006367 int lineno;
6368 int col_offset;
6369
Neal Norwitzad74aa82008-03-31 05:14:30 +00006370 if (obj == Py_None) {
6371 *out = NULL;
6372 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006373 }
6374 if (PyObject_HasAttrString(obj, "lineno")) {
6375 int res;
6376 tmp = PyObject_GetAttrString(obj, "lineno");
6377 if (tmp == NULL) goto failed;
6378 res = obj2ast_int(tmp, &lineno, arena);
6379 if (res != 0) goto failed;
6380 Py_XDECREF(tmp);
6381 tmp = NULL;
6382 } else {
6383 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6384 return 1;
6385 }
6386 if (PyObject_HasAttrString(obj, "col_offset")) {
6387 int res;
6388 tmp = PyObject_GetAttrString(obj, "col_offset");
6389 if (tmp == NULL) goto failed;
6390 res = obj2ast_int(tmp, &col_offset, arena);
6391 if (res != 0) goto failed;
6392 Py_XDECREF(tmp);
6393 tmp = NULL;
6394 } else {
6395 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6396 return 1;
6397 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006398 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6399 if (isinstance == -1) {
6400 return 1;
6401 }
6402 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006403 expr_ty type;
6404 identifier name;
6405 asdl_seq* body;
6406
6407 if (PyObject_HasAttrString(obj, "type")) {
6408 int res;
6409 tmp = PyObject_GetAttrString(obj, "type");
6410 if (tmp == NULL) goto failed;
6411 res = obj2ast_expr(tmp, &type, arena);
6412 if (res != 0) goto failed;
6413 Py_XDECREF(tmp);
6414 tmp = NULL;
6415 } else {
6416 type = NULL;
6417 }
6418 if (PyObject_HasAttrString(obj, "name")) {
6419 int res;
6420 tmp = PyObject_GetAttrString(obj, "name");
6421 if (tmp == NULL) goto failed;
6422 res = obj2ast_identifier(tmp, &name, arena);
6423 if (res != 0) goto failed;
6424 Py_XDECREF(tmp);
6425 tmp = NULL;
6426 } else {
6427 name = NULL;
6428 }
6429 if (PyObject_HasAttrString(obj, "body")) {
6430 int res;
6431 Py_ssize_t len;
6432 Py_ssize_t i;
6433 tmp = PyObject_GetAttrString(obj, "body");
6434 if (tmp == NULL) goto failed;
6435 if (!PyList_Check(tmp)) {
6436 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6437 goto failed;
6438 }
6439 len = PyList_GET_SIZE(tmp);
6440 body = asdl_seq_new(len, arena);
6441 if (body == NULL) goto failed;
6442 for (i = 0; i < len; i++) {
6443 stmt_ty value;
6444 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6445 if (res != 0) goto failed;
6446 asdl_seq_SET(body, i, value);
6447 }
6448 Py_XDECREF(tmp);
6449 tmp = NULL;
6450 } else {
6451 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6452 return 1;
6453 }
6454 *out = ExceptHandler(type, name, body, lineno, col_offset,
6455 arena);
6456 if (*out == NULL) goto failed;
6457 return 0;
6458 }
6459
Benjamin Peterson5b066812010-11-20 01:38:49 +00006460 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6461 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006462 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006463 return 1;
6464}
6465
6466int
6467obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6468{
6469 PyObject* tmp = NULL;
6470 asdl_seq* args;
6471 identifier vararg;
6472 expr_ty varargannotation;
6473 asdl_seq* kwonlyargs;
6474 identifier kwarg;
6475 expr_ty kwargannotation;
6476 asdl_seq* defaults;
6477 asdl_seq* kw_defaults;
6478
6479 if (PyObject_HasAttrString(obj, "args")) {
6480 int res;
6481 Py_ssize_t len;
6482 Py_ssize_t i;
6483 tmp = PyObject_GetAttrString(obj, "args");
6484 if (tmp == NULL) goto failed;
6485 if (!PyList_Check(tmp)) {
6486 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6487 goto failed;
6488 }
6489 len = PyList_GET_SIZE(tmp);
6490 args = asdl_seq_new(len, arena);
6491 if (args == NULL) goto failed;
6492 for (i = 0; i < len; i++) {
6493 arg_ty value;
6494 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6495 if (res != 0) goto failed;
6496 asdl_seq_SET(args, i, value);
6497 }
6498 Py_XDECREF(tmp);
6499 tmp = NULL;
6500 } else {
6501 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6502 return 1;
6503 }
6504 if (PyObject_HasAttrString(obj, "vararg")) {
6505 int res;
6506 tmp = PyObject_GetAttrString(obj, "vararg");
6507 if (tmp == NULL) goto failed;
6508 res = obj2ast_identifier(tmp, &vararg, arena);
6509 if (res != 0) goto failed;
6510 Py_XDECREF(tmp);
6511 tmp = NULL;
6512 } else {
6513 vararg = NULL;
6514 }
6515 if (PyObject_HasAttrString(obj, "varargannotation")) {
6516 int res;
6517 tmp = PyObject_GetAttrString(obj, "varargannotation");
6518 if (tmp == NULL) goto failed;
6519 res = obj2ast_expr(tmp, &varargannotation, arena);
6520 if (res != 0) goto failed;
6521 Py_XDECREF(tmp);
6522 tmp = NULL;
6523 } else {
6524 varargannotation = NULL;
6525 }
6526 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6527 int res;
6528 Py_ssize_t len;
6529 Py_ssize_t i;
6530 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6531 if (tmp == NULL) goto failed;
6532 if (!PyList_Check(tmp)) {
6533 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6534 goto failed;
6535 }
6536 len = PyList_GET_SIZE(tmp);
6537 kwonlyargs = asdl_seq_new(len, arena);
6538 if (kwonlyargs == NULL) goto failed;
6539 for (i = 0; i < len; i++) {
6540 arg_ty value;
6541 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6542 if (res != 0) goto failed;
6543 asdl_seq_SET(kwonlyargs, i, value);
6544 }
6545 Py_XDECREF(tmp);
6546 tmp = NULL;
6547 } else {
6548 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6549 return 1;
6550 }
6551 if (PyObject_HasAttrString(obj, "kwarg")) {
6552 int res;
6553 tmp = PyObject_GetAttrString(obj, "kwarg");
6554 if (tmp == NULL) goto failed;
6555 res = obj2ast_identifier(tmp, &kwarg, arena);
6556 if (res != 0) goto failed;
6557 Py_XDECREF(tmp);
6558 tmp = NULL;
6559 } else {
6560 kwarg = NULL;
6561 }
6562 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6563 int res;
6564 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6565 if (tmp == NULL) goto failed;
6566 res = obj2ast_expr(tmp, &kwargannotation, arena);
6567 if (res != 0) goto failed;
6568 Py_XDECREF(tmp);
6569 tmp = NULL;
6570 } else {
6571 kwargannotation = NULL;
6572 }
6573 if (PyObject_HasAttrString(obj, "defaults")) {
6574 int res;
6575 Py_ssize_t len;
6576 Py_ssize_t i;
6577 tmp = PyObject_GetAttrString(obj, "defaults");
6578 if (tmp == NULL) goto failed;
6579 if (!PyList_Check(tmp)) {
6580 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6581 goto failed;
6582 }
6583 len = PyList_GET_SIZE(tmp);
6584 defaults = asdl_seq_new(len, arena);
6585 if (defaults == NULL) goto failed;
6586 for (i = 0; i < len; i++) {
6587 expr_ty value;
6588 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6589 if (res != 0) goto failed;
6590 asdl_seq_SET(defaults, i, value);
6591 }
6592 Py_XDECREF(tmp);
6593 tmp = NULL;
6594 } else {
6595 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6596 return 1;
6597 }
6598 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6599 int res;
6600 Py_ssize_t len;
6601 Py_ssize_t i;
6602 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6603 if (tmp == NULL) goto failed;
6604 if (!PyList_Check(tmp)) {
6605 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6606 goto failed;
6607 }
6608 len = PyList_GET_SIZE(tmp);
6609 kw_defaults = asdl_seq_new(len, arena);
6610 if (kw_defaults == NULL) goto failed;
6611 for (i = 0; i < len; i++) {
6612 expr_ty value;
6613 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6614 if (res != 0) goto failed;
6615 asdl_seq_SET(kw_defaults, i, value);
6616 }
6617 Py_XDECREF(tmp);
6618 tmp = NULL;
6619 } else {
6620 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6621 return 1;
6622 }
6623 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6624 kwargannotation, defaults, kw_defaults, arena);
6625 return 0;
6626failed:
6627 Py_XDECREF(tmp);
6628 return 1;
6629}
6630
6631int
6632obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6633{
6634 PyObject* tmp = NULL;
6635 identifier arg;
6636 expr_ty annotation;
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 arg");
6648 return 1;
6649 }
6650 if (PyObject_HasAttrString(obj, "annotation")) {
6651 int res;
6652 tmp = PyObject_GetAttrString(obj, "annotation");
6653 if (tmp == NULL) goto failed;
6654 res = obj2ast_expr(tmp, &annotation, arena);
6655 if (res != 0) goto failed;
6656 Py_XDECREF(tmp);
6657 tmp = NULL;
6658 } else {
6659 annotation = NULL;
6660 }
6661 *out = arg(arg, annotation, arena);
6662 return 0;
6663failed:
6664 Py_XDECREF(tmp);
6665 return 1;
6666}
6667
6668int
6669obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6670{
6671 PyObject* tmp = NULL;
6672 identifier arg;
6673 expr_ty value;
6674
6675 if (PyObject_HasAttrString(obj, "arg")) {
6676 int res;
6677 tmp = PyObject_GetAttrString(obj, "arg");
6678 if (tmp == NULL) goto failed;
6679 res = obj2ast_identifier(tmp, &arg, arena);
6680 if (res != 0) goto failed;
6681 Py_XDECREF(tmp);
6682 tmp = NULL;
6683 } else {
6684 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6685 return 1;
6686 }
6687 if (PyObject_HasAttrString(obj, "value")) {
6688 int res;
6689 tmp = PyObject_GetAttrString(obj, "value");
6690 if (tmp == NULL) goto failed;
6691 res = obj2ast_expr(tmp, &value, arena);
6692 if (res != 0) goto failed;
6693 Py_XDECREF(tmp);
6694 tmp = NULL;
6695 } else {
6696 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6697 return 1;
6698 }
6699 *out = keyword(arg, value, arena);
6700 return 0;
6701failed:
6702 Py_XDECREF(tmp);
6703 return 1;
6704}
6705
6706int
6707obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6708{
6709 PyObject* tmp = NULL;
6710 identifier name;
6711 identifier asname;
6712
6713 if (PyObject_HasAttrString(obj, "name")) {
6714 int res;
6715 tmp = PyObject_GetAttrString(obj, "name");
6716 if (tmp == NULL) goto failed;
6717 res = obj2ast_identifier(tmp, &name, arena);
6718 if (res != 0) goto failed;
6719 Py_XDECREF(tmp);
6720 tmp = NULL;
6721 } else {
6722 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6723 return 1;
6724 }
6725 if (PyObject_HasAttrString(obj, "asname")) {
6726 int res;
6727 tmp = PyObject_GetAttrString(obj, "asname");
6728 if (tmp == NULL) goto failed;
6729 res = obj2ast_identifier(tmp, &asname, arena);
6730 if (res != 0) goto failed;
6731 Py_XDECREF(tmp);
6732 tmp = NULL;
6733 } else {
6734 asname = NULL;
6735 }
6736 *out = alias(name, asname, arena);
6737 return 0;
6738failed:
6739 Py_XDECREF(tmp);
6740 return 1;
6741}
6742
6743
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;
Benjamin Peterson2193d2b2011-07-22 10:50:23 -05006759 if (PyModule_AddStringConstant(m, "__version__", "") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006760 return NULL;
6761 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6762 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006763 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006764 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006765 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006766 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006767 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006768 0) return NULL;
6769 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6770 NULL;
6771 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6772 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006773 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006774 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006775 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_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, "Return", (PyObject*)Return_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, "Delete", (PyObject*)Delete_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, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006782 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006783 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006784 0) return NULL;
6785 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6786 NULL;
6787 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6788 NULL;
6789 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6790 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6791 NULL;
6792 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6793 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006794 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006795 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006796 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006797 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006798 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006799 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006800 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006801 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006802 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006803 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006804 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006805 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006806 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006807 return NULL;
6808 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6809 NULL;
6810 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6811 NULL;
6812 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6813 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006814 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006815 return NULL;
6816 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6817 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006818 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006819 return NULL;
6820 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6821 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006822 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006823 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006824 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006825 return NULL;
6826 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6827 NULL;
6828 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6829 NULL;
6830 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6831 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006832 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006833 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006834 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006835 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006836 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006837 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006838 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006839 (PyObject*)GeneratorExp_type) < 0) return NULL;
6840 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6841 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006842 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006843 return NULL;
6844 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6845 NULL;
6846 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6847 NULL;
6848 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6849 NULL;
6850 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6851 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006852 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006853 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006854 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006855 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006856 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006857 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006858 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006859 return NULL;
6860 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6861 NULL;
6862 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6863 NULL;
6864 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6865 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006866 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006867 (PyObject*)expr_context_type) < 0) return NULL;
6868 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6873 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006874 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006875 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006876 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006877 return NULL;
6878 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6879 NULL;
6880 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6881 NULL;
6882 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6883 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006884 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006885 return NULL;
6886 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6887 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006888 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006889 return NULL;
6890 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6891 NULL;
6892 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006893 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006894 return NULL;
6895 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6896 NULL;
6897 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6898 NULL;
6899 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6904 NULL;
6905 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6906 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006907 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006908 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006909 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006910 return NULL;
6911 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6912 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_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, "BitAnd", (PyObject*)BitAnd_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, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006918 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006919 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006920 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006921 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006922 return NULL;
6923 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6924 NULL;
6925 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6926 NULL;
6927 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6928 NULL;
6929 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6930 NULL;
6931 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6932 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6933 NULL;
6934 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6935 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6936 NULL;
6937 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6938 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6939 NULL;
6940 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6941 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6942 NULL;
6943 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6944 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6945 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006946 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006947 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006948 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006949 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006950 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006951 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006952 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006953 0) return NULL;
6954 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6955 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006956 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006957 return NULL;
6958 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6959 NULL;
6960 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006961}
6962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006963
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006964PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006965{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006966 init_types();
6967 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006968}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006969
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006970/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6971mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006972{
6973 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006974 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6975 (PyObject*)Interactive_type};
6976 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006977 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006978 assert(0 <= mode && mode <= 2);
6979
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006980 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006981
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006982 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6983 if (isinstance == -1)
6984 return NULL;
6985 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006986 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6987 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006988 return NULL;
6989 }
6990 if (obj2ast_mod(ast, &res, arena) != 0)
6991 return NULL;
6992 else
6993 return res;
6994}
6995
6996int PyAST_Check(PyObject* obj)
6997{
6998 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006999 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007000}
7001
Martin v. Löwis5b222132007-06-10 09:51:05 +00007002