blob: e03dac0bb52537e39bc4898ef9eea12ca5309f3a [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson2e24deb2009-07-17 19:11:02 +00005 __version__ 73626.
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 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000530 result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(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);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000540 if (!l) return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 for(i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000542 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000543 if (!s) {
544 Py_DECREF(l);
545 return 0;
546 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000547 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000548 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
550 Py_DECREF(l);
551 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000552}
553
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000554/* Conversion AST -> Python */
555
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000556static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
557{
558 int i, n = asdl_seq_LEN(seq);
559 PyObject *result = PyList_New(n);
560 PyObject *value;
561 if (!result)
562 return NULL;
563 for (i = 0; i < n; i++) {
564 value = func(asdl_seq_GET(seq, i));
565 if (!value) {
566 Py_DECREF(result);
567 return NULL;
568 }
569 PyList_SET_ITEM(result, i, value);
570 }
571 return result;
572}
573
574static PyObject* ast2obj_object(void *o)
575{
576 if (!o)
577 o = Py_None;
578 Py_INCREF((PyObject*)o);
579 return (PyObject*)o;
580}
581#define ast2obj_identifier ast2obj_object
582#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000583
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000584static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000585{
Christian Heimes217cfd12007-12-02 14:31:20 +0000586 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000587}
588
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000589/* Conversion Python -> AST */
590
591static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
592{
593 if (obj == Py_None)
594 obj = NULL;
595 if (obj)
596 PyArena_AddPyObject(arena, obj);
597 Py_XINCREF(obj);
598 *out = obj;
599 return 0;
600}
601
602#define obj2ast_identifier obj2ast_object
603#define obj2ast_string obj2ast_object
604
605static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
606{
607 int i;
608 if (!PyLong_Check(obj)) {
609 PyObject *s = PyObject_Repr(obj);
610 if (s == NULL) return 1;
611 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000612 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000613 Py_DECREF(s);
614 return 1;
615 }
616
617 i = (int)PyLong_AsLong(obj);
618 if (i == -1 && PyErr_Occurred())
619 return 1;
620 *out = i;
621 return 0;
622}
623
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000624static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000625{
626 PyObject *empty_tuple, *d;
627 if (PyType_Ready(&AST_type) < 0)
628 return -1;
629 d = AST_type.tp_dict;
630 empty_tuple = PyTuple_New(0);
631 if (!empty_tuple ||
632 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
633 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
634 Py_XDECREF(empty_tuple);
635 return -1;
636 }
637 Py_DECREF(empty_tuple);
638 return 0;
639}
640
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000641
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000642static int init_types(void)
643{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000645 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000646 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000647 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 if (!mod_type) return 0;
649 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 Interactive_type = make_type("Interactive", mod_type,
653 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000655 Expression_type = make_type("Expression", mod_type, Expression_fields,
656 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000660 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000662 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000664 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000666 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000682 With_type = make_type("With", stmt_type, With_fields, 3);
683 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000684 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
689 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000696 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000700 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
701 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000704 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000706 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000708 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000710 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000712 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000721 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000725 Set_type = make_type("Set", expr_type, Set_fields, 1);
726 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000729 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
730 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000731 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
732 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733 GeneratorExp_type = make_type("GeneratorExp", expr_type,
734 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000736 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000740 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000746 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
747 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000748 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
749 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000754 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
755 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000760 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000762 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!expr_context_type) return 0;
764 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000765 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000769 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000771 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000773 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000777 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000779 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000781 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000783 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000785 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000789 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!slice_type) return 0;
791 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000792 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000794 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000796 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000798 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!boolop_type) return 0;
800 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000801 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000803 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000805 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000807 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000809 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000810 if (!operator_type) return 0;
811 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000812 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000814 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000816 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000818 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000820 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000822 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000824 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000826 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000828 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000830 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000832 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000834 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000836 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000838 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000840 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000842 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000844 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000846 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000848 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000850 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000852 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000854 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000856 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000858 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000860 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!unaryop_type) return 0;
862 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000863 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000865 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000867 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000869 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000871 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000873 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000875 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000877 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000879 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!cmpop_type) return 0;
881 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000882 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000884 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000886 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000888 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000890 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000892 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000894 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000896 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000898 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000900 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000902 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000904 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000906 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000908 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000910 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000912 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000914 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000916 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000917 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000918 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000919 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000920 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000922 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000923 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000924 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000925 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000926 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000927 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
928 return 0;
929 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
930 ExceptHandler_fields, 3);
931 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000932 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000933 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000934 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000935 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000936 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000937 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000938 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000939 if (!alias_type) return 0;
940 initialized = 1;
941 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000942}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000943
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000944static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
945static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
946static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
947static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
948 arena);
949static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
950static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
951static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
952static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
953static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
954static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
955 arena);
956static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
957 arena);
958static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
959static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
960static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
961static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000964Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965{
966 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000967 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000968 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 p->kind = Module_kind;
971 p->v.Module.body = body;
972 return p;
973}
974
975mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977{
978 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000980 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 p->kind = Interactive_kind;
983 p->v.Interactive.body = body;
984 return p;
985}
986
987mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000988Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
990 mod_ty p;
991 if (!body) {
992 PyErr_SetString(PyExc_ValueError,
993 "field body is required for Expression");
994 return NULL;
995 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000997 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 p->kind = Expression_kind;
1000 p->v.Expression.body = body;
1001 return p;
1002}
1003
1004mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001005Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006{
1007 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001008 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001009 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011 p->kind = Suite_kind;
1012 p->v.Suite.body = body;
1013 return p;
1014}
1015
1016stmt_ty
1017FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001018 decorator_list, expr_ty returns, int lineno, int col_offset,
1019 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020{
1021 stmt_ty p;
1022 if (!name) {
1023 PyErr_SetString(PyExc_ValueError,
1024 "field name is required for FunctionDef");
1025 return NULL;
1026 }
1027 if (!args) {
1028 PyErr_SetString(PyExc_ValueError,
1029 "field args is required for FunctionDef");
1030 return NULL;
1031 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001032 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001033 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 p->kind = FunctionDef_kind;
1036 p->v.FunctionDef.name = name;
1037 p->v.FunctionDef.args = args;
1038 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001039 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001040 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001042 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 return p;
1044}
1045
1046stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001047ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001048 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1049 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050{
1051 stmt_ty p;
1052 if (!name) {
1053 PyErr_SetString(PyExc_ValueError,
1054 "field name is required for ClassDef");
1055 return NULL;
1056 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001057 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001058 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 p->kind = ClassDef_kind;
1061 p->v.ClassDef.name = name;
1062 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001063 p->v.ClassDef.keywords = keywords;
1064 p->v.ClassDef.starargs = starargs;
1065 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001067 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001069 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 return p;
1071}
1072
1073stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001074Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075{
1076 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001077 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001078 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 p->kind = Return_kind;
1081 p->v.Return.value = value;
1082 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001083 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 return p;
1085}
1086
1087stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001088Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089{
1090 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001091 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001092 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 p->kind = Delete_kind;
1095 p->v.Delete.targets = targets;
1096 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001097 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 return p;
1099}
1100
1101stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001102Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1103 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104{
1105 stmt_ty p;
1106 if (!value) {
1107 PyErr_SetString(PyExc_ValueError,
1108 "field value is required for Assign");
1109 return NULL;
1110 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001112 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 p->kind = Assign_kind;
1115 p->v.Assign.targets = targets;
1116 p->v.Assign.value = value;
1117 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001118 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 return p;
1120}
1121
1122stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001123AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1124 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125{
1126 stmt_ty p;
1127 if (!target) {
1128 PyErr_SetString(PyExc_ValueError,
1129 "field target is required for AugAssign");
1130 return NULL;
1131 }
1132 if (!op) {
1133 PyErr_SetString(PyExc_ValueError,
1134 "field op is required for AugAssign");
1135 return NULL;
1136 }
1137 if (!value) {
1138 PyErr_SetString(PyExc_ValueError,
1139 "field value is required for AugAssign");
1140 return NULL;
1141 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001143 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 p->kind = AugAssign_kind;
1146 p->v.AugAssign.target = target;
1147 p->v.AugAssign.op = op;
1148 p->v.AugAssign.value = value;
1149 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001150 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return p;
1152}
1153
1154stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001156 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157{
1158 stmt_ty p;
1159 if (!target) {
1160 PyErr_SetString(PyExc_ValueError,
1161 "field target is required for For");
1162 return NULL;
1163 }
1164 if (!iter) {
1165 PyErr_SetString(PyExc_ValueError,
1166 "field iter is required for For");
1167 return NULL;
1168 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001169 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001170 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 p->kind = For_kind;
1173 p->v.For.target = target;
1174 p->v.For.iter = iter;
1175 p->v.For.body = body;
1176 p->v.For.orelse = orelse;
1177 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001178 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 return p;
1180}
1181
1182stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001183While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1184 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185{
1186 stmt_ty p;
1187 if (!test) {
1188 PyErr_SetString(PyExc_ValueError,
1189 "field test is required for While");
1190 return NULL;
1191 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001193 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 p->kind = While_kind;
1196 p->v.While.test = test;
1197 p->v.While.body = body;
1198 p->v.While.orelse = orelse;
1199 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return p;
1202}
1203
1204stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001205If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1206 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207{
1208 stmt_ty p;
1209 if (!test) {
1210 PyErr_SetString(PyExc_ValueError,
1211 "field test is required for If");
1212 return NULL;
1213 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001214 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001215 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 p->kind = If_kind;
1218 p->v.If.test = test;
1219 p->v.If.body = body;
1220 p->v.If.orelse = orelse;
1221 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return p;
1224}
1225
1226stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001227With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001228 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001229{
1230 stmt_ty p;
1231 if (!context_expr) {
1232 PyErr_SetString(PyExc_ValueError,
1233 "field context_expr is required for With");
1234 return NULL;
1235 }
1236 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001237 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001238 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001239 p->kind = With_kind;
1240 p->v.With.context_expr = context_expr;
1241 p->v.With.optional_vars = optional_vars;
1242 p->v.With.body = body;
1243 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001244 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001245 return p;
1246}
1247
1248stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001249Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250{
1251 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001252 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001253 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001256 p->v.Raise.exc = exc;
1257 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001259 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 return p;
1261}
1262
1263stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001264TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001265 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266{
1267 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001269 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 p->kind = TryExcept_kind;
1272 p->v.TryExcept.body = body;
1273 p->v.TryExcept.handlers = handlers;
1274 p->v.TryExcept.orelse = orelse;
1275 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001276 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 return p;
1278}
1279
1280stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1282 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283{
1284 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001285 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001286 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 p->kind = TryFinally_kind;
1289 p->v.TryFinally.body = body;
1290 p->v.TryFinally.finalbody = finalbody;
1291 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001292 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 return p;
1294}
1295
1296stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001297Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298{
1299 stmt_ty p;
1300 if (!test) {
1301 PyErr_SetString(PyExc_ValueError,
1302 "field test is required for Assert");
1303 return NULL;
1304 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001306 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 p->kind = Assert_kind;
1309 p->v.Assert.test = test;
1310 p->v.Assert.msg = msg;
1311 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001312 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return p;
1314}
1315
1316stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001317Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318{
1319 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001320 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001321 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 p->kind = Import_kind;
1324 p->v.Import.names = names;
1325 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001326 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 return p;
1328}
1329
1330stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001331ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1332 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333{
1334 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001335 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001336 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 p->kind = ImportFrom_kind;
1339 p->v.ImportFrom.module = module;
1340 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001341 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001343 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 return p;
1345}
1346
1347stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001348Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349{
1350 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001351 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001352 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 p->kind = Global_kind;
1355 p->v.Global.names = names;
1356 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001357 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 return p;
1359}
1360
1361stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001362Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1363{
1364 stmt_ty p;
1365 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1366 if (!p)
1367 return NULL;
1368 p->kind = Nonlocal_kind;
1369 p->v.Nonlocal.names = names;
1370 p->lineno = lineno;
1371 p->col_offset = col_offset;
1372 return p;
1373}
1374
1375stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001376Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377{
1378 stmt_ty p;
1379 if (!value) {
1380 PyErr_SetString(PyExc_ValueError,
1381 "field value is required for Expr");
1382 return NULL;
1383 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001385 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 p->kind = Expr_kind;
1388 p->v.Expr.value = value;
1389 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001390 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 return p;
1392}
1393
1394stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001395Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396{
1397 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001398 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001399 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 p->kind = Pass_kind;
1402 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001403 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 return p;
1405}
1406
1407stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001408Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409{
1410 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001411 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001412 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 p->kind = Break_kind;
1415 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return p;
1418}
1419
1420stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422{
1423 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001424 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001425 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 p->kind = Continue_kind;
1428 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001429 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 return p;
1431}
1432
1433expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001434BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1435 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436{
1437 expr_ty p;
1438 if (!op) {
1439 PyErr_SetString(PyExc_ValueError,
1440 "field op is required for BoolOp");
1441 return NULL;
1442 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001443 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001444 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 p->kind = BoolOp_kind;
1447 p->v.BoolOp.op = op;
1448 p->v.BoolOp.values = values;
1449 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 return p;
1452}
1453
1454expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001455BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1456 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457{
1458 expr_ty p;
1459 if (!left) {
1460 PyErr_SetString(PyExc_ValueError,
1461 "field left is required for BinOp");
1462 return NULL;
1463 }
1464 if (!op) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "field op is required for BinOp");
1467 return NULL;
1468 }
1469 if (!right) {
1470 PyErr_SetString(PyExc_ValueError,
1471 "field right is required for BinOp");
1472 return NULL;
1473 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001474 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001475 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 p->kind = BinOp_kind;
1478 p->v.BinOp.left = left;
1479 p->v.BinOp.op = op;
1480 p->v.BinOp.right = right;
1481 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001482 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483 return p;
1484}
1485
1486expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001487UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1488 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489{
1490 expr_ty p;
1491 if (!op) {
1492 PyErr_SetString(PyExc_ValueError,
1493 "field op is required for UnaryOp");
1494 return NULL;
1495 }
1496 if (!operand) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "field operand is required for UnaryOp");
1499 return NULL;
1500 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001501 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001502 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 p->kind = UnaryOp_kind;
1505 p->v.UnaryOp.op = op;
1506 p->v.UnaryOp.operand = operand;
1507 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001508 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 return p;
1510}
1511
1512expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001513Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1514 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515{
1516 expr_ty p;
1517 if (!args) {
1518 PyErr_SetString(PyExc_ValueError,
1519 "field args is required for Lambda");
1520 return NULL;
1521 }
1522 if (!body) {
1523 PyErr_SetString(PyExc_ValueError,
1524 "field body is required for Lambda");
1525 return NULL;
1526 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001527 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001528 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 p->kind = Lambda_kind;
1531 p->v.Lambda.args = args;
1532 p->v.Lambda.body = body;
1533 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001534 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 return p;
1536}
1537
1538expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001539IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1540 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001541{
1542 expr_ty p;
1543 if (!test) {
1544 PyErr_SetString(PyExc_ValueError,
1545 "field test is required for IfExp");
1546 return NULL;
1547 }
1548 if (!body) {
1549 PyErr_SetString(PyExc_ValueError,
1550 "field body is required for IfExp");
1551 return NULL;
1552 }
1553 if (!orelse) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "field orelse is required for IfExp");
1556 return NULL;
1557 }
1558 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001559 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001560 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001561 p->kind = IfExp_kind;
1562 p->v.IfExp.test = test;
1563 p->v.IfExp.body = body;
1564 p->v.IfExp.orelse = orelse;
1565 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001566 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001567 return p;
1568}
1569
1570expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001571Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1572 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573{
1574 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001575 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001576 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 p->kind = Dict_kind;
1579 p->v.Dict.keys = keys;
1580 p->v.Dict.values = values;
1581 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001582 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 return p;
1584}
1585
1586expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001587Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1588{
1589 expr_ty p;
1590 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001591 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001592 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001593 p->kind = Set_kind;
1594 p->v.Set.elts = elts;
1595 p->lineno = lineno;
1596 p->col_offset = col_offset;
1597 return p;
1598}
1599
1600expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001601ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1602 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603{
1604 expr_ty p;
1605 if (!elt) {
1606 PyErr_SetString(PyExc_ValueError,
1607 "field elt is required for ListComp");
1608 return NULL;
1609 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001610 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001611 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 p->kind = ListComp_kind;
1614 p->v.ListComp.elt = elt;
1615 p->v.ListComp.generators = generators;
1616 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001617 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 return p;
1619}
1620
1621expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001622SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1623 *arena)
1624{
1625 expr_ty p;
1626 if (!elt) {
1627 PyErr_SetString(PyExc_ValueError,
1628 "field elt is required for SetComp");
1629 return NULL;
1630 }
1631 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1632 if (!p)
1633 return NULL;
1634 p->kind = SetComp_kind;
1635 p->v.SetComp.elt = elt;
1636 p->v.SetComp.generators = generators;
1637 p->lineno = lineno;
1638 p->col_offset = col_offset;
1639 return p;
1640}
1641
1642expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001643DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1644 col_offset, PyArena *arena)
1645{
1646 expr_ty p;
1647 if (!key) {
1648 PyErr_SetString(PyExc_ValueError,
1649 "field key is required for DictComp");
1650 return NULL;
1651 }
1652 if (!value) {
1653 PyErr_SetString(PyExc_ValueError,
1654 "field value is required for DictComp");
1655 return NULL;
1656 }
1657 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1658 if (!p)
1659 return NULL;
1660 p->kind = DictComp_kind;
1661 p->v.DictComp.key = key;
1662 p->v.DictComp.value = value;
1663 p->v.DictComp.generators = generators;
1664 p->lineno = lineno;
1665 p->col_offset = col_offset;
1666 return p;
1667}
1668
1669expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001670GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1671 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672{
1673 expr_ty p;
1674 if (!elt) {
1675 PyErr_SetString(PyExc_ValueError,
1676 "field elt is required for GeneratorExp");
1677 return NULL;
1678 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001679 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001680 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 p->kind = GeneratorExp_kind;
1683 p->v.GeneratorExp.elt = elt;
1684 p->v.GeneratorExp.generators = generators;
1685 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001686 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 return p;
1688}
1689
1690expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001691Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692{
1693 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001694 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001695 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 p->kind = Yield_kind;
1698 p->v.Yield.value = value;
1699 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001700 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 return p;
1702}
1703
1704expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001705Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1706 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707{
1708 expr_ty p;
1709 if (!left) {
1710 PyErr_SetString(PyExc_ValueError,
1711 "field left is required for Compare");
1712 return NULL;
1713 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001714 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001715 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 p->kind = Compare_kind;
1718 p->v.Compare.left = left;
1719 p->v.Compare.ops = ops;
1720 p->v.Compare.comparators = comparators;
1721 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001722 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 return p;
1724}
1725
1726expr_ty
1727Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729{
1730 expr_ty p;
1731 if (!func) {
1732 PyErr_SetString(PyExc_ValueError,
1733 "field func is required for Call");
1734 return NULL;
1735 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001736 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001737 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 p->kind = Call_kind;
1740 p->v.Call.func = func;
1741 p->v.Call.args = args;
1742 p->v.Call.keywords = keywords;
1743 p->v.Call.starargs = starargs;
1744 p->v.Call.kwargs = kwargs;
1745 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return p;
1748}
1749
1750expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001751Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
1753 expr_ty p;
1754 if (!n) {
1755 PyErr_SetString(PyExc_ValueError,
1756 "field n is required for Num");
1757 return NULL;
1758 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001759 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001760 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762 p->kind = Num_kind;
1763 p->v.Num.n = n;
1764 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001765 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return p;
1767}
1768
1769expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001770Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771{
1772 expr_ty p;
1773 if (!s) {
1774 PyErr_SetString(PyExc_ValueError,
1775 "field s is required for Str");
1776 return NULL;
1777 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001778 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001779 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 p->kind = Str_kind;
1782 p->v.Str.s = s;
1783 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001784 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 return p;
1786}
1787
1788expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001789Bytes(string s, int lineno, int col_offset, PyArena *arena)
1790{
1791 expr_ty p;
1792 if (!s) {
1793 PyErr_SetString(PyExc_ValueError,
1794 "field s is required for Bytes");
1795 return NULL;
1796 }
1797 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001798 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001799 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001800 p->kind = Bytes_kind;
1801 p->v.Bytes.s = s;
1802 p->lineno = lineno;
1803 p->col_offset = col_offset;
1804 return p;
1805}
1806
1807expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001808Ellipsis(int lineno, int col_offset, PyArena *arena)
1809{
1810 expr_ty p;
1811 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001812 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001813 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001814 p->kind = Ellipsis_kind;
1815 p->lineno = lineno;
1816 p->col_offset = col_offset;
1817 return p;
1818}
1819
1820expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001821Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1822 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823{
1824 expr_ty p;
1825 if (!value) {
1826 PyErr_SetString(PyExc_ValueError,
1827 "field value is required for Attribute");
1828 return NULL;
1829 }
1830 if (!attr) {
1831 PyErr_SetString(PyExc_ValueError,
1832 "field attr is required for Attribute");
1833 return NULL;
1834 }
1835 if (!ctx) {
1836 PyErr_SetString(PyExc_ValueError,
1837 "field ctx is required for Attribute");
1838 return NULL;
1839 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001841 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 p->kind = Attribute_kind;
1844 p->v.Attribute.value = value;
1845 p->v.Attribute.attr = attr;
1846 p->v.Attribute.ctx = ctx;
1847 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001848 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 return p;
1850}
1851
1852expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001853Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1854 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855{
1856 expr_ty p;
1857 if (!value) {
1858 PyErr_SetString(PyExc_ValueError,
1859 "field value is required for Subscript");
1860 return NULL;
1861 }
1862 if (!slice) {
1863 PyErr_SetString(PyExc_ValueError,
1864 "field slice is required for Subscript");
1865 return NULL;
1866 }
1867 if (!ctx) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field ctx is required for Subscript");
1870 return NULL;
1871 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001873 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 p->kind = Subscript_kind;
1876 p->v.Subscript.value = value;
1877 p->v.Subscript.slice = slice;
1878 p->v.Subscript.ctx = ctx;
1879 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001880 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 return p;
1882}
1883
1884expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001885Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1886 *arena)
1887{
1888 expr_ty p;
1889 if (!value) {
1890 PyErr_SetString(PyExc_ValueError,
1891 "field value is required for Starred");
1892 return NULL;
1893 }
1894 if (!ctx) {
1895 PyErr_SetString(PyExc_ValueError,
1896 "field ctx is required for Starred");
1897 return NULL;
1898 }
1899 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1900 if (!p)
1901 return NULL;
1902 p->kind = Starred_kind;
1903 p->v.Starred.value = value;
1904 p->v.Starred.ctx = ctx;
1905 p->lineno = lineno;
1906 p->col_offset = col_offset;
1907 return p;
1908}
1909
1910expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001911Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1912 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913{
1914 expr_ty p;
1915 if (!id) {
1916 PyErr_SetString(PyExc_ValueError,
1917 "field id is required for Name");
1918 return NULL;
1919 }
1920 if (!ctx) {
1921 PyErr_SetString(PyExc_ValueError,
1922 "field ctx is required for Name");
1923 return NULL;
1924 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001925 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001926 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 p->kind = Name_kind;
1929 p->v.Name.id = id;
1930 p->v.Name.ctx = ctx;
1931 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001932 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 return p;
1934}
1935
1936expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001937List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1938 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939{
1940 expr_ty p;
1941 if (!ctx) {
1942 PyErr_SetString(PyExc_ValueError,
1943 "field ctx is required for List");
1944 return NULL;
1945 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001946 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001947 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 p->kind = List_kind;
1950 p->v.List.elts = elts;
1951 p->v.List.ctx = ctx;
1952 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001953 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 return p;
1955}
1956
1957expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001958Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1959 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960{
1961 expr_ty p;
1962 if (!ctx) {
1963 PyErr_SetString(PyExc_ValueError,
1964 "field ctx is required for Tuple");
1965 return NULL;
1966 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001967 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001968 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 p->kind = Tuple_kind;
1971 p->v.Tuple.elts = elts;
1972 p->v.Tuple.ctx = ctx;
1973 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001974 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 return p;
1976}
1977
1978slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001979Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980{
1981 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001983 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 p->kind = Slice_kind;
1986 p->v.Slice.lower = lower;
1987 p->v.Slice.upper = upper;
1988 p->v.Slice.step = step;
1989 return p;
1990}
1991
1992slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994{
1995 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001996 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001997 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 p->kind = ExtSlice_kind;
2000 p->v.ExtSlice.dims = dims;
2001 return p;
2002}
2003
2004slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002005Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006{
2007 slice_ty p;
2008 if (!value) {
2009 PyErr_SetString(PyExc_ValueError,
2010 "field value is required for Index");
2011 return NULL;
2012 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002013 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002014 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 p->kind = Index_kind;
2017 p->v.Index.value = value;
2018 return p;
2019}
2020
2021comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023{
2024 comprehension_ty p;
2025 if (!target) {
2026 PyErr_SetString(PyExc_ValueError,
2027 "field target is required for comprehension");
2028 return NULL;
2029 }
2030 if (!iter) {
2031 PyErr_SetString(PyExc_ValueError,
2032 "field iter is required for comprehension");
2033 return NULL;
2034 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002036 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 p->target = target;
2039 p->iter = iter;
2040 p->ifs = ifs;
2041 return p;
2042}
2043
2044excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002045ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002046 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047{
2048 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002049 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002050 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002052 p->kind = ExceptHandler_kind;
2053 p->v.ExceptHandler.type = type;
2054 p->v.ExceptHandler.name = name;
2055 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002056 p->lineno = lineno;
2057 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 return p;
2059}
2060
2061arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002062arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2063 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2064 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065{
2066 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067 p = (arguments_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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 p->args = args;
2071 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002072 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002073 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002075 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002077 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 return p;
2079}
2080
Neal Norwitzc1505362006-12-28 06:47:50 +00002081arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002082arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002083{
2084 arg_ty p;
2085 if (!arg) {
2086 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002087 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002088 return NULL;
2089 }
2090 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002091 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002092 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002093 p->arg = arg;
2094 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002095 return p;
2096}
2097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100{
2101 keyword_ty p;
2102 if (!arg) {
2103 PyErr_SetString(PyExc_ValueError,
2104 "field arg is required for keyword");
2105 return NULL;
2106 }
2107 if (!value) {
2108 PyErr_SetString(PyExc_ValueError,
2109 "field value is required for keyword");
2110 return NULL;
2111 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002112 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002113 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 p->arg = arg;
2116 p->value = value;
2117 return p;
2118}
2119
2120alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002121alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122{
2123 alias_ty p;
2124 if (!name) {
2125 PyErr_SetString(PyExc_ValueError,
2126 "field name is required for alias");
2127 return NULL;
2128 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002129 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002130 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 p->name = name;
2133 p->asname = asname;
2134 return p;
2135}
2136
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002137
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002138PyObject*
2139ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002140{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002141 mod_ty o = (mod_ty)_o;
2142 PyObject *result = NULL, *value = NULL;
2143 if (!o) {
2144 Py_INCREF(Py_None);
2145 return Py_None;
2146 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 switch (o->kind) {
2149 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002150 result = PyType_GenericNew(Module_type, NULL, NULL);
2151 if (!result) goto failed;
2152 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2153 if (!value) goto failed;
2154 if (PyObject_SetAttrString(result, "body", value) == -1)
2155 goto failed;
2156 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 break;
2158 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002159 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2160 if (!result) goto failed;
2161 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2162 if (!value) goto failed;
2163 if (PyObject_SetAttrString(result, "body", value) == -1)
2164 goto failed;
2165 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 break;
2167 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002168 result = PyType_GenericNew(Expression_type, NULL, NULL);
2169 if (!result) goto failed;
2170 value = ast2obj_expr(o->v.Expression.body);
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 Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002177 result = PyType_GenericNew(Suite_type, NULL, NULL);
2178 if (!result) goto failed;
2179 value = ast2obj_list(o->v.Suite.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 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002186 return result;
2187failed:
2188 Py_XDECREF(value);
2189 Py_XDECREF(result);
2190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191}
2192
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002193PyObject*
2194ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002196 stmt_ty o = (stmt_ty)_o;
2197 PyObject *result = NULL, *value = NULL;
2198 if (!o) {
2199 Py_INCREF(Py_None);
2200 return Py_None;
2201 }
2202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 switch (o->kind) {
2204 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002205 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2206 if (!result) goto failed;
2207 value = ast2obj_identifier(o->v.FunctionDef.name);
2208 if (!value) goto failed;
2209 if (PyObject_SetAttrString(result, "name", value) == -1)
2210 goto failed;
2211 Py_DECREF(value);
2212 value = ast2obj_arguments(o->v.FunctionDef.args);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "args", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
2217 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2218 if (!value) goto failed;
2219 if (PyObject_SetAttrString(result, "body", value) == -1)
2220 goto failed;
2221 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002222 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2223 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002224 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002225 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2226 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002227 goto failed;
2228 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002229 value = ast2obj_expr(o->v.FunctionDef.returns);
2230 if (!value) goto failed;
2231 if (PyObject_SetAttrString(result, "returns", value) == -1)
2232 goto failed;
2233 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 break;
2235 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002236 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2237 if (!result) goto failed;
2238 value = ast2obj_identifier(o->v.ClassDef.name);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "name", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
2243 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2244 if (!value) goto failed;
2245 if (PyObject_SetAttrString(result, "bases", value) == -1)
2246 goto failed;
2247 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002248 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2249 if (!value) goto failed;
2250 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2251 goto failed;
2252 Py_DECREF(value);
2253 value = ast2obj_expr(o->v.ClassDef.starargs);
2254 if (!value) goto failed;
2255 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2256 goto failed;
2257 Py_DECREF(value);
2258 value = ast2obj_expr(o->v.ClassDef.kwargs);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002263 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "body", value) == -1)
2266 goto failed;
2267 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002268 value = ast2obj_list(o->v.ClassDef.decorator_list,
2269 ast2obj_expr);
2270 if (!value) goto failed;
2271 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2272 -1)
2273 goto failed;
2274 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 break;
2276 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002277 result = PyType_GenericNew(Return_type, NULL, NULL);
2278 if (!result) goto failed;
2279 value = ast2obj_expr(o->v.Return.value);
2280 if (!value) goto failed;
2281 if (PyObject_SetAttrString(result, "value", value) == -1)
2282 goto failed;
2283 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 break;
2285 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002286 result = PyType_GenericNew(Delete_type, NULL, NULL);
2287 if (!result) goto failed;
2288 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2289 if (!value) goto failed;
2290 if (PyObject_SetAttrString(result, "targets", value) == -1)
2291 goto failed;
2292 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 break;
2294 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002295 result = PyType_GenericNew(Assign_type, NULL, NULL);
2296 if (!result) goto failed;
2297 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2298 if (!value) goto failed;
2299 if (PyObject_SetAttrString(result, "targets", value) == -1)
2300 goto failed;
2301 Py_DECREF(value);
2302 value = ast2obj_expr(o->v.Assign.value);
2303 if (!value) goto failed;
2304 if (PyObject_SetAttrString(result, "value", value) == -1)
2305 goto failed;
2306 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 break;
2308 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002309 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2310 if (!result) goto failed;
2311 value = ast2obj_expr(o->v.AugAssign.target);
2312 if (!value) goto failed;
2313 if (PyObject_SetAttrString(result, "target", value) == -1)
2314 goto failed;
2315 Py_DECREF(value);
2316 value = ast2obj_operator(o->v.AugAssign.op);
2317 if (!value) goto failed;
2318 if (PyObject_SetAttrString(result, "op", value) == -1)
2319 goto failed;
2320 Py_DECREF(value);
2321 value = ast2obj_expr(o->v.AugAssign.value);
2322 if (!value) goto failed;
2323 if (PyObject_SetAttrString(result, "value", value) == -1)
2324 goto failed;
2325 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002328 result = PyType_GenericNew(For_type, NULL, NULL);
2329 if (!result) goto failed;
2330 value = ast2obj_expr(o->v.For.target);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "target", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_expr(o->v.For.iter);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "iter", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
2340 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2341 if (!value) goto failed;
2342 if (PyObject_SetAttrString(result, "body", value) == -1)
2343 goto failed;
2344 Py_DECREF(value);
2345 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2346 if (!value) goto failed;
2347 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2348 goto failed;
2349 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 break;
2351 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002352 result = PyType_GenericNew(While_type, NULL, NULL);
2353 if (!result) goto failed;
2354 value = ast2obj_expr(o->v.While.test);
2355 if (!value) goto failed;
2356 if (PyObject_SetAttrString(result, "test", value) == -1)
2357 goto failed;
2358 Py_DECREF(value);
2359 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "body", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
2364 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2365 if (!value) goto failed;
2366 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2367 goto failed;
2368 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369 break;
2370 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002371 result = PyType_GenericNew(If_type, NULL, NULL);
2372 if (!result) goto failed;
2373 value = ast2obj_expr(o->v.If.test);
2374 if (!value) goto failed;
2375 if (PyObject_SetAttrString(result, "test", value) == -1)
2376 goto failed;
2377 Py_DECREF(value);
2378 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2379 if (!value) goto failed;
2380 if (PyObject_SetAttrString(result, "body", value) == -1)
2381 goto failed;
2382 Py_DECREF(value);
2383 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2384 if (!value) goto failed;
2385 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2386 goto failed;
2387 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002389 case With_kind:
2390 result = PyType_GenericNew(With_type, NULL, NULL);
2391 if (!result) goto failed;
2392 value = ast2obj_expr(o->v.With.context_expr);
2393 if (!value) goto failed;
2394 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2395 goto failed;
2396 Py_DECREF(value);
2397 value = ast2obj_expr(o->v.With.optional_vars);
2398 if (!value) goto failed;
2399 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2400 -1)
2401 goto failed;
2402 Py_DECREF(value);
2403 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2404 if (!value) goto failed;
2405 if (PyObject_SetAttrString(result, "body", value) == -1)
2406 goto failed;
2407 Py_DECREF(value);
2408 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002410 result = PyType_GenericNew(Raise_type, NULL, NULL);
2411 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002412 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002413 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002414 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002415 goto failed;
2416 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002417 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002418 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002419 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002420 goto failed;
2421 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 break;
2423 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002424 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2425 if (!result) goto failed;
2426 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2427 if (!value) goto failed;
2428 if (PyObject_SetAttrString(result, "body", value) == -1)
2429 goto failed;
2430 Py_DECREF(value);
2431 value = ast2obj_list(o->v.TryExcept.handlers,
2432 ast2obj_excepthandler);
2433 if (!value) goto failed;
2434 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2435 goto failed;
2436 Py_DECREF(value);
2437 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2438 if (!value) goto failed;
2439 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2440 goto failed;
2441 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 break;
2443 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002444 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2445 if (!result) goto failed;
2446 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2447 if (!value) goto failed;
2448 if (PyObject_SetAttrString(result, "body", value) == -1)
2449 goto failed;
2450 Py_DECREF(value);
2451 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2452 if (!value) goto failed;
2453 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2454 goto failed;
2455 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 break;
2457 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002458 result = PyType_GenericNew(Assert_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_expr(o->v.Assert.test);
2461 if (!value) goto failed;
2462 if (PyObject_SetAttrString(result, "test", value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 value = ast2obj_expr(o->v.Assert.msg);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "msg", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 break;
2471 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002472 result = PyType_GenericNew(Import_type, NULL, NULL);
2473 if (!result) goto failed;
2474 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2475 if (!value) goto failed;
2476 if (PyObject_SetAttrString(result, "names", value) == -1)
2477 goto failed;
2478 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 break;
2480 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002481 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2482 if (!result) goto failed;
2483 value = ast2obj_identifier(o->v.ImportFrom.module);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "module", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
2488 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "names", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002493 value = ast2obj_int(o->v.ImportFrom.level);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "level", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002500 result = PyType_GenericNew(Global_type, NULL, NULL);
2501 if (!result) goto failed;
2502 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "names", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002508 case Nonlocal_kind:
2509 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2510 if (!result) goto failed;
2511 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "names", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 result = PyType_GenericNew(Expr_type, NULL, NULL);
2519 if (!result) goto failed;
2520 value = ast2obj_expr(o->v.Expr.value);
2521 if (!value) goto failed;
2522 if (PyObject_SetAttrString(result, "value", value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 break;
2526 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002527 result = PyType_GenericNew(Pass_type, NULL, NULL);
2528 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 break;
2530 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002531 result = PyType_GenericNew(Break_type, NULL, NULL);
2532 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 break;
2534 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002535 result = PyType_GenericNew(Continue_type, NULL, NULL);
2536 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 break;
2538 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002539 value = ast2obj_int(o->lineno);
2540 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002541 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2542 goto failed;
2543 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002544 value = ast2obj_int(o->col_offset);
2545 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002546 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2547 goto failed;
2548 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002549 return result;
2550failed:
2551 Py_XDECREF(value);
2552 Py_XDECREF(result);
2553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554}
2555
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002556PyObject*
2557ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002559 expr_ty o = (expr_ty)_o;
2560 PyObject *result = NULL, *value = NULL;
2561 if (!o) {
2562 Py_INCREF(Py_None);
2563 return Py_None;
2564 }
2565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 switch (o->kind) {
2567 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002568 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2569 if (!result) goto failed;
2570 value = ast2obj_boolop(o->v.BoolOp.op);
2571 if (!value) goto failed;
2572 if (PyObject_SetAttrString(result, "op", value) == -1)
2573 goto failed;
2574 Py_DECREF(value);
2575 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2576 if (!value) goto failed;
2577 if (PyObject_SetAttrString(result, "values", value) == -1)
2578 goto failed;
2579 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002580 break;
2581 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002582 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2583 if (!result) goto failed;
2584 value = ast2obj_expr(o->v.BinOp.left);
2585 if (!value) goto failed;
2586 if (PyObject_SetAttrString(result, "left", value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
2589 value = ast2obj_operator(o->v.BinOp.op);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "op", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 value = ast2obj_expr(o->v.BinOp.right);
2595 if (!value) goto failed;
2596 if (PyObject_SetAttrString(result, "right", value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 break;
2600 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002601 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2602 if (!result) goto failed;
2603 value = ast2obj_unaryop(o->v.UnaryOp.op);
2604 if (!value) goto failed;
2605 if (PyObject_SetAttrString(result, "op", value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 value = ast2obj_expr(o->v.UnaryOp.operand);
2609 if (!value) goto failed;
2610 if (PyObject_SetAttrString(result, "operand", value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 break;
2614 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002615 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2616 if (!result) goto failed;
2617 value = ast2obj_arguments(o->v.Lambda.args);
2618 if (!value) goto failed;
2619 if (PyObject_SetAttrString(result, "args", value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 value = ast2obj_expr(o->v.Lambda.body);
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "body", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002628 case IfExp_kind:
2629 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2630 if (!result) goto failed;
2631 value = ast2obj_expr(o->v.IfExp.test);
2632 if (!value) goto failed;
2633 if (PyObject_SetAttrString(result, "test", value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
2636 value = ast2obj_expr(o->v.IfExp.body);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "body", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
2641 value = ast2obj_expr(o->v.IfExp.orelse);
2642 if (!value) goto failed;
2643 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002648 result = PyType_GenericNew(Dict_type, NULL, NULL);
2649 if (!result) goto failed;
2650 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2651 if (!value) goto failed;
2652 if (PyObject_SetAttrString(result, "keys", value) == -1)
2653 goto failed;
2654 Py_DECREF(value);
2655 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2656 if (!value) goto failed;
2657 if (PyObject_SetAttrString(result, "values", value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002661 case Set_kind:
2662 result = PyType_GenericNew(Set_type, NULL, NULL);
2663 if (!result) goto failed;
2664 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "elts", value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002671 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2672 if (!result) goto failed;
2673 value = ast2obj_expr(o->v.ListComp.elt);
2674 if (!value) goto failed;
2675 if (PyObject_SetAttrString(result, "elt", value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
2678 value = ast2obj_list(o->v.ListComp.generators,
2679 ast2obj_comprehension);
2680 if (!value) goto failed;
2681 if (PyObject_SetAttrString(result, "generators", value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002685 case SetComp_kind:
2686 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2687 if (!result) goto failed;
2688 value = ast2obj_expr(o->v.SetComp.elt);
2689 if (!value) goto failed;
2690 if (PyObject_SetAttrString(result, "elt", value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_list(o->v.SetComp.generators,
2694 ast2obj_comprehension);
2695 if (!value) goto failed;
2696 if (PyObject_SetAttrString(result, "generators", value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002700 case DictComp_kind:
2701 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2702 if (!result) goto failed;
2703 value = ast2obj_expr(o->v.DictComp.key);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "key", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 value = ast2obj_expr(o->v.DictComp.value);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "value", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
2713 value = ast2obj_list(o->v.DictComp.generators,
2714 ast2obj_comprehension);
2715 if (!value) goto failed;
2716 if (PyObject_SetAttrString(result, "generators", value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
2719 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002721 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2722 if (!result) goto failed;
2723 value = ast2obj_expr(o->v.GeneratorExp.elt);
2724 if (!value) goto failed;
2725 if (PyObject_SetAttrString(result, "elt", value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_list(o->v.GeneratorExp.generators,
2729 ast2obj_comprehension);
2730 if (!value) goto failed;
2731 if (PyObject_SetAttrString(result, "generators", value) == -1)
2732 goto failed;
2733 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 break;
2735 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002736 result = PyType_GenericNew(Yield_type, NULL, NULL);
2737 if (!result) goto failed;
2738 value = ast2obj_expr(o->v.Yield.value);
2739 if (!value) goto failed;
2740 if (PyObject_SetAttrString(result, "value", value) == -1)
2741 goto failed;
2742 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 break;
2744 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002745 result = PyType_GenericNew(Compare_type, NULL, NULL);
2746 if (!result) goto failed;
2747 value = ast2obj_expr(o->v.Compare.left);
2748 if (!value) goto failed;
2749 if (PyObject_SetAttrString(result, "left", value) == -1)
2750 goto failed;
2751 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002752 {
2753 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2754 value = PyList_New(n);
2755 if (!value) goto failed;
2756 for(i = 0; i < n; i++)
2757 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2758 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002759 if (!value) goto failed;
2760 if (PyObject_SetAttrString(result, "ops", value) == -1)
2761 goto failed;
2762 Py_DECREF(value);
2763 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 break;
2769 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002770 result = PyType_GenericNew(Call_type, NULL, NULL);
2771 if (!result) goto failed;
2772 value = ast2obj_expr(o->v.Call.func);
2773 if (!value) goto failed;
2774 if (PyObject_SetAttrString(result, "func", value) == -1)
2775 goto failed;
2776 Py_DECREF(value);
2777 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2778 if (!value) goto failed;
2779 if (PyObject_SetAttrString(result, "args", value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
2782 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2783 if (!value) goto failed;
2784 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 value = ast2obj_expr(o->v.Call.starargs);
2788 if (!value) goto failed;
2789 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 value = ast2obj_expr(o->v.Call.kwargs);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002799 result = PyType_GenericNew(Num_type, NULL, NULL);
2800 if (!result) goto failed;
2801 value = ast2obj_object(o->v.Num.n);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "n", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 break;
2807 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002808 result = PyType_GenericNew(Str_type, NULL, NULL);
2809 if (!result) goto failed;
2810 value = ast2obj_string(o->v.Str.s);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "s", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002816 case Bytes_kind:
2817 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_string(o->v.Bytes.s);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "s", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002825 case Ellipsis_kind:
2826 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2827 if (!result) goto failed;
2828 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002830 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_expr(o->v.Attribute.value);
2833 if (!value) goto failed;
2834 if (PyObject_SetAttrString(result, "value", value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_identifier(o->v.Attribute.attr);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "attr", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_expr_context(o->v.Attribute.ctx);
2843 if (!value) goto failed;
2844 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 break;
2848 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002849 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2850 if (!result) goto failed;
2851 value = ast2obj_expr(o->v.Subscript.value);
2852 if (!value) goto failed;
2853 if (PyObject_SetAttrString(result, "value", value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 value = ast2obj_slice(o->v.Subscript.slice);
2857 if (!value) goto failed;
2858 if (PyObject_SetAttrString(result, "slice", value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_expr_context(o->v.Subscript.ctx);
2862 if (!value) goto failed;
2863 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002867 case Starred_kind:
2868 result = PyType_GenericNew(Starred_type, NULL, NULL);
2869 if (!result) goto failed;
2870 value = ast2obj_expr(o->v.Starred.value);
2871 if (!value) goto failed;
2872 if (PyObject_SetAttrString(result, "value", value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 value = ast2obj_expr_context(o->v.Starred.ctx);
2876 if (!value) goto failed;
2877 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002882 result = PyType_GenericNew(Name_type, NULL, NULL);
2883 if (!result) goto failed;
2884 value = ast2obj_identifier(o->v.Name.id);
2885 if (!value) goto failed;
2886 if (PyObject_SetAttrString(result, "id", value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_expr_context(o->v.Name.ctx);
2890 if (!value) goto failed;
2891 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 break;
2895 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002896 result = PyType_GenericNew(List_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2899 if (!value) goto failed;
2900 if (PyObject_SetAttrString(result, "elts", value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_expr_context(o->v.List.ctx);
2904 if (!value) goto failed;
2905 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 break;
2909 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002910 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2913 if (!value) goto failed;
2914 if (PyObject_SetAttrString(result, "elts", value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 value = ast2obj_expr_context(o->v.Tuple.ctx);
2918 if (!value) goto failed;
2919 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 break;
2923 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002924 value = ast2obj_int(o->lineno);
2925 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002926 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2927 goto failed;
2928 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002929 value = ast2obj_int(o->col_offset);
2930 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002931 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2932 goto failed;
2933 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002934 return result;
2935failed:
2936 Py_XDECREF(value);
2937 Py_XDECREF(result);
2938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939}
2940
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002943 switch(o) {
2944 case Load:
2945 Py_INCREF(Load_singleton);
2946 return Load_singleton;
2947 case Store:
2948 Py_INCREF(Store_singleton);
2949 return Store_singleton;
2950 case Del:
2951 Py_INCREF(Del_singleton);
2952 return Del_singleton;
2953 case AugLoad:
2954 Py_INCREF(AugLoad_singleton);
2955 return AugLoad_singleton;
2956 case AugStore:
2957 Py_INCREF(AugStore_singleton);
2958 return AugStore_singleton;
2959 case Param:
2960 Py_INCREF(Param_singleton);
2961 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002962 default:
2963 /* should never happen, but just in case ... */
2964 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002967}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002968PyObject*
2969ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002971 slice_ty o = (slice_ty)_o;
2972 PyObject *result = NULL, *value = NULL;
2973 if (!o) {
2974 Py_INCREF(Py_None);
2975 return Py_None;
2976 }
2977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002980 result = PyType_GenericNew(Slice_type, NULL, NULL);
2981 if (!result) goto failed;
2982 value = ast2obj_expr(o->v.Slice.lower);
2983 if (!value) goto failed;
2984 if (PyObject_SetAttrString(result, "lower", value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 value = ast2obj_expr(o->v.Slice.upper);
2988 if (!value) goto failed;
2989 if (PyObject_SetAttrString(result, "upper", value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_expr(o->v.Slice.step);
2993 if (!value) goto failed;
2994 if (PyObject_SetAttrString(result, "step", value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 break;
2998 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002999 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3000 if (!result) goto failed;
3001 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3002 if (!value) goto failed;
3003 if (PyObject_SetAttrString(result, "dims", value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 break;
3007 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003008 result = PyType_GenericNew(Index_type, NULL, NULL);
3009 if (!result) goto failed;
3010 value = ast2obj_expr(o->v.Index.value);
3011 if (!value) goto failed;
3012 if (PyObject_SetAttrString(result, "value", value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 break;
3016 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003017 return result;
3018failed:
3019 Py_XDECREF(value);
3020 Py_XDECREF(result);
3021 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022}
3023
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003024PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003026 switch(o) {
3027 case And:
3028 Py_INCREF(And_singleton);
3029 return And_singleton;
3030 case Or:
3031 Py_INCREF(Or_singleton);
3032 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003033 default:
3034 /* should never happen, but just in case ... */
3035 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003038}
3039PyObject* ast2obj_operator(operator_ty o)
3040{
3041 switch(o) {
3042 case Add:
3043 Py_INCREF(Add_singleton);
3044 return Add_singleton;
3045 case Sub:
3046 Py_INCREF(Sub_singleton);
3047 return Sub_singleton;
3048 case Mult:
3049 Py_INCREF(Mult_singleton);
3050 return Mult_singleton;
3051 case Div:
3052 Py_INCREF(Div_singleton);
3053 return Div_singleton;
3054 case Mod:
3055 Py_INCREF(Mod_singleton);
3056 return Mod_singleton;
3057 case Pow:
3058 Py_INCREF(Pow_singleton);
3059 return Pow_singleton;
3060 case LShift:
3061 Py_INCREF(LShift_singleton);
3062 return LShift_singleton;
3063 case RShift:
3064 Py_INCREF(RShift_singleton);
3065 return RShift_singleton;
3066 case BitOr:
3067 Py_INCREF(BitOr_singleton);
3068 return BitOr_singleton;
3069 case BitXor:
3070 Py_INCREF(BitXor_singleton);
3071 return BitXor_singleton;
3072 case BitAnd:
3073 Py_INCREF(BitAnd_singleton);
3074 return BitAnd_singleton;
3075 case FloorDiv:
3076 Py_INCREF(FloorDiv_singleton);
3077 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003078 default:
3079 /* should never happen, but just in case ... */
3080 PyErr_Format(PyExc_SystemError, "unknown operator found");
3081 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003082 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003083}
3084PyObject* ast2obj_unaryop(unaryop_ty o)
3085{
3086 switch(o) {
3087 case Invert:
3088 Py_INCREF(Invert_singleton);
3089 return Invert_singleton;
3090 case Not:
3091 Py_INCREF(Not_singleton);
3092 return Not_singleton;
3093 case UAdd:
3094 Py_INCREF(UAdd_singleton);
3095 return UAdd_singleton;
3096 case USub:
3097 Py_INCREF(USub_singleton);
3098 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003099 default:
3100 /* should never happen, but just in case ... */
3101 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3102 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003103 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003104}
3105PyObject* ast2obj_cmpop(cmpop_ty o)
3106{
3107 switch(o) {
3108 case Eq:
3109 Py_INCREF(Eq_singleton);
3110 return Eq_singleton;
3111 case NotEq:
3112 Py_INCREF(NotEq_singleton);
3113 return NotEq_singleton;
3114 case Lt:
3115 Py_INCREF(Lt_singleton);
3116 return Lt_singleton;
3117 case LtE:
3118 Py_INCREF(LtE_singleton);
3119 return LtE_singleton;
3120 case Gt:
3121 Py_INCREF(Gt_singleton);
3122 return Gt_singleton;
3123 case GtE:
3124 Py_INCREF(GtE_singleton);
3125 return GtE_singleton;
3126 case Is:
3127 Py_INCREF(Is_singleton);
3128 return Is_singleton;
3129 case IsNot:
3130 Py_INCREF(IsNot_singleton);
3131 return IsNot_singleton;
3132 case In:
3133 Py_INCREF(In_singleton);
3134 return In_singleton;
3135 case NotIn:
3136 Py_INCREF(NotIn_singleton);
3137 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003138 default:
3139 /* should never happen, but just in case ... */
3140 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3141 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003142 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003143}
3144PyObject*
3145ast2obj_comprehension(void* _o)
3146{
3147 comprehension_ty o = (comprehension_ty)_o;
3148 PyObject *result = NULL, *value = NULL;
3149 if (!o) {
3150 Py_INCREF(Py_None);
3151 return Py_None;
3152 }
3153
3154 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3155 if (!result) return NULL;
3156 value = ast2obj_expr(o->target);
3157 if (!value) goto failed;
3158 if (PyObject_SetAttrString(result, "target", value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 value = ast2obj_expr(o->iter);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "iter", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 value = ast2obj_list(o->ifs, ast2obj_expr);
3167 if (!value) goto failed;
3168 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3169 goto failed;
3170 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003171 return result;
3172failed:
3173 Py_XDECREF(value);
3174 Py_XDECREF(result);
3175 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176}
3177
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003178PyObject*
3179ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003181 excepthandler_ty o = (excepthandler_ty)_o;
3182 PyObject *result = NULL, *value = NULL;
3183 if (!o) {
3184 Py_INCREF(Py_None);
3185 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003187
Neal Norwitzad74aa82008-03-31 05:14:30 +00003188 switch (o->kind) {
3189 case ExceptHandler_kind:
3190 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3191 if (!result) goto failed;
3192 value = ast2obj_expr(o->v.ExceptHandler.type);
3193 if (!value) goto failed;
3194 if (PyObject_SetAttrString(result, "type", value) == -1)
3195 goto failed;
3196 Py_DECREF(value);
3197 value = ast2obj_identifier(o->v.ExceptHandler.name);
3198 if (!value) goto failed;
3199 if (PyObject_SetAttrString(result, "name", value) == -1)
3200 goto failed;
3201 Py_DECREF(value);
3202 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3203 if (!value) goto failed;
3204 if (PyObject_SetAttrString(result, "body", value) == -1)
3205 goto failed;
3206 Py_DECREF(value);
3207 break;
3208 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003209 value = ast2obj_int(o->lineno);
3210 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003211 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003212 goto failed;
3213 Py_DECREF(value);
3214 value = ast2obj_int(o->col_offset);
3215 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003216 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003217 goto failed;
3218 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003219 return result;
3220failed:
3221 Py_XDECREF(value);
3222 Py_XDECREF(result);
3223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224}
3225
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003226PyObject*
3227ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003229 arguments_ty o = (arguments_ty)_o;
3230 PyObject *result = NULL, *value = NULL;
3231 if (!o) {
3232 Py_INCREF(Py_None);
3233 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003235
3236 result = PyType_GenericNew(arguments_type, NULL, NULL);
3237 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003238 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003239 if (!value) goto failed;
3240 if (PyObject_SetAttrString(result, "args", value) == -1)
3241 goto failed;
3242 Py_DECREF(value);
3243 value = ast2obj_identifier(o->vararg);
3244 if (!value) goto failed;
3245 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3246 goto failed;
3247 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003248 value = ast2obj_expr(o->varargannotation);
3249 if (!value) goto failed;
3250 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
3253 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003254 if (!value) goto failed;
3255 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3256 goto failed;
3257 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003258 value = ast2obj_identifier(o->kwarg);
3259 if (!value) goto failed;
3260 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3261 goto failed;
3262 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003263 value = ast2obj_expr(o->kwargannotation);
3264 if (!value) goto failed;
3265 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3266 goto failed;
3267 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003268 value = ast2obj_list(o->defaults, ast2obj_expr);
3269 if (!value) goto failed;
3270 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3271 goto failed;
3272 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003273 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3274 if (!value) goto failed;
3275 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3276 goto failed;
3277 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003278 return result;
3279failed:
3280 Py_XDECREF(value);
3281 Py_XDECREF(result);
3282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283}
3284
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003285PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003286ast2obj_arg(void* _o)
3287{
3288 arg_ty o = (arg_ty)_o;
3289 PyObject *result = NULL, *value = NULL;
3290 if (!o) {
3291 Py_INCREF(Py_None);
3292 return Py_None;
3293 }
3294
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003295 result = PyType_GenericNew(arg_type, NULL, NULL);
3296 if (!result) return NULL;
3297 value = ast2obj_identifier(o->arg);
3298 if (!value) goto failed;
3299 if (PyObject_SetAttrString(result, "arg", value) == -1)
3300 goto failed;
3301 Py_DECREF(value);
3302 value = ast2obj_expr(o->annotation);
3303 if (!value) goto failed;
3304 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3305 goto failed;
3306 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003307 return result;
3308failed:
3309 Py_XDECREF(value);
3310 Py_XDECREF(result);
3311 return NULL;
3312}
3313
3314PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003315ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003317 keyword_ty o = (keyword_ty)_o;
3318 PyObject *result = NULL, *value = NULL;
3319 if (!o) {
3320 Py_INCREF(Py_None);
3321 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003323
3324 result = PyType_GenericNew(keyword_type, NULL, NULL);
3325 if (!result) return NULL;
3326 value = ast2obj_identifier(o->arg);
3327 if (!value) goto failed;
3328 if (PyObject_SetAttrString(result, "arg", value) == -1)
3329 goto failed;
3330 Py_DECREF(value);
3331 value = ast2obj_expr(o->value);
3332 if (!value) goto failed;
3333 if (PyObject_SetAttrString(result, "value", value) == -1)
3334 goto failed;
3335 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003336 return result;
3337failed:
3338 Py_XDECREF(value);
3339 Py_XDECREF(result);
3340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003343PyObject*
3344ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003346 alias_ty o = (alias_ty)_o;
3347 PyObject *result = NULL, *value = NULL;
3348 if (!o) {
3349 Py_INCREF(Py_None);
3350 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003352
3353 result = PyType_GenericNew(alias_type, NULL, NULL);
3354 if (!result) return NULL;
3355 value = ast2obj_identifier(o->name);
3356 if (!value) goto failed;
3357 if (PyObject_SetAttrString(result, "name", value) == -1)
3358 goto failed;
3359 Py_DECREF(value);
3360 value = ast2obj_identifier(o->asname);
3361 if (!value) goto failed;
3362 if (PyObject_SetAttrString(result, "asname", value) == -1)
3363 goto failed;
3364 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003365 return result;
3366failed:
3367 Py_XDECREF(value);
3368 Py_XDECREF(result);
3369 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370}
3371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003373int
3374obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3375{
3376 PyObject* tmp = NULL;
3377
3378
3379 if (obj == Py_None) {
3380 *out = NULL;
3381 return 0;
3382 }
3383 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3384 asdl_seq* body;
3385
3386 if (PyObject_HasAttrString(obj, "body")) {
3387 int res;
3388 Py_ssize_t len;
3389 Py_ssize_t i;
3390 tmp = PyObject_GetAttrString(obj, "body");
3391 if (tmp == NULL) goto failed;
3392 if (!PyList_Check(tmp)) {
3393 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3394 goto failed;
3395 }
3396 len = PyList_GET_SIZE(tmp);
3397 body = asdl_seq_new(len, arena);
3398 if (body == NULL) goto failed;
3399 for (i = 0; i < len; i++) {
3400 stmt_ty value;
3401 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3402 if (res != 0) goto failed;
3403 asdl_seq_SET(body, i, value);
3404 }
3405 Py_XDECREF(tmp);
3406 tmp = NULL;
3407 } else {
3408 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3409 return 1;
3410 }
3411 *out = Module(body, arena);
3412 if (*out == NULL) goto failed;
3413 return 0;
3414 }
3415 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3416 asdl_seq* body;
3417
3418 if (PyObject_HasAttrString(obj, "body")) {
3419 int res;
3420 Py_ssize_t len;
3421 Py_ssize_t i;
3422 tmp = PyObject_GetAttrString(obj, "body");
3423 if (tmp == NULL) goto failed;
3424 if (!PyList_Check(tmp)) {
3425 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3426 goto failed;
3427 }
3428 len = PyList_GET_SIZE(tmp);
3429 body = asdl_seq_new(len, arena);
3430 if (body == NULL) goto failed;
3431 for (i = 0; i < len; i++) {
3432 stmt_ty value;
3433 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3434 if (res != 0) goto failed;
3435 asdl_seq_SET(body, i, value);
3436 }
3437 Py_XDECREF(tmp);
3438 tmp = NULL;
3439 } else {
3440 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3441 return 1;
3442 }
3443 *out = Interactive(body, arena);
3444 if (*out == NULL) goto failed;
3445 return 0;
3446 }
3447 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3448 expr_ty body;
3449
3450 if (PyObject_HasAttrString(obj, "body")) {
3451 int res;
3452 tmp = PyObject_GetAttrString(obj, "body");
3453 if (tmp == NULL) goto failed;
3454 res = obj2ast_expr(tmp, &body, arena);
3455 if (res != 0) goto failed;
3456 Py_XDECREF(tmp);
3457 tmp = NULL;
3458 } else {
3459 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3460 return 1;
3461 }
3462 *out = Expression(body, arena);
3463 if (*out == NULL) goto failed;
3464 return 0;
3465 }
3466 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3467 asdl_seq* body;
3468
3469 if (PyObject_HasAttrString(obj, "body")) {
3470 int res;
3471 Py_ssize_t len;
3472 Py_ssize_t i;
3473 tmp = PyObject_GetAttrString(obj, "body");
3474 if (tmp == NULL) goto failed;
3475 if (!PyList_Check(tmp)) {
3476 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3477 goto failed;
3478 }
3479 len = PyList_GET_SIZE(tmp);
3480 body = asdl_seq_new(len, arena);
3481 if (body == NULL) goto failed;
3482 for (i = 0; i < len; i++) {
3483 stmt_ty value;
3484 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3485 if (res != 0) goto failed;
3486 asdl_seq_SET(body, i, value);
3487 }
3488 Py_XDECREF(tmp);
3489 tmp = NULL;
3490 } else {
3491 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3492 return 1;
3493 }
3494 *out = Suite(body, arena);
3495 if (*out == NULL) goto failed;
3496 return 0;
3497 }
3498
3499 tmp = PyObject_Repr(obj);
3500 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00003501 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003502failed:
3503 Py_XDECREF(tmp);
3504 return 1;
3505}
3506
3507int
3508obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3509{
3510 PyObject* tmp = NULL;
3511
3512 int lineno;
3513 int col_offset;
3514
3515 if (obj == Py_None) {
3516 *out = NULL;
3517 return 0;
3518 }
3519 if (PyObject_HasAttrString(obj, "lineno")) {
3520 int res;
3521 tmp = PyObject_GetAttrString(obj, "lineno");
3522 if (tmp == NULL) goto failed;
3523 res = obj2ast_int(tmp, &lineno, arena);
3524 if (res != 0) goto failed;
3525 Py_XDECREF(tmp);
3526 tmp = NULL;
3527 } else {
3528 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3529 return 1;
3530 }
3531 if (PyObject_HasAttrString(obj, "col_offset")) {
3532 int res;
3533 tmp = PyObject_GetAttrString(obj, "col_offset");
3534 if (tmp == NULL) goto failed;
3535 res = obj2ast_int(tmp, &col_offset, arena);
3536 if (res != 0) goto failed;
3537 Py_XDECREF(tmp);
3538 tmp = NULL;
3539 } else {
3540 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3541 return 1;
3542 }
3543 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3544 identifier name;
3545 arguments_ty args;
3546 asdl_seq* body;
3547 asdl_seq* decorator_list;
3548 expr_ty returns;
3549
3550 if (PyObject_HasAttrString(obj, "name")) {
3551 int res;
3552 tmp = PyObject_GetAttrString(obj, "name");
3553 if (tmp == NULL) goto failed;
3554 res = obj2ast_identifier(tmp, &name, arena);
3555 if (res != 0) goto failed;
3556 Py_XDECREF(tmp);
3557 tmp = NULL;
3558 } else {
3559 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3560 return 1;
3561 }
3562 if (PyObject_HasAttrString(obj, "args")) {
3563 int res;
3564 tmp = PyObject_GetAttrString(obj, "args");
3565 if (tmp == NULL) goto failed;
3566 res = obj2ast_arguments(tmp, &args, arena);
3567 if (res != 0) goto failed;
3568 Py_XDECREF(tmp);
3569 tmp = NULL;
3570 } else {
3571 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3572 return 1;
3573 }
3574 if (PyObject_HasAttrString(obj, "body")) {
3575 int res;
3576 Py_ssize_t len;
3577 Py_ssize_t i;
3578 tmp = PyObject_GetAttrString(obj, "body");
3579 if (tmp == NULL) goto failed;
3580 if (!PyList_Check(tmp)) {
3581 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3582 goto failed;
3583 }
3584 len = PyList_GET_SIZE(tmp);
3585 body = asdl_seq_new(len, arena);
3586 if (body == NULL) goto failed;
3587 for (i = 0; i < len; i++) {
3588 stmt_ty value;
3589 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3590 if (res != 0) goto failed;
3591 asdl_seq_SET(body, i, value);
3592 }
3593 Py_XDECREF(tmp);
3594 tmp = NULL;
3595 } else {
3596 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3597 return 1;
3598 }
3599 if (PyObject_HasAttrString(obj, "decorator_list")) {
3600 int res;
3601 Py_ssize_t len;
3602 Py_ssize_t i;
3603 tmp = PyObject_GetAttrString(obj, "decorator_list");
3604 if (tmp == NULL) goto failed;
3605 if (!PyList_Check(tmp)) {
3606 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3607 goto failed;
3608 }
3609 len = PyList_GET_SIZE(tmp);
3610 decorator_list = asdl_seq_new(len, arena);
3611 if (decorator_list == NULL) goto failed;
3612 for (i = 0; i < len; i++) {
3613 expr_ty value;
3614 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3615 if (res != 0) goto failed;
3616 asdl_seq_SET(decorator_list, i, value);
3617 }
3618 Py_XDECREF(tmp);
3619 tmp = NULL;
3620 } else {
3621 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3622 return 1;
3623 }
3624 if (PyObject_HasAttrString(obj, "returns")) {
3625 int res;
3626 tmp = PyObject_GetAttrString(obj, "returns");
3627 if (tmp == NULL) goto failed;
3628 res = obj2ast_expr(tmp, &returns, arena);
3629 if (res != 0) goto failed;
3630 Py_XDECREF(tmp);
3631 tmp = NULL;
3632 } else {
3633 returns = NULL;
3634 }
3635 *out = FunctionDef(name, args, body, decorator_list, returns,
3636 lineno, col_offset, arena);
3637 if (*out == NULL) goto failed;
3638 return 0;
3639 }
3640 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3641 identifier name;
3642 asdl_seq* bases;
3643 asdl_seq* keywords;
3644 expr_ty starargs;
3645 expr_ty kwargs;
3646 asdl_seq* body;
3647 asdl_seq* decorator_list;
3648
3649 if (PyObject_HasAttrString(obj, "name")) {
3650 int res;
3651 tmp = PyObject_GetAttrString(obj, "name");
3652 if (tmp == NULL) goto failed;
3653 res = obj2ast_identifier(tmp, &name, arena);
3654 if (res != 0) goto failed;
3655 Py_XDECREF(tmp);
3656 tmp = NULL;
3657 } else {
3658 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3659 return 1;
3660 }
3661 if (PyObject_HasAttrString(obj, "bases")) {
3662 int res;
3663 Py_ssize_t len;
3664 Py_ssize_t i;
3665 tmp = PyObject_GetAttrString(obj, "bases");
3666 if (tmp == NULL) goto failed;
3667 if (!PyList_Check(tmp)) {
3668 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3669 goto failed;
3670 }
3671 len = PyList_GET_SIZE(tmp);
3672 bases = asdl_seq_new(len, arena);
3673 if (bases == NULL) goto failed;
3674 for (i = 0; i < len; i++) {
3675 expr_ty value;
3676 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3677 if (res != 0) goto failed;
3678 asdl_seq_SET(bases, i, value);
3679 }
3680 Py_XDECREF(tmp);
3681 tmp = NULL;
3682 } else {
3683 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3684 return 1;
3685 }
3686 if (PyObject_HasAttrString(obj, "keywords")) {
3687 int res;
3688 Py_ssize_t len;
3689 Py_ssize_t i;
3690 tmp = PyObject_GetAttrString(obj, "keywords");
3691 if (tmp == NULL) goto failed;
3692 if (!PyList_Check(tmp)) {
3693 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3694 goto failed;
3695 }
3696 len = PyList_GET_SIZE(tmp);
3697 keywords = asdl_seq_new(len, arena);
3698 if (keywords == NULL) goto failed;
3699 for (i = 0; i < len; i++) {
3700 keyword_ty value;
3701 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3702 if (res != 0) goto failed;
3703 asdl_seq_SET(keywords, i, value);
3704 }
3705 Py_XDECREF(tmp);
3706 tmp = NULL;
3707 } else {
3708 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3709 return 1;
3710 }
3711 if (PyObject_HasAttrString(obj, "starargs")) {
3712 int res;
3713 tmp = PyObject_GetAttrString(obj, "starargs");
3714 if (tmp == NULL) goto failed;
3715 res = obj2ast_expr(tmp, &starargs, arena);
3716 if (res != 0) goto failed;
3717 Py_XDECREF(tmp);
3718 tmp = NULL;
3719 } else {
3720 starargs = NULL;
3721 }
3722 if (PyObject_HasAttrString(obj, "kwargs")) {
3723 int res;
3724 tmp = PyObject_GetAttrString(obj, "kwargs");
3725 if (tmp == NULL) goto failed;
3726 res = obj2ast_expr(tmp, &kwargs, arena);
3727 if (res != 0) goto failed;
3728 Py_XDECREF(tmp);
3729 tmp = NULL;
3730 } else {
3731 kwargs = NULL;
3732 }
3733 if (PyObject_HasAttrString(obj, "body")) {
3734 int res;
3735 Py_ssize_t len;
3736 Py_ssize_t i;
3737 tmp = PyObject_GetAttrString(obj, "body");
3738 if (tmp == NULL) goto failed;
3739 if (!PyList_Check(tmp)) {
3740 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3741 goto failed;
3742 }
3743 len = PyList_GET_SIZE(tmp);
3744 body = asdl_seq_new(len, arena);
3745 if (body == NULL) goto failed;
3746 for (i = 0; i < len; i++) {
3747 stmt_ty value;
3748 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3749 if (res != 0) goto failed;
3750 asdl_seq_SET(body, i, value);
3751 }
3752 Py_XDECREF(tmp);
3753 tmp = NULL;
3754 } else {
3755 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3756 return 1;
3757 }
3758 if (PyObject_HasAttrString(obj, "decorator_list")) {
3759 int res;
3760 Py_ssize_t len;
3761 Py_ssize_t i;
3762 tmp = PyObject_GetAttrString(obj, "decorator_list");
3763 if (tmp == NULL) goto failed;
3764 if (!PyList_Check(tmp)) {
3765 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3766 goto failed;
3767 }
3768 len = PyList_GET_SIZE(tmp);
3769 decorator_list = asdl_seq_new(len, arena);
3770 if (decorator_list == NULL) goto failed;
3771 for (i = 0; i < len; i++) {
3772 expr_ty value;
3773 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3774 if (res != 0) goto failed;
3775 asdl_seq_SET(decorator_list, i, value);
3776 }
3777 Py_XDECREF(tmp);
3778 tmp = NULL;
3779 } else {
3780 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3781 return 1;
3782 }
3783 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3784 decorator_list, lineno, col_offset, arena);
3785 if (*out == NULL) goto failed;
3786 return 0;
3787 }
3788 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3789 expr_ty value;
3790
3791 if (PyObject_HasAttrString(obj, "value")) {
3792 int res;
3793 tmp = PyObject_GetAttrString(obj, "value");
3794 if (tmp == NULL) goto failed;
3795 res = obj2ast_expr(tmp, &value, arena);
3796 if (res != 0) goto failed;
3797 Py_XDECREF(tmp);
3798 tmp = NULL;
3799 } else {
3800 value = NULL;
3801 }
3802 *out = Return(value, lineno, col_offset, arena);
3803 if (*out == NULL) goto failed;
3804 return 0;
3805 }
3806 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3807 asdl_seq* targets;
3808
3809 if (PyObject_HasAttrString(obj, "targets")) {
3810 int res;
3811 Py_ssize_t len;
3812 Py_ssize_t i;
3813 tmp = PyObject_GetAttrString(obj, "targets");
3814 if (tmp == NULL) goto failed;
3815 if (!PyList_Check(tmp)) {
3816 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3817 goto failed;
3818 }
3819 len = PyList_GET_SIZE(tmp);
3820 targets = asdl_seq_new(len, arena);
3821 if (targets == NULL) goto failed;
3822 for (i = 0; i < len; i++) {
3823 expr_ty value;
3824 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3825 if (res != 0) goto failed;
3826 asdl_seq_SET(targets, i, value);
3827 }
3828 Py_XDECREF(tmp);
3829 tmp = NULL;
3830 } else {
3831 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3832 return 1;
3833 }
3834 *out = Delete(targets, lineno, col_offset, arena);
3835 if (*out == NULL) goto failed;
3836 return 0;
3837 }
3838 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3839 asdl_seq* targets;
3840 expr_ty value;
3841
3842 if (PyObject_HasAttrString(obj, "targets")) {
3843 int res;
3844 Py_ssize_t len;
3845 Py_ssize_t i;
3846 tmp = PyObject_GetAttrString(obj, "targets");
3847 if (tmp == NULL) goto failed;
3848 if (!PyList_Check(tmp)) {
3849 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3850 goto failed;
3851 }
3852 len = PyList_GET_SIZE(tmp);
3853 targets = asdl_seq_new(len, arena);
3854 if (targets == NULL) goto failed;
3855 for (i = 0; i < len; i++) {
3856 expr_ty value;
3857 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3858 if (res != 0) goto failed;
3859 asdl_seq_SET(targets, i, value);
3860 }
3861 Py_XDECREF(tmp);
3862 tmp = NULL;
3863 } else {
3864 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3865 return 1;
3866 }
3867 if (PyObject_HasAttrString(obj, "value")) {
3868 int res;
3869 tmp = PyObject_GetAttrString(obj, "value");
3870 if (tmp == NULL) goto failed;
3871 res = obj2ast_expr(tmp, &value, arena);
3872 if (res != 0) goto failed;
3873 Py_XDECREF(tmp);
3874 tmp = NULL;
3875 } else {
3876 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3877 return 1;
3878 }
3879 *out = Assign(targets, value, lineno, col_offset, arena);
3880 if (*out == NULL) goto failed;
3881 return 0;
3882 }
3883 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3884 expr_ty target;
3885 operator_ty op;
3886 expr_ty value;
3887
3888 if (PyObject_HasAttrString(obj, "target")) {
3889 int res;
3890 tmp = PyObject_GetAttrString(obj, "target");
3891 if (tmp == NULL) goto failed;
3892 res = obj2ast_expr(tmp, &target, arena);
3893 if (res != 0) goto failed;
3894 Py_XDECREF(tmp);
3895 tmp = NULL;
3896 } else {
3897 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3898 return 1;
3899 }
3900 if (PyObject_HasAttrString(obj, "op")) {
3901 int res;
3902 tmp = PyObject_GetAttrString(obj, "op");
3903 if (tmp == NULL) goto failed;
3904 res = obj2ast_operator(tmp, &op, arena);
3905 if (res != 0) goto failed;
3906 Py_XDECREF(tmp);
3907 tmp = NULL;
3908 } else {
3909 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3910 return 1;
3911 }
3912 if (PyObject_HasAttrString(obj, "value")) {
3913 int res;
3914 tmp = PyObject_GetAttrString(obj, "value");
3915 if (tmp == NULL) goto failed;
3916 res = obj2ast_expr(tmp, &value, arena);
3917 if (res != 0) goto failed;
3918 Py_XDECREF(tmp);
3919 tmp = NULL;
3920 } else {
3921 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3922 return 1;
3923 }
3924 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3925 if (*out == NULL) goto failed;
3926 return 0;
3927 }
3928 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3929 expr_ty target;
3930 expr_ty iter;
3931 asdl_seq* body;
3932 asdl_seq* orelse;
3933
3934 if (PyObject_HasAttrString(obj, "target")) {
3935 int res;
3936 tmp = PyObject_GetAttrString(obj, "target");
3937 if (tmp == NULL) goto failed;
3938 res = obj2ast_expr(tmp, &target, arena);
3939 if (res != 0) goto failed;
3940 Py_XDECREF(tmp);
3941 tmp = NULL;
3942 } else {
3943 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3944 return 1;
3945 }
3946 if (PyObject_HasAttrString(obj, "iter")) {
3947 int res;
3948 tmp = PyObject_GetAttrString(obj, "iter");
3949 if (tmp == NULL) goto failed;
3950 res = obj2ast_expr(tmp, &iter, arena);
3951 if (res != 0) goto failed;
3952 Py_XDECREF(tmp);
3953 tmp = NULL;
3954 } else {
3955 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3956 return 1;
3957 }
3958 if (PyObject_HasAttrString(obj, "body")) {
3959 int res;
3960 Py_ssize_t len;
3961 Py_ssize_t i;
3962 tmp = PyObject_GetAttrString(obj, "body");
3963 if (tmp == NULL) goto failed;
3964 if (!PyList_Check(tmp)) {
3965 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3966 goto failed;
3967 }
3968 len = PyList_GET_SIZE(tmp);
3969 body = asdl_seq_new(len, arena);
3970 if (body == NULL) goto failed;
3971 for (i = 0; i < len; i++) {
3972 stmt_ty value;
3973 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3974 if (res != 0) goto failed;
3975 asdl_seq_SET(body, i, value);
3976 }
3977 Py_XDECREF(tmp);
3978 tmp = NULL;
3979 } else {
3980 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3981 return 1;
3982 }
3983 if (PyObject_HasAttrString(obj, "orelse")) {
3984 int res;
3985 Py_ssize_t len;
3986 Py_ssize_t i;
3987 tmp = PyObject_GetAttrString(obj, "orelse");
3988 if (tmp == NULL) goto failed;
3989 if (!PyList_Check(tmp)) {
3990 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3991 goto failed;
3992 }
3993 len = PyList_GET_SIZE(tmp);
3994 orelse = asdl_seq_new(len, arena);
3995 if (orelse == NULL) goto failed;
3996 for (i = 0; i < len; i++) {
3997 stmt_ty value;
3998 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3999 if (res != 0) goto failed;
4000 asdl_seq_SET(orelse, i, value);
4001 }
4002 Py_XDECREF(tmp);
4003 tmp = NULL;
4004 } else {
4005 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4006 return 1;
4007 }
4008 *out = For(target, iter, body, orelse, lineno, col_offset,
4009 arena);
4010 if (*out == NULL) goto failed;
4011 return 0;
4012 }
4013 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
4014 expr_ty test;
4015 asdl_seq* body;
4016 asdl_seq* orelse;
4017
4018 if (PyObject_HasAttrString(obj, "test")) {
4019 int res;
4020 tmp = PyObject_GetAttrString(obj, "test");
4021 if (tmp == NULL) goto failed;
4022 res = obj2ast_expr(tmp, &test, arena);
4023 if (res != 0) goto failed;
4024 Py_XDECREF(tmp);
4025 tmp = NULL;
4026 } else {
4027 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4028 return 1;
4029 }
4030 if (PyObject_HasAttrString(obj, "body")) {
4031 int res;
4032 Py_ssize_t len;
4033 Py_ssize_t i;
4034 tmp = PyObject_GetAttrString(obj, "body");
4035 if (tmp == NULL) goto failed;
4036 if (!PyList_Check(tmp)) {
4037 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4038 goto failed;
4039 }
4040 len = PyList_GET_SIZE(tmp);
4041 body = asdl_seq_new(len, arena);
4042 if (body == NULL) goto failed;
4043 for (i = 0; i < len; i++) {
4044 stmt_ty value;
4045 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4046 if (res != 0) goto failed;
4047 asdl_seq_SET(body, i, value);
4048 }
4049 Py_XDECREF(tmp);
4050 tmp = NULL;
4051 } else {
4052 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4053 return 1;
4054 }
4055 if (PyObject_HasAttrString(obj, "orelse")) {
4056 int res;
4057 Py_ssize_t len;
4058 Py_ssize_t i;
4059 tmp = PyObject_GetAttrString(obj, "orelse");
4060 if (tmp == NULL) goto failed;
4061 if (!PyList_Check(tmp)) {
4062 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4063 goto failed;
4064 }
4065 len = PyList_GET_SIZE(tmp);
4066 orelse = asdl_seq_new(len, arena);
4067 if (orelse == NULL) goto failed;
4068 for (i = 0; i < len; i++) {
4069 stmt_ty value;
4070 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4071 if (res != 0) goto failed;
4072 asdl_seq_SET(orelse, i, value);
4073 }
4074 Py_XDECREF(tmp);
4075 tmp = NULL;
4076 } else {
4077 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4078 return 1;
4079 }
4080 *out = While(test, body, orelse, lineno, col_offset, arena);
4081 if (*out == NULL) goto failed;
4082 return 0;
4083 }
4084 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
4085 expr_ty test;
4086 asdl_seq* body;
4087 asdl_seq* orelse;
4088
4089 if (PyObject_HasAttrString(obj, "test")) {
4090 int res;
4091 tmp = PyObject_GetAttrString(obj, "test");
4092 if (tmp == NULL) goto failed;
4093 res = obj2ast_expr(tmp, &test, arena);
4094 if (res != 0) goto failed;
4095 Py_XDECREF(tmp);
4096 tmp = NULL;
4097 } else {
4098 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4099 return 1;
4100 }
4101 if (PyObject_HasAttrString(obj, "body")) {
4102 int res;
4103 Py_ssize_t len;
4104 Py_ssize_t i;
4105 tmp = PyObject_GetAttrString(obj, "body");
4106 if (tmp == NULL) goto failed;
4107 if (!PyList_Check(tmp)) {
4108 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4109 goto failed;
4110 }
4111 len = PyList_GET_SIZE(tmp);
4112 body = asdl_seq_new(len, arena);
4113 if (body == NULL) goto failed;
4114 for (i = 0; i < len; i++) {
4115 stmt_ty value;
4116 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4117 if (res != 0) goto failed;
4118 asdl_seq_SET(body, i, value);
4119 }
4120 Py_XDECREF(tmp);
4121 tmp = NULL;
4122 } else {
4123 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4124 return 1;
4125 }
4126 if (PyObject_HasAttrString(obj, "orelse")) {
4127 int res;
4128 Py_ssize_t len;
4129 Py_ssize_t i;
4130 tmp = PyObject_GetAttrString(obj, "orelse");
4131 if (tmp == NULL) goto failed;
4132 if (!PyList_Check(tmp)) {
4133 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4134 goto failed;
4135 }
4136 len = PyList_GET_SIZE(tmp);
4137 orelse = asdl_seq_new(len, arena);
4138 if (orelse == NULL) goto failed;
4139 for (i = 0; i < len; i++) {
4140 stmt_ty value;
4141 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4142 if (res != 0) goto failed;
4143 asdl_seq_SET(orelse, i, value);
4144 }
4145 Py_XDECREF(tmp);
4146 tmp = NULL;
4147 } else {
4148 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4149 return 1;
4150 }
4151 *out = If(test, body, orelse, lineno, col_offset, arena);
4152 if (*out == NULL) goto failed;
4153 return 0;
4154 }
4155 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
4156 expr_ty context_expr;
4157 expr_ty optional_vars;
4158 asdl_seq* body;
4159
4160 if (PyObject_HasAttrString(obj, "context_expr")) {
4161 int res;
4162 tmp = PyObject_GetAttrString(obj, "context_expr");
4163 if (tmp == NULL) goto failed;
4164 res = obj2ast_expr(tmp, &context_expr, arena);
4165 if (res != 0) goto failed;
4166 Py_XDECREF(tmp);
4167 tmp = NULL;
4168 } else {
4169 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4170 return 1;
4171 }
4172 if (PyObject_HasAttrString(obj, "optional_vars")) {
4173 int res;
4174 tmp = PyObject_GetAttrString(obj, "optional_vars");
4175 if (tmp == NULL) goto failed;
4176 res = obj2ast_expr(tmp, &optional_vars, arena);
4177 if (res != 0) goto failed;
4178 Py_XDECREF(tmp);
4179 tmp = NULL;
4180 } else {
4181 optional_vars = NULL;
4182 }
4183 if (PyObject_HasAttrString(obj, "body")) {
4184 int res;
4185 Py_ssize_t len;
4186 Py_ssize_t i;
4187 tmp = PyObject_GetAttrString(obj, "body");
4188 if (tmp == NULL) goto failed;
4189 if (!PyList_Check(tmp)) {
4190 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4191 goto failed;
4192 }
4193 len = PyList_GET_SIZE(tmp);
4194 body = asdl_seq_new(len, arena);
4195 if (body == NULL) goto failed;
4196 for (i = 0; i < len; i++) {
4197 stmt_ty value;
4198 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4199 if (res != 0) goto failed;
4200 asdl_seq_SET(body, i, value);
4201 }
4202 Py_XDECREF(tmp);
4203 tmp = NULL;
4204 } else {
4205 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4206 return 1;
4207 }
4208 *out = With(context_expr, optional_vars, body, lineno,
4209 col_offset, arena);
4210 if (*out == NULL) goto failed;
4211 return 0;
4212 }
4213 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4214 expr_ty exc;
4215 expr_ty cause;
4216
4217 if (PyObject_HasAttrString(obj, "exc")) {
4218 int res;
4219 tmp = PyObject_GetAttrString(obj, "exc");
4220 if (tmp == NULL) goto failed;
4221 res = obj2ast_expr(tmp, &exc, arena);
4222 if (res != 0) goto failed;
4223 Py_XDECREF(tmp);
4224 tmp = NULL;
4225 } else {
4226 exc = NULL;
4227 }
4228 if (PyObject_HasAttrString(obj, "cause")) {
4229 int res;
4230 tmp = PyObject_GetAttrString(obj, "cause");
4231 if (tmp == NULL) goto failed;
4232 res = obj2ast_expr(tmp, &cause, arena);
4233 if (res != 0) goto failed;
4234 Py_XDECREF(tmp);
4235 tmp = NULL;
4236 } else {
4237 cause = NULL;
4238 }
4239 *out = Raise(exc, cause, lineno, col_offset, arena);
4240 if (*out == NULL) goto failed;
4241 return 0;
4242 }
4243 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4244 asdl_seq* body;
4245 asdl_seq* handlers;
4246 asdl_seq* orelse;
4247
4248 if (PyObject_HasAttrString(obj, "body")) {
4249 int res;
4250 Py_ssize_t len;
4251 Py_ssize_t i;
4252 tmp = PyObject_GetAttrString(obj, "body");
4253 if (tmp == NULL) goto failed;
4254 if (!PyList_Check(tmp)) {
4255 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4256 goto failed;
4257 }
4258 len = PyList_GET_SIZE(tmp);
4259 body = asdl_seq_new(len, arena);
4260 if (body == NULL) goto failed;
4261 for (i = 0; i < len; i++) {
4262 stmt_ty value;
4263 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4264 if (res != 0) goto failed;
4265 asdl_seq_SET(body, i, value);
4266 }
4267 Py_XDECREF(tmp);
4268 tmp = NULL;
4269 } else {
4270 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4271 return 1;
4272 }
4273 if (PyObject_HasAttrString(obj, "handlers")) {
4274 int res;
4275 Py_ssize_t len;
4276 Py_ssize_t i;
4277 tmp = PyObject_GetAttrString(obj, "handlers");
4278 if (tmp == NULL) goto failed;
4279 if (!PyList_Check(tmp)) {
4280 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4281 goto failed;
4282 }
4283 len = PyList_GET_SIZE(tmp);
4284 handlers = asdl_seq_new(len, arena);
4285 if (handlers == NULL) goto failed;
4286 for (i = 0; i < len; i++) {
4287 excepthandler_ty value;
4288 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4289 if (res != 0) goto failed;
4290 asdl_seq_SET(handlers, i, value);
4291 }
4292 Py_XDECREF(tmp);
4293 tmp = NULL;
4294 } else {
4295 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4296 return 1;
4297 }
4298 if (PyObject_HasAttrString(obj, "orelse")) {
4299 int res;
4300 Py_ssize_t len;
4301 Py_ssize_t i;
4302 tmp = PyObject_GetAttrString(obj, "orelse");
4303 if (tmp == NULL) goto failed;
4304 if (!PyList_Check(tmp)) {
4305 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4306 goto failed;
4307 }
4308 len = PyList_GET_SIZE(tmp);
4309 orelse = asdl_seq_new(len, arena);
4310 if (orelse == NULL) goto failed;
4311 for (i = 0; i < len; i++) {
4312 stmt_ty value;
4313 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4314 if (res != 0) goto failed;
4315 asdl_seq_SET(orelse, i, value);
4316 }
4317 Py_XDECREF(tmp);
4318 tmp = NULL;
4319 } else {
4320 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4321 return 1;
4322 }
4323 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4324 arena);
4325 if (*out == NULL) goto failed;
4326 return 0;
4327 }
4328 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4329 asdl_seq* body;
4330 asdl_seq* finalbody;
4331
4332 if (PyObject_HasAttrString(obj, "body")) {
4333 int res;
4334 Py_ssize_t len;
4335 Py_ssize_t i;
4336 tmp = PyObject_GetAttrString(obj, "body");
4337 if (tmp == NULL) goto failed;
4338 if (!PyList_Check(tmp)) {
4339 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4340 goto failed;
4341 }
4342 len = PyList_GET_SIZE(tmp);
4343 body = asdl_seq_new(len, arena);
4344 if (body == NULL) goto failed;
4345 for (i = 0; i < len; i++) {
4346 stmt_ty value;
4347 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4348 if (res != 0) goto failed;
4349 asdl_seq_SET(body, i, value);
4350 }
4351 Py_XDECREF(tmp);
4352 tmp = NULL;
4353 } else {
4354 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4355 return 1;
4356 }
4357 if (PyObject_HasAttrString(obj, "finalbody")) {
4358 int res;
4359 Py_ssize_t len;
4360 Py_ssize_t i;
4361 tmp = PyObject_GetAttrString(obj, "finalbody");
4362 if (tmp == NULL) goto failed;
4363 if (!PyList_Check(tmp)) {
4364 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4365 goto failed;
4366 }
4367 len = PyList_GET_SIZE(tmp);
4368 finalbody = asdl_seq_new(len, arena);
4369 if (finalbody == NULL) goto failed;
4370 for (i = 0; i < len; i++) {
4371 stmt_ty value;
4372 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4373 if (res != 0) goto failed;
4374 asdl_seq_SET(finalbody, i, value);
4375 }
4376 Py_XDECREF(tmp);
4377 tmp = NULL;
4378 } else {
4379 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4380 return 1;
4381 }
4382 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4383 if (*out == NULL) goto failed;
4384 return 0;
4385 }
4386 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4387 expr_ty test;
4388 expr_ty msg;
4389
4390 if (PyObject_HasAttrString(obj, "test")) {
4391 int res;
4392 tmp = PyObject_GetAttrString(obj, "test");
4393 if (tmp == NULL) goto failed;
4394 res = obj2ast_expr(tmp, &test, arena);
4395 if (res != 0) goto failed;
4396 Py_XDECREF(tmp);
4397 tmp = NULL;
4398 } else {
4399 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4400 return 1;
4401 }
4402 if (PyObject_HasAttrString(obj, "msg")) {
4403 int res;
4404 tmp = PyObject_GetAttrString(obj, "msg");
4405 if (tmp == NULL) goto failed;
4406 res = obj2ast_expr(tmp, &msg, arena);
4407 if (res != 0) goto failed;
4408 Py_XDECREF(tmp);
4409 tmp = NULL;
4410 } else {
4411 msg = NULL;
4412 }
4413 *out = Assert(test, msg, lineno, col_offset, arena);
4414 if (*out == NULL) goto failed;
4415 return 0;
4416 }
4417 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4418 asdl_seq* names;
4419
4420 if (PyObject_HasAttrString(obj, "names")) {
4421 int res;
4422 Py_ssize_t len;
4423 Py_ssize_t i;
4424 tmp = PyObject_GetAttrString(obj, "names");
4425 if (tmp == NULL) goto failed;
4426 if (!PyList_Check(tmp)) {
4427 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4428 goto failed;
4429 }
4430 len = PyList_GET_SIZE(tmp);
4431 names = asdl_seq_new(len, arena);
4432 if (names == NULL) goto failed;
4433 for (i = 0; i < len; i++) {
4434 alias_ty value;
4435 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4436 if (res != 0) goto failed;
4437 asdl_seq_SET(names, i, value);
4438 }
4439 Py_XDECREF(tmp);
4440 tmp = NULL;
4441 } else {
4442 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4443 return 1;
4444 }
4445 *out = Import(names, lineno, col_offset, arena);
4446 if (*out == NULL) goto failed;
4447 return 0;
4448 }
4449 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4450 identifier module;
4451 asdl_seq* names;
4452 int level;
4453
4454 if (PyObject_HasAttrString(obj, "module")) {
4455 int res;
4456 tmp = PyObject_GetAttrString(obj, "module");
4457 if (tmp == NULL) goto failed;
4458 res = obj2ast_identifier(tmp, &module, arena);
4459 if (res != 0) goto failed;
4460 Py_XDECREF(tmp);
4461 tmp = NULL;
4462 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004463 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004464 }
4465 if (PyObject_HasAttrString(obj, "names")) {
4466 int res;
4467 Py_ssize_t len;
4468 Py_ssize_t i;
4469 tmp = PyObject_GetAttrString(obj, "names");
4470 if (tmp == NULL) goto failed;
4471 if (!PyList_Check(tmp)) {
4472 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4473 goto failed;
4474 }
4475 len = PyList_GET_SIZE(tmp);
4476 names = asdl_seq_new(len, arena);
4477 if (names == NULL) goto failed;
4478 for (i = 0; i < len; i++) {
4479 alias_ty value;
4480 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4481 if (res != 0) goto failed;
4482 asdl_seq_SET(names, i, value);
4483 }
4484 Py_XDECREF(tmp);
4485 tmp = NULL;
4486 } else {
4487 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4488 return 1;
4489 }
4490 if (PyObject_HasAttrString(obj, "level")) {
4491 int res;
4492 tmp = PyObject_GetAttrString(obj, "level");
4493 if (tmp == NULL) goto failed;
4494 res = obj2ast_int(tmp, &level, arena);
4495 if (res != 0) goto failed;
4496 Py_XDECREF(tmp);
4497 tmp = NULL;
4498 } else {
4499 level = 0;
4500 }
4501 *out = ImportFrom(module, names, level, lineno, col_offset,
4502 arena);
4503 if (*out == NULL) goto failed;
4504 return 0;
4505 }
4506 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4507 asdl_seq* names;
4508
4509 if (PyObject_HasAttrString(obj, "names")) {
4510 int res;
4511 Py_ssize_t len;
4512 Py_ssize_t i;
4513 tmp = PyObject_GetAttrString(obj, "names");
4514 if (tmp == NULL) goto failed;
4515 if (!PyList_Check(tmp)) {
4516 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4517 goto failed;
4518 }
4519 len = PyList_GET_SIZE(tmp);
4520 names = asdl_seq_new(len, arena);
4521 if (names == NULL) goto failed;
4522 for (i = 0; i < len; i++) {
4523 identifier value;
4524 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4525 if (res != 0) goto failed;
4526 asdl_seq_SET(names, i, value);
4527 }
4528 Py_XDECREF(tmp);
4529 tmp = NULL;
4530 } else {
4531 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4532 return 1;
4533 }
4534 *out = Global(names, lineno, col_offset, arena);
4535 if (*out == NULL) goto failed;
4536 return 0;
4537 }
4538 if (PyObject_IsInstance(obj, (PyObject*)Nonlocal_type)) {
4539 asdl_seq* names;
4540
4541 if (PyObject_HasAttrString(obj, "names")) {
4542 int res;
4543 Py_ssize_t len;
4544 Py_ssize_t i;
4545 tmp = PyObject_GetAttrString(obj, "names");
4546 if (tmp == NULL) goto failed;
4547 if (!PyList_Check(tmp)) {
4548 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4549 goto failed;
4550 }
4551 len = PyList_GET_SIZE(tmp);
4552 names = asdl_seq_new(len, arena);
4553 if (names == NULL) goto failed;
4554 for (i = 0; i < len; i++) {
4555 identifier value;
4556 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4557 if (res != 0) goto failed;
4558 asdl_seq_SET(names, i, value);
4559 }
4560 Py_XDECREF(tmp);
4561 tmp = NULL;
4562 } else {
4563 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4564 return 1;
4565 }
4566 *out = Nonlocal(names, lineno, col_offset, arena);
4567 if (*out == NULL) goto failed;
4568 return 0;
4569 }
4570 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4571 expr_ty value;
4572
4573 if (PyObject_HasAttrString(obj, "value")) {
4574 int res;
4575 tmp = PyObject_GetAttrString(obj, "value");
4576 if (tmp == NULL) goto failed;
4577 res = obj2ast_expr(tmp, &value, arena);
4578 if (res != 0) goto failed;
4579 Py_XDECREF(tmp);
4580 tmp = NULL;
4581 } else {
4582 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4583 return 1;
4584 }
4585 *out = Expr(value, lineno, col_offset, arena);
4586 if (*out == NULL) goto failed;
4587 return 0;
4588 }
4589 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4590
4591 *out = Pass(lineno, col_offset, arena);
4592 if (*out == NULL) goto failed;
4593 return 0;
4594 }
4595 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4596
4597 *out = Break(lineno, col_offset, arena);
4598 if (*out == NULL) goto failed;
4599 return 0;
4600 }
4601 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4602
4603 *out = Continue(lineno, col_offset, arena);
4604 if (*out == NULL) goto failed;
4605 return 0;
4606 }
4607
4608 tmp = PyObject_Repr(obj);
4609 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00004610 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004611failed:
4612 Py_XDECREF(tmp);
4613 return 1;
4614}
4615
4616int
4617obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4618{
4619 PyObject* tmp = NULL;
4620
4621 int lineno;
4622 int col_offset;
4623
4624 if (obj == Py_None) {
4625 *out = NULL;
4626 return 0;
4627 }
4628 if (PyObject_HasAttrString(obj, "lineno")) {
4629 int res;
4630 tmp = PyObject_GetAttrString(obj, "lineno");
4631 if (tmp == NULL) goto failed;
4632 res = obj2ast_int(tmp, &lineno, arena);
4633 if (res != 0) goto failed;
4634 Py_XDECREF(tmp);
4635 tmp = NULL;
4636 } else {
4637 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4638 return 1;
4639 }
4640 if (PyObject_HasAttrString(obj, "col_offset")) {
4641 int res;
4642 tmp = PyObject_GetAttrString(obj, "col_offset");
4643 if (tmp == NULL) goto failed;
4644 res = obj2ast_int(tmp, &col_offset, arena);
4645 if (res != 0) goto failed;
4646 Py_XDECREF(tmp);
4647 tmp = NULL;
4648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4650 return 1;
4651 }
4652 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4653 boolop_ty op;
4654 asdl_seq* values;
4655
4656 if (PyObject_HasAttrString(obj, "op")) {
4657 int res;
4658 tmp = PyObject_GetAttrString(obj, "op");
4659 if (tmp == NULL) goto failed;
4660 res = obj2ast_boolop(tmp, &op, arena);
4661 if (res != 0) goto failed;
4662 Py_XDECREF(tmp);
4663 tmp = NULL;
4664 } else {
4665 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4666 return 1;
4667 }
4668 if (PyObject_HasAttrString(obj, "values")) {
4669 int res;
4670 Py_ssize_t len;
4671 Py_ssize_t i;
4672 tmp = PyObject_GetAttrString(obj, "values");
4673 if (tmp == NULL) goto failed;
4674 if (!PyList_Check(tmp)) {
4675 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4676 goto failed;
4677 }
4678 len = PyList_GET_SIZE(tmp);
4679 values = asdl_seq_new(len, arena);
4680 if (values == NULL) goto failed;
4681 for (i = 0; i < len; i++) {
4682 expr_ty value;
4683 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4684 if (res != 0) goto failed;
4685 asdl_seq_SET(values, i, value);
4686 }
4687 Py_XDECREF(tmp);
4688 tmp = NULL;
4689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4691 return 1;
4692 }
4693 *out = BoolOp(op, values, lineno, col_offset, arena);
4694 if (*out == NULL) goto failed;
4695 return 0;
4696 }
4697 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4698 expr_ty left;
4699 operator_ty op;
4700 expr_ty right;
4701
4702 if (PyObject_HasAttrString(obj, "left")) {
4703 int res;
4704 tmp = PyObject_GetAttrString(obj, "left");
4705 if (tmp == NULL) goto failed;
4706 res = obj2ast_expr(tmp, &left, arena);
4707 if (res != 0) goto failed;
4708 Py_XDECREF(tmp);
4709 tmp = NULL;
4710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4712 return 1;
4713 }
4714 if (PyObject_HasAttrString(obj, "op")) {
4715 int res;
4716 tmp = PyObject_GetAttrString(obj, "op");
4717 if (tmp == NULL) goto failed;
4718 res = obj2ast_operator(tmp, &op, arena);
4719 if (res != 0) goto failed;
4720 Py_XDECREF(tmp);
4721 tmp = NULL;
4722 } else {
4723 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4724 return 1;
4725 }
4726 if (PyObject_HasAttrString(obj, "right")) {
4727 int res;
4728 tmp = PyObject_GetAttrString(obj, "right");
4729 if (tmp == NULL) goto failed;
4730 res = obj2ast_expr(tmp, &right, arena);
4731 if (res != 0) goto failed;
4732 Py_XDECREF(tmp);
4733 tmp = NULL;
4734 } else {
4735 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4736 return 1;
4737 }
4738 *out = BinOp(left, op, right, lineno, col_offset, arena);
4739 if (*out == NULL) goto failed;
4740 return 0;
4741 }
4742 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4743 unaryop_ty op;
4744 expr_ty operand;
4745
4746 if (PyObject_HasAttrString(obj, "op")) {
4747 int res;
4748 tmp = PyObject_GetAttrString(obj, "op");
4749 if (tmp == NULL) goto failed;
4750 res = obj2ast_unaryop(tmp, &op, arena);
4751 if (res != 0) goto failed;
4752 Py_XDECREF(tmp);
4753 tmp = NULL;
4754 } else {
4755 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4756 return 1;
4757 }
4758 if (PyObject_HasAttrString(obj, "operand")) {
4759 int res;
4760 tmp = PyObject_GetAttrString(obj, "operand");
4761 if (tmp == NULL) goto failed;
4762 res = obj2ast_expr(tmp, &operand, arena);
4763 if (res != 0) goto failed;
4764 Py_XDECREF(tmp);
4765 tmp = NULL;
4766 } else {
4767 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4768 return 1;
4769 }
4770 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4771 if (*out == NULL) goto failed;
4772 return 0;
4773 }
4774 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4775 arguments_ty args;
4776 expr_ty body;
4777
4778 if (PyObject_HasAttrString(obj, "args")) {
4779 int res;
4780 tmp = PyObject_GetAttrString(obj, "args");
4781 if (tmp == NULL) goto failed;
4782 res = obj2ast_arguments(tmp, &args, arena);
4783 if (res != 0) goto failed;
4784 Py_XDECREF(tmp);
4785 tmp = NULL;
4786 } else {
4787 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4788 return 1;
4789 }
4790 if (PyObject_HasAttrString(obj, "body")) {
4791 int res;
4792 tmp = PyObject_GetAttrString(obj, "body");
4793 if (tmp == NULL) goto failed;
4794 res = obj2ast_expr(tmp, &body, arena);
4795 if (res != 0) goto failed;
4796 Py_XDECREF(tmp);
4797 tmp = NULL;
4798 } else {
4799 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4800 return 1;
4801 }
4802 *out = Lambda(args, body, lineno, col_offset, arena);
4803 if (*out == NULL) goto failed;
4804 return 0;
4805 }
4806 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4807 expr_ty test;
4808 expr_ty body;
4809 expr_ty orelse;
4810
4811 if (PyObject_HasAttrString(obj, "test")) {
4812 int res;
4813 tmp = PyObject_GetAttrString(obj, "test");
4814 if (tmp == NULL) goto failed;
4815 res = obj2ast_expr(tmp, &test, arena);
4816 if (res != 0) goto failed;
4817 Py_XDECREF(tmp);
4818 tmp = NULL;
4819 } else {
4820 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4821 return 1;
4822 }
4823 if (PyObject_HasAttrString(obj, "body")) {
4824 int res;
4825 tmp = PyObject_GetAttrString(obj, "body");
4826 if (tmp == NULL) goto failed;
4827 res = obj2ast_expr(tmp, &body, arena);
4828 if (res != 0) goto failed;
4829 Py_XDECREF(tmp);
4830 tmp = NULL;
4831 } else {
4832 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4833 return 1;
4834 }
4835 if (PyObject_HasAttrString(obj, "orelse")) {
4836 int res;
4837 tmp = PyObject_GetAttrString(obj, "orelse");
4838 if (tmp == NULL) goto failed;
4839 res = obj2ast_expr(tmp, &orelse, arena);
4840 if (res != 0) goto failed;
4841 Py_XDECREF(tmp);
4842 tmp = NULL;
4843 } else {
4844 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4845 return 1;
4846 }
4847 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4848 if (*out == NULL) goto failed;
4849 return 0;
4850 }
4851 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4852 asdl_seq* keys;
4853 asdl_seq* values;
4854
4855 if (PyObject_HasAttrString(obj, "keys")) {
4856 int res;
4857 Py_ssize_t len;
4858 Py_ssize_t i;
4859 tmp = PyObject_GetAttrString(obj, "keys");
4860 if (tmp == NULL) goto failed;
4861 if (!PyList_Check(tmp)) {
4862 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4863 goto failed;
4864 }
4865 len = PyList_GET_SIZE(tmp);
4866 keys = asdl_seq_new(len, arena);
4867 if (keys == NULL) goto failed;
4868 for (i = 0; i < len; i++) {
4869 expr_ty value;
4870 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4871 if (res != 0) goto failed;
4872 asdl_seq_SET(keys, i, value);
4873 }
4874 Py_XDECREF(tmp);
4875 tmp = NULL;
4876 } else {
4877 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4878 return 1;
4879 }
4880 if (PyObject_HasAttrString(obj, "values")) {
4881 int res;
4882 Py_ssize_t len;
4883 Py_ssize_t i;
4884 tmp = PyObject_GetAttrString(obj, "values");
4885 if (tmp == NULL) goto failed;
4886 if (!PyList_Check(tmp)) {
4887 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4888 goto failed;
4889 }
4890 len = PyList_GET_SIZE(tmp);
4891 values = asdl_seq_new(len, arena);
4892 if (values == NULL) goto failed;
4893 for (i = 0; i < len; i++) {
4894 expr_ty value;
4895 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4896 if (res != 0) goto failed;
4897 asdl_seq_SET(values, i, value);
4898 }
4899 Py_XDECREF(tmp);
4900 tmp = NULL;
4901 } else {
4902 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4903 return 1;
4904 }
4905 *out = Dict(keys, values, lineno, col_offset, arena);
4906 if (*out == NULL) goto failed;
4907 return 0;
4908 }
4909 if (PyObject_IsInstance(obj, (PyObject*)Set_type)) {
4910 asdl_seq* elts;
4911
4912 if (PyObject_HasAttrString(obj, "elts")) {
4913 int res;
4914 Py_ssize_t len;
4915 Py_ssize_t i;
4916 tmp = PyObject_GetAttrString(obj, "elts");
4917 if (tmp == NULL) goto failed;
4918 if (!PyList_Check(tmp)) {
4919 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4920 goto failed;
4921 }
4922 len = PyList_GET_SIZE(tmp);
4923 elts = asdl_seq_new(len, arena);
4924 if (elts == NULL) goto failed;
4925 for (i = 0; i < len; i++) {
4926 expr_ty value;
4927 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4928 if (res != 0) goto failed;
4929 asdl_seq_SET(elts, i, value);
4930 }
4931 Py_XDECREF(tmp);
4932 tmp = NULL;
4933 } else {
4934 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4935 return 1;
4936 }
4937 *out = Set(elts, lineno, col_offset, arena);
4938 if (*out == NULL) goto failed;
4939 return 0;
4940 }
4941 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4942 expr_ty elt;
4943 asdl_seq* generators;
4944
4945 if (PyObject_HasAttrString(obj, "elt")) {
4946 int res;
4947 tmp = PyObject_GetAttrString(obj, "elt");
4948 if (tmp == NULL) goto failed;
4949 res = obj2ast_expr(tmp, &elt, arena);
4950 if (res != 0) goto failed;
4951 Py_XDECREF(tmp);
4952 tmp = NULL;
4953 } else {
4954 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4955 return 1;
4956 }
4957 if (PyObject_HasAttrString(obj, "generators")) {
4958 int res;
4959 Py_ssize_t len;
4960 Py_ssize_t i;
4961 tmp = PyObject_GetAttrString(obj, "generators");
4962 if (tmp == NULL) goto failed;
4963 if (!PyList_Check(tmp)) {
4964 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4965 goto failed;
4966 }
4967 len = PyList_GET_SIZE(tmp);
4968 generators = asdl_seq_new(len, arena);
4969 if (generators == NULL) goto failed;
4970 for (i = 0; i < len; i++) {
4971 comprehension_ty value;
4972 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4973 if (res != 0) goto failed;
4974 asdl_seq_SET(generators, i, value);
4975 }
4976 Py_XDECREF(tmp);
4977 tmp = NULL;
4978 } else {
4979 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4980 return 1;
4981 }
4982 *out = ListComp(elt, generators, lineno, col_offset, arena);
4983 if (*out == NULL) goto failed;
4984 return 0;
4985 }
4986 if (PyObject_IsInstance(obj, (PyObject*)SetComp_type)) {
4987 expr_ty elt;
4988 asdl_seq* generators;
4989
4990 if (PyObject_HasAttrString(obj, "elt")) {
4991 int res;
4992 tmp = PyObject_GetAttrString(obj, "elt");
4993 if (tmp == NULL) goto failed;
4994 res = obj2ast_expr(tmp, &elt, arena);
4995 if (res != 0) goto failed;
4996 Py_XDECREF(tmp);
4997 tmp = NULL;
4998 } else {
4999 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5000 return 1;
5001 }
5002 if (PyObject_HasAttrString(obj, "generators")) {
5003 int res;
5004 Py_ssize_t len;
5005 Py_ssize_t i;
5006 tmp = PyObject_GetAttrString(obj, "generators");
5007 if (tmp == NULL) goto failed;
5008 if (!PyList_Check(tmp)) {
5009 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5010 goto failed;
5011 }
5012 len = PyList_GET_SIZE(tmp);
5013 generators = asdl_seq_new(len, arena);
5014 if (generators == NULL) goto failed;
5015 for (i = 0; i < len; i++) {
5016 comprehension_ty value;
5017 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5018 if (res != 0) goto failed;
5019 asdl_seq_SET(generators, i, value);
5020 }
5021 Py_XDECREF(tmp);
5022 tmp = NULL;
5023 } else {
5024 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5025 return 1;
5026 }
5027 *out = SetComp(elt, generators, lineno, col_offset, arena);
5028 if (*out == NULL) goto failed;
5029 return 0;
5030 }
5031 if (PyObject_IsInstance(obj, (PyObject*)DictComp_type)) {
5032 expr_ty key;
5033 expr_ty value;
5034 asdl_seq* generators;
5035
5036 if (PyObject_HasAttrString(obj, "key")) {
5037 int res;
5038 tmp = PyObject_GetAttrString(obj, "key");
5039 if (tmp == NULL) goto failed;
5040 res = obj2ast_expr(tmp, &key, arena);
5041 if (res != 0) goto failed;
5042 Py_XDECREF(tmp);
5043 tmp = NULL;
5044 } else {
5045 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5046 return 1;
5047 }
5048 if (PyObject_HasAttrString(obj, "value")) {
5049 int res;
5050 tmp = PyObject_GetAttrString(obj, "value");
5051 if (tmp == NULL) goto failed;
5052 res = obj2ast_expr(tmp, &value, arena);
5053 if (res != 0) goto failed;
5054 Py_XDECREF(tmp);
5055 tmp = NULL;
5056 } else {
5057 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5058 return 1;
5059 }
5060 if (PyObject_HasAttrString(obj, "generators")) {
5061 int res;
5062 Py_ssize_t len;
5063 Py_ssize_t i;
5064 tmp = PyObject_GetAttrString(obj, "generators");
5065 if (tmp == NULL) goto failed;
5066 if (!PyList_Check(tmp)) {
5067 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5068 goto failed;
5069 }
5070 len = PyList_GET_SIZE(tmp);
5071 generators = asdl_seq_new(len, arena);
5072 if (generators == NULL) goto failed;
5073 for (i = 0; i < len; i++) {
5074 comprehension_ty value;
5075 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5076 if (res != 0) goto failed;
5077 asdl_seq_SET(generators, i, value);
5078 }
5079 Py_XDECREF(tmp);
5080 tmp = NULL;
5081 } else {
5082 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5083 return 1;
5084 }
5085 *out = DictComp(key, value, generators, lineno, col_offset,
5086 arena);
5087 if (*out == NULL) goto failed;
5088 return 0;
5089 }
5090 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
5091 expr_ty elt;
5092 asdl_seq* generators;
5093
5094 if (PyObject_HasAttrString(obj, "elt")) {
5095 int res;
5096 tmp = PyObject_GetAttrString(obj, "elt");
5097 if (tmp == NULL) goto failed;
5098 res = obj2ast_expr(tmp, &elt, arena);
5099 if (res != 0) goto failed;
5100 Py_XDECREF(tmp);
5101 tmp = NULL;
5102 } else {
5103 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5104 return 1;
5105 }
5106 if (PyObject_HasAttrString(obj, "generators")) {
5107 int res;
5108 Py_ssize_t len;
5109 Py_ssize_t i;
5110 tmp = PyObject_GetAttrString(obj, "generators");
5111 if (tmp == NULL) goto failed;
5112 if (!PyList_Check(tmp)) {
5113 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5114 goto failed;
5115 }
5116 len = PyList_GET_SIZE(tmp);
5117 generators = asdl_seq_new(len, arena);
5118 if (generators == NULL) goto failed;
5119 for (i = 0; i < len; i++) {
5120 comprehension_ty value;
5121 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5122 if (res != 0) goto failed;
5123 asdl_seq_SET(generators, i, value);
5124 }
5125 Py_XDECREF(tmp);
5126 tmp = NULL;
5127 } else {
5128 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5129 return 1;
5130 }
5131 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5132 if (*out == NULL) goto failed;
5133 return 0;
5134 }
5135 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
5136 expr_ty value;
5137
5138 if (PyObject_HasAttrString(obj, "value")) {
5139 int res;
5140 tmp = PyObject_GetAttrString(obj, "value");
5141 if (tmp == NULL) goto failed;
5142 res = obj2ast_expr(tmp, &value, arena);
5143 if (res != 0) goto failed;
5144 Py_XDECREF(tmp);
5145 tmp = NULL;
5146 } else {
5147 value = NULL;
5148 }
5149 *out = Yield(value, lineno, col_offset, arena);
5150 if (*out == NULL) goto failed;
5151 return 0;
5152 }
5153 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
5154 expr_ty left;
5155 asdl_int_seq* ops;
5156 asdl_seq* comparators;
5157
5158 if (PyObject_HasAttrString(obj, "left")) {
5159 int res;
5160 tmp = PyObject_GetAttrString(obj, "left");
5161 if (tmp == NULL) goto failed;
5162 res = obj2ast_expr(tmp, &left, arena);
5163 if (res != 0) goto failed;
5164 Py_XDECREF(tmp);
5165 tmp = NULL;
5166 } else {
5167 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5168 return 1;
5169 }
5170 if (PyObject_HasAttrString(obj, "ops")) {
5171 int res;
5172 Py_ssize_t len;
5173 Py_ssize_t i;
5174 tmp = PyObject_GetAttrString(obj, "ops");
5175 if (tmp == NULL) goto failed;
5176 if (!PyList_Check(tmp)) {
5177 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5178 goto failed;
5179 }
5180 len = PyList_GET_SIZE(tmp);
5181 ops = asdl_int_seq_new(len, arena);
5182 if (ops == NULL) goto failed;
5183 for (i = 0; i < len; i++) {
5184 cmpop_ty value;
5185 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5186 if (res != 0) goto failed;
5187 asdl_seq_SET(ops, i, value);
5188 }
5189 Py_XDECREF(tmp);
5190 tmp = NULL;
5191 } else {
5192 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5193 return 1;
5194 }
5195 if (PyObject_HasAttrString(obj, "comparators")) {
5196 int res;
5197 Py_ssize_t len;
5198 Py_ssize_t i;
5199 tmp = PyObject_GetAttrString(obj, "comparators");
5200 if (tmp == NULL) goto failed;
5201 if (!PyList_Check(tmp)) {
5202 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5203 goto failed;
5204 }
5205 len = PyList_GET_SIZE(tmp);
5206 comparators = asdl_seq_new(len, arena);
5207 if (comparators == NULL) goto failed;
5208 for (i = 0; i < len; i++) {
5209 expr_ty value;
5210 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5211 if (res != 0) goto failed;
5212 asdl_seq_SET(comparators, i, value);
5213 }
5214 Py_XDECREF(tmp);
5215 tmp = NULL;
5216 } else {
5217 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5218 return 1;
5219 }
5220 *out = Compare(left, ops, comparators, lineno, col_offset,
5221 arena);
5222 if (*out == NULL) goto failed;
5223 return 0;
5224 }
5225 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
5226 expr_ty func;
5227 asdl_seq* args;
5228 asdl_seq* keywords;
5229 expr_ty starargs;
5230 expr_ty kwargs;
5231
5232 if (PyObject_HasAttrString(obj, "func")) {
5233 int res;
5234 tmp = PyObject_GetAttrString(obj, "func");
5235 if (tmp == NULL) goto failed;
5236 res = obj2ast_expr(tmp, &func, arena);
5237 if (res != 0) goto failed;
5238 Py_XDECREF(tmp);
5239 tmp = NULL;
5240 } else {
5241 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5242 return 1;
5243 }
5244 if (PyObject_HasAttrString(obj, "args")) {
5245 int res;
5246 Py_ssize_t len;
5247 Py_ssize_t i;
5248 tmp = PyObject_GetAttrString(obj, "args");
5249 if (tmp == NULL) goto failed;
5250 if (!PyList_Check(tmp)) {
5251 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5252 goto failed;
5253 }
5254 len = PyList_GET_SIZE(tmp);
5255 args = asdl_seq_new(len, arena);
5256 if (args == NULL) goto failed;
5257 for (i = 0; i < len; i++) {
5258 expr_ty value;
5259 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5260 if (res != 0) goto failed;
5261 asdl_seq_SET(args, i, value);
5262 }
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5267 return 1;
5268 }
5269 if (PyObject_HasAttrString(obj, "keywords")) {
5270 int res;
5271 Py_ssize_t len;
5272 Py_ssize_t i;
5273 tmp = PyObject_GetAttrString(obj, "keywords");
5274 if (tmp == NULL) goto failed;
5275 if (!PyList_Check(tmp)) {
5276 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5277 goto failed;
5278 }
5279 len = PyList_GET_SIZE(tmp);
5280 keywords = asdl_seq_new(len, arena);
5281 if (keywords == NULL) goto failed;
5282 for (i = 0; i < len; i++) {
5283 keyword_ty value;
5284 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5285 if (res != 0) goto failed;
5286 asdl_seq_SET(keywords, i, value);
5287 }
5288 Py_XDECREF(tmp);
5289 tmp = NULL;
5290 } else {
5291 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5292 return 1;
5293 }
5294 if (PyObject_HasAttrString(obj, "starargs")) {
5295 int res;
5296 tmp = PyObject_GetAttrString(obj, "starargs");
5297 if (tmp == NULL) goto failed;
5298 res = obj2ast_expr(tmp, &starargs, arena);
5299 if (res != 0) goto failed;
5300 Py_XDECREF(tmp);
5301 tmp = NULL;
5302 } else {
5303 starargs = NULL;
5304 }
5305 if (PyObject_HasAttrString(obj, "kwargs")) {
5306 int res;
5307 tmp = PyObject_GetAttrString(obj, "kwargs");
5308 if (tmp == NULL) goto failed;
5309 res = obj2ast_expr(tmp, &kwargs, arena);
5310 if (res != 0) goto failed;
5311 Py_XDECREF(tmp);
5312 tmp = NULL;
5313 } else {
5314 kwargs = NULL;
5315 }
5316 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5317 col_offset, arena);
5318 if (*out == NULL) goto failed;
5319 return 0;
5320 }
5321 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5322 object n;
5323
5324 if (PyObject_HasAttrString(obj, "n")) {
5325 int res;
5326 tmp = PyObject_GetAttrString(obj, "n");
5327 if (tmp == NULL) goto failed;
5328 res = obj2ast_object(tmp, &n, arena);
5329 if (res != 0) goto failed;
5330 Py_XDECREF(tmp);
5331 tmp = NULL;
5332 } else {
5333 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5334 return 1;
5335 }
5336 *out = Num(n, lineno, col_offset, arena);
5337 if (*out == NULL) goto failed;
5338 return 0;
5339 }
5340 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5341 string s;
5342
5343 if (PyObject_HasAttrString(obj, "s")) {
5344 int res;
5345 tmp = PyObject_GetAttrString(obj, "s");
5346 if (tmp == NULL) goto failed;
5347 res = obj2ast_string(tmp, &s, arena);
5348 if (res != 0) goto failed;
5349 Py_XDECREF(tmp);
5350 tmp = NULL;
5351 } else {
5352 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5353 return 1;
5354 }
5355 *out = Str(s, lineno, col_offset, arena);
5356 if (*out == NULL) goto failed;
5357 return 0;
5358 }
5359 if (PyObject_IsInstance(obj, (PyObject*)Bytes_type)) {
5360 string s;
5361
5362 if (PyObject_HasAttrString(obj, "s")) {
5363 int res;
5364 tmp = PyObject_GetAttrString(obj, "s");
5365 if (tmp == NULL) goto failed;
5366 res = obj2ast_string(tmp, &s, arena);
5367 if (res != 0) goto failed;
5368 Py_XDECREF(tmp);
5369 tmp = NULL;
5370 } else {
5371 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5372 return 1;
5373 }
5374 *out = Bytes(s, lineno, col_offset, arena);
5375 if (*out == NULL) goto failed;
5376 return 0;
5377 }
5378 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5379
5380 *out = Ellipsis(lineno, col_offset, arena);
5381 if (*out == NULL) goto failed;
5382 return 0;
5383 }
5384 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5385 expr_ty value;
5386 identifier attr;
5387 expr_context_ty ctx;
5388
5389 if (PyObject_HasAttrString(obj, "value")) {
5390 int res;
5391 tmp = PyObject_GetAttrString(obj, "value");
5392 if (tmp == NULL) goto failed;
5393 res = obj2ast_expr(tmp, &value, arena);
5394 if (res != 0) goto failed;
5395 Py_XDECREF(tmp);
5396 tmp = NULL;
5397 } else {
5398 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5399 return 1;
5400 }
5401 if (PyObject_HasAttrString(obj, "attr")) {
5402 int res;
5403 tmp = PyObject_GetAttrString(obj, "attr");
5404 if (tmp == NULL) goto failed;
5405 res = obj2ast_identifier(tmp, &attr, arena);
5406 if (res != 0) goto failed;
5407 Py_XDECREF(tmp);
5408 tmp = NULL;
5409 } else {
5410 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5411 return 1;
5412 }
5413 if (PyObject_HasAttrString(obj, "ctx")) {
5414 int res;
5415 tmp = PyObject_GetAttrString(obj, "ctx");
5416 if (tmp == NULL) goto failed;
5417 res = obj2ast_expr_context(tmp, &ctx, arena);
5418 if (res != 0) goto failed;
5419 Py_XDECREF(tmp);
5420 tmp = NULL;
5421 } else {
5422 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5423 return 1;
5424 }
5425 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5426 if (*out == NULL) goto failed;
5427 return 0;
5428 }
5429 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5430 expr_ty value;
5431 slice_ty slice;
5432 expr_context_ty ctx;
5433
5434 if (PyObject_HasAttrString(obj, "value")) {
5435 int res;
5436 tmp = PyObject_GetAttrString(obj, "value");
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_expr(tmp, &value, arena);
5439 if (res != 0) goto failed;
5440 Py_XDECREF(tmp);
5441 tmp = NULL;
5442 } else {
5443 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5444 return 1;
5445 }
5446 if (PyObject_HasAttrString(obj, "slice")) {
5447 int res;
5448 tmp = PyObject_GetAttrString(obj, "slice");
5449 if (tmp == NULL) goto failed;
5450 res = obj2ast_slice(tmp, &slice, arena);
5451 if (res != 0) goto failed;
5452 Py_XDECREF(tmp);
5453 tmp = NULL;
5454 } else {
5455 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5456 return 1;
5457 }
5458 if (PyObject_HasAttrString(obj, "ctx")) {
5459 int res;
5460 tmp = PyObject_GetAttrString(obj, "ctx");
5461 if (tmp == NULL) goto failed;
5462 res = obj2ast_expr_context(tmp, &ctx, arena);
5463 if (res != 0) goto failed;
5464 Py_XDECREF(tmp);
5465 tmp = NULL;
5466 } else {
5467 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5468 return 1;
5469 }
5470 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5471 if (*out == NULL) goto failed;
5472 return 0;
5473 }
5474 if (PyObject_IsInstance(obj, (PyObject*)Starred_type)) {
5475 expr_ty value;
5476 expr_context_ty ctx;
5477
5478 if (PyObject_HasAttrString(obj, "value")) {
5479 int res;
5480 tmp = PyObject_GetAttrString(obj, "value");
5481 if (tmp == NULL) goto failed;
5482 res = obj2ast_expr(tmp, &value, arena);
5483 if (res != 0) goto failed;
5484 Py_XDECREF(tmp);
5485 tmp = NULL;
5486 } else {
5487 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5488 return 1;
5489 }
5490 if (PyObject_HasAttrString(obj, "ctx")) {
5491 int res;
5492 tmp = PyObject_GetAttrString(obj, "ctx");
5493 if (tmp == NULL) goto failed;
5494 res = obj2ast_expr_context(tmp, &ctx, arena);
5495 if (res != 0) goto failed;
5496 Py_XDECREF(tmp);
5497 tmp = NULL;
5498 } else {
5499 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5500 return 1;
5501 }
5502 *out = Starred(value, ctx, lineno, col_offset, arena);
5503 if (*out == NULL) goto failed;
5504 return 0;
5505 }
5506 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5507 identifier id;
5508 expr_context_ty ctx;
5509
5510 if (PyObject_HasAttrString(obj, "id")) {
5511 int res;
5512 tmp = PyObject_GetAttrString(obj, "id");
5513 if (tmp == NULL) goto failed;
5514 res = obj2ast_identifier(tmp, &id, arena);
5515 if (res != 0) goto failed;
5516 Py_XDECREF(tmp);
5517 tmp = NULL;
5518 } else {
5519 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5520 return 1;
5521 }
5522 if (PyObject_HasAttrString(obj, "ctx")) {
5523 int res;
5524 tmp = PyObject_GetAttrString(obj, "ctx");
5525 if (tmp == NULL) goto failed;
5526 res = obj2ast_expr_context(tmp, &ctx, arena);
5527 if (res != 0) goto failed;
5528 Py_XDECREF(tmp);
5529 tmp = NULL;
5530 } else {
5531 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5532 return 1;
5533 }
5534 *out = Name(id, ctx, lineno, col_offset, arena);
5535 if (*out == NULL) goto failed;
5536 return 0;
5537 }
5538 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5539 asdl_seq* elts;
5540 expr_context_ty ctx;
5541
5542 if (PyObject_HasAttrString(obj, "elts")) {
5543 int res;
5544 Py_ssize_t len;
5545 Py_ssize_t i;
5546 tmp = PyObject_GetAttrString(obj, "elts");
5547 if (tmp == NULL) goto failed;
5548 if (!PyList_Check(tmp)) {
5549 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5550 goto failed;
5551 }
5552 len = PyList_GET_SIZE(tmp);
5553 elts = asdl_seq_new(len, arena);
5554 if (elts == NULL) goto failed;
5555 for (i = 0; i < len; i++) {
5556 expr_ty value;
5557 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5558 if (res != 0) goto failed;
5559 asdl_seq_SET(elts, i, value);
5560 }
5561 Py_XDECREF(tmp);
5562 tmp = NULL;
5563 } else {
5564 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5565 return 1;
5566 }
5567 if (PyObject_HasAttrString(obj, "ctx")) {
5568 int res;
5569 tmp = PyObject_GetAttrString(obj, "ctx");
5570 if (tmp == NULL) goto failed;
5571 res = obj2ast_expr_context(tmp, &ctx, arena);
5572 if (res != 0) goto failed;
5573 Py_XDECREF(tmp);
5574 tmp = NULL;
5575 } else {
5576 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5577 return 1;
5578 }
5579 *out = List(elts, ctx, lineno, col_offset, arena);
5580 if (*out == NULL) goto failed;
5581 return 0;
5582 }
5583 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5584 asdl_seq* elts;
5585 expr_context_ty ctx;
5586
5587 if (PyObject_HasAttrString(obj, "elts")) {
5588 int res;
5589 Py_ssize_t len;
5590 Py_ssize_t i;
5591 tmp = PyObject_GetAttrString(obj, "elts");
5592 if (tmp == NULL) goto failed;
5593 if (!PyList_Check(tmp)) {
5594 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5595 goto failed;
5596 }
5597 len = PyList_GET_SIZE(tmp);
5598 elts = asdl_seq_new(len, arena);
5599 if (elts == NULL) goto failed;
5600 for (i = 0; i < len; i++) {
5601 expr_ty value;
5602 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5603 if (res != 0) goto failed;
5604 asdl_seq_SET(elts, i, value);
5605 }
5606 Py_XDECREF(tmp);
5607 tmp = NULL;
5608 } else {
5609 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5610 return 1;
5611 }
5612 if (PyObject_HasAttrString(obj, "ctx")) {
5613 int res;
5614 tmp = PyObject_GetAttrString(obj, "ctx");
5615 if (tmp == NULL) goto failed;
5616 res = obj2ast_expr_context(tmp, &ctx, arena);
5617 if (res != 0) goto failed;
5618 Py_XDECREF(tmp);
5619 tmp = NULL;
5620 } else {
5621 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5622 return 1;
5623 }
5624 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5625 if (*out == NULL) goto failed;
5626 return 0;
5627 }
5628
5629 tmp = PyObject_Repr(obj);
5630 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005631 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005632failed:
5633 Py_XDECREF(tmp);
5634 return 1;
5635}
5636
5637int
5638obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5639{
5640 PyObject* tmp = NULL;
5641
5642 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5643 *out = Load;
5644 return 0;
5645 }
5646 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5647 *out = Store;
5648 return 0;
5649 }
5650 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5651 *out = Del;
5652 return 0;
5653 }
5654 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5655 *out = AugLoad;
5656 return 0;
5657 }
5658 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5659 *out = AugStore;
5660 return 0;
5661 }
5662 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5663 *out = Param;
5664 return 0;
5665 }
5666
5667 tmp = PyObject_Repr(obj);
5668 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005669 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005670failed:
5671 Py_XDECREF(tmp);
5672 return 1;
5673}
5674
5675int
5676obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5677{
5678 PyObject* tmp = NULL;
5679
5680
5681 if (obj == Py_None) {
5682 *out = NULL;
5683 return 0;
5684 }
5685 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5686 expr_ty lower;
5687 expr_ty upper;
5688 expr_ty step;
5689
5690 if (PyObject_HasAttrString(obj, "lower")) {
5691 int res;
5692 tmp = PyObject_GetAttrString(obj, "lower");
5693 if (tmp == NULL) goto failed;
5694 res = obj2ast_expr(tmp, &lower, arena);
5695 if (res != 0) goto failed;
5696 Py_XDECREF(tmp);
5697 tmp = NULL;
5698 } else {
5699 lower = NULL;
5700 }
5701 if (PyObject_HasAttrString(obj, "upper")) {
5702 int res;
5703 tmp = PyObject_GetAttrString(obj, "upper");
5704 if (tmp == NULL) goto failed;
5705 res = obj2ast_expr(tmp, &upper, arena);
5706 if (res != 0) goto failed;
5707 Py_XDECREF(tmp);
5708 tmp = NULL;
5709 } else {
5710 upper = NULL;
5711 }
5712 if (PyObject_HasAttrString(obj, "step")) {
5713 int res;
5714 tmp = PyObject_GetAttrString(obj, "step");
5715 if (tmp == NULL) goto failed;
5716 res = obj2ast_expr(tmp, &step, arena);
5717 if (res != 0) goto failed;
5718 Py_XDECREF(tmp);
5719 tmp = NULL;
5720 } else {
5721 step = NULL;
5722 }
5723 *out = Slice(lower, upper, step, arena);
5724 if (*out == NULL) goto failed;
5725 return 0;
5726 }
5727 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5728 asdl_seq* dims;
5729
5730 if (PyObject_HasAttrString(obj, "dims")) {
5731 int res;
5732 Py_ssize_t len;
5733 Py_ssize_t i;
5734 tmp = PyObject_GetAttrString(obj, "dims");
5735 if (tmp == NULL) goto failed;
5736 if (!PyList_Check(tmp)) {
5737 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5738 goto failed;
5739 }
5740 len = PyList_GET_SIZE(tmp);
5741 dims = asdl_seq_new(len, arena);
5742 if (dims == NULL) goto failed;
5743 for (i = 0; i < len; i++) {
5744 slice_ty value;
5745 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5746 if (res != 0) goto failed;
5747 asdl_seq_SET(dims, i, value);
5748 }
5749 Py_XDECREF(tmp);
5750 tmp = NULL;
5751 } else {
5752 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5753 return 1;
5754 }
5755 *out = ExtSlice(dims, arena);
5756 if (*out == NULL) goto failed;
5757 return 0;
5758 }
5759 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5760 expr_ty value;
5761
5762 if (PyObject_HasAttrString(obj, "value")) {
5763 int res;
5764 tmp = PyObject_GetAttrString(obj, "value");
5765 if (tmp == NULL) goto failed;
5766 res = obj2ast_expr(tmp, &value, arena);
5767 if (res != 0) goto failed;
5768 Py_XDECREF(tmp);
5769 tmp = NULL;
5770 } else {
5771 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5772 return 1;
5773 }
5774 *out = Index(value, arena);
5775 if (*out == NULL) goto failed;
5776 return 0;
5777 }
5778
5779 tmp = PyObject_Repr(obj);
5780 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005781 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005782failed:
5783 Py_XDECREF(tmp);
5784 return 1;
5785}
5786
5787int
5788obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5789{
5790 PyObject* tmp = NULL;
5791
5792 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5793 *out = And;
5794 return 0;
5795 }
5796 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5797 *out = Or;
5798 return 0;
5799 }
5800
5801 tmp = PyObject_Repr(obj);
5802 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005803 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005804failed:
5805 Py_XDECREF(tmp);
5806 return 1;
5807}
5808
5809int
5810obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5811{
5812 PyObject* tmp = NULL;
5813
5814 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5815 *out = Add;
5816 return 0;
5817 }
5818 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5819 *out = Sub;
5820 return 0;
5821 }
5822 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5823 *out = Mult;
5824 return 0;
5825 }
5826 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5827 *out = Div;
5828 return 0;
5829 }
5830 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5831 *out = Mod;
5832 return 0;
5833 }
5834 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5835 *out = Pow;
5836 return 0;
5837 }
5838 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5839 *out = LShift;
5840 return 0;
5841 }
5842 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5843 *out = RShift;
5844 return 0;
5845 }
5846 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5847 *out = BitOr;
5848 return 0;
5849 }
5850 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5851 *out = BitXor;
5852 return 0;
5853 }
5854 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5855 *out = BitAnd;
5856 return 0;
5857 }
5858 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5859 *out = FloorDiv;
5860 return 0;
5861 }
5862
5863 tmp = PyObject_Repr(obj);
5864 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005865 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005866failed:
5867 Py_XDECREF(tmp);
5868 return 1;
5869}
5870
5871int
5872obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5873{
5874 PyObject* tmp = NULL;
5875
5876 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5877 *out = Invert;
5878 return 0;
5879 }
5880 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5881 *out = Not;
5882 return 0;
5883 }
5884 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5885 *out = UAdd;
5886 return 0;
5887 }
5888 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5889 *out = USub;
5890 return 0;
5891 }
5892
5893 tmp = PyObject_Repr(obj);
5894 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005895 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005896failed:
5897 Py_XDECREF(tmp);
5898 return 1;
5899}
5900
5901int
5902obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5903{
5904 PyObject* tmp = NULL;
5905
5906 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5907 *out = Eq;
5908 return 0;
5909 }
5910 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5911 *out = NotEq;
5912 return 0;
5913 }
5914 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5915 *out = Lt;
5916 return 0;
5917 }
5918 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5919 *out = LtE;
5920 return 0;
5921 }
5922 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5923 *out = Gt;
5924 return 0;
5925 }
5926 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5927 *out = GtE;
5928 return 0;
5929 }
5930 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5931 *out = Is;
5932 return 0;
5933 }
5934 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5935 *out = IsNot;
5936 return 0;
5937 }
5938 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5939 *out = In;
5940 return 0;
5941 }
5942 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5943 *out = NotIn;
5944 return 0;
5945 }
5946
5947 tmp = PyObject_Repr(obj);
5948 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005949 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005950failed:
5951 Py_XDECREF(tmp);
5952 return 1;
5953}
5954
5955int
5956obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5957{
5958 PyObject* tmp = NULL;
5959 expr_ty target;
5960 expr_ty iter;
5961 asdl_seq* ifs;
5962
5963 if (PyObject_HasAttrString(obj, "target")) {
5964 int res;
5965 tmp = PyObject_GetAttrString(obj, "target");
5966 if (tmp == NULL) goto failed;
5967 res = obj2ast_expr(tmp, &target, arena);
5968 if (res != 0) goto failed;
5969 Py_XDECREF(tmp);
5970 tmp = NULL;
5971 } else {
5972 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5973 return 1;
5974 }
5975 if (PyObject_HasAttrString(obj, "iter")) {
5976 int res;
5977 tmp = PyObject_GetAttrString(obj, "iter");
5978 if (tmp == NULL) goto failed;
5979 res = obj2ast_expr(tmp, &iter, arena);
5980 if (res != 0) goto failed;
5981 Py_XDECREF(tmp);
5982 tmp = NULL;
5983 } else {
5984 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5985 return 1;
5986 }
5987 if (PyObject_HasAttrString(obj, "ifs")) {
5988 int res;
5989 Py_ssize_t len;
5990 Py_ssize_t i;
5991 tmp = PyObject_GetAttrString(obj, "ifs");
5992 if (tmp == NULL) goto failed;
5993 if (!PyList_Check(tmp)) {
5994 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5995 goto failed;
5996 }
5997 len = PyList_GET_SIZE(tmp);
5998 ifs = asdl_seq_new(len, arena);
5999 if (ifs == NULL) goto failed;
6000 for (i = 0; i < len; i++) {
6001 expr_ty value;
6002 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6003 if (res != 0) goto failed;
6004 asdl_seq_SET(ifs, i, value);
6005 }
6006 Py_XDECREF(tmp);
6007 tmp = NULL;
6008 } else {
6009 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6010 return 1;
6011 }
6012 *out = comprehension(target, iter, ifs, arena);
6013 return 0;
6014failed:
6015 Py_XDECREF(tmp);
6016 return 1;
6017}
6018
6019int
6020obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6021{
6022 PyObject* tmp = NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006023
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006024 int lineno;
6025 int col_offset;
6026
Neal Norwitzad74aa82008-03-31 05:14:30 +00006027 if (obj == Py_None) {
6028 *out = NULL;
6029 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006030 }
6031 if (PyObject_HasAttrString(obj, "lineno")) {
6032 int res;
6033 tmp = PyObject_GetAttrString(obj, "lineno");
6034 if (tmp == NULL) goto failed;
6035 res = obj2ast_int(tmp, &lineno, arena);
6036 if (res != 0) goto failed;
6037 Py_XDECREF(tmp);
6038 tmp = NULL;
6039 } else {
6040 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6041 return 1;
6042 }
6043 if (PyObject_HasAttrString(obj, "col_offset")) {
6044 int res;
6045 tmp = PyObject_GetAttrString(obj, "col_offset");
6046 if (tmp == NULL) goto failed;
6047 res = obj2ast_int(tmp, &col_offset, arena);
6048 if (res != 0) goto failed;
6049 Py_XDECREF(tmp);
6050 tmp = NULL;
6051 } else {
6052 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6053 return 1;
6054 }
Neal Norwitzad74aa82008-03-31 05:14:30 +00006055 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
6056 expr_ty type;
6057 identifier name;
6058 asdl_seq* body;
6059
6060 if (PyObject_HasAttrString(obj, "type")) {
6061 int res;
6062 tmp = PyObject_GetAttrString(obj, "type");
6063 if (tmp == NULL) goto failed;
6064 res = obj2ast_expr(tmp, &type, arena);
6065 if (res != 0) goto failed;
6066 Py_XDECREF(tmp);
6067 tmp = NULL;
6068 } else {
6069 type = NULL;
6070 }
6071 if (PyObject_HasAttrString(obj, "name")) {
6072 int res;
6073 tmp = PyObject_GetAttrString(obj, "name");
6074 if (tmp == NULL) goto failed;
6075 res = obj2ast_identifier(tmp, &name, arena);
6076 if (res != 0) goto failed;
6077 Py_XDECREF(tmp);
6078 tmp = NULL;
6079 } else {
6080 name = NULL;
6081 }
6082 if (PyObject_HasAttrString(obj, "body")) {
6083 int res;
6084 Py_ssize_t len;
6085 Py_ssize_t i;
6086 tmp = PyObject_GetAttrString(obj, "body");
6087 if (tmp == NULL) goto failed;
6088 if (!PyList_Check(tmp)) {
6089 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6090 goto failed;
6091 }
6092 len = PyList_GET_SIZE(tmp);
6093 body = asdl_seq_new(len, arena);
6094 if (body == NULL) goto failed;
6095 for (i = 0; i < len; i++) {
6096 stmt_ty value;
6097 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6098 if (res != 0) goto failed;
6099 asdl_seq_SET(body, i, value);
6100 }
6101 Py_XDECREF(tmp);
6102 tmp = NULL;
6103 } else {
6104 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6105 return 1;
6106 }
6107 *out = ExceptHandler(type, name, body, lineno, col_offset,
6108 arena);
6109 if (*out == NULL) goto failed;
6110 return 0;
6111 }
6112
6113 tmp = PyObject_Repr(obj);
6114 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00006115 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyBytes_AS_STRING(tmp));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006116failed:
6117 Py_XDECREF(tmp);
6118 return 1;
6119}
6120
6121int
6122obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6123{
6124 PyObject* tmp = NULL;
6125 asdl_seq* args;
6126 identifier vararg;
6127 expr_ty varargannotation;
6128 asdl_seq* kwonlyargs;
6129 identifier kwarg;
6130 expr_ty kwargannotation;
6131 asdl_seq* defaults;
6132 asdl_seq* kw_defaults;
6133
6134 if (PyObject_HasAttrString(obj, "args")) {
6135 int res;
6136 Py_ssize_t len;
6137 Py_ssize_t i;
6138 tmp = PyObject_GetAttrString(obj, "args");
6139 if (tmp == NULL) goto failed;
6140 if (!PyList_Check(tmp)) {
6141 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6142 goto failed;
6143 }
6144 len = PyList_GET_SIZE(tmp);
6145 args = asdl_seq_new(len, arena);
6146 if (args == NULL) goto failed;
6147 for (i = 0; i < len; i++) {
6148 arg_ty value;
6149 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6150 if (res != 0) goto failed;
6151 asdl_seq_SET(args, i, value);
6152 }
6153 Py_XDECREF(tmp);
6154 tmp = NULL;
6155 } else {
6156 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6157 return 1;
6158 }
6159 if (PyObject_HasAttrString(obj, "vararg")) {
6160 int res;
6161 tmp = PyObject_GetAttrString(obj, "vararg");
6162 if (tmp == NULL) goto failed;
6163 res = obj2ast_identifier(tmp, &vararg, arena);
6164 if (res != 0) goto failed;
6165 Py_XDECREF(tmp);
6166 tmp = NULL;
6167 } else {
6168 vararg = NULL;
6169 }
6170 if (PyObject_HasAttrString(obj, "varargannotation")) {
6171 int res;
6172 tmp = PyObject_GetAttrString(obj, "varargannotation");
6173 if (tmp == NULL) goto failed;
6174 res = obj2ast_expr(tmp, &varargannotation, arena);
6175 if (res != 0) goto failed;
6176 Py_XDECREF(tmp);
6177 tmp = NULL;
6178 } else {
6179 varargannotation = NULL;
6180 }
6181 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6182 int res;
6183 Py_ssize_t len;
6184 Py_ssize_t i;
6185 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6186 if (tmp == NULL) goto failed;
6187 if (!PyList_Check(tmp)) {
6188 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6189 goto failed;
6190 }
6191 len = PyList_GET_SIZE(tmp);
6192 kwonlyargs = asdl_seq_new(len, arena);
6193 if (kwonlyargs == NULL) goto failed;
6194 for (i = 0; i < len; i++) {
6195 arg_ty value;
6196 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6197 if (res != 0) goto failed;
6198 asdl_seq_SET(kwonlyargs, i, value);
6199 }
6200 Py_XDECREF(tmp);
6201 tmp = NULL;
6202 } else {
6203 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6204 return 1;
6205 }
6206 if (PyObject_HasAttrString(obj, "kwarg")) {
6207 int res;
6208 tmp = PyObject_GetAttrString(obj, "kwarg");
6209 if (tmp == NULL) goto failed;
6210 res = obj2ast_identifier(tmp, &kwarg, arena);
6211 if (res != 0) goto failed;
6212 Py_XDECREF(tmp);
6213 tmp = NULL;
6214 } else {
6215 kwarg = NULL;
6216 }
6217 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6218 int res;
6219 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6220 if (tmp == NULL) goto failed;
6221 res = obj2ast_expr(tmp, &kwargannotation, arena);
6222 if (res != 0) goto failed;
6223 Py_XDECREF(tmp);
6224 tmp = NULL;
6225 } else {
6226 kwargannotation = NULL;
6227 }
6228 if (PyObject_HasAttrString(obj, "defaults")) {
6229 int res;
6230 Py_ssize_t len;
6231 Py_ssize_t i;
6232 tmp = PyObject_GetAttrString(obj, "defaults");
6233 if (tmp == NULL) goto failed;
6234 if (!PyList_Check(tmp)) {
6235 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6236 goto failed;
6237 }
6238 len = PyList_GET_SIZE(tmp);
6239 defaults = asdl_seq_new(len, arena);
6240 if (defaults == NULL) goto failed;
6241 for (i = 0; i < len; i++) {
6242 expr_ty value;
6243 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6244 if (res != 0) goto failed;
6245 asdl_seq_SET(defaults, i, value);
6246 }
6247 Py_XDECREF(tmp);
6248 tmp = NULL;
6249 } else {
6250 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6251 return 1;
6252 }
6253 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6254 int res;
6255 Py_ssize_t len;
6256 Py_ssize_t i;
6257 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6258 if (tmp == NULL) goto failed;
6259 if (!PyList_Check(tmp)) {
6260 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6261 goto failed;
6262 }
6263 len = PyList_GET_SIZE(tmp);
6264 kw_defaults = asdl_seq_new(len, arena);
6265 if (kw_defaults == NULL) goto failed;
6266 for (i = 0; i < len; i++) {
6267 expr_ty value;
6268 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6269 if (res != 0) goto failed;
6270 asdl_seq_SET(kw_defaults, i, value);
6271 }
6272 Py_XDECREF(tmp);
6273 tmp = NULL;
6274 } else {
6275 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6276 return 1;
6277 }
6278 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6279 kwargannotation, defaults, kw_defaults, arena);
6280 return 0;
6281failed:
6282 Py_XDECREF(tmp);
6283 return 1;
6284}
6285
6286int
6287obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6288{
6289 PyObject* tmp = NULL;
6290 identifier arg;
6291 expr_ty annotation;
6292
6293 if (PyObject_HasAttrString(obj, "arg")) {
6294 int res;
6295 tmp = PyObject_GetAttrString(obj, "arg");
6296 if (tmp == NULL) goto failed;
6297 res = obj2ast_identifier(tmp, &arg, arena);
6298 if (res != 0) goto failed;
6299 Py_XDECREF(tmp);
6300 tmp = NULL;
6301 } else {
6302 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6303 return 1;
6304 }
6305 if (PyObject_HasAttrString(obj, "annotation")) {
6306 int res;
6307 tmp = PyObject_GetAttrString(obj, "annotation");
6308 if (tmp == NULL) goto failed;
6309 res = obj2ast_expr(tmp, &annotation, arena);
6310 if (res != 0) goto failed;
6311 Py_XDECREF(tmp);
6312 tmp = NULL;
6313 } else {
6314 annotation = NULL;
6315 }
6316 *out = arg(arg, annotation, arena);
6317 return 0;
6318failed:
6319 Py_XDECREF(tmp);
6320 return 1;
6321}
6322
6323int
6324obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6325{
6326 PyObject* tmp = NULL;
6327 identifier arg;
6328 expr_ty value;
6329
6330 if (PyObject_HasAttrString(obj, "arg")) {
6331 int res;
6332 tmp = PyObject_GetAttrString(obj, "arg");
6333 if (tmp == NULL) goto failed;
6334 res = obj2ast_identifier(tmp, &arg, arena);
6335 if (res != 0) goto failed;
6336 Py_XDECREF(tmp);
6337 tmp = NULL;
6338 } else {
6339 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6340 return 1;
6341 }
6342 if (PyObject_HasAttrString(obj, "value")) {
6343 int res;
6344 tmp = PyObject_GetAttrString(obj, "value");
6345 if (tmp == NULL) goto failed;
6346 res = obj2ast_expr(tmp, &value, arena);
6347 if (res != 0) goto failed;
6348 Py_XDECREF(tmp);
6349 tmp = NULL;
6350 } else {
6351 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6352 return 1;
6353 }
6354 *out = keyword(arg, value, arena);
6355 return 0;
6356failed:
6357 Py_XDECREF(tmp);
6358 return 1;
6359}
6360
6361int
6362obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6363{
6364 PyObject* tmp = NULL;
6365 identifier name;
6366 identifier asname;
6367
6368 if (PyObject_HasAttrString(obj, "name")) {
6369 int res;
6370 tmp = PyObject_GetAttrString(obj, "name");
6371 if (tmp == NULL) goto failed;
6372 res = obj2ast_identifier(tmp, &name, arena);
6373 if (res != 0) goto failed;
6374 Py_XDECREF(tmp);
6375 tmp = NULL;
6376 } else {
6377 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6378 return 1;
6379 }
6380 if (PyObject_HasAttrString(obj, "asname")) {
6381 int res;
6382 tmp = PyObject_GetAttrString(obj, "asname");
6383 if (tmp == NULL) goto failed;
6384 res = obj2ast_identifier(tmp, &asname, arena);
6385 if (res != 0) goto failed;
6386 Py_XDECREF(tmp);
6387 tmp = NULL;
6388 } else {
6389 asname = NULL;
6390 }
6391 *out = alias(name, asname, arena);
6392 return 0;
6393failed:
6394 Py_XDECREF(tmp);
6395 return 1;
6396}
6397
6398
Martin v. Löwis1a214512008-06-11 05:26:20 +00006399static struct PyModuleDef _astmodule = {
6400 PyModuleDef_HEAD_INIT, "_ast"
6401};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006402PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006403PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006404{
6405 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006406 if (!init_types()) return NULL;
6407 m = PyModule_Create(&_astmodule);
6408 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006409 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006410 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6411 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006412 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006413 return NULL;
Benjamin Peterson2e24deb2009-07-17 19:11:02 +00006414 if (PyModule_AddStringConstant(m, "__version__", "73626") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006415 return NULL;
6416 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6417 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006418 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006419 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006420 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006421 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006422 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006423 0) return NULL;
6424 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6425 NULL;
6426 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6427 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006428 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006429 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006430 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006431 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006432 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006433 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006434 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006435 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006436 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006437 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006438 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006439 0) return NULL;
6440 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6441 NULL;
6442 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6443 NULL;
6444 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6445 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6446 NULL;
6447 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6448 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006449 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006450 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006451 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006452 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006453 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006454 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006455 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006456 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006457 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006458 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006459 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006460 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006461 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006462 return NULL;
6463 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6464 NULL;
6465 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6466 NULL;
6467 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6468 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006469 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006470 return NULL;
6471 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6472 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006473 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006474 return NULL;
6475 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6476 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006477 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006478 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006479 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006480 return NULL;
6481 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6482 NULL;
6483 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6484 NULL;
6485 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6486 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006487 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006488 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006489 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006490 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006491 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006492 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006493 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006494 (PyObject*)GeneratorExp_type) < 0) return NULL;
6495 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6496 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006497 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006498 return NULL;
6499 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6500 NULL;
6501 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6502 NULL;
6503 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6504 NULL;
6505 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6506 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006507 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006508 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006509 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006510 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006511 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006512 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006513 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006514 return NULL;
6515 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6516 NULL;
6517 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6518 NULL;
6519 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6520 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006521 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006522 (PyObject*)expr_context_type) < 0) return NULL;
6523 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6524 NULL;
6525 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6526 NULL;
6527 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6528 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006529 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006530 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006531 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006532 return NULL;
6533 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6534 NULL;
6535 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6536 NULL;
6537 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6538 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006539 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006540 return NULL;
6541 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6542 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006543 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006544 return NULL;
6545 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6546 NULL;
6547 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006548 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006549 return NULL;
6550 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6551 NULL;
6552 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6553 NULL;
6554 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6555 NULL;
6556 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6557 NULL;
6558 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6559 NULL;
6560 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6561 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006562 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006563 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006564 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006565 return NULL;
6566 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6567 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006568 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006569 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006570 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006571 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006572 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006573 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006574 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006575 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006576 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006577 return NULL;
6578 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6579 NULL;
6580 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6581 NULL;
6582 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6583 NULL;
6584 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6585 NULL;
6586 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6587 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6588 NULL;
6589 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6590 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6591 NULL;
6592 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6593 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6594 NULL;
6595 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6596 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6597 NULL;
6598 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6599 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6600 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006601 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006602 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006603 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006604 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006605 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006606 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006607 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006608 0) return NULL;
6609 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6610 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006611 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006612 return NULL;
6613 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6614 NULL;
6615 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006616}
6617
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006618
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006619PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006620{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006621 init_types();
6622 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006623}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006624
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006625/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6626mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006627{
6628 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006629 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6630 (PyObject*)Interactive_type};
6631 char *req_name[] = {"Module", "Expression", "Interactive"};
6632 assert(0 <= mode && mode <= 2);
6633
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006634 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006635
6636 if (!PyObject_IsInstance(ast, req_type[mode])) {
6637 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6638 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006639 return NULL;
6640 }
6641 if (obj2ast_mod(ast, &res, arena) != 0)
6642 return NULL;
6643 else
6644 return res;
6645}
6646
6647int PyAST_Check(PyObject* obj)
6648{
6649 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006650 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006651}
6652
Martin v. Löwis5b222132007-06-10 09:51:05 +00006653