blob: 1e85b2fac7e011b0a2e492099bc0c7e33b7d3bc6 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Petersone6d4a9b2008-12-06 18:33:20 +00005 __version__ 67616.
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;
1335 if (!module) {
1336 PyErr_SetString(PyExc_ValueError,
1337 "field module is required for ImportFrom");
1338 return NULL;
1339 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001340 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001341 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 p->kind = ImportFrom_kind;
1344 p->v.ImportFrom.module = module;
1345 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001346 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001348 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 return p;
1350}
1351
1352stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001353Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354{
1355 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001356 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001357 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 p->kind = Global_kind;
1360 p->v.Global.names = names;
1361 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001362 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 return p;
1364}
1365
1366stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001367Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1368{
1369 stmt_ty p;
1370 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1371 if (!p)
1372 return NULL;
1373 p->kind = Nonlocal_kind;
1374 p->v.Nonlocal.names = names;
1375 p->lineno = lineno;
1376 p->col_offset = col_offset;
1377 return p;
1378}
1379
1380stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001381Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382{
1383 stmt_ty p;
1384 if (!value) {
1385 PyErr_SetString(PyExc_ValueError,
1386 "field value is required for Expr");
1387 return NULL;
1388 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001389 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001390 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 p->kind = Expr_kind;
1393 p->v.Expr.value = value;
1394 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001395 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return p;
1397}
1398
1399stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001400Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401{
1402 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001403 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001404 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 p->kind = Pass_kind;
1407 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001408 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 return p;
1410}
1411
1412stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001413Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414{
1415 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001417 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 p->kind = Break_kind;
1420 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return p;
1423}
1424
1425stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427{
1428 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001429 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001430 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 p->kind = Continue_kind;
1433 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001434 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 return p;
1436}
1437
1438expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001439BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1440 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441{
1442 expr_ty p;
1443 if (!op) {
1444 PyErr_SetString(PyExc_ValueError,
1445 "field op is required for BoolOp");
1446 return NULL;
1447 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001448 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001449 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 p->kind = BoolOp_kind;
1452 p->v.BoolOp.op = op;
1453 p->v.BoolOp.values = values;
1454 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001455 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 return p;
1457}
1458
1459expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001460BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1461 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
1463 expr_ty p;
1464 if (!left) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "field left is required for BinOp");
1467 return NULL;
1468 }
1469 if (!op) {
1470 PyErr_SetString(PyExc_ValueError,
1471 "field op is required for BinOp");
1472 return NULL;
1473 }
1474 if (!right) {
1475 PyErr_SetString(PyExc_ValueError,
1476 "field right is required for BinOp");
1477 return NULL;
1478 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001479 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001480 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 p->kind = BinOp_kind;
1483 p->v.BinOp.left = left;
1484 p->v.BinOp.op = op;
1485 p->v.BinOp.right = right;
1486 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001487 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 return p;
1489}
1490
1491expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001492UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1493 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494{
1495 expr_ty p;
1496 if (!op) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "field op is required for UnaryOp");
1499 return NULL;
1500 }
1501 if (!operand) {
1502 PyErr_SetString(PyExc_ValueError,
1503 "field operand is required for UnaryOp");
1504 return NULL;
1505 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001506 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001507 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 p->kind = UnaryOp_kind;
1510 p->v.UnaryOp.op = op;
1511 p->v.UnaryOp.operand = operand;
1512 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001513 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 return p;
1515}
1516
1517expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001518Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1519 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520{
1521 expr_ty p;
1522 if (!args) {
1523 PyErr_SetString(PyExc_ValueError,
1524 "field args is required for Lambda");
1525 return NULL;
1526 }
1527 if (!body) {
1528 PyErr_SetString(PyExc_ValueError,
1529 "field body is required for Lambda");
1530 return NULL;
1531 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001532 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001533 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 p->kind = Lambda_kind;
1536 p->v.Lambda.args = args;
1537 p->v.Lambda.body = body;
1538 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001539 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 return p;
1541}
1542
1543expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001544IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1545 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001546{
1547 expr_ty p;
1548 if (!test) {
1549 PyErr_SetString(PyExc_ValueError,
1550 "field test is required for IfExp");
1551 return NULL;
1552 }
1553 if (!body) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "field body is required for IfExp");
1556 return NULL;
1557 }
1558 if (!orelse) {
1559 PyErr_SetString(PyExc_ValueError,
1560 "field orelse is required for IfExp");
1561 return NULL;
1562 }
1563 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001564 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001565 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001566 p->kind = IfExp_kind;
1567 p->v.IfExp.test = test;
1568 p->v.IfExp.body = body;
1569 p->v.IfExp.orelse = orelse;
1570 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001571 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001572 return p;
1573}
1574
1575expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001576Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1577 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578{
1579 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001580 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001581 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 p->kind = Dict_kind;
1584 p->v.Dict.keys = keys;
1585 p->v.Dict.values = values;
1586 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001587 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 return p;
1589}
1590
1591expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001592Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1593{
1594 expr_ty p;
1595 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001596 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001597 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001598 p->kind = Set_kind;
1599 p->v.Set.elts = elts;
1600 p->lineno = lineno;
1601 p->col_offset = col_offset;
1602 return p;
1603}
1604
1605expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001606ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1607 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608{
1609 expr_ty p;
1610 if (!elt) {
1611 PyErr_SetString(PyExc_ValueError,
1612 "field elt is required for ListComp");
1613 return NULL;
1614 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001615 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001616 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 p->kind = ListComp_kind;
1619 p->v.ListComp.elt = elt;
1620 p->v.ListComp.generators = generators;
1621 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001622 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 return p;
1624}
1625
1626expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001627SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1628 *arena)
1629{
1630 expr_ty p;
1631 if (!elt) {
1632 PyErr_SetString(PyExc_ValueError,
1633 "field elt is required for SetComp");
1634 return NULL;
1635 }
1636 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1637 if (!p)
1638 return NULL;
1639 p->kind = SetComp_kind;
1640 p->v.SetComp.elt = elt;
1641 p->v.SetComp.generators = generators;
1642 p->lineno = lineno;
1643 p->col_offset = col_offset;
1644 return p;
1645}
1646
1647expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001648DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1649 col_offset, PyArena *arena)
1650{
1651 expr_ty p;
1652 if (!key) {
1653 PyErr_SetString(PyExc_ValueError,
1654 "field key is required for DictComp");
1655 return NULL;
1656 }
1657 if (!value) {
1658 PyErr_SetString(PyExc_ValueError,
1659 "field value is required for DictComp");
1660 return NULL;
1661 }
1662 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1663 if (!p)
1664 return NULL;
1665 p->kind = DictComp_kind;
1666 p->v.DictComp.key = key;
1667 p->v.DictComp.value = value;
1668 p->v.DictComp.generators = generators;
1669 p->lineno = lineno;
1670 p->col_offset = col_offset;
1671 return p;
1672}
1673
1674expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001675GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1676 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677{
1678 expr_ty p;
1679 if (!elt) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field elt is required for GeneratorExp");
1682 return NULL;
1683 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001685 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 p->kind = GeneratorExp_kind;
1688 p->v.GeneratorExp.elt = elt;
1689 p->v.GeneratorExp.generators = generators;
1690 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001691 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 return p;
1693}
1694
1695expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697{
1698 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001699 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001700 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 p->kind = Yield_kind;
1703 p->v.Yield.value = value;
1704 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001705 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 return p;
1707}
1708
1709expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001710Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1711 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712{
1713 expr_ty p;
1714 if (!left) {
1715 PyErr_SetString(PyExc_ValueError,
1716 "field left is required for Compare");
1717 return NULL;
1718 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001719 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001720 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 p->kind = Compare_kind;
1723 p->v.Compare.left = left;
1724 p->v.Compare.ops = ops;
1725 p->v.Compare.comparators = comparators;
1726 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001727 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 return p;
1729}
1730
1731expr_ty
1732Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001733 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734{
1735 expr_ty p;
1736 if (!func) {
1737 PyErr_SetString(PyExc_ValueError,
1738 "field func is required for Call");
1739 return NULL;
1740 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001741 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001742 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 p->kind = Call_kind;
1745 p->v.Call.func = func;
1746 p->v.Call.args = args;
1747 p->v.Call.keywords = keywords;
1748 p->v.Call.starargs = starargs;
1749 p->v.Call.kwargs = kwargs;
1750 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001751 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752 return p;
1753}
1754
1755expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001756Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757{
1758 expr_ty p;
1759 if (!n) {
1760 PyErr_SetString(PyExc_ValueError,
1761 "field n is required for Num");
1762 return NULL;
1763 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001764 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001765 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 p->kind = Num_kind;
1768 p->v.Num.n = n;
1769 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001770 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 return p;
1772}
1773
1774expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001775Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776{
1777 expr_ty p;
1778 if (!s) {
1779 PyErr_SetString(PyExc_ValueError,
1780 "field s is required for Str");
1781 return NULL;
1782 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001783 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001784 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786 p->kind = Str_kind;
1787 p->v.Str.s = s;
1788 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001789 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 return p;
1791}
1792
1793expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001794Bytes(string s, int lineno, int col_offset, PyArena *arena)
1795{
1796 expr_ty p;
1797 if (!s) {
1798 PyErr_SetString(PyExc_ValueError,
1799 "field s is required for Bytes");
1800 return NULL;
1801 }
1802 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001803 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001804 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001805 p->kind = Bytes_kind;
1806 p->v.Bytes.s = s;
1807 p->lineno = lineno;
1808 p->col_offset = col_offset;
1809 return p;
1810}
1811
1812expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001813Ellipsis(int lineno, int col_offset, PyArena *arena)
1814{
1815 expr_ty p;
1816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001817 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001818 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001819 p->kind = Ellipsis_kind;
1820 p->lineno = lineno;
1821 p->col_offset = col_offset;
1822 return p;
1823}
1824
1825expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001826Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1827 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828{
1829 expr_ty p;
1830 if (!value) {
1831 PyErr_SetString(PyExc_ValueError,
1832 "field value is required for Attribute");
1833 return NULL;
1834 }
1835 if (!attr) {
1836 PyErr_SetString(PyExc_ValueError,
1837 "field attr is required for Attribute");
1838 return NULL;
1839 }
1840 if (!ctx) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field ctx is required for Attribute");
1843 return NULL;
1844 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001846 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 p->kind = Attribute_kind;
1849 p->v.Attribute.value = value;
1850 p->v.Attribute.attr = attr;
1851 p->v.Attribute.ctx = ctx;
1852 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001853 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 return p;
1855}
1856
1857expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001858Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1859 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860{
1861 expr_ty p;
1862 if (!value) {
1863 PyErr_SetString(PyExc_ValueError,
1864 "field value is required for Subscript");
1865 return NULL;
1866 }
1867 if (!slice) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field slice is required for Subscript");
1870 return NULL;
1871 }
1872 if (!ctx) {
1873 PyErr_SetString(PyExc_ValueError,
1874 "field ctx is required for Subscript");
1875 return NULL;
1876 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001877 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001878 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 p->kind = Subscript_kind;
1881 p->v.Subscript.value = value;
1882 p->v.Subscript.slice = slice;
1883 p->v.Subscript.ctx = ctx;
1884 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001885 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 return p;
1887}
1888
1889expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001890Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1891 *arena)
1892{
1893 expr_ty p;
1894 if (!value) {
1895 PyErr_SetString(PyExc_ValueError,
1896 "field value is required for Starred");
1897 return NULL;
1898 }
1899 if (!ctx) {
1900 PyErr_SetString(PyExc_ValueError,
1901 "field ctx is required for Starred");
1902 return NULL;
1903 }
1904 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1905 if (!p)
1906 return NULL;
1907 p->kind = Starred_kind;
1908 p->v.Starred.value = value;
1909 p->v.Starred.ctx = ctx;
1910 p->lineno = lineno;
1911 p->col_offset = col_offset;
1912 return p;
1913}
1914
1915expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001916Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1917 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918{
1919 expr_ty p;
1920 if (!id) {
1921 PyErr_SetString(PyExc_ValueError,
1922 "field id is required for Name");
1923 return NULL;
1924 }
1925 if (!ctx) {
1926 PyErr_SetString(PyExc_ValueError,
1927 "field ctx is required for Name");
1928 return NULL;
1929 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001930 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001931 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 p->kind = Name_kind;
1934 p->v.Name.id = id;
1935 p->v.Name.ctx = ctx;
1936 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001937 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 return p;
1939}
1940
1941expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001942List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1943 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944{
1945 expr_ty p;
1946 if (!ctx) {
1947 PyErr_SetString(PyExc_ValueError,
1948 "field ctx is required for List");
1949 return NULL;
1950 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001951 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001952 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 p->kind = List_kind;
1955 p->v.List.elts = elts;
1956 p->v.List.ctx = ctx;
1957 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001958 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 return p;
1960}
1961
1962expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001963Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1964 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965{
1966 expr_ty p;
1967 if (!ctx) {
1968 PyErr_SetString(PyExc_ValueError,
1969 "field ctx is required for Tuple");
1970 return NULL;
1971 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001972 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001973 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 p->kind = Tuple_kind;
1976 p->v.Tuple.elts = elts;
1977 p->v.Tuple.ctx = ctx;
1978 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001979 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 return p;
1981}
1982
1983slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001984Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985{
1986 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001987 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001988 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 p->kind = Slice_kind;
1991 p->v.Slice.lower = lower;
1992 p->v.Slice.upper = upper;
1993 p->v.Slice.step = step;
1994 return p;
1995}
1996
1997slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001998ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999{
2000 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002001 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002002 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 p->kind = ExtSlice_kind;
2005 p->v.ExtSlice.dims = dims;
2006 return p;
2007}
2008
2009slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011{
2012 slice_ty p;
2013 if (!value) {
2014 PyErr_SetString(PyExc_ValueError,
2015 "field value is required for Index");
2016 return NULL;
2017 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002018 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002019 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 p->kind = Index_kind;
2022 p->v.Index.value = value;
2023 return p;
2024}
2025
2026comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002027comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028{
2029 comprehension_ty p;
2030 if (!target) {
2031 PyErr_SetString(PyExc_ValueError,
2032 "field target is required for comprehension");
2033 return NULL;
2034 }
2035 if (!iter) {
2036 PyErr_SetString(PyExc_ValueError,
2037 "field iter is required for comprehension");
2038 return NULL;
2039 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002040 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002041 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 p->target = target;
2044 p->iter = iter;
2045 p->ifs = ifs;
2046 return p;
2047}
2048
2049excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002050ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002051 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052{
2053 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002054 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002055 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002057 p->kind = ExceptHandler_kind;
2058 p->v.ExceptHandler.type = type;
2059 p->v.ExceptHandler.name = name;
2060 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002061 p->lineno = lineno;
2062 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 return p;
2064}
2065
2066arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002067arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2068 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2069 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070{
2071 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002072 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002073 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 p->args = args;
2076 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002077 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002078 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002080 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002082 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 return p;
2084}
2085
Neal Norwitzc1505362006-12-28 06:47:50 +00002086arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002087arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002088{
2089 arg_ty p;
2090 if (!arg) {
2091 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002092 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002093 return NULL;
2094 }
2095 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002096 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002097 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002098 p->arg = arg;
2099 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002100 return p;
2101}
2102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105{
2106 keyword_ty p;
2107 if (!arg) {
2108 PyErr_SetString(PyExc_ValueError,
2109 "field arg is required for keyword");
2110 return NULL;
2111 }
2112 if (!value) {
2113 PyErr_SetString(PyExc_ValueError,
2114 "field value is required for keyword");
2115 return NULL;
2116 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002117 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002118 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 p->arg = arg;
2121 p->value = value;
2122 return p;
2123}
2124
2125alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002126alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127{
2128 alias_ty p;
2129 if (!name) {
2130 PyErr_SetString(PyExc_ValueError,
2131 "field name is required for alias");
2132 return NULL;
2133 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002134 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002135 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 p->name = name;
2138 p->asname = asname;
2139 return p;
2140}
2141
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002142
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002143PyObject*
2144ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002145{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002146 mod_ty o = (mod_ty)_o;
2147 PyObject *result = NULL, *value = NULL;
2148 if (!o) {
2149 Py_INCREF(Py_None);
2150 return Py_None;
2151 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 switch (o->kind) {
2154 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002155 result = PyType_GenericNew(Module_type, NULL, NULL);
2156 if (!result) goto failed;
2157 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2158 if (!value) goto failed;
2159 if (PyObject_SetAttrString(result, "body", value) == -1)
2160 goto failed;
2161 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 break;
2163 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002164 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2165 if (!result) goto failed;
2166 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2167 if (!value) goto failed;
2168 if (PyObject_SetAttrString(result, "body", value) == -1)
2169 goto failed;
2170 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 break;
2172 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002173 result = PyType_GenericNew(Expression_type, NULL, NULL);
2174 if (!result) goto failed;
2175 value = ast2obj_expr(o->v.Expression.body);
2176 if (!value) goto failed;
2177 if (PyObject_SetAttrString(result, "body", value) == -1)
2178 goto failed;
2179 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 break;
2181 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002182 result = PyType_GenericNew(Suite_type, NULL, NULL);
2183 if (!result) goto failed;
2184 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2185 if (!value) goto failed;
2186 if (PyObject_SetAttrString(result, "body", value) == -1)
2187 goto failed;
2188 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 break;
2190 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002191 return result;
2192failed:
2193 Py_XDECREF(value);
2194 Py_XDECREF(result);
2195 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196}
2197
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002198PyObject*
2199ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002201 stmt_ty o = (stmt_ty)_o;
2202 PyObject *result = NULL, *value = NULL;
2203 if (!o) {
2204 Py_INCREF(Py_None);
2205 return Py_None;
2206 }
2207
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 switch (o->kind) {
2209 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002210 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2211 if (!result) goto failed;
2212 value = ast2obj_identifier(o->v.FunctionDef.name);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "name", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
2217 value = ast2obj_arguments(o->v.FunctionDef.args);
2218 if (!value) goto failed;
2219 if (PyObject_SetAttrString(result, "args", value) == -1)
2220 goto failed;
2221 Py_DECREF(value);
2222 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2223 if (!value) goto failed;
2224 if (PyObject_SetAttrString(result, "body", value) == -1)
2225 goto failed;
2226 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002227 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2228 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002229 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002230 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2231 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002232 goto failed;
2233 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002234 value = ast2obj_expr(o->v.FunctionDef.returns);
2235 if (!value) goto failed;
2236 if (PyObject_SetAttrString(result, "returns", value) == -1)
2237 goto failed;
2238 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 break;
2240 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002241 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2242 if (!result) goto failed;
2243 value = ast2obj_identifier(o->v.ClassDef.name);
2244 if (!value) goto failed;
2245 if (PyObject_SetAttrString(result, "name", value) == -1)
2246 goto failed;
2247 Py_DECREF(value);
2248 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2249 if (!value) goto failed;
2250 if (PyObject_SetAttrString(result, "bases", value) == -1)
2251 goto failed;
2252 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002253 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2254 if (!value) goto failed;
2255 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2256 goto failed;
2257 Py_DECREF(value);
2258 value = ast2obj_expr(o->v.ClassDef.starargs);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
2263 value = ast2obj_expr(o->v.ClassDef.kwargs);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2266 goto failed;
2267 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002268 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2269 if (!value) goto failed;
2270 if (PyObject_SetAttrString(result, "body", value) == -1)
2271 goto failed;
2272 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002273 value = ast2obj_list(o->v.ClassDef.decorator_list,
2274 ast2obj_expr);
2275 if (!value) goto failed;
2276 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2277 -1)
2278 goto failed;
2279 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 break;
2281 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002282 result = PyType_GenericNew(Return_type, NULL, NULL);
2283 if (!result) goto failed;
2284 value = ast2obj_expr(o->v.Return.value);
2285 if (!value) goto failed;
2286 if (PyObject_SetAttrString(result, "value", value) == -1)
2287 goto failed;
2288 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 break;
2290 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002291 result = PyType_GenericNew(Delete_type, NULL, NULL);
2292 if (!result) goto failed;
2293 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2294 if (!value) goto failed;
2295 if (PyObject_SetAttrString(result, "targets", value) == -1)
2296 goto failed;
2297 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 break;
2299 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002300 result = PyType_GenericNew(Assign_type, NULL, NULL);
2301 if (!result) goto failed;
2302 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2303 if (!value) goto failed;
2304 if (PyObject_SetAttrString(result, "targets", value) == -1)
2305 goto failed;
2306 Py_DECREF(value);
2307 value = ast2obj_expr(o->v.Assign.value);
2308 if (!value) goto failed;
2309 if (PyObject_SetAttrString(result, "value", value) == -1)
2310 goto failed;
2311 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 break;
2313 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002314 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2315 if (!result) goto failed;
2316 value = ast2obj_expr(o->v.AugAssign.target);
2317 if (!value) goto failed;
2318 if (PyObject_SetAttrString(result, "target", value) == -1)
2319 goto failed;
2320 Py_DECREF(value);
2321 value = ast2obj_operator(o->v.AugAssign.op);
2322 if (!value) goto failed;
2323 if (PyObject_SetAttrString(result, "op", value) == -1)
2324 goto failed;
2325 Py_DECREF(value);
2326 value = ast2obj_expr(o->v.AugAssign.value);
2327 if (!value) goto failed;
2328 if (PyObject_SetAttrString(result, "value", value) == -1)
2329 goto failed;
2330 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002333 result = PyType_GenericNew(For_type, NULL, NULL);
2334 if (!result) goto failed;
2335 value = ast2obj_expr(o->v.For.target);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "target", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
2340 value = ast2obj_expr(o->v.For.iter);
2341 if (!value) goto failed;
2342 if (PyObject_SetAttrString(result, "iter", value) == -1)
2343 goto failed;
2344 Py_DECREF(value);
2345 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2346 if (!value) goto failed;
2347 if (PyObject_SetAttrString(result, "body", value) == -1)
2348 goto failed;
2349 Py_DECREF(value);
2350 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2351 if (!value) goto failed;
2352 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 break;
2356 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002357 result = PyType_GenericNew(While_type, NULL, NULL);
2358 if (!result) goto failed;
2359 value = ast2obj_expr(o->v.While.test);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "test", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
2364 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2365 if (!value) goto failed;
2366 if (PyObject_SetAttrString(result, "body", value) == -1)
2367 goto failed;
2368 Py_DECREF(value);
2369 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2370 if (!value) goto failed;
2371 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2372 goto failed;
2373 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 break;
2375 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002376 result = PyType_GenericNew(If_type, NULL, NULL);
2377 if (!result) goto failed;
2378 value = ast2obj_expr(o->v.If.test);
2379 if (!value) goto failed;
2380 if (PyObject_SetAttrString(result, "test", value) == -1)
2381 goto failed;
2382 Py_DECREF(value);
2383 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2384 if (!value) goto failed;
2385 if (PyObject_SetAttrString(result, "body", value) == -1)
2386 goto failed;
2387 Py_DECREF(value);
2388 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2389 if (!value) goto failed;
2390 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2391 goto failed;
2392 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002394 case With_kind:
2395 result = PyType_GenericNew(With_type, NULL, NULL);
2396 if (!result) goto failed;
2397 value = ast2obj_expr(o->v.With.context_expr);
2398 if (!value) goto failed;
2399 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2400 goto failed;
2401 Py_DECREF(value);
2402 value = ast2obj_expr(o->v.With.optional_vars);
2403 if (!value) goto failed;
2404 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2405 -1)
2406 goto failed;
2407 Py_DECREF(value);
2408 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2409 if (!value) goto failed;
2410 if (PyObject_SetAttrString(result, "body", value) == -1)
2411 goto failed;
2412 Py_DECREF(value);
2413 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002415 result = PyType_GenericNew(Raise_type, NULL, NULL);
2416 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002417 value = ast2obj_expr(o->v.Raise.exc);
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, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002420 goto failed;
2421 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002422 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002423 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002424 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002425 goto failed;
2426 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 break;
2428 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2430 if (!result) goto failed;
2431 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2432 if (!value) goto failed;
2433 if (PyObject_SetAttrString(result, "body", value) == -1)
2434 goto failed;
2435 Py_DECREF(value);
2436 value = ast2obj_list(o->v.TryExcept.handlers,
2437 ast2obj_excepthandler);
2438 if (!value) goto failed;
2439 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2440 goto failed;
2441 Py_DECREF(value);
2442 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2443 if (!value) goto failed;
2444 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 break;
2448 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002449 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2450 if (!result) goto failed;
2451 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2452 if (!value) goto failed;
2453 if (PyObject_SetAttrString(result, "body", value) == -1)
2454 goto failed;
2455 Py_DECREF(value);
2456 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2457 if (!value) goto failed;
2458 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2459 goto failed;
2460 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 break;
2462 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002463 result = PyType_GenericNew(Assert_type, NULL, NULL);
2464 if (!result) goto failed;
2465 value = ast2obj_expr(o->v.Assert.test);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "test", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
2470 value = ast2obj_expr(o->v.Assert.msg);
2471 if (!value) goto failed;
2472 if (PyObject_SetAttrString(result, "msg", value) == -1)
2473 goto failed;
2474 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 break;
2476 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002477 result = PyType_GenericNew(Import_type, NULL, NULL);
2478 if (!result) goto failed;
2479 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2480 if (!value) goto failed;
2481 if (PyObject_SetAttrString(result, "names", value) == -1)
2482 goto failed;
2483 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 break;
2485 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002486 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2487 if (!result) goto failed;
2488 value = ast2obj_identifier(o->v.ImportFrom.module);
2489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "module", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
2493 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "names", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002498 value = ast2obj_int(o->v.ImportFrom.level);
2499 if (!value) goto failed;
2500 if (PyObject_SetAttrString(result, "level", value) == -1)
2501 goto failed;
2502 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002505 result = PyType_GenericNew(Global_type, NULL, NULL);
2506 if (!result) goto failed;
2507 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2508 if (!value) goto failed;
2509 if (PyObject_SetAttrString(result, "names", value) == -1)
2510 goto failed;
2511 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002513 case Nonlocal_kind:
2514 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2515 if (!result) goto failed;
2516 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "names", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
2521 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002523 result = PyType_GenericNew(Expr_type, NULL, NULL);
2524 if (!result) goto failed;
2525 value = ast2obj_expr(o->v.Expr.value);
2526 if (!value) goto failed;
2527 if (PyObject_SetAttrString(result, "value", value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 break;
2531 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002532 result = PyType_GenericNew(Pass_type, NULL, NULL);
2533 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 break;
2535 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002536 result = PyType_GenericNew(Break_type, NULL, NULL);
2537 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 break;
2539 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002540 result = PyType_GenericNew(Continue_type, NULL, NULL);
2541 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 break;
2543 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002544 value = ast2obj_int(o->lineno);
2545 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002546 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2547 goto failed;
2548 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002549 value = ast2obj_int(o->col_offset);
2550 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002551 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2552 goto failed;
2553 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002554 return result;
2555failed:
2556 Py_XDECREF(value);
2557 Py_XDECREF(result);
2558 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559}
2560
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002561PyObject*
2562ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002564 expr_ty o = (expr_ty)_o;
2565 PyObject *result = NULL, *value = NULL;
2566 if (!o) {
2567 Py_INCREF(Py_None);
2568 return Py_None;
2569 }
2570
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 switch (o->kind) {
2572 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002573 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2574 if (!result) goto failed;
2575 value = ast2obj_boolop(o->v.BoolOp.op);
2576 if (!value) goto failed;
2577 if (PyObject_SetAttrString(result, "op", value) == -1)
2578 goto failed;
2579 Py_DECREF(value);
2580 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2581 if (!value) goto failed;
2582 if (PyObject_SetAttrString(result, "values", value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 break;
2586 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002587 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2588 if (!result) goto failed;
2589 value = ast2obj_expr(o->v.BinOp.left);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "left", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 value = ast2obj_operator(o->v.BinOp.op);
2595 if (!value) goto failed;
2596 if (PyObject_SetAttrString(result, "op", value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 value = ast2obj_expr(o->v.BinOp.right);
2600 if (!value) goto failed;
2601 if (PyObject_SetAttrString(result, "right", value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 break;
2605 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002606 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2607 if (!result) goto failed;
2608 value = ast2obj_unaryop(o->v.UnaryOp.op);
2609 if (!value) goto failed;
2610 if (PyObject_SetAttrString(result, "op", value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 value = ast2obj_expr(o->v.UnaryOp.operand);
2614 if (!value) goto failed;
2615 if (PyObject_SetAttrString(result, "operand", value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 break;
2619 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002620 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2621 if (!result) goto failed;
2622 value = ast2obj_arguments(o->v.Lambda.args);
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "args", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 value = ast2obj_expr(o->v.Lambda.body);
2628 if (!value) goto failed;
2629 if (PyObject_SetAttrString(result, "body", value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002633 case IfExp_kind:
2634 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2635 if (!result) goto failed;
2636 value = ast2obj_expr(o->v.IfExp.test);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "test", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
2641 value = ast2obj_expr(o->v.IfExp.body);
2642 if (!value) goto failed;
2643 if (PyObject_SetAttrString(result, "body", value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 value = ast2obj_expr(o->v.IfExp.orelse);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002653 result = PyType_GenericNew(Dict_type, NULL, NULL);
2654 if (!result) goto failed;
2655 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2656 if (!value) goto failed;
2657 if (PyObject_SetAttrString(result, "keys", value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
2660 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2661 if (!value) goto failed;
2662 if (PyObject_SetAttrString(result, "values", value) == -1)
2663 goto failed;
2664 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002666 case Set_kind:
2667 result = PyType_GenericNew(Set_type, NULL, NULL);
2668 if (!result) goto failed;
2669 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2670 if (!value) goto failed;
2671 if (PyObject_SetAttrString(result, "elts", value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002676 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_expr(o->v.ListComp.elt);
2679 if (!value) goto failed;
2680 if (PyObject_SetAttrString(result, "elt", value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_list(o->v.ListComp.generators,
2684 ast2obj_comprehension);
2685 if (!value) goto failed;
2686 if (PyObject_SetAttrString(result, "generators", value) == -1)
2687 goto failed;
2688 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002690 case SetComp_kind:
2691 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2692 if (!result) goto failed;
2693 value = ast2obj_expr(o->v.SetComp.elt);
2694 if (!value) goto failed;
2695 if (PyObject_SetAttrString(result, "elt", value) == -1)
2696 goto failed;
2697 Py_DECREF(value);
2698 value = ast2obj_list(o->v.SetComp.generators,
2699 ast2obj_comprehension);
2700 if (!value) goto failed;
2701 if (PyObject_SetAttrString(result, "generators", value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002705 case DictComp_kind:
2706 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2707 if (!result) goto failed;
2708 value = ast2obj_expr(o->v.DictComp.key);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "key", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
2713 value = ast2obj_expr(o->v.DictComp.value);
2714 if (!value) goto failed;
2715 if (PyObject_SetAttrString(result, "value", value) == -1)
2716 goto failed;
2717 Py_DECREF(value);
2718 value = ast2obj_list(o->v.DictComp.generators,
2719 ast2obj_comprehension);
2720 if (!value) goto failed;
2721 if (PyObject_SetAttrString(result, "generators", value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002726 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2727 if (!result) goto failed;
2728 value = ast2obj_expr(o->v.GeneratorExp.elt);
2729 if (!value) goto failed;
2730 if (PyObject_SetAttrString(result, "elt", value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 value = ast2obj_list(o->v.GeneratorExp.generators,
2734 ast2obj_comprehension);
2735 if (!value) goto failed;
2736 if (PyObject_SetAttrString(result, "generators", value) == -1)
2737 goto failed;
2738 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 break;
2740 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002741 result = PyType_GenericNew(Yield_type, NULL, NULL);
2742 if (!result) goto failed;
2743 value = ast2obj_expr(o->v.Yield.value);
2744 if (!value) goto failed;
2745 if (PyObject_SetAttrString(result, "value", value) == -1)
2746 goto failed;
2747 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 break;
2749 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002750 result = PyType_GenericNew(Compare_type, NULL, NULL);
2751 if (!result) goto failed;
2752 value = ast2obj_expr(o->v.Compare.left);
2753 if (!value) goto failed;
2754 if (PyObject_SetAttrString(result, "left", value) == -1)
2755 goto failed;
2756 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002757 {
2758 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2759 value = PyList_New(n);
2760 if (!value) goto failed;
2761 for(i = 0; i < n; i++)
2762 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2763 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "ops", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
2768 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2769 if (!value) goto failed;
2770 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 break;
2774 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002775 result = PyType_GenericNew(Call_type, NULL, NULL);
2776 if (!result) goto failed;
2777 value = ast2obj_expr(o->v.Call.func);
2778 if (!value) goto failed;
2779 if (PyObject_SetAttrString(result, "func", value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
2782 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2783 if (!value) goto failed;
2784 if (PyObject_SetAttrString(result, "args", value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2788 if (!value) goto failed;
2789 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 value = ast2obj_expr(o->v.Call.starargs);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 value = ast2obj_expr(o->v.Call.kwargs);
2798 if (!value) goto failed;
2799 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002804 result = PyType_GenericNew(Num_type, NULL, NULL);
2805 if (!result) goto failed;
2806 value = ast2obj_object(o->v.Num.n);
2807 if (!value) goto failed;
2808 if (PyObject_SetAttrString(result, "n", value) == -1)
2809 goto failed;
2810 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 break;
2812 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002813 result = PyType_GenericNew(Str_type, NULL, NULL);
2814 if (!result) goto failed;
2815 value = ast2obj_string(o->v.Str.s);
2816 if (!value) goto failed;
2817 if (PyObject_SetAttrString(result, "s", value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002821 case Bytes_kind:
2822 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2823 if (!result) goto failed;
2824 value = ast2obj_string(o->v.Bytes.s);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "s", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002830 case Ellipsis_kind:
2831 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2832 if (!result) goto failed;
2833 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002835 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2836 if (!result) goto failed;
2837 value = ast2obj_expr(o->v.Attribute.value);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "value", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_identifier(o->v.Attribute.attr);
2843 if (!value) goto failed;
2844 if (PyObject_SetAttrString(result, "attr", value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 value = ast2obj_expr_context(o->v.Attribute.ctx);
2848 if (!value) goto failed;
2849 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 break;
2853 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002854 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2855 if (!result) goto failed;
2856 value = ast2obj_expr(o->v.Subscript.value);
2857 if (!value) goto failed;
2858 if (PyObject_SetAttrString(result, "value", value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_slice(o->v.Subscript.slice);
2862 if (!value) goto failed;
2863 if (PyObject_SetAttrString(result, "slice", value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 value = ast2obj_expr_context(o->v.Subscript.ctx);
2867 if (!value) goto failed;
2868 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002872 case Starred_kind:
2873 result = PyType_GenericNew(Starred_type, NULL, NULL);
2874 if (!result) goto failed;
2875 value = ast2obj_expr(o->v.Starred.value);
2876 if (!value) goto failed;
2877 if (PyObject_SetAttrString(result, "value", value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 value = ast2obj_expr_context(o->v.Starred.ctx);
2881 if (!value) goto failed;
2882 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002887 result = PyType_GenericNew(Name_type, NULL, NULL);
2888 if (!result) goto failed;
2889 value = ast2obj_identifier(o->v.Name.id);
2890 if (!value) goto failed;
2891 if (PyObject_SetAttrString(result, "id", value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 value = ast2obj_expr_context(o->v.Name.ctx);
2895 if (!value) goto failed;
2896 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 break;
2900 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002901 result = PyType_GenericNew(List_type, NULL, NULL);
2902 if (!result) goto failed;
2903 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2904 if (!value) goto failed;
2905 if (PyObject_SetAttrString(result, "elts", value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 value = ast2obj_expr_context(o->v.List.ctx);
2909 if (!value) goto failed;
2910 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2911 goto failed;
2912 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 break;
2914 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002915 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2916 if (!result) goto failed;
2917 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2918 if (!value) goto failed;
2919 if (PyObject_SetAttrString(result, "elts", value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
2922 value = ast2obj_expr_context(o->v.Tuple.ctx);
2923 if (!value) goto failed;
2924 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2925 goto failed;
2926 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 break;
2928 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002929 value = ast2obj_int(o->lineno);
2930 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002931 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2932 goto failed;
2933 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002934 value = ast2obj_int(o->col_offset);
2935 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002936 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2937 goto failed;
2938 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002939 return result;
2940failed:
2941 Py_XDECREF(value);
2942 Py_XDECREF(result);
2943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944}
2945
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002946PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002948 switch(o) {
2949 case Load:
2950 Py_INCREF(Load_singleton);
2951 return Load_singleton;
2952 case Store:
2953 Py_INCREF(Store_singleton);
2954 return Store_singleton;
2955 case Del:
2956 Py_INCREF(Del_singleton);
2957 return Del_singleton;
2958 case AugLoad:
2959 Py_INCREF(AugLoad_singleton);
2960 return AugLoad_singleton;
2961 case AugStore:
2962 Py_INCREF(AugStore_singleton);
2963 return AugStore_singleton;
2964 case Param:
2965 Py_INCREF(Param_singleton);
2966 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002967 default:
2968 /* should never happen, but just in case ... */
2969 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002973PyObject*
2974ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002976 slice_ty o = (slice_ty)_o;
2977 PyObject *result = NULL, *value = NULL;
2978 if (!o) {
2979 Py_INCREF(Py_None);
2980 return Py_None;
2981 }
2982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002985 result = PyType_GenericNew(Slice_type, NULL, NULL);
2986 if (!result) goto failed;
2987 value = ast2obj_expr(o->v.Slice.lower);
2988 if (!value) goto failed;
2989 if (PyObject_SetAttrString(result, "lower", value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_expr(o->v.Slice.upper);
2993 if (!value) goto failed;
2994 if (PyObject_SetAttrString(result, "upper", value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
2997 value = ast2obj_expr(o->v.Slice.step);
2998 if (!value) goto failed;
2999 if (PyObject_SetAttrString(result, "step", value) == -1)
3000 goto failed;
3001 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002 break;
3003 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003004 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3005 if (!result) goto failed;
3006 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3007 if (!value) goto failed;
3008 if (PyObject_SetAttrString(result, "dims", value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 break;
3012 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003013 result = PyType_GenericNew(Index_type, NULL, NULL);
3014 if (!result) goto failed;
3015 value = ast2obj_expr(o->v.Index.value);
3016 if (!value) goto failed;
3017 if (PyObject_SetAttrString(result, "value", value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 break;
3021 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003022 return result;
3023failed:
3024 Py_XDECREF(value);
3025 Py_XDECREF(result);
3026 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027}
3028
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003029PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003031 switch(o) {
3032 case And:
3033 Py_INCREF(And_singleton);
3034 return And_singleton;
3035 case Or:
3036 Py_INCREF(Or_singleton);
3037 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003038 default:
3039 /* should never happen, but just in case ... */
3040 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003043}
3044PyObject* ast2obj_operator(operator_ty o)
3045{
3046 switch(o) {
3047 case Add:
3048 Py_INCREF(Add_singleton);
3049 return Add_singleton;
3050 case Sub:
3051 Py_INCREF(Sub_singleton);
3052 return Sub_singleton;
3053 case Mult:
3054 Py_INCREF(Mult_singleton);
3055 return Mult_singleton;
3056 case Div:
3057 Py_INCREF(Div_singleton);
3058 return Div_singleton;
3059 case Mod:
3060 Py_INCREF(Mod_singleton);
3061 return Mod_singleton;
3062 case Pow:
3063 Py_INCREF(Pow_singleton);
3064 return Pow_singleton;
3065 case LShift:
3066 Py_INCREF(LShift_singleton);
3067 return LShift_singleton;
3068 case RShift:
3069 Py_INCREF(RShift_singleton);
3070 return RShift_singleton;
3071 case BitOr:
3072 Py_INCREF(BitOr_singleton);
3073 return BitOr_singleton;
3074 case BitXor:
3075 Py_INCREF(BitXor_singleton);
3076 return BitXor_singleton;
3077 case BitAnd:
3078 Py_INCREF(BitAnd_singleton);
3079 return BitAnd_singleton;
3080 case FloorDiv:
3081 Py_INCREF(FloorDiv_singleton);
3082 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003083 default:
3084 /* should never happen, but just in case ... */
3085 PyErr_Format(PyExc_SystemError, "unknown operator found");
3086 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003087 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003088}
3089PyObject* ast2obj_unaryop(unaryop_ty o)
3090{
3091 switch(o) {
3092 case Invert:
3093 Py_INCREF(Invert_singleton);
3094 return Invert_singleton;
3095 case Not:
3096 Py_INCREF(Not_singleton);
3097 return Not_singleton;
3098 case UAdd:
3099 Py_INCREF(UAdd_singleton);
3100 return UAdd_singleton;
3101 case USub:
3102 Py_INCREF(USub_singleton);
3103 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003104 default:
3105 /* should never happen, but just in case ... */
3106 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3107 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003108 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003109}
3110PyObject* ast2obj_cmpop(cmpop_ty o)
3111{
3112 switch(o) {
3113 case Eq:
3114 Py_INCREF(Eq_singleton);
3115 return Eq_singleton;
3116 case NotEq:
3117 Py_INCREF(NotEq_singleton);
3118 return NotEq_singleton;
3119 case Lt:
3120 Py_INCREF(Lt_singleton);
3121 return Lt_singleton;
3122 case LtE:
3123 Py_INCREF(LtE_singleton);
3124 return LtE_singleton;
3125 case Gt:
3126 Py_INCREF(Gt_singleton);
3127 return Gt_singleton;
3128 case GtE:
3129 Py_INCREF(GtE_singleton);
3130 return GtE_singleton;
3131 case Is:
3132 Py_INCREF(Is_singleton);
3133 return Is_singleton;
3134 case IsNot:
3135 Py_INCREF(IsNot_singleton);
3136 return IsNot_singleton;
3137 case In:
3138 Py_INCREF(In_singleton);
3139 return In_singleton;
3140 case NotIn:
3141 Py_INCREF(NotIn_singleton);
3142 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003143 default:
3144 /* should never happen, but just in case ... */
3145 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3146 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003147 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003148}
3149PyObject*
3150ast2obj_comprehension(void* _o)
3151{
3152 comprehension_ty o = (comprehension_ty)_o;
3153 PyObject *result = NULL, *value = NULL;
3154 if (!o) {
3155 Py_INCREF(Py_None);
3156 return Py_None;
3157 }
3158
3159 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3160 if (!result) return NULL;
3161 value = ast2obj_expr(o->target);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "target", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 value = ast2obj_expr(o->iter);
3167 if (!value) goto failed;
3168 if (PyObject_SetAttrString(result, "iter", value) == -1)
3169 goto failed;
3170 Py_DECREF(value);
3171 value = ast2obj_list(o->ifs, ast2obj_expr);
3172 if (!value) goto failed;
3173 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3174 goto failed;
3175 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003176 return result;
3177failed:
3178 Py_XDECREF(value);
3179 Py_XDECREF(result);
3180 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181}
3182
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003183PyObject*
3184ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003186 excepthandler_ty o = (excepthandler_ty)_o;
3187 PyObject *result = NULL, *value = NULL;
3188 if (!o) {
3189 Py_INCREF(Py_None);
3190 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003192
Neal Norwitzad74aa82008-03-31 05:14:30 +00003193 switch (o->kind) {
3194 case ExceptHandler_kind:
3195 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3196 if (!result) goto failed;
3197 value = ast2obj_expr(o->v.ExceptHandler.type);
3198 if (!value) goto failed;
3199 if (PyObject_SetAttrString(result, "type", value) == -1)
3200 goto failed;
3201 Py_DECREF(value);
3202 value = ast2obj_identifier(o->v.ExceptHandler.name);
3203 if (!value) goto failed;
3204 if (PyObject_SetAttrString(result, "name", value) == -1)
3205 goto failed;
3206 Py_DECREF(value);
3207 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3208 if (!value) goto failed;
3209 if (PyObject_SetAttrString(result, "body", value) == -1)
3210 goto failed;
3211 Py_DECREF(value);
3212 break;
3213 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003214 value = ast2obj_int(o->lineno);
3215 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003216 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003217 goto failed;
3218 Py_DECREF(value);
3219 value = ast2obj_int(o->col_offset);
3220 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003221 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003222 goto failed;
3223 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003224 return result;
3225failed:
3226 Py_XDECREF(value);
3227 Py_XDECREF(result);
3228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229}
3230
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003231PyObject*
3232ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003234 arguments_ty o = (arguments_ty)_o;
3235 PyObject *result = NULL, *value = NULL;
3236 if (!o) {
3237 Py_INCREF(Py_None);
3238 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003240
3241 result = PyType_GenericNew(arguments_type, NULL, NULL);
3242 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003243 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003244 if (!value) goto failed;
3245 if (PyObject_SetAttrString(result, "args", value) == -1)
3246 goto failed;
3247 Py_DECREF(value);
3248 value = ast2obj_identifier(o->vararg);
3249 if (!value) goto failed;
3250 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003253 value = ast2obj_expr(o->varargannotation);
3254 if (!value) goto failed;
3255 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3256 goto failed;
3257 Py_DECREF(value);
3258 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003259 if (!value) goto failed;
3260 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3261 goto failed;
3262 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003263 value = ast2obj_identifier(o->kwarg);
3264 if (!value) goto failed;
3265 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3266 goto failed;
3267 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003268 value = ast2obj_expr(o->kwargannotation);
3269 if (!value) goto failed;
3270 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3271 goto failed;
3272 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003273 value = ast2obj_list(o->defaults, ast2obj_expr);
3274 if (!value) goto failed;
3275 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3276 goto failed;
3277 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003278 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3279 if (!value) goto failed;
3280 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3281 goto failed;
3282 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003283 return result;
3284failed:
3285 Py_XDECREF(value);
3286 Py_XDECREF(result);
3287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288}
3289
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003290PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003291ast2obj_arg(void* _o)
3292{
3293 arg_ty o = (arg_ty)_o;
3294 PyObject *result = NULL, *value = NULL;
3295 if (!o) {
3296 Py_INCREF(Py_None);
3297 return Py_None;
3298 }
3299
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003300 result = PyType_GenericNew(arg_type, NULL, NULL);
3301 if (!result) return NULL;
3302 value = ast2obj_identifier(o->arg);
3303 if (!value) goto failed;
3304 if (PyObject_SetAttrString(result, "arg", value) == -1)
3305 goto failed;
3306 Py_DECREF(value);
3307 value = ast2obj_expr(o->annotation);
3308 if (!value) goto failed;
3309 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3310 goto failed;
3311 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003312 return result;
3313failed:
3314 Py_XDECREF(value);
3315 Py_XDECREF(result);
3316 return NULL;
3317}
3318
3319PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003320ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003322 keyword_ty o = (keyword_ty)_o;
3323 PyObject *result = NULL, *value = NULL;
3324 if (!o) {
3325 Py_INCREF(Py_None);
3326 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003328
3329 result = PyType_GenericNew(keyword_type, NULL, NULL);
3330 if (!result) return NULL;
3331 value = ast2obj_identifier(o->arg);
3332 if (!value) goto failed;
3333 if (PyObject_SetAttrString(result, "arg", value) == -1)
3334 goto failed;
3335 Py_DECREF(value);
3336 value = ast2obj_expr(o->value);
3337 if (!value) goto failed;
3338 if (PyObject_SetAttrString(result, "value", value) == -1)
3339 goto failed;
3340 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003341 return result;
3342failed:
3343 Py_XDECREF(value);
3344 Py_XDECREF(result);
3345 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003346}
3347
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003348PyObject*
3349ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003351 alias_ty o = (alias_ty)_o;
3352 PyObject *result = NULL, *value = NULL;
3353 if (!o) {
3354 Py_INCREF(Py_None);
3355 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003357
3358 result = PyType_GenericNew(alias_type, NULL, NULL);
3359 if (!result) return NULL;
3360 value = ast2obj_identifier(o->name);
3361 if (!value) goto failed;
3362 if (PyObject_SetAttrString(result, "name", value) == -1)
3363 goto failed;
3364 Py_DECREF(value);
3365 value = ast2obj_identifier(o->asname);
3366 if (!value) goto failed;
3367 if (PyObject_SetAttrString(result, "asname", value) == -1)
3368 goto failed;
3369 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003370 return result;
3371failed:
3372 Py_XDECREF(value);
3373 Py_XDECREF(result);
3374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375}
3376
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003378int
3379obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3380{
3381 PyObject* tmp = NULL;
3382
3383
3384 if (obj == Py_None) {
3385 *out = NULL;
3386 return 0;
3387 }
3388 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3389 asdl_seq* body;
3390
3391 if (PyObject_HasAttrString(obj, "body")) {
3392 int res;
3393 Py_ssize_t len;
3394 Py_ssize_t i;
3395 tmp = PyObject_GetAttrString(obj, "body");
3396 if (tmp == NULL) goto failed;
3397 if (!PyList_Check(tmp)) {
3398 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3399 goto failed;
3400 }
3401 len = PyList_GET_SIZE(tmp);
3402 body = asdl_seq_new(len, arena);
3403 if (body == NULL) goto failed;
3404 for (i = 0; i < len; i++) {
3405 stmt_ty value;
3406 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3407 if (res != 0) goto failed;
3408 asdl_seq_SET(body, i, value);
3409 }
3410 Py_XDECREF(tmp);
3411 tmp = NULL;
3412 } else {
3413 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3414 return 1;
3415 }
3416 *out = Module(body, arena);
3417 if (*out == NULL) goto failed;
3418 return 0;
3419 }
3420 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3421 asdl_seq* body;
3422
3423 if (PyObject_HasAttrString(obj, "body")) {
3424 int res;
3425 Py_ssize_t len;
3426 Py_ssize_t i;
3427 tmp = PyObject_GetAttrString(obj, "body");
3428 if (tmp == NULL) goto failed;
3429 if (!PyList_Check(tmp)) {
3430 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3431 goto failed;
3432 }
3433 len = PyList_GET_SIZE(tmp);
3434 body = asdl_seq_new(len, arena);
3435 if (body == NULL) goto failed;
3436 for (i = 0; i < len; i++) {
3437 stmt_ty value;
3438 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3439 if (res != 0) goto failed;
3440 asdl_seq_SET(body, i, value);
3441 }
3442 Py_XDECREF(tmp);
3443 tmp = NULL;
3444 } else {
3445 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3446 return 1;
3447 }
3448 *out = Interactive(body, arena);
3449 if (*out == NULL) goto failed;
3450 return 0;
3451 }
3452 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3453 expr_ty body;
3454
3455 if (PyObject_HasAttrString(obj, "body")) {
3456 int res;
3457 tmp = PyObject_GetAttrString(obj, "body");
3458 if (tmp == NULL) goto failed;
3459 res = obj2ast_expr(tmp, &body, arena);
3460 if (res != 0) goto failed;
3461 Py_XDECREF(tmp);
3462 tmp = NULL;
3463 } else {
3464 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3465 return 1;
3466 }
3467 *out = Expression(body, arena);
3468 if (*out == NULL) goto failed;
3469 return 0;
3470 }
3471 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3472 asdl_seq* body;
3473
3474 if (PyObject_HasAttrString(obj, "body")) {
3475 int res;
3476 Py_ssize_t len;
3477 Py_ssize_t i;
3478 tmp = PyObject_GetAttrString(obj, "body");
3479 if (tmp == NULL) goto failed;
3480 if (!PyList_Check(tmp)) {
3481 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3482 goto failed;
3483 }
3484 len = PyList_GET_SIZE(tmp);
3485 body = asdl_seq_new(len, arena);
3486 if (body == NULL) goto failed;
3487 for (i = 0; i < len; i++) {
3488 stmt_ty value;
3489 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3490 if (res != 0) goto failed;
3491 asdl_seq_SET(body, i, value);
3492 }
3493 Py_XDECREF(tmp);
3494 tmp = NULL;
3495 } else {
3496 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3497 return 1;
3498 }
3499 *out = Suite(body, arena);
3500 if (*out == NULL) goto failed;
3501 return 0;
3502 }
3503
3504 tmp = PyObject_Repr(obj);
3505 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00003506 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 +00003507failed:
3508 Py_XDECREF(tmp);
3509 return 1;
3510}
3511
3512int
3513obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3514{
3515 PyObject* tmp = NULL;
3516
3517 int lineno;
3518 int col_offset;
3519
3520 if (obj == Py_None) {
3521 *out = NULL;
3522 return 0;
3523 }
3524 if (PyObject_HasAttrString(obj, "lineno")) {
3525 int res;
3526 tmp = PyObject_GetAttrString(obj, "lineno");
3527 if (tmp == NULL) goto failed;
3528 res = obj2ast_int(tmp, &lineno, arena);
3529 if (res != 0) goto failed;
3530 Py_XDECREF(tmp);
3531 tmp = NULL;
3532 } else {
3533 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3534 return 1;
3535 }
3536 if (PyObject_HasAttrString(obj, "col_offset")) {
3537 int res;
3538 tmp = PyObject_GetAttrString(obj, "col_offset");
3539 if (tmp == NULL) goto failed;
3540 res = obj2ast_int(tmp, &col_offset, arena);
3541 if (res != 0) goto failed;
3542 Py_XDECREF(tmp);
3543 tmp = NULL;
3544 } else {
3545 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3546 return 1;
3547 }
3548 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3549 identifier name;
3550 arguments_ty args;
3551 asdl_seq* body;
3552 asdl_seq* decorator_list;
3553 expr_ty returns;
3554
3555 if (PyObject_HasAttrString(obj, "name")) {
3556 int res;
3557 tmp = PyObject_GetAttrString(obj, "name");
3558 if (tmp == NULL) goto failed;
3559 res = obj2ast_identifier(tmp, &name, arena);
3560 if (res != 0) goto failed;
3561 Py_XDECREF(tmp);
3562 tmp = NULL;
3563 } else {
3564 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3565 return 1;
3566 }
3567 if (PyObject_HasAttrString(obj, "args")) {
3568 int res;
3569 tmp = PyObject_GetAttrString(obj, "args");
3570 if (tmp == NULL) goto failed;
3571 res = obj2ast_arguments(tmp, &args, arena);
3572 if (res != 0) goto failed;
3573 Py_XDECREF(tmp);
3574 tmp = NULL;
3575 } else {
3576 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3577 return 1;
3578 }
3579 if (PyObject_HasAttrString(obj, "body")) {
3580 int res;
3581 Py_ssize_t len;
3582 Py_ssize_t i;
3583 tmp = PyObject_GetAttrString(obj, "body");
3584 if (tmp == NULL) goto failed;
3585 if (!PyList_Check(tmp)) {
3586 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3587 goto failed;
3588 }
3589 len = PyList_GET_SIZE(tmp);
3590 body = asdl_seq_new(len, arena);
3591 if (body == NULL) goto failed;
3592 for (i = 0; i < len; i++) {
3593 stmt_ty value;
3594 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3595 if (res != 0) goto failed;
3596 asdl_seq_SET(body, i, value);
3597 }
3598 Py_XDECREF(tmp);
3599 tmp = NULL;
3600 } else {
3601 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3602 return 1;
3603 }
3604 if (PyObject_HasAttrString(obj, "decorator_list")) {
3605 int res;
3606 Py_ssize_t len;
3607 Py_ssize_t i;
3608 tmp = PyObject_GetAttrString(obj, "decorator_list");
3609 if (tmp == NULL) goto failed;
3610 if (!PyList_Check(tmp)) {
3611 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3612 goto failed;
3613 }
3614 len = PyList_GET_SIZE(tmp);
3615 decorator_list = asdl_seq_new(len, arena);
3616 if (decorator_list == NULL) goto failed;
3617 for (i = 0; i < len; i++) {
3618 expr_ty value;
3619 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3620 if (res != 0) goto failed;
3621 asdl_seq_SET(decorator_list, i, value);
3622 }
3623 Py_XDECREF(tmp);
3624 tmp = NULL;
3625 } else {
3626 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3627 return 1;
3628 }
3629 if (PyObject_HasAttrString(obj, "returns")) {
3630 int res;
3631 tmp = PyObject_GetAttrString(obj, "returns");
3632 if (tmp == NULL) goto failed;
3633 res = obj2ast_expr(tmp, &returns, arena);
3634 if (res != 0) goto failed;
3635 Py_XDECREF(tmp);
3636 tmp = NULL;
3637 } else {
3638 returns = NULL;
3639 }
3640 *out = FunctionDef(name, args, body, decorator_list, returns,
3641 lineno, col_offset, arena);
3642 if (*out == NULL) goto failed;
3643 return 0;
3644 }
3645 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3646 identifier name;
3647 asdl_seq* bases;
3648 asdl_seq* keywords;
3649 expr_ty starargs;
3650 expr_ty kwargs;
3651 asdl_seq* body;
3652 asdl_seq* decorator_list;
3653
3654 if (PyObject_HasAttrString(obj, "name")) {
3655 int res;
3656 tmp = PyObject_GetAttrString(obj, "name");
3657 if (tmp == NULL) goto failed;
3658 res = obj2ast_identifier(tmp, &name, arena);
3659 if (res != 0) goto failed;
3660 Py_XDECREF(tmp);
3661 tmp = NULL;
3662 } else {
3663 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3664 return 1;
3665 }
3666 if (PyObject_HasAttrString(obj, "bases")) {
3667 int res;
3668 Py_ssize_t len;
3669 Py_ssize_t i;
3670 tmp = PyObject_GetAttrString(obj, "bases");
3671 if (tmp == NULL) goto failed;
3672 if (!PyList_Check(tmp)) {
3673 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3674 goto failed;
3675 }
3676 len = PyList_GET_SIZE(tmp);
3677 bases = asdl_seq_new(len, arena);
3678 if (bases == NULL) goto failed;
3679 for (i = 0; i < len; i++) {
3680 expr_ty value;
3681 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3682 if (res != 0) goto failed;
3683 asdl_seq_SET(bases, i, value);
3684 }
3685 Py_XDECREF(tmp);
3686 tmp = NULL;
3687 } else {
3688 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3689 return 1;
3690 }
3691 if (PyObject_HasAttrString(obj, "keywords")) {
3692 int res;
3693 Py_ssize_t len;
3694 Py_ssize_t i;
3695 tmp = PyObject_GetAttrString(obj, "keywords");
3696 if (tmp == NULL) goto failed;
3697 if (!PyList_Check(tmp)) {
3698 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3699 goto failed;
3700 }
3701 len = PyList_GET_SIZE(tmp);
3702 keywords = asdl_seq_new(len, arena);
3703 if (keywords == NULL) goto failed;
3704 for (i = 0; i < len; i++) {
3705 keyword_ty value;
3706 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3707 if (res != 0) goto failed;
3708 asdl_seq_SET(keywords, i, value);
3709 }
3710 Py_XDECREF(tmp);
3711 tmp = NULL;
3712 } else {
3713 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3714 return 1;
3715 }
3716 if (PyObject_HasAttrString(obj, "starargs")) {
3717 int res;
3718 tmp = PyObject_GetAttrString(obj, "starargs");
3719 if (tmp == NULL) goto failed;
3720 res = obj2ast_expr(tmp, &starargs, arena);
3721 if (res != 0) goto failed;
3722 Py_XDECREF(tmp);
3723 tmp = NULL;
3724 } else {
3725 starargs = NULL;
3726 }
3727 if (PyObject_HasAttrString(obj, "kwargs")) {
3728 int res;
3729 tmp = PyObject_GetAttrString(obj, "kwargs");
3730 if (tmp == NULL) goto failed;
3731 res = obj2ast_expr(tmp, &kwargs, arena);
3732 if (res != 0) goto failed;
3733 Py_XDECREF(tmp);
3734 tmp = NULL;
3735 } else {
3736 kwargs = NULL;
3737 }
3738 if (PyObject_HasAttrString(obj, "body")) {
3739 int res;
3740 Py_ssize_t len;
3741 Py_ssize_t i;
3742 tmp = PyObject_GetAttrString(obj, "body");
3743 if (tmp == NULL) goto failed;
3744 if (!PyList_Check(tmp)) {
3745 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3746 goto failed;
3747 }
3748 len = PyList_GET_SIZE(tmp);
3749 body = asdl_seq_new(len, arena);
3750 if (body == NULL) goto failed;
3751 for (i = 0; i < len; i++) {
3752 stmt_ty value;
3753 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3754 if (res != 0) goto failed;
3755 asdl_seq_SET(body, i, value);
3756 }
3757 Py_XDECREF(tmp);
3758 tmp = NULL;
3759 } else {
3760 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3761 return 1;
3762 }
3763 if (PyObject_HasAttrString(obj, "decorator_list")) {
3764 int res;
3765 Py_ssize_t len;
3766 Py_ssize_t i;
3767 tmp = PyObject_GetAttrString(obj, "decorator_list");
3768 if (tmp == NULL) goto failed;
3769 if (!PyList_Check(tmp)) {
3770 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3771 goto failed;
3772 }
3773 len = PyList_GET_SIZE(tmp);
3774 decorator_list = asdl_seq_new(len, arena);
3775 if (decorator_list == NULL) goto failed;
3776 for (i = 0; i < len; i++) {
3777 expr_ty value;
3778 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3779 if (res != 0) goto failed;
3780 asdl_seq_SET(decorator_list, i, value);
3781 }
3782 Py_XDECREF(tmp);
3783 tmp = NULL;
3784 } else {
3785 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3786 return 1;
3787 }
3788 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3789 decorator_list, lineno, col_offset, arena);
3790 if (*out == NULL) goto failed;
3791 return 0;
3792 }
3793 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3794 expr_ty value;
3795
3796 if (PyObject_HasAttrString(obj, "value")) {
3797 int res;
3798 tmp = PyObject_GetAttrString(obj, "value");
3799 if (tmp == NULL) goto failed;
3800 res = obj2ast_expr(tmp, &value, arena);
3801 if (res != 0) goto failed;
3802 Py_XDECREF(tmp);
3803 tmp = NULL;
3804 } else {
3805 value = NULL;
3806 }
3807 *out = Return(value, lineno, col_offset, arena);
3808 if (*out == NULL) goto failed;
3809 return 0;
3810 }
3811 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3812 asdl_seq* targets;
3813
3814 if (PyObject_HasAttrString(obj, "targets")) {
3815 int res;
3816 Py_ssize_t len;
3817 Py_ssize_t i;
3818 tmp = PyObject_GetAttrString(obj, "targets");
3819 if (tmp == NULL) goto failed;
3820 if (!PyList_Check(tmp)) {
3821 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3822 goto failed;
3823 }
3824 len = PyList_GET_SIZE(tmp);
3825 targets = asdl_seq_new(len, arena);
3826 if (targets == NULL) goto failed;
3827 for (i = 0; i < len; i++) {
3828 expr_ty value;
3829 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3830 if (res != 0) goto failed;
3831 asdl_seq_SET(targets, i, value);
3832 }
3833 Py_XDECREF(tmp);
3834 tmp = NULL;
3835 } else {
3836 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3837 return 1;
3838 }
3839 *out = Delete(targets, lineno, col_offset, arena);
3840 if (*out == NULL) goto failed;
3841 return 0;
3842 }
3843 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3844 asdl_seq* targets;
3845 expr_ty value;
3846
3847 if (PyObject_HasAttrString(obj, "targets")) {
3848 int res;
3849 Py_ssize_t len;
3850 Py_ssize_t i;
3851 tmp = PyObject_GetAttrString(obj, "targets");
3852 if (tmp == NULL) goto failed;
3853 if (!PyList_Check(tmp)) {
3854 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3855 goto failed;
3856 }
3857 len = PyList_GET_SIZE(tmp);
3858 targets = asdl_seq_new(len, arena);
3859 if (targets == NULL) goto failed;
3860 for (i = 0; i < len; i++) {
3861 expr_ty value;
3862 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3863 if (res != 0) goto failed;
3864 asdl_seq_SET(targets, i, value);
3865 }
3866 Py_XDECREF(tmp);
3867 tmp = NULL;
3868 } else {
3869 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3870 return 1;
3871 }
3872 if (PyObject_HasAttrString(obj, "value")) {
3873 int res;
3874 tmp = PyObject_GetAttrString(obj, "value");
3875 if (tmp == NULL) goto failed;
3876 res = obj2ast_expr(tmp, &value, arena);
3877 if (res != 0) goto failed;
3878 Py_XDECREF(tmp);
3879 tmp = NULL;
3880 } else {
3881 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3882 return 1;
3883 }
3884 *out = Assign(targets, value, lineno, col_offset, arena);
3885 if (*out == NULL) goto failed;
3886 return 0;
3887 }
3888 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3889 expr_ty target;
3890 operator_ty op;
3891 expr_ty value;
3892
3893 if (PyObject_HasAttrString(obj, "target")) {
3894 int res;
3895 tmp = PyObject_GetAttrString(obj, "target");
3896 if (tmp == NULL) goto failed;
3897 res = obj2ast_expr(tmp, &target, arena);
3898 if (res != 0) goto failed;
3899 Py_XDECREF(tmp);
3900 tmp = NULL;
3901 } else {
3902 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3903 return 1;
3904 }
3905 if (PyObject_HasAttrString(obj, "op")) {
3906 int res;
3907 tmp = PyObject_GetAttrString(obj, "op");
3908 if (tmp == NULL) goto failed;
3909 res = obj2ast_operator(tmp, &op, arena);
3910 if (res != 0) goto failed;
3911 Py_XDECREF(tmp);
3912 tmp = NULL;
3913 } else {
3914 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3915 return 1;
3916 }
3917 if (PyObject_HasAttrString(obj, "value")) {
3918 int res;
3919 tmp = PyObject_GetAttrString(obj, "value");
3920 if (tmp == NULL) goto failed;
3921 res = obj2ast_expr(tmp, &value, arena);
3922 if (res != 0) goto failed;
3923 Py_XDECREF(tmp);
3924 tmp = NULL;
3925 } else {
3926 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3927 return 1;
3928 }
3929 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3930 if (*out == NULL) goto failed;
3931 return 0;
3932 }
3933 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3934 expr_ty target;
3935 expr_ty iter;
3936 asdl_seq* body;
3937 asdl_seq* orelse;
3938
3939 if (PyObject_HasAttrString(obj, "target")) {
3940 int res;
3941 tmp = PyObject_GetAttrString(obj, "target");
3942 if (tmp == NULL) goto failed;
3943 res = obj2ast_expr(tmp, &target, arena);
3944 if (res != 0) goto failed;
3945 Py_XDECREF(tmp);
3946 tmp = NULL;
3947 } else {
3948 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3949 return 1;
3950 }
3951 if (PyObject_HasAttrString(obj, "iter")) {
3952 int res;
3953 tmp = PyObject_GetAttrString(obj, "iter");
3954 if (tmp == NULL) goto failed;
3955 res = obj2ast_expr(tmp, &iter, arena);
3956 if (res != 0) goto failed;
3957 Py_XDECREF(tmp);
3958 tmp = NULL;
3959 } else {
3960 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3961 return 1;
3962 }
3963 if (PyObject_HasAttrString(obj, "body")) {
3964 int res;
3965 Py_ssize_t len;
3966 Py_ssize_t i;
3967 tmp = PyObject_GetAttrString(obj, "body");
3968 if (tmp == NULL) goto failed;
3969 if (!PyList_Check(tmp)) {
3970 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3971 goto failed;
3972 }
3973 len = PyList_GET_SIZE(tmp);
3974 body = asdl_seq_new(len, arena);
3975 if (body == NULL) goto failed;
3976 for (i = 0; i < len; i++) {
3977 stmt_ty value;
3978 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3979 if (res != 0) goto failed;
3980 asdl_seq_SET(body, i, value);
3981 }
3982 Py_XDECREF(tmp);
3983 tmp = NULL;
3984 } else {
3985 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3986 return 1;
3987 }
3988 if (PyObject_HasAttrString(obj, "orelse")) {
3989 int res;
3990 Py_ssize_t len;
3991 Py_ssize_t i;
3992 tmp = PyObject_GetAttrString(obj, "orelse");
3993 if (tmp == NULL) goto failed;
3994 if (!PyList_Check(tmp)) {
3995 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3996 goto failed;
3997 }
3998 len = PyList_GET_SIZE(tmp);
3999 orelse = asdl_seq_new(len, arena);
4000 if (orelse == NULL) goto failed;
4001 for (i = 0; i < len; i++) {
4002 stmt_ty value;
4003 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4004 if (res != 0) goto failed;
4005 asdl_seq_SET(orelse, i, value);
4006 }
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4011 return 1;
4012 }
4013 *out = For(target, iter, body, orelse, lineno, col_offset,
4014 arena);
4015 if (*out == NULL) goto failed;
4016 return 0;
4017 }
4018 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
4019 expr_ty test;
4020 asdl_seq* body;
4021 asdl_seq* orelse;
4022
4023 if (PyObject_HasAttrString(obj, "test")) {
4024 int res;
4025 tmp = PyObject_GetAttrString(obj, "test");
4026 if (tmp == NULL) goto failed;
4027 res = obj2ast_expr(tmp, &test, arena);
4028 if (res != 0) goto failed;
4029 Py_XDECREF(tmp);
4030 tmp = NULL;
4031 } else {
4032 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4033 return 1;
4034 }
4035 if (PyObject_HasAttrString(obj, "body")) {
4036 int res;
4037 Py_ssize_t len;
4038 Py_ssize_t i;
4039 tmp = PyObject_GetAttrString(obj, "body");
4040 if (tmp == NULL) goto failed;
4041 if (!PyList_Check(tmp)) {
4042 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4043 goto failed;
4044 }
4045 len = PyList_GET_SIZE(tmp);
4046 body = asdl_seq_new(len, arena);
4047 if (body == NULL) goto failed;
4048 for (i = 0; i < len; i++) {
4049 stmt_ty value;
4050 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4051 if (res != 0) goto failed;
4052 asdl_seq_SET(body, i, value);
4053 }
4054 Py_XDECREF(tmp);
4055 tmp = NULL;
4056 } else {
4057 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4058 return 1;
4059 }
4060 if (PyObject_HasAttrString(obj, "orelse")) {
4061 int res;
4062 Py_ssize_t len;
4063 Py_ssize_t i;
4064 tmp = PyObject_GetAttrString(obj, "orelse");
4065 if (tmp == NULL) goto failed;
4066 if (!PyList_Check(tmp)) {
4067 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4068 goto failed;
4069 }
4070 len = PyList_GET_SIZE(tmp);
4071 orelse = asdl_seq_new(len, arena);
4072 if (orelse == NULL) goto failed;
4073 for (i = 0; i < len; i++) {
4074 stmt_ty value;
4075 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4076 if (res != 0) goto failed;
4077 asdl_seq_SET(orelse, i, value);
4078 }
4079 Py_XDECREF(tmp);
4080 tmp = NULL;
4081 } else {
4082 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4083 return 1;
4084 }
4085 *out = While(test, body, orelse, lineno, col_offset, arena);
4086 if (*out == NULL) goto failed;
4087 return 0;
4088 }
4089 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
4090 expr_ty test;
4091 asdl_seq* body;
4092 asdl_seq* orelse;
4093
4094 if (PyObject_HasAttrString(obj, "test")) {
4095 int res;
4096 tmp = PyObject_GetAttrString(obj, "test");
4097 if (tmp == NULL) goto failed;
4098 res = obj2ast_expr(tmp, &test, arena);
4099 if (res != 0) goto failed;
4100 Py_XDECREF(tmp);
4101 tmp = NULL;
4102 } else {
4103 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4104 return 1;
4105 }
4106 if (PyObject_HasAttrString(obj, "body")) {
4107 int res;
4108 Py_ssize_t len;
4109 Py_ssize_t i;
4110 tmp = PyObject_GetAttrString(obj, "body");
4111 if (tmp == NULL) goto failed;
4112 if (!PyList_Check(tmp)) {
4113 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4114 goto failed;
4115 }
4116 len = PyList_GET_SIZE(tmp);
4117 body = asdl_seq_new(len, arena);
4118 if (body == NULL) goto failed;
4119 for (i = 0; i < len; i++) {
4120 stmt_ty value;
4121 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4122 if (res != 0) goto failed;
4123 asdl_seq_SET(body, i, value);
4124 }
4125 Py_XDECREF(tmp);
4126 tmp = NULL;
4127 } else {
4128 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4129 return 1;
4130 }
4131 if (PyObject_HasAttrString(obj, "orelse")) {
4132 int res;
4133 Py_ssize_t len;
4134 Py_ssize_t i;
4135 tmp = PyObject_GetAttrString(obj, "orelse");
4136 if (tmp == NULL) goto failed;
4137 if (!PyList_Check(tmp)) {
4138 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4139 goto failed;
4140 }
4141 len = PyList_GET_SIZE(tmp);
4142 orelse = asdl_seq_new(len, arena);
4143 if (orelse == NULL) goto failed;
4144 for (i = 0; i < len; i++) {
4145 stmt_ty value;
4146 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4147 if (res != 0) goto failed;
4148 asdl_seq_SET(orelse, i, value);
4149 }
4150 Py_XDECREF(tmp);
4151 tmp = NULL;
4152 } else {
4153 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4154 return 1;
4155 }
4156 *out = If(test, body, orelse, lineno, col_offset, arena);
4157 if (*out == NULL) goto failed;
4158 return 0;
4159 }
4160 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
4161 expr_ty context_expr;
4162 expr_ty optional_vars;
4163 asdl_seq* body;
4164
4165 if (PyObject_HasAttrString(obj, "context_expr")) {
4166 int res;
4167 tmp = PyObject_GetAttrString(obj, "context_expr");
4168 if (tmp == NULL) goto failed;
4169 res = obj2ast_expr(tmp, &context_expr, arena);
4170 if (res != 0) goto failed;
4171 Py_XDECREF(tmp);
4172 tmp = NULL;
4173 } else {
4174 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4175 return 1;
4176 }
4177 if (PyObject_HasAttrString(obj, "optional_vars")) {
4178 int res;
4179 tmp = PyObject_GetAttrString(obj, "optional_vars");
4180 if (tmp == NULL) goto failed;
4181 res = obj2ast_expr(tmp, &optional_vars, arena);
4182 if (res != 0) goto failed;
4183 Py_XDECREF(tmp);
4184 tmp = NULL;
4185 } else {
4186 optional_vars = NULL;
4187 }
4188 if (PyObject_HasAttrString(obj, "body")) {
4189 int res;
4190 Py_ssize_t len;
4191 Py_ssize_t i;
4192 tmp = PyObject_GetAttrString(obj, "body");
4193 if (tmp == NULL) goto failed;
4194 if (!PyList_Check(tmp)) {
4195 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4196 goto failed;
4197 }
4198 len = PyList_GET_SIZE(tmp);
4199 body = asdl_seq_new(len, arena);
4200 if (body == NULL) goto failed;
4201 for (i = 0; i < len; i++) {
4202 stmt_ty value;
4203 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4204 if (res != 0) goto failed;
4205 asdl_seq_SET(body, i, value);
4206 }
4207 Py_XDECREF(tmp);
4208 tmp = NULL;
4209 } else {
4210 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4211 return 1;
4212 }
4213 *out = With(context_expr, optional_vars, body, lineno,
4214 col_offset, arena);
4215 if (*out == NULL) goto failed;
4216 return 0;
4217 }
4218 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4219 expr_ty exc;
4220 expr_ty cause;
4221
4222 if (PyObject_HasAttrString(obj, "exc")) {
4223 int res;
4224 tmp = PyObject_GetAttrString(obj, "exc");
4225 if (tmp == NULL) goto failed;
4226 res = obj2ast_expr(tmp, &exc, arena);
4227 if (res != 0) goto failed;
4228 Py_XDECREF(tmp);
4229 tmp = NULL;
4230 } else {
4231 exc = NULL;
4232 }
4233 if (PyObject_HasAttrString(obj, "cause")) {
4234 int res;
4235 tmp = PyObject_GetAttrString(obj, "cause");
4236 if (tmp == NULL) goto failed;
4237 res = obj2ast_expr(tmp, &cause, arena);
4238 if (res != 0) goto failed;
4239 Py_XDECREF(tmp);
4240 tmp = NULL;
4241 } else {
4242 cause = NULL;
4243 }
4244 *out = Raise(exc, cause, lineno, col_offset, arena);
4245 if (*out == NULL) goto failed;
4246 return 0;
4247 }
4248 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4249 asdl_seq* body;
4250 asdl_seq* handlers;
4251 asdl_seq* orelse;
4252
4253 if (PyObject_HasAttrString(obj, "body")) {
4254 int res;
4255 Py_ssize_t len;
4256 Py_ssize_t i;
4257 tmp = PyObject_GetAttrString(obj, "body");
4258 if (tmp == NULL) goto failed;
4259 if (!PyList_Check(tmp)) {
4260 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4261 goto failed;
4262 }
4263 len = PyList_GET_SIZE(tmp);
4264 body = asdl_seq_new(len, arena);
4265 if (body == NULL) goto failed;
4266 for (i = 0; i < len; i++) {
4267 stmt_ty value;
4268 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4269 if (res != 0) goto failed;
4270 asdl_seq_SET(body, i, value);
4271 }
4272 Py_XDECREF(tmp);
4273 tmp = NULL;
4274 } else {
4275 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4276 return 1;
4277 }
4278 if (PyObject_HasAttrString(obj, "handlers")) {
4279 int res;
4280 Py_ssize_t len;
4281 Py_ssize_t i;
4282 tmp = PyObject_GetAttrString(obj, "handlers");
4283 if (tmp == NULL) goto failed;
4284 if (!PyList_Check(tmp)) {
4285 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4286 goto failed;
4287 }
4288 len = PyList_GET_SIZE(tmp);
4289 handlers = asdl_seq_new(len, arena);
4290 if (handlers == NULL) goto failed;
4291 for (i = 0; i < len; i++) {
4292 excepthandler_ty value;
4293 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4294 if (res != 0) goto failed;
4295 asdl_seq_SET(handlers, i, value);
4296 }
4297 Py_XDECREF(tmp);
4298 tmp = NULL;
4299 } else {
4300 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4301 return 1;
4302 }
4303 if (PyObject_HasAttrString(obj, "orelse")) {
4304 int res;
4305 Py_ssize_t len;
4306 Py_ssize_t i;
4307 tmp = PyObject_GetAttrString(obj, "orelse");
4308 if (tmp == NULL) goto failed;
4309 if (!PyList_Check(tmp)) {
4310 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4311 goto failed;
4312 }
4313 len = PyList_GET_SIZE(tmp);
4314 orelse = asdl_seq_new(len, arena);
4315 if (orelse == NULL) goto failed;
4316 for (i = 0; i < len; i++) {
4317 stmt_ty value;
4318 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4319 if (res != 0) goto failed;
4320 asdl_seq_SET(orelse, i, value);
4321 }
4322 Py_XDECREF(tmp);
4323 tmp = NULL;
4324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4326 return 1;
4327 }
4328 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4329 arena);
4330 if (*out == NULL) goto failed;
4331 return 0;
4332 }
4333 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4334 asdl_seq* body;
4335 asdl_seq* finalbody;
4336
4337 if (PyObject_HasAttrString(obj, "body")) {
4338 int res;
4339 Py_ssize_t len;
4340 Py_ssize_t i;
4341 tmp = PyObject_GetAttrString(obj, "body");
4342 if (tmp == NULL) goto failed;
4343 if (!PyList_Check(tmp)) {
4344 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4345 goto failed;
4346 }
4347 len = PyList_GET_SIZE(tmp);
4348 body = asdl_seq_new(len, arena);
4349 if (body == NULL) goto failed;
4350 for (i = 0; i < len; i++) {
4351 stmt_ty value;
4352 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4353 if (res != 0) goto failed;
4354 asdl_seq_SET(body, i, value);
4355 }
4356 Py_XDECREF(tmp);
4357 tmp = NULL;
4358 } else {
4359 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4360 return 1;
4361 }
4362 if (PyObject_HasAttrString(obj, "finalbody")) {
4363 int res;
4364 Py_ssize_t len;
4365 Py_ssize_t i;
4366 tmp = PyObject_GetAttrString(obj, "finalbody");
4367 if (tmp == NULL) goto failed;
4368 if (!PyList_Check(tmp)) {
4369 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4370 goto failed;
4371 }
4372 len = PyList_GET_SIZE(tmp);
4373 finalbody = asdl_seq_new(len, arena);
4374 if (finalbody == NULL) goto failed;
4375 for (i = 0; i < len; i++) {
4376 stmt_ty value;
4377 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4378 if (res != 0) goto failed;
4379 asdl_seq_SET(finalbody, i, value);
4380 }
4381 Py_XDECREF(tmp);
4382 tmp = NULL;
4383 } else {
4384 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4385 return 1;
4386 }
4387 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4388 if (*out == NULL) goto failed;
4389 return 0;
4390 }
4391 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4392 expr_ty test;
4393 expr_ty msg;
4394
4395 if (PyObject_HasAttrString(obj, "test")) {
4396 int res;
4397 tmp = PyObject_GetAttrString(obj, "test");
4398 if (tmp == NULL) goto failed;
4399 res = obj2ast_expr(tmp, &test, arena);
4400 if (res != 0) goto failed;
4401 Py_XDECREF(tmp);
4402 tmp = NULL;
4403 } else {
4404 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4405 return 1;
4406 }
4407 if (PyObject_HasAttrString(obj, "msg")) {
4408 int res;
4409 tmp = PyObject_GetAttrString(obj, "msg");
4410 if (tmp == NULL) goto failed;
4411 res = obj2ast_expr(tmp, &msg, arena);
4412 if (res != 0) goto failed;
4413 Py_XDECREF(tmp);
4414 tmp = NULL;
4415 } else {
4416 msg = NULL;
4417 }
4418 *out = Assert(test, msg, lineno, col_offset, arena);
4419 if (*out == NULL) goto failed;
4420 return 0;
4421 }
4422 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4423 asdl_seq* names;
4424
4425 if (PyObject_HasAttrString(obj, "names")) {
4426 int res;
4427 Py_ssize_t len;
4428 Py_ssize_t i;
4429 tmp = PyObject_GetAttrString(obj, "names");
4430 if (tmp == NULL) goto failed;
4431 if (!PyList_Check(tmp)) {
4432 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4433 goto failed;
4434 }
4435 len = PyList_GET_SIZE(tmp);
4436 names = asdl_seq_new(len, arena);
4437 if (names == NULL) goto failed;
4438 for (i = 0; i < len; i++) {
4439 alias_ty value;
4440 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4441 if (res != 0) goto failed;
4442 asdl_seq_SET(names, i, value);
4443 }
4444 Py_XDECREF(tmp);
4445 tmp = NULL;
4446 } else {
4447 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4448 return 1;
4449 }
4450 *out = Import(names, lineno, col_offset, arena);
4451 if (*out == NULL) goto failed;
4452 return 0;
4453 }
4454 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4455 identifier module;
4456 asdl_seq* names;
4457 int level;
4458
4459 if (PyObject_HasAttrString(obj, "module")) {
4460 int res;
4461 tmp = PyObject_GetAttrString(obj, "module");
4462 if (tmp == NULL) goto failed;
4463 res = obj2ast_identifier(tmp, &module, arena);
4464 if (res != 0) goto failed;
4465 Py_XDECREF(tmp);
4466 tmp = NULL;
4467 } else {
4468 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4469 return 1;
4470 }
4471 if (PyObject_HasAttrString(obj, "names")) {
4472 int res;
4473 Py_ssize_t len;
4474 Py_ssize_t i;
4475 tmp = PyObject_GetAttrString(obj, "names");
4476 if (tmp == NULL) goto failed;
4477 if (!PyList_Check(tmp)) {
4478 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479 goto failed;
4480 }
4481 len = PyList_GET_SIZE(tmp);
4482 names = asdl_seq_new(len, arena);
4483 if (names == NULL) goto failed;
4484 for (i = 0; i < len; i++) {
4485 alias_ty value;
4486 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4487 if (res != 0) goto failed;
4488 asdl_seq_SET(names, i, value);
4489 }
4490 Py_XDECREF(tmp);
4491 tmp = NULL;
4492 } else {
4493 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4494 return 1;
4495 }
4496 if (PyObject_HasAttrString(obj, "level")) {
4497 int res;
4498 tmp = PyObject_GetAttrString(obj, "level");
4499 if (tmp == NULL) goto failed;
4500 res = obj2ast_int(tmp, &level, arena);
4501 if (res != 0) goto failed;
4502 Py_XDECREF(tmp);
4503 tmp = NULL;
4504 } else {
4505 level = 0;
4506 }
4507 *out = ImportFrom(module, names, level, lineno, col_offset,
4508 arena);
4509 if (*out == NULL) goto failed;
4510 return 0;
4511 }
4512 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4513 asdl_seq* names;
4514
4515 if (PyObject_HasAttrString(obj, "names")) {
4516 int res;
4517 Py_ssize_t len;
4518 Py_ssize_t i;
4519 tmp = PyObject_GetAttrString(obj, "names");
4520 if (tmp == NULL) goto failed;
4521 if (!PyList_Check(tmp)) {
4522 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4523 goto failed;
4524 }
4525 len = PyList_GET_SIZE(tmp);
4526 names = asdl_seq_new(len, arena);
4527 if (names == NULL) goto failed;
4528 for (i = 0; i < len; i++) {
4529 identifier value;
4530 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4531 if (res != 0) goto failed;
4532 asdl_seq_SET(names, i, value);
4533 }
4534 Py_XDECREF(tmp);
4535 tmp = NULL;
4536 } else {
4537 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4538 return 1;
4539 }
4540 *out = Global(names, lineno, col_offset, arena);
4541 if (*out == NULL) goto failed;
4542 return 0;
4543 }
4544 if (PyObject_IsInstance(obj, (PyObject*)Nonlocal_type)) {
4545 asdl_seq* names;
4546
4547 if (PyObject_HasAttrString(obj, "names")) {
4548 int res;
4549 Py_ssize_t len;
4550 Py_ssize_t i;
4551 tmp = PyObject_GetAttrString(obj, "names");
4552 if (tmp == NULL) goto failed;
4553 if (!PyList_Check(tmp)) {
4554 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4555 goto failed;
4556 }
4557 len = PyList_GET_SIZE(tmp);
4558 names = asdl_seq_new(len, arena);
4559 if (names == NULL) goto failed;
4560 for (i = 0; i < len; i++) {
4561 identifier value;
4562 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4563 if (res != 0) goto failed;
4564 asdl_seq_SET(names, i, value);
4565 }
4566 Py_XDECREF(tmp);
4567 tmp = NULL;
4568 } else {
4569 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4570 return 1;
4571 }
4572 *out = Nonlocal(names, lineno, col_offset, arena);
4573 if (*out == NULL) goto failed;
4574 return 0;
4575 }
4576 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4577 expr_ty value;
4578
4579 if (PyObject_HasAttrString(obj, "value")) {
4580 int res;
4581 tmp = PyObject_GetAttrString(obj, "value");
4582 if (tmp == NULL) goto failed;
4583 res = obj2ast_expr(tmp, &value, arena);
4584 if (res != 0) goto failed;
4585 Py_XDECREF(tmp);
4586 tmp = NULL;
4587 } else {
4588 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4589 return 1;
4590 }
4591 *out = Expr(value, lineno, col_offset, arena);
4592 if (*out == NULL) goto failed;
4593 return 0;
4594 }
4595 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4596
4597 *out = Pass(lineno, col_offset, arena);
4598 if (*out == NULL) goto failed;
4599 return 0;
4600 }
4601 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4602
4603 *out = Break(lineno, col_offset, arena);
4604 if (*out == NULL) goto failed;
4605 return 0;
4606 }
4607 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4608
4609 *out = Continue(lineno, col_offset, arena);
4610 if (*out == NULL) goto failed;
4611 return 0;
4612 }
4613
4614 tmp = PyObject_Repr(obj);
4615 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00004616 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 +00004617failed:
4618 Py_XDECREF(tmp);
4619 return 1;
4620}
4621
4622int
4623obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4624{
4625 PyObject* tmp = NULL;
4626
4627 int lineno;
4628 int col_offset;
4629
4630 if (obj == Py_None) {
4631 *out = NULL;
4632 return 0;
4633 }
4634 if (PyObject_HasAttrString(obj, "lineno")) {
4635 int res;
4636 tmp = PyObject_GetAttrString(obj, "lineno");
4637 if (tmp == NULL) goto failed;
4638 res = obj2ast_int(tmp, &lineno, arena);
4639 if (res != 0) goto failed;
4640 Py_XDECREF(tmp);
4641 tmp = NULL;
4642 } else {
4643 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4644 return 1;
4645 }
4646 if (PyObject_HasAttrString(obj, "col_offset")) {
4647 int res;
4648 tmp = PyObject_GetAttrString(obj, "col_offset");
4649 if (tmp == NULL) goto failed;
4650 res = obj2ast_int(tmp, &col_offset, arena);
4651 if (res != 0) goto failed;
4652 Py_XDECREF(tmp);
4653 tmp = NULL;
4654 } else {
4655 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4656 return 1;
4657 }
4658 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4659 boolop_ty op;
4660 asdl_seq* values;
4661
4662 if (PyObject_HasAttrString(obj, "op")) {
4663 int res;
4664 tmp = PyObject_GetAttrString(obj, "op");
4665 if (tmp == NULL) goto failed;
4666 res = obj2ast_boolop(tmp, &op, arena);
4667 if (res != 0) goto failed;
4668 Py_XDECREF(tmp);
4669 tmp = NULL;
4670 } else {
4671 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4672 return 1;
4673 }
4674 if (PyObject_HasAttrString(obj, "values")) {
4675 int res;
4676 Py_ssize_t len;
4677 Py_ssize_t i;
4678 tmp = PyObject_GetAttrString(obj, "values");
4679 if (tmp == NULL) goto failed;
4680 if (!PyList_Check(tmp)) {
4681 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4682 goto failed;
4683 }
4684 len = PyList_GET_SIZE(tmp);
4685 values = asdl_seq_new(len, arena);
4686 if (values == NULL) goto failed;
4687 for (i = 0; i < len; i++) {
4688 expr_ty value;
4689 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4690 if (res != 0) goto failed;
4691 asdl_seq_SET(values, i, value);
4692 }
4693 Py_XDECREF(tmp);
4694 tmp = NULL;
4695 } else {
4696 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4697 return 1;
4698 }
4699 *out = BoolOp(op, values, lineno, col_offset, arena);
4700 if (*out == NULL) goto failed;
4701 return 0;
4702 }
4703 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4704 expr_ty left;
4705 operator_ty op;
4706 expr_ty right;
4707
4708 if (PyObject_HasAttrString(obj, "left")) {
4709 int res;
4710 tmp = PyObject_GetAttrString(obj, "left");
4711 if (tmp == NULL) goto failed;
4712 res = obj2ast_expr(tmp, &left, arena);
4713 if (res != 0) goto failed;
4714 Py_XDECREF(tmp);
4715 tmp = NULL;
4716 } else {
4717 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4718 return 1;
4719 }
4720 if (PyObject_HasAttrString(obj, "op")) {
4721 int res;
4722 tmp = PyObject_GetAttrString(obj, "op");
4723 if (tmp == NULL) goto failed;
4724 res = obj2ast_operator(tmp, &op, arena);
4725 if (res != 0) goto failed;
4726 Py_XDECREF(tmp);
4727 tmp = NULL;
4728 } else {
4729 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4730 return 1;
4731 }
4732 if (PyObject_HasAttrString(obj, "right")) {
4733 int res;
4734 tmp = PyObject_GetAttrString(obj, "right");
4735 if (tmp == NULL) goto failed;
4736 res = obj2ast_expr(tmp, &right, arena);
4737 if (res != 0) goto failed;
4738 Py_XDECREF(tmp);
4739 tmp = NULL;
4740 } else {
4741 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4742 return 1;
4743 }
4744 *out = BinOp(left, op, right, lineno, col_offset, arena);
4745 if (*out == NULL) goto failed;
4746 return 0;
4747 }
4748 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4749 unaryop_ty op;
4750 expr_ty operand;
4751
4752 if (PyObject_HasAttrString(obj, "op")) {
4753 int res;
4754 tmp = PyObject_GetAttrString(obj, "op");
4755 if (tmp == NULL) goto failed;
4756 res = obj2ast_unaryop(tmp, &op, arena);
4757 if (res != 0) goto failed;
4758 Py_XDECREF(tmp);
4759 tmp = NULL;
4760 } else {
4761 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4762 return 1;
4763 }
4764 if (PyObject_HasAttrString(obj, "operand")) {
4765 int res;
4766 tmp = PyObject_GetAttrString(obj, "operand");
4767 if (tmp == NULL) goto failed;
4768 res = obj2ast_expr(tmp, &operand, arena);
4769 if (res != 0) goto failed;
4770 Py_XDECREF(tmp);
4771 tmp = NULL;
4772 } else {
4773 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4774 return 1;
4775 }
4776 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4777 if (*out == NULL) goto failed;
4778 return 0;
4779 }
4780 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4781 arguments_ty args;
4782 expr_ty body;
4783
4784 if (PyObject_HasAttrString(obj, "args")) {
4785 int res;
4786 tmp = PyObject_GetAttrString(obj, "args");
4787 if (tmp == NULL) goto failed;
4788 res = obj2ast_arguments(tmp, &args, arena);
4789 if (res != 0) goto failed;
4790 Py_XDECREF(tmp);
4791 tmp = NULL;
4792 } else {
4793 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4794 return 1;
4795 }
4796 if (PyObject_HasAttrString(obj, "body")) {
4797 int res;
4798 tmp = PyObject_GetAttrString(obj, "body");
4799 if (tmp == NULL) goto failed;
4800 res = obj2ast_expr(tmp, &body, arena);
4801 if (res != 0) goto failed;
4802 Py_XDECREF(tmp);
4803 tmp = NULL;
4804 } else {
4805 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4806 return 1;
4807 }
4808 *out = Lambda(args, body, lineno, col_offset, arena);
4809 if (*out == NULL) goto failed;
4810 return 0;
4811 }
4812 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4813 expr_ty test;
4814 expr_ty body;
4815 expr_ty orelse;
4816
4817 if (PyObject_HasAttrString(obj, "test")) {
4818 int res;
4819 tmp = PyObject_GetAttrString(obj, "test");
4820 if (tmp == NULL) goto failed;
4821 res = obj2ast_expr(tmp, &test, arena);
4822 if (res != 0) goto failed;
4823 Py_XDECREF(tmp);
4824 tmp = NULL;
4825 } else {
4826 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4827 return 1;
4828 }
4829 if (PyObject_HasAttrString(obj, "body")) {
4830 int res;
4831 tmp = PyObject_GetAttrString(obj, "body");
4832 if (tmp == NULL) goto failed;
4833 res = obj2ast_expr(tmp, &body, arena);
4834 if (res != 0) goto failed;
4835 Py_XDECREF(tmp);
4836 tmp = NULL;
4837 } else {
4838 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4839 return 1;
4840 }
4841 if (PyObject_HasAttrString(obj, "orelse")) {
4842 int res;
4843 tmp = PyObject_GetAttrString(obj, "orelse");
4844 if (tmp == NULL) goto failed;
4845 res = obj2ast_expr(tmp, &orelse, arena);
4846 if (res != 0) goto failed;
4847 Py_XDECREF(tmp);
4848 tmp = NULL;
4849 } else {
4850 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4851 return 1;
4852 }
4853 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4854 if (*out == NULL) goto failed;
4855 return 0;
4856 }
4857 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4858 asdl_seq* keys;
4859 asdl_seq* values;
4860
4861 if (PyObject_HasAttrString(obj, "keys")) {
4862 int res;
4863 Py_ssize_t len;
4864 Py_ssize_t i;
4865 tmp = PyObject_GetAttrString(obj, "keys");
4866 if (tmp == NULL) goto failed;
4867 if (!PyList_Check(tmp)) {
4868 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4869 goto failed;
4870 }
4871 len = PyList_GET_SIZE(tmp);
4872 keys = asdl_seq_new(len, arena);
4873 if (keys == NULL) goto failed;
4874 for (i = 0; i < len; i++) {
4875 expr_ty value;
4876 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4877 if (res != 0) goto failed;
4878 asdl_seq_SET(keys, i, value);
4879 }
4880 Py_XDECREF(tmp);
4881 tmp = NULL;
4882 } else {
4883 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4884 return 1;
4885 }
4886 if (PyObject_HasAttrString(obj, "values")) {
4887 int res;
4888 Py_ssize_t len;
4889 Py_ssize_t i;
4890 tmp = PyObject_GetAttrString(obj, "values");
4891 if (tmp == NULL) goto failed;
4892 if (!PyList_Check(tmp)) {
4893 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4894 goto failed;
4895 }
4896 len = PyList_GET_SIZE(tmp);
4897 values = asdl_seq_new(len, arena);
4898 if (values == NULL) goto failed;
4899 for (i = 0; i < len; i++) {
4900 expr_ty value;
4901 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4902 if (res != 0) goto failed;
4903 asdl_seq_SET(values, i, value);
4904 }
4905 Py_XDECREF(tmp);
4906 tmp = NULL;
4907 } else {
4908 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4909 return 1;
4910 }
4911 *out = Dict(keys, values, lineno, col_offset, arena);
4912 if (*out == NULL) goto failed;
4913 return 0;
4914 }
4915 if (PyObject_IsInstance(obj, (PyObject*)Set_type)) {
4916 asdl_seq* elts;
4917
4918 if (PyObject_HasAttrString(obj, "elts")) {
4919 int res;
4920 Py_ssize_t len;
4921 Py_ssize_t i;
4922 tmp = PyObject_GetAttrString(obj, "elts");
4923 if (tmp == NULL) goto failed;
4924 if (!PyList_Check(tmp)) {
4925 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4926 goto failed;
4927 }
4928 len = PyList_GET_SIZE(tmp);
4929 elts = asdl_seq_new(len, arena);
4930 if (elts == NULL) goto failed;
4931 for (i = 0; i < len; i++) {
4932 expr_ty value;
4933 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4934 if (res != 0) goto failed;
4935 asdl_seq_SET(elts, i, value);
4936 }
4937 Py_XDECREF(tmp);
4938 tmp = NULL;
4939 } else {
4940 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4941 return 1;
4942 }
4943 *out = Set(elts, lineno, col_offset, arena);
4944 if (*out == NULL) goto failed;
4945 return 0;
4946 }
4947 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4948 expr_ty elt;
4949 asdl_seq* generators;
4950
4951 if (PyObject_HasAttrString(obj, "elt")) {
4952 int res;
4953 tmp = PyObject_GetAttrString(obj, "elt");
4954 if (tmp == NULL) goto failed;
4955 res = obj2ast_expr(tmp, &elt, arena);
4956 if (res != 0) goto failed;
4957 Py_XDECREF(tmp);
4958 tmp = NULL;
4959 } else {
4960 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4961 return 1;
4962 }
4963 if (PyObject_HasAttrString(obj, "generators")) {
4964 int res;
4965 Py_ssize_t len;
4966 Py_ssize_t i;
4967 tmp = PyObject_GetAttrString(obj, "generators");
4968 if (tmp == NULL) goto failed;
4969 if (!PyList_Check(tmp)) {
4970 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4971 goto failed;
4972 }
4973 len = PyList_GET_SIZE(tmp);
4974 generators = asdl_seq_new(len, arena);
4975 if (generators == NULL) goto failed;
4976 for (i = 0; i < len; i++) {
4977 comprehension_ty value;
4978 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4979 if (res != 0) goto failed;
4980 asdl_seq_SET(generators, i, value);
4981 }
4982 Py_XDECREF(tmp);
4983 tmp = NULL;
4984 } else {
4985 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4986 return 1;
4987 }
4988 *out = ListComp(elt, generators, lineno, col_offset, arena);
4989 if (*out == NULL) goto failed;
4990 return 0;
4991 }
4992 if (PyObject_IsInstance(obj, (PyObject*)SetComp_type)) {
4993 expr_ty elt;
4994 asdl_seq* generators;
4995
4996 if (PyObject_HasAttrString(obj, "elt")) {
4997 int res;
4998 tmp = PyObject_GetAttrString(obj, "elt");
4999 if (tmp == NULL) goto failed;
5000 res = obj2ast_expr(tmp, &elt, arena);
5001 if (res != 0) goto failed;
5002 Py_XDECREF(tmp);
5003 tmp = NULL;
5004 } else {
5005 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5006 return 1;
5007 }
5008 if (PyObject_HasAttrString(obj, "generators")) {
5009 int res;
5010 Py_ssize_t len;
5011 Py_ssize_t i;
5012 tmp = PyObject_GetAttrString(obj, "generators");
5013 if (tmp == NULL) goto failed;
5014 if (!PyList_Check(tmp)) {
5015 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5016 goto failed;
5017 }
5018 len = PyList_GET_SIZE(tmp);
5019 generators = asdl_seq_new(len, arena);
5020 if (generators == NULL) goto failed;
5021 for (i = 0; i < len; i++) {
5022 comprehension_ty value;
5023 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5024 if (res != 0) goto failed;
5025 asdl_seq_SET(generators, i, value);
5026 }
5027 Py_XDECREF(tmp);
5028 tmp = NULL;
5029 } else {
5030 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5031 return 1;
5032 }
5033 *out = SetComp(elt, generators, lineno, col_offset, arena);
5034 if (*out == NULL) goto failed;
5035 return 0;
5036 }
5037 if (PyObject_IsInstance(obj, (PyObject*)DictComp_type)) {
5038 expr_ty key;
5039 expr_ty value;
5040 asdl_seq* generators;
5041
5042 if (PyObject_HasAttrString(obj, "key")) {
5043 int res;
5044 tmp = PyObject_GetAttrString(obj, "key");
5045 if (tmp == NULL) goto failed;
5046 res = obj2ast_expr(tmp, &key, arena);
5047 if (res != 0) goto failed;
5048 Py_XDECREF(tmp);
5049 tmp = NULL;
5050 } else {
5051 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5052 return 1;
5053 }
5054 if (PyObject_HasAttrString(obj, "value")) {
5055 int res;
5056 tmp = PyObject_GetAttrString(obj, "value");
5057 if (tmp == NULL) goto failed;
5058 res = obj2ast_expr(tmp, &value, arena);
5059 if (res != 0) goto failed;
5060 Py_XDECREF(tmp);
5061 tmp = NULL;
5062 } else {
5063 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5064 return 1;
5065 }
5066 if (PyObject_HasAttrString(obj, "generators")) {
5067 int res;
5068 Py_ssize_t len;
5069 Py_ssize_t i;
5070 tmp = PyObject_GetAttrString(obj, "generators");
5071 if (tmp == NULL) goto failed;
5072 if (!PyList_Check(tmp)) {
5073 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5074 goto failed;
5075 }
5076 len = PyList_GET_SIZE(tmp);
5077 generators = asdl_seq_new(len, arena);
5078 if (generators == NULL) goto failed;
5079 for (i = 0; i < len; i++) {
5080 comprehension_ty value;
5081 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5082 if (res != 0) goto failed;
5083 asdl_seq_SET(generators, i, value);
5084 }
5085 Py_XDECREF(tmp);
5086 tmp = NULL;
5087 } else {
5088 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5089 return 1;
5090 }
5091 *out = DictComp(key, value, generators, lineno, col_offset,
5092 arena);
5093 if (*out == NULL) goto failed;
5094 return 0;
5095 }
5096 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
5097 expr_ty elt;
5098 asdl_seq* generators;
5099
5100 if (PyObject_HasAttrString(obj, "elt")) {
5101 int res;
5102 tmp = PyObject_GetAttrString(obj, "elt");
5103 if (tmp == NULL) goto failed;
5104 res = obj2ast_expr(tmp, &elt, arena);
5105 if (res != 0) goto failed;
5106 Py_XDECREF(tmp);
5107 tmp = NULL;
5108 } else {
5109 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5110 return 1;
5111 }
5112 if (PyObject_HasAttrString(obj, "generators")) {
5113 int res;
5114 Py_ssize_t len;
5115 Py_ssize_t i;
5116 tmp = PyObject_GetAttrString(obj, "generators");
5117 if (tmp == NULL) goto failed;
5118 if (!PyList_Check(tmp)) {
5119 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5120 goto failed;
5121 }
5122 len = PyList_GET_SIZE(tmp);
5123 generators = asdl_seq_new(len, arena);
5124 if (generators == NULL) goto failed;
5125 for (i = 0; i < len; i++) {
5126 comprehension_ty value;
5127 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5128 if (res != 0) goto failed;
5129 asdl_seq_SET(generators, i, value);
5130 }
5131 Py_XDECREF(tmp);
5132 tmp = NULL;
5133 } else {
5134 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5135 return 1;
5136 }
5137 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5138 if (*out == NULL) goto failed;
5139 return 0;
5140 }
5141 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
5142 expr_ty value;
5143
5144 if (PyObject_HasAttrString(obj, "value")) {
5145 int res;
5146 tmp = PyObject_GetAttrString(obj, "value");
5147 if (tmp == NULL) goto failed;
5148 res = obj2ast_expr(tmp, &value, arena);
5149 if (res != 0) goto failed;
5150 Py_XDECREF(tmp);
5151 tmp = NULL;
5152 } else {
5153 value = NULL;
5154 }
5155 *out = Yield(value, lineno, col_offset, arena);
5156 if (*out == NULL) goto failed;
5157 return 0;
5158 }
5159 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
5160 expr_ty left;
5161 asdl_int_seq* ops;
5162 asdl_seq* comparators;
5163
5164 if (PyObject_HasAttrString(obj, "left")) {
5165 int res;
5166 tmp = PyObject_GetAttrString(obj, "left");
5167 if (tmp == NULL) goto failed;
5168 res = obj2ast_expr(tmp, &left, arena);
5169 if (res != 0) goto failed;
5170 Py_XDECREF(tmp);
5171 tmp = NULL;
5172 } else {
5173 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5174 return 1;
5175 }
5176 if (PyObject_HasAttrString(obj, "ops")) {
5177 int res;
5178 Py_ssize_t len;
5179 Py_ssize_t i;
5180 tmp = PyObject_GetAttrString(obj, "ops");
5181 if (tmp == NULL) goto failed;
5182 if (!PyList_Check(tmp)) {
5183 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5184 goto failed;
5185 }
5186 len = PyList_GET_SIZE(tmp);
5187 ops = asdl_int_seq_new(len, arena);
5188 if (ops == NULL) goto failed;
5189 for (i = 0; i < len; i++) {
5190 cmpop_ty value;
5191 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5192 if (res != 0) goto failed;
5193 asdl_seq_SET(ops, i, value);
5194 }
5195 Py_XDECREF(tmp);
5196 tmp = NULL;
5197 } else {
5198 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5199 return 1;
5200 }
5201 if (PyObject_HasAttrString(obj, "comparators")) {
5202 int res;
5203 Py_ssize_t len;
5204 Py_ssize_t i;
5205 tmp = PyObject_GetAttrString(obj, "comparators");
5206 if (tmp == NULL) goto failed;
5207 if (!PyList_Check(tmp)) {
5208 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5209 goto failed;
5210 }
5211 len = PyList_GET_SIZE(tmp);
5212 comparators = asdl_seq_new(len, arena);
5213 if (comparators == NULL) goto failed;
5214 for (i = 0; i < len; i++) {
5215 expr_ty value;
5216 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5217 if (res != 0) goto failed;
5218 asdl_seq_SET(comparators, i, value);
5219 }
5220 Py_XDECREF(tmp);
5221 tmp = NULL;
5222 } else {
5223 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5224 return 1;
5225 }
5226 *out = Compare(left, ops, comparators, lineno, col_offset,
5227 arena);
5228 if (*out == NULL) goto failed;
5229 return 0;
5230 }
5231 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
5232 expr_ty func;
5233 asdl_seq* args;
5234 asdl_seq* keywords;
5235 expr_ty starargs;
5236 expr_ty kwargs;
5237
5238 if (PyObject_HasAttrString(obj, "func")) {
5239 int res;
5240 tmp = PyObject_GetAttrString(obj, "func");
5241 if (tmp == NULL) goto failed;
5242 res = obj2ast_expr(tmp, &func, arena);
5243 if (res != 0) goto failed;
5244 Py_XDECREF(tmp);
5245 tmp = NULL;
5246 } else {
5247 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5248 return 1;
5249 }
5250 if (PyObject_HasAttrString(obj, "args")) {
5251 int res;
5252 Py_ssize_t len;
5253 Py_ssize_t i;
5254 tmp = PyObject_GetAttrString(obj, "args");
5255 if (tmp == NULL) goto failed;
5256 if (!PyList_Check(tmp)) {
5257 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5258 goto failed;
5259 }
5260 len = PyList_GET_SIZE(tmp);
5261 args = asdl_seq_new(len, arena);
5262 if (args == NULL) goto failed;
5263 for (i = 0; i < len; i++) {
5264 expr_ty value;
5265 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5266 if (res != 0) goto failed;
5267 asdl_seq_SET(args, i, value);
5268 }
5269 Py_XDECREF(tmp);
5270 tmp = NULL;
5271 } else {
5272 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5273 return 1;
5274 }
5275 if (PyObject_HasAttrString(obj, "keywords")) {
5276 int res;
5277 Py_ssize_t len;
5278 Py_ssize_t i;
5279 tmp = PyObject_GetAttrString(obj, "keywords");
5280 if (tmp == NULL) goto failed;
5281 if (!PyList_Check(tmp)) {
5282 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5283 goto failed;
5284 }
5285 len = PyList_GET_SIZE(tmp);
5286 keywords = asdl_seq_new(len, arena);
5287 if (keywords == NULL) goto failed;
5288 for (i = 0; i < len; i++) {
5289 keyword_ty value;
5290 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5291 if (res != 0) goto failed;
5292 asdl_seq_SET(keywords, i, value);
5293 }
5294 Py_XDECREF(tmp);
5295 tmp = NULL;
5296 } else {
5297 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5298 return 1;
5299 }
5300 if (PyObject_HasAttrString(obj, "starargs")) {
5301 int res;
5302 tmp = PyObject_GetAttrString(obj, "starargs");
5303 if (tmp == NULL) goto failed;
5304 res = obj2ast_expr(tmp, &starargs, arena);
5305 if (res != 0) goto failed;
5306 Py_XDECREF(tmp);
5307 tmp = NULL;
5308 } else {
5309 starargs = NULL;
5310 }
5311 if (PyObject_HasAttrString(obj, "kwargs")) {
5312 int res;
5313 tmp = PyObject_GetAttrString(obj, "kwargs");
5314 if (tmp == NULL) goto failed;
5315 res = obj2ast_expr(tmp, &kwargs, arena);
5316 if (res != 0) goto failed;
5317 Py_XDECREF(tmp);
5318 tmp = NULL;
5319 } else {
5320 kwargs = NULL;
5321 }
5322 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5323 col_offset, arena);
5324 if (*out == NULL) goto failed;
5325 return 0;
5326 }
5327 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5328 object n;
5329
5330 if (PyObject_HasAttrString(obj, "n")) {
5331 int res;
5332 tmp = PyObject_GetAttrString(obj, "n");
5333 if (tmp == NULL) goto failed;
5334 res = obj2ast_object(tmp, &n, arena);
5335 if (res != 0) goto failed;
5336 Py_XDECREF(tmp);
5337 tmp = NULL;
5338 } else {
5339 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5340 return 1;
5341 }
5342 *out = Num(n, lineno, col_offset, arena);
5343 if (*out == NULL) goto failed;
5344 return 0;
5345 }
5346 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5347 string s;
5348
5349 if (PyObject_HasAttrString(obj, "s")) {
5350 int res;
5351 tmp = PyObject_GetAttrString(obj, "s");
5352 if (tmp == NULL) goto failed;
5353 res = obj2ast_string(tmp, &s, arena);
5354 if (res != 0) goto failed;
5355 Py_XDECREF(tmp);
5356 tmp = NULL;
5357 } else {
5358 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5359 return 1;
5360 }
5361 *out = Str(s, lineno, col_offset, arena);
5362 if (*out == NULL) goto failed;
5363 return 0;
5364 }
5365 if (PyObject_IsInstance(obj, (PyObject*)Bytes_type)) {
5366 string s;
5367
5368 if (PyObject_HasAttrString(obj, "s")) {
5369 int res;
5370 tmp = PyObject_GetAttrString(obj, "s");
5371 if (tmp == NULL) goto failed;
5372 res = obj2ast_string(tmp, &s, arena);
5373 if (res != 0) goto failed;
5374 Py_XDECREF(tmp);
5375 tmp = NULL;
5376 } else {
5377 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5378 return 1;
5379 }
5380 *out = Bytes(s, lineno, col_offset, arena);
5381 if (*out == NULL) goto failed;
5382 return 0;
5383 }
5384 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5385
5386 *out = Ellipsis(lineno, col_offset, arena);
5387 if (*out == NULL) goto failed;
5388 return 0;
5389 }
5390 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5391 expr_ty value;
5392 identifier attr;
5393 expr_context_ty ctx;
5394
5395 if (PyObject_HasAttrString(obj, "value")) {
5396 int res;
5397 tmp = PyObject_GetAttrString(obj, "value");
5398 if (tmp == NULL) goto failed;
5399 res = obj2ast_expr(tmp, &value, arena);
5400 if (res != 0) goto failed;
5401 Py_XDECREF(tmp);
5402 tmp = NULL;
5403 } else {
5404 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5405 return 1;
5406 }
5407 if (PyObject_HasAttrString(obj, "attr")) {
5408 int res;
5409 tmp = PyObject_GetAttrString(obj, "attr");
5410 if (tmp == NULL) goto failed;
5411 res = obj2ast_identifier(tmp, &attr, arena);
5412 if (res != 0) goto failed;
5413 Py_XDECREF(tmp);
5414 tmp = NULL;
5415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5417 return 1;
5418 }
5419 if (PyObject_HasAttrString(obj, "ctx")) {
5420 int res;
5421 tmp = PyObject_GetAttrString(obj, "ctx");
5422 if (tmp == NULL) goto failed;
5423 res = obj2ast_expr_context(tmp, &ctx, arena);
5424 if (res != 0) goto failed;
5425 Py_XDECREF(tmp);
5426 tmp = NULL;
5427 } else {
5428 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5429 return 1;
5430 }
5431 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5432 if (*out == NULL) goto failed;
5433 return 0;
5434 }
5435 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5436 expr_ty value;
5437 slice_ty slice;
5438 expr_context_ty ctx;
5439
5440 if (PyObject_HasAttrString(obj, "value")) {
5441 int res;
5442 tmp = PyObject_GetAttrString(obj, "value");
5443 if (tmp == NULL) goto failed;
5444 res = obj2ast_expr(tmp, &value, arena);
5445 if (res != 0) goto failed;
5446 Py_XDECREF(tmp);
5447 tmp = NULL;
5448 } else {
5449 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5450 return 1;
5451 }
5452 if (PyObject_HasAttrString(obj, "slice")) {
5453 int res;
5454 tmp = PyObject_GetAttrString(obj, "slice");
5455 if (tmp == NULL) goto failed;
5456 res = obj2ast_slice(tmp, &slice, arena);
5457 if (res != 0) goto failed;
5458 Py_XDECREF(tmp);
5459 tmp = NULL;
5460 } else {
5461 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5462 return 1;
5463 }
5464 if (PyObject_HasAttrString(obj, "ctx")) {
5465 int res;
5466 tmp = PyObject_GetAttrString(obj, "ctx");
5467 if (tmp == NULL) goto failed;
5468 res = obj2ast_expr_context(tmp, &ctx, arena);
5469 if (res != 0) goto failed;
5470 Py_XDECREF(tmp);
5471 tmp = NULL;
5472 } else {
5473 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5474 return 1;
5475 }
5476 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5477 if (*out == NULL) goto failed;
5478 return 0;
5479 }
5480 if (PyObject_IsInstance(obj, (PyObject*)Starred_type)) {
5481 expr_ty value;
5482 expr_context_ty ctx;
5483
5484 if (PyObject_HasAttrString(obj, "value")) {
5485 int res;
5486 tmp = PyObject_GetAttrString(obj, "value");
5487 if (tmp == NULL) goto failed;
5488 res = obj2ast_expr(tmp, &value, arena);
5489 if (res != 0) goto failed;
5490 Py_XDECREF(tmp);
5491 tmp = NULL;
5492 } else {
5493 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5494 return 1;
5495 }
5496 if (PyObject_HasAttrString(obj, "ctx")) {
5497 int res;
5498 tmp = PyObject_GetAttrString(obj, "ctx");
5499 if (tmp == NULL) goto failed;
5500 res = obj2ast_expr_context(tmp, &ctx, arena);
5501 if (res != 0) goto failed;
5502 Py_XDECREF(tmp);
5503 tmp = NULL;
5504 } else {
5505 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5506 return 1;
5507 }
5508 *out = Starred(value, ctx, lineno, col_offset, arena);
5509 if (*out == NULL) goto failed;
5510 return 0;
5511 }
5512 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5513 identifier id;
5514 expr_context_ty ctx;
5515
5516 if (PyObject_HasAttrString(obj, "id")) {
5517 int res;
5518 tmp = PyObject_GetAttrString(obj, "id");
5519 if (tmp == NULL) goto failed;
5520 res = obj2ast_identifier(tmp, &id, arena);
5521 if (res != 0) goto failed;
5522 Py_XDECREF(tmp);
5523 tmp = NULL;
5524 } else {
5525 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5526 return 1;
5527 }
5528 if (PyObject_HasAttrString(obj, "ctx")) {
5529 int res;
5530 tmp = PyObject_GetAttrString(obj, "ctx");
5531 if (tmp == NULL) goto failed;
5532 res = obj2ast_expr_context(tmp, &ctx, arena);
5533 if (res != 0) goto failed;
5534 Py_XDECREF(tmp);
5535 tmp = NULL;
5536 } else {
5537 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5538 return 1;
5539 }
5540 *out = Name(id, ctx, lineno, col_offset, arena);
5541 if (*out == NULL) goto failed;
5542 return 0;
5543 }
5544 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5545 asdl_seq* elts;
5546 expr_context_ty ctx;
5547
5548 if (PyObject_HasAttrString(obj, "elts")) {
5549 int res;
5550 Py_ssize_t len;
5551 Py_ssize_t i;
5552 tmp = PyObject_GetAttrString(obj, "elts");
5553 if (tmp == NULL) goto failed;
5554 if (!PyList_Check(tmp)) {
5555 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5556 goto failed;
5557 }
5558 len = PyList_GET_SIZE(tmp);
5559 elts = asdl_seq_new(len, arena);
5560 if (elts == NULL) goto failed;
5561 for (i = 0; i < len; i++) {
5562 expr_ty value;
5563 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5564 if (res != 0) goto failed;
5565 asdl_seq_SET(elts, i, value);
5566 }
5567 Py_XDECREF(tmp);
5568 tmp = NULL;
5569 } else {
5570 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5571 return 1;
5572 }
5573 if (PyObject_HasAttrString(obj, "ctx")) {
5574 int res;
5575 tmp = PyObject_GetAttrString(obj, "ctx");
5576 if (tmp == NULL) goto failed;
5577 res = obj2ast_expr_context(tmp, &ctx, arena);
5578 if (res != 0) goto failed;
5579 Py_XDECREF(tmp);
5580 tmp = NULL;
5581 } else {
5582 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5583 return 1;
5584 }
5585 *out = List(elts, ctx, lineno, col_offset, arena);
5586 if (*out == NULL) goto failed;
5587 return 0;
5588 }
5589 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5590 asdl_seq* elts;
5591 expr_context_ty ctx;
5592
5593 if (PyObject_HasAttrString(obj, "elts")) {
5594 int res;
5595 Py_ssize_t len;
5596 Py_ssize_t i;
5597 tmp = PyObject_GetAttrString(obj, "elts");
5598 if (tmp == NULL) goto failed;
5599 if (!PyList_Check(tmp)) {
5600 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5601 goto failed;
5602 }
5603 len = PyList_GET_SIZE(tmp);
5604 elts = asdl_seq_new(len, arena);
5605 if (elts == NULL) goto failed;
5606 for (i = 0; i < len; i++) {
5607 expr_ty value;
5608 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5609 if (res != 0) goto failed;
5610 asdl_seq_SET(elts, i, value);
5611 }
5612 Py_XDECREF(tmp);
5613 tmp = NULL;
5614 } else {
5615 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5616 return 1;
5617 }
5618 if (PyObject_HasAttrString(obj, "ctx")) {
5619 int res;
5620 tmp = PyObject_GetAttrString(obj, "ctx");
5621 if (tmp == NULL) goto failed;
5622 res = obj2ast_expr_context(tmp, &ctx, arena);
5623 if (res != 0) goto failed;
5624 Py_XDECREF(tmp);
5625 tmp = NULL;
5626 } else {
5627 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5628 return 1;
5629 }
5630 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5631 if (*out == NULL) goto failed;
5632 return 0;
5633 }
5634
5635 tmp = PyObject_Repr(obj);
5636 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005637 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 +00005638failed:
5639 Py_XDECREF(tmp);
5640 return 1;
5641}
5642
5643int
5644obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5645{
5646 PyObject* tmp = NULL;
5647
5648 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5649 *out = Load;
5650 return 0;
5651 }
5652 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5653 *out = Store;
5654 return 0;
5655 }
5656 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5657 *out = Del;
5658 return 0;
5659 }
5660 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5661 *out = AugLoad;
5662 return 0;
5663 }
5664 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5665 *out = AugStore;
5666 return 0;
5667 }
5668 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5669 *out = Param;
5670 return 0;
5671 }
5672
5673 tmp = PyObject_Repr(obj);
5674 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005675 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 +00005676failed:
5677 Py_XDECREF(tmp);
5678 return 1;
5679}
5680
5681int
5682obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5683{
5684 PyObject* tmp = NULL;
5685
5686
5687 if (obj == Py_None) {
5688 *out = NULL;
5689 return 0;
5690 }
5691 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5692 expr_ty lower;
5693 expr_ty upper;
5694 expr_ty step;
5695
5696 if (PyObject_HasAttrString(obj, "lower")) {
5697 int res;
5698 tmp = PyObject_GetAttrString(obj, "lower");
5699 if (tmp == NULL) goto failed;
5700 res = obj2ast_expr(tmp, &lower, arena);
5701 if (res != 0) goto failed;
5702 Py_XDECREF(tmp);
5703 tmp = NULL;
5704 } else {
5705 lower = NULL;
5706 }
5707 if (PyObject_HasAttrString(obj, "upper")) {
5708 int res;
5709 tmp = PyObject_GetAttrString(obj, "upper");
5710 if (tmp == NULL) goto failed;
5711 res = obj2ast_expr(tmp, &upper, arena);
5712 if (res != 0) goto failed;
5713 Py_XDECREF(tmp);
5714 tmp = NULL;
5715 } else {
5716 upper = NULL;
5717 }
5718 if (PyObject_HasAttrString(obj, "step")) {
5719 int res;
5720 tmp = PyObject_GetAttrString(obj, "step");
5721 if (tmp == NULL) goto failed;
5722 res = obj2ast_expr(tmp, &step, arena);
5723 if (res != 0) goto failed;
5724 Py_XDECREF(tmp);
5725 tmp = NULL;
5726 } else {
5727 step = NULL;
5728 }
5729 *out = Slice(lower, upper, step, arena);
5730 if (*out == NULL) goto failed;
5731 return 0;
5732 }
5733 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5734 asdl_seq* dims;
5735
5736 if (PyObject_HasAttrString(obj, "dims")) {
5737 int res;
5738 Py_ssize_t len;
5739 Py_ssize_t i;
5740 tmp = PyObject_GetAttrString(obj, "dims");
5741 if (tmp == NULL) goto failed;
5742 if (!PyList_Check(tmp)) {
5743 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5744 goto failed;
5745 }
5746 len = PyList_GET_SIZE(tmp);
5747 dims = asdl_seq_new(len, arena);
5748 if (dims == NULL) goto failed;
5749 for (i = 0; i < len; i++) {
5750 slice_ty value;
5751 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5752 if (res != 0) goto failed;
5753 asdl_seq_SET(dims, i, value);
5754 }
5755 Py_XDECREF(tmp);
5756 tmp = NULL;
5757 } else {
5758 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5759 return 1;
5760 }
5761 *out = ExtSlice(dims, arena);
5762 if (*out == NULL) goto failed;
5763 return 0;
5764 }
5765 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5766 expr_ty value;
5767
5768 if (PyObject_HasAttrString(obj, "value")) {
5769 int res;
5770 tmp = PyObject_GetAttrString(obj, "value");
5771 if (tmp == NULL) goto failed;
5772 res = obj2ast_expr(tmp, &value, arena);
5773 if (res != 0) goto failed;
5774 Py_XDECREF(tmp);
5775 tmp = NULL;
5776 } else {
5777 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5778 return 1;
5779 }
5780 *out = Index(value, arena);
5781 if (*out == NULL) goto failed;
5782 return 0;
5783 }
5784
5785 tmp = PyObject_Repr(obj);
5786 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005787 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 +00005788failed:
5789 Py_XDECREF(tmp);
5790 return 1;
5791}
5792
5793int
5794obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5795{
5796 PyObject* tmp = NULL;
5797
5798 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5799 *out = And;
5800 return 0;
5801 }
5802 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5803 *out = Or;
5804 return 0;
5805 }
5806
5807 tmp = PyObject_Repr(obj);
5808 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005809 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 +00005810failed:
5811 Py_XDECREF(tmp);
5812 return 1;
5813}
5814
5815int
5816obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5817{
5818 PyObject* tmp = NULL;
5819
5820 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5821 *out = Add;
5822 return 0;
5823 }
5824 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5825 *out = Sub;
5826 return 0;
5827 }
5828 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5829 *out = Mult;
5830 return 0;
5831 }
5832 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5833 *out = Div;
5834 return 0;
5835 }
5836 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5837 *out = Mod;
5838 return 0;
5839 }
5840 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5841 *out = Pow;
5842 return 0;
5843 }
5844 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5845 *out = LShift;
5846 return 0;
5847 }
5848 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5849 *out = RShift;
5850 return 0;
5851 }
5852 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5853 *out = BitOr;
5854 return 0;
5855 }
5856 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5857 *out = BitXor;
5858 return 0;
5859 }
5860 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5861 *out = BitAnd;
5862 return 0;
5863 }
5864 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5865 *out = FloorDiv;
5866 return 0;
5867 }
5868
5869 tmp = PyObject_Repr(obj);
5870 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005871 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 +00005872failed:
5873 Py_XDECREF(tmp);
5874 return 1;
5875}
5876
5877int
5878obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5879{
5880 PyObject* tmp = NULL;
5881
5882 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5883 *out = Invert;
5884 return 0;
5885 }
5886 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5887 *out = Not;
5888 return 0;
5889 }
5890 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5891 *out = UAdd;
5892 return 0;
5893 }
5894 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5895 *out = USub;
5896 return 0;
5897 }
5898
5899 tmp = PyObject_Repr(obj);
5900 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005901 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 +00005902failed:
5903 Py_XDECREF(tmp);
5904 return 1;
5905}
5906
5907int
5908obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5909{
5910 PyObject* tmp = NULL;
5911
5912 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5913 *out = Eq;
5914 return 0;
5915 }
5916 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5917 *out = NotEq;
5918 return 0;
5919 }
5920 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5921 *out = Lt;
5922 return 0;
5923 }
5924 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5925 *out = LtE;
5926 return 0;
5927 }
5928 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5929 *out = Gt;
5930 return 0;
5931 }
5932 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5933 *out = GtE;
5934 return 0;
5935 }
5936 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5937 *out = Is;
5938 return 0;
5939 }
5940 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5941 *out = IsNot;
5942 return 0;
5943 }
5944 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5945 *out = In;
5946 return 0;
5947 }
5948 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5949 *out = NotIn;
5950 return 0;
5951 }
5952
5953 tmp = PyObject_Repr(obj);
5954 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00005955 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 +00005956failed:
5957 Py_XDECREF(tmp);
5958 return 1;
5959}
5960
5961int
5962obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5963{
5964 PyObject* tmp = NULL;
5965 expr_ty target;
5966 expr_ty iter;
5967 asdl_seq* ifs;
5968
5969 if (PyObject_HasAttrString(obj, "target")) {
5970 int res;
5971 tmp = PyObject_GetAttrString(obj, "target");
5972 if (tmp == NULL) goto failed;
5973 res = obj2ast_expr(tmp, &target, arena);
5974 if (res != 0) goto failed;
5975 Py_XDECREF(tmp);
5976 tmp = NULL;
5977 } else {
5978 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5979 return 1;
5980 }
5981 if (PyObject_HasAttrString(obj, "iter")) {
5982 int res;
5983 tmp = PyObject_GetAttrString(obj, "iter");
5984 if (tmp == NULL) goto failed;
5985 res = obj2ast_expr(tmp, &iter, arena);
5986 if (res != 0) goto failed;
5987 Py_XDECREF(tmp);
5988 tmp = NULL;
5989 } else {
5990 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5991 return 1;
5992 }
5993 if (PyObject_HasAttrString(obj, "ifs")) {
5994 int res;
5995 Py_ssize_t len;
5996 Py_ssize_t i;
5997 tmp = PyObject_GetAttrString(obj, "ifs");
5998 if (tmp == NULL) goto failed;
5999 if (!PyList_Check(tmp)) {
6000 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6001 goto failed;
6002 }
6003 len = PyList_GET_SIZE(tmp);
6004 ifs = asdl_seq_new(len, arena);
6005 if (ifs == NULL) goto failed;
6006 for (i = 0; i < len; i++) {
6007 expr_ty value;
6008 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6009 if (res != 0) goto failed;
6010 asdl_seq_SET(ifs, i, value);
6011 }
6012 Py_XDECREF(tmp);
6013 tmp = NULL;
6014 } else {
6015 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6016 return 1;
6017 }
6018 *out = comprehension(target, iter, ifs, arena);
6019 return 0;
6020failed:
6021 Py_XDECREF(tmp);
6022 return 1;
6023}
6024
6025int
6026obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6027{
6028 PyObject* tmp = NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006029
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006030 int lineno;
6031 int col_offset;
6032
Neal Norwitzad74aa82008-03-31 05:14:30 +00006033 if (obj == Py_None) {
6034 *out = NULL;
6035 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006036 }
6037 if (PyObject_HasAttrString(obj, "lineno")) {
6038 int res;
6039 tmp = PyObject_GetAttrString(obj, "lineno");
6040 if (tmp == NULL) goto failed;
6041 res = obj2ast_int(tmp, &lineno, arena);
6042 if (res != 0) goto failed;
6043 Py_XDECREF(tmp);
6044 tmp = NULL;
6045 } else {
6046 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6047 return 1;
6048 }
6049 if (PyObject_HasAttrString(obj, "col_offset")) {
6050 int res;
6051 tmp = PyObject_GetAttrString(obj, "col_offset");
6052 if (tmp == NULL) goto failed;
6053 res = obj2ast_int(tmp, &col_offset, arena);
6054 if (res != 0) goto failed;
6055 Py_XDECREF(tmp);
6056 tmp = NULL;
6057 } else {
6058 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6059 return 1;
6060 }
Neal Norwitzad74aa82008-03-31 05:14:30 +00006061 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
6062 expr_ty type;
6063 identifier name;
6064 asdl_seq* body;
6065
6066 if (PyObject_HasAttrString(obj, "type")) {
6067 int res;
6068 tmp = PyObject_GetAttrString(obj, "type");
6069 if (tmp == NULL) goto failed;
6070 res = obj2ast_expr(tmp, &type, arena);
6071 if (res != 0) goto failed;
6072 Py_XDECREF(tmp);
6073 tmp = NULL;
6074 } else {
6075 type = NULL;
6076 }
6077 if (PyObject_HasAttrString(obj, "name")) {
6078 int res;
6079 tmp = PyObject_GetAttrString(obj, "name");
6080 if (tmp == NULL) goto failed;
6081 res = obj2ast_identifier(tmp, &name, arena);
6082 if (res != 0) goto failed;
6083 Py_XDECREF(tmp);
6084 tmp = NULL;
6085 } else {
6086 name = NULL;
6087 }
6088 if (PyObject_HasAttrString(obj, "body")) {
6089 int res;
6090 Py_ssize_t len;
6091 Py_ssize_t i;
6092 tmp = PyObject_GetAttrString(obj, "body");
6093 if (tmp == NULL) goto failed;
6094 if (!PyList_Check(tmp)) {
6095 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6096 goto failed;
6097 }
6098 len = PyList_GET_SIZE(tmp);
6099 body = asdl_seq_new(len, arena);
6100 if (body == NULL) goto failed;
6101 for (i = 0; i < len; i++) {
6102 stmt_ty value;
6103 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6104 if (res != 0) goto failed;
6105 asdl_seq_SET(body, i, value);
6106 }
6107 Py_XDECREF(tmp);
6108 tmp = NULL;
6109 } else {
6110 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6111 return 1;
6112 }
6113 *out = ExceptHandler(type, name, body, lineno, col_offset,
6114 arena);
6115 if (*out == NULL) goto failed;
6116 return 0;
6117 }
6118
6119 tmp = PyObject_Repr(obj);
6120 if (tmp == NULL) goto failed;
Christian Heimes72b710a2008-05-26 13:28:38 +00006121 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 +00006122failed:
6123 Py_XDECREF(tmp);
6124 return 1;
6125}
6126
6127int
6128obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6129{
6130 PyObject* tmp = NULL;
6131 asdl_seq* args;
6132 identifier vararg;
6133 expr_ty varargannotation;
6134 asdl_seq* kwonlyargs;
6135 identifier kwarg;
6136 expr_ty kwargannotation;
6137 asdl_seq* defaults;
6138 asdl_seq* kw_defaults;
6139
6140 if (PyObject_HasAttrString(obj, "args")) {
6141 int res;
6142 Py_ssize_t len;
6143 Py_ssize_t i;
6144 tmp = PyObject_GetAttrString(obj, "args");
6145 if (tmp == NULL) goto failed;
6146 if (!PyList_Check(tmp)) {
6147 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6148 goto failed;
6149 }
6150 len = PyList_GET_SIZE(tmp);
6151 args = asdl_seq_new(len, arena);
6152 if (args == NULL) goto failed;
6153 for (i = 0; i < len; i++) {
6154 arg_ty value;
6155 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6156 if (res != 0) goto failed;
6157 asdl_seq_SET(args, i, value);
6158 }
6159 Py_XDECREF(tmp);
6160 tmp = NULL;
6161 } else {
6162 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6163 return 1;
6164 }
6165 if (PyObject_HasAttrString(obj, "vararg")) {
6166 int res;
6167 tmp = PyObject_GetAttrString(obj, "vararg");
6168 if (tmp == NULL) goto failed;
6169 res = obj2ast_identifier(tmp, &vararg, arena);
6170 if (res != 0) goto failed;
6171 Py_XDECREF(tmp);
6172 tmp = NULL;
6173 } else {
6174 vararg = NULL;
6175 }
6176 if (PyObject_HasAttrString(obj, "varargannotation")) {
6177 int res;
6178 tmp = PyObject_GetAttrString(obj, "varargannotation");
6179 if (tmp == NULL) goto failed;
6180 res = obj2ast_expr(tmp, &varargannotation, arena);
6181 if (res != 0) goto failed;
6182 Py_XDECREF(tmp);
6183 tmp = NULL;
6184 } else {
6185 varargannotation = NULL;
6186 }
6187 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6188 int res;
6189 Py_ssize_t len;
6190 Py_ssize_t i;
6191 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6192 if (tmp == NULL) goto failed;
6193 if (!PyList_Check(tmp)) {
6194 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6195 goto failed;
6196 }
6197 len = PyList_GET_SIZE(tmp);
6198 kwonlyargs = asdl_seq_new(len, arena);
6199 if (kwonlyargs == NULL) goto failed;
6200 for (i = 0; i < len; i++) {
6201 arg_ty value;
6202 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6203 if (res != 0) goto failed;
6204 asdl_seq_SET(kwonlyargs, i, value);
6205 }
6206 Py_XDECREF(tmp);
6207 tmp = NULL;
6208 } else {
6209 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6210 return 1;
6211 }
6212 if (PyObject_HasAttrString(obj, "kwarg")) {
6213 int res;
6214 tmp = PyObject_GetAttrString(obj, "kwarg");
6215 if (tmp == NULL) goto failed;
6216 res = obj2ast_identifier(tmp, &kwarg, arena);
6217 if (res != 0) goto failed;
6218 Py_XDECREF(tmp);
6219 tmp = NULL;
6220 } else {
6221 kwarg = NULL;
6222 }
6223 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6224 int res;
6225 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6226 if (tmp == NULL) goto failed;
6227 res = obj2ast_expr(tmp, &kwargannotation, arena);
6228 if (res != 0) goto failed;
6229 Py_XDECREF(tmp);
6230 tmp = NULL;
6231 } else {
6232 kwargannotation = NULL;
6233 }
6234 if (PyObject_HasAttrString(obj, "defaults")) {
6235 int res;
6236 Py_ssize_t len;
6237 Py_ssize_t i;
6238 tmp = PyObject_GetAttrString(obj, "defaults");
6239 if (tmp == NULL) goto failed;
6240 if (!PyList_Check(tmp)) {
6241 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6242 goto failed;
6243 }
6244 len = PyList_GET_SIZE(tmp);
6245 defaults = asdl_seq_new(len, arena);
6246 if (defaults == NULL) goto failed;
6247 for (i = 0; i < len; i++) {
6248 expr_ty value;
6249 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6250 if (res != 0) goto failed;
6251 asdl_seq_SET(defaults, i, value);
6252 }
6253 Py_XDECREF(tmp);
6254 tmp = NULL;
6255 } else {
6256 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6257 return 1;
6258 }
6259 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6260 int res;
6261 Py_ssize_t len;
6262 Py_ssize_t i;
6263 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6264 if (tmp == NULL) goto failed;
6265 if (!PyList_Check(tmp)) {
6266 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6267 goto failed;
6268 }
6269 len = PyList_GET_SIZE(tmp);
6270 kw_defaults = asdl_seq_new(len, arena);
6271 if (kw_defaults == NULL) goto failed;
6272 for (i = 0; i < len; i++) {
6273 expr_ty value;
6274 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6275 if (res != 0) goto failed;
6276 asdl_seq_SET(kw_defaults, i, value);
6277 }
6278 Py_XDECREF(tmp);
6279 tmp = NULL;
6280 } else {
6281 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6282 return 1;
6283 }
6284 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6285 kwargannotation, defaults, kw_defaults, arena);
6286 return 0;
6287failed:
6288 Py_XDECREF(tmp);
6289 return 1;
6290}
6291
6292int
6293obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6294{
6295 PyObject* tmp = NULL;
6296 identifier arg;
6297 expr_ty annotation;
6298
6299 if (PyObject_HasAttrString(obj, "arg")) {
6300 int res;
6301 tmp = PyObject_GetAttrString(obj, "arg");
6302 if (tmp == NULL) goto failed;
6303 res = obj2ast_identifier(tmp, &arg, arena);
6304 if (res != 0) goto failed;
6305 Py_XDECREF(tmp);
6306 tmp = NULL;
6307 } else {
6308 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6309 return 1;
6310 }
6311 if (PyObject_HasAttrString(obj, "annotation")) {
6312 int res;
6313 tmp = PyObject_GetAttrString(obj, "annotation");
6314 if (tmp == NULL) goto failed;
6315 res = obj2ast_expr(tmp, &annotation, arena);
6316 if (res != 0) goto failed;
6317 Py_XDECREF(tmp);
6318 tmp = NULL;
6319 } else {
6320 annotation = NULL;
6321 }
6322 *out = arg(arg, annotation, arena);
6323 return 0;
6324failed:
6325 Py_XDECREF(tmp);
6326 return 1;
6327}
6328
6329int
6330obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6331{
6332 PyObject* tmp = NULL;
6333 identifier arg;
6334 expr_ty value;
6335
6336 if (PyObject_HasAttrString(obj, "arg")) {
6337 int res;
6338 tmp = PyObject_GetAttrString(obj, "arg");
6339 if (tmp == NULL) goto failed;
6340 res = obj2ast_identifier(tmp, &arg, arena);
6341 if (res != 0) goto failed;
6342 Py_XDECREF(tmp);
6343 tmp = NULL;
6344 } else {
6345 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6346 return 1;
6347 }
6348 if (PyObject_HasAttrString(obj, "value")) {
6349 int res;
6350 tmp = PyObject_GetAttrString(obj, "value");
6351 if (tmp == NULL) goto failed;
6352 res = obj2ast_expr(tmp, &value, arena);
6353 if (res != 0) goto failed;
6354 Py_XDECREF(tmp);
6355 tmp = NULL;
6356 } else {
6357 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6358 return 1;
6359 }
6360 *out = keyword(arg, value, arena);
6361 return 0;
6362failed:
6363 Py_XDECREF(tmp);
6364 return 1;
6365}
6366
6367int
6368obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6369{
6370 PyObject* tmp = NULL;
6371 identifier name;
6372 identifier asname;
6373
6374 if (PyObject_HasAttrString(obj, "name")) {
6375 int res;
6376 tmp = PyObject_GetAttrString(obj, "name");
6377 if (tmp == NULL) goto failed;
6378 res = obj2ast_identifier(tmp, &name, arena);
6379 if (res != 0) goto failed;
6380 Py_XDECREF(tmp);
6381 tmp = NULL;
6382 } else {
6383 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6384 return 1;
6385 }
6386 if (PyObject_HasAttrString(obj, "asname")) {
6387 int res;
6388 tmp = PyObject_GetAttrString(obj, "asname");
6389 if (tmp == NULL) goto failed;
6390 res = obj2ast_identifier(tmp, &asname, arena);
6391 if (res != 0) goto failed;
6392 Py_XDECREF(tmp);
6393 tmp = NULL;
6394 } else {
6395 asname = NULL;
6396 }
6397 *out = alias(name, asname, arena);
6398 return 0;
6399failed:
6400 Py_XDECREF(tmp);
6401 return 1;
6402}
6403
6404
Martin v. Löwis1a214512008-06-11 05:26:20 +00006405static struct PyModuleDef _astmodule = {
6406 PyModuleDef_HEAD_INIT, "_ast"
6407};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006408PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006409PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006410{
6411 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006412 if (!init_types()) return NULL;
6413 m = PyModule_Create(&_astmodule);
6414 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006415 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006416 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6417 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006418 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006419 return NULL;
Benjamin Petersone6d4a9b2008-12-06 18:33:20 +00006420 if (PyModule_AddStringConstant(m, "__version__", "67616") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006421 return NULL;
6422 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6423 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006424 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006425 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006426 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006427 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006428 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006429 0) return NULL;
6430 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6431 NULL;
6432 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6433 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006434 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006435 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006436 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_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, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006439 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006440 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006441 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006442 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006443 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006444 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006445 0) return NULL;
6446 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6447 NULL;
6448 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6449 NULL;
6450 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6451 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6452 NULL;
6453 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6454 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006455 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006456 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006457 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_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, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006460 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006461 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006462 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006463 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006464 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006465 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006466 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006467 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006468 return NULL;
6469 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6470 NULL;
6471 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6472 NULL;
6473 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6474 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006475 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006476 return NULL;
6477 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6478 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006479 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006480 return NULL;
6481 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6482 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006483 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006484 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006485 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006486 return NULL;
6487 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6488 NULL;
6489 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6490 NULL;
6491 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6492 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006493 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006494 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006495 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006496 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006497 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006498 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006499 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006500 (PyObject*)GeneratorExp_type) < 0) return NULL;
6501 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6502 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006503 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006504 return NULL;
6505 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6506 NULL;
6507 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6508 NULL;
6509 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6510 NULL;
6511 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6512 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006513 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006514 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006515 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006516 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006517 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006518 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006519 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006520 return NULL;
6521 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6522 NULL;
6523 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6524 NULL;
6525 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6526 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006527 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006528 (PyObject*)expr_context_type) < 0) return NULL;
6529 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6530 NULL;
6531 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6532 NULL;
6533 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6534 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006535 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006536 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006537 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006538 return NULL;
6539 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6540 NULL;
6541 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6542 NULL;
6543 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6544 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006545 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006546 return NULL;
6547 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6548 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006549 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006550 return NULL;
6551 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6552 NULL;
6553 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006554 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006555 return NULL;
6556 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6557 NULL;
6558 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6559 NULL;
6560 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6561 NULL;
6562 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6563 NULL;
6564 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6565 NULL;
6566 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6567 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006568 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_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, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006571 return NULL;
6572 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6573 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006574 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_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, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006577 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006578 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006579 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006580 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006581 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006582 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006583 return NULL;
6584 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6585 NULL;
6586 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6587 NULL;
6588 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6589 NULL;
6590 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6591 NULL;
6592 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6593 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6594 NULL;
6595 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6596 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6597 NULL;
6598 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6599 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6600 NULL;
6601 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6602 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6603 NULL;
6604 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6605 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6606 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006607 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006608 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006609 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006610 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006611 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006612 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006613 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006614 0) return NULL;
6615 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6616 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006617 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006618 return NULL;
6619 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6620 NULL;
6621 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006622}
6623
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006624
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006625PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006626{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006627 init_types();
6628 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006629}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006630
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006631/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6632mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006633{
6634 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006635 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6636 (PyObject*)Interactive_type};
6637 char *req_name[] = {"Module", "Expression", "Interactive"};
6638 assert(0 <= mode && mode <= 2);
6639
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006640 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006641
6642 if (!PyObject_IsInstance(ast, req_type[mode])) {
6643 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6644 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006645 return NULL;
6646 }
6647 if (obj2ast_mod(ast, &res, arena) != 0)
6648 return NULL;
6649 else
6650 return res;
6651}
6652
6653int PyAST_Check(PyObject* obj)
6654{
6655 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006656 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006657}
6658
Martin v. Löwis5b222132007-06-10 09:51:05 +00006659