blob: 89c07cd602d2ab017e3d35feb42f7282d18f0234 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Petersonefad2442011-07-22 10:39:12 -05005 __version__ 82163.
Thomas Wouterscf297e42007-02-23 15:07:44 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Neal Norwitz207c9f32008-03-31 04:42:11 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000045 "decorator_list",
Neal Norwitzc1505362006-12-28 06:47:50 +000046 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Neal Norwitz53d960c2006-02-28 22:47:29 +000048static PyTypeObject *ClassDef_type;
49static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000050 "name",
51 "bases",
Guido van Rossum52cc1d82007-03-18 15:41:51 +000052 "keywords",
53 "starargs",
54 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000055 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000056 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000057};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Return_type;
59static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "value",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Delete_type;
63static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Assign_type;
67static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000068 "targets",
69 "value",
70};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *AugAssign_type;
72static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000073 "target",
74 "op",
75 "value",
76};
Neal Norwitz53d960c2006-02-28 22:47:29 +000077static PyTypeObject *For_type;
78static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000079 "target",
80 "iter",
81 "body",
82 "orelse",
83};
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static PyTypeObject *While_type;
85static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000086 "test",
87 "body",
88 "orelse",
89};
Neal Norwitz53d960c2006-02-28 22:47:29 +000090static PyTypeObject *If_type;
91static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000092 "test",
93 "body",
94 "orelse",
95};
Neal Norwitz53d960c2006-02-28 22:47:29 +000096static PyTypeObject *With_type;
97static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +000098 "context_expr",
99 "optional_vars",
100 "body",
101};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000102static PyTypeObject *Raise_type;
103static char *Raise_fields[]={
Collin Winter828f04a2007-08-31 00:04:24 +0000104 "exc",
105 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static PyTypeObject *TryExcept_type;
108static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000109 "body",
110 "handlers",
111 "orelse",
112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *TryFinally_type;
114static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115 "body",
116 "finalbody",
117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *Assert_type;
119static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120 "test",
121 "msg",
122};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000123static PyTypeObject *Import_type;
124static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000125 "names",
126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *ImportFrom_type;
128static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000129 "module",
130 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000131 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000133static PyTypeObject *Global_type;
134static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135 "names",
136};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000137static PyTypeObject *Nonlocal_type;
138static char *Nonlocal_fields[]={
139 "names",
140};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000141static PyTypeObject *Expr_type;
142static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000143 "value",
144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *Pass_type;
146static PyTypeObject *Break_type;
147static PyTypeObject *Continue_type;
148static PyTypeObject *expr_type;
149static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000150 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000151 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000152};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *BoolOp_type;
155static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000156 "op",
157 "values",
158};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BinOp_type;
160static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "left",
162 "op",
163 "right",
164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *UnaryOp_type;
166static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167 "op",
168 "operand",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *Lambda_type;
171static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "args",
173 "body",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *IfExp_type;
176static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000177 "test",
178 "body",
179 "orelse",
180};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static PyTypeObject *Dict_type;
182static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000183 "keys",
184 "values",
185};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000186static PyTypeObject *Set_type;
187static char *Set_fields[]={
188 "elts",
189};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000190static PyTypeObject *ListComp_type;
191static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000192 "elt",
193 "generators",
194};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000195static PyTypeObject *SetComp_type;
196static char *SetComp_fields[]={
197 "elt",
198 "generators",
199};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000200static PyTypeObject *DictComp_type;
201static char *DictComp_fields[]={
202 "key",
203 "value",
204 "generators",
205};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000206static PyTypeObject *GeneratorExp_type;
207static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000208 "elt",
209 "generators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Yield_type;
212static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "value",
214};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static PyTypeObject *Compare_type;
216static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217 "left",
218 "ops",
219 "comparators",
220};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000221static PyTypeObject *Call_type;
222static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000223 "func",
224 "args",
225 "keywords",
226 "starargs",
227 "kwargs",
228};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000229static PyTypeObject *Num_type;
230static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000231 "n",
232};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000233static PyTypeObject *Str_type;
234static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000235 "s",
236};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000237static PyTypeObject *Bytes_type;
238static char *Bytes_fields[]={
239 "s",
240};
Georg Brandl52318d62006-09-06 07:06:08 +0000241static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000242static PyTypeObject *Attribute_type;
243static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000244 "value",
245 "attr",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *Subscript_type;
249static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "value",
251 "slice",
252 "ctx",
253};
Guido van Rossum0368b722007-05-11 16:50:42 +0000254static PyTypeObject *Starred_type;
255static char *Starred_fields[]={
256 "value",
257 "ctx",
258};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000259static PyTypeObject *Name_type;
260static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000261 "id",
262 "ctx",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *List_type;
265static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000266 "elts",
267 "ctx",
268};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000269static PyTypeObject *Tuple_type;
270static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271 "elts",
272 "ctx",
273};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000274static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000275static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
276*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
277static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *Load_type;
279static PyTypeObject *Store_type;
280static PyTypeObject *Del_type;
281static PyTypeObject *AugLoad_type;
282static PyTypeObject *AugStore_type;
283static PyTypeObject *Param_type;
284static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000285static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000286static PyTypeObject *Slice_type;
287static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000288 "lower",
289 "upper",
290 "step",
291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *ExtSlice_type;
293static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000294 "dims",
295};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Index_type;
297static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298 "value",
299};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301static PyObject *And_singleton, *Or_singleton;
302static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000303static PyTypeObject *And_type;
304static PyTypeObject *Or_type;
305static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000306static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
307*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
308*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
309*FloorDiv_singleton;
310static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000311static PyTypeObject *Add_type;
312static PyTypeObject *Sub_type;
313static PyTypeObject *Mult_type;
314static PyTypeObject *Div_type;
315static PyTypeObject *Mod_type;
316static PyTypeObject *Pow_type;
317static PyTypeObject *LShift_type;
318static PyTypeObject *RShift_type;
319static PyTypeObject *BitOr_type;
320static PyTypeObject *BitXor_type;
321static PyTypeObject *BitAnd_type;
322static PyTypeObject *FloorDiv_type;
323static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000324static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
325*USub_singleton;
326static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Invert_type;
328static PyTypeObject *Not_type;
329static PyTypeObject *UAdd_type;
330static PyTypeObject *USub_type;
331static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
333*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
334*NotIn_singleton;
335static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000336static PyTypeObject *Eq_type;
337static PyTypeObject *NotEq_type;
338static PyTypeObject *Lt_type;
339static PyTypeObject *LtE_type;
340static PyTypeObject *Gt_type;
341static PyTypeObject *GtE_type;
342static PyTypeObject *Is_type;
343static PyTypeObject *IsNot_type;
344static PyTypeObject *In_type;
345static PyTypeObject *NotIn_type;
346static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000349 "target",
350 "iter",
351 "ifs",
352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000354static char *excepthandler_attributes[] = {
355 "lineno",
356 "col_offset",
357};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000359static PyTypeObject *ExceptHandler_type;
360static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "type",
362 "name",
363 "body",
364};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000365static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000366static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000367static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000368 "args",
369 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000370 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000371 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000372 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000373 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000375 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000376};
Neal Norwitzc1505362006-12-28 06:47:50 +0000377static PyTypeObject *arg_type;
378static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000379static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000380 "arg",
381 "annotation",
382};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000383static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000384static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000385static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000386 "arg",
387 "value",
388};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000389static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000390static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000392 "name",
393 "asname",
394};
395
396
Neal Norwitz207c9f32008-03-31 04:42:11 +0000397static int
398ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
399{
400 Py_ssize_t i, numfields = 0;
401 int res = -1;
402 PyObject *key, *value, *fields;
403 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
404 if (!fields)
405 PyErr_Clear();
406 if (fields) {
407 numfields = PySequence_Size(fields);
408 if (numfields == -1)
409 goto cleanup;
410 }
411 res = 0; /* if no error occurs, this stays 0 to the end */
412 if (PyTuple_GET_SIZE(args) > 0) {
413 if (numfields != PyTuple_GET_SIZE(args)) {
414 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000415 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000416 Py_TYPE(self)->tp_name,
417 numfields == 0 ? "" : "either 0 or ",
418 numfields, numfields == 1 ? "" : "s");
419 res = -1;
420 goto cleanup;
421 }
422 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
423 /* cannot be reached when fields is NULL */
424 PyObject *name = PySequence_GetItem(fields, i);
425 if (!name) {
426 res = -1;
427 goto cleanup;
428 }
429 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
430 Py_DECREF(name);
431 if (res < 0)
432 goto cleanup;
433 }
434 }
435 if (kw) {
436 i = 0; /* needed by PyDict_Next */
437 while (PyDict_Next(kw, &i, &key, &value)) {
438 res = PyObject_SetAttr(self, key, value);
439 if (res < 0)
440 goto cleanup;
441 }
442 }
443 cleanup:
444 Py_XDECREF(fields);
445 return res;
446}
447
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000448/* Pickling support */
449static PyObject *
450ast_type_reduce(PyObject *self, PyObject *unused)
451{
452 PyObject *res;
453 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
454 if (dict == NULL) {
455 if (PyErr_ExceptionMatches(PyExc_AttributeError))
456 PyErr_Clear();
457 else
458 return NULL;
459 }
460 if (dict) {
461 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
462 Py_DECREF(dict);
463 return res;
464 }
465 return Py_BuildValue("O()", Py_TYPE(self));
466}
467
468static PyMethodDef ast_type_methods[] = {
469 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
470 {NULL}
471};
472
Neal Norwitz207c9f32008-03-31 04:42:11 +0000473static PyTypeObject AST_type = {
474 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000475 "_ast.AST",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000476 sizeof(PyObject),
477 0,
478 0, /* tp_dealloc */
479 0, /* tp_print */
480 0, /* tp_getattr */
481 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000482 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000483 0, /* tp_repr */
484 0, /* tp_as_number */
485 0, /* tp_as_sequence */
486 0, /* tp_as_mapping */
487 0, /* tp_hash */
488 0, /* tp_call */
489 0, /* tp_str */
490 PyObject_GenericGetAttr, /* tp_getattro */
491 PyObject_GenericSetAttr, /* tp_setattro */
492 0, /* tp_as_buffer */
493 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
494 0, /* tp_doc */
495 0, /* tp_traverse */
496 0, /* tp_clear */
497 0, /* tp_richcompare */
498 0, /* tp_weaklistoffset */
499 0, /* tp_iter */
500 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000501 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000502 0, /* tp_members */
503 0, /* tp_getset */
504 0, /* tp_base */
505 0, /* tp_dict */
506 0, /* tp_descr_get */
507 0, /* tp_descr_set */
508 0, /* tp_dictoffset */
509 (initproc)ast_type_init, /* tp_init */
510 PyType_GenericAlloc, /* tp_alloc */
511 PyType_GenericNew, /* tp_new */
512 PyObject_Del, /* tp_free */
513};
514
515
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000516static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
517{
518 PyObject *fnames, *result;
519 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000520 fnames = PyTuple_New(num_fields);
521 if (!fnames) return NULL;
522 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000523 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000524 if (!field) {
525 Py_DECREF(fnames);
526 return NULL;
527 }
528 PyTuple_SET_ITEM(fnames, i, field);
529 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000530 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000531 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000532 Py_DECREF(fnames);
533 return (PyTypeObject*)result;
534}
535
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000536static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
537{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000538 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000539 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000540 if (!l)
541 return 0;
542 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000543 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000544 if (!s) {
545 Py_DECREF(l);
546 return 0;
547 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000548 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000549 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
551 Py_DECREF(l);
552 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000553}
554
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000555/* Conversion AST -> Python */
556
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000557static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
558{
559 int i, n = asdl_seq_LEN(seq);
560 PyObject *result = PyList_New(n);
561 PyObject *value;
562 if (!result)
563 return NULL;
564 for (i = 0; i < n; i++) {
565 value = func(asdl_seq_GET(seq, i));
566 if (!value) {
567 Py_DECREF(result);
568 return NULL;
569 }
570 PyList_SET_ITEM(result, i, value);
571 }
572 return result;
573}
574
575static PyObject* ast2obj_object(void *o)
576{
577 if (!o)
578 o = Py_None;
579 Py_INCREF((PyObject*)o);
580 return (PyObject*)o;
581}
582#define ast2obj_identifier ast2obj_object
583#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000584
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000585static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000586{
Christian Heimes217cfd12007-12-02 14:31:20 +0000587 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000588}
589
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000590/* Conversion Python -> AST */
591
592static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
593{
594 if (obj == Py_None)
595 obj = NULL;
596 if (obj)
597 PyArena_AddPyObject(arena, obj);
598 Py_XINCREF(obj);
599 *out = obj;
600 return 0;
601}
602
Benjamin Peterson180e6352011-07-22 11:09:07 -0500603static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500604{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500605 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
606 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500607 return 1;
608 }
609 return obj2ast_object(obj, out, arena);
610}
611
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500612static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
613{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400614 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500615 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
616 return 1;
617 }
618 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500619}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000620
621static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
622{
623 int i;
624 if (!PyLong_Check(obj)) {
625 PyObject *s = PyObject_Repr(obj);
626 if (s == NULL) return 1;
627 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Christian Heimes72b710a2008-05-26 13:28:38 +0000628 PyBytes_AS_STRING(s));
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000629 Py_DECREF(s);
630 return 1;
631 }
632
633 i = (int)PyLong_AsLong(obj);
634 if (i == -1 && PyErr_Occurred())
635 return 1;
636 *out = i;
637 return 0;
638}
639
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000640static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000641{
642 PyObject *empty_tuple, *d;
643 if (PyType_Ready(&AST_type) < 0)
644 return -1;
645 d = AST_type.tp_dict;
646 empty_tuple = PyTuple_New(0);
647 if (!empty_tuple ||
648 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
649 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
650 Py_XDECREF(empty_tuple);
651 return -1;
652 }
653 Py_DECREF(empty_tuple);
654 return 0;
655}
656
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000657
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658static int init_types(void)
659{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 if (initialized) return 1;
Benjamin Petersonce825f12008-10-24 23:11:02 +0000662 if (add_ast_fields() < 0) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000663 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664 if (!mod_type) return 0;
665 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Interactive_type = make_type("Interactive", mod_type,
669 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000671 Expression_type = make_type("Expression", mod_type, Expression_fields,
672 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000676 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000678 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000680 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000682 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000698 With_type = make_type("With", stmt_type, With_fields, 3);
699 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000700 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
705 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000707 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000712 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000716 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
717 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000718 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000720 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000722 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000724 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000726 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000728 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000737 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000741 Set_type = make_type("Set", expr_type, Set_fields, 1);
742 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000743 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000745 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
746 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000747 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
748 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000749 GeneratorExp_type = make_type("GeneratorExp", expr_type,
750 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000754 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000760 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000762 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
763 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000764 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
765 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000768 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000770 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
771 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000772 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000776 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000778 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!expr_context_type) return 0;
780 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000781 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000783 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000785 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000789 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000793 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000795 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000797 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000799 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000801 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000803 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000805 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!slice_type) return 0;
807 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000814 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!boolop_type) return 0;
816 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000817 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000819 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000821 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000823 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000825 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!operator_type) return 0;
827 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000828 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000830 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000832 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000834 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000836 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000838 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000840 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000842 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000844 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000846 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000848 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000850 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000852 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000854 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000856 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000858 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000860 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000862 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000864 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000866 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000868 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000870 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000872 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000874 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000876 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!unaryop_type) return 0;
878 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000879 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000881 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000883 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000885 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000887 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000889 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000890 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000891 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000893 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000895 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!cmpop_type) return 0;
897 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000898 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000900 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000902 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000904 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000906 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000908 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000910 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000912 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000914 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000916 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000917 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000918 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000919 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000920 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000922 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000924 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000926 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000928 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000930 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000931 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000932 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000933 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000934 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000935 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000936 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000937 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000938 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000939 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000940 if (!comprehension_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000941 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000942 if (!excepthandler_type) return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000943 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
944 return 0;
945 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
946 ExceptHandler_fields, 3);
947 if (!ExceptHandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000948 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000949 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000950 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000951 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000952 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000953 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000954 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000955 if (!alias_type) return 0;
956 initialized = 1;
957 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000958}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000959
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000960static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
961static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
962static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
963static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
964 arena);
965static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
966static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
967static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
968static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
969static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
970static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
971 arena);
972static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
973 arena);
974static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
975static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
976static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
977static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000980Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981{
982 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000983 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000984 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 p->kind = Module_kind;
987 p->v.Module.body = body;
988 return p;
989}
990
991mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000992Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993{
994 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000995 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000996 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 p->kind = Interactive_kind;
999 p->v.Interactive.body = body;
1000 return p;
1001}
1002
1003mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001004Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005{
1006 mod_ty p;
1007 if (!body) {
1008 PyErr_SetString(PyExc_ValueError,
1009 "field body is required for Expression");
1010 return NULL;
1011 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001013 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 p->kind = Expression_kind;
1016 p->v.Expression.body = body;
1017 return p;
1018}
1019
1020mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001021Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022{
1023 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001024 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001025 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 p->kind = Suite_kind;
1028 p->v.Suite.body = body;
1029 return p;
1030}
1031
1032stmt_ty
1033FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001034 decorator_list, expr_ty returns, int lineno, int col_offset,
1035 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036{
1037 stmt_ty p;
1038 if (!name) {
1039 PyErr_SetString(PyExc_ValueError,
1040 "field name is required for FunctionDef");
1041 return NULL;
1042 }
1043 if (!args) {
1044 PyErr_SetString(PyExc_ValueError,
1045 "field args is required for FunctionDef");
1046 return NULL;
1047 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001048 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001049 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 p->kind = FunctionDef_kind;
1052 p->v.FunctionDef.name = name;
1053 p->v.FunctionDef.args = args;
1054 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001055 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +00001056 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001058 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 return p;
1060}
1061
1062stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001063ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001064 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1065 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066{
1067 stmt_ty p;
1068 if (!name) {
1069 PyErr_SetString(PyExc_ValueError,
1070 "field name is required for ClassDef");
1071 return NULL;
1072 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001073 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001074 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 p->kind = ClassDef_kind;
1077 p->v.ClassDef.name = name;
1078 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001079 p->v.ClassDef.keywords = keywords;
1080 p->v.ClassDef.starargs = starargs;
1081 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001083 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001085 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return p;
1087}
1088
1089stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001090Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091{
1092 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001093 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001094 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 p->kind = Return_kind;
1097 p->v.Return.value = value;
1098 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001099 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 return p;
1101}
1102
1103stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001104Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105{
1106 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001107 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001108 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 p->kind = Delete_kind;
1111 p->v.Delete.targets = targets;
1112 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001113 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return p;
1115}
1116
1117stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001118Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1119 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120{
1121 stmt_ty p;
1122 if (!value) {
1123 PyErr_SetString(PyExc_ValueError,
1124 "field value is required for Assign");
1125 return NULL;
1126 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001128 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 p->kind = Assign_kind;
1131 p->v.Assign.targets = targets;
1132 p->v.Assign.value = value;
1133 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001134 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return p;
1136}
1137
1138stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001139AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1140 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141{
1142 stmt_ty p;
1143 if (!target) {
1144 PyErr_SetString(PyExc_ValueError,
1145 "field target is required for AugAssign");
1146 return NULL;
1147 }
1148 if (!op) {
1149 PyErr_SetString(PyExc_ValueError,
1150 "field op is required for AugAssign");
1151 return NULL;
1152 }
1153 if (!value) {
1154 PyErr_SetString(PyExc_ValueError,
1155 "field value is required for AugAssign");
1156 return NULL;
1157 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001158 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001159 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 p->kind = AugAssign_kind;
1162 p->v.AugAssign.target = target;
1163 p->v.AugAssign.op = op;
1164 p->v.AugAssign.value = value;
1165 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001166 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return p;
1168}
1169
1170stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001172 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173{
1174 stmt_ty p;
1175 if (!target) {
1176 PyErr_SetString(PyExc_ValueError,
1177 "field target is required for For");
1178 return NULL;
1179 }
1180 if (!iter) {
1181 PyErr_SetString(PyExc_ValueError,
1182 "field iter is required for For");
1183 return NULL;
1184 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001185 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001186 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 p->kind = For_kind;
1189 p->v.For.target = target;
1190 p->v.For.iter = iter;
1191 p->v.For.body = body;
1192 p->v.For.orelse = orelse;
1193 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001194 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return p;
1196}
1197
1198stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001199While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1200 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
1202 stmt_ty p;
1203 if (!test) {
1204 PyErr_SetString(PyExc_ValueError,
1205 "field test is required for While");
1206 return NULL;
1207 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001208 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001209 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 p->kind = While_kind;
1212 p->v.While.test = test;
1213 p->v.While.body = body;
1214 p->v.While.orelse = orelse;
1215 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001216 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 return p;
1218}
1219
1220stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001221If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1222 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223{
1224 stmt_ty p;
1225 if (!test) {
1226 PyErr_SetString(PyExc_ValueError,
1227 "field test is required for If");
1228 return NULL;
1229 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001230 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001231 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 p->kind = If_kind;
1234 p->v.If.test = test;
1235 p->v.If.body = body;
1236 p->v.If.orelse = orelse;
1237 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001238 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 return p;
1240}
1241
1242stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001243With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001244 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001245{
1246 stmt_ty p;
1247 if (!context_expr) {
1248 PyErr_SetString(PyExc_ValueError,
1249 "field context_expr is required for With");
1250 return NULL;
1251 }
1252 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001253 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001254 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001255 p->kind = With_kind;
1256 p->v.With.context_expr = context_expr;
1257 p->v.With.optional_vars = optional_vars;
1258 p->v.With.body = body;
1259 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001260 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001261 return p;
1262}
1263
1264stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001265Raise(expr_ty exc, expr_ty cause, int lineno, 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 = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001272 p->v.Raise.exc = exc;
1273 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001275 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 return p;
1277}
1278
1279stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001280TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282{
1283 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001285 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 p->kind = TryExcept_kind;
1288 p->v.TryExcept.body = body;
1289 p->v.TryExcept.handlers = handlers;
1290 p->v.TryExcept.orelse = orelse;
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 +00001297TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1298 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299{
1300 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001302 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 p->kind = TryFinally_kind;
1305 p->v.TryFinally.body = body;
1306 p->v.TryFinally.finalbody = finalbody;
1307 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001308 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 return p;
1310}
1311
1312stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001313Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314{
1315 stmt_ty p;
1316 if (!test) {
1317 PyErr_SetString(PyExc_ValueError,
1318 "field test is required for Assert");
1319 return NULL;
1320 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001321 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001322 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 p->kind = Assert_kind;
1325 p->v.Assert.test = test;
1326 p->v.Assert.msg = msg;
1327 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001328 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 return p;
1330}
1331
1332stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001333Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334{
1335 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001336 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001337 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 p->kind = Import_kind;
1340 p->v.Import.names = names;
1341 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001342 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 return p;
1344}
1345
1346stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001347ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1348 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349{
1350 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001351 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001352 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 p->kind = ImportFrom_kind;
1355 p->v.ImportFrom.module = module;
1356 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001357 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001359 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return p;
1361}
1362
1363stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001364Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365{
1366 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001367 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001368 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 p->kind = Global_kind;
1371 p->v.Global.names = names;
1372 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001373 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 return p;
1375}
1376
1377stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001378Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1379{
1380 stmt_ty p;
1381 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1382 if (!p)
1383 return NULL;
1384 p->kind = Nonlocal_kind;
1385 p->v.Nonlocal.names = names;
1386 p->lineno = lineno;
1387 p->col_offset = col_offset;
1388 return p;
1389}
1390
1391stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001392Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393{
1394 stmt_ty p;
1395 if (!value) {
1396 PyErr_SetString(PyExc_ValueError,
1397 "field value is required for Expr");
1398 return NULL;
1399 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001400 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001401 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 p->kind = Expr_kind;
1404 p->v.Expr.value = value;
1405 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 return p;
1408}
1409
1410stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001411Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412{
1413 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001415 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 p->kind = Pass_kind;
1418 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001419 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 return p;
1421}
1422
1423stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001424Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
1426 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001427 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001428 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 p->kind = Break_kind;
1431 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001432 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 return p;
1434}
1435
1436stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001437Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438{
1439 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001440 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001441 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 p->kind = Continue_kind;
1444 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001445 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 return p;
1447}
1448
1449expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1451 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
1453 expr_ty p;
1454 if (!op) {
1455 PyErr_SetString(PyExc_ValueError,
1456 "field op is required for BoolOp");
1457 return NULL;
1458 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001459 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001460 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 p->kind = BoolOp_kind;
1463 p->v.BoolOp.op = op;
1464 p->v.BoolOp.values = values;
1465 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001466 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 return p;
1468}
1469
1470expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001471BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1472 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473{
1474 expr_ty p;
1475 if (!left) {
1476 PyErr_SetString(PyExc_ValueError,
1477 "field left is required for BinOp");
1478 return NULL;
1479 }
1480 if (!op) {
1481 PyErr_SetString(PyExc_ValueError,
1482 "field op is required for BinOp");
1483 return NULL;
1484 }
1485 if (!right) {
1486 PyErr_SetString(PyExc_ValueError,
1487 "field right is required for BinOp");
1488 return NULL;
1489 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001490 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001491 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 p->kind = BinOp_kind;
1494 p->v.BinOp.left = left;
1495 p->v.BinOp.op = op;
1496 p->v.BinOp.right = right;
1497 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001498 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 return p;
1500}
1501
1502expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001503UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1504 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505{
1506 expr_ty p;
1507 if (!op) {
1508 PyErr_SetString(PyExc_ValueError,
1509 "field op is required for UnaryOp");
1510 return NULL;
1511 }
1512 if (!operand) {
1513 PyErr_SetString(PyExc_ValueError,
1514 "field operand is required for UnaryOp");
1515 return NULL;
1516 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001517 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001518 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 p->kind = UnaryOp_kind;
1521 p->v.UnaryOp.op = op;
1522 p->v.UnaryOp.operand = operand;
1523 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001524 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 return p;
1526}
1527
1528expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001529Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1530 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531{
1532 expr_ty p;
1533 if (!args) {
1534 PyErr_SetString(PyExc_ValueError,
1535 "field args is required for Lambda");
1536 return NULL;
1537 }
1538 if (!body) {
1539 PyErr_SetString(PyExc_ValueError,
1540 "field body is required for Lambda");
1541 return NULL;
1542 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001543 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001544 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 p->kind = Lambda_kind;
1547 p->v.Lambda.args = args;
1548 p->v.Lambda.body = body;
1549 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001550 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 return p;
1552}
1553
1554expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001555IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1556 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001557{
1558 expr_ty p;
1559 if (!test) {
1560 PyErr_SetString(PyExc_ValueError,
1561 "field test is required for IfExp");
1562 return NULL;
1563 }
1564 if (!body) {
1565 PyErr_SetString(PyExc_ValueError,
1566 "field body is required for IfExp");
1567 return NULL;
1568 }
1569 if (!orelse) {
1570 PyErr_SetString(PyExc_ValueError,
1571 "field orelse is required for IfExp");
1572 return NULL;
1573 }
1574 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001575 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001576 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001577 p->kind = IfExp_kind;
1578 p->v.IfExp.test = test;
1579 p->v.IfExp.body = body;
1580 p->v.IfExp.orelse = orelse;
1581 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001582 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001583 return p;
1584}
1585
1586expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001587Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1588 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589{
1590 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001591 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001592 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 p->kind = Dict_kind;
1595 p->v.Dict.keys = keys;
1596 p->v.Dict.values = values;
1597 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001598 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 return p;
1600}
1601
1602expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001603Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1604{
1605 expr_ty p;
1606 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001607 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001608 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001609 p->kind = Set_kind;
1610 p->v.Set.elts = elts;
1611 p->lineno = lineno;
1612 p->col_offset = col_offset;
1613 return p;
1614}
1615
1616expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001617ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1618 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619{
1620 expr_ty p;
1621 if (!elt) {
1622 PyErr_SetString(PyExc_ValueError,
1623 "field elt is required for ListComp");
1624 return NULL;
1625 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001626 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001627 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 p->kind = ListComp_kind;
1630 p->v.ListComp.elt = elt;
1631 p->v.ListComp.generators = generators;
1632 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001633 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 return p;
1635}
1636
1637expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001638SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1639 *arena)
1640{
1641 expr_ty p;
1642 if (!elt) {
1643 PyErr_SetString(PyExc_ValueError,
1644 "field elt is required for SetComp");
1645 return NULL;
1646 }
1647 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1648 if (!p)
1649 return NULL;
1650 p->kind = SetComp_kind;
1651 p->v.SetComp.elt = elt;
1652 p->v.SetComp.generators = generators;
1653 p->lineno = lineno;
1654 p->col_offset = col_offset;
1655 return p;
1656}
1657
1658expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001659DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1660 col_offset, PyArena *arena)
1661{
1662 expr_ty p;
1663 if (!key) {
1664 PyErr_SetString(PyExc_ValueError,
1665 "field key is required for DictComp");
1666 return NULL;
1667 }
1668 if (!value) {
1669 PyErr_SetString(PyExc_ValueError,
1670 "field value is required for DictComp");
1671 return NULL;
1672 }
1673 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1674 if (!p)
1675 return NULL;
1676 p->kind = DictComp_kind;
1677 p->v.DictComp.key = key;
1678 p->v.DictComp.value = value;
1679 p->v.DictComp.generators = generators;
1680 p->lineno = lineno;
1681 p->col_offset = col_offset;
1682 return p;
1683}
1684
1685expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001686GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1687 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688{
1689 expr_ty p;
1690 if (!elt) {
1691 PyErr_SetString(PyExc_ValueError,
1692 "field elt is required for GeneratorExp");
1693 return NULL;
1694 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001695 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001696 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 p->kind = GeneratorExp_kind;
1699 p->v.GeneratorExp.elt = elt;
1700 p->v.GeneratorExp.generators = generators;
1701 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001702 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 return p;
1704}
1705
1706expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001707Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708{
1709 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001710 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001711 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 p->kind = Yield_kind;
1714 p->v.Yield.value = value;
1715 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001716 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 return p;
1718}
1719
1720expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001721Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1722 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723{
1724 expr_ty p;
1725 if (!left) {
1726 PyErr_SetString(PyExc_ValueError,
1727 "field left is required for Compare");
1728 return NULL;
1729 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001730 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001731 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 p->kind = Compare_kind;
1734 p->v.Compare.left = left;
1735 p->v.Compare.ops = ops;
1736 p->v.Compare.comparators = comparators;
1737 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001738 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 return p;
1740}
1741
1742expr_ty
1743Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001744 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745{
1746 expr_ty p;
1747 if (!func) {
1748 PyErr_SetString(PyExc_ValueError,
1749 "field func is required for Call");
1750 return NULL;
1751 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001752 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001753 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755 p->kind = Call_kind;
1756 p->v.Call.func = func;
1757 p->v.Call.args = args;
1758 p->v.Call.keywords = keywords;
1759 p->v.Call.starargs = starargs;
1760 p->v.Call.kwargs = kwargs;
1761 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001762 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 return p;
1764}
1765
1766expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001767Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768{
1769 expr_ty p;
1770 if (!n) {
1771 PyErr_SetString(PyExc_ValueError,
1772 "field n is required for Num");
1773 return NULL;
1774 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001775 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001776 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778 p->kind = Num_kind;
1779 p->v.Num.n = n;
1780 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001781 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return p;
1783}
1784
1785expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001786Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787{
1788 expr_ty p;
1789 if (!s) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field s is required for Str");
1792 return NULL;
1793 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001794 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001795 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 p->kind = Str_kind;
1798 p->v.Str.s = s;
1799 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001800 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 return p;
1802}
1803
1804expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001805Bytes(string s, int lineno, int col_offset, PyArena *arena)
1806{
1807 expr_ty p;
1808 if (!s) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field s is required for Bytes");
1811 return NULL;
1812 }
1813 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001814 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001815 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001816 p->kind = Bytes_kind;
1817 p->v.Bytes.s = s;
1818 p->lineno = lineno;
1819 p->col_offset = col_offset;
1820 return p;
1821}
1822
1823expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001824Ellipsis(int lineno, int col_offset, PyArena *arena)
1825{
1826 expr_ty p;
1827 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001828 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001829 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001830 p->kind = Ellipsis_kind;
1831 p->lineno = lineno;
1832 p->col_offset = col_offset;
1833 return p;
1834}
1835
1836expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001837Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1838 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839{
1840 expr_ty p;
1841 if (!value) {
1842 PyErr_SetString(PyExc_ValueError,
1843 "field value is required for Attribute");
1844 return NULL;
1845 }
1846 if (!attr) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field attr is required for Attribute");
1849 return NULL;
1850 }
1851 if (!ctx) {
1852 PyErr_SetString(PyExc_ValueError,
1853 "field ctx is required for Attribute");
1854 return NULL;
1855 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001857 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 p->kind = Attribute_kind;
1860 p->v.Attribute.value = value;
1861 p->v.Attribute.attr = attr;
1862 p->v.Attribute.ctx = ctx;
1863 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001864 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return p;
1866}
1867
1868expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001869Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1870 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871{
1872 expr_ty p;
1873 if (!value) {
1874 PyErr_SetString(PyExc_ValueError,
1875 "field value is required for Subscript");
1876 return NULL;
1877 }
1878 if (!slice) {
1879 PyErr_SetString(PyExc_ValueError,
1880 "field slice is required for Subscript");
1881 return NULL;
1882 }
1883 if (!ctx) {
1884 PyErr_SetString(PyExc_ValueError,
1885 "field ctx is required for Subscript");
1886 return NULL;
1887 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001889 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 p->kind = Subscript_kind;
1892 p->v.Subscript.value = value;
1893 p->v.Subscript.slice = slice;
1894 p->v.Subscript.ctx = ctx;
1895 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001896 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 return p;
1898}
1899
1900expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001901Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1902 *arena)
1903{
1904 expr_ty p;
1905 if (!value) {
1906 PyErr_SetString(PyExc_ValueError,
1907 "field value is required for Starred");
1908 return NULL;
1909 }
1910 if (!ctx) {
1911 PyErr_SetString(PyExc_ValueError,
1912 "field ctx is required for Starred");
1913 return NULL;
1914 }
1915 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1916 if (!p)
1917 return NULL;
1918 p->kind = Starred_kind;
1919 p->v.Starred.value = value;
1920 p->v.Starred.ctx = ctx;
1921 p->lineno = lineno;
1922 p->col_offset = col_offset;
1923 return p;
1924}
1925
1926expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001927Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1928 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929{
1930 expr_ty p;
1931 if (!id) {
1932 PyErr_SetString(PyExc_ValueError,
1933 "field id is required for Name");
1934 return NULL;
1935 }
1936 if (!ctx) {
1937 PyErr_SetString(PyExc_ValueError,
1938 "field ctx is required for Name");
1939 return NULL;
1940 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001942 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 p->kind = Name_kind;
1945 p->v.Name.id = id;
1946 p->v.Name.ctx = ctx;
1947 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001948 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 return p;
1950}
1951
1952expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001953List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1954 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955{
1956 expr_ty p;
1957 if (!ctx) {
1958 PyErr_SetString(PyExc_ValueError,
1959 "field ctx is required for List");
1960 return NULL;
1961 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001962 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001963 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001965 p->kind = List_kind;
1966 p->v.List.elts = elts;
1967 p->v.List.ctx = ctx;
1968 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001969 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 return p;
1971}
1972
1973expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001974Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1975 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976{
1977 expr_ty p;
1978 if (!ctx) {
1979 PyErr_SetString(PyExc_ValueError,
1980 "field ctx is required for Tuple");
1981 return NULL;
1982 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001983 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001984 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 p->kind = Tuple_kind;
1987 p->v.Tuple.elts = elts;
1988 p->v.Tuple.ctx = ctx;
1989 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001990 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 return p;
1992}
1993
1994slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996{
1997 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001998 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001999 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 p->kind = Slice_kind;
2002 p->v.Slice.lower = lower;
2003 p->v.Slice.upper = upper;
2004 p->v.Slice.step = step;
2005 return p;
2006}
2007
2008slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010{
2011 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002012 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002013 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 p->kind = ExtSlice_kind;
2016 p->v.ExtSlice.dims = dims;
2017 return p;
2018}
2019
2020slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022{
2023 slice_ty p;
2024 if (!value) {
2025 PyErr_SetString(PyExc_ValueError,
2026 "field value is required for Index");
2027 return NULL;
2028 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002029 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002030 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 p->kind = Index_kind;
2033 p->v.Index.value = value;
2034 return p;
2035}
2036
2037comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039{
2040 comprehension_ty p;
2041 if (!target) {
2042 PyErr_SetString(PyExc_ValueError,
2043 "field target is required for comprehension");
2044 return NULL;
2045 }
2046 if (!iter) {
2047 PyErr_SetString(PyExc_ValueError,
2048 "field iter is required for comprehension");
2049 return NULL;
2050 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002051 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002052 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 p->target = target;
2055 p->iter = iter;
2056 p->ifs = ifs;
2057 return p;
2058}
2059
2060excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002061ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002062 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063{
2064 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002066 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002068 p->kind = ExceptHandler_kind;
2069 p->v.ExceptHandler.type = type;
2070 p->v.ExceptHandler.name = name;
2071 p->v.ExceptHandler.body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002072 p->lineno = lineno;
2073 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 return p;
2075}
2076
2077arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002078arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2079 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2080 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081{
2082 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002083 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002084 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 p->args = args;
2087 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002088 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002089 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002091 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002093 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 return p;
2095}
2096
Neal Norwitzc1505362006-12-28 06:47:50 +00002097arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002098arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002099{
2100 arg_ty p;
2101 if (!arg) {
2102 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002103 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002104 return NULL;
2105 }
2106 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002107 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002108 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002109 p->arg = arg;
2110 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002111 return p;
2112}
2113
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002115keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116{
2117 keyword_ty p;
2118 if (!arg) {
2119 PyErr_SetString(PyExc_ValueError,
2120 "field arg is required for keyword");
2121 return NULL;
2122 }
2123 if (!value) {
2124 PyErr_SetString(PyExc_ValueError,
2125 "field value is required for keyword");
2126 return NULL;
2127 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002128 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002129 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 p->arg = arg;
2132 p->value = value;
2133 return p;
2134}
2135
2136alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002137alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138{
2139 alias_ty p;
2140 if (!name) {
2141 PyErr_SetString(PyExc_ValueError,
2142 "field name is required for alias");
2143 return NULL;
2144 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002145 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002146 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 p->name = name;
2149 p->asname = asname;
2150 return p;
2151}
2152
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002153
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002154PyObject*
2155ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002156{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002157 mod_ty o = (mod_ty)_o;
2158 PyObject *result = NULL, *value = NULL;
2159 if (!o) {
2160 Py_INCREF(Py_None);
2161 return Py_None;
2162 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002163
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 switch (o->kind) {
2165 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002166 result = PyType_GenericNew(Module_type, NULL, NULL);
2167 if (!result) goto failed;
2168 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2169 if (!value) goto failed;
2170 if (PyObject_SetAttrString(result, "body", value) == -1)
2171 goto failed;
2172 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 break;
2174 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002175 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2176 if (!result) goto failed;
2177 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2178 if (!value) goto failed;
2179 if (PyObject_SetAttrString(result, "body", value) == -1)
2180 goto failed;
2181 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 break;
2183 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002184 result = PyType_GenericNew(Expression_type, NULL, NULL);
2185 if (!result) goto failed;
2186 value = ast2obj_expr(o->v.Expression.body);
2187 if (!value) goto failed;
2188 if (PyObject_SetAttrString(result, "body", value) == -1)
2189 goto failed;
2190 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 break;
2192 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002193 result = PyType_GenericNew(Suite_type, NULL, NULL);
2194 if (!result) goto failed;
2195 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2196 if (!value) goto failed;
2197 if (PyObject_SetAttrString(result, "body", value) == -1)
2198 goto failed;
2199 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 break;
2201 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002202 return result;
2203failed:
2204 Py_XDECREF(value);
2205 Py_XDECREF(result);
2206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207}
2208
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002209PyObject*
2210ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002212 stmt_ty o = (stmt_ty)_o;
2213 PyObject *result = NULL, *value = NULL;
2214 if (!o) {
2215 Py_INCREF(Py_None);
2216 return Py_None;
2217 }
2218
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 switch (o->kind) {
2220 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002221 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2222 if (!result) goto failed;
2223 value = ast2obj_identifier(o->v.FunctionDef.name);
2224 if (!value) goto failed;
2225 if (PyObject_SetAttrString(result, "name", value) == -1)
2226 goto failed;
2227 Py_DECREF(value);
2228 value = ast2obj_arguments(o->v.FunctionDef.args);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "args", value) == -1)
2231 goto failed;
2232 Py_DECREF(value);
2233 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "body", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002238 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2239 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002240 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002241 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2242 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002243 goto failed;
2244 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002245 value = ast2obj_expr(o->v.FunctionDef.returns);
2246 if (!value) goto failed;
2247 if (PyObject_SetAttrString(result, "returns", value) == -1)
2248 goto failed;
2249 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 break;
2251 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002252 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2253 if (!result) goto failed;
2254 value = ast2obj_identifier(o->v.ClassDef.name);
2255 if (!value) goto failed;
2256 if (PyObject_SetAttrString(result, "name", value) == -1)
2257 goto failed;
2258 Py_DECREF(value);
2259 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2260 if (!value) goto failed;
2261 if (PyObject_SetAttrString(result, "bases", value) == -1)
2262 goto failed;
2263 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002264 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2265 if (!value) goto failed;
2266 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2267 goto failed;
2268 Py_DECREF(value);
2269 value = ast2obj_expr(o->v.ClassDef.starargs);
2270 if (!value) goto failed;
2271 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2272 goto failed;
2273 Py_DECREF(value);
2274 value = ast2obj_expr(o->v.ClassDef.kwargs);
2275 if (!value) goto failed;
2276 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2277 goto failed;
2278 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002279 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2280 if (!value) goto failed;
2281 if (PyObject_SetAttrString(result, "body", value) == -1)
2282 goto failed;
2283 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002284 value = ast2obj_list(o->v.ClassDef.decorator_list,
2285 ast2obj_expr);
2286 if (!value) goto failed;
2287 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2288 -1)
2289 goto failed;
2290 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 break;
2292 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002293 result = PyType_GenericNew(Return_type, NULL, NULL);
2294 if (!result) goto failed;
2295 value = ast2obj_expr(o->v.Return.value);
2296 if (!value) goto failed;
2297 if (PyObject_SetAttrString(result, "value", value) == -1)
2298 goto failed;
2299 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 break;
2301 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002302 result = PyType_GenericNew(Delete_type, NULL, NULL);
2303 if (!result) goto failed;
2304 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2305 if (!value) goto failed;
2306 if (PyObject_SetAttrString(result, "targets", value) == -1)
2307 goto failed;
2308 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 break;
2310 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002311 result = PyType_GenericNew(Assign_type, NULL, NULL);
2312 if (!result) goto failed;
2313 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2314 if (!value) goto failed;
2315 if (PyObject_SetAttrString(result, "targets", value) == -1)
2316 goto failed;
2317 Py_DECREF(value);
2318 value = ast2obj_expr(o->v.Assign.value);
2319 if (!value) goto failed;
2320 if (PyObject_SetAttrString(result, "value", value) == -1)
2321 goto failed;
2322 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 break;
2324 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002325 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2326 if (!result) goto failed;
2327 value = ast2obj_expr(o->v.AugAssign.target);
2328 if (!value) goto failed;
2329 if (PyObject_SetAttrString(result, "target", value) == -1)
2330 goto failed;
2331 Py_DECREF(value);
2332 value = ast2obj_operator(o->v.AugAssign.op);
2333 if (!value) goto failed;
2334 if (PyObject_SetAttrString(result, "op", value) == -1)
2335 goto failed;
2336 Py_DECREF(value);
2337 value = ast2obj_expr(o->v.AugAssign.value);
2338 if (!value) goto failed;
2339 if (PyObject_SetAttrString(result, "value", value) == -1)
2340 goto failed;
2341 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002344 result = PyType_GenericNew(For_type, NULL, NULL);
2345 if (!result) goto failed;
2346 value = ast2obj_expr(o->v.For.target);
2347 if (!value) goto failed;
2348 if (PyObject_SetAttrString(result, "target", value) == -1)
2349 goto failed;
2350 Py_DECREF(value);
2351 value = ast2obj_expr(o->v.For.iter);
2352 if (!value) goto failed;
2353 if (PyObject_SetAttrString(result, "iter", value) == -1)
2354 goto failed;
2355 Py_DECREF(value);
2356 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2357 if (!value) goto failed;
2358 if (PyObject_SetAttrString(result, "body", value) == -1)
2359 goto failed;
2360 Py_DECREF(value);
2361 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2362 if (!value) goto failed;
2363 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2364 goto failed;
2365 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 break;
2367 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002368 result = PyType_GenericNew(While_type, NULL, NULL);
2369 if (!result) goto failed;
2370 value = ast2obj_expr(o->v.While.test);
2371 if (!value) goto failed;
2372 if (PyObject_SetAttrString(result, "test", value) == -1)
2373 goto failed;
2374 Py_DECREF(value);
2375 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2376 if (!value) goto failed;
2377 if (PyObject_SetAttrString(result, "body", value) == -1)
2378 goto failed;
2379 Py_DECREF(value);
2380 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2381 if (!value) goto failed;
2382 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2383 goto failed;
2384 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 break;
2386 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002387 result = PyType_GenericNew(If_type, NULL, NULL);
2388 if (!result) goto failed;
2389 value = ast2obj_expr(o->v.If.test);
2390 if (!value) goto failed;
2391 if (PyObject_SetAttrString(result, "test", value) == -1)
2392 goto failed;
2393 Py_DECREF(value);
2394 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2395 if (!value) goto failed;
2396 if (PyObject_SetAttrString(result, "body", value) == -1)
2397 goto failed;
2398 Py_DECREF(value);
2399 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2400 if (!value) goto failed;
2401 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2402 goto failed;
2403 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002405 case With_kind:
2406 result = PyType_GenericNew(With_type, NULL, NULL);
2407 if (!result) goto failed;
2408 value = ast2obj_expr(o->v.With.context_expr);
2409 if (!value) goto failed;
2410 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2411 goto failed;
2412 Py_DECREF(value);
2413 value = ast2obj_expr(o->v.With.optional_vars);
2414 if (!value) goto failed;
2415 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2416 -1)
2417 goto failed;
2418 Py_DECREF(value);
2419 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "body", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
2424 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426 result = PyType_GenericNew(Raise_type, NULL, NULL);
2427 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002428 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002430 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002431 goto failed;
2432 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002433 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002434 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002435 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002436 goto failed;
2437 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 break;
2439 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2441 if (!result) goto failed;
2442 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2443 if (!value) goto failed;
2444 if (PyObject_SetAttrString(result, "body", value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
2447 value = ast2obj_list(o->v.TryExcept.handlers,
2448 ast2obj_excepthandler);
2449 if (!value) goto failed;
2450 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2451 goto failed;
2452 Py_DECREF(value);
2453 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2454 if (!value) goto failed;
2455 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2456 goto failed;
2457 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 break;
2459 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002460 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2461 if (!result) goto failed;
2462 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2463 if (!value) goto failed;
2464 if (PyObject_SetAttrString(result, "body", value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
2467 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2468 if (!value) goto failed;
2469 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2470 goto failed;
2471 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 break;
2473 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002474 result = PyType_GenericNew(Assert_type, NULL, NULL);
2475 if (!result) goto failed;
2476 value = ast2obj_expr(o->v.Assert.test);
2477 if (!value) goto failed;
2478 if (PyObject_SetAttrString(result, "test", value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
2481 value = ast2obj_expr(o->v.Assert.msg);
2482 if (!value) goto failed;
2483 if (PyObject_SetAttrString(result, "msg", value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 break;
2487 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002488 result = PyType_GenericNew(Import_type, NULL, NULL);
2489 if (!result) goto failed;
2490 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2491 if (!value) goto failed;
2492 if (PyObject_SetAttrString(result, "names", value) == -1)
2493 goto failed;
2494 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 break;
2496 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002497 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2498 if (!result) goto failed;
2499 value = ast2obj_identifier(o->v.ImportFrom.module);
2500 if (!value) goto failed;
2501 if (PyObject_SetAttrString(result, "module", value) == -1)
2502 goto failed;
2503 Py_DECREF(value);
2504 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2505 if (!value) goto failed;
2506 if (PyObject_SetAttrString(result, "names", value) == -1)
2507 goto failed;
2508 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002509 value = ast2obj_int(o->v.ImportFrom.level);
2510 if (!value) goto failed;
2511 if (PyObject_SetAttrString(result, "level", value) == -1)
2512 goto failed;
2513 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002516 result = PyType_GenericNew(Global_type, NULL, NULL);
2517 if (!result) goto failed;
2518 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2519 if (!value) goto failed;
2520 if (PyObject_SetAttrString(result, "names", value) == -1)
2521 goto failed;
2522 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002524 case Nonlocal_kind:
2525 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2526 if (!result) goto failed;
2527 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2528 if (!value) goto failed;
2529 if (PyObject_SetAttrString(result, "names", value) == -1)
2530 goto failed;
2531 Py_DECREF(value);
2532 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002534 result = PyType_GenericNew(Expr_type, NULL, NULL);
2535 if (!result) goto failed;
2536 value = ast2obj_expr(o->v.Expr.value);
2537 if (!value) goto failed;
2538 if (PyObject_SetAttrString(result, "value", value) == -1)
2539 goto failed;
2540 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 break;
2542 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002543 result = PyType_GenericNew(Pass_type, NULL, NULL);
2544 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 break;
2546 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547 result = PyType_GenericNew(Break_type, NULL, NULL);
2548 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 break;
2550 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002551 result = PyType_GenericNew(Continue_type, NULL, NULL);
2552 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 break;
2554 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002555 value = ast2obj_int(o->lineno);
2556 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002557 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2558 goto failed;
2559 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002560 value = ast2obj_int(o->col_offset);
2561 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002562 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2563 goto failed;
2564 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002565 return result;
2566failed:
2567 Py_XDECREF(value);
2568 Py_XDECREF(result);
2569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570}
2571
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002572PyObject*
2573ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002575 expr_ty o = (expr_ty)_o;
2576 PyObject *result = NULL, *value = NULL;
2577 if (!o) {
2578 Py_INCREF(Py_None);
2579 return Py_None;
2580 }
2581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 switch (o->kind) {
2583 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002584 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2585 if (!result) goto failed;
2586 value = ast2obj_boolop(o->v.BoolOp.op);
2587 if (!value) goto failed;
2588 if (PyObject_SetAttrString(result, "op", value) == -1)
2589 goto failed;
2590 Py_DECREF(value);
2591 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2592 if (!value) goto failed;
2593 if (PyObject_SetAttrString(result, "values", value) == -1)
2594 goto failed;
2595 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 break;
2597 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002598 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2599 if (!result) goto failed;
2600 value = ast2obj_expr(o->v.BinOp.left);
2601 if (!value) goto failed;
2602 if (PyObject_SetAttrString(result, "left", value) == -1)
2603 goto failed;
2604 Py_DECREF(value);
2605 value = ast2obj_operator(o->v.BinOp.op);
2606 if (!value) goto failed;
2607 if (PyObject_SetAttrString(result, "op", value) == -1)
2608 goto failed;
2609 Py_DECREF(value);
2610 value = ast2obj_expr(o->v.BinOp.right);
2611 if (!value) goto failed;
2612 if (PyObject_SetAttrString(result, "right", value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 break;
2616 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002617 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2618 if (!result) goto failed;
2619 value = ast2obj_unaryop(o->v.UnaryOp.op);
2620 if (!value) goto failed;
2621 if (PyObject_SetAttrString(result, "op", value) == -1)
2622 goto failed;
2623 Py_DECREF(value);
2624 value = ast2obj_expr(o->v.UnaryOp.operand);
2625 if (!value) goto failed;
2626 if (PyObject_SetAttrString(result, "operand", value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 break;
2630 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002631 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2632 if (!result) goto failed;
2633 value = ast2obj_arguments(o->v.Lambda.args);
2634 if (!value) goto failed;
2635 if (PyObject_SetAttrString(result, "args", value) == -1)
2636 goto failed;
2637 Py_DECREF(value);
2638 value = ast2obj_expr(o->v.Lambda.body);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "body", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002644 case IfExp_kind:
2645 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2646 if (!result) goto failed;
2647 value = ast2obj_expr(o->v.IfExp.test);
2648 if (!value) goto failed;
2649 if (PyObject_SetAttrString(result, "test", value) == -1)
2650 goto failed;
2651 Py_DECREF(value);
2652 value = ast2obj_expr(o->v.IfExp.body);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "body", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 value = ast2obj_expr(o->v.IfExp.orelse);
2658 if (!value) goto failed;
2659 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002664 result = PyType_GenericNew(Dict_type, NULL, NULL);
2665 if (!result) goto failed;
2666 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2667 if (!value) goto failed;
2668 if (PyObject_SetAttrString(result, "keys", value) == -1)
2669 goto failed;
2670 Py_DECREF(value);
2671 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2672 if (!value) goto failed;
2673 if (PyObject_SetAttrString(result, "values", value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002677 case Set_kind:
2678 result = PyType_GenericNew(Set_type, NULL, NULL);
2679 if (!result) goto failed;
2680 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2681 if (!value) goto failed;
2682 if (PyObject_SetAttrString(result, "elts", value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002687 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2688 if (!result) goto failed;
2689 value = ast2obj_expr(o->v.ListComp.elt);
2690 if (!value) goto failed;
2691 if (PyObject_SetAttrString(result, "elt", value) == -1)
2692 goto failed;
2693 Py_DECREF(value);
2694 value = ast2obj_list(o->v.ListComp.generators,
2695 ast2obj_comprehension);
2696 if (!value) goto failed;
2697 if (PyObject_SetAttrString(result, "generators", value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002701 case SetComp_kind:
2702 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2703 if (!result) goto failed;
2704 value = ast2obj_expr(o->v.SetComp.elt);
2705 if (!value) goto failed;
2706 if (PyObject_SetAttrString(result, "elt", value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_list(o->v.SetComp.generators,
2710 ast2obj_comprehension);
2711 if (!value) goto failed;
2712 if (PyObject_SetAttrString(result, "generators", value) == -1)
2713 goto failed;
2714 Py_DECREF(value);
2715 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002716 case DictComp_kind:
2717 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2718 if (!result) goto failed;
2719 value = ast2obj_expr(o->v.DictComp.key);
2720 if (!value) goto failed;
2721 if (PyObject_SetAttrString(result, "key", value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 value = ast2obj_expr(o->v.DictComp.value);
2725 if (!value) goto failed;
2726 if (PyObject_SetAttrString(result, "value", value) == -1)
2727 goto failed;
2728 Py_DECREF(value);
2729 value = ast2obj_list(o->v.DictComp.generators,
2730 ast2obj_comprehension);
2731 if (!value) goto failed;
2732 if (PyObject_SetAttrString(result, "generators", value) == -1)
2733 goto failed;
2734 Py_DECREF(value);
2735 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002737 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2738 if (!result) goto failed;
2739 value = ast2obj_expr(o->v.GeneratorExp.elt);
2740 if (!value) goto failed;
2741 if (PyObject_SetAttrString(result, "elt", value) == -1)
2742 goto failed;
2743 Py_DECREF(value);
2744 value = ast2obj_list(o->v.GeneratorExp.generators,
2745 ast2obj_comprehension);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "generators", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750 break;
2751 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002752 result = PyType_GenericNew(Yield_type, NULL, NULL);
2753 if (!result) goto failed;
2754 value = ast2obj_expr(o->v.Yield.value);
2755 if (!value) goto failed;
2756 if (PyObject_SetAttrString(result, "value", value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 break;
2760 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002761 result = PyType_GenericNew(Compare_type, NULL, NULL);
2762 if (!result) goto failed;
2763 value = ast2obj_expr(o->v.Compare.left);
2764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "left", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002768 {
2769 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2770 value = PyList_New(n);
2771 if (!value) goto failed;
2772 for(i = 0; i < n; i++)
2773 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2774 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002775 if (!value) goto failed;
2776 if (PyObject_SetAttrString(result, "ops", value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2780 if (!value) goto failed;
2781 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 break;
2785 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002786 result = PyType_GenericNew(Call_type, NULL, NULL);
2787 if (!result) goto failed;
2788 value = ast2obj_expr(o->v.Call.func);
2789 if (!value) goto failed;
2790 if (PyObject_SetAttrString(result, "func", value) == -1)
2791 goto failed;
2792 Py_DECREF(value);
2793 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2794 if (!value) goto failed;
2795 if (PyObject_SetAttrString(result, "args", value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2799 if (!value) goto failed;
2800 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 value = ast2obj_expr(o->v.Call.starargs);
2804 if (!value) goto failed;
2805 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 value = ast2obj_expr(o->v.Call.kwargs);
2809 if (!value) goto failed;
2810 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2811 goto failed;
2812 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002815 result = PyType_GenericNew(Num_type, NULL, NULL);
2816 if (!result) goto failed;
2817 value = ast2obj_object(o->v.Num.n);
2818 if (!value) goto failed;
2819 if (PyObject_SetAttrString(result, "n", value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 break;
2823 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002824 result = PyType_GenericNew(Str_type, NULL, NULL);
2825 if (!result) goto failed;
2826 value = ast2obj_string(o->v.Str.s);
2827 if (!value) goto failed;
2828 if (PyObject_SetAttrString(result, "s", value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002832 case Bytes_kind:
2833 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2834 if (!result) goto failed;
2835 value = ast2obj_string(o->v.Bytes.s);
2836 if (!value) goto failed;
2837 if (PyObject_SetAttrString(result, "s", value) == -1)
2838 goto failed;
2839 Py_DECREF(value);
2840 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002841 case Ellipsis_kind:
2842 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2843 if (!result) goto failed;
2844 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002846 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2847 if (!result) goto failed;
2848 value = ast2obj_expr(o->v.Attribute.value);
2849 if (!value) goto failed;
2850 if (PyObject_SetAttrString(result, "value", value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 value = ast2obj_identifier(o->v.Attribute.attr);
2854 if (!value) goto failed;
2855 if (PyObject_SetAttrString(result, "attr", value) == -1)
2856 goto failed;
2857 Py_DECREF(value);
2858 value = ast2obj_expr_context(o->v.Attribute.ctx);
2859 if (!value) goto failed;
2860 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2861 goto failed;
2862 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 break;
2864 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002865 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2866 if (!result) goto failed;
2867 value = ast2obj_expr(o->v.Subscript.value);
2868 if (!value) goto failed;
2869 if (PyObject_SetAttrString(result, "value", value) == -1)
2870 goto failed;
2871 Py_DECREF(value);
2872 value = ast2obj_slice(o->v.Subscript.slice);
2873 if (!value) goto failed;
2874 if (PyObject_SetAttrString(result, "slice", value) == -1)
2875 goto failed;
2876 Py_DECREF(value);
2877 value = ast2obj_expr_context(o->v.Subscript.ctx);
2878 if (!value) goto failed;
2879 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2880 goto failed;
2881 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002883 case Starred_kind:
2884 result = PyType_GenericNew(Starred_type, NULL, NULL);
2885 if (!result) goto failed;
2886 value = ast2obj_expr(o->v.Starred.value);
2887 if (!value) goto failed;
2888 if (PyObject_SetAttrString(result, "value", value) == -1)
2889 goto failed;
2890 Py_DECREF(value);
2891 value = ast2obj_expr_context(o->v.Starred.ctx);
2892 if (!value) goto failed;
2893 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
2896 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002898 result = PyType_GenericNew(Name_type, NULL, NULL);
2899 if (!result) goto failed;
2900 value = ast2obj_identifier(o->v.Name.id);
2901 if (!value) goto failed;
2902 if (PyObject_SetAttrString(result, "id", value) == -1)
2903 goto failed;
2904 Py_DECREF(value);
2905 value = ast2obj_expr_context(o->v.Name.ctx);
2906 if (!value) goto failed;
2907 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2908 goto failed;
2909 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 break;
2911 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002912 result = PyType_GenericNew(List_type, NULL, NULL);
2913 if (!result) goto failed;
2914 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2915 if (!value) goto failed;
2916 if (PyObject_SetAttrString(result, "elts", value) == -1)
2917 goto failed;
2918 Py_DECREF(value);
2919 value = ast2obj_expr_context(o->v.List.ctx);
2920 if (!value) goto failed;
2921 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2922 goto failed;
2923 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 break;
2925 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002926 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2927 if (!result) goto failed;
2928 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2929 if (!value) goto failed;
2930 if (PyObject_SetAttrString(result, "elts", value) == -1)
2931 goto failed;
2932 Py_DECREF(value);
2933 value = ast2obj_expr_context(o->v.Tuple.ctx);
2934 if (!value) goto failed;
2935 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2936 goto failed;
2937 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 break;
2939 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002940 value = ast2obj_int(o->lineno);
2941 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002942 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2943 goto failed;
2944 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002945 value = ast2obj_int(o->col_offset);
2946 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002947 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2948 goto failed;
2949 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002950 return result;
2951failed:
2952 Py_XDECREF(value);
2953 Py_XDECREF(result);
2954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955}
2956
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002957PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002959 switch(o) {
2960 case Load:
2961 Py_INCREF(Load_singleton);
2962 return Load_singleton;
2963 case Store:
2964 Py_INCREF(Store_singleton);
2965 return Store_singleton;
2966 case Del:
2967 Py_INCREF(Del_singleton);
2968 return Del_singleton;
2969 case AugLoad:
2970 Py_INCREF(AugLoad_singleton);
2971 return AugLoad_singleton;
2972 case AugStore:
2973 Py_INCREF(AugStore_singleton);
2974 return AugStore_singleton;
2975 case Param:
2976 Py_INCREF(Param_singleton);
2977 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002978 default:
2979 /* should never happen, but just in case ... */
2980 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002984PyObject*
2985ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002987 slice_ty o = (slice_ty)_o;
2988 PyObject *result = NULL, *value = NULL;
2989 if (!o) {
2990 Py_INCREF(Py_None);
2991 return Py_None;
2992 }
2993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002996 result = PyType_GenericNew(Slice_type, NULL, NULL);
2997 if (!result) goto failed;
2998 value = ast2obj_expr(o->v.Slice.lower);
2999 if (!value) goto failed;
3000 if (PyObject_SetAttrString(result, "lower", value) == -1)
3001 goto failed;
3002 Py_DECREF(value);
3003 value = ast2obj_expr(o->v.Slice.upper);
3004 if (!value) goto failed;
3005 if (PyObject_SetAttrString(result, "upper", value) == -1)
3006 goto failed;
3007 Py_DECREF(value);
3008 value = ast2obj_expr(o->v.Slice.step);
3009 if (!value) goto failed;
3010 if (PyObject_SetAttrString(result, "step", value) == -1)
3011 goto failed;
3012 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 break;
3014 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003015 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3016 if (!result) goto failed;
3017 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3018 if (!value) goto failed;
3019 if (PyObject_SetAttrString(result, "dims", value) == -1)
3020 goto failed;
3021 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 break;
3023 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003024 result = PyType_GenericNew(Index_type, NULL, NULL);
3025 if (!result) goto failed;
3026 value = ast2obj_expr(o->v.Index.value);
3027 if (!value) goto failed;
3028 if (PyObject_SetAttrString(result, "value", value) == -1)
3029 goto failed;
3030 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 break;
3032 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003033 return result;
3034failed:
3035 Py_XDECREF(value);
3036 Py_XDECREF(result);
3037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038}
3039
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003040PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003042 switch(o) {
3043 case And:
3044 Py_INCREF(And_singleton);
3045 return And_singleton;
3046 case Or:
3047 Py_INCREF(Or_singleton);
3048 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003049 default:
3050 /* should never happen, but just in case ... */
3051 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003054}
3055PyObject* ast2obj_operator(operator_ty o)
3056{
3057 switch(o) {
3058 case Add:
3059 Py_INCREF(Add_singleton);
3060 return Add_singleton;
3061 case Sub:
3062 Py_INCREF(Sub_singleton);
3063 return Sub_singleton;
3064 case Mult:
3065 Py_INCREF(Mult_singleton);
3066 return Mult_singleton;
3067 case Div:
3068 Py_INCREF(Div_singleton);
3069 return Div_singleton;
3070 case Mod:
3071 Py_INCREF(Mod_singleton);
3072 return Mod_singleton;
3073 case Pow:
3074 Py_INCREF(Pow_singleton);
3075 return Pow_singleton;
3076 case LShift:
3077 Py_INCREF(LShift_singleton);
3078 return LShift_singleton;
3079 case RShift:
3080 Py_INCREF(RShift_singleton);
3081 return RShift_singleton;
3082 case BitOr:
3083 Py_INCREF(BitOr_singleton);
3084 return BitOr_singleton;
3085 case BitXor:
3086 Py_INCREF(BitXor_singleton);
3087 return BitXor_singleton;
3088 case BitAnd:
3089 Py_INCREF(BitAnd_singleton);
3090 return BitAnd_singleton;
3091 case FloorDiv:
3092 Py_INCREF(FloorDiv_singleton);
3093 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003094 default:
3095 /* should never happen, but just in case ... */
3096 PyErr_Format(PyExc_SystemError, "unknown operator found");
3097 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003098 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003099}
3100PyObject* ast2obj_unaryop(unaryop_ty o)
3101{
3102 switch(o) {
3103 case Invert:
3104 Py_INCREF(Invert_singleton);
3105 return Invert_singleton;
3106 case Not:
3107 Py_INCREF(Not_singleton);
3108 return Not_singleton;
3109 case UAdd:
3110 Py_INCREF(UAdd_singleton);
3111 return UAdd_singleton;
3112 case USub:
3113 Py_INCREF(USub_singleton);
3114 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003115 default:
3116 /* should never happen, but just in case ... */
3117 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3118 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003119 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003120}
3121PyObject* ast2obj_cmpop(cmpop_ty o)
3122{
3123 switch(o) {
3124 case Eq:
3125 Py_INCREF(Eq_singleton);
3126 return Eq_singleton;
3127 case NotEq:
3128 Py_INCREF(NotEq_singleton);
3129 return NotEq_singleton;
3130 case Lt:
3131 Py_INCREF(Lt_singleton);
3132 return Lt_singleton;
3133 case LtE:
3134 Py_INCREF(LtE_singleton);
3135 return LtE_singleton;
3136 case Gt:
3137 Py_INCREF(Gt_singleton);
3138 return Gt_singleton;
3139 case GtE:
3140 Py_INCREF(GtE_singleton);
3141 return GtE_singleton;
3142 case Is:
3143 Py_INCREF(Is_singleton);
3144 return Is_singleton;
3145 case IsNot:
3146 Py_INCREF(IsNot_singleton);
3147 return IsNot_singleton;
3148 case In:
3149 Py_INCREF(In_singleton);
3150 return In_singleton;
3151 case NotIn:
3152 Py_INCREF(NotIn_singleton);
3153 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003154 default:
3155 /* should never happen, but just in case ... */
3156 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3157 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003158 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003159}
3160PyObject*
3161ast2obj_comprehension(void* _o)
3162{
3163 comprehension_ty o = (comprehension_ty)_o;
3164 PyObject *result = NULL, *value = NULL;
3165 if (!o) {
3166 Py_INCREF(Py_None);
3167 return Py_None;
3168 }
3169
3170 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3171 if (!result) return NULL;
3172 value = ast2obj_expr(o->target);
3173 if (!value) goto failed;
3174 if (PyObject_SetAttrString(result, "target", value) == -1)
3175 goto failed;
3176 Py_DECREF(value);
3177 value = ast2obj_expr(o->iter);
3178 if (!value) goto failed;
3179 if (PyObject_SetAttrString(result, "iter", value) == -1)
3180 goto failed;
3181 Py_DECREF(value);
3182 value = ast2obj_list(o->ifs, ast2obj_expr);
3183 if (!value) goto failed;
3184 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3185 goto failed;
3186 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003187 return result;
3188failed:
3189 Py_XDECREF(value);
3190 Py_XDECREF(result);
3191 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192}
3193
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003194PyObject*
3195ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003197 excepthandler_ty o = (excepthandler_ty)_o;
3198 PyObject *result = NULL, *value = NULL;
3199 if (!o) {
3200 Py_INCREF(Py_None);
3201 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003203
Neal Norwitzad74aa82008-03-31 05:14:30 +00003204 switch (o->kind) {
3205 case ExceptHandler_kind:
3206 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3207 if (!result) goto failed;
3208 value = ast2obj_expr(o->v.ExceptHandler.type);
3209 if (!value) goto failed;
3210 if (PyObject_SetAttrString(result, "type", value) == -1)
3211 goto failed;
3212 Py_DECREF(value);
3213 value = ast2obj_identifier(o->v.ExceptHandler.name);
3214 if (!value) goto failed;
3215 if (PyObject_SetAttrString(result, "name", value) == -1)
3216 goto failed;
3217 Py_DECREF(value);
3218 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3219 if (!value) goto failed;
3220 if (PyObject_SetAttrString(result, "body", value) == -1)
3221 goto failed;
3222 Py_DECREF(value);
3223 break;
3224 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003225 value = ast2obj_int(o->lineno);
3226 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003227 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228 goto failed;
3229 Py_DECREF(value);
3230 value = ast2obj_int(o->col_offset);
3231 if (!value) goto failed;
Neal Norwitzad74aa82008-03-31 05:14:30 +00003232 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003233 goto failed;
3234 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003235 return result;
3236failed:
3237 Py_XDECREF(value);
3238 Py_XDECREF(result);
3239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240}
3241
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003242PyObject*
3243ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003245 arguments_ty o = (arguments_ty)_o;
3246 PyObject *result = NULL, *value = NULL;
3247 if (!o) {
3248 Py_INCREF(Py_None);
3249 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003251
3252 result = PyType_GenericNew(arguments_type, NULL, NULL);
3253 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003254 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003255 if (!value) goto failed;
3256 if (PyObject_SetAttrString(result, "args", value) == -1)
3257 goto failed;
3258 Py_DECREF(value);
3259 value = ast2obj_identifier(o->vararg);
3260 if (!value) goto failed;
3261 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3262 goto failed;
3263 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003264 value = ast2obj_expr(o->varargannotation);
3265 if (!value) goto failed;
3266 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3267 goto failed;
3268 Py_DECREF(value);
3269 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003270 if (!value) goto failed;
3271 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3272 goto failed;
3273 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003274 value = ast2obj_identifier(o->kwarg);
3275 if (!value) goto failed;
3276 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3277 goto failed;
3278 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003279 value = ast2obj_expr(o->kwargannotation);
3280 if (!value) goto failed;
3281 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3282 goto failed;
3283 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003284 value = ast2obj_list(o->defaults, ast2obj_expr);
3285 if (!value) goto failed;
3286 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3287 goto failed;
3288 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003289 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3290 if (!value) goto failed;
3291 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3292 goto failed;
3293 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003294 return result;
3295failed:
3296 Py_XDECREF(value);
3297 Py_XDECREF(result);
3298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
3300
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003301PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003302ast2obj_arg(void* _o)
3303{
3304 arg_ty o = (arg_ty)_o;
3305 PyObject *result = NULL, *value = NULL;
3306 if (!o) {
3307 Py_INCREF(Py_None);
3308 return Py_None;
3309 }
3310
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003311 result = PyType_GenericNew(arg_type, NULL, NULL);
3312 if (!result) return NULL;
3313 value = ast2obj_identifier(o->arg);
3314 if (!value) goto failed;
3315 if (PyObject_SetAttrString(result, "arg", value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
3318 value = ast2obj_expr(o->annotation);
3319 if (!value) goto failed;
3320 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3321 goto failed;
3322 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003323 return result;
3324failed:
3325 Py_XDECREF(value);
3326 Py_XDECREF(result);
3327 return NULL;
3328}
3329
3330PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003331ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003333 keyword_ty o = (keyword_ty)_o;
3334 PyObject *result = NULL, *value = NULL;
3335 if (!o) {
3336 Py_INCREF(Py_None);
3337 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003339
3340 result = PyType_GenericNew(keyword_type, NULL, NULL);
3341 if (!result) return NULL;
3342 value = ast2obj_identifier(o->arg);
3343 if (!value) goto failed;
3344 if (PyObject_SetAttrString(result, "arg", value) == -1)
3345 goto failed;
3346 Py_DECREF(value);
3347 value = ast2obj_expr(o->value);
3348 if (!value) goto failed;
3349 if (PyObject_SetAttrString(result, "value", value) == -1)
3350 goto failed;
3351 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003352 return result;
3353failed:
3354 Py_XDECREF(value);
3355 Py_XDECREF(result);
3356 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357}
3358
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003359PyObject*
3360ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003362 alias_ty o = (alias_ty)_o;
3363 PyObject *result = NULL, *value = NULL;
3364 if (!o) {
3365 Py_INCREF(Py_None);
3366 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003368
3369 result = PyType_GenericNew(alias_type, NULL, NULL);
3370 if (!result) return NULL;
3371 value = ast2obj_identifier(o->name);
3372 if (!value) goto failed;
3373 if (PyObject_SetAttrString(result, "name", value) == -1)
3374 goto failed;
3375 Py_DECREF(value);
3376 value = ast2obj_identifier(o->asname);
3377 if (!value) goto failed;
3378 if (PyObject_SetAttrString(result, "asname", value) == -1)
3379 goto failed;
3380 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003381 return result;
3382failed:
3383 Py_XDECREF(value);
3384 Py_XDECREF(result);
3385 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386}
3387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003389int
3390obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3391{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003392 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003393
Benjamin Petersond8f65972010-11-20 04:31:07 +00003394 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003395
3396 if (obj == Py_None) {
3397 *out = NULL;
3398 return 0;
3399 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003400 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3401 if (isinstance == -1) {
3402 return 1;
3403 }
3404 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003405 asdl_seq* body;
3406
3407 if (PyObject_HasAttrString(obj, "body")) {
3408 int res;
3409 Py_ssize_t len;
3410 Py_ssize_t i;
3411 tmp = PyObject_GetAttrString(obj, "body");
3412 if (tmp == NULL) goto failed;
3413 if (!PyList_Check(tmp)) {
3414 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3415 goto failed;
3416 }
3417 len = PyList_GET_SIZE(tmp);
3418 body = asdl_seq_new(len, arena);
3419 if (body == NULL) goto failed;
3420 for (i = 0; i < len; i++) {
3421 stmt_ty value;
3422 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3423 if (res != 0) goto failed;
3424 asdl_seq_SET(body, i, value);
3425 }
3426 Py_XDECREF(tmp);
3427 tmp = NULL;
3428 } else {
3429 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3430 return 1;
3431 }
3432 *out = Module(body, arena);
3433 if (*out == NULL) goto failed;
3434 return 0;
3435 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003436 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3437 if (isinstance == -1) {
3438 return 1;
3439 }
3440 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003441 asdl_seq* body;
3442
3443 if (PyObject_HasAttrString(obj, "body")) {
3444 int res;
3445 Py_ssize_t len;
3446 Py_ssize_t i;
3447 tmp = PyObject_GetAttrString(obj, "body");
3448 if (tmp == NULL) goto failed;
3449 if (!PyList_Check(tmp)) {
3450 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3451 goto failed;
3452 }
3453 len = PyList_GET_SIZE(tmp);
3454 body = asdl_seq_new(len, arena);
3455 if (body == NULL) goto failed;
3456 for (i = 0; i < len; i++) {
3457 stmt_ty value;
3458 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3459 if (res != 0) goto failed;
3460 asdl_seq_SET(body, i, value);
3461 }
3462 Py_XDECREF(tmp);
3463 tmp = NULL;
3464 } else {
3465 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3466 return 1;
3467 }
3468 *out = Interactive(body, arena);
3469 if (*out == NULL) goto failed;
3470 return 0;
3471 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003472 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3473 if (isinstance == -1) {
3474 return 1;
3475 }
3476 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003477 expr_ty body;
3478
3479 if (PyObject_HasAttrString(obj, "body")) {
3480 int res;
3481 tmp = PyObject_GetAttrString(obj, "body");
3482 if (tmp == NULL) goto failed;
3483 res = obj2ast_expr(tmp, &body, arena);
3484 if (res != 0) goto failed;
3485 Py_XDECREF(tmp);
3486 tmp = NULL;
3487 } else {
3488 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3489 return 1;
3490 }
3491 *out = Expression(body, arena);
3492 if (*out == NULL) goto failed;
3493 return 0;
3494 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003495 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3496 if (isinstance == -1) {
3497 return 1;
3498 }
3499 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003500 asdl_seq* body;
3501
3502 if (PyObject_HasAttrString(obj, "body")) {
3503 int res;
3504 Py_ssize_t len;
3505 Py_ssize_t i;
3506 tmp = PyObject_GetAttrString(obj, "body");
3507 if (tmp == NULL) goto failed;
3508 if (!PyList_Check(tmp)) {
3509 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3510 goto failed;
3511 }
3512 len = PyList_GET_SIZE(tmp);
3513 body = asdl_seq_new(len, arena);
3514 if (body == NULL) goto failed;
3515 for (i = 0; i < len; i++) {
3516 stmt_ty value;
3517 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3518 if (res != 0) goto failed;
3519 asdl_seq_SET(body, i, value);
3520 }
3521 Py_XDECREF(tmp);
3522 tmp = NULL;
3523 } else {
3524 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3525 return 1;
3526 }
3527 *out = Suite(body, arena);
3528 if (*out == NULL) goto failed;
3529 return 0;
3530 }
3531
Benjamin Peterson5b066812010-11-20 01:38:49 +00003532 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3533 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003534 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003535 return 1;
3536}
3537
3538int
3539obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3540{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003541 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003542
Benjamin Petersond8f65972010-11-20 04:31:07 +00003543 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003544 int lineno;
3545 int col_offset;
3546
3547 if (obj == Py_None) {
3548 *out = NULL;
3549 return 0;
3550 }
3551 if (PyObject_HasAttrString(obj, "lineno")) {
3552 int res;
3553 tmp = PyObject_GetAttrString(obj, "lineno");
3554 if (tmp == NULL) goto failed;
3555 res = obj2ast_int(tmp, &lineno, arena);
3556 if (res != 0) goto failed;
3557 Py_XDECREF(tmp);
3558 tmp = NULL;
3559 } else {
3560 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3561 return 1;
3562 }
3563 if (PyObject_HasAttrString(obj, "col_offset")) {
3564 int res;
3565 tmp = PyObject_GetAttrString(obj, "col_offset");
3566 if (tmp == NULL) goto failed;
3567 res = obj2ast_int(tmp, &col_offset, arena);
3568 if (res != 0) goto failed;
3569 Py_XDECREF(tmp);
3570 tmp = NULL;
3571 } else {
3572 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3573 return 1;
3574 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003575 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3576 if (isinstance == -1) {
3577 return 1;
3578 }
3579 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003580 identifier name;
3581 arguments_ty args;
3582 asdl_seq* body;
3583 asdl_seq* decorator_list;
3584 expr_ty returns;
3585
3586 if (PyObject_HasAttrString(obj, "name")) {
3587 int res;
3588 tmp = PyObject_GetAttrString(obj, "name");
3589 if (tmp == NULL) goto failed;
3590 res = obj2ast_identifier(tmp, &name, arena);
3591 if (res != 0) goto failed;
3592 Py_XDECREF(tmp);
3593 tmp = NULL;
3594 } else {
3595 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3596 return 1;
3597 }
3598 if (PyObject_HasAttrString(obj, "args")) {
3599 int res;
3600 tmp = PyObject_GetAttrString(obj, "args");
3601 if (tmp == NULL) goto failed;
3602 res = obj2ast_arguments(tmp, &args, arena);
3603 if (res != 0) goto failed;
3604 Py_XDECREF(tmp);
3605 tmp = NULL;
3606 } else {
3607 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3608 return 1;
3609 }
3610 if (PyObject_HasAttrString(obj, "body")) {
3611 int res;
3612 Py_ssize_t len;
3613 Py_ssize_t i;
3614 tmp = PyObject_GetAttrString(obj, "body");
3615 if (tmp == NULL) goto failed;
3616 if (!PyList_Check(tmp)) {
3617 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3618 goto failed;
3619 }
3620 len = PyList_GET_SIZE(tmp);
3621 body = asdl_seq_new(len, arena);
3622 if (body == NULL) goto failed;
3623 for (i = 0; i < len; i++) {
3624 stmt_ty value;
3625 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3626 if (res != 0) goto failed;
3627 asdl_seq_SET(body, i, value);
3628 }
3629 Py_XDECREF(tmp);
3630 tmp = NULL;
3631 } else {
3632 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3633 return 1;
3634 }
3635 if (PyObject_HasAttrString(obj, "decorator_list")) {
3636 int res;
3637 Py_ssize_t len;
3638 Py_ssize_t i;
3639 tmp = PyObject_GetAttrString(obj, "decorator_list");
3640 if (tmp == NULL) goto failed;
3641 if (!PyList_Check(tmp)) {
3642 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3643 goto failed;
3644 }
3645 len = PyList_GET_SIZE(tmp);
3646 decorator_list = asdl_seq_new(len, arena);
3647 if (decorator_list == NULL) goto failed;
3648 for (i = 0; i < len; i++) {
3649 expr_ty value;
3650 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3651 if (res != 0) goto failed;
3652 asdl_seq_SET(decorator_list, i, value);
3653 }
3654 Py_XDECREF(tmp);
3655 tmp = NULL;
3656 } else {
3657 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3658 return 1;
3659 }
3660 if (PyObject_HasAttrString(obj, "returns")) {
3661 int res;
3662 tmp = PyObject_GetAttrString(obj, "returns");
3663 if (tmp == NULL) goto failed;
3664 res = obj2ast_expr(tmp, &returns, arena);
3665 if (res != 0) goto failed;
3666 Py_XDECREF(tmp);
3667 tmp = NULL;
3668 } else {
3669 returns = NULL;
3670 }
3671 *out = FunctionDef(name, args, body, decorator_list, returns,
3672 lineno, col_offset, arena);
3673 if (*out == NULL) goto failed;
3674 return 0;
3675 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003676 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3677 if (isinstance == -1) {
3678 return 1;
3679 }
3680 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003681 identifier name;
3682 asdl_seq* bases;
3683 asdl_seq* keywords;
3684 expr_ty starargs;
3685 expr_ty kwargs;
3686 asdl_seq* body;
3687 asdl_seq* decorator_list;
3688
3689 if (PyObject_HasAttrString(obj, "name")) {
3690 int res;
3691 tmp = PyObject_GetAttrString(obj, "name");
3692 if (tmp == NULL) goto failed;
3693 res = obj2ast_identifier(tmp, &name, arena);
3694 if (res != 0) goto failed;
3695 Py_XDECREF(tmp);
3696 tmp = NULL;
3697 } else {
3698 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3699 return 1;
3700 }
3701 if (PyObject_HasAttrString(obj, "bases")) {
3702 int res;
3703 Py_ssize_t len;
3704 Py_ssize_t i;
3705 tmp = PyObject_GetAttrString(obj, "bases");
3706 if (tmp == NULL) goto failed;
3707 if (!PyList_Check(tmp)) {
3708 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3709 goto failed;
3710 }
3711 len = PyList_GET_SIZE(tmp);
3712 bases = asdl_seq_new(len, arena);
3713 if (bases == NULL) goto failed;
3714 for (i = 0; i < len; i++) {
3715 expr_ty value;
3716 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3717 if (res != 0) goto failed;
3718 asdl_seq_SET(bases, i, value);
3719 }
3720 Py_XDECREF(tmp);
3721 tmp = NULL;
3722 } else {
3723 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3724 return 1;
3725 }
3726 if (PyObject_HasAttrString(obj, "keywords")) {
3727 int res;
3728 Py_ssize_t len;
3729 Py_ssize_t i;
3730 tmp = PyObject_GetAttrString(obj, "keywords");
3731 if (tmp == NULL) goto failed;
3732 if (!PyList_Check(tmp)) {
3733 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3734 goto failed;
3735 }
3736 len = PyList_GET_SIZE(tmp);
3737 keywords = asdl_seq_new(len, arena);
3738 if (keywords == NULL) goto failed;
3739 for (i = 0; i < len; i++) {
3740 keyword_ty value;
3741 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3742 if (res != 0) goto failed;
3743 asdl_seq_SET(keywords, i, value);
3744 }
3745 Py_XDECREF(tmp);
3746 tmp = NULL;
3747 } else {
3748 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3749 return 1;
3750 }
3751 if (PyObject_HasAttrString(obj, "starargs")) {
3752 int res;
3753 tmp = PyObject_GetAttrString(obj, "starargs");
3754 if (tmp == NULL) goto failed;
3755 res = obj2ast_expr(tmp, &starargs, arena);
3756 if (res != 0) goto failed;
3757 Py_XDECREF(tmp);
3758 tmp = NULL;
3759 } else {
3760 starargs = NULL;
3761 }
3762 if (PyObject_HasAttrString(obj, "kwargs")) {
3763 int res;
3764 tmp = PyObject_GetAttrString(obj, "kwargs");
3765 if (tmp == NULL) goto failed;
3766 res = obj2ast_expr(tmp, &kwargs, arena);
3767 if (res != 0) goto failed;
3768 Py_XDECREF(tmp);
3769 tmp = NULL;
3770 } else {
3771 kwargs = NULL;
3772 }
3773 if (PyObject_HasAttrString(obj, "body")) {
3774 int res;
3775 Py_ssize_t len;
3776 Py_ssize_t i;
3777 tmp = PyObject_GetAttrString(obj, "body");
3778 if (tmp == NULL) goto failed;
3779 if (!PyList_Check(tmp)) {
3780 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3781 goto failed;
3782 }
3783 len = PyList_GET_SIZE(tmp);
3784 body = asdl_seq_new(len, arena);
3785 if (body == NULL) goto failed;
3786 for (i = 0; i < len; i++) {
3787 stmt_ty value;
3788 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3789 if (res != 0) goto failed;
3790 asdl_seq_SET(body, i, value);
3791 }
3792 Py_XDECREF(tmp);
3793 tmp = NULL;
3794 } else {
3795 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3796 return 1;
3797 }
3798 if (PyObject_HasAttrString(obj, "decorator_list")) {
3799 int res;
3800 Py_ssize_t len;
3801 Py_ssize_t i;
3802 tmp = PyObject_GetAttrString(obj, "decorator_list");
3803 if (tmp == NULL) goto failed;
3804 if (!PyList_Check(tmp)) {
3805 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3806 goto failed;
3807 }
3808 len = PyList_GET_SIZE(tmp);
3809 decorator_list = asdl_seq_new(len, arena);
3810 if (decorator_list == NULL) goto failed;
3811 for (i = 0; i < len; i++) {
3812 expr_ty value;
3813 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3814 if (res != 0) goto failed;
3815 asdl_seq_SET(decorator_list, i, value);
3816 }
3817 Py_XDECREF(tmp);
3818 tmp = NULL;
3819 } else {
3820 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3821 return 1;
3822 }
3823 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3824 decorator_list, lineno, col_offset, arena);
3825 if (*out == NULL) goto failed;
3826 return 0;
3827 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003828 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3829 if (isinstance == -1) {
3830 return 1;
3831 }
3832 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003833 expr_ty value;
3834
3835 if (PyObject_HasAttrString(obj, "value")) {
3836 int res;
3837 tmp = PyObject_GetAttrString(obj, "value");
3838 if (tmp == NULL) goto failed;
3839 res = obj2ast_expr(tmp, &value, arena);
3840 if (res != 0) goto failed;
3841 Py_XDECREF(tmp);
3842 tmp = NULL;
3843 } else {
3844 value = NULL;
3845 }
3846 *out = Return(value, lineno, col_offset, arena);
3847 if (*out == NULL) goto failed;
3848 return 0;
3849 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003850 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3851 if (isinstance == -1) {
3852 return 1;
3853 }
3854 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003855 asdl_seq* targets;
3856
3857 if (PyObject_HasAttrString(obj, "targets")) {
3858 int res;
3859 Py_ssize_t len;
3860 Py_ssize_t i;
3861 tmp = PyObject_GetAttrString(obj, "targets");
3862 if (tmp == NULL) goto failed;
3863 if (!PyList_Check(tmp)) {
3864 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3865 goto failed;
3866 }
3867 len = PyList_GET_SIZE(tmp);
3868 targets = asdl_seq_new(len, arena);
3869 if (targets == NULL) goto failed;
3870 for (i = 0; i < len; i++) {
3871 expr_ty value;
3872 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3873 if (res != 0) goto failed;
3874 asdl_seq_SET(targets, i, value);
3875 }
3876 Py_XDECREF(tmp);
3877 tmp = NULL;
3878 } else {
3879 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3880 return 1;
3881 }
3882 *out = Delete(targets, lineno, col_offset, arena);
3883 if (*out == NULL) goto failed;
3884 return 0;
3885 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003886 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3887 if (isinstance == -1) {
3888 return 1;
3889 }
3890 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003891 asdl_seq* targets;
3892 expr_ty value;
3893
3894 if (PyObject_HasAttrString(obj, "targets")) {
3895 int res;
3896 Py_ssize_t len;
3897 Py_ssize_t i;
3898 tmp = PyObject_GetAttrString(obj, "targets");
3899 if (tmp == NULL) goto failed;
3900 if (!PyList_Check(tmp)) {
3901 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3902 goto failed;
3903 }
3904 len = PyList_GET_SIZE(tmp);
3905 targets = asdl_seq_new(len, arena);
3906 if (targets == NULL) goto failed;
3907 for (i = 0; i < len; i++) {
3908 expr_ty value;
3909 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3910 if (res != 0) goto failed;
3911 asdl_seq_SET(targets, i, value);
3912 }
3913 Py_XDECREF(tmp);
3914 tmp = NULL;
3915 } else {
3916 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3917 return 1;
3918 }
3919 if (PyObject_HasAttrString(obj, "value")) {
3920 int res;
3921 tmp = PyObject_GetAttrString(obj, "value");
3922 if (tmp == NULL) goto failed;
3923 res = obj2ast_expr(tmp, &value, arena);
3924 if (res != 0) goto failed;
3925 Py_XDECREF(tmp);
3926 tmp = NULL;
3927 } else {
3928 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3929 return 1;
3930 }
3931 *out = Assign(targets, value, lineno, col_offset, arena);
3932 if (*out == NULL) goto failed;
3933 return 0;
3934 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003935 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3936 if (isinstance == -1) {
3937 return 1;
3938 }
3939 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003940 expr_ty target;
3941 operator_ty op;
3942 expr_ty value;
3943
3944 if (PyObject_HasAttrString(obj, "target")) {
3945 int res;
3946 tmp = PyObject_GetAttrString(obj, "target");
3947 if (tmp == NULL) goto failed;
3948 res = obj2ast_expr(tmp, &target, arena);
3949 if (res != 0) goto failed;
3950 Py_XDECREF(tmp);
3951 tmp = NULL;
3952 } else {
3953 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3954 return 1;
3955 }
3956 if (PyObject_HasAttrString(obj, "op")) {
3957 int res;
3958 tmp = PyObject_GetAttrString(obj, "op");
3959 if (tmp == NULL) goto failed;
3960 res = obj2ast_operator(tmp, &op, arena);
3961 if (res != 0) goto failed;
3962 Py_XDECREF(tmp);
3963 tmp = NULL;
3964 } else {
3965 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3966 return 1;
3967 }
3968 if (PyObject_HasAttrString(obj, "value")) {
3969 int res;
3970 tmp = PyObject_GetAttrString(obj, "value");
3971 if (tmp == NULL) goto failed;
3972 res = obj2ast_expr(tmp, &value, arena);
3973 if (res != 0) goto failed;
3974 Py_XDECREF(tmp);
3975 tmp = NULL;
3976 } else {
3977 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3978 return 1;
3979 }
3980 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3981 if (*out == NULL) goto failed;
3982 return 0;
3983 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00003984 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3985 if (isinstance == -1) {
3986 return 1;
3987 }
3988 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003989 expr_ty target;
3990 expr_ty iter;
3991 asdl_seq* body;
3992 asdl_seq* orelse;
3993
3994 if (PyObject_HasAttrString(obj, "target")) {
3995 int res;
3996 tmp = PyObject_GetAttrString(obj, "target");
3997 if (tmp == NULL) goto failed;
3998 res = obj2ast_expr(tmp, &target, arena);
3999 if (res != 0) goto failed;
4000 Py_XDECREF(tmp);
4001 tmp = NULL;
4002 } else {
4003 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4004 return 1;
4005 }
4006 if (PyObject_HasAttrString(obj, "iter")) {
4007 int res;
4008 tmp = PyObject_GetAttrString(obj, "iter");
4009 if (tmp == NULL) goto failed;
4010 res = obj2ast_expr(tmp, &iter, arena);
4011 if (res != 0) goto failed;
4012 Py_XDECREF(tmp);
4013 tmp = NULL;
4014 } else {
4015 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4016 return 1;
4017 }
4018 if (PyObject_HasAttrString(obj, "body")) {
4019 int res;
4020 Py_ssize_t len;
4021 Py_ssize_t i;
4022 tmp = PyObject_GetAttrString(obj, "body");
4023 if (tmp == NULL) goto failed;
4024 if (!PyList_Check(tmp)) {
4025 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4026 goto failed;
4027 }
4028 len = PyList_GET_SIZE(tmp);
4029 body = asdl_seq_new(len, arena);
4030 if (body == NULL) goto failed;
4031 for (i = 0; i < len; i++) {
4032 stmt_ty value;
4033 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4034 if (res != 0) goto failed;
4035 asdl_seq_SET(body, i, value);
4036 }
4037 Py_XDECREF(tmp);
4038 tmp = NULL;
4039 } else {
4040 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4041 return 1;
4042 }
4043 if (PyObject_HasAttrString(obj, "orelse")) {
4044 int res;
4045 Py_ssize_t len;
4046 Py_ssize_t i;
4047 tmp = PyObject_GetAttrString(obj, "orelse");
4048 if (tmp == NULL) goto failed;
4049 if (!PyList_Check(tmp)) {
4050 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4051 goto failed;
4052 }
4053 len = PyList_GET_SIZE(tmp);
4054 orelse = asdl_seq_new(len, arena);
4055 if (orelse == NULL) goto failed;
4056 for (i = 0; i < len; i++) {
4057 stmt_ty value;
4058 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4059 if (res != 0) goto failed;
4060 asdl_seq_SET(orelse, i, value);
4061 }
4062 Py_XDECREF(tmp);
4063 tmp = NULL;
4064 } else {
4065 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4066 return 1;
4067 }
4068 *out = For(target, iter, body, orelse, lineno, col_offset,
4069 arena);
4070 if (*out == NULL) goto failed;
4071 return 0;
4072 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004073 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4074 if (isinstance == -1) {
4075 return 1;
4076 }
4077 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004078 expr_ty test;
4079 asdl_seq* body;
4080 asdl_seq* orelse;
4081
4082 if (PyObject_HasAttrString(obj, "test")) {
4083 int res;
4084 tmp = PyObject_GetAttrString(obj, "test");
4085 if (tmp == NULL) goto failed;
4086 res = obj2ast_expr(tmp, &test, arena);
4087 if (res != 0) goto failed;
4088 Py_XDECREF(tmp);
4089 tmp = NULL;
4090 } else {
4091 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4092 return 1;
4093 }
4094 if (PyObject_HasAttrString(obj, "body")) {
4095 int res;
4096 Py_ssize_t len;
4097 Py_ssize_t i;
4098 tmp = PyObject_GetAttrString(obj, "body");
4099 if (tmp == NULL) goto failed;
4100 if (!PyList_Check(tmp)) {
4101 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4102 goto failed;
4103 }
4104 len = PyList_GET_SIZE(tmp);
4105 body = asdl_seq_new(len, arena);
4106 if (body == NULL) goto failed;
4107 for (i = 0; i < len; i++) {
4108 stmt_ty value;
4109 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4110 if (res != 0) goto failed;
4111 asdl_seq_SET(body, i, value);
4112 }
4113 Py_XDECREF(tmp);
4114 tmp = NULL;
4115 } else {
4116 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4117 return 1;
4118 }
4119 if (PyObject_HasAttrString(obj, "orelse")) {
4120 int res;
4121 Py_ssize_t len;
4122 Py_ssize_t i;
4123 tmp = PyObject_GetAttrString(obj, "orelse");
4124 if (tmp == NULL) goto failed;
4125 if (!PyList_Check(tmp)) {
4126 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4127 goto failed;
4128 }
4129 len = PyList_GET_SIZE(tmp);
4130 orelse = asdl_seq_new(len, arena);
4131 if (orelse == NULL) goto failed;
4132 for (i = 0; i < len; i++) {
4133 stmt_ty value;
4134 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4135 if (res != 0) goto failed;
4136 asdl_seq_SET(orelse, i, value);
4137 }
4138 Py_XDECREF(tmp);
4139 tmp = NULL;
4140 } else {
4141 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4142 return 1;
4143 }
4144 *out = While(test, body, orelse, lineno, col_offset, arena);
4145 if (*out == NULL) goto failed;
4146 return 0;
4147 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004148 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4149 if (isinstance == -1) {
4150 return 1;
4151 }
4152 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004153 expr_ty test;
4154 asdl_seq* body;
4155 asdl_seq* orelse;
4156
4157 if (PyObject_HasAttrString(obj, "test")) {
4158 int res;
4159 tmp = PyObject_GetAttrString(obj, "test");
4160 if (tmp == NULL) goto failed;
4161 res = obj2ast_expr(tmp, &test, arena);
4162 if (res != 0) goto failed;
4163 Py_XDECREF(tmp);
4164 tmp = NULL;
4165 } else {
4166 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4167 return 1;
4168 }
4169 if (PyObject_HasAttrString(obj, "body")) {
4170 int res;
4171 Py_ssize_t len;
4172 Py_ssize_t i;
4173 tmp = PyObject_GetAttrString(obj, "body");
4174 if (tmp == NULL) goto failed;
4175 if (!PyList_Check(tmp)) {
4176 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4177 goto failed;
4178 }
4179 len = PyList_GET_SIZE(tmp);
4180 body = asdl_seq_new(len, arena);
4181 if (body == NULL) goto failed;
4182 for (i = 0; i < len; i++) {
4183 stmt_ty value;
4184 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4185 if (res != 0) goto failed;
4186 asdl_seq_SET(body, i, value);
4187 }
4188 Py_XDECREF(tmp);
4189 tmp = NULL;
4190 } else {
4191 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4192 return 1;
4193 }
4194 if (PyObject_HasAttrString(obj, "orelse")) {
4195 int res;
4196 Py_ssize_t len;
4197 Py_ssize_t i;
4198 tmp = PyObject_GetAttrString(obj, "orelse");
4199 if (tmp == NULL) goto failed;
4200 if (!PyList_Check(tmp)) {
4201 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4202 goto failed;
4203 }
4204 len = PyList_GET_SIZE(tmp);
4205 orelse = asdl_seq_new(len, arena);
4206 if (orelse == NULL) goto failed;
4207 for (i = 0; i < len; i++) {
4208 stmt_ty value;
4209 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4210 if (res != 0) goto failed;
4211 asdl_seq_SET(orelse, i, value);
4212 }
4213 Py_XDECREF(tmp);
4214 tmp = NULL;
4215 } else {
4216 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4217 return 1;
4218 }
4219 *out = If(test, body, orelse, lineno, col_offset, arena);
4220 if (*out == NULL) goto failed;
4221 return 0;
4222 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004223 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4224 if (isinstance == -1) {
4225 return 1;
4226 }
4227 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004228 expr_ty context_expr;
4229 expr_ty optional_vars;
4230 asdl_seq* body;
4231
4232 if (PyObject_HasAttrString(obj, "context_expr")) {
4233 int res;
4234 tmp = PyObject_GetAttrString(obj, "context_expr");
4235 if (tmp == NULL) goto failed;
4236 res = obj2ast_expr(tmp, &context_expr, arena);
4237 if (res != 0) goto failed;
4238 Py_XDECREF(tmp);
4239 tmp = NULL;
4240 } else {
4241 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4242 return 1;
4243 }
4244 if (PyObject_HasAttrString(obj, "optional_vars")) {
4245 int res;
4246 tmp = PyObject_GetAttrString(obj, "optional_vars");
4247 if (tmp == NULL) goto failed;
4248 res = obj2ast_expr(tmp, &optional_vars, arena);
4249 if (res != 0) goto failed;
4250 Py_XDECREF(tmp);
4251 tmp = NULL;
4252 } else {
4253 optional_vars = NULL;
4254 }
4255 if (PyObject_HasAttrString(obj, "body")) {
4256 int res;
4257 Py_ssize_t len;
4258 Py_ssize_t i;
4259 tmp = PyObject_GetAttrString(obj, "body");
4260 if (tmp == NULL) goto failed;
4261 if (!PyList_Check(tmp)) {
4262 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4263 goto failed;
4264 }
4265 len = PyList_GET_SIZE(tmp);
4266 body = asdl_seq_new(len, arena);
4267 if (body == NULL) goto failed;
4268 for (i = 0; i < len; i++) {
4269 stmt_ty value;
4270 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4271 if (res != 0) goto failed;
4272 asdl_seq_SET(body, i, value);
4273 }
4274 Py_XDECREF(tmp);
4275 tmp = NULL;
4276 } else {
4277 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4278 return 1;
4279 }
4280 *out = With(context_expr, optional_vars, body, lineno,
4281 col_offset, arena);
4282 if (*out == NULL) goto failed;
4283 return 0;
4284 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004285 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4286 if (isinstance == -1) {
4287 return 1;
4288 }
4289 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004290 expr_ty exc;
4291 expr_ty cause;
4292
4293 if (PyObject_HasAttrString(obj, "exc")) {
4294 int res;
4295 tmp = PyObject_GetAttrString(obj, "exc");
4296 if (tmp == NULL) goto failed;
4297 res = obj2ast_expr(tmp, &exc, arena);
4298 if (res != 0) goto failed;
4299 Py_XDECREF(tmp);
4300 tmp = NULL;
4301 } else {
4302 exc = NULL;
4303 }
4304 if (PyObject_HasAttrString(obj, "cause")) {
4305 int res;
4306 tmp = PyObject_GetAttrString(obj, "cause");
4307 if (tmp == NULL) goto failed;
4308 res = obj2ast_expr(tmp, &cause, arena);
4309 if (res != 0) goto failed;
4310 Py_XDECREF(tmp);
4311 tmp = NULL;
4312 } else {
4313 cause = NULL;
4314 }
4315 *out = Raise(exc, cause, lineno, col_offset, arena);
4316 if (*out == NULL) goto failed;
4317 return 0;
4318 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004319 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4320 if (isinstance == -1) {
4321 return 1;
4322 }
4323 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004324 asdl_seq* body;
4325 asdl_seq* handlers;
4326 asdl_seq* orelse;
4327
4328 if (PyObject_HasAttrString(obj, "body")) {
4329 int res;
4330 Py_ssize_t len;
4331 Py_ssize_t i;
4332 tmp = PyObject_GetAttrString(obj, "body");
4333 if (tmp == NULL) goto failed;
4334 if (!PyList_Check(tmp)) {
4335 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4336 goto failed;
4337 }
4338 len = PyList_GET_SIZE(tmp);
4339 body = asdl_seq_new(len, arena);
4340 if (body == NULL) goto failed;
4341 for (i = 0; i < len; i++) {
4342 stmt_ty value;
4343 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4344 if (res != 0) goto failed;
4345 asdl_seq_SET(body, i, value);
4346 }
4347 Py_XDECREF(tmp);
4348 tmp = NULL;
4349 } else {
4350 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4351 return 1;
4352 }
4353 if (PyObject_HasAttrString(obj, "handlers")) {
4354 int res;
4355 Py_ssize_t len;
4356 Py_ssize_t i;
4357 tmp = PyObject_GetAttrString(obj, "handlers");
4358 if (tmp == NULL) goto failed;
4359 if (!PyList_Check(tmp)) {
4360 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4361 goto failed;
4362 }
4363 len = PyList_GET_SIZE(tmp);
4364 handlers = asdl_seq_new(len, arena);
4365 if (handlers == NULL) goto failed;
4366 for (i = 0; i < len; i++) {
4367 excepthandler_ty value;
4368 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4369 if (res != 0) goto failed;
4370 asdl_seq_SET(handlers, i, value);
4371 }
4372 Py_XDECREF(tmp);
4373 tmp = NULL;
4374 } else {
4375 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4376 return 1;
4377 }
4378 if (PyObject_HasAttrString(obj, "orelse")) {
4379 int res;
4380 Py_ssize_t len;
4381 Py_ssize_t i;
4382 tmp = PyObject_GetAttrString(obj, "orelse");
4383 if (tmp == NULL) goto failed;
4384 if (!PyList_Check(tmp)) {
4385 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4386 goto failed;
4387 }
4388 len = PyList_GET_SIZE(tmp);
4389 orelse = asdl_seq_new(len, arena);
4390 if (orelse == NULL) goto failed;
4391 for (i = 0; i < len; i++) {
4392 stmt_ty value;
4393 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4394 if (res != 0) goto failed;
4395 asdl_seq_SET(orelse, i, value);
4396 }
4397 Py_XDECREF(tmp);
4398 tmp = NULL;
4399 } else {
4400 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4401 return 1;
4402 }
4403 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4404 arena);
4405 if (*out == NULL) goto failed;
4406 return 0;
4407 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004408 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4409 if (isinstance == -1) {
4410 return 1;
4411 }
4412 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004413 asdl_seq* body;
4414 asdl_seq* finalbody;
4415
4416 if (PyObject_HasAttrString(obj, "body")) {
4417 int res;
4418 Py_ssize_t len;
4419 Py_ssize_t i;
4420 tmp = PyObject_GetAttrString(obj, "body");
4421 if (tmp == NULL) goto failed;
4422 if (!PyList_Check(tmp)) {
4423 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4424 goto failed;
4425 }
4426 len = PyList_GET_SIZE(tmp);
4427 body = asdl_seq_new(len, arena);
4428 if (body == NULL) goto failed;
4429 for (i = 0; i < len; i++) {
4430 stmt_ty value;
4431 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4432 if (res != 0) goto failed;
4433 asdl_seq_SET(body, i, value);
4434 }
4435 Py_XDECREF(tmp);
4436 tmp = NULL;
4437 } else {
4438 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4439 return 1;
4440 }
4441 if (PyObject_HasAttrString(obj, "finalbody")) {
4442 int res;
4443 Py_ssize_t len;
4444 Py_ssize_t i;
4445 tmp = PyObject_GetAttrString(obj, "finalbody");
4446 if (tmp == NULL) goto failed;
4447 if (!PyList_Check(tmp)) {
4448 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4449 goto failed;
4450 }
4451 len = PyList_GET_SIZE(tmp);
4452 finalbody = asdl_seq_new(len, arena);
4453 if (finalbody == NULL) goto failed;
4454 for (i = 0; i < len; i++) {
4455 stmt_ty value;
4456 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4457 if (res != 0) goto failed;
4458 asdl_seq_SET(finalbody, i, value);
4459 }
4460 Py_XDECREF(tmp);
4461 tmp = NULL;
4462 } else {
4463 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4464 return 1;
4465 }
4466 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4467 if (*out == NULL) goto failed;
4468 return 0;
4469 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004470 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4471 if (isinstance == -1) {
4472 return 1;
4473 }
4474 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004475 expr_ty test;
4476 expr_ty msg;
4477
4478 if (PyObject_HasAttrString(obj, "test")) {
4479 int res;
4480 tmp = PyObject_GetAttrString(obj, "test");
4481 if (tmp == NULL) goto failed;
4482 res = obj2ast_expr(tmp, &test, arena);
4483 if (res != 0) goto failed;
4484 Py_XDECREF(tmp);
4485 tmp = NULL;
4486 } else {
4487 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4488 return 1;
4489 }
4490 if (PyObject_HasAttrString(obj, "msg")) {
4491 int res;
4492 tmp = PyObject_GetAttrString(obj, "msg");
4493 if (tmp == NULL) goto failed;
4494 res = obj2ast_expr(tmp, &msg, arena);
4495 if (res != 0) goto failed;
4496 Py_XDECREF(tmp);
4497 tmp = NULL;
4498 } else {
4499 msg = NULL;
4500 }
4501 *out = Assert(test, msg, lineno, col_offset, arena);
4502 if (*out == NULL) goto failed;
4503 return 0;
4504 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004505 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4506 if (isinstance == -1) {
4507 return 1;
4508 }
4509 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004510 asdl_seq* names;
4511
4512 if (PyObject_HasAttrString(obj, "names")) {
4513 int res;
4514 Py_ssize_t len;
4515 Py_ssize_t i;
4516 tmp = PyObject_GetAttrString(obj, "names");
4517 if (tmp == NULL) goto failed;
4518 if (!PyList_Check(tmp)) {
4519 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4520 goto failed;
4521 }
4522 len = PyList_GET_SIZE(tmp);
4523 names = asdl_seq_new(len, arena);
4524 if (names == NULL) goto failed;
4525 for (i = 0; i < len; i++) {
4526 alias_ty value;
4527 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4528 if (res != 0) goto failed;
4529 asdl_seq_SET(names, i, value);
4530 }
4531 Py_XDECREF(tmp);
4532 tmp = NULL;
4533 } else {
4534 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4535 return 1;
4536 }
4537 *out = Import(names, lineno, col_offset, arena);
4538 if (*out == NULL) goto failed;
4539 return 0;
4540 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004541 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4542 if (isinstance == -1) {
4543 return 1;
4544 }
4545 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004546 identifier module;
4547 asdl_seq* names;
4548 int level;
4549
4550 if (PyObject_HasAttrString(obj, "module")) {
4551 int res;
4552 tmp = PyObject_GetAttrString(obj, "module");
4553 if (tmp == NULL) goto failed;
4554 res = obj2ast_identifier(tmp, &module, arena);
4555 if (res != 0) goto failed;
4556 Py_XDECREF(tmp);
4557 tmp = NULL;
4558 } else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00004559 module = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004560 }
4561 if (PyObject_HasAttrString(obj, "names")) {
4562 int res;
4563 Py_ssize_t len;
4564 Py_ssize_t i;
4565 tmp = PyObject_GetAttrString(obj, "names");
4566 if (tmp == NULL) goto failed;
4567 if (!PyList_Check(tmp)) {
4568 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4569 goto failed;
4570 }
4571 len = PyList_GET_SIZE(tmp);
4572 names = asdl_seq_new(len, arena);
4573 if (names == NULL) goto failed;
4574 for (i = 0; i < len; i++) {
4575 alias_ty value;
4576 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4577 if (res != 0) goto failed;
4578 asdl_seq_SET(names, i, value);
4579 }
4580 Py_XDECREF(tmp);
4581 tmp = NULL;
4582 } else {
4583 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4584 return 1;
4585 }
4586 if (PyObject_HasAttrString(obj, "level")) {
4587 int res;
4588 tmp = PyObject_GetAttrString(obj, "level");
4589 if (tmp == NULL) goto failed;
4590 res = obj2ast_int(tmp, &level, arena);
4591 if (res != 0) goto failed;
4592 Py_XDECREF(tmp);
4593 tmp = NULL;
4594 } else {
4595 level = 0;
4596 }
4597 *out = ImportFrom(module, names, level, lineno, col_offset,
4598 arena);
4599 if (*out == NULL) goto failed;
4600 return 0;
4601 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004602 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4603 if (isinstance == -1) {
4604 return 1;
4605 }
4606 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004607 asdl_seq* names;
4608
4609 if (PyObject_HasAttrString(obj, "names")) {
4610 int res;
4611 Py_ssize_t len;
4612 Py_ssize_t i;
4613 tmp = PyObject_GetAttrString(obj, "names");
4614 if (tmp == NULL) goto failed;
4615 if (!PyList_Check(tmp)) {
4616 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4617 goto failed;
4618 }
4619 len = PyList_GET_SIZE(tmp);
4620 names = asdl_seq_new(len, arena);
4621 if (names == NULL) goto failed;
4622 for (i = 0; i < len; i++) {
4623 identifier value;
4624 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4625 if (res != 0) goto failed;
4626 asdl_seq_SET(names, i, value);
4627 }
4628 Py_XDECREF(tmp);
4629 tmp = NULL;
4630 } else {
4631 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4632 return 1;
4633 }
4634 *out = Global(names, lineno, col_offset, arena);
4635 if (*out == NULL) goto failed;
4636 return 0;
4637 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004638 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4639 if (isinstance == -1) {
4640 return 1;
4641 }
4642 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004643 asdl_seq* names;
4644
4645 if (PyObject_HasAttrString(obj, "names")) {
4646 int res;
4647 Py_ssize_t len;
4648 Py_ssize_t i;
4649 tmp = PyObject_GetAttrString(obj, "names");
4650 if (tmp == NULL) goto failed;
4651 if (!PyList_Check(tmp)) {
4652 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4653 goto failed;
4654 }
4655 len = PyList_GET_SIZE(tmp);
4656 names = asdl_seq_new(len, arena);
4657 if (names == NULL) goto failed;
4658 for (i = 0; i < len; i++) {
4659 identifier value;
4660 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4661 if (res != 0) goto failed;
4662 asdl_seq_SET(names, i, value);
4663 }
4664 Py_XDECREF(tmp);
4665 tmp = NULL;
4666 } else {
4667 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4668 return 1;
4669 }
4670 *out = Nonlocal(names, lineno, col_offset, arena);
4671 if (*out == NULL) goto failed;
4672 return 0;
4673 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004674 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4675 if (isinstance == -1) {
4676 return 1;
4677 }
4678 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004679 expr_ty value;
4680
4681 if (PyObject_HasAttrString(obj, "value")) {
4682 int res;
4683 tmp = PyObject_GetAttrString(obj, "value");
4684 if (tmp == NULL) goto failed;
4685 res = obj2ast_expr(tmp, &value, arena);
4686 if (res != 0) goto failed;
4687 Py_XDECREF(tmp);
4688 tmp = NULL;
4689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4691 return 1;
4692 }
4693 *out = Expr(value, lineno, col_offset, arena);
4694 if (*out == NULL) goto failed;
4695 return 0;
4696 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004697 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4698 if (isinstance == -1) {
4699 return 1;
4700 }
4701 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004702
4703 *out = Pass(lineno, col_offset, arena);
4704 if (*out == NULL) goto failed;
4705 return 0;
4706 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004707 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4708 if (isinstance == -1) {
4709 return 1;
4710 }
4711 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004712
4713 *out = Break(lineno, col_offset, arena);
4714 if (*out == NULL) goto failed;
4715 return 0;
4716 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004717 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4718 if (isinstance == -1) {
4719 return 1;
4720 }
4721 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004722
4723 *out = Continue(lineno, col_offset, arena);
4724 if (*out == NULL) goto failed;
4725 return 0;
4726 }
4727
Benjamin Peterson5b066812010-11-20 01:38:49 +00004728 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4729 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004730 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004731 return 1;
4732}
4733
4734int
4735obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4736{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004737 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004738
Benjamin Petersond8f65972010-11-20 04:31:07 +00004739 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004740 int lineno;
4741 int col_offset;
4742
4743 if (obj == Py_None) {
4744 *out = NULL;
4745 return 0;
4746 }
4747 if (PyObject_HasAttrString(obj, "lineno")) {
4748 int res;
4749 tmp = PyObject_GetAttrString(obj, "lineno");
4750 if (tmp == NULL) goto failed;
4751 res = obj2ast_int(tmp, &lineno, arena);
4752 if (res != 0) goto failed;
4753 Py_XDECREF(tmp);
4754 tmp = NULL;
4755 } else {
4756 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4757 return 1;
4758 }
4759 if (PyObject_HasAttrString(obj, "col_offset")) {
4760 int res;
4761 tmp = PyObject_GetAttrString(obj, "col_offset");
4762 if (tmp == NULL) goto failed;
4763 res = obj2ast_int(tmp, &col_offset, arena);
4764 if (res != 0) goto failed;
4765 Py_XDECREF(tmp);
4766 tmp = NULL;
4767 } else {
4768 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4769 return 1;
4770 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004771 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4772 if (isinstance == -1) {
4773 return 1;
4774 }
4775 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004776 boolop_ty op;
4777 asdl_seq* values;
4778
4779 if (PyObject_HasAttrString(obj, "op")) {
4780 int res;
4781 tmp = PyObject_GetAttrString(obj, "op");
4782 if (tmp == NULL) goto failed;
4783 res = obj2ast_boolop(tmp, &op, arena);
4784 if (res != 0) goto failed;
4785 Py_XDECREF(tmp);
4786 tmp = NULL;
4787 } else {
4788 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4789 return 1;
4790 }
4791 if (PyObject_HasAttrString(obj, "values")) {
4792 int res;
4793 Py_ssize_t len;
4794 Py_ssize_t i;
4795 tmp = PyObject_GetAttrString(obj, "values");
4796 if (tmp == NULL) goto failed;
4797 if (!PyList_Check(tmp)) {
4798 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4799 goto failed;
4800 }
4801 len = PyList_GET_SIZE(tmp);
4802 values = asdl_seq_new(len, arena);
4803 if (values == NULL) goto failed;
4804 for (i = 0; i < len; i++) {
4805 expr_ty value;
4806 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4807 if (res != 0) goto failed;
4808 asdl_seq_SET(values, i, value);
4809 }
4810 Py_XDECREF(tmp);
4811 tmp = NULL;
4812 } else {
4813 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4814 return 1;
4815 }
4816 *out = BoolOp(op, values, lineno, col_offset, arena);
4817 if (*out == NULL) goto failed;
4818 return 0;
4819 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004820 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4821 if (isinstance == -1) {
4822 return 1;
4823 }
4824 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004825 expr_ty left;
4826 operator_ty op;
4827 expr_ty right;
4828
4829 if (PyObject_HasAttrString(obj, "left")) {
4830 int res;
4831 tmp = PyObject_GetAttrString(obj, "left");
4832 if (tmp == NULL) goto failed;
4833 res = obj2ast_expr(tmp, &left, arena);
4834 if (res != 0) goto failed;
4835 Py_XDECREF(tmp);
4836 tmp = NULL;
4837 } else {
4838 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4839 return 1;
4840 }
4841 if (PyObject_HasAttrString(obj, "op")) {
4842 int res;
4843 tmp = PyObject_GetAttrString(obj, "op");
4844 if (tmp == NULL) goto failed;
4845 res = obj2ast_operator(tmp, &op, arena);
4846 if (res != 0) goto failed;
4847 Py_XDECREF(tmp);
4848 tmp = NULL;
4849 } else {
4850 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4851 return 1;
4852 }
4853 if (PyObject_HasAttrString(obj, "right")) {
4854 int res;
4855 tmp = PyObject_GetAttrString(obj, "right");
4856 if (tmp == NULL) goto failed;
4857 res = obj2ast_expr(tmp, &right, arena);
4858 if (res != 0) goto failed;
4859 Py_XDECREF(tmp);
4860 tmp = NULL;
4861 } else {
4862 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4863 return 1;
4864 }
4865 *out = BinOp(left, op, right, lineno, col_offset, arena);
4866 if (*out == NULL) goto failed;
4867 return 0;
4868 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004869 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4870 if (isinstance == -1) {
4871 return 1;
4872 }
4873 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004874 unaryop_ty op;
4875 expr_ty operand;
4876
4877 if (PyObject_HasAttrString(obj, "op")) {
4878 int res;
4879 tmp = PyObject_GetAttrString(obj, "op");
4880 if (tmp == NULL) goto failed;
4881 res = obj2ast_unaryop(tmp, &op, arena);
4882 if (res != 0) goto failed;
4883 Py_XDECREF(tmp);
4884 tmp = NULL;
4885 } else {
4886 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4887 return 1;
4888 }
4889 if (PyObject_HasAttrString(obj, "operand")) {
4890 int res;
4891 tmp = PyObject_GetAttrString(obj, "operand");
4892 if (tmp == NULL) goto failed;
4893 res = obj2ast_expr(tmp, &operand, arena);
4894 if (res != 0) goto failed;
4895 Py_XDECREF(tmp);
4896 tmp = NULL;
4897 } else {
4898 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4899 return 1;
4900 }
4901 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4902 if (*out == NULL) goto failed;
4903 return 0;
4904 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004905 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4906 if (isinstance == -1) {
4907 return 1;
4908 }
4909 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004910 arguments_ty args;
4911 expr_ty body;
4912
4913 if (PyObject_HasAttrString(obj, "args")) {
4914 int res;
4915 tmp = PyObject_GetAttrString(obj, "args");
4916 if (tmp == NULL) goto failed;
4917 res = obj2ast_arguments(tmp, &args, arena);
4918 if (res != 0) goto failed;
4919 Py_XDECREF(tmp);
4920 tmp = NULL;
4921 } else {
4922 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4923 return 1;
4924 }
4925 if (PyObject_HasAttrString(obj, "body")) {
4926 int res;
4927 tmp = PyObject_GetAttrString(obj, "body");
4928 if (tmp == NULL) goto failed;
4929 res = obj2ast_expr(tmp, &body, arena);
4930 if (res != 0) goto failed;
4931 Py_XDECREF(tmp);
4932 tmp = NULL;
4933 } else {
4934 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4935 return 1;
4936 }
4937 *out = Lambda(args, body, lineno, col_offset, arena);
4938 if (*out == NULL) goto failed;
4939 return 0;
4940 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004941 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4942 if (isinstance == -1) {
4943 return 1;
4944 }
4945 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004946 expr_ty test;
4947 expr_ty body;
4948 expr_ty orelse;
4949
4950 if (PyObject_HasAttrString(obj, "test")) {
4951 int res;
4952 tmp = PyObject_GetAttrString(obj, "test");
4953 if (tmp == NULL) goto failed;
4954 res = obj2ast_expr(tmp, &test, arena);
4955 if (res != 0) goto failed;
4956 Py_XDECREF(tmp);
4957 tmp = NULL;
4958 } else {
4959 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4960 return 1;
4961 }
4962 if (PyObject_HasAttrString(obj, "body")) {
4963 int res;
4964 tmp = PyObject_GetAttrString(obj, "body");
4965 if (tmp == NULL) goto failed;
4966 res = obj2ast_expr(tmp, &body, arena);
4967 if (res != 0) goto failed;
4968 Py_XDECREF(tmp);
4969 tmp = NULL;
4970 } else {
4971 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4972 return 1;
4973 }
4974 if (PyObject_HasAttrString(obj, "orelse")) {
4975 int res;
4976 tmp = PyObject_GetAttrString(obj, "orelse");
4977 if (tmp == NULL) goto failed;
4978 res = obj2ast_expr(tmp, &orelse, arena);
4979 if (res != 0) goto failed;
4980 Py_XDECREF(tmp);
4981 tmp = NULL;
4982 } else {
4983 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4984 return 1;
4985 }
4986 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4987 if (*out == NULL) goto failed;
4988 return 0;
4989 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00004990 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4991 if (isinstance == -1) {
4992 return 1;
4993 }
4994 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004995 asdl_seq* keys;
4996 asdl_seq* values;
4997
4998 if (PyObject_HasAttrString(obj, "keys")) {
4999 int res;
5000 Py_ssize_t len;
5001 Py_ssize_t i;
5002 tmp = PyObject_GetAttrString(obj, "keys");
5003 if (tmp == NULL) goto failed;
5004 if (!PyList_Check(tmp)) {
5005 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5006 goto failed;
5007 }
5008 len = PyList_GET_SIZE(tmp);
5009 keys = asdl_seq_new(len, arena);
5010 if (keys == NULL) goto failed;
5011 for (i = 0; i < len; i++) {
5012 expr_ty value;
5013 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5014 if (res != 0) goto failed;
5015 asdl_seq_SET(keys, i, value);
5016 }
5017 Py_XDECREF(tmp);
5018 tmp = NULL;
5019 } else {
5020 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5021 return 1;
5022 }
5023 if (PyObject_HasAttrString(obj, "values")) {
5024 int res;
5025 Py_ssize_t len;
5026 Py_ssize_t i;
5027 tmp = PyObject_GetAttrString(obj, "values");
5028 if (tmp == NULL) goto failed;
5029 if (!PyList_Check(tmp)) {
5030 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5031 goto failed;
5032 }
5033 len = PyList_GET_SIZE(tmp);
5034 values = asdl_seq_new(len, arena);
5035 if (values == NULL) goto failed;
5036 for (i = 0; i < len; i++) {
5037 expr_ty value;
5038 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5039 if (res != 0) goto failed;
5040 asdl_seq_SET(values, i, value);
5041 }
5042 Py_XDECREF(tmp);
5043 tmp = NULL;
5044 } else {
5045 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5046 return 1;
5047 }
5048 *out = Dict(keys, values, lineno, col_offset, arena);
5049 if (*out == NULL) goto failed;
5050 return 0;
5051 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005052 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5053 if (isinstance == -1) {
5054 return 1;
5055 }
5056 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005057 asdl_seq* elts;
5058
5059 if (PyObject_HasAttrString(obj, "elts")) {
5060 int res;
5061 Py_ssize_t len;
5062 Py_ssize_t i;
5063 tmp = PyObject_GetAttrString(obj, "elts");
5064 if (tmp == NULL) goto failed;
5065 if (!PyList_Check(tmp)) {
5066 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5067 goto failed;
5068 }
5069 len = PyList_GET_SIZE(tmp);
5070 elts = asdl_seq_new(len, arena);
5071 if (elts == NULL) goto failed;
5072 for (i = 0; i < len; i++) {
5073 expr_ty value;
5074 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5075 if (res != 0) goto failed;
5076 asdl_seq_SET(elts, i, value);
5077 }
5078 Py_XDECREF(tmp);
5079 tmp = NULL;
5080 } else {
5081 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5082 return 1;
5083 }
5084 *out = Set(elts, lineno, col_offset, arena);
5085 if (*out == NULL) goto failed;
5086 return 0;
5087 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005088 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5089 if (isinstance == -1) {
5090 return 1;
5091 }
5092 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005093 expr_ty elt;
5094 asdl_seq* generators;
5095
5096 if (PyObject_HasAttrString(obj, "elt")) {
5097 int res;
5098 tmp = PyObject_GetAttrString(obj, "elt");
5099 if (tmp == NULL) goto failed;
5100 res = obj2ast_expr(tmp, &elt, arena);
5101 if (res != 0) goto failed;
5102 Py_XDECREF(tmp);
5103 tmp = NULL;
5104 } else {
5105 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5106 return 1;
5107 }
5108 if (PyObject_HasAttrString(obj, "generators")) {
5109 int res;
5110 Py_ssize_t len;
5111 Py_ssize_t i;
5112 tmp = PyObject_GetAttrString(obj, "generators");
5113 if (tmp == NULL) goto failed;
5114 if (!PyList_Check(tmp)) {
5115 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5116 goto failed;
5117 }
5118 len = PyList_GET_SIZE(tmp);
5119 generators = asdl_seq_new(len, arena);
5120 if (generators == NULL) goto failed;
5121 for (i = 0; i < len; i++) {
5122 comprehension_ty value;
5123 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5124 if (res != 0) goto failed;
5125 asdl_seq_SET(generators, i, value);
5126 }
5127 Py_XDECREF(tmp);
5128 tmp = NULL;
5129 } else {
5130 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5131 return 1;
5132 }
5133 *out = ListComp(elt, generators, lineno, col_offset, arena);
5134 if (*out == NULL) goto failed;
5135 return 0;
5136 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005137 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5138 if (isinstance == -1) {
5139 return 1;
5140 }
5141 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005142 expr_ty elt;
5143 asdl_seq* generators;
5144
5145 if (PyObject_HasAttrString(obj, "elt")) {
5146 int res;
5147 tmp = PyObject_GetAttrString(obj, "elt");
5148 if (tmp == NULL) goto failed;
5149 res = obj2ast_expr(tmp, &elt, arena);
5150 if (res != 0) goto failed;
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "generators")) {
5158 int res;
5159 Py_ssize_t len;
5160 Py_ssize_t i;
5161 tmp = PyObject_GetAttrString(obj, "generators");
5162 if (tmp == NULL) goto failed;
5163 if (!PyList_Check(tmp)) {
5164 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5165 goto failed;
5166 }
5167 len = PyList_GET_SIZE(tmp);
5168 generators = asdl_seq_new(len, arena);
5169 if (generators == NULL) goto failed;
5170 for (i = 0; i < len; i++) {
5171 comprehension_ty value;
5172 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5173 if (res != 0) goto failed;
5174 asdl_seq_SET(generators, i, value);
5175 }
5176 Py_XDECREF(tmp);
5177 tmp = NULL;
5178 } else {
5179 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5180 return 1;
5181 }
5182 *out = SetComp(elt, generators, lineno, col_offset, arena);
5183 if (*out == NULL) goto failed;
5184 return 0;
5185 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005186 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5187 if (isinstance == -1) {
5188 return 1;
5189 }
5190 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005191 expr_ty key;
5192 expr_ty value;
5193 asdl_seq* generators;
5194
5195 if (PyObject_HasAttrString(obj, "key")) {
5196 int res;
5197 tmp = PyObject_GetAttrString(obj, "key");
5198 if (tmp == NULL) goto failed;
5199 res = obj2ast_expr(tmp, &key, arena);
5200 if (res != 0) goto failed;
5201 Py_XDECREF(tmp);
5202 tmp = NULL;
5203 } else {
5204 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5205 return 1;
5206 }
5207 if (PyObject_HasAttrString(obj, "value")) {
5208 int res;
5209 tmp = PyObject_GetAttrString(obj, "value");
5210 if (tmp == NULL) goto failed;
5211 res = obj2ast_expr(tmp, &value, arena);
5212 if (res != 0) goto failed;
5213 Py_XDECREF(tmp);
5214 tmp = NULL;
5215 } else {
5216 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5217 return 1;
5218 }
5219 if (PyObject_HasAttrString(obj, "generators")) {
5220 int res;
5221 Py_ssize_t len;
5222 Py_ssize_t i;
5223 tmp = PyObject_GetAttrString(obj, "generators");
5224 if (tmp == NULL) goto failed;
5225 if (!PyList_Check(tmp)) {
5226 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5227 goto failed;
5228 }
5229 len = PyList_GET_SIZE(tmp);
5230 generators = asdl_seq_new(len, arena);
5231 if (generators == NULL) goto failed;
5232 for (i = 0; i < len; i++) {
5233 comprehension_ty value;
5234 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5235 if (res != 0) goto failed;
5236 asdl_seq_SET(generators, i, value);
5237 }
5238 Py_XDECREF(tmp);
5239 tmp = NULL;
5240 } else {
5241 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5242 return 1;
5243 }
5244 *out = DictComp(key, value, generators, lineno, col_offset,
5245 arena);
5246 if (*out == NULL) goto failed;
5247 return 0;
5248 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005249 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5250 if (isinstance == -1) {
5251 return 1;
5252 }
5253 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005254 expr_ty elt;
5255 asdl_seq* generators;
5256
5257 if (PyObject_HasAttrString(obj, "elt")) {
5258 int res;
5259 tmp = PyObject_GetAttrString(obj, "elt");
5260 if (tmp == NULL) goto failed;
5261 res = obj2ast_expr(tmp, &elt, arena);
5262 if (res != 0) goto failed;
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5267 return 1;
5268 }
5269 if (PyObject_HasAttrString(obj, "generators")) {
5270 int res;
5271 Py_ssize_t len;
5272 Py_ssize_t i;
5273 tmp = PyObject_GetAttrString(obj, "generators");
5274 if (tmp == NULL) goto failed;
5275 if (!PyList_Check(tmp)) {
5276 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5277 goto failed;
5278 }
5279 len = PyList_GET_SIZE(tmp);
5280 generators = asdl_seq_new(len, arena);
5281 if (generators == NULL) goto failed;
5282 for (i = 0; i < len; i++) {
5283 comprehension_ty value;
5284 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5285 if (res != 0) goto failed;
5286 asdl_seq_SET(generators, i, value);
5287 }
5288 Py_XDECREF(tmp);
5289 tmp = NULL;
5290 } else {
5291 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5292 return 1;
5293 }
5294 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5295 if (*out == NULL) goto failed;
5296 return 0;
5297 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005298 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5299 if (isinstance == -1) {
5300 return 1;
5301 }
5302 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005303 expr_ty value;
5304
5305 if (PyObject_HasAttrString(obj, "value")) {
5306 int res;
5307 tmp = PyObject_GetAttrString(obj, "value");
5308 if (tmp == NULL) goto failed;
5309 res = obj2ast_expr(tmp, &value, arena);
5310 if (res != 0) goto failed;
5311 Py_XDECREF(tmp);
5312 tmp = NULL;
5313 } else {
5314 value = NULL;
5315 }
5316 *out = Yield(value, lineno, col_offset, arena);
5317 if (*out == NULL) goto failed;
5318 return 0;
5319 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005320 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5321 if (isinstance == -1) {
5322 return 1;
5323 }
5324 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005325 expr_ty left;
5326 asdl_int_seq* ops;
5327 asdl_seq* comparators;
5328
5329 if (PyObject_HasAttrString(obj, "left")) {
5330 int res;
5331 tmp = PyObject_GetAttrString(obj, "left");
5332 if (tmp == NULL) goto failed;
5333 res = obj2ast_expr(tmp, &left, arena);
5334 if (res != 0) goto failed;
5335 Py_XDECREF(tmp);
5336 tmp = NULL;
5337 } else {
5338 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5339 return 1;
5340 }
5341 if (PyObject_HasAttrString(obj, "ops")) {
5342 int res;
5343 Py_ssize_t len;
5344 Py_ssize_t i;
5345 tmp = PyObject_GetAttrString(obj, "ops");
5346 if (tmp == NULL) goto failed;
5347 if (!PyList_Check(tmp)) {
5348 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5349 goto failed;
5350 }
5351 len = PyList_GET_SIZE(tmp);
5352 ops = asdl_int_seq_new(len, arena);
5353 if (ops == NULL) goto failed;
5354 for (i = 0; i < len; i++) {
5355 cmpop_ty value;
5356 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5357 if (res != 0) goto failed;
5358 asdl_seq_SET(ops, i, value);
5359 }
5360 Py_XDECREF(tmp);
5361 tmp = NULL;
5362 } else {
5363 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5364 return 1;
5365 }
5366 if (PyObject_HasAttrString(obj, "comparators")) {
5367 int res;
5368 Py_ssize_t len;
5369 Py_ssize_t i;
5370 tmp = PyObject_GetAttrString(obj, "comparators");
5371 if (tmp == NULL) goto failed;
5372 if (!PyList_Check(tmp)) {
5373 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5374 goto failed;
5375 }
5376 len = PyList_GET_SIZE(tmp);
5377 comparators = asdl_seq_new(len, arena);
5378 if (comparators == NULL) goto failed;
5379 for (i = 0; i < len; i++) {
5380 expr_ty value;
5381 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5382 if (res != 0) goto failed;
5383 asdl_seq_SET(comparators, i, value);
5384 }
5385 Py_XDECREF(tmp);
5386 tmp = NULL;
5387 } else {
5388 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5389 return 1;
5390 }
5391 *out = Compare(left, ops, comparators, lineno, col_offset,
5392 arena);
5393 if (*out == NULL) goto failed;
5394 return 0;
5395 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005396 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5397 if (isinstance == -1) {
5398 return 1;
5399 }
5400 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005401 expr_ty func;
5402 asdl_seq* args;
5403 asdl_seq* keywords;
5404 expr_ty starargs;
5405 expr_ty kwargs;
5406
5407 if (PyObject_HasAttrString(obj, "func")) {
5408 int res;
5409 tmp = PyObject_GetAttrString(obj, "func");
5410 if (tmp == NULL) goto failed;
5411 res = obj2ast_expr(tmp, &func, arena);
5412 if (res != 0) goto failed;
5413 Py_XDECREF(tmp);
5414 tmp = NULL;
5415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5417 return 1;
5418 }
5419 if (PyObject_HasAttrString(obj, "args")) {
5420 int res;
5421 Py_ssize_t len;
5422 Py_ssize_t i;
5423 tmp = PyObject_GetAttrString(obj, "args");
5424 if (tmp == NULL) goto failed;
5425 if (!PyList_Check(tmp)) {
5426 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5427 goto failed;
5428 }
5429 len = PyList_GET_SIZE(tmp);
5430 args = asdl_seq_new(len, arena);
5431 if (args == NULL) goto failed;
5432 for (i = 0; i < len; i++) {
5433 expr_ty value;
5434 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5435 if (res != 0) goto failed;
5436 asdl_seq_SET(args, i, value);
5437 }
5438 Py_XDECREF(tmp);
5439 tmp = NULL;
5440 } else {
5441 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5442 return 1;
5443 }
5444 if (PyObject_HasAttrString(obj, "keywords")) {
5445 int res;
5446 Py_ssize_t len;
5447 Py_ssize_t i;
5448 tmp = PyObject_GetAttrString(obj, "keywords");
5449 if (tmp == NULL) goto failed;
5450 if (!PyList_Check(tmp)) {
5451 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5452 goto failed;
5453 }
5454 len = PyList_GET_SIZE(tmp);
5455 keywords = asdl_seq_new(len, arena);
5456 if (keywords == NULL) goto failed;
5457 for (i = 0; i < len; i++) {
5458 keyword_ty value;
5459 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5460 if (res != 0) goto failed;
5461 asdl_seq_SET(keywords, i, value);
5462 }
5463 Py_XDECREF(tmp);
5464 tmp = NULL;
5465 } else {
5466 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5467 return 1;
5468 }
5469 if (PyObject_HasAttrString(obj, "starargs")) {
5470 int res;
5471 tmp = PyObject_GetAttrString(obj, "starargs");
5472 if (tmp == NULL) goto failed;
5473 res = obj2ast_expr(tmp, &starargs, arena);
5474 if (res != 0) goto failed;
5475 Py_XDECREF(tmp);
5476 tmp = NULL;
5477 } else {
5478 starargs = NULL;
5479 }
5480 if (PyObject_HasAttrString(obj, "kwargs")) {
5481 int res;
5482 tmp = PyObject_GetAttrString(obj, "kwargs");
5483 if (tmp == NULL) goto failed;
5484 res = obj2ast_expr(tmp, &kwargs, arena);
5485 if (res != 0) goto failed;
5486 Py_XDECREF(tmp);
5487 tmp = NULL;
5488 } else {
5489 kwargs = NULL;
5490 }
5491 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5492 col_offset, arena);
5493 if (*out == NULL) goto failed;
5494 return 0;
5495 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005496 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5497 if (isinstance == -1) {
5498 return 1;
5499 }
5500 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005501 object n;
5502
5503 if (PyObject_HasAttrString(obj, "n")) {
5504 int res;
5505 tmp = PyObject_GetAttrString(obj, "n");
5506 if (tmp == NULL) goto failed;
5507 res = obj2ast_object(tmp, &n, arena);
5508 if (res != 0) goto failed;
5509 Py_XDECREF(tmp);
5510 tmp = NULL;
5511 } else {
5512 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5513 return 1;
5514 }
5515 *out = Num(n, lineno, col_offset, arena);
5516 if (*out == NULL) goto failed;
5517 return 0;
5518 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005519 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5520 if (isinstance == -1) {
5521 return 1;
5522 }
5523 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005524 string s;
5525
5526 if (PyObject_HasAttrString(obj, "s")) {
5527 int res;
5528 tmp = PyObject_GetAttrString(obj, "s");
5529 if (tmp == NULL) goto failed;
5530 res = obj2ast_string(tmp, &s, arena);
5531 if (res != 0) goto failed;
5532 Py_XDECREF(tmp);
5533 tmp = NULL;
5534 } else {
5535 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5536 return 1;
5537 }
5538 *out = Str(s, lineno, col_offset, arena);
5539 if (*out == NULL) goto failed;
5540 return 0;
5541 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005542 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5543 if (isinstance == -1) {
5544 return 1;
5545 }
5546 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005547 string s;
5548
5549 if (PyObject_HasAttrString(obj, "s")) {
5550 int res;
5551 tmp = PyObject_GetAttrString(obj, "s");
5552 if (tmp == NULL) goto failed;
5553 res = obj2ast_string(tmp, &s, arena);
5554 if (res != 0) goto failed;
5555 Py_XDECREF(tmp);
5556 tmp = NULL;
5557 } else {
5558 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5559 return 1;
5560 }
5561 *out = Bytes(s, lineno, col_offset, arena);
5562 if (*out == NULL) goto failed;
5563 return 0;
5564 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005565 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5566 if (isinstance == -1) {
5567 return 1;
5568 }
5569 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005570
5571 *out = Ellipsis(lineno, col_offset, arena);
5572 if (*out == NULL) goto failed;
5573 return 0;
5574 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005575 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5576 if (isinstance == -1) {
5577 return 1;
5578 }
5579 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005580 expr_ty value;
5581 identifier attr;
5582 expr_context_ty ctx;
5583
5584 if (PyObject_HasAttrString(obj, "value")) {
5585 int res;
5586 tmp = PyObject_GetAttrString(obj, "value");
5587 if (tmp == NULL) goto failed;
5588 res = obj2ast_expr(tmp, &value, arena);
5589 if (res != 0) goto failed;
5590 Py_XDECREF(tmp);
5591 tmp = NULL;
5592 } else {
5593 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5594 return 1;
5595 }
5596 if (PyObject_HasAttrString(obj, "attr")) {
5597 int res;
5598 tmp = PyObject_GetAttrString(obj, "attr");
5599 if (tmp == NULL) goto failed;
5600 res = obj2ast_identifier(tmp, &attr, arena);
5601 if (res != 0) goto failed;
5602 Py_XDECREF(tmp);
5603 tmp = NULL;
5604 } else {
5605 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5606 return 1;
5607 }
5608 if (PyObject_HasAttrString(obj, "ctx")) {
5609 int res;
5610 tmp = PyObject_GetAttrString(obj, "ctx");
5611 if (tmp == NULL) goto failed;
5612 res = obj2ast_expr_context(tmp, &ctx, arena);
5613 if (res != 0) goto failed;
5614 Py_XDECREF(tmp);
5615 tmp = NULL;
5616 } else {
5617 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5618 return 1;
5619 }
5620 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5621 if (*out == NULL) goto failed;
5622 return 0;
5623 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005624 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5625 if (isinstance == -1) {
5626 return 1;
5627 }
5628 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005629 expr_ty value;
5630 slice_ty slice;
5631 expr_context_ty ctx;
5632
5633 if (PyObject_HasAttrString(obj, "value")) {
5634 int res;
5635 tmp = PyObject_GetAttrString(obj, "value");
5636 if (tmp == NULL) goto failed;
5637 res = obj2ast_expr(tmp, &value, arena);
5638 if (res != 0) goto failed;
5639 Py_XDECREF(tmp);
5640 tmp = NULL;
5641 } else {
5642 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5643 return 1;
5644 }
5645 if (PyObject_HasAttrString(obj, "slice")) {
5646 int res;
5647 tmp = PyObject_GetAttrString(obj, "slice");
5648 if (tmp == NULL) goto failed;
5649 res = obj2ast_slice(tmp, &slice, arena);
5650 if (res != 0) goto failed;
5651 Py_XDECREF(tmp);
5652 tmp = NULL;
5653 } else {
5654 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5655 return 1;
5656 }
5657 if (PyObject_HasAttrString(obj, "ctx")) {
5658 int res;
5659 tmp = PyObject_GetAttrString(obj, "ctx");
5660 if (tmp == NULL) goto failed;
5661 res = obj2ast_expr_context(tmp, &ctx, arena);
5662 if (res != 0) goto failed;
5663 Py_XDECREF(tmp);
5664 tmp = NULL;
5665 } else {
5666 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5667 return 1;
5668 }
5669 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5670 if (*out == NULL) goto failed;
5671 return 0;
5672 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005673 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5674 if (isinstance == -1) {
5675 return 1;
5676 }
5677 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005678 expr_ty value;
5679 expr_context_ty ctx;
5680
5681 if (PyObject_HasAttrString(obj, "value")) {
5682 int res;
5683 tmp = PyObject_GetAttrString(obj, "value");
5684 if (tmp == NULL) goto failed;
5685 res = obj2ast_expr(tmp, &value, arena);
5686 if (res != 0) goto failed;
5687 Py_XDECREF(tmp);
5688 tmp = NULL;
5689 } else {
5690 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5691 return 1;
5692 }
5693 if (PyObject_HasAttrString(obj, "ctx")) {
5694 int res;
5695 tmp = PyObject_GetAttrString(obj, "ctx");
5696 if (tmp == NULL) goto failed;
5697 res = obj2ast_expr_context(tmp, &ctx, arena);
5698 if (res != 0) goto failed;
5699 Py_XDECREF(tmp);
5700 tmp = NULL;
5701 } else {
5702 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5703 return 1;
5704 }
5705 *out = Starred(value, ctx, lineno, col_offset, arena);
5706 if (*out == NULL) goto failed;
5707 return 0;
5708 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005709 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5710 if (isinstance == -1) {
5711 return 1;
5712 }
5713 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005714 identifier id;
5715 expr_context_ty ctx;
5716
5717 if (PyObject_HasAttrString(obj, "id")) {
5718 int res;
5719 tmp = PyObject_GetAttrString(obj, "id");
5720 if (tmp == NULL) goto failed;
5721 res = obj2ast_identifier(tmp, &id, arena);
5722 if (res != 0) goto failed;
5723 Py_XDECREF(tmp);
5724 tmp = NULL;
5725 } else {
5726 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5727 return 1;
5728 }
5729 if (PyObject_HasAttrString(obj, "ctx")) {
5730 int res;
5731 tmp = PyObject_GetAttrString(obj, "ctx");
5732 if (tmp == NULL) goto failed;
5733 res = obj2ast_expr_context(tmp, &ctx, arena);
5734 if (res != 0) goto failed;
5735 Py_XDECREF(tmp);
5736 tmp = NULL;
5737 } else {
5738 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5739 return 1;
5740 }
5741 *out = Name(id, ctx, lineno, col_offset, arena);
5742 if (*out == NULL) goto failed;
5743 return 0;
5744 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005745 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5746 if (isinstance == -1) {
5747 return 1;
5748 }
5749 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005750 asdl_seq* elts;
5751 expr_context_ty ctx;
5752
5753 if (PyObject_HasAttrString(obj, "elts")) {
5754 int res;
5755 Py_ssize_t len;
5756 Py_ssize_t i;
5757 tmp = PyObject_GetAttrString(obj, "elts");
5758 if (tmp == NULL) goto failed;
5759 if (!PyList_Check(tmp)) {
5760 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5761 goto failed;
5762 }
5763 len = PyList_GET_SIZE(tmp);
5764 elts = asdl_seq_new(len, arena);
5765 if (elts == NULL) goto failed;
5766 for (i = 0; i < len; i++) {
5767 expr_ty value;
5768 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5769 if (res != 0) goto failed;
5770 asdl_seq_SET(elts, i, value);
5771 }
5772 Py_XDECREF(tmp);
5773 tmp = NULL;
5774 } else {
5775 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5776 return 1;
5777 }
5778 if (PyObject_HasAttrString(obj, "ctx")) {
5779 int res;
5780 tmp = PyObject_GetAttrString(obj, "ctx");
5781 if (tmp == NULL) goto failed;
5782 res = obj2ast_expr_context(tmp, &ctx, arena);
5783 if (res != 0) goto failed;
5784 Py_XDECREF(tmp);
5785 tmp = NULL;
5786 } else {
5787 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5788 return 1;
5789 }
5790 *out = List(elts, ctx, lineno, col_offset, arena);
5791 if (*out == NULL) goto failed;
5792 return 0;
5793 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005794 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5795 if (isinstance == -1) {
5796 return 1;
5797 }
5798 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005799 asdl_seq* elts;
5800 expr_context_ty ctx;
5801
5802 if (PyObject_HasAttrString(obj, "elts")) {
5803 int res;
5804 Py_ssize_t len;
5805 Py_ssize_t i;
5806 tmp = PyObject_GetAttrString(obj, "elts");
5807 if (tmp == NULL) goto failed;
5808 if (!PyList_Check(tmp)) {
5809 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5810 goto failed;
5811 }
5812 len = PyList_GET_SIZE(tmp);
5813 elts = asdl_seq_new(len, arena);
5814 if (elts == NULL) goto failed;
5815 for (i = 0; i < len; i++) {
5816 expr_ty value;
5817 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5818 if (res != 0) goto failed;
5819 asdl_seq_SET(elts, i, value);
5820 }
5821 Py_XDECREF(tmp);
5822 tmp = NULL;
5823 } else {
5824 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5825 return 1;
5826 }
5827 if (PyObject_HasAttrString(obj, "ctx")) {
5828 int res;
5829 tmp = PyObject_GetAttrString(obj, "ctx");
5830 if (tmp == NULL) goto failed;
5831 res = obj2ast_expr_context(tmp, &ctx, arena);
5832 if (res != 0) goto failed;
5833 Py_XDECREF(tmp);
5834 tmp = NULL;
5835 } else {
5836 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5837 return 1;
5838 }
5839 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5840 if (*out == NULL) goto failed;
5841 return 0;
5842 }
5843
Benjamin Peterson5b066812010-11-20 01:38:49 +00005844 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5845 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00005846 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005847 return 1;
5848}
5849
5850int
5851obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5852{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005853 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005854
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005855 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5856 if (isinstance == -1) {
5857 return 1;
5858 }
5859 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005860 *out = Load;
5861 return 0;
5862 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005863 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5864 if (isinstance == -1) {
5865 return 1;
5866 }
5867 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005868 *out = Store;
5869 return 0;
5870 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005871 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5872 if (isinstance == -1) {
5873 return 1;
5874 }
5875 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005876 *out = Del;
5877 return 0;
5878 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005879 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5880 if (isinstance == -1) {
5881 return 1;
5882 }
5883 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005884 *out = AugLoad;
5885 return 0;
5886 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005887 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5888 if (isinstance == -1) {
5889 return 1;
5890 }
5891 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005892 *out = AugStore;
5893 return 0;
5894 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005895 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5896 if (isinstance == -1) {
5897 return 1;
5898 }
5899 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005900 *out = Param;
5901 return 0;
5902 }
5903
Benjamin Peterson5b066812010-11-20 01:38:49 +00005904 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005905 return 1;
5906}
5907
5908int
5909obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5910{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005911 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005912
Benjamin Petersond8f65972010-11-20 04:31:07 +00005913 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005914
5915 if (obj == Py_None) {
5916 *out = NULL;
5917 return 0;
5918 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005919 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5920 if (isinstance == -1) {
5921 return 1;
5922 }
5923 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005924 expr_ty lower;
5925 expr_ty upper;
5926 expr_ty step;
5927
5928 if (PyObject_HasAttrString(obj, "lower")) {
5929 int res;
5930 tmp = PyObject_GetAttrString(obj, "lower");
5931 if (tmp == NULL) goto failed;
5932 res = obj2ast_expr(tmp, &lower, arena);
5933 if (res != 0) goto failed;
5934 Py_XDECREF(tmp);
5935 tmp = NULL;
5936 } else {
5937 lower = NULL;
5938 }
5939 if (PyObject_HasAttrString(obj, "upper")) {
5940 int res;
5941 tmp = PyObject_GetAttrString(obj, "upper");
5942 if (tmp == NULL) goto failed;
5943 res = obj2ast_expr(tmp, &upper, arena);
5944 if (res != 0) goto failed;
5945 Py_XDECREF(tmp);
5946 tmp = NULL;
5947 } else {
5948 upper = NULL;
5949 }
5950 if (PyObject_HasAttrString(obj, "step")) {
5951 int res;
5952 tmp = PyObject_GetAttrString(obj, "step");
5953 if (tmp == NULL) goto failed;
5954 res = obj2ast_expr(tmp, &step, arena);
5955 if (res != 0) goto failed;
5956 Py_XDECREF(tmp);
5957 tmp = NULL;
5958 } else {
5959 step = NULL;
5960 }
5961 *out = Slice(lower, upper, step, arena);
5962 if (*out == NULL) goto failed;
5963 return 0;
5964 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00005965 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5966 if (isinstance == -1) {
5967 return 1;
5968 }
5969 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005970 asdl_seq* dims;
5971
5972 if (PyObject_HasAttrString(obj, "dims")) {
5973 int res;
5974 Py_ssize_t len;
5975 Py_ssize_t i;
5976 tmp = PyObject_GetAttrString(obj, "dims");
5977 if (tmp == NULL) goto failed;
5978 if (!PyList_Check(tmp)) {
5979 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5980 goto failed;
5981 }
5982 len = PyList_GET_SIZE(tmp);
5983 dims = asdl_seq_new(len, arena);
5984 if (dims == NULL) goto failed;
5985 for (i = 0; i < len; i++) {
5986 slice_ty value;
5987 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5988 if (res != 0) goto failed;
5989 asdl_seq_SET(dims, i, value);
5990 }
5991 Py_XDECREF(tmp);
5992 tmp = NULL;
5993 } else {
5994 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5995 return 1;
5996 }
5997 *out = ExtSlice(dims, arena);
5998 if (*out == NULL) goto failed;
5999 return 0;
6000 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006001 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6002 if (isinstance == -1) {
6003 return 1;
6004 }
6005 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006006 expr_ty value;
6007
6008 if (PyObject_HasAttrString(obj, "value")) {
6009 int res;
6010 tmp = PyObject_GetAttrString(obj, "value");
6011 if (tmp == NULL) goto failed;
6012 res = obj2ast_expr(tmp, &value, arena);
6013 if (res != 0) goto failed;
6014 Py_XDECREF(tmp);
6015 tmp = NULL;
6016 } else {
6017 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6018 return 1;
6019 }
6020 *out = Index(value, arena);
6021 if (*out == NULL) goto failed;
6022 return 0;
6023 }
6024
Benjamin Peterson5b066812010-11-20 01:38:49 +00006025 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6026 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006027 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006028 return 1;
6029}
6030
6031int
6032obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6033{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006034 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006035
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006036 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6037 if (isinstance == -1) {
6038 return 1;
6039 }
6040 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006041 *out = And;
6042 return 0;
6043 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006044 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6045 if (isinstance == -1) {
6046 return 1;
6047 }
6048 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006049 *out = Or;
6050 return 0;
6051 }
6052
Benjamin Peterson5b066812010-11-20 01:38:49 +00006053 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006054 return 1;
6055}
6056
6057int
6058obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6059{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006060 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006061
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006062 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6063 if (isinstance == -1) {
6064 return 1;
6065 }
6066 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006067 *out = Add;
6068 return 0;
6069 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006070 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6071 if (isinstance == -1) {
6072 return 1;
6073 }
6074 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006075 *out = Sub;
6076 return 0;
6077 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006078 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6079 if (isinstance == -1) {
6080 return 1;
6081 }
6082 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006083 *out = Mult;
6084 return 0;
6085 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006086 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6087 if (isinstance == -1) {
6088 return 1;
6089 }
6090 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006091 *out = Div;
6092 return 0;
6093 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006094 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6095 if (isinstance == -1) {
6096 return 1;
6097 }
6098 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006099 *out = Mod;
6100 return 0;
6101 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006102 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6103 if (isinstance == -1) {
6104 return 1;
6105 }
6106 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006107 *out = Pow;
6108 return 0;
6109 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006110 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6111 if (isinstance == -1) {
6112 return 1;
6113 }
6114 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006115 *out = LShift;
6116 return 0;
6117 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006118 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6119 if (isinstance == -1) {
6120 return 1;
6121 }
6122 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006123 *out = RShift;
6124 return 0;
6125 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006126 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6127 if (isinstance == -1) {
6128 return 1;
6129 }
6130 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006131 *out = BitOr;
6132 return 0;
6133 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006134 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6135 if (isinstance == -1) {
6136 return 1;
6137 }
6138 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006139 *out = BitXor;
6140 return 0;
6141 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006142 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6143 if (isinstance == -1) {
6144 return 1;
6145 }
6146 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006147 *out = BitAnd;
6148 return 0;
6149 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006150 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6151 if (isinstance == -1) {
6152 return 1;
6153 }
6154 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006155 *out = FloorDiv;
6156 return 0;
6157 }
6158
Benjamin Peterson5b066812010-11-20 01:38:49 +00006159 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006160 return 1;
6161}
6162
6163int
6164obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6165{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006166 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006167
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006168 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6169 if (isinstance == -1) {
6170 return 1;
6171 }
6172 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006173 *out = Invert;
6174 return 0;
6175 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006176 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6177 if (isinstance == -1) {
6178 return 1;
6179 }
6180 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006181 *out = Not;
6182 return 0;
6183 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006184 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6185 if (isinstance == -1) {
6186 return 1;
6187 }
6188 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006189 *out = UAdd;
6190 return 0;
6191 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006192 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6193 if (isinstance == -1) {
6194 return 1;
6195 }
6196 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006197 *out = USub;
6198 return 0;
6199 }
6200
Benjamin Peterson5b066812010-11-20 01:38:49 +00006201 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006202 return 1;
6203}
6204
6205int
6206obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6207{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006208 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006209
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006210 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6211 if (isinstance == -1) {
6212 return 1;
6213 }
6214 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006215 *out = Eq;
6216 return 0;
6217 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006218 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6219 if (isinstance == -1) {
6220 return 1;
6221 }
6222 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006223 *out = NotEq;
6224 return 0;
6225 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006226 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6227 if (isinstance == -1) {
6228 return 1;
6229 }
6230 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006231 *out = Lt;
6232 return 0;
6233 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006234 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6235 if (isinstance == -1) {
6236 return 1;
6237 }
6238 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006239 *out = LtE;
6240 return 0;
6241 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006242 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6243 if (isinstance == -1) {
6244 return 1;
6245 }
6246 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006247 *out = Gt;
6248 return 0;
6249 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006250 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6251 if (isinstance == -1) {
6252 return 1;
6253 }
6254 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006255 *out = GtE;
6256 return 0;
6257 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006258 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6259 if (isinstance == -1) {
6260 return 1;
6261 }
6262 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006263 *out = Is;
6264 return 0;
6265 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006266 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6267 if (isinstance == -1) {
6268 return 1;
6269 }
6270 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006271 *out = IsNot;
6272 return 0;
6273 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006274 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6275 if (isinstance == -1) {
6276 return 1;
6277 }
6278 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006279 *out = In;
6280 return 0;
6281 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006282 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6283 if (isinstance == -1) {
6284 return 1;
6285 }
6286 if (isinstance) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006287 *out = NotIn;
6288 return 0;
6289 }
6290
Benjamin Peterson5b066812010-11-20 01:38:49 +00006291 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006292 return 1;
6293}
6294
6295int
6296obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6297{
6298 PyObject* tmp = NULL;
6299 expr_ty target;
6300 expr_ty iter;
6301 asdl_seq* ifs;
6302
6303 if (PyObject_HasAttrString(obj, "target")) {
6304 int res;
6305 tmp = PyObject_GetAttrString(obj, "target");
6306 if (tmp == NULL) goto failed;
6307 res = obj2ast_expr(tmp, &target, arena);
6308 if (res != 0) goto failed;
6309 Py_XDECREF(tmp);
6310 tmp = NULL;
6311 } else {
6312 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6313 return 1;
6314 }
6315 if (PyObject_HasAttrString(obj, "iter")) {
6316 int res;
6317 tmp = PyObject_GetAttrString(obj, "iter");
6318 if (tmp == NULL) goto failed;
6319 res = obj2ast_expr(tmp, &iter, arena);
6320 if (res != 0) goto failed;
6321 Py_XDECREF(tmp);
6322 tmp = NULL;
6323 } else {
6324 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6325 return 1;
6326 }
6327 if (PyObject_HasAttrString(obj, "ifs")) {
6328 int res;
6329 Py_ssize_t len;
6330 Py_ssize_t i;
6331 tmp = PyObject_GetAttrString(obj, "ifs");
6332 if (tmp == NULL) goto failed;
6333 if (!PyList_Check(tmp)) {
6334 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6335 goto failed;
6336 }
6337 len = PyList_GET_SIZE(tmp);
6338 ifs = asdl_seq_new(len, arena);
6339 if (ifs == NULL) goto failed;
6340 for (i = 0; i < len; i++) {
6341 expr_ty value;
6342 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6343 if (res != 0) goto failed;
6344 asdl_seq_SET(ifs, i, value);
6345 }
6346 Py_XDECREF(tmp);
6347 tmp = NULL;
6348 } else {
6349 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6350 return 1;
6351 }
6352 *out = comprehension(target, iter, ifs, arena);
6353 return 0;
6354failed:
6355 Py_XDECREF(tmp);
6356 return 1;
6357}
6358
6359int
6360obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6361{
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006362 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006363
Benjamin Petersond8f65972010-11-20 04:31:07 +00006364 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006365 int lineno;
6366 int col_offset;
6367
Neal Norwitzad74aa82008-03-31 05:14:30 +00006368 if (obj == Py_None) {
6369 *out = NULL;
6370 return 0;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006371 }
6372 if (PyObject_HasAttrString(obj, "lineno")) {
6373 int res;
6374 tmp = PyObject_GetAttrString(obj, "lineno");
6375 if (tmp == NULL) goto failed;
6376 res = obj2ast_int(tmp, &lineno, arena);
6377 if (res != 0) goto failed;
6378 Py_XDECREF(tmp);
6379 tmp = NULL;
6380 } else {
6381 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6382 return 1;
6383 }
6384 if (PyObject_HasAttrString(obj, "col_offset")) {
6385 int res;
6386 tmp = PyObject_GetAttrString(obj, "col_offset");
6387 if (tmp == NULL) goto failed;
6388 res = obj2ast_int(tmp, &col_offset, arena);
6389 if (res != 0) goto failed;
6390 Py_XDECREF(tmp);
6391 tmp = NULL;
6392 } else {
6393 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6394 return 1;
6395 }
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006396 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6397 if (isinstance == -1) {
6398 return 1;
6399 }
6400 if (isinstance) {
Neal Norwitzad74aa82008-03-31 05:14:30 +00006401 expr_ty type;
6402 identifier name;
6403 asdl_seq* body;
6404
6405 if (PyObject_HasAttrString(obj, "type")) {
6406 int res;
6407 tmp = PyObject_GetAttrString(obj, "type");
6408 if (tmp == NULL) goto failed;
6409 res = obj2ast_expr(tmp, &type, arena);
6410 if (res != 0) goto failed;
6411 Py_XDECREF(tmp);
6412 tmp = NULL;
6413 } else {
6414 type = NULL;
6415 }
6416 if (PyObject_HasAttrString(obj, "name")) {
6417 int res;
6418 tmp = PyObject_GetAttrString(obj, "name");
6419 if (tmp == NULL) goto failed;
6420 res = obj2ast_identifier(tmp, &name, arena);
6421 if (res != 0) goto failed;
6422 Py_XDECREF(tmp);
6423 tmp = NULL;
6424 } else {
6425 name = NULL;
6426 }
6427 if (PyObject_HasAttrString(obj, "body")) {
6428 int res;
6429 Py_ssize_t len;
6430 Py_ssize_t i;
6431 tmp = PyObject_GetAttrString(obj, "body");
6432 if (tmp == NULL) goto failed;
6433 if (!PyList_Check(tmp)) {
6434 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6435 goto failed;
6436 }
6437 len = PyList_GET_SIZE(tmp);
6438 body = asdl_seq_new(len, arena);
6439 if (body == NULL) goto failed;
6440 for (i = 0; i < len; i++) {
6441 stmt_ty value;
6442 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6443 if (res != 0) goto failed;
6444 asdl_seq_SET(body, i, value);
6445 }
6446 Py_XDECREF(tmp);
6447 tmp = NULL;
6448 } else {
6449 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6450 return 1;
6451 }
6452 *out = ExceptHandler(type, name, body, lineno, col_offset,
6453 arena);
6454 if (*out == NULL) goto failed;
6455 return 0;
6456 }
6457
Benjamin Peterson5b066812010-11-20 01:38:49 +00006458 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6459 failed:
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006460 Py_XDECREF(tmp);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006461 return 1;
6462}
6463
6464int
6465obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6466{
6467 PyObject* tmp = NULL;
6468 asdl_seq* args;
6469 identifier vararg;
6470 expr_ty varargannotation;
6471 asdl_seq* kwonlyargs;
6472 identifier kwarg;
6473 expr_ty kwargannotation;
6474 asdl_seq* defaults;
6475 asdl_seq* kw_defaults;
6476
6477 if (PyObject_HasAttrString(obj, "args")) {
6478 int res;
6479 Py_ssize_t len;
6480 Py_ssize_t i;
6481 tmp = PyObject_GetAttrString(obj, "args");
6482 if (tmp == NULL) goto failed;
6483 if (!PyList_Check(tmp)) {
6484 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6485 goto failed;
6486 }
6487 len = PyList_GET_SIZE(tmp);
6488 args = asdl_seq_new(len, arena);
6489 if (args == NULL) goto failed;
6490 for (i = 0; i < len; i++) {
6491 arg_ty value;
6492 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6493 if (res != 0) goto failed;
6494 asdl_seq_SET(args, i, value);
6495 }
6496 Py_XDECREF(tmp);
6497 tmp = NULL;
6498 } else {
6499 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6500 return 1;
6501 }
6502 if (PyObject_HasAttrString(obj, "vararg")) {
6503 int res;
6504 tmp = PyObject_GetAttrString(obj, "vararg");
6505 if (tmp == NULL) goto failed;
6506 res = obj2ast_identifier(tmp, &vararg, arena);
6507 if (res != 0) goto failed;
6508 Py_XDECREF(tmp);
6509 tmp = NULL;
6510 } else {
6511 vararg = NULL;
6512 }
6513 if (PyObject_HasAttrString(obj, "varargannotation")) {
6514 int res;
6515 tmp = PyObject_GetAttrString(obj, "varargannotation");
6516 if (tmp == NULL) goto failed;
6517 res = obj2ast_expr(tmp, &varargannotation, arena);
6518 if (res != 0) goto failed;
6519 Py_XDECREF(tmp);
6520 tmp = NULL;
6521 } else {
6522 varargannotation = NULL;
6523 }
6524 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6525 int res;
6526 Py_ssize_t len;
6527 Py_ssize_t i;
6528 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6529 if (tmp == NULL) goto failed;
6530 if (!PyList_Check(tmp)) {
6531 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6532 goto failed;
6533 }
6534 len = PyList_GET_SIZE(tmp);
6535 kwonlyargs = asdl_seq_new(len, arena);
6536 if (kwonlyargs == NULL) goto failed;
6537 for (i = 0; i < len; i++) {
6538 arg_ty value;
6539 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6540 if (res != 0) goto failed;
6541 asdl_seq_SET(kwonlyargs, i, value);
6542 }
6543 Py_XDECREF(tmp);
6544 tmp = NULL;
6545 } else {
6546 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6547 return 1;
6548 }
6549 if (PyObject_HasAttrString(obj, "kwarg")) {
6550 int res;
6551 tmp = PyObject_GetAttrString(obj, "kwarg");
6552 if (tmp == NULL) goto failed;
6553 res = obj2ast_identifier(tmp, &kwarg, arena);
6554 if (res != 0) goto failed;
6555 Py_XDECREF(tmp);
6556 tmp = NULL;
6557 } else {
6558 kwarg = NULL;
6559 }
6560 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6561 int res;
6562 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6563 if (tmp == NULL) goto failed;
6564 res = obj2ast_expr(tmp, &kwargannotation, arena);
6565 if (res != 0) goto failed;
6566 Py_XDECREF(tmp);
6567 tmp = NULL;
6568 } else {
6569 kwargannotation = NULL;
6570 }
6571 if (PyObject_HasAttrString(obj, "defaults")) {
6572 int res;
6573 Py_ssize_t len;
6574 Py_ssize_t i;
6575 tmp = PyObject_GetAttrString(obj, "defaults");
6576 if (tmp == NULL) goto failed;
6577 if (!PyList_Check(tmp)) {
6578 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6579 goto failed;
6580 }
6581 len = PyList_GET_SIZE(tmp);
6582 defaults = asdl_seq_new(len, arena);
6583 if (defaults == NULL) goto failed;
6584 for (i = 0; i < len; i++) {
6585 expr_ty value;
6586 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6587 if (res != 0) goto failed;
6588 asdl_seq_SET(defaults, i, value);
6589 }
6590 Py_XDECREF(tmp);
6591 tmp = NULL;
6592 } else {
6593 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6594 return 1;
6595 }
6596 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6597 int res;
6598 Py_ssize_t len;
6599 Py_ssize_t i;
6600 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6601 if (tmp == NULL) goto failed;
6602 if (!PyList_Check(tmp)) {
6603 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6604 goto failed;
6605 }
6606 len = PyList_GET_SIZE(tmp);
6607 kw_defaults = asdl_seq_new(len, arena);
6608 if (kw_defaults == NULL) goto failed;
6609 for (i = 0; i < len; i++) {
6610 expr_ty value;
6611 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6612 if (res != 0) goto failed;
6613 asdl_seq_SET(kw_defaults, i, value);
6614 }
6615 Py_XDECREF(tmp);
6616 tmp = NULL;
6617 } else {
6618 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6619 return 1;
6620 }
6621 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6622 kwargannotation, defaults, kw_defaults, arena);
6623 return 0;
6624failed:
6625 Py_XDECREF(tmp);
6626 return 1;
6627}
6628
6629int
6630obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6631{
6632 PyObject* tmp = NULL;
6633 identifier arg;
6634 expr_ty annotation;
6635
6636 if (PyObject_HasAttrString(obj, "arg")) {
6637 int res;
6638 tmp = PyObject_GetAttrString(obj, "arg");
6639 if (tmp == NULL) goto failed;
6640 res = obj2ast_identifier(tmp, &arg, arena);
6641 if (res != 0) goto failed;
6642 Py_XDECREF(tmp);
6643 tmp = NULL;
6644 } else {
6645 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6646 return 1;
6647 }
6648 if (PyObject_HasAttrString(obj, "annotation")) {
6649 int res;
6650 tmp = PyObject_GetAttrString(obj, "annotation");
6651 if (tmp == NULL) goto failed;
6652 res = obj2ast_expr(tmp, &annotation, arena);
6653 if (res != 0) goto failed;
6654 Py_XDECREF(tmp);
6655 tmp = NULL;
6656 } else {
6657 annotation = NULL;
6658 }
6659 *out = arg(arg, annotation, arena);
6660 return 0;
6661failed:
6662 Py_XDECREF(tmp);
6663 return 1;
6664}
6665
6666int
6667obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6668{
6669 PyObject* tmp = NULL;
6670 identifier arg;
6671 expr_ty value;
6672
6673 if (PyObject_HasAttrString(obj, "arg")) {
6674 int res;
6675 tmp = PyObject_GetAttrString(obj, "arg");
6676 if (tmp == NULL) goto failed;
6677 res = obj2ast_identifier(tmp, &arg, arena);
6678 if (res != 0) goto failed;
6679 Py_XDECREF(tmp);
6680 tmp = NULL;
6681 } else {
6682 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6683 return 1;
6684 }
6685 if (PyObject_HasAttrString(obj, "value")) {
6686 int res;
6687 tmp = PyObject_GetAttrString(obj, "value");
6688 if (tmp == NULL) goto failed;
6689 res = obj2ast_expr(tmp, &value, arena);
6690 if (res != 0) goto failed;
6691 Py_XDECREF(tmp);
6692 tmp = NULL;
6693 } else {
6694 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6695 return 1;
6696 }
6697 *out = keyword(arg, value, arena);
6698 return 0;
6699failed:
6700 Py_XDECREF(tmp);
6701 return 1;
6702}
6703
6704int
6705obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6706{
6707 PyObject* tmp = NULL;
6708 identifier name;
6709 identifier asname;
6710
6711 if (PyObject_HasAttrString(obj, "name")) {
6712 int res;
6713 tmp = PyObject_GetAttrString(obj, "name");
6714 if (tmp == NULL) goto failed;
6715 res = obj2ast_identifier(tmp, &name, arena);
6716 if (res != 0) goto failed;
6717 Py_XDECREF(tmp);
6718 tmp = NULL;
6719 } else {
6720 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6721 return 1;
6722 }
6723 if (PyObject_HasAttrString(obj, "asname")) {
6724 int res;
6725 tmp = PyObject_GetAttrString(obj, "asname");
6726 if (tmp == NULL) goto failed;
6727 res = obj2ast_identifier(tmp, &asname, arena);
6728 if (res != 0) goto failed;
6729 Py_XDECREF(tmp);
6730 tmp = NULL;
6731 } else {
6732 asname = NULL;
6733 }
6734 *out = alias(name, asname, arena);
6735 return 0;
6736failed:
6737 Py_XDECREF(tmp);
6738 return 1;
6739}
6740
6741
Martin v. Löwis1a214512008-06-11 05:26:20 +00006742static struct PyModuleDef _astmodule = {
6743 PyModuleDef_HEAD_INIT, "_ast"
6744};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006745PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006746PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006747{
6748 PyObject *m, *d;
Martin v. Löwis1a214512008-06-11 05:26:20 +00006749 if (!init_types()) return NULL;
6750 m = PyModule_Create(&_astmodule);
6751 if (!m) return NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006752 d = PyModule_GetDict(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +00006753 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return
6754 NULL;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006755 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006756 return NULL;
Benjamin Petersonefad2442011-07-22 10:39:12 -05006757 if (PyModule_AddStringConstant(m, "__version__", "82163") < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006758 return NULL;
6759 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
6760 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006761 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006762 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006763 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006764 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006765 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006766 0) return NULL;
6767 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6768 NULL;
6769 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return
6770 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006771 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006772 < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006773 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006774 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006775 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006776 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006777 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006778 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006779 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006780 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006781 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006782 0) return NULL;
6783 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return
6784 NULL;
6785 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6786 NULL;
6787 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6788 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return
6789 NULL;
6790 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6791 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006792 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006793 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006794 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006795 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006796 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006797 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006798 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006799 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006800 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006801 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006802 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006803 return NULL;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006804 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006805 return NULL;
6806 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return
6807 NULL;
6808 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return
6809 NULL;
6810 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6811 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006812 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006813 return NULL;
6814 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return
6815 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006816 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006817 return NULL;
6818 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6819 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006820 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006821 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006822 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006823 return NULL;
6824 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6825 NULL;
6826 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return
6827 NULL;
6828 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return
6829 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006830 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006831 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006832 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006833 return NULL;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006834 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006835 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006836 if (PyDict_SetItemString(d, "GeneratorExp",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006837 (PyObject*)GeneratorExp_type) < 0) return NULL;
6838 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6839 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006840 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006841 return NULL;
6842 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return
6843 NULL;
6844 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return
6845 NULL;
6846 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return
6847 NULL;
6848 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6849 NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00006850 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006851 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006852 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006853 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006854 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006855 0) return NULL;
Guido van Rossum0368b722007-05-11 16:50:42 +00006856 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006857 return NULL;
6858 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return
6859 NULL;
6860 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return
6861 NULL;
6862 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6863 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006864 if (PyDict_SetItemString(d, "expr_context",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006865 (PyObject*)expr_context_type) < 0) return NULL;
6866 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return
6867 NULL;
6868 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return
6871 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006872 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006873 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006874 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006875 return NULL;
6876 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6877 NULL;
6878 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6879 NULL;
6880 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6881 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006882 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006883 return NULL;
6884 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6885 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006886 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006887 return NULL;
6888 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return
6889 NULL;
6890 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006891 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006892 return NULL;
6893 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return
6894 NULL;
6895 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return
6896 NULL;
6897 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return
6898 NULL;
6899 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return
6904 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006905 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006906 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006907 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006908 return NULL;
6909 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6910 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006911 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006912 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006914 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006915 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006916 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006917 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006918 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006919 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006920 return NULL;
6921 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return
6924 NULL;
6925 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return
6926 NULL;
6927 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6928 NULL;
6929 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6930 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6931 NULL;
6932 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6933 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return
6934 NULL;
6935 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6936 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return
6937 NULL;
6938 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6939 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6940 NULL;
6941 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6942 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6943 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006944 if (PyDict_SetItemString(d, "comprehension",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006945 (PyObject*)comprehension_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006946 if (PyDict_SetItemString(d, "excepthandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006947 (PyObject*)excepthandler_type) < 0) return NULL;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006948 if (PyDict_SetItemString(d, "ExceptHandler",
Martin v. Löwis1a214512008-06-11 05:26:20 +00006949 (PyObject*)ExceptHandler_type) < 0) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006950 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
Martin v. Löwis1a214512008-06-11 05:26:20 +00006951 0) return NULL;
6952 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return
6953 NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006954 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00006955 return NULL;
6956 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6957 NULL;
6958 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006959}
6960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006961
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006962PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006963{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006964 init_types();
6965 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006966}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006967
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006968/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6969mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006970{
6971 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006972 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6973 (PyObject*)Interactive_type};
6974 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006975 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006976 assert(0 <= mode && mode <= 2);
6977
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006978 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006979
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006980 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6981 if (isinstance == -1)
6982 return NULL;
6983 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006984 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6985 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006986 return NULL;
6987 }
6988 if (obj2ast_mod(ast, &res, arena) != 0)
6989 return NULL;
6990 else
6991 return res;
6992}
6993
6994int PyAST_Check(PyObject* obj)
6995{
6996 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006997 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006998}
6999
Martin v. Löwis5b222132007-06-10 09:51:05 +00007000