blob: e7c8f6d1176e5a556a240468b5079e6aa52b52d9 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07003#include <stddef.h>
4
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "Python.h"
6#include "Python-ast.h"
7
Neal Norwitz207c9f32008-03-31 04:42:11 +00008static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +00009static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000010static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000011static PyTypeObject *Module_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020012_Py_IDENTIFIER(body);
Neal Norwitz53d960c2006-02-28 22:47:29 +000013static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020014 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000015};
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *Interactive_type;
17static char *Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020018 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000019};
Neal Norwitz53d960c2006-02-28 22:47:29 +000020static PyTypeObject *Expression_type;
21static char *Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020022 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000023};
Neal Norwitz53d960c2006-02-28 22:47:29 +000024static PyTypeObject *Suite_type;
25static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020026 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000027};
Neal Norwitz53d960c2006-02-28 22:47:29 +000028static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020029_Py_IDENTIFIER(lineno);
30_Py_IDENTIFIER(col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000031static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020032 "lineno",
33 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000034};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000035static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000036static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020037_Py_IDENTIFIER(name);
38_Py_IDENTIFIER(args);
39_Py_IDENTIFIER(decorator_list);
40_Py_IDENTIFIER(returns);
Neal Norwitz53d960c2006-02-28 22:47:29 +000041static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020042 "name",
43 "args",
44 "body",
45 "decorator_list",
46 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Neal Norwitz53d960c2006-02-28 22:47:29 +000048static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020049_Py_IDENTIFIER(bases);
50_Py_IDENTIFIER(keywords);
51_Py_IDENTIFIER(starargs);
52_Py_IDENTIFIER(kwargs);
Neal Norwitz53d960c2006-02-28 22:47:29 +000053static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020054 "name",
55 "bases",
56 "keywords",
57 "starargs",
58 "kwargs",
59 "body",
60 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000061};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020063_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000064static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020065 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000066};
Neal Norwitz53d960c2006-02-28 22:47:29 +000067static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020068_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000069static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020070 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000071};
Neal Norwitz53d960c2006-02-28 22:47:29 +000072static PyTypeObject *Assign_type;
73static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "targets",
75 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000076};
Neal Norwitz53d960c2006-02-28 22:47:29 +000077static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020078_Py_IDENTIFIER(target);
79_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000080static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020081 "target",
82 "op",
83 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000084};
Neal Norwitz53d960c2006-02-28 22:47:29 +000085static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020086_Py_IDENTIFIER(iter);
87_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +000088static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020089 "target",
90 "iter",
91 "body",
92 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000093};
Neal Norwitz53d960c2006-02-28 22:47:29 +000094static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020095_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +000096static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020097 "test",
98 "body",
99 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000100};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000101static PyTypeObject *If_type;
102static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200103 "test",
104 "body",
105 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200108_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000109static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200110 "items",
111 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200114_Py_IDENTIFIER(exc);
115_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200117 "exc",
118 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000119};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500120static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200121_Py_IDENTIFIER(handlers);
122_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500123static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200124 "body",
125 "handlers",
126 "orelse",
127 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000129static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200130_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000131static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200132 "test",
133 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000134};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000135static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200136_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000137static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200138 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000139};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000140static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200141_Py_IDENTIFIER(module);
142_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000143static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200144 "module",
145 "names",
146 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000147};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000148static PyTypeObject *Global_type;
149static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200150 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000151};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000152static PyTypeObject *Nonlocal_type;
153static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200154 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000155};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static PyTypeObject *Expr_type;
157static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200158 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000159};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000160static PyTypeObject *Pass_type;
161static PyTypeObject *Break_type;
162static PyTypeObject *Continue_type;
163static PyTypeObject *expr_type;
164static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200165 "lineno",
166 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000167};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000168static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000169static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200170_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000171static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200172 "op",
173 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200176_Py_IDENTIFIER(left);
177_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000178static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200179 "left",
180 "op",
181 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000182};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000183static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200184_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200186 "op",
187 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000188};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000189static PyTypeObject *Lambda_type;
190static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200191 "args",
192 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000194static PyTypeObject *IfExp_type;
195static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200196 "test",
197 "body",
198 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000199};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000200static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200201_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000202static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200203 "keys",
204 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000205};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000206static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200207_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000208static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200209 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200212_Py_IDENTIFIER(elt);
213_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000214static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200215 "elt",
216 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000218static PyTypeObject *SetComp_type;
219static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200220 "elt",
221 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000222};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000223static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200224_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000225static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200226 "key",
227 "value",
228 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000229};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000230static PyTypeObject *GeneratorExp_type;
231static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200232 "elt",
233 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000234};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000235static PyTypeObject *Yield_type;
236static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200237 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500238};
239static PyTypeObject *YieldFrom_type;
240static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200241 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000242};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000243static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200244_Py_IDENTIFIER(ops);
245_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000246static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200247 "left",
248 "ops",
249 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000251static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200252_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000253static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200254 "func",
255 "args",
256 "keywords",
257 "starargs",
258 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000260static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200261_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000262static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200263 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000264};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000265static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200266_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000267static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200268 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000269};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000270static PyTypeObject *Bytes_type;
271static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200272 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000273};
Georg Brandl52318d62006-09-06 07:06:08 +0000274static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000275static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200276_Py_IDENTIFIER(attr);
277_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200279 "value",
280 "attr",
281 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000282};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000283static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200284_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200286 "value",
287 "slice",
288 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000289};
Guido van Rossum0368b722007-05-11 16:50:42 +0000290static PyTypeObject *Starred_type;
291static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200292 "value",
293 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000294};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000295static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200296_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000297static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200298 "id",
299 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000300};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000301static PyTypeObject *List_type;
302static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200303 "elts",
304 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000305};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000306static PyTypeObject *Tuple_type;
307static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200308 "elts",
309 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000310};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000311static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000312static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
313*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
314static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000315static PyTypeObject *Load_type;
316static PyTypeObject *Store_type;
317static PyTypeObject *Del_type;
318static PyTypeObject *AugLoad_type;
319static PyTypeObject *AugStore_type;
320static PyTypeObject *Param_type;
321static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000322static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000323static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200324_Py_IDENTIFIER(lower);
325_Py_IDENTIFIER(upper);
326_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200328 "lower",
329 "upper",
330 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000331};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000332static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200333_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000334static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200335 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000336};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000337static PyTypeObject *Index_type;
338static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200339 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000340};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000341static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000342static PyObject *And_singleton, *Or_singleton;
343static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000344static PyTypeObject *And_type;
345static PyTypeObject *Or_type;
346static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
348*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
349*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
350*FloorDiv_singleton;
351static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static PyTypeObject *Add_type;
353static PyTypeObject *Sub_type;
354static PyTypeObject *Mult_type;
355static PyTypeObject *Div_type;
356static PyTypeObject *Mod_type;
357static PyTypeObject *Pow_type;
358static PyTypeObject *LShift_type;
359static PyTypeObject *RShift_type;
360static PyTypeObject *BitOr_type;
361static PyTypeObject *BitXor_type;
362static PyTypeObject *BitAnd_type;
363static PyTypeObject *FloorDiv_type;
364static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
366*USub_singleton;
367static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000368static PyTypeObject *Invert_type;
369static PyTypeObject *Not_type;
370static PyTypeObject *UAdd_type;
371static PyTypeObject *USub_type;
372static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000373static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
374*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
375*NotIn_singleton;
376static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000377static PyTypeObject *Eq_type;
378static PyTypeObject *NotEq_type;
379static PyTypeObject *Lt_type;
380static PyTypeObject *LtE_type;
381static PyTypeObject *Gt_type;
382static PyTypeObject *GtE_type;
383static PyTypeObject *Is_type;
384static PyTypeObject *IsNot_type;
385static PyTypeObject *In_type;
386static PyTypeObject *NotIn_type;
387static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000388static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200389_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000390static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200391 "target",
392 "iter",
393 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000394};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000395static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000396static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200397 "lineno",
398 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000399};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000400static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000401static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200402_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000403static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200404 "type",
405 "name",
406 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000407};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000408static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000409static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200410_Py_IDENTIFIER(vararg);
411_Py_IDENTIFIER(varargannotation);
412_Py_IDENTIFIER(kwonlyargs);
413_Py_IDENTIFIER(kwarg);
414_Py_IDENTIFIER(kwargannotation);
415_Py_IDENTIFIER(defaults);
416_Py_IDENTIFIER(kw_defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000417static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200418 "args",
419 "vararg",
420 "varargannotation",
421 "kwonlyargs",
422 "kwarg",
423 "kwargannotation",
424 "defaults",
425 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000426};
Neal Norwitzc1505362006-12-28 06:47:50 +0000427static PyTypeObject *arg_type;
428static PyObject* ast2obj_arg(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200429_Py_IDENTIFIER(arg);
430_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000431static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200432 "arg",
433 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000434};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000435static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000436static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000437static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200438 "arg",
439 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000440};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000441static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000442static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200443_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000444static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200445 "name",
446 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000447};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500448static PyTypeObject *withitem_type;
449static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200450_Py_IDENTIFIER(context_expr);
451_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500452static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200453 "context_expr",
454 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500455};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000456
457
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700458typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100459 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700460 PyObject *dict;
461} AST_object;
462
Benjamin Peterson1767e022012-03-14 21:50:29 -0500463static void
464ast_dealloc(AST_object *self)
465{
466 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200467 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500468}
469
Neal Norwitz207c9f32008-03-31 04:42:11 +0000470static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700471ast_traverse(AST_object *self, visitproc visit, void *arg)
472{
473 Py_VISIT(self->dict);
474 return 0;
475}
476
477static void
478ast_clear(AST_object *self)
479{
480 Py_CLEAR(self->dict);
481}
482
483static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000484ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
485{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200486 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000487 Py_ssize_t i, numfields = 0;
488 int res = -1;
489 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200490 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000491 if (!fields)
492 PyErr_Clear();
493 if (fields) {
494 numfields = PySequence_Size(fields);
495 if (numfields == -1)
496 goto cleanup;
497 }
498 res = 0; /* if no error occurs, this stays 0 to the end */
499 if (PyTuple_GET_SIZE(args) > 0) {
500 if (numfields != PyTuple_GET_SIZE(args)) {
501 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000502 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000503 Py_TYPE(self)->tp_name,
504 numfields == 0 ? "" : "either 0 or ",
505 numfields, numfields == 1 ? "" : "s");
506 res = -1;
507 goto cleanup;
508 }
509 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
510 /* cannot be reached when fields is NULL */
511 PyObject *name = PySequence_GetItem(fields, i);
512 if (!name) {
513 res = -1;
514 goto cleanup;
515 }
516 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
517 Py_DECREF(name);
518 if (res < 0)
519 goto cleanup;
520 }
521 }
522 if (kw) {
523 i = 0; /* needed by PyDict_Next */
524 while (PyDict_Next(kw, &i, &key, &value)) {
525 res = PyObject_SetAttr(self, key, value);
526 if (res < 0)
527 goto cleanup;
528 }
529 }
530 cleanup:
531 Py_XDECREF(fields);
532 return res;
533}
534
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000535/* Pickling support */
536static PyObject *
537ast_type_reduce(PyObject *self, PyObject *unused)
538{
539 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200540 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200541 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000542 if (dict == NULL) {
543 if (PyErr_ExceptionMatches(PyExc_AttributeError))
544 PyErr_Clear();
545 else
546 return NULL;
547 }
548 if (dict) {
549 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
550 Py_DECREF(dict);
551 return res;
552 }
553 return Py_BuildValue("O()", Py_TYPE(self));
554}
555
556static PyMethodDef ast_type_methods[] = {
557 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
558 {NULL}
559};
560
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700561static PyGetSetDef ast_type_getsets[] = {
562 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
563 {NULL}
564};
565
Neal Norwitz207c9f32008-03-31 04:42:11 +0000566static PyTypeObject AST_type = {
567 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000568 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700569 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000570 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500571 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000572 0, /* tp_print */
573 0, /* tp_getattr */
574 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000575 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000576 0, /* tp_repr */
577 0, /* tp_as_number */
578 0, /* tp_as_sequence */
579 0, /* tp_as_mapping */
580 0, /* tp_hash */
581 0, /* tp_call */
582 0, /* tp_str */
583 PyObject_GenericGetAttr, /* tp_getattro */
584 PyObject_GenericSetAttr, /* tp_setattro */
585 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700586 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000587 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700588 (traverseproc)ast_traverse, /* tp_traverse */
589 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000590 0, /* tp_richcompare */
591 0, /* tp_weaklistoffset */
592 0, /* tp_iter */
593 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000594 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000595 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700596 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000597 0, /* tp_base */
598 0, /* tp_dict */
599 0, /* tp_descr_get */
600 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700601 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000602 (initproc)ast_type_init, /* tp_init */
603 PyType_GenericAlloc, /* tp_alloc */
604 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700605 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000606};
607
608
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000609static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
610{
611 PyObject *fnames, *result;
612 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000613 fnames = PyTuple_New(num_fields);
614 if (!fnames) return NULL;
615 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000616 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000617 if (!field) {
618 Py_DECREF(fnames);
619 return NULL;
620 }
621 PyTuple_SET_ITEM(fnames, i, field);
622 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000623 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000625 Py_DECREF(fnames);
626 return (PyTypeObject*)result;
627}
628
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000629static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
630{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200632 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000633 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000634 if (!l)
635 return 0;
636 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000637 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!s) {
639 Py_DECREF(l);
640 return 0;
641 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000642 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200644 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000645 Py_DECREF(l);
646 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647}
648
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000649/* Conversion AST -> Python */
650
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
652{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700653 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 PyObject *result = PyList_New(n);
655 PyObject *value;
656 if (!result)
657 return NULL;
658 for (i = 0; i < n; i++) {
659 value = func(asdl_seq_GET(seq, i));
660 if (!value) {
661 Py_DECREF(result);
662 return NULL;
663 }
664 PyList_SET_ITEM(result, i, value);
665 }
666 return result;
667}
668
669static PyObject* ast2obj_object(void *o)
670{
671 if (!o)
672 o = Py_None;
673 Py_INCREF((PyObject*)o);
674 return (PyObject*)o;
675}
676#define ast2obj_identifier ast2obj_object
677#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500678#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000680static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681{
Christian Heimes217cfd12007-12-02 14:31:20 +0000682 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683}
684
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000685/* Conversion Python -> AST */
686
687static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
688{
689 if (obj == Py_None)
690 obj = NULL;
691 if (obj)
692 PyArena_AddPyObject(arena, obj);
693 Py_XINCREF(obj);
694 *out = obj;
695 return 0;
696}
697
Benjamin Peterson180e6352011-07-22 11:09:07 -0500698static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500699{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500700 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
701 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500702 return 1;
703 }
704 return obj2ast_object(obj, out, arena);
705}
706
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500707static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
708{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400709 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500710 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
711 return 1;
712 }
713 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500714}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000715
Benjamin Petersone2498412011-08-09 16:08:39 -0500716static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
717{
718 if (!PyBytes_CheckExact(obj)) {
719 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
720 return 1;
721 }
722 return obj2ast_object(obj, out, arena);
723}
724
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000725static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
726{
727 int i;
728 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100729 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000730 return 1;
731 }
732
733 i = (int)PyLong_AsLong(obj);
734 if (i == -1 && PyErr_Occurred())
735 return 1;
736 *out = i;
737 return 0;
738}
739
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000740static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000741{
742 PyObject *empty_tuple, *d;
743 if (PyType_Ready(&AST_type) < 0)
744 return -1;
745 d = AST_type.tp_dict;
746 empty_tuple = PyTuple_New(0);
747 if (!empty_tuple ||
748 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
749 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
750 Py_XDECREF(empty_tuple);
751 return -1;
752 }
753 Py_DECREF(empty_tuple);
754 return 0;
755}
756
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000757
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758static int init_types(void)
759{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200760 static int initialized;
761 if (initialized) return 1;
762 if (add_ast_fields() < 0) return 0;
763 mod_type = make_type("mod", &AST_type, NULL, 0);
764 if (!mod_type) return 0;
765 if (!add_attributes(mod_type, NULL, 0)) return 0;
766 Module_type = make_type("Module", mod_type, Module_fields, 1);
767 if (!Module_type) return 0;
768 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
769 1);
770 if (!Interactive_type) return 0;
771 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
772 if (!Expression_type) return 0;
773 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
774 if (!Suite_type) return 0;
775 stmt_type = make_type("stmt", &AST_type, NULL, 0);
776 if (!stmt_type) return 0;
777 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
778 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
779 5);
780 if (!FunctionDef_type) return 0;
781 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
782 if (!ClassDef_type) return 0;
783 Return_type = make_type("Return", stmt_type, Return_fields, 1);
784 if (!Return_type) return 0;
785 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
786 if (!Delete_type) return 0;
787 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
788 if (!Assign_type) return 0;
789 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
790 if (!AugAssign_type) return 0;
791 For_type = make_type("For", stmt_type, For_fields, 4);
792 if (!For_type) return 0;
793 While_type = make_type("While", stmt_type, While_fields, 3);
794 if (!While_type) return 0;
795 If_type = make_type("If", stmt_type, If_fields, 3);
796 if (!If_type) return 0;
797 With_type = make_type("With", stmt_type, With_fields, 2);
798 if (!With_type) return 0;
799 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
800 if (!Raise_type) return 0;
801 Try_type = make_type("Try", stmt_type, Try_fields, 4);
802 if (!Try_type) return 0;
803 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
804 if (!Assert_type) return 0;
805 Import_type = make_type("Import", stmt_type, Import_fields, 1);
806 if (!Import_type) return 0;
807 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
808 if (!ImportFrom_type) return 0;
809 Global_type = make_type("Global", stmt_type, Global_fields, 1);
810 if (!Global_type) return 0;
811 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
812 if (!Nonlocal_type) return 0;
813 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
814 if (!Expr_type) return 0;
815 Pass_type = make_type("Pass", stmt_type, NULL, 0);
816 if (!Pass_type) return 0;
817 Break_type = make_type("Break", stmt_type, NULL, 0);
818 if (!Break_type) return 0;
819 Continue_type = make_type("Continue", stmt_type, NULL, 0);
820 if (!Continue_type) return 0;
821 expr_type = make_type("expr", &AST_type, NULL, 0);
822 if (!expr_type) return 0;
823 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
824 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
825 if (!BoolOp_type) return 0;
826 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
827 if (!BinOp_type) return 0;
828 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
829 if (!UnaryOp_type) return 0;
830 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
831 if (!Lambda_type) return 0;
832 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
833 if (!IfExp_type) return 0;
834 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
835 if (!Dict_type) return 0;
836 Set_type = make_type("Set", expr_type, Set_fields, 1);
837 if (!Set_type) return 0;
838 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
839 if (!ListComp_type) return 0;
840 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
841 if (!SetComp_type) return 0;
842 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
843 if (!DictComp_type) return 0;
844 GeneratorExp_type = make_type("GeneratorExp", expr_type,
845 GeneratorExp_fields, 2);
846 if (!GeneratorExp_type) return 0;
847 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
848 if (!Yield_type) return 0;
849 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
850 if (!YieldFrom_type) return 0;
851 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
852 if (!Compare_type) return 0;
853 Call_type = make_type("Call", expr_type, Call_fields, 5);
854 if (!Call_type) return 0;
855 Num_type = make_type("Num", expr_type, Num_fields, 1);
856 if (!Num_type) return 0;
857 Str_type = make_type("Str", expr_type, Str_fields, 1);
858 if (!Str_type) return 0;
859 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
860 if (!Bytes_type) return 0;
861 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
862 if (!Ellipsis_type) return 0;
863 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
864 if (!Attribute_type) return 0;
865 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
866 if (!Subscript_type) return 0;
867 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
868 if (!Starred_type) return 0;
869 Name_type = make_type("Name", expr_type, Name_fields, 2);
870 if (!Name_type) return 0;
871 List_type = make_type("List", expr_type, List_fields, 2);
872 if (!List_type) return 0;
873 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
874 if (!Tuple_type) return 0;
875 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
876 if (!expr_context_type) return 0;
877 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
878 Load_type = make_type("Load", expr_context_type, NULL, 0);
879 if (!Load_type) return 0;
880 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
881 if (!Load_singleton) return 0;
882 Store_type = make_type("Store", expr_context_type, NULL, 0);
883 if (!Store_type) return 0;
884 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
885 if (!Store_singleton) return 0;
886 Del_type = make_type("Del", expr_context_type, NULL, 0);
887 if (!Del_type) return 0;
888 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
889 if (!Del_singleton) return 0;
890 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
891 if (!AugLoad_type) return 0;
892 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
893 if (!AugLoad_singleton) return 0;
894 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
895 if (!AugStore_type) return 0;
896 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
897 if (!AugStore_singleton) return 0;
898 Param_type = make_type("Param", expr_context_type, NULL, 0);
899 if (!Param_type) return 0;
900 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
901 if (!Param_singleton) return 0;
902 slice_type = make_type("slice", &AST_type, NULL, 0);
903 if (!slice_type) return 0;
904 if (!add_attributes(slice_type, NULL, 0)) return 0;
905 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
906 if (!Slice_type) return 0;
907 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
908 if (!ExtSlice_type) return 0;
909 Index_type = make_type("Index", slice_type, Index_fields, 1);
910 if (!Index_type) return 0;
911 boolop_type = make_type("boolop", &AST_type, NULL, 0);
912 if (!boolop_type) return 0;
913 if (!add_attributes(boolop_type, NULL, 0)) return 0;
914 And_type = make_type("And", boolop_type, NULL, 0);
915 if (!And_type) return 0;
916 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
917 if (!And_singleton) return 0;
918 Or_type = make_type("Or", boolop_type, NULL, 0);
919 if (!Or_type) return 0;
920 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
921 if (!Or_singleton) return 0;
922 operator_type = make_type("operator", &AST_type, NULL, 0);
923 if (!operator_type) return 0;
924 if (!add_attributes(operator_type, NULL, 0)) return 0;
925 Add_type = make_type("Add", operator_type, NULL, 0);
926 if (!Add_type) return 0;
927 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
928 if (!Add_singleton) return 0;
929 Sub_type = make_type("Sub", operator_type, NULL, 0);
930 if (!Sub_type) return 0;
931 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
932 if (!Sub_singleton) return 0;
933 Mult_type = make_type("Mult", operator_type, NULL, 0);
934 if (!Mult_type) return 0;
935 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
936 if (!Mult_singleton) return 0;
937 Div_type = make_type("Div", operator_type, NULL, 0);
938 if (!Div_type) return 0;
939 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
940 if (!Div_singleton) return 0;
941 Mod_type = make_type("Mod", operator_type, NULL, 0);
942 if (!Mod_type) return 0;
943 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
944 if (!Mod_singleton) return 0;
945 Pow_type = make_type("Pow", operator_type, NULL, 0);
946 if (!Pow_type) return 0;
947 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
948 if (!Pow_singleton) return 0;
949 LShift_type = make_type("LShift", operator_type, NULL, 0);
950 if (!LShift_type) return 0;
951 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
952 if (!LShift_singleton) return 0;
953 RShift_type = make_type("RShift", operator_type, NULL, 0);
954 if (!RShift_type) return 0;
955 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
956 if (!RShift_singleton) return 0;
957 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
958 if (!BitOr_type) return 0;
959 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
960 if (!BitOr_singleton) return 0;
961 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
962 if (!BitXor_type) return 0;
963 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
964 if (!BitXor_singleton) return 0;
965 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
966 if (!BitAnd_type) return 0;
967 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
968 if (!BitAnd_singleton) return 0;
969 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
970 if (!FloorDiv_type) return 0;
971 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
972 if (!FloorDiv_singleton) return 0;
973 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
974 if (!unaryop_type) return 0;
975 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
976 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
977 if (!Invert_type) return 0;
978 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
979 if (!Invert_singleton) return 0;
980 Not_type = make_type("Not", unaryop_type, NULL, 0);
981 if (!Not_type) return 0;
982 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
983 if (!Not_singleton) return 0;
984 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
985 if (!UAdd_type) return 0;
986 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
987 if (!UAdd_singleton) return 0;
988 USub_type = make_type("USub", unaryop_type, NULL, 0);
989 if (!USub_type) return 0;
990 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
991 if (!USub_singleton) return 0;
992 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
993 if (!cmpop_type) return 0;
994 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
995 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
996 if (!Eq_type) return 0;
997 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
998 if (!Eq_singleton) return 0;
999 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1000 if (!NotEq_type) return 0;
1001 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1002 if (!NotEq_singleton) return 0;
1003 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1004 if (!Lt_type) return 0;
1005 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1006 if (!Lt_singleton) return 0;
1007 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1008 if (!LtE_type) return 0;
1009 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1010 if (!LtE_singleton) return 0;
1011 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1012 if (!Gt_type) return 0;
1013 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1014 if (!Gt_singleton) return 0;
1015 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1016 if (!GtE_type) return 0;
1017 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1018 if (!GtE_singleton) return 0;
1019 Is_type = make_type("Is", cmpop_type, NULL, 0);
1020 if (!Is_type) return 0;
1021 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1022 if (!Is_singleton) return 0;
1023 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1024 if (!IsNot_type) return 0;
1025 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1026 if (!IsNot_singleton) return 0;
1027 In_type = make_type("In", cmpop_type, NULL, 0);
1028 if (!In_type) return 0;
1029 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1030 if (!In_singleton) return 0;
1031 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1032 if (!NotIn_type) return 0;
1033 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1034 if (!NotIn_singleton) return 0;
1035 comprehension_type = make_type("comprehension", &AST_type,
1036 comprehension_fields, 3);
1037 if (!comprehension_type) return 0;
1038 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1039 if (!excepthandler_type) return 0;
1040 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1041 return 0;
1042 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1043 ExceptHandler_fields, 3);
1044 if (!ExceptHandler_type) return 0;
1045 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
1046 if (!arguments_type) return 0;
1047 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1048 if (!arg_type) return 0;
1049 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1050 if (!keyword_type) return 0;
1051 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1052 if (!alias_type) return 0;
1053 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1054 if (!withitem_type) return 0;
1055 initialized = 1;
1056 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001057}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001058
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001059static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1060static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1061static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1062static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1063 arena);
1064static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1065static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1066static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1067static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1068static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1069static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1070 arena);
1071static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1072 arena);
1073static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1074static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1075static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1076static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001077static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001080Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001082 mod_ty p;
1083 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1084 if (!p)
1085 return NULL;
1086 p->kind = Module_kind;
1087 p->v.Module.body = body;
1088 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
1090
1091mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001092Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001094 mod_ty p;
1095 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1096 if (!p)
1097 return NULL;
1098 p->kind = Interactive_kind;
1099 p->v.Interactive.body = body;
1100 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101}
1102
1103mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001104Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001106 mod_ty p;
1107 if (!body) {
1108 PyErr_SetString(PyExc_ValueError,
1109 "field body is required for Expression");
1110 return NULL;
1111 }
1112 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1113 if (!p)
1114 return NULL;
1115 p->kind = Expression_kind;
1116 p->v.Expression.body = body;
1117 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
1119
1120mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001123 mod_ty p;
1124 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1125 if (!p)
1126 return NULL;
1127 p->kind = Suite_kind;
1128 p->v.Suite.body = body;
1129 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
1132stmt_ty
1133FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001134 decorator_list, expr_ty returns, int lineno, int col_offset,
1135 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001137 stmt_ty p;
1138 if (!name) {
1139 PyErr_SetString(PyExc_ValueError,
1140 "field name is required for FunctionDef");
1141 return NULL;
1142 }
1143 if (!args) {
1144 PyErr_SetString(PyExc_ValueError,
1145 "field args is required for FunctionDef");
1146 return NULL;
1147 }
1148 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1149 if (!p)
1150 return NULL;
1151 p->kind = FunctionDef_kind;
1152 p->v.FunctionDef.name = name;
1153 p->v.FunctionDef.args = args;
1154 p->v.FunctionDef.body = body;
1155 p->v.FunctionDef.decorator_list = decorator_list;
1156 p->v.FunctionDef.returns = returns;
1157 p->lineno = lineno;
1158 p->col_offset = col_offset;
1159 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
1162stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001163ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001164 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1165 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001167 stmt_ty p;
1168 if (!name) {
1169 PyErr_SetString(PyExc_ValueError,
1170 "field name is required for ClassDef");
1171 return NULL;
1172 }
1173 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1174 if (!p)
1175 return NULL;
1176 p->kind = ClassDef_kind;
1177 p->v.ClassDef.name = name;
1178 p->v.ClassDef.bases = bases;
1179 p->v.ClassDef.keywords = keywords;
1180 p->v.ClassDef.starargs = starargs;
1181 p->v.ClassDef.kwargs = kwargs;
1182 p->v.ClassDef.body = body;
1183 p->v.ClassDef.decorator_list = decorator_list;
1184 p->lineno = lineno;
1185 p->col_offset = col_offset;
1186 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187}
1188
1189stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001190Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001192 stmt_ty p;
1193 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1194 if (!p)
1195 return NULL;
1196 p->kind = Return_kind;
1197 p->v.Return.value = value;
1198 p->lineno = lineno;
1199 p->col_offset = col_offset;
1200 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201}
1202
1203stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001204Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001206 stmt_ty p;
1207 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1208 if (!p)
1209 return NULL;
1210 p->kind = Delete_kind;
1211 p->v.Delete.targets = targets;
1212 p->lineno = lineno;
1213 p->col_offset = col_offset;
1214 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215}
1216
1217stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1219 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001221 stmt_ty p;
1222 if (!value) {
1223 PyErr_SetString(PyExc_ValueError,
1224 "field value is required for Assign");
1225 return NULL;
1226 }
1227 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1228 if (!p)
1229 return NULL;
1230 p->kind = Assign_kind;
1231 p->v.Assign.targets = targets;
1232 p->v.Assign.value = value;
1233 p->lineno = lineno;
1234 p->col_offset = col_offset;
1235 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236}
1237
1238stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1240 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001242 stmt_ty p;
1243 if (!target) {
1244 PyErr_SetString(PyExc_ValueError,
1245 "field target is required for AugAssign");
1246 return NULL;
1247 }
1248 if (!op) {
1249 PyErr_SetString(PyExc_ValueError,
1250 "field op is required for AugAssign");
1251 return NULL;
1252 }
1253 if (!value) {
1254 PyErr_SetString(PyExc_ValueError,
1255 "field value is required for AugAssign");
1256 return NULL;
1257 }
1258 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1259 if (!p)
1260 return NULL;
1261 p->kind = AugAssign_kind;
1262 p->v.AugAssign.target = target;
1263 p->v.AugAssign.op = op;
1264 p->v.AugAssign.value = value;
1265 p->lineno = lineno;
1266 p->col_offset = col_offset;
1267 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268}
1269
1270stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001272 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001274 stmt_ty p;
1275 if (!target) {
1276 PyErr_SetString(PyExc_ValueError,
1277 "field target is required for For");
1278 return NULL;
1279 }
1280 if (!iter) {
1281 PyErr_SetString(PyExc_ValueError,
1282 "field iter is required for For");
1283 return NULL;
1284 }
1285 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1286 if (!p)
1287 return NULL;
1288 p->kind = For_kind;
1289 p->v.For.target = target;
1290 p->v.For.iter = iter;
1291 p->v.For.body = body;
1292 p->v.For.orelse = orelse;
1293 p->lineno = lineno;
1294 p->col_offset = col_offset;
1295 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296}
1297
1298stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001299While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1300 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001302 stmt_ty p;
1303 if (!test) {
1304 PyErr_SetString(PyExc_ValueError,
1305 "field test is required for While");
1306 return NULL;
1307 }
1308 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1309 if (!p)
1310 return NULL;
1311 p->kind = While_kind;
1312 p->v.While.test = test;
1313 p->v.While.body = body;
1314 p->v.While.orelse = orelse;
1315 p->lineno = lineno;
1316 p->col_offset = col_offset;
1317 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318}
1319
1320stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001321If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1322 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001324 stmt_ty p;
1325 if (!test) {
1326 PyErr_SetString(PyExc_ValueError,
1327 "field test is required for If");
1328 return NULL;
1329 }
1330 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1331 if (!p)
1332 return NULL;
1333 p->kind = If_kind;
1334 p->v.If.test = test;
1335 p->v.If.body = body;
1336 p->v.If.orelse = orelse;
1337 p->lineno = lineno;
1338 p->col_offset = col_offset;
1339 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340}
1341
1342stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001343With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1344 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001345{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001346 stmt_ty p;
1347 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1348 if (!p)
1349 return NULL;
1350 p->kind = With_kind;
1351 p->v.With.items = items;
1352 p->v.With.body = body;
1353 p->lineno = lineno;
1354 p->col_offset = col_offset;
1355 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001356}
1357
1358stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001359Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001361 stmt_ty p;
1362 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1363 if (!p)
1364 return NULL;
1365 p->kind = Raise_kind;
1366 p->v.Raise.exc = exc;
1367 p->v.Raise.cause = cause;
1368 p->lineno = lineno;
1369 p->col_offset = col_offset;
1370 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371}
1372
1373stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001374Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1375 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001377 stmt_ty p;
1378 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1379 if (!p)
1380 return NULL;
1381 p->kind = Try_kind;
1382 p->v.Try.body = body;
1383 p->v.Try.handlers = handlers;
1384 p->v.Try.orelse = orelse;
1385 p->v.Try.finalbody = finalbody;
1386 p->lineno = lineno;
1387 p->col_offset = col_offset;
1388 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389}
1390
1391stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001392Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001394 stmt_ty p;
1395 if (!test) {
1396 PyErr_SetString(PyExc_ValueError,
1397 "field test is required for Assert");
1398 return NULL;
1399 }
1400 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1401 if (!p)
1402 return NULL;
1403 p->kind = Assert_kind;
1404 p->v.Assert.test = test;
1405 p->v.Assert.msg = msg;
1406 p->lineno = lineno;
1407 p->col_offset = col_offset;
1408 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409}
1410
1411stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001412Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001414 stmt_ty p;
1415 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1416 if (!p)
1417 return NULL;
1418 p->kind = Import_kind;
1419 p->v.Import.names = names;
1420 p->lineno = lineno;
1421 p->col_offset = col_offset;
1422 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423}
1424
1425stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1427 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001429 stmt_ty p;
1430 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1431 if (!p)
1432 return NULL;
1433 p->kind = ImportFrom_kind;
1434 p->v.ImportFrom.module = module;
1435 p->v.ImportFrom.names = names;
1436 p->v.ImportFrom.level = level;
1437 p->lineno = lineno;
1438 p->col_offset = col_offset;
1439 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440}
1441
1442stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001445 stmt_ty p;
1446 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1447 if (!p)
1448 return NULL;
1449 p->kind = Global_kind;
1450 p->v.Global.names = names;
1451 p->lineno = lineno;
1452 p->col_offset = col_offset;
1453 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454}
1455
1456stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001457Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1458{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001459 stmt_ty p;
1460 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1461 if (!p)
1462 return NULL;
1463 p->kind = Nonlocal_kind;
1464 p->v.Nonlocal.names = names;
1465 p->lineno = lineno;
1466 p->col_offset = col_offset;
1467 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001468}
1469
1470stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001471Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001473 stmt_ty p;
1474 if (!value) {
1475 PyErr_SetString(PyExc_ValueError,
1476 "field value is required for Expr");
1477 return NULL;
1478 }
1479 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1480 if (!p)
1481 return NULL;
1482 p->kind = Expr_kind;
1483 p->v.Expr.value = value;
1484 p->lineno = lineno;
1485 p->col_offset = col_offset;
1486 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
1489stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001492 stmt_ty p;
1493 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1494 if (!p)
1495 return NULL;
1496 p->kind = Pass_kind;
1497 p->lineno = lineno;
1498 p->col_offset = col_offset;
1499 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001503Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001505 stmt_ty p;
1506 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1507 if (!p)
1508 return NULL;
1509 p->kind = Break_kind;
1510 p->lineno = lineno;
1511 p->col_offset = col_offset;
1512 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
1515stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001516Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001518 stmt_ty p;
1519 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1520 if (!p)
1521 return NULL;
1522 p->kind = Continue_kind;
1523 p->lineno = lineno;
1524 p->col_offset = col_offset;
1525 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526}
1527
1528expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001529BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1530 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001532 expr_ty p;
1533 if (!op) {
1534 PyErr_SetString(PyExc_ValueError,
1535 "field op is required for BoolOp");
1536 return NULL;
1537 }
1538 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1539 if (!p)
1540 return NULL;
1541 p->kind = BoolOp_kind;
1542 p->v.BoolOp.op = op;
1543 p->v.BoolOp.values = values;
1544 p->lineno = lineno;
1545 p->col_offset = col_offset;
1546 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001550BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1551 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001553 expr_ty p;
1554 if (!left) {
1555 PyErr_SetString(PyExc_ValueError,
1556 "field left is required for BinOp");
1557 return NULL;
1558 }
1559 if (!op) {
1560 PyErr_SetString(PyExc_ValueError,
1561 "field op is required for BinOp");
1562 return NULL;
1563 }
1564 if (!right) {
1565 PyErr_SetString(PyExc_ValueError,
1566 "field right is required for BinOp");
1567 return NULL;
1568 }
1569 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1570 if (!p)
1571 return NULL;
1572 p->kind = BinOp_kind;
1573 p->v.BinOp.left = left;
1574 p->v.BinOp.op = op;
1575 p->v.BinOp.right = right;
1576 p->lineno = lineno;
1577 p->col_offset = col_offset;
1578 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579}
1580
1581expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001582UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1583 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001585 expr_ty p;
1586 if (!op) {
1587 PyErr_SetString(PyExc_ValueError,
1588 "field op is required for UnaryOp");
1589 return NULL;
1590 }
1591 if (!operand) {
1592 PyErr_SetString(PyExc_ValueError,
1593 "field operand is required for UnaryOp");
1594 return NULL;
1595 }
1596 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1597 if (!p)
1598 return NULL;
1599 p->kind = UnaryOp_kind;
1600 p->v.UnaryOp.op = op;
1601 p->v.UnaryOp.operand = operand;
1602 p->lineno = lineno;
1603 p->col_offset = col_offset;
1604 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605}
1606
1607expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001608Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1609 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001611 expr_ty p;
1612 if (!args) {
1613 PyErr_SetString(PyExc_ValueError,
1614 "field args is required for Lambda");
1615 return NULL;
1616 }
1617 if (!body) {
1618 PyErr_SetString(PyExc_ValueError,
1619 "field body is required for Lambda");
1620 return NULL;
1621 }
1622 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1623 if (!p)
1624 return NULL;
1625 p->kind = Lambda_kind;
1626 p->v.Lambda.args = args;
1627 p->v.Lambda.body = body;
1628 p->lineno = lineno;
1629 p->col_offset = col_offset;
1630 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631}
1632
1633expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001634IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1635 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001636{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001637 expr_ty p;
1638 if (!test) {
1639 PyErr_SetString(PyExc_ValueError,
1640 "field test is required for IfExp");
1641 return NULL;
1642 }
1643 if (!body) {
1644 PyErr_SetString(PyExc_ValueError,
1645 "field body is required for IfExp");
1646 return NULL;
1647 }
1648 if (!orelse) {
1649 PyErr_SetString(PyExc_ValueError,
1650 "field orelse is required for IfExp");
1651 return NULL;
1652 }
1653 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1654 if (!p)
1655 return NULL;
1656 p->kind = IfExp_kind;
1657 p->v.IfExp.test = test;
1658 p->v.IfExp.body = body;
1659 p->v.IfExp.orelse = orelse;
1660 p->lineno = lineno;
1661 p->col_offset = col_offset;
1662 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001663}
1664
1665expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1667 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001669 expr_ty p;
1670 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1671 if (!p)
1672 return NULL;
1673 p->kind = Dict_kind;
1674 p->v.Dict.keys = keys;
1675 p->v.Dict.values = values;
1676 p->lineno = lineno;
1677 p->col_offset = col_offset;
1678 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679}
1680
1681expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001682Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1683{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001684 expr_ty p;
1685 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1686 if (!p)
1687 return NULL;
1688 p->kind = Set_kind;
1689 p->v.Set.elts = elts;
1690 p->lineno = lineno;
1691 p->col_offset = col_offset;
1692 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001693}
1694
1695expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1697 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001699 expr_ty p;
1700 if (!elt) {
1701 PyErr_SetString(PyExc_ValueError,
1702 "field elt is required for ListComp");
1703 return NULL;
1704 }
1705 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1706 if (!p)
1707 return NULL;
1708 p->kind = ListComp_kind;
1709 p->v.ListComp.elt = elt;
1710 p->v.ListComp.generators = generators;
1711 p->lineno = lineno;
1712 p->col_offset = col_offset;
1713 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714}
1715
1716expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001717SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1718 *arena)
1719{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001720 expr_ty p;
1721 if (!elt) {
1722 PyErr_SetString(PyExc_ValueError,
1723 "field elt is required for SetComp");
1724 return NULL;
1725 }
1726 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1727 if (!p)
1728 return NULL;
1729 p->kind = SetComp_kind;
1730 p->v.SetComp.elt = elt;
1731 p->v.SetComp.generators = generators;
1732 p->lineno = lineno;
1733 p->col_offset = col_offset;
1734 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001735}
1736
1737expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001738DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1739 col_offset, PyArena *arena)
1740{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001741 expr_ty p;
1742 if (!key) {
1743 PyErr_SetString(PyExc_ValueError,
1744 "field key is required for DictComp");
1745 return NULL;
1746 }
1747 if (!value) {
1748 PyErr_SetString(PyExc_ValueError,
1749 "field value is required for DictComp");
1750 return NULL;
1751 }
1752 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1753 if (!p)
1754 return NULL;
1755 p->kind = DictComp_kind;
1756 p->v.DictComp.key = key;
1757 p->v.DictComp.value = value;
1758 p->v.DictComp.generators = generators;
1759 p->lineno = lineno;
1760 p->col_offset = col_offset;
1761 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762}
1763
1764expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001765GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1766 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001768 expr_ty p;
1769 if (!elt) {
1770 PyErr_SetString(PyExc_ValueError,
1771 "field elt is required for GeneratorExp");
1772 return NULL;
1773 }
1774 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1775 if (!p)
1776 return NULL;
1777 p->kind = GeneratorExp_kind;
1778 p->v.GeneratorExp.elt = elt;
1779 p->v.GeneratorExp.generators = generators;
1780 p->lineno = lineno;
1781 p->col_offset = col_offset;
1782 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783}
1784
1785expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001786Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001788 expr_ty p;
1789 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1790 if (!p)
1791 return NULL;
1792 p->kind = Yield_kind;
1793 p->v.Yield.value = value;
1794 p->lineno = lineno;
1795 p->col_offset = col_offset;
1796 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797}
1798
1799expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001800YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1801{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001802 expr_ty p;
1803 if (!value) {
1804 PyErr_SetString(PyExc_ValueError,
1805 "field value is required for YieldFrom");
1806 return NULL;
1807 }
1808 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1809 if (!p)
1810 return NULL;
1811 p->kind = YieldFrom_kind;
1812 p->v.YieldFrom.value = value;
1813 p->lineno = lineno;
1814 p->col_offset = col_offset;
1815 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001816}
1817
1818expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001819Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1820 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001822 expr_ty p;
1823 if (!left) {
1824 PyErr_SetString(PyExc_ValueError,
1825 "field left is required for Compare");
1826 return NULL;
1827 }
1828 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1829 if (!p)
1830 return NULL;
1831 p->kind = Compare_kind;
1832 p->v.Compare.left = left;
1833 p->v.Compare.ops = ops;
1834 p->v.Compare.comparators = comparators;
1835 p->lineno = lineno;
1836 p->col_offset = col_offset;
1837 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838}
1839
1840expr_ty
1841Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001842 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001844 expr_ty p;
1845 if (!func) {
1846 PyErr_SetString(PyExc_ValueError,
1847 "field func is required for Call");
1848 return NULL;
1849 }
1850 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1851 if (!p)
1852 return NULL;
1853 p->kind = Call_kind;
1854 p->v.Call.func = func;
1855 p->v.Call.args = args;
1856 p->v.Call.keywords = keywords;
1857 p->v.Call.starargs = starargs;
1858 p->v.Call.kwargs = kwargs;
1859 p->lineno = lineno;
1860 p->col_offset = col_offset;
1861 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862}
1863
1864expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001865Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001867 expr_ty p;
1868 if (!n) {
1869 PyErr_SetString(PyExc_ValueError,
1870 "field n is required for Num");
1871 return NULL;
1872 }
1873 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1874 if (!p)
1875 return NULL;
1876 p->kind = Num_kind;
1877 p->v.Num.n = n;
1878 p->lineno = lineno;
1879 p->col_offset = col_offset;
1880 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881}
1882
1883expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001884Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001886 expr_ty p;
1887 if (!s) {
1888 PyErr_SetString(PyExc_ValueError,
1889 "field s is required for Str");
1890 return NULL;
1891 }
1892 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1893 if (!p)
1894 return NULL;
1895 p->kind = Str_kind;
1896 p->v.Str.s = s;
1897 p->lineno = lineno;
1898 p->col_offset = col_offset;
1899 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
1902expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001903Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001905 expr_ty p;
1906 if (!s) {
1907 PyErr_SetString(PyExc_ValueError,
1908 "field s is required for Bytes");
1909 return NULL;
1910 }
1911 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1912 if (!p)
1913 return NULL;
1914 p->kind = Bytes_kind;
1915 p->v.Bytes.s = s;
1916 p->lineno = lineno;
1917 p->col_offset = col_offset;
1918 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001919}
1920
1921expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001922Ellipsis(int lineno, int col_offset, PyArena *arena)
1923{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001924 expr_ty p;
1925 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1926 if (!p)
1927 return NULL;
1928 p->kind = Ellipsis_kind;
1929 p->lineno = lineno;
1930 p->col_offset = col_offset;
1931 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001932}
1933
1934expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001935Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1936 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001938 expr_ty p;
1939 if (!value) {
1940 PyErr_SetString(PyExc_ValueError,
1941 "field value is required for Attribute");
1942 return NULL;
1943 }
1944 if (!attr) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field attr is required for Attribute");
1947 return NULL;
1948 }
1949 if (!ctx) {
1950 PyErr_SetString(PyExc_ValueError,
1951 "field ctx is required for Attribute");
1952 return NULL;
1953 }
1954 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1955 if (!p)
1956 return NULL;
1957 p->kind = Attribute_kind;
1958 p->v.Attribute.value = value;
1959 p->v.Attribute.attr = attr;
1960 p->v.Attribute.ctx = ctx;
1961 p->lineno = lineno;
1962 p->col_offset = col_offset;
1963 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964}
1965
1966expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001967Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1968 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001970 expr_ty p;
1971 if (!value) {
1972 PyErr_SetString(PyExc_ValueError,
1973 "field value is required for Subscript");
1974 return NULL;
1975 }
1976 if (!slice) {
1977 PyErr_SetString(PyExc_ValueError,
1978 "field slice is required for Subscript");
1979 return NULL;
1980 }
1981 if (!ctx) {
1982 PyErr_SetString(PyExc_ValueError,
1983 "field ctx is required for Subscript");
1984 return NULL;
1985 }
1986 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1987 if (!p)
1988 return NULL;
1989 p->kind = Subscript_kind;
1990 p->v.Subscript.value = value;
1991 p->v.Subscript.slice = slice;
1992 p->v.Subscript.ctx = ctx;
1993 p->lineno = lineno;
1994 p->col_offset = col_offset;
1995 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996}
1997
1998expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001999Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2000 *arena)
2001{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002002 expr_ty p;
2003 if (!value) {
2004 PyErr_SetString(PyExc_ValueError,
2005 "field value is required for Starred");
2006 return NULL;
2007 }
2008 if (!ctx) {
2009 PyErr_SetString(PyExc_ValueError,
2010 "field ctx is required for Starred");
2011 return NULL;
2012 }
2013 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2014 if (!p)
2015 return NULL;
2016 p->kind = Starred_kind;
2017 p->v.Starred.value = value;
2018 p->v.Starred.ctx = ctx;
2019 p->lineno = lineno;
2020 p->col_offset = col_offset;
2021 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002022}
2023
2024expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002025Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2026 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002028 expr_ty p;
2029 if (!id) {
2030 PyErr_SetString(PyExc_ValueError,
2031 "field id is required for Name");
2032 return NULL;
2033 }
2034 if (!ctx) {
2035 PyErr_SetString(PyExc_ValueError,
2036 "field ctx is required for Name");
2037 return NULL;
2038 }
2039 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2040 if (!p)
2041 return NULL;
2042 p->kind = Name_kind;
2043 p->v.Name.id = id;
2044 p->v.Name.ctx = ctx;
2045 p->lineno = lineno;
2046 p->col_offset = col_offset;
2047 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048}
2049
2050expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002051List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2052 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002054 expr_ty p;
2055 if (!ctx) {
2056 PyErr_SetString(PyExc_ValueError,
2057 "field ctx is required for List");
2058 return NULL;
2059 }
2060 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2061 if (!p)
2062 return NULL;
2063 p->kind = List_kind;
2064 p->v.List.elts = elts;
2065 p->v.List.ctx = ctx;
2066 p->lineno = lineno;
2067 p->col_offset = col_offset;
2068 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
2070
2071expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002072Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2073 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002075 expr_ty p;
2076 if (!ctx) {
2077 PyErr_SetString(PyExc_ValueError,
2078 "field ctx is required for Tuple");
2079 return NULL;
2080 }
2081 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2082 if (!p)
2083 return NULL;
2084 p->kind = Tuple_kind;
2085 p->v.Tuple.elts = elts;
2086 p->v.Tuple.ctx = ctx;
2087 p->lineno = lineno;
2088 p->col_offset = col_offset;
2089 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090}
2091
2092slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002095 slice_ty p;
2096 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2097 if (!p)
2098 return NULL;
2099 p->kind = Slice_kind;
2100 p->v.Slice.lower = lower;
2101 p->v.Slice.upper = upper;
2102 p->v.Slice.step = step;
2103 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104}
2105
2106slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002109 slice_ty p;
2110 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2111 if (!p)
2112 return NULL;
2113 p->kind = ExtSlice_kind;
2114 p->v.ExtSlice.dims = dims;
2115 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116}
2117
2118slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002119Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002121 slice_ty p;
2122 if (!value) {
2123 PyErr_SetString(PyExc_ValueError,
2124 "field value is required for Index");
2125 return NULL;
2126 }
2127 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2128 if (!p)
2129 return NULL;
2130 p->kind = Index_kind;
2131 p->v.Index.value = value;
2132 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133}
2134
2135comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002136comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002138 comprehension_ty p;
2139 if (!target) {
2140 PyErr_SetString(PyExc_ValueError,
2141 "field target is required for comprehension");
2142 return NULL;
2143 }
2144 if (!iter) {
2145 PyErr_SetString(PyExc_ValueError,
2146 "field iter is required for comprehension");
2147 return NULL;
2148 }
2149 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2150 if (!p)
2151 return NULL;
2152 p->target = target;
2153 p->iter = iter;
2154 p->ifs = ifs;
2155 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156}
2157
2158excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002159ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002160 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002162 excepthandler_ty p;
2163 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2164 if (!p)
2165 return NULL;
2166 p->kind = ExceptHandler_kind;
2167 p->v.ExceptHandler.type = type;
2168 p->v.ExceptHandler.name = name;
2169 p->v.ExceptHandler.body = body;
2170 p->lineno = lineno;
2171 p->col_offset = col_offset;
2172 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173}
2174
2175arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002176arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2177 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2178 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002180 arguments_ty p;
2181 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2182 if (!p)
2183 return NULL;
2184 p->args = args;
2185 p->vararg = vararg;
2186 p->varargannotation = varargannotation;
2187 p->kwonlyargs = kwonlyargs;
2188 p->kwarg = kwarg;
2189 p->kwargannotation = kwargannotation;
2190 p->defaults = defaults;
2191 p->kw_defaults = kw_defaults;
2192 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193}
2194
Neal Norwitzc1505362006-12-28 06:47:50 +00002195arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002196arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002197{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002198 arg_ty p;
2199 if (!arg) {
2200 PyErr_SetString(PyExc_ValueError,
2201 "field arg is required for arg");
2202 return NULL;
2203 }
2204 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2205 if (!p)
2206 return NULL;
2207 p->arg = arg;
2208 p->annotation = annotation;
2209 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002210}
2211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002213keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002215 keyword_ty p;
2216 if (!arg) {
2217 PyErr_SetString(PyExc_ValueError,
2218 "field arg is required for keyword");
2219 return NULL;
2220 }
2221 if (!value) {
2222 PyErr_SetString(PyExc_ValueError,
2223 "field value is required for keyword");
2224 return NULL;
2225 }
2226 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2227 if (!p)
2228 return NULL;
2229 p->arg = arg;
2230 p->value = value;
2231 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232}
2233
2234alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002235alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002237 alias_ty p;
2238 if (!name) {
2239 PyErr_SetString(PyExc_ValueError,
2240 "field name is required for alias");
2241 return NULL;
2242 }
2243 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2244 if (!p)
2245 return NULL;
2246 p->name = name;
2247 p->asname = asname;
2248 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249}
2250
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002251withitem_ty
2252withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2253{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002254 withitem_ty p;
2255 if (!context_expr) {
2256 PyErr_SetString(PyExc_ValueError,
2257 "field context_expr is required for withitem");
2258 return NULL;
2259 }
2260 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2261 if (!p)
2262 return NULL;
2263 p->context_expr = context_expr;
2264 p->optional_vars = optional_vars;
2265 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002266}
2267
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002268
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002269PyObject*
2270ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002271{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002272 mod_ty o = (mod_ty)_o;
2273 PyObject *result = NULL, *value = NULL;
2274 if (!o) {
2275 Py_INCREF(Py_None);
2276 return Py_None;
2277 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002278
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002279 switch (o->kind) {
2280 case Module_kind:
2281 result = PyType_GenericNew(Module_type, NULL, NULL);
2282 if (!result) goto failed;
2283 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2284 if (!value) goto failed;
2285 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2286 goto failed;
2287 Py_DECREF(value);
2288 break;
2289 case Interactive_kind:
2290 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2291 if (!result) goto failed;
2292 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2293 if (!value) goto failed;
2294 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
2297 break;
2298 case Expression_kind:
2299 result = PyType_GenericNew(Expression_type, NULL, NULL);
2300 if (!result) goto failed;
2301 value = ast2obj_expr(o->v.Expression.body);
2302 if (!value) goto failed;
2303 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2304 goto failed;
2305 Py_DECREF(value);
2306 break;
2307 case Suite_kind:
2308 result = PyType_GenericNew(Suite_type, NULL, NULL);
2309 if (!result) goto failed;
2310 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2311 if (!value) goto failed;
2312 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2313 goto failed;
2314 Py_DECREF(value);
2315 break;
2316 }
2317 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002318failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002319 Py_XDECREF(value);
2320 Py_XDECREF(result);
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322}
2323
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002324PyObject*
2325ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002327 stmt_ty o = (stmt_ty)_o;
2328 PyObject *result = NULL, *value = NULL;
2329 if (!o) {
2330 Py_INCREF(Py_None);
2331 return Py_None;
2332 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002333
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002334 switch (o->kind) {
2335 case FunctionDef_kind:
2336 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2337 if (!result) goto failed;
2338 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002339 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002340 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2341 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002342 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002343 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002344 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002345 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2346 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002347 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002348 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2349 if (!value) goto failed;
2350 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2351 goto failed;
2352 Py_DECREF(value);
2353 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2354 if (!value) goto failed;
2355 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2356 goto failed;
2357 Py_DECREF(value);
2358 value = ast2obj_expr(o->v.FunctionDef.returns);
2359 if (!value) goto failed;
2360 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 break;
2364 case ClassDef_kind:
2365 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2366 if (!result) goto failed;
2367 value = ast2obj_identifier(o->v.ClassDef.name);
2368 if (!value) goto failed;
2369 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2370 goto failed;
2371 Py_DECREF(value);
2372 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2373 if (!value) goto failed;
2374 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
2377 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2378 if (!value) goto failed;
2379 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 value = ast2obj_expr(o->v.ClassDef.starargs);
2383 if (!value) goto failed;
2384 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2385 goto failed;
2386 Py_DECREF(value);
2387 value = ast2obj_expr(o->v.ClassDef.kwargs);
2388 if (!value) goto failed;
2389 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2390 goto failed;
2391 Py_DECREF(value);
2392 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2393 if (!value) goto failed;
2394 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2395 goto failed;
2396 Py_DECREF(value);
2397 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2398 if (!value) goto failed;
2399 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2400 goto failed;
2401 Py_DECREF(value);
2402 break;
2403 case Return_kind:
2404 result = PyType_GenericNew(Return_type, NULL, NULL);
2405 if (!result) goto failed;
2406 value = ast2obj_expr(o->v.Return.value);
2407 if (!value) goto failed;
2408 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
2411 break;
2412 case Delete_kind:
2413 result = PyType_GenericNew(Delete_type, NULL, NULL);
2414 if (!result) goto failed;
2415 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2416 if (!value) goto failed;
2417 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2418 goto failed;
2419 Py_DECREF(value);
2420 break;
2421 case Assign_kind:
2422 result = PyType_GenericNew(Assign_type, NULL, NULL);
2423 if (!result) goto failed;
2424 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2425 if (!value) goto failed;
2426 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
2429 value = ast2obj_expr(o->v.Assign.value);
2430 if (!value) goto failed;
2431 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2432 goto failed;
2433 Py_DECREF(value);
2434 break;
2435 case AugAssign_kind:
2436 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2437 if (!result) goto failed;
2438 value = ast2obj_expr(o->v.AugAssign.target);
2439 if (!value) goto failed;
2440 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2441 goto failed;
2442 Py_DECREF(value);
2443 value = ast2obj_operator(o->v.AugAssign.op);
2444 if (!value) goto failed;
2445 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2446 goto failed;
2447 Py_DECREF(value);
2448 value = ast2obj_expr(o->v.AugAssign.value);
2449 if (!value) goto failed;
2450 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2451 goto failed;
2452 Py_DECREF(value);
2453 break;
2454 case For_kind:
2455 result = PyType_GenericNew(For_type, NULL, NULL);
2456 if (!result) goto failed;
2457 value = ast2obj_expr(o->v.For.target);
2458 if (!value) goto failed;
2459 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2460 goto failed;
2461 Py_DECREF(value);
2462 value = ast2obj_expr(o->v.For.iter);
2463 if (!value) goto failed;
2464 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
2467 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2468 if (!value) goto failed;
2469 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2470 goto failed;
2471 Py_DECREF(value);
2472 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2473 if (!value) goto failed;
2474 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2475 goto failed;
2476 Py_DECREF(value);
2477 break;
2478 case While_kind:
2479 result = PyType_GenericNew(While_type, NULL, NULL);
2480 if (!result) goto failed;
2481 value = ast2obj_expr(o->v.While.test);
2482 if (!value) goto failed;
2483 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
2486 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2487 if (!value) goto failed;
2488 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2489 goto failed;
2490 Py_DECREF(value);
2491 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2492 if (!value) goto failed;
2493 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2494 goto failed;
2495 Py_DECREF(value);
2496 break;
2497 case If_kind:
2498 result = PyType_GenericNew(If_type, NULL, NULL);
2499 if (!result) goto failed;
2500 value = ast2obj_expr(o->v.If.test);
2501 if (!value) goto failed;
2502 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2503 goto failed;
2504 Py_DECREF(value);
2505 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2506 if (!value) goto failed;
2507 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2508 goto failed;
2509 Py_DECREF(value);
2510 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2511 if (!value) goto failed;
2512 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2513 goto failed;
2514 Py_DECREF(value);
2515 break;
2516 case With_kind:
2517 result = PyType_GenericNew(With_type, NULL, NULL);
2518 if (!result) goto failed;
2519 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2520 if (!value) goto failed;
2521 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2522 goto failed;
2523 Py_DECREF(value);
2524 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2525 if (!value) goto failed;
2526 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2527 goto failed;
2528 Py_DECREF(value);
2529 break;
2530 case Raise_kind:
2531 result = PyType_GenericNew(Raise_type, NULL, NULL);
2532 if (!result) goto failed;
2533 value = ast2obj_expr(o->v.Raise.exc);
2534 if (!value) goto failed;
2535 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2536 goto failed;
2537 Py_DECREF(value);
2538 value = ast2obj_expr(o->v.Raise.cause);
2539 if (!value) goto failed;
2540 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2541 goto failed;
2542 Py_DECREF(value);
2543 break;
2544 case Try_kind:
2545 result = PyType_GenericNew(Try_type, NULL, NULL);
2546 if (!result) goto failed;
2547 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2548 if (!value) goto failed;
2549 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2550 goto failed;
2551 Py_DECREF(value);
2552 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2553 if (!value) goto failed;
2554 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2555 goto failed;
2556 Py_DECREF(value);
2557 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2558 if (!value) goto failed;
2559 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2560 goto failed;
2561 Py_DECREF(value);
2562 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2563 if (!value) goto failed;
2564 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 break;
2568 case Assert_kind:
2569 result = PyType_GenericNew(Assert_type, NULL, NULL);
2570 if (!result) goto failed;
2571 value = ast2obj_expr(o->v.Assert.test);
2572 if (!value) goto failed;
2573 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2574 goto failed;
2575 Py_DECREF(value);
2576 value = ast2obj_expr(o->v.Assert.msg);
2577 if (!value) goto failed;
2578 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 break;
2582 case Import_kind:
2583 result = PyType_GenericNew(Import_type, NULL, NULL);
2584 if (!result) goto failed;
2585 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2586 if (!value) goto failed;
2587 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2588 goto failed;
2589 Py_DECREF(value);
2590 break;
2591 case ImportFrom_kind:
2592 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2593 if (!result) goto failed;
2594 value = ast2obj_identifier(o->v.ImportFrom.module);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2600 if (!value) goto failed;
2601 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
2604 value = ast2obj_int(o->v.ImportFrom.level);
2605 if (!value) goto failed;
2606 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2607 goto failed;
2608 Py_DECREF(value);
2609 break;
2610 case Global_kind:
2611 result = PyType_GenericNew(Global_type, NULL, NULL);
2612 if (!result) goto failed;
2613 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2614 if (!value) goto failed;
2615 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
2618 break;
2619 case Nonlocal_kind:
2620 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2621 if (!result) goto failed;
2622 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2623 if (!value) goto failed;
2624 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 break;
2628 case Expr_kind:
2629 result = PyType_GenericNew(Expr_type, NULL, NULL);
2630 if (!result) goto failed;
2631 value = ast2obj_expr(o->v.Expr.value);
2632 if (!value) goto failed;
2633 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
2636 break;
2637 case Pass_kind:
2638 result = PyType_GenericNew(Pass_type, NULL, NULL);
2639 if (!result) goto failed;
2640 break;
2641 case Break_kind:
2642 result = PyType_GenericNew(Break_type, NULL, NULL);
2643 if (!result) goto failed;
2644 break;
2645 case Continue_kind:
2646 result = PyType_GenericNew(Continue_type, NULL, NULL);
2647 if (!result) goto failed;
2648 break;
2649 }
2650 value = ast2obj_int(o->lineno);
2651 if (!value) goto failed;
2652 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2653 goto failed;
2654 Py_DECREF(value);
2655 value = ast2obj_int(o->col_offset);
2656 if (!value) goto failed;
2657 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2658 goto failed;
2659 Py_DECREF(value);
2660 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002661failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002662 Py_XDECREF(value);
2663 Py_XDECREF(result);
2664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665}
2666
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002667PyObject*
2668ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002670 expr_ty o = (expr_ty)_o;
2671 PyObject *result = NULL, *value = NULL;
2672 if (!o) {
2673 Py_INCREF(Py_None);
2674 return Py_None;
2675 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002676
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002677 switch (o->kind) {
2678 case BoolOp_kind:
2679 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2680 if (!result) goto failed;
2681 value = ast2obj_boolop(o->v.BoolOp.op);
2682 if (!value) goto failed;
2683 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2684 goto failed;
2685 Py_DECREF(value);
2686 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2687 if (!value) goto failed;
2688 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2689 goto failed;
2690 Py_DECREF(value);
2691 break;
2692 case BinOp_kind:
2693 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2694 if (!result) goto failed;
2695 value = ast2obj_expr(o->v.BinOp.left);
2696 if (!value) goto failed;
2697 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 value = ast2obj_operator(o->v.BinOp.op);
2701 if (!value) goto failed;
2702 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2703 goto failed;
2704 Py_DECREF(value);
2705 value = ast2obj_expr(o->v.BinOp.right);
2706 if (!value) goto failed;
2707 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2708 goto failed;
2709 Py_DECREF(value);
2710 break;
2711 case UnaryOp_kind:
2712 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2713 if (!result) goto failed;
2714 value = ast2obj_unaryop(o->v.UnaryOp.op);
2715 if (!value) goto failed;
2716 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
2719 value = ast2obj_expr(o->v.UnaryOp.operand);
2720 if (!value) goto failed;
2721 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 break;
2725 case Lambda_kind:
2726 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2727 if (!result) goto failed;
2728 value = ast2obj_arguments(o->v.Lambda.args);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 value = ast2obj_expr(o->v.Lambda.body);
2734 if (!value) goto failed;
2735 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2736 goto failed;
2737 Py_DECREF(value);
2738 break;
2739 case IfExp_kind:
2740 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2741 if (!result) goto failed;
2742 value = ast2obj_expr(o->v.IfExp.test);
2743 if (!value) goto failed;
2744 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2745 goto failed;
2746 Py_DECREF(value);
2747 value = ast2obj_expr(o->v.IfExp.body);
2748 if (!value) goto failed;
2749 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2750 goto failed;
2751 Py_DECREF(value);
2752 value = ast2obj_expr(o->v.IfExp.orelse);
2753 if (!value) goto failed;
2754 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2755 goto failed;
2756 Py_DECREF(value);
2757 break;
2758 case Dict_kind:
2759 result = PyType_GenericNew(Dict_type, NULL, NULL);
2760 if (!result) goto failed;
2761 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2762 if (!value) goto failed;
2763 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2764 goto failed;
2765 Py_DECREF(value);
2766 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2767 if (!value) goto failed;
2768 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2769 goto failed;
2770 Py_DECREF(value);
2771 break;
2772 case Set_kind:
2773 result = PyType_GenericNew(Set_type, NULL, NULL);
2774 if (!result) goto failed;
2775 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2776 if (!value) goto failed;
2777 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2778 goto failed;
2779 Py_DECREF(value);
2780 break;
2781 case ListComp_kind:
2782 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2783 if (!result) goto failed;
2784 value = ast2obj_expr(o->v.ListComp.elt);
2785 if (!value) goto failed;
2786 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
2789 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2790 if (!value) goto failed;
2791 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2792 goto failed;
2793 Py_DECREF(value);
2794 break;
2795 case SetComp_kind:
2796 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2797 if (!result) goto failed;
2798 value = ast2obj_expr(o->v.SetComp.elt);
2799 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2804 if (!value) goto failed;
2805 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 break;
2809 case DictComp_kind:
2810 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2811 if (!result) goto failed;
2812 value = ast2obj_expr(o->v.DictComp.key);
2813 if (!value) goto failed;
2814 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2815 goto failed;
2816 Py_DECREF(value);
2817 value = ast2obj_expr(o->v.DictComp.value);
2818 if (!value) goto failed;
2819 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
2822 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2823 if (!value) goto failed;
2824 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2825 goto failed;
2826 Py_DECREF(value);
2827 break;
2828 case GeneratorExp_kind:
2829 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2830 if (!result) goto failed;
2831 value = ast2obj_expr(o->v.GeneratorExp.elt);
2832 if (!value) goto failed;
2833 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2834 goto failed;
2835 Py_DECREF(value);
2836 value = ast2obj_list(o->v.GeneratorExp.generators,
2837 ast2obj_comprehension);
2838 if (!value) goto failed;
2839 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 break;
2843 case Yield_kind:
2844 result = PyType_GenericNew(Yield_type, NULL, NULL);
2845 if (!result) goto failed;
2846 value = ast2obj_expr(o->v.Yield.value);
2847 if (!value) goto failed;
2848 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2849 goto failed;
2850 Py_DECREF(value);
2851 break;
2852 case YieldFrom_kind:
2853 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2854 if (!result) goto failed;
2855 value = ast2obj_expr(o->v.YieldFrom.value);
2856 if (!value) goto failed;
2857 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2858 goto failed;
2859 Py_DECREF(value);
2860 break;
2861 case Compare_kind:
2862 result = PyType_GenericNew(Compare_type, NULL, NULL);
2863 if (!result) goto failed;
2864 value = ast2obj_expr(o->v.Compare.left);
2865 if (!value) goto failed;
2866 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2867 goto failed;
2868 Py_DECREF(value);
2869 {
2870 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2871 value = PyList_New(n);
2872 if (!value) goto failed;
2873 for(i = 0; i < n; i++)
2874 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002876 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002877 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2878 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002879 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002880 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002881 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002882 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
2883 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002884 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002885 break;
2886 case Call_kind:
2887 result = PyType_GenericNew(Call_type, NULL, NULL);
2888 if (!result) goto failed;
2889 value = ast2obj_expr(o->v.Call.func);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2895 if (!value) goto failed;
2896 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
2899 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2900 if (!value) goto failed;
2901 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2902 goto failed;
2903 Py_DECREF(value);
2904 value = ast2obj_expr(o->v.Call.starargs);
2905 if (!value) goto failed;
2906 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2907 goto failed;
2908 Py_DECREF(value);
2909 value = ast2obj_expr(o->v.Call.kwargs);
2910 if (!value) goto failed;
2911 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2912 goto failed;
2913 Py_DECREF(value);
2914 break;
2915 case Num_kind:
2916 result = PyType_GenericNew(Num_type, NULL, NULL);
2917 if (!result) goto failed;
2918 value = ast2obj_object(o->v.Num.n);
2919 if (!value) goto failed;
2920 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2921 goto failed;
2922 Py_DECREF(value);
2923 break;
2924 case Str_kind:
2925 result = PyType_GenericNew(Str_type, NULL, NULL);
2926 if (!result) goto failed;
2927 value = ast2obj_string(o->v.Str.s);
2928 if (!value) goto failed;
2929 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2930 goto failed;
2931 Py_DECREF(value);
2932 break;
2933 case Bytes_kind:
2934 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2935 if (!result) goto failed;
2936 value = ast2obj_bytes(o->v.Bytes.s);
2937 if (!value) goto failed;
2938 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2939 goto failed;
2940 Py_DECREF(value);
2941 break;
2942 case Ellipsis_kind:
2943 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2944 if (!result) goto failed;
2945 break;
2946 case Attribute_kind:
2947 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2948 if (!result) goto failed;
2949 value = ast2obj_expr(o->v.Attribute.value);
2950 if (!value) goto failed;
2951 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2952 goto failed;
2953 Py_DECREF(value);
2954 value = ast2obj_identifier(o->v.Attribute.attr);
2955 if (!value) goto failed;
2956 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
2957 goto failed;
2958 Py_DECREF(value);
2959 value = ast2obj_expr_context(o->v.Attribute.ctx);
2960 if (!value) goto failed;
2961 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2962 goto failed;
2963 Py_DECREF(value);
2964 break;
2965 case Subscript_kind:
2966 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2967 if (!result) goto failed;
2968 value = ast2obj_expr(o->v.Subscript.value);
2969 if (!value) goto failed;
2970 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 value = ast2obj_slice(o->v.Subscript.slice);
2974 if (!value) goto failed;
2975 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 value = ast2obj_expr_context(o->v.Subscript.ctx);
2979 if (!value) goto failed;
2980 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
2983 break;
2984 case Starred_kind:
2985 result = PyType_GenericNew(Starred_type, NULL, NULL);
2986 if (!result) goto failed;
2987 value = ast2obj_expr(o->v.Starred.value);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_expr_context(o->v.Starred.ctx);
2993 if (!value) goto failed;
2994 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
2997 break;
2998 case Name_kind:
2999 result = PyType_GenericNew(Name_type, NULL, NULL);
3000 if (!result) goto failed;
3001 value = ast2obj_identifier(o->v.Name.id);
3002 if (!value) goto failed;
3003 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
3006 value = ast2obj_expr_context(o->v.Name.ctx);
3007 if (!value) goto failed;
3008 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
3011 break;
3012 case List_kind:
3013 result = PyType_GenericNew(List_type, NULL, NULL);
3014 if (!result) goto failed;
3015 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3016 if (!value) goto failed;
3017 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
3020 value = ast2obj_expr_context(o->v.List.ctx);
3021 if (!value) goto failed;
3022 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3023 goto failed;
3024 Py_DECREF(value);
3025 break;
3026 case Tuple_kind:
3027 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3028 if (!result) goto failed;
3029 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3030 if (!value) goto failed;
3031 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3032 goto failed;
3033 Py_DECREF(value);
3034 value = ast2obj_expr_context(o->v.Tuple.ctx);
3035 if (!value) goto failed;
3036 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3037 goto failed;
3038 Py_DECREF(value);
3039 break;
3040 }
3041 value = ast2obj_int(o->lineno);
3042 if (!value) goto failed;
3043 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3044 goto failed;
3045 Py_DECREF(value);
3046 value = ast2obj_int(o->col_offset);
3047 if (!value) goto failed;
3048 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3049 goto failed;
3050 Py_DECREF(value);
3051 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003052failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003053 Py_XDECREF(value);
3054 Py_XDECREF(result);
3055 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056}
3057
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003058PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003060 switch(o) {
3061 case Load:
3062 Py_INCREF(Load_singleton);
3063 return Load_singleton;
3064 case Store:
3065 Py_INCREF(Store_singleton);
3066 return Store_singleton;
3067 case Del:
3068 Py_INCREF(Del_singleton);
3069 return Del_singleton;
3070 case AugLoad:
3071 Py_INCREF(AugLoad_singleton);
3072 return AugLoad_singleton;
3073 case AugStore:
3074 Py_INCREF(AugStore_singleton);
3075 return AugStore_singleton;
3076 case Param:
3077 Py_INCREF(Param_singleton);
3078 return Param_singleton;
3079 default:
3080 /* should never happen, but just in case ... */
3081 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3082 return NULL;
3083 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003085PyObject*
3086ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003088 slice_ty o = (slice_ty)_o;
3089 PyObject *result = NULL, *value = NULL;
3090 if (!o) {
3091 Py_INCREF(Py_None);
3092 return Py_None;
3093 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003094
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003095 switch (o->kind) {
3096 case Slice_kind:
3097 result = PyType_GenericNew(Slice_type, NULL, NULL);
3098 if (!result) goto failed;
3099 value = ast2obj_expr(o->v.Slice.lower);
3100 if (!value) goto failed;
3101 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3102 goto failed;
3103 Py_DECREF(value);
3104 value = ast2obj_expr(o->v.Slice.upper);
3105 if (!value) goto failed;
3106 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3107 goto failed;
3108 Py_DECREF(value);
3109 value = ast2obj_expr(o->v.Slice.step);
3110 if (!value) goto failed;
3111 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3112 goto failed;
3113 Py_DECREF(value);
3114 break;
3115 case ExtSlice_kind:
3116 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3117 if (!result) goto failed;
3118 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3119 if (!value) goto failed;
3120 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3121 goto failed;
3122 Py_DECREF(value);
3123 break;
3124 case Index_kind:
3125 result = PyType_GenericNew(Index_type, NULL, NULL);
3126 if (!result) goto failed;
3127 value = ast2obj_expr(o->v.Index.value);
3128 if (!value) goto failed;
3129 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3130 goto failed;
3131 Py_DECREF(value);
3132 break;
3133 }
3134 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003135failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003136 Py_XDECREF(value);
3137 Py_XDECREF(result);
3138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139}
3140
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003141PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003143 switch(o) {
3144 case And:
3145 Py_INCREF(And_singleton);
3146 return And_singleton;
3147 case Or:
3148 Py_INCREF(Or_singleton);
3149 return Or_singleton;
3150 default:
3151 /* should never happen, but just in case ... */
3152 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3153 return NULL;
3154 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003155}
3156PyObject* ast2obj_operator(operator_ty o)
3157{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003158 switch(o) {
3159 case Add:
3160 Py_INCREF(Add_singleton);
3161 return Add_singleton;
3162 case Sub:
3163 Py_INCREF(Sub_singleton);
3164 return Sub_singleton;
3165 case Mult:
3166 Py_INCREF(Mult_singleton);
3167 return Mult_singleton;
3168 case Div:
3169 Py_INCREF(Div_singleton);
3170 return Div_singleton;
3171 case Mod:
3172 Py_INCREF(Mod_singleton);
3173 return Mod_singleton;
3174 case Pow:
3175 Py_INCREF(Pow_singleton);
3176 return Pow_singleton;
3177 case LShift:
3178 Py_INCREF(LShift_singleton);
3179 return LShift_singleton;
3180 case RShift:
3181 Py_INCREF(RShift_singleton);
3182 return RShift_singleton;
3183 case BitOr:
3184 Py_INCREF(BitOr_singleton);
3185 return BitOr_singleton;
3186 case BitXor:
3187 Py_INCREF(BitXor_singleton);
3188 return BitXor_singleton;
3189 case BitAnd:
3190 Py_INCREF(BitAnd_singleton);
3191 return BitAnd_singleton;
3192 case FloorDiv:
3193 Py_INCREF(FloorDiv_singleton);
3194 return FloorDiv_singleton;
3195 default:
3196 /* should never happen, but just in case ... */
3197 PyErr_Format(PyExc_SystemError, "unknown operator found");
3198 return NULL;
3199 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003200}
3201PyObject* ast2obj_unaryop(unaryop_ty o)
3202{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003203 switch(o) {
3204 case Invert:
3205 Py_INCREF(Invert_singleton);
3206 return Invert_singleton;
3207 case Not:
3208 Py_INCREF(Not_singleton);
3209 return Not_singleton;
3210 case UAdd:
3211 Py_INCREF(UAdd_singleton);
3212 return UAdd_singleton;
3213 case USub:
3214 Py_INCREF(USub_singleton);
3215 return USub_singleton;
3216 default:
3217 /* should never happen, but just in case ... */
3218 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3219 return NULL;
3220 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003221}
3222PyObject* ast2obj_cmpop(cmpop_ty o)
3223{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003224 switch(o) {
3225 case Eq:
3226 Py_INCREF(Eq_singleton);
3227 return Eq_singleton;
3228 case NotEq:
3229 Py_INCREF(NotEq_singleton);
3230 return NotEq_singleton;
3231 case Lt:
3232 Py_INCREF(Lt_singleton);
3233 return Lt_singleton;
3234 case LtE:
3235 Py_INCREF(LtE_singleton);
3236 return LtE_singleton;
3237 case Gt:
3238 Py_INCREF(Gt_singleton);
3239 return Gt_singleton;
3240 case GtE:
3241 Py_INCREF(GtE_singleton);
3242 return GtE_singleton;
3243 case Is:
3244 Py_INCREF(Is_singleton);
3245 return Is_singleton;
3246 case IsNot:
3247 Py_INCREF(IsNot_singleton);
3248 return IsNot_singleton;
3249 case In:
3250 Py_INCREF(In_singleton);
3251 return In_singleton;
3252 case NotIn:
3253 Py_INCREF(NotIn_singleton);
3254 return NotIn_singleton;
3255 default:
3256 /* should never happen, but just in case ... */
3257 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3258 return NULL;
3259 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003260}
3261PyObject*
3262ast2obj_comprehension(void* _o)
3263{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003264 comprehension_ty o = (comprehension_ty)_o;
3265 PyObject *result = NULL, *value = NULL;
3266 if (!o) {
3267 Py_INCREF(Py_None);
3268 return Py_None;
3269 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003270
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003271 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3272 if (!result) return NULL;
3273 value = ast2obj_expr(o->target);
3274 if (!value) goto failed;
3275 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3276 goto failed;
3277 Py_DECREF(value);
3278 value = ast2obj_expr(o->iter);
3279 if (!value) goto failed;
3280 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3281 goto failed;
3282 Py_DECREF(value);
3283 value = ast2obj_list(o->ifs, ast2obj_expr);
3284 if (!value) goto failed;
3285 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3286 goto failed;
3287 Py_DECREF(value);
3288 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003289failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003290 Py_XDECREF(value);
3291 Py_XDECREF(result);
3292 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293}
3294
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003295PyObject*
3296ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003298 excepthandler_ty o = (excepthandler_ty)_o;
3299 PyObject *result = NULL, *value = NULL;
3300 if (!o) {
3301 Py_INCREF(Py_None);
3302 return Py_None;
3303 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003304
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003305 switch (o->kind) {
3306 case ExceptHandler_kind:
3307 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3308 if (!result) goto failed;
3309 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003310 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003311 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3312 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003313 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003314 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003315 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003316 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3317 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003318 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003319 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3320 if (!value) goto failed;
3321 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3322 goto failed;
3323 Py_DECREF(value);
3324 break;
3325 }
3326 value = ast2obj_int(o->lineno);
3327 if (!value) goto failed;
3328 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3329 goto failed;
3330 Py_DECREF(value);
3331 value = ast2obj_int(o->col_offset);
3332 if (!value) goto failed;
3333 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3334 goto failed;
3335 Py_DECREF(value);
3336 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003337failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 Py_XDECREF(value);
3339 Py_XDECREF(result);
3340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003343PyObject*
3344ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003346 arguments_ty o = (arguments_ty)_o;
3347 PyObject *result = NULL, *value = NULL;
3348 if (!o) {
3349 Py_INCREF(Py_None);
3350 return Py_None;
3351 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003352
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003353 result = PyType_GenericNew(arguments_type, NULL, NULL);
3354 if (!result) return NULL;
3355 value = ast2obj_list(o->args, ast2obj_arg);
3356 if (!value) goto failed;
3357 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3358 goto failed;
3359 Py_DECREF(value);
3360 value = ast2obj_identifier(o->vararg);
3361 if (!value) goto failed;
3362 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3363 goto failed;
3364 Py_DECREF(value);
3365 value = ast2obj_expr(o->varargannotation);
3366 if (!value) goto failed;
3367 if (_PyObject_SetAttrId(result, &PyId_varargannotation, value) == -1)
3368 goto failed;
3369 Py_DECREF(value);
3370 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3371 if (!value) goto failed;
3372 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3373 goto failed;
3374 Py_DECREF(value);
3375 value = ast2obj_identifier(o->kwarg);
3376 if (!value) goto failed;
3377 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
3378 goto failed;
3379 Py_DECREF(value);
3380 value = ast2obj_expr(o->kwargannotation);
3381 if (!value) goto failed;
3382 if (_PyObject_SetAttrId(result, &PyId_kwargannotation, value) == -1)
3383 goto failed;
3384 Py_DECREF(value);
3385 value = ast2obj_list(o->defaults, ast2obj_expr);
3386 if (!value) goto failed;
3387 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3388 goto failed;
3389 Py_DECREF(value);
3390 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3391 if (!value) goto failed;
3392 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
3393 goto failed;
3394 Py_DECREF(value);
3395 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003396failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003397 Py_XDECREF(value);
3398 Py_XDECREF(result);
3399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400}
3401
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003402PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003403ast2obj_arg(void* _o)
3404{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003405 arg_ty o = (arg_ty)_o;
3406 PyObject *result = NULL, *value = NULL;
3407 if (!o) {
3408 Py_INCREF(Py_None);
3409 return Py_None;
3410 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003411
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003412 result = PyType_GenericNew(arg_type, NULL, NULL);
3413 if (!result) return NULL;
3414 value = ast2obj_identifier(o->arg);
3415 if (!value) goto failed;
3416 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3417 goto failed;
3418 Py_DECREF(value);
3419 value = ast2obj_expr(o->annotation);
3420 if (!value) goto failed;
3421 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3422 goto failed;
3423 Py_DECREF(value);
3424 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003425failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003426 Py_XDECREF(value);
3427 Py_XDECREF(result);
3428 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003429}
3430
3431PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003432ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003434 keyword_ty o = (keyword_ty)_o;
3435 PyObject *result = NULL, *value = NULL;
3436 if (!o) {
3437 Py_INCREF(Py_None);
3438 return Py_None;
3439 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003440
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003441 result = PyType_GenericNew(keyword_type, NULL, NULL);
3442 if (!result) return NULL;
3443 value = ast2obj_identifier(o->arg);
3444 if (!value) goto failed;
3445 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3446 goto failed;
3447 Py_DECREF(value);
3448 value = ast2obj_expr(o->value);
3449 if (!value) goto failed;
3450 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3451 goto failed;
3452 Py_DECREF(value);
3453 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003454failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003455 Py_XDECREF(value);
3456 Py_XDECREF(result);
3457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458}
3459
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003460PyObject*
3461ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003463 alias_ty o = (alias_ty)_o;
3464 PyObject *result = NULL, *value = NULL;
3465 if (!o) {
3466 Py_INCREF(Py_None);
3467 return Py_None;
3468 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003469
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003470 result = PyType_GenericNew(alias_type, NULL, NULL);
3471 if (!result) return NULL;
3472 value = ast2obj_identifier(o->name);
3473 if (!value) goto failed;
3474 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3475 goto failed;
3476 Py_DECREF(value);
3477 value = ast2obj_identifier(o->asname);
3478 if (!value) goto failed;
3479 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3480 goto failed;
3481 Py_DECREF(value);
3482 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003483failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003484 Py_XDECREF(value);
3485 Py_XDECREF(result);
3486 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487}
3488
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003489PyObject*
3490ast2obj_withitem(void* _o)
3491{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003492 withitem_ty o = (withitem_ty)_o;
3493 PyObject *result = NULL, *value = NULL;
3494 if (!o) {
3495 Py_INCREF(Py_None);
3496 return Py_None;
3497 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003498
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003499 result = PyType_GenericNew(withitem_type, NULL, NULL);
3500 if (!result) return NULL;
3501 value = ast2obj_expr(o->context_expr);
3502 if (!value) goto failed;
3503 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3504 goto failed;
3505 Py_DECREF(value);
3506 value = ast2obj_expr(o->optional_vars);
3507 if (!value) goto failed;
3508 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3509 goto failed;
3510 Py_DECREF(value);
3511 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003512failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003513 Py_XDECREF(value);
3514 Py_XDECREF(result);
3515 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003516}
3517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003519int
3520obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3521{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003522 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003523
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003524 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003525
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 if (obj == Py_None) {
3527 *out = NULL;
3528 return 0;
3529 }
3530 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3531 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003532 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 }
3534 if (isinstance) {
3535 asdl_seq* body;
3536
3537 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3538 int res;
3539 Py_ssize_t len;
3540 Py_ssize_t i;
3541 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3542 if (tmp == NULL) goto failed;
3543 if (!PyList_Check(tmp)) {
3544 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3545 goto failed;
3546 }
3547 len = PyList_GET_SIZE(tmp);
3548 body = asdl_seq_new(len, arena);
3549 if (body == NULL) goto failed;
3550 for (i = 0; i < len; i++) {
3551 stmt_ty value;
3552 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3553 if (res != 0) goto failed;
3554 asdl_seq_SET(body, i, value);
3555 }
3556 Py_XDECREF(tmp);
3557 tmp = NULL;
3558 } else {
3559 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3560 return 1;
3561 }
3562 *out = Module(body, arena);
3563 if (*out == NULL) goto failed;
3564 return 0;
3565 }
3566 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3567 if (isinstance == -1) {
3568 return 1;
3569 }
3570 if (isinstance) {
3571 asdl_seq* body;
3572
3573 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3574 int res;
3575 Py_ssize_t len;
3576 Py_ssize_t i;
3577 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3578 if (tmp == NULL) goto failed;
3579 if (!PyList_Check(tmp)) {
3580 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3581 goto failed;
3582 }
3583 len = PyList_GET_SIZE(tmp);
3584 body = asdl_seq_new(len, arena);
3585 if (body == NULL) goto failed;
3586 for (i = 0; i < len; i++) {
3587 stmt_ty value;
3588 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3589 if (res != 0) goto failed;
3590 asdl_seq_SET(body, i, value);
3591 }
3592 Py_XDECREF(tmp);
3593 tmp = NULL;
3594 } else {
3595 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3596 return 1;
3597 }
3598 *out = Interactive(body, arena);
3599 if (*out == NULL) goto failed;
3600 return 0;
3601 }
3602 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3603 if (isinstance == -1) {
3604 return 1;
3605 }
3606 if (isinstance) {
3607 expr_ty body;
3608
3609 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3610 int res;
3611 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3612 if (tmp == NULL) goto failed;
3613 res = obj2ast_expr(tmp, &body, arena);
3614 if (res != 0) goto failed;
3615 Py_XDECREF(tmp);
3616 tmp = NULL;
3617 } else {
3618 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3619 return 1;
3620 }
3621 *out = Expression(body, arena);
3622 if (*out == NULL) goto failed;
3623 return 0;
3624 }
3625 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3626 if (isinstance == -1) {
3627 return 1;
3628 }
3629 if (isinstance) {
3630 asdl_seq* body;
3631
3632 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3633 int res;
3634 Py_ssize_t len;
3635 Py_ssize_t i;
3636 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3637 if (tmp == NULL) goto failed;
3638 if (!PyList_Check(tmp)) {
3639 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3640 goto failed;
3641 }
3642 len = PyList_GET_SIZE(tmp);
3643 body = asdl_seq_new(len, arena);
3644 if (body == NULL) goto failed;
3645 for (i = 0; i < len; i++) {
3646 stmt_ty value;
3647 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3648 if (res != 0) goto failed;
3649 asdl_seq_SET(body, i, value);
3650 }
3651 Py_XDECREF(tmp);
3652 tmp = NULL;
3653 } else {
3654 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3655 return 1;
3656 }
3657 *out = Suite(body, arena);
3658 if (*out == NULL) goto failed;
3659 return 0;
3660 }
3661
3662 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3663 failed:
3664 Py_XDECREF(tmp);
3665 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003666}
3667
3668int
3669obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3670{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003671 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003672
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003673 PyObject *tmp = NULL;
3674 int lineno;
3675 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003676
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003677 if (obj == Py_None) {
3678 *out = NULL;
3679 return 0;
3680 }
3681 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3682 int res;
3683 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3684 if (tmp == NULL) goto failed;
3685 res = obj2ast_int(tmp, &lineno, arena);
3686 if (res != 0) goto failed;
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00003687 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003688 tmp = NULL;
3689 } else {
3690 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003691 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003692 }
3693 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3694 int res;
3695 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3696 if (tmp == NULL) goto failed;
3697 res = obj2ast_int(tmp, &col_offset, arena);
3698 if (res != 0) goto failed;
3699 Py_XDECREF(tmp);
3700 tmp = NULL;
3701 } else {
3702 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3703 return 1;
3704 }
3705 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3706 if (isinstance == -1) {
3707 return 1;
3708 }
3709 if (isinstance) {
3710 identifier name;
3711 arguments_ty args;
3712 asdl_seq* body;
3713 asdl_seq* decorator_list;
3714 expr_ty returns;
3715
3716 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3717 int res;
3718 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3719 if (tmp == NULL) goto failed;
3720 res = obj2ast_identifier(tmp, &name, arena);
3721 if (res != 0) goto failed;
3722 Py_XDECREF(tmp);
3723 tmp = NULL;
3724 } else {
3725 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3726 return 1;
3727 }
3728 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3729 int res;
3730 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3731 if (tmp == NULL) goto failed;
3732 res = obj2ast_arguments(tmp, &args, arena);
3733 if (res != 0) goto failed;
3734 Py_XDECREF(tmp);
3735 tmp = NULL;
3736 } else {
3737 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3738 return 1;
3739 }
3740 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3741 int res;
3742 Py_ssize_t len;
3743 Py_ssize_t i;
3744 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3745 if (tmp == NULL) goto failed;
3746 if (!PyList_Check(tmp)) {
3747 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3748 goto failed;
3749 }
3750 len = PyList_GET_SIZE(tmp);
3751 body = asdl_seq_new(len, arena);
3752 if (body == NULL) goto failed;
3753 for (i = 0; i < len; i++) {
3754 stmt_ty value;
3755 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3756 if (res != 0) goto failed;
3757 asdl_seq_SET(body, i, value);
3758 }
3759 Py_XDECREF(tmp);
3760 tmp = NULL;
3761 } else {
3762 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3763 return 1;
3764 }
3765 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3766 int res;
3767 Py_ssize_t len;
3768 Py_ssize_t i;
3769 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3770 if (tmp == NULL) goto failed;
3771 if (!PyList_Check(tmp)) {
3772 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3773 goto failed;
3774 }
3775 len = PyList_GET_SIZE(tmp);
3776 decorator_list = asdl_seq_new(len, arena);
3777 if (decorator_list == NULL) goto failed;
3778 for (i = 0; i < len; i++) {
3779 expr_ty value;
3780 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3781 if (res != 0) goto failed;
3782 asdl_seq_SET(decorator_list, i, value);
3783 }
3784 Py_XDECREF(tmp);
3785 tmp = NULL;
3786 } else {
3787 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3788 return 1;
3789 }
3790 if (_PyObject_HasAttrId(obj, &PyId_returns)) {
3791 int res;
3792 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3793 if (tmp == NULL) goto failed;
3794 res = obj2ast_expr(tmp, &returns, arena);
3795 if (res != 0) goto failed;
3796 Py_XDECREF(tmp);
3797 tmp = NULL;
3798 } else {
3799 returns = NULL;
3800 }
3801 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3802 col_offset, arena);
3803 if (*out == NULL) goto failed;
3804 return 0;
3805 }
3806 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3807 if (isinstance == -1) {
3808 return 1;
3809 }
3810 if (isinstance) {
3811 identifier name;
3812 asdl_seq* bases;
3813 asdl_seq* keywords;
3814 expr_ty starargs;
3815 expr_ty kwargs;
3816 asdl_seq* body;
3817 asdl_seq* decorator_list;
3818
3819 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3820 int res;
3821 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3822 if (tmp == NULL) goto failed;
3823 res = obj2ast_identifier(tmp, &name, arena);
3824 if (res != 0) goto failed;
3825 Py_XDECREF(tmp);
3826 tmp = NULL;
3827 } else {
3828 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3829 return 1;
3830 }
3831 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3832 int res;
3833 Py_ssize_t len;
3834 Py_ssize_t i;
3835 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3836 if (tmp == NULL) goto failed;
3837 if (!PyList_Check(tmp)) {
3838 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3839 goto failed;
3840 }
3841 len = PyList_GET_SIZE(tmp);
3842 bases = asdl_seq_new(len, arena);
3843 if (bases == NULL) goto failed;
3844 for (i = 0; i < len; i++) {
3845 expr_ty value;
3846 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3847 if (res != 0) goto failed;
3848 asdl_seq_SET(bases, i, value);
3849 }
3850 Py_XDECREF(tmp);
3851 tmp = NULL;
3852 } else {
3853 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3854 return 1;
3855 }
3856 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3857 int res;
3858 Py_ssize_t len;
3859 Py_ssize_t i;
3860 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3861 if (tmp == NULL) goto failed;
3862 if (!PyList_Check(tmp)) {
3863 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3864 goto failed;
3865 }
3866 len = PyList_GET_SIZE(tmp);
3867 keywords = asdl_seq_new(len, arena);
3868 if (keywords == NULL) goto failed;
3869 for (i = 0; i < len; i++) {
3870 keyword_ty value;
3871 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3872 if (res != 0) goto failed;
3873 asdl_seq_SET(keywords, i, value);
3874 }
3875 Py_XDECREF(tmp);
3876 tmp = NULL;
3877 } else {
3878 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3879 return 1;
3880 }
3881 if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
3882 int res;
3883 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3884 if (tmp == NULL) goto failed;
3885 res = obj2ast_expr(tmp, &starargs, arena);
3886 if (res != 0) goto failed;
3887 Py_XDECREF(tmp);
3888 tmp = NULL;
3889 } else {
3890 starargs = NULL;
3891 }
3892 if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
3893 int res;
3894 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3895 if (tmp == NULL) goto failed;
3896 res = obj2ast_expr(tmp, &kwargs, arena);
3897 if (res != 0) goto failed;
3898 Py_XDECREF(tmp);
3899 tmp = NULL;
3900 } else {
3901 kwargs = NULL;
3902 }
3903 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3904 int res;
3905 Py_ssize_t len;
3906 Py_ssize_t i;
3907 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3908 if (tmp == NULL) goto failed;
3909 if (!PyList_Check(tmp)) {
3910 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3911 goto failed;
3912 }
3913 len = PyList_GET_SIZE(tmp);
3914 body = asdl_seq_new(len, arena);
3915 if (body == NULL) goto failed;
3916 for (i = 0; i < len; i++) {
3917 stmt_ty value;
3918 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3919 if (res != 0) goto failed;
3920 asdl_seq_SET(body, i, value);
3921 }
3922 Py_XDECREF(tmp);
3923 tmp = NULL;
3924 } else {
3925 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3926 return 1;
3927 }
3928 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3929 int res;
3930 Py_ssize_t len;
3931 Py_ssize_t i;
3932 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3933 if (tmp == NULL) goto failed;
3934 if (!PyList_Check(tmp)) {
3935 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3936 goto failed;
3937 }
3938 len = PyList_GET_SIZE(tmp);
3939 decorator_list = asdl_seq_new(len, arena);
3940 if (decorator_list == NULL) goto failed;
3941 for (i = 0; i < len; i++) {
3942 expr_ty value;
3943 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3944 if (res != 0) goto failed;
3945 asdl_seq_SET(decorator_list, i, value);
3946 }
3947 Py_XDECREF(tmp);
3948 tmp = NULL;
3949 } else {
3950 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3951 return 1;
3952 }
3953 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3954 decorator_list, lineno, col_offset, arena);
3955 if (*out == NULL) goto failed;
3956 return 0;
3957 }
3958 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3959 if (isinstance == -1) {
3960 return 1;
3961 }
3962 if (isinstance) {
3963 expr_ty value;
3964
3965 if (_PyObject_HasAttrId(obj, &PyId_value)) {
3966 int res;
3967 tmp = _PyObject_GetAttrId(obj, &PyId_value);
3968 if (tmp == NULL) goto failed;
3969 res = obj2ast_expr(tmp, &value, arena);
3970 if (res != 0) goto failed;
3971 Py_XDECREF(tmp);
3972 tmp = NULL;
3973 } else {
3974 value = NULL;
3975 }
3976 *out = Return(value, lineno, col_offset, arena);
3977 if (*out == NULL) goto failed;
3978 return 0;
3979 }
3980 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3981 if (isinstance == -1) {
3982 return 1;
3983 }
3984 if (isinstance) {
3985 asdl_seq* targets;
3986
3987 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
3988 int res;
3989 Py_ssize_t len;
3990 Py_ssize_t i;
3991 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
3992 if (tmp == NULL) goto failed;
3993 if (!PyList_Check(tmp)) {
3994 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3995 goto failed;
3996 }
3997 len = PyList_GET_SIZE(tmp);
3998 targets = asdl_seq_new(len, arena);
3999 if (targets == NULL) goto failed;
4000 for (i = 0; i < len; i++) {
4001 expr_ty value;
4002 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4003 if (res != 0) goto failed;
4004 asdl_seq_SET(targets, i, value);
4005 }
4006 Py_XDECREF(tmp);
4007 tmp = NULL;
4008 } else {
4009 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4010 return 1;
4011 }
4012 *out = Delete(targets, lineno, col_offset, arena);
4013 if (*out == NULL) goto failed;
4014 return 0;
4015 }
4016 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4017 if (isinstance == -1) {
4018 return 1;
4019 }
4020 if (isinstance) {
4021 asdl_seq* targets;
4022 expr_ty value;
4023
4024 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4025 int res;
4026 Py_ssize_t len;
4027 Py_ssize_t i;
4028 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4029 if (tmp == NULL) goto failed;
4030 if (!PyList_Check(tmp)) {
4031 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4032 goto failed;
4033 }
4034 len = PyList_GET_SIZE(tmp);
4035 targets = asdl_seq_new(len, arena);
4036 if (targets == NULL) goto failed;
4037 for (i = 0; i < len; i++) {
4038 expr_ty value;
4039 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4040 if (res != 0) goto failed;
4041 asdl_seq_SET(targets, i, value);
4042 }
4043 Py_XDECREF(tmp);
4044 tmp = NULL;
4045 } else {
4046 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4047 return 1;
4048 }
4049 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4050 int res;
4051 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4052 if (tmp == NULL) goto failed;
4053 res = obj2ast_expr(tmp, &value, arena);
4054 if (res != 0) goto failed;
4055 Py_XDECREF(tmp);
4056 tmp = NULL;
4057 } else {
4058 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4059 return 1;
4060 }
4061 *out = Assign(targets, value, lineno, col_offset, arena);
4062 if (*out == NULL) goto failed;
4063 return 0;
4064 }
4065 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4066 if (isinstance == -1) {
4067 return 1;
4068 }
4069 if (isinstance) {
4070 expr_ty target;
4071 operator_ty op;
4072 expr_ty value;
4073
4074 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4075 int res;
4076 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4077 if (tmp == NULL) goto failed;
4078 res = obj2ast_expr(tmp, &target, arena);
4079 if (res != 0) goto failed;
4080 Py_XDECREF(tmp);
4081 tmp = NULL;
4082 } else {
4083 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4084 return 1;
4085 }
4086 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4087 int res;
4088 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4089 if (tmp == NULL) goto failed;
4090 res = obj2ast_operator(tmp, &op, arena);
4091 if (res != 0) goto failed;
4092 Py_XDECREF(tmp);
4093 tmp = NULL;
4094 } else {
4095 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4096 return 1;
4097 }
4098 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4099 int res;
4100 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4101 if (tmp == NULL) goto failed;
4102 res = obj2ast_expr(tmp, &value, arena);
4103 if (res != 0) goto failed;
4104 Py_XDECREF(tmp);
4105 tmp = NULL;
4106 } else {
4107 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4108 return 1;
4109 }
4110 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4111 if (*out == NULL) goto failed;
4112 return 0;
4113 }
4114 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4115 if (isinstance == -1) {
4116 return 1;
4117 }
4118 if (isinstance) {
4119 expr_ty target;
4120 expr_ty iter;
4121 asdl_seq* body;
4122 asdl_seq* orelse;
4123
4124 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4125 int res;
4126 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4127 if (tmp == NULL) goto failed;
4128 res = obj2ast_expr(tmp, &target, arena);
4129 if (res != 0) goto failed;
4130 Py_XDECREF(tmp);
4131 tmp = NULL;
4132 } else {
4133 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4134 return 1;
4135 }
4136 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4137 int res;
4138 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4139 if (tmp == NULL) goto failed;
4140 res = obj2ast_expr(tmp, &iter, arena);
4141 if (res != 0) goto failed;
4142 Py_XDECREF(tmp);
4143 tmp = NULL;
4144 } else {
4145 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4146 return 1;
4147 }
4148 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4149 int res;
4150 Py_ssize_t len;
4151 Py_ssize_t i;
4152 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4153 if (tmp == NULL) goto failed;
4154 if (!PyList_Check(tmp)) {
4155 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4156 goto failed;
4157 }
4158 len = PyList_GET_SIZE(tmp);
4159 body = asdl_seq_new(len, arena);
4160 if (body == NULL) goto failed;
4161 for (i = 0; i < len; i++) {
4162 stmt_ty value;
4163 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4164 if (res != 0) goto failed;
4165 asdl_seq_SET(body, i, value);
4166 }
4167 Py_XDECREF(tmp);
4168 tmp = NULL;
4169 } else {
4170 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4171 return 1;
4172 }
4173 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4174 int res;
4175 Py_ssize_t len;
4176 Py_ssize_t i;
4177 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4178 if (tmp == NULL) goto failed;
4179 if (!PyList_Check(tmp)) {
4180 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4181 goto failed;
4182 }
4183 len = PyList_GET_SIZE(tmp);
4184 orelse = asdl_seq_new(len, arena);
4185 if (orelse == NULL) goto failed;
4186 for (i = 0; i < len; i++) {
4187 stmt_ty value;
4188 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4189 if (res != 0) goto failed;
4190 asdl_seq_SET(orelse, i, value);
4191 }
4192 Py_XDECREF(tmp);
4193 tmp = NULL;
4194 } else {
4195 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4196 return 1;
4197 }
4198 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4199 if (*out == NULL) goto failed;
4200 return 0;
4201 }
4202 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4203 if (isinstance == -1) {
4204 return 1;
4205 }
4206 if (isinstance) {
4207 expr_ty test;
4208 asdl_seq* body;
4209 asdl_seq* orelse;
4210
4211 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4212 int res;
4213 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4214 if (tmp == NULL) goto failed;
4215 res = obj2ast_expr(tmp, &test, arena);
4216 if (res != 0) goto failed;
4217 Py_XDECREF(tmp);
4218 tmp = NULL;
4219 } else {
4220 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4221 return 1;
4222 }
4223 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4224 int res;
4225 Py_ssize_t len;
4226 Py_ssize_t i;
4227 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4228 if (tmp == NULL) goto failed;
4229 if (!PyList_Check(tmp)) {
4230 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4231 goto failed;
4232 }
4233 len = PyList_GET_SIZE(tmp);
4234 body = asdl_seq_new(len, arena);
4235 if (body == NULL) goto failed;
4236 for (i = 0; i < len; i++) {
4237 stmt_ty value;
4238 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4239 if (res != 0) goto failed;
4240 asdl_seq_SET(body, i, value);
4241 }
4242 Py_XDECREF(tmp);
4243 tmp = NULL;
4244 } else {
4245 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4246 return 1;
4247 }
4248 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4249 int res;
4250 Py_ssize_t len;
4251 Py_ssize_t i;
4252 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4253 if (tmp == NULL) goto failed;
4254 if (!PyList_Check(tmp)) {
4255 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4256 goto failed;
4257 }
4258 len = PyList_GET_SIZE(tmp);
4259 orelse = asdl_seq_new(len, arena);
4260 if (orelse == NULL) goto failed;
4261 for (i = 0; i < len; i++) {
4262 stmt_ty value;
4263 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4264 if (res != 0) goto failed;
4265 asdl_seq_SET(orelse, i, value);
4266 }
4267 Py_XDECREF(tmp);
4268 tmp = NULL;
4269 } else {
4270 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4271 return 1;
4272 }
4273 *out = While(test, body, orelse, lineno, col_offset, arena);
4274 if (*out == NULL) goto failed;
4275 return 0;
4276 }
4277 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4278 if (isinstance == -1) {
4279 return 1;
4280 }
4281 if (isinstance) {
4282 expr_ty test;
4283 asdl_seq* body;
4284 asdl_seq* orelse;
4285
4286 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4287 int res;
4288 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4289 if (tmp == NULL) goto failed;
4290 res = obj2ast_expr(tmp, &test, arena);
4291 if (res != 0) goto failed;
4292 Py_XDECREF(tmp);
4293 tmp = NULL;
4294 } else {
4295 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4296 return 1;
4297 }
4298 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4299 int res;
4300 Py_ssize_t len;
4301 Py_ssize_t i;
4302 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4303 if (tmp == NULL) goto failed;
4304 if (!PyList_Check(tmp)) {
4305 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4306 goto failed;
4307 }
4308 len = PyList_GET_SIZE(tmp);
4309 body = asdl_seq_new(len, arena);
4310 if (body == NULL) goto failed;
4311 for (i = 0; i < len; i++) {
4312 stmt_ty value;
4313 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4314 if (res != 0) goto failed;
4315 asdl_seq_SET(body, i, value);
4316 }
4317 Py_XDECREF(tmp);
4318 tmp = NULL;
4319 } else {
4320 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4321 return 1;
4322 }
4323 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4324 int res;
4325 Py_ssize_t len;
4326 Py_ssize_t i;
4327 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4328 if (tmp == NULL) goto failed;
4329 if (!PyList_Check(tmp)) {
4330 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4331 goto failed;
4332 }
4333 len = PyList_GET_SIZE(tmp);
4334 orelse = asdl_seq_new(len, arena);
4335 if (orelse == NULL) goto failed;
4336 for (i = 0; i < len; i++) {
4337 stmt_ty value;
4338 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4339 if (res != 0) goto failed;
4340 asdl_seq_SET(orelse, i, value);
4341 }
4342 Py_XDECREF(tmp);
4343 tmp = NULL;
4344 } else {
4345 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4346 return 1;
4347 }
4348 *out = If(test, body, orelse, lineno, col_offset, arena);
4349 if (*out == NULL) goto failed;
4350 return 0;
4351 }
4352 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4353 if (isinstance == -1) {
4354 return 1;
4355 }
4356 if (isinstance) {
4357 asdl_seq* items;
4358 asdl_seq* body;
4359
4360 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4361 int res;
4362 Py_ssize_t len;
4363 Py_ssize_t i;
4364 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4365 if (tmp == NULL) goto failed;
4366 if (!PyList_Check(tmp)) {
4367 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4368 goto failed;
4369 }
4370 len = PyList_GET_SIZE(tmp);
4371 items = asdl_seq_new(len, arena);
4372 if (items == NULL) goto failed;
4373 for (i = 0; i < len; i++) {
4374 withitem_ty value;
4375 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4376 if (res != 0) goto failed;
4377 asdl_seq_SET(items, i, value);
4378 }
4379 Py_XDECREF(tmp);
4380 tmp = NULL;
4381 } else {
4382 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4383 return 1;
4384 }
4385 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4386 int res;
4387 Py_ssize_t len;
4388 Py_ssize_t i;
4389 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4390 if (tmp == NULL) goto failed;
4391 if (!PyList_Check(tmp)) {
4392 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4393 goto failed;
4394 }
4395 len = PyList_GET_SIZE(tmp);
4396 body = asdl_seq_new(len, arena);
4397 if (body == NULL) goto failed;
4398 for (i = 0; i < len; i++) {
4399 stmt_ty value;
4400 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4401 if (res != 0) goto failed;
4402 asdl_seq_SET(body, i, value);
4403 }
4404 Py_XDECREF(tmp);
4405 tmp = NULL;
4406 } else {
4407 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4408 return 1;
4409 }
4410 *out = With(items, body, lineno, col_offset, arena);
4411 if (*out == NULL) goto failed;
4412 return 0;
4413 }
4414 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4415 if (isinstance == -1) {
4416 return 1;
4417 }
4418 if (isinstance) {
4419 expr_ty exc;
4420 expr_ty cause;
4421
4422 if (_PyObject_HasAttrId(obj, &PyId_exc)) {
4423 int res;
4424 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4425 if (tmp == NULL) goto failed;
4426 res = obj2ast_expr(tmp, &exc, arena);
4427 if (res != 0) goto failed;
4428 Py_XDECREF(tmp);
4429 tmp = NULL;
4430 } else {
4431 exc = NULL;
4432 }
4433 if (_PyObject_HasAttrId(obj, &PyId_cause)) {
4434 int res;
4435 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4436 if (tmp == NULL) goto failed;
4437 res = obj2ast_expr(tmp, &cause, arena);
4438 if (res != 0) goto failed;
4439 Py_XDECREF(tmp);
4440 tmp = NULL;
4441 } else {
4442 cause = NULL;
4443 }
4444 *out = Raise(exc, cause, lineno, col_offset, arena);
4445 if (*out == NULL) goto failed;
4446 return 0;
4447 }
4448 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4449 if (isinstance == -1) {
4450 return 1;
4451 }
4452 if (isinstance) {
4453 asdl_seq* body;
4454 asdl_seq* handlers;
4455 asdl_seq* orelse;
4456 asdl_seq* finalbody;
4457
4458 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4459 int res;
4460 Py_ssize_t len;
4461 Py_ssize_t i;
4462 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4463 if (tmp == NULL) goto failed;
4464 if (!PyList_Check(tmp)) {
4465 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4466 goto failed;
4467 }
4468 len = PyList_GET_SIZE(tmp);
4469 body = asdl_seq_new(len, arena);
4470 if (body == NULL) goto failed;
4471 for (i = 0; i < len; i++) {
4472 stmt_ty value;
4473 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4474 if (res != 0) goto failed;
4475 asdl_seq_SET(body, i, value);
4476 }
4477 Py_XDECREF(tmp);
4478 tmp = NULL;
4479 } else {
4480 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4481 return 1;
4482 }
4483 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4484 int res;
4485 Py_ssize_t len;
4486 Py_ssize_t i;
4487 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4488 if (tmp == NULL) goto failed;
4489 if (!PyList_Check(tmp)) {
4490 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4491 goto failed;
4492 }
4493 len = PyList_GET_SIZE(tmp);
4494 handlers = asdl_seq_new(len, arena);
4495 if (handlers == NULL) goto failed;
4496 for (i = 0; i < len; i++) {
4497 excepthandler_ty value;
4498 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4499 if (res != 0) goto failed;
4500 asdl_seq_SET(handlers, i, value);
4501 }
4502 Py_XDECREF(tmp);
4503 tmp = NULL;
4504 } else {
4505 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4506 return 1;
4507 }
4508 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4509 int res;
4510 Py_ssize_t len;
4511 Py_ssize_t i;
4512 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4513 if (tmp == NULL) goto failed;
4514 if (!PyList_Check(tmp)) {
4515 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4516 goto failed;
4517 }
4518 len = PyList_GET_SIZE(tmp);
4519 orelse = asdl_seq_new(len, arena);
4520 if (orelse == NULL) goto failed;
4521 for (i = 0; i < len; i++) {
4522 stmt_ty value;
4523 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4524 if (res != 0) goto failed;
4525 asdl_seq_SET(orelse, i, value);
4526 }
4527 Py_XDECREF(tmp);
4528 tmp = NULL;
4529 } else {
4530 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4531 return 1;
4532 }
4533 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4534 int res;
4535 Py_ssize_t len;
4536 Py_ssize_t i;
4537 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4538 if (tmp == NULL) goto failed;
4539 if (!PyList_Check(tmp)) {
4540 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4541 goto failed;
4542 }
4543 len = PyList_GET_SIZE(tmp);
4544 finalbody = asdl_seq_new(len, arena);
4545 if (finalbody == NULL) goto failed;
4546 for (i = 0; i < len; i++) {
4547 stmt_ty value;
4548 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4549 if (res != 0) goto failed;
4550 asdl_seq_SET(finalbody, i, value);
4551 }
4552 Py_XDECREF(tmp);
4553 tmp = NULL;
4554 } else {
4555 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4556 return 1;
4557 }
4558 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4559 arena);
4560 if (*out == NULL) goto failed;
4561 return 0;
4562 }
4563 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4564 if (isinstance == -1) {
4565 return 1;
4566 }
4567 if (isinstance) {
4568 expr_ty test;
4569 expr_ty msg;
4570
4571 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4572 int res;
4573 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4574 if (tmp == NULL) goto failed;
4575 res = obj2ast_expr(tmp, &test, arena);
4576 if (res != 0) goto failed;
4577 Py_XDECREF(tmp);
4578 tmp = NULL;
4579 } else {
4580 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4581 return 1;
4582 }
4583 if (_PyObject_HasAttrId(obj, &PyId_msg)) {
4584 int res;
4585 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4586 if (tmp == NULL) goto failed;
4587 res = obj2ast_expr(tmp, &msg, arena);
4588 if (res != 0) goto failed;
4589 Py_XDECREF(tmp);
4590 tmp = NULL;
4591 } else {
4592 msg = NULL;
4593 }
4594 *out = Assert(test, msg, lineno, col_offset, arena);
4595 if (*out == NULL) goto failed;
4596 return 0;
4597 }
4598 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4599 if (isinstance == -1) {
4600 return 1;
4601 }
4602 if (isinstance) {
4603 asdl_seq* names;
4604
4605 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4606 int res;
4607 Py_ssize_t len;
4608 Py_ssize_t i;
4609 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4610 if (tmp == NULL) goto failed;
4611 if (!PyList_Check(tmp)) {
4612 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4613 goto failed;
4614 }
4615 len = PyList_GET_SIZE(tmp);
4616 names = asdl_seq_new(len, arena);
4617 if (names == NULL) goto failed;
4618 for (i = 0; i < len; i++) {
4619 alias_ty value;
4620 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4621 if (res != 0) goto failed;
4622 asdl_seq_SET(names, i, value);
4623 }
4624 Py_XDECREF(tmp);
4625 tmp = NULL;
4626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4628 return 1;
4629 }
4630 *out = Import(names, lineno, col_offset, arena);
4631 if (*out == NULL) goto failed;
4632 return 0;
4633 }
4634 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4635 if (isinstance == -1) {
4636 return 1;
4637 }
4638 if (isinstance) {
4639 identifier module;
4640 asdl_seq* names;
4641 int level;
4642
4643 if (_PyObject_HasAttrId(obj, &PyId_module)) {
4644 int res;
4645 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4646 if (tmp == NULL) goto failed;
4647 res = obj2ast_identifier(tmp, &module, arena);
4648 if (res != 0) goto failed;
4649 Py_XDECREF(tmp);
4650 tmp = NULL;
4651 } else {
4652 module = NULL;
4653 }
4654 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4655 int res;
4656 Py_ssize_t len;
4657 Py_ssize_t i;
4658 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4659 if (tmp == NULL) goto failed;
4660 if (!PyList_Check(tmp)) {
4661 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4662 goto failed;
4663 }
4664 len = PyList_GET_SIZE(tmp);
4665 names = asdl_seq_new(len, arena);
4666 if (names == NULL) goto failed;
4667 for (i = 0; i < len; i++) {
4668 alias_ty value;
4669 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4670 if (res != 0) goto failed;
4671 asdl_seq_SET(names, i, value);
4672 }
4673 Py_XDECREF(tmp);
4674 tmp = NULL;
4675 } else {
4676 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4677 return 1;
4678 }
4679 if (_PyObject_HasAttrId(obj, &PyId_level)) {
4680 int res;
4681 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4682 if (tmp == NULL) goto failed;
4683 res = obj2ast_int(tmp, &level, arena);
4684 if (res != 0) goto failed;
4685 Py_XDECREF(tmp);
4686 tmp = NULL;
4687 } else {
4688 level = 0;
4689 }
4690 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4691 if (*out == NULL) goto failed;
4692 return 0;
4693 }
4694 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4695 if (isinstance == -1) {
4696 return 1;
4697 }
4698 if (isinstance) {
4699 asdl_seq* names;
4700
4701 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4702 int res;
4703 Py_ssize_t len;
4704 Py_ssize_t i;
4705 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4706 if (tmp == NULL) goto failed;
4707 if (!PyList_Check(tmp)) {
4708 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4709 goto failed;
4710 }
4711 len = PyList_GET_SIZE(tmp);
4712 names = asdl_seq_new(len, arena);
4713 if (names == NULL) goto failed;
4714 for (i = 0; i < len; i++) {
4715 identifier value;
4716 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4717 if (res != 0) goto failed;
4718 asdl_seq_SET(names, i, value);
4719 }
4720 Py_XDECREF(tmp);
4721 tmp = NULL;
4722 } else {
4723 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4724 return 1;
4725 }
4726 *out = Global(names, lineno, col_offset, arena);
4727 if (*out == NULL) goto failed;
4728 return 0;
4729 }
4730 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4731 if (isinstance == -1) {
4732 return 1;
4733 }
4734 if (isinstance) {
4735 asdl_seq* names;
4736
4737 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4738 int res;
4739 Py_ssize_t len;
4740 Py_ssize_t i;
4741 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4742 if (tmp == NULL) goto failed;
4743 if (!PyList_Check(tmp)) {
4744 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4745 goto failed;
4746 }
4747 len = PyList_GET_SIZE(tmp);
4748 names = asdl_seq_new(len, arena);
4749 if (names == NULL) goto failed;
4750 for (i = 0; i < len; i++) {
4751 identifier value;
4752 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4753 if (res != 0) goto failed;
4754 asdl_seq_SET(names, i, value);
4755 }
4756 Py_XDECREF(tmp);
4757 tmp = NULL;
4758 } else {
4759 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4760 return 1;
4761 }
4762 *out = Nonlocal(names, lineno, col_offset, arena);
4763 if (*out == NULL) goto failed;
4764 return 0;
4765 }
4766 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4767 if (isinstance == -1) {
4768 return 1;
4769 }
4770 if (isinstance) {
4771 expr_ty value;
4772
4773 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4774 int res;
4775 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4776 if (tmp == NULL) goto failed;
4777 res = obj2ast_expr(tmp, &value, arena);
4778 if (res != 0) goto failed;
4779 Py_XDECREF(tmp);
4780 tmp = NULL;
4781 } else {
4782 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4783 return 1;
4784 }
4785 *out = Expr(value, lineno, col_offset, arena);
4786 if (*out == NULL) goto failed;
4787 return 0;
4788 }
4789 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4790 if (isinstance == -1) {
4791 return 1;
4792 }
4793 if (isinstance) {
4794
4795 *out = Pass(lineno, col_offset, arena);
4796 if (*out == NULL) goto failed;
4797 return 0;
4798 }
4799 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4800 if (isinstance == -1) {
4801 return 1;
4802 }
4803 if (isinstance) {
4804
4805 *out = Break(lineno, col_offset, arena);
4806 if (*out == NULL) goto failed;
4807 return 0;
4808 }
4809 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4810 if (isinstance == -1) {
4811 return 1;
4812 }
4813 if (isinstance) {
4814
4815 *out = Continue(lineno, col_offset, arena);
4816 if (*out == NULL) goto failed;
4817 return 0;
4818 }
4819
4820 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4821 failed:
4822 Py_XDECREF(tmp);
4823 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004824}
4825
4826int
4827obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4828{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004829 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004830
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004831 PyObject *tmp = NULL;
4832 int lineno;
4833 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004834
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004835 if (obj == Py_None) {
4836 *out = NULL;
4837 return 0;
4838 }
4839 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4840 int res;
4841 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4842 if (tmp == NULL) goto failed;
4843 res = obj2ast_int(tmp, &lineno, arena);
4844 if (res != 0) goto failed;
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00004845 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004846 tmp = NULL;
4847 } else {
4848 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004849 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004850 }
4851 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4852 int res;
4853 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4854 if (tmp == NULL) goto failed;
4855 res = obj2ast_int(tmp, &col_offset, arena);
4856 if (res != 0) goto failed;
4857 Py_XDECREF(tmp);
4858 tmp = NULL;
4859 } else {
4860 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4861 return 1;
4862 }
4863 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4864 if (isinstance == -1) {
4865 return 1;
4866 }
4867 if (isinstance) {
4868 boolop_ty op;
4869 asdl_seq* values;
4870
4871 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4872 int res;
4873 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4874 if (tmp == NULL) goto failed;
4875 res = obj2ast_boolop(tmp, &op, arena);
4876 if (res != 0) goto failed;
4877 Py_XDECREF(tmp);
4878 tmp = NULL;
4879 } else {
4880 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4881 return 1;
4882 }
4883 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4884 int res;
4885 Py_ssize_t len;
4886 Py_ssize_t i;
4887 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4888 if (tmp == NULL) goto failed;
4889 if (!PyList_Check(tmp)) {
4890 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4891 goto failed;
4892 }
4893 len = PyList_GET_SIZE(tmp);
4894 values = asdl_seq_new(len, arena);
4895 if (values == NULL) goto failed;
4896 for (i = 0; i < len; i++) {
4897 expr_ty value;
4898 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4899 if (res != 0) goto failed;
4900 asdl_seq_SET(values, i, value);
4901 }
4902 Py_XDECREF(tmp);
4903 tmp = NULL;
4904 } else {
4905 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4906 return 1;
4907 }
4908 *out = BoolOp(op, values, lineno, col_offset, arena);
4909 if (*out == NULL) goto failed;
4910 return 0;
4911 }
4912 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4913 if (isinstance == -1) {
4914 return 1;
4915 }
4916 if (isinstance) {
4917 expr_ty left;
4918 operator_ty op;
4919 expr_ty right;
4920
4921 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4922 int res;
4923 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4924 if (tmp == NULL) goto failed;
4925 res = obj2ast_expr(tmp, &left, arena);
4926 if (res != 0) goto failed;
4927 Py_XDECREF(tmp);
4928 tmp = NULL;
4929 } else {
4930 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4931 return 1;
4932 }
4933 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4934 int res;
4935 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4936 if (tmp == NULL) goto failed;
4937 res = obj2ast_operator(tmp, &op, arena);
4938 if (res != 0) goto failed;
4939 Py_XDECREF(tmp);
4940 tmp = NULL;
4941 } else {
4942 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4943 return 1;
4944 }
4945 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4946 int res;
4947 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4948 if (tmp == NULL) goto failed;
4949 res = obj2ast_expr(tmp, &right, arena);
4950 if (res != 0) goto failed;
4951 Py_XDECREF(tmp);
4952 tmp = NULL;
4953 } else {
4954 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4955 return 1;
4956 }
4957 *out = BinOp(left, op, right, lineno, col_offset, arena);
4958 if (*out == NULL) goto failed;
4959 return 0;
4960 }
4961 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4962 if (isinstance == -1) {
4963 return 1;
4964 }
4965 if (isinstance) {
4966 unaryop_ty op;
4967 expr_ty operand;
4968
4969 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4970 int res;
4971 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4972 if (tmp == NULL) goto failed;
4973 res = obj2ast_unaryop(tmp, &op, arena);
4974 if (res != 0) goto failed;
4975 Py_XDECREF(tmp);
4976 tmp = NULL;
4977 } else {
4978 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4979 return 1;
4980 }
4981 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4982 int res;
4983 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4984 if (tmp == NULL) goto failed;
4985 res = obj2ast_expr(tmp, &operand, arena);
4986 if (res != 0) goto failed;
4987 Py_XDECREF(tmp);
4988 tmp = NULL;
4989 } else {
4990 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4991 return 1;
4992 }
4993 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4994 if (*out == NULL) goto failed;
4995 return 0;
4996 }
4997 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4998 if (isinstance == -1) {
4999 return 1;
5000 }
5001 if (isinstance) {
5002 arguments_ty args;
5003 expr_ty body;
5004
5005 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5006 int res;
5007 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5008 if (tmp == NULL) goto failed;
5009 res = obj2ast_arguments(tmp, &args, arena);
5010 if (res != 0) goto failed;
5011 Py_XDECREF(tmp);
5012 tmp = NULL;
5013 } else {
5014 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5015 return 1;
5016 }
5017 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5018 int res;
5019 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5020 if (tmp == NULL) goto failed;
5021 res = obj2ast_expr(tmp, &body, arena);
5022 if (res != 0) goto failed;
5023 Py_XDECREF(tmp);
5024 tmp = NULL;
5025 } else {
5026 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5027 return 1;
5028 }
5029 *out = Lambda(args, body, lineno, col_offset, arena);
5030 if (*out == NULL) goto failed;
5031 return 0;
5032 }
5033 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5034 if (isinstance == -1) {
5035 return 1;
5036 }
5037 if (isinstance) {
5038 expr_ty test;
5039 expr_ty body;
5040 expr_ty orelse;
5041
5042 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5043 int res;
5044 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5045 if (tmp == NULL) goto failed;
5046 res = obj2ast_expr(tmp, &test, arena);
5047 if (res != 0) goto failed;
5048 Py_XDECREF(tmp);
5049 tmp = NULL;
5050 } else {
5051 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5052 return 1;
5053 }
5054 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5055 int res;
5056 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5057 if (tmp == NULL) goto failed;
5058 res = obj2ast_expr(tmp, &body, arena);
5059 if (res != 0) goto failed;
5060 Py_XDECREF(tmp);
5061 tmp = NULL;
5062 } else {
5063 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5064 return 1;
5065 }
5066 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5067 int res;
5068 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5069 if (tmp == NULL) goto failed;
5070 res = obj2ast_expr(tmp, &orelse, arena);
5071 if (res != 0) goto failed;
5072 Py_XDECREF(tmp);
5073 tmp = NULL;
5074 } else {
5075 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5076 return 1;
5077 }
5078 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5079 if (*out == NULL) goto failed;
5080 return 0;
5081 }
5082 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5083 if (isinstance == -1) {
5084 return 1;
5085 }
5086 if (isinstance) {
5087 asdl_seq* keys;
5088 asdl_seq* values;
5089
5090 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5091 int res;
5092 Py_ssize_t len;
5093 Py_ssize_t i;
5094 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5095 if (tmp == NULL) goto failed;
5096 if (!PyList_Check(tmp)) {
5097 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5098 goto failed;
5099 }
5100 len = PyList_GET_SIZE(tmp);
5101 keys = asdl_seq_new(len, arena);
5102 if (keys == NULL) goto failed;
5103 for (i = 0; i < len; i++) {
5104 expr_ty value;
5105 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5106 if (res != 0) goto failed;
5107 asdl_seq_SET(keys, i, value);
5108 }
5109 Py_XDECREF(tmp);
5110 tmp = NULL;
5111 } else {
5112 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5113 return 1;
5114 }
5115 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5116 int res;
5117 Py_ssize_t len;
5118 Py_ssize_t i;
5119 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5120 if (tmp == NULL) goto failed;
5121 if (!PyList_Check(tmp)) {
5122 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5123 goto failed;
5124 }
5125 len = PyList_GET_SIZE(tmp);
5126 values = asdl_seq_new(len, arena);
5127 if (values == NULL) goto failed;
5128 for (i = 0; i < len; i++) {
5129 expr_ty value;
5130 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5131 if (res != 0) goto failed;
5132 asdl_seq_SET(values, i, value);
5133 }
5134 Py_XDECREF(tmp);
5135 tmp = NULL;
5136 } else {
5137 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5138 return 1;
5139 }
5140 *out = Dict(keys, values, lineno, col_offset, arena);
5141 if (*out == NULL) goto failed;
5142 return 0;
5143 }
5144 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5145 if (isinstance == -1) {
5146 return 1;
5147 }
5148 if (isinstance) {
5149 asdl_seq* elts;
5150
5151 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5152 int res;
5153 Py_ssize_t len;
5154 Py_ssize_t i;
5155 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5156 if (tmp == NULL) goto failed;
5157 if (!PyList_Check(tmp)) {
5158 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5159 goto failed;
5160 }
5161 len = PyList_GET_SIZE(tmp);
5162 elts = asdl_seq_new(len, arena);
5163 if (elts == NULL) goto failed;
5164 for (i = 0; i < len; i++) {
5165 expr_ty value;
5166 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5167 if (res != 0) goto failed;
5168 asdl_seq_SET(elts, i, value);
5169 }
5170 Py_XDECREF(tmp);
5171 tmp = NULL;
5172 } else {
5173 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5174 return 1;
5175 }
5176 *out = Set(elts, lineno, col_offset, arena);
5177 if (*out == NULL) goto failed;
5178 return 0;
5179 }
5180 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5181 if (isinstance == -1) {
5182 return 1;
5183 }
5184 if (isinstance) {
5185 expr_ty elt;
5186 asdl_seq* generators;
5187
5188 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5189 int res;
5190 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5191 if (tmp == NULL) goto failed;
5192 res = obj2ast_expr(tmp, &elt, arena);
5193 if (res != 0) goto failed;
5194 Py_XDECREF(tmp);
5195 tmp = NULL;
5196 } else {
5197 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5198 return 1;
5199 }
5200 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5201 int res;
5202 Py_ssize_t len;
5203 Py_ssize_t i;
5204 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5205 if (tmp == NULL) goto failed;
5206 if (!PyList_Check(tmp)) {
5207 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5208 goto failed;
5209 }
5210 len = PyList_GET_SIZE(tmp);
5211 generators = asdl_seq_new(len, arena);
5212 if (generators == NULL) goto failed;
5213 for (i = 0; i < len; i++) {
5214 comprehension_ty value;
5215 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5216 if (res != 0) goto failed;
5217 asdl_seq_SET(generators, i, value);
5218 }
5219 Py_XDECREF(tmp);
5220 tmp = NULL;
5221 } else {
5222 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5223 return 1;
5224 }
5225 *out = ListComp(elt, generators, lineno, col_offset, arena);
5226 if (*out == NULL) goto failed;
5227 return 0;
5228 }
5229 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5230 if (isinstance == -1) {
5231 return 1;
5232 }
5233 if (isinstance) {
5234 expr_ty elt;
5235 asdl_seq* generators;
5236
5237 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5238 int res;
5239 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5240 if (tmp == NULL) goto failed;
5241 res = obj2ast_expr(tmp, &elt, arena);
5242 if (res != 0) goto failed;
5243 Py_XDECREF(tmp);
5244 tmp = NULL;
5245 } else {
5246 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5247 return 1;
5248 }
5249 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5250 int res;
5251 Py_ssize_t len;
5252 Py_ssize_t i;
5253 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5254 if (tmp == NULL) goto failed;
5255 if (!PyList_Check(tmp)) {
5256 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5257 goto failed;
5258 }
5259 len = PyList_GET_SIZE(tmp);
5260 generators = asdl_seq_new(len, arena);
5261 if (generators == NULL) goto failed;
5262 for (i = 0; i < len; i++) {
5263 comprehension_ty value;
5264 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5265 if (res != 0) goto failed;
5266 asdl_seq_SET(generators, i, value);
5267 }
5268 Py_XDECREF(tmp);
5269 tmp = NULL;
5270 } else {
5271 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5272 return 1;
5273 }
5274 *out = SetComp(elt, generators, lineno, col_offset, arena);
5275 if (*out == NULL) goto failed;
5276 return 0;
5277 }
5278 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5279 if (isinstance == -1) {
5280 return 1;
5281 }
5282 if (isinstance) {
5283 expr_ty key;
5284 expr_ty value;
5285 asdl_seq* generators;
5286
5287 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5288 int res;
5289 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5290 if (tmp == NULL) goto failed;
5291 res = obj2ast_expr(tmp, &key, arena);
5292 if (res != 0) goto failed;
5293 Py_XDECREF(tmp);
5294 tmp = NULL;
5295 } else {
5296 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5297 return 1;
5298 }
5299 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5300 int res;
5301 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5302 if (tmp == NULL) goto failed;
5303 res = obj2ast_expr(tmp, &value, arena);
5304 if (res != 0) goto failed;
5305 Py_XDECREF(tmp);
5306 tmp = NULL;
5307 } else {
5308 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5309 return 1;
5310 }
5311 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5312 int res;
5313 Py_ssize_t len;
5314 Py_ssize_t i;
5315 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5316 if (tmp == NULL) goto failed;
5317 if (!PyList_Check(tmp)) {
5318 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5319 goto failed;
5320 }
5321 len = PyList_GET_SIZE(tmp);
5322 generators = asdl_seq_new(len, arena);
5323 if (generators == NULL) goto failed;
5324 for (i = 0; i < len; i++) {
5325 comprehension_ty value;
5326 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5327 if (res != 0) goto failed;
5328 asdl_seq_SET(generators, i, value);
5329 }
5330 Py_XDECREF(tmp);
5331 tmp = NULL;
5332 } else {
5333 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5334 return 1;
5335 }
5336 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5337 if (*out == NULL) goto failed;
5338 return 0;
5339 }
5340 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5341 if (isinstance == -1) {
5342 return 1;
5343 }
5344 if (isinstance) {
5345 expr_ty elt;
5346 asdl_seq* generators;
5347
5348 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5349 int res;
5350 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5351 if (tmp == NULL) goto failed;
5352 res = obj2ast_expr(tmp, &elt, arena);
5353 if (res != 0) goto failed;
5354 Py_XDECREF(tmp);
5355 tmp = NULL;
5356 } else {
5357 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5358 return 1;
5359 }
5360 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5361 int res;
5362 Py_ssize_t len;
5363 Py_ssize_t i;
5364 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5365 if (tmp == NULL) goto failed;
5366 if (!PyList_Check(tmp)) {
5367 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5368 goto failed;
5369 }
5370 len = PyList_GET_SIZE(tmp);
5371 generators = asdl_seq_new(len, arena);
5372 if (generators == NULL) goto failed;
5373 for (i = 0; i < len; i++) {
5374 comprehension_ty value;
5375 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5376 if (res != 0) goto failed;
5377 asdl_seq_SET(generators, i, value);
5378 }
5379 Py_XDECREF(tmp);
5380 tmp = NULL;
5381 } else {
5382 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5383 return 1;
5384 }
5385 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5386 if (*out == NULL) goto failed;
5387 return 0;
5388 }
5389 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5390 if (isinstance == -1) {
5391 return 1;
5392 }
5393 if (isinstance) {
5394 expr_ty value;
5395
5396 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5397 int res;
5398 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5399 if (tmp == NULL) goto failed;
5400 res = obj2ast_expr(tmp, &value, arena);
5401 if (res != 0) goto failed;
5402 Py_XDECREF(tmp);
5403 tmp = NULL;
5404 } else {
5405 value = NULL;
5406 }
5407 *out = Yield(value, lineno, col_offset, arena);
5408 if (*out == NULL) goto failed;
5409 return 0;
5410 }
5411 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5412 if (isinstance == -1) {
5413 return 1;
5414 }
5415 if (isinstance) {
5416 expr_ty value;
5417
5418 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5419 int res;
5420 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5421 if (tmp == NULL) goto failed;
5422 res = obj2ast_expr(tmp, &value, arena);
5423 if (res != 0) goto failed;
5424 Py_XDECREF(tmp);
5425 tmp = NULL;
5426 } else {
5427 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5428 return 1;
5429 }
5430 *out = YieldFrom(value, lineno, col_offset, arena);
5431 if (*out == NULL) goto failed;
5432 return 0;
5433 }
5434 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5435 if (isinstance == -1) {
5436 return 1;
5437 }
5438 if (isinstance) {
5439 expr_ty left;
5440 asdl_int_seq* ops;
5441 asdl_seq* comparators;
5442
5443 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5444 int res;
5445 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5446 if (tmp == NULL) goto failed;
5447 res = obj2ast_expr(tmp, &left, arena);
5448 if (res != 0) goto failed;
5449 Py_XDECREF(tmp);
5450 tmp = NULL;
5451 } else {
5452 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5453 return 1;
5454 }
5455 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5456 int res;
5457 Py_ssize_t len;
5458 Py_ssize_t i;
5459 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5460 if (tmp == NULL) goto failed;
5461 if (!PyList_Check(tmp)) {
5462 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5463 goto failed;
5464 }
5465 len = PyList_GET_SIZE(tmp);
5466 ops = asdl_int_seq_new(len, arena);
5467 if (ops == NULL) goto failed;
5468 for (i = 0; i < len; i++) {
5469 cmpop_ty value;
5470 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5471 if (res != 0) goto failed;
5472 asdl_seq_SET(ops, i, value);
5473 }
5474 Py_XDECREF(tmp);
5475 tmp = NULL;
5476 } else {
5477 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5478 return 1;
5479 }
5480 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5481 int res;
5482 Py_ssize_t len;
5483 Py_ssize_t i;
5484 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5485 if (tmp == NULL) goto failed;
5486 if (!PyList_Check(tmp)) {
5487 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5488 goto failed;
5489 }
5490 len = PyList_GET_SIZE(tmp);
5491 comparators = asdl_seq_new(len, arena);
5492 if (comparators == NULL) goto failed;
5493 for (i = 0; i < len; i++) {
5494 expr_ty value;
5495 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5496 if (res != 0) goto failed;
5497 asdl_seq_SET(comparators, i, value);
5498 }
5499 Py_XDECREF(tmp);
5500 tmp = NULL;
5501 } else {
5502 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5503 return 1;
5504 }
5505 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5506 if (*out == NULL) goto failed;
5507 return 0;
5508 }
5509 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5510 if (isinstance == -1) {
5511 return 1;
5512 }
5513 if (isinstance) {
5514 expr_ty func;
5515 asdl_seq* args;
5516 asdl_seq* keywords;
5517 expr_ty starargs;
5518 expr_ty kwargs;
5519
5520 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5521 int res;
5522 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5523 if (tmp == NULL) goto failed;
5524 res = obj2ast_expr(tmp, &func, arena);
5525 if (res != 0) goto failed;
5526 Py_XDECREF(tmp);
5527 tmp = NULL;
5528 } else {
5529 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5530 return 1;
5531 }
5532 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5533 int res;
5534 Py_ssize_t len;
5535 Py_ssize_t i;
5536 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5537 if (tmp == NULL) goto failed;
5538 if (!PyList_Check(tmp)) {
5539 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5540 goto failed;
5541 }
5542 len = PyList_GET_SIZE(tmp);
5543 args = asdl_seq_new(len, arena);
5544 if (args == NULL) goto failed;
5545 for (i = 0; i < len; i++) {
5546 expr_ty value;
5547 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5548 if (res != 0) goto failed;
5549 asdl_seq_SET(args, i, value);
5550 }
5551 Py_XDECREF(tmp);
5552 tmp = NULL;
5553 } else {
5554 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5555 return 1;
5556 }
5557 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5558 int res;
5559 Py_ssize_t len;
5560 Py_ssize_t i;
5561 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5562 if (tmp == NULL) goto failed;
5563 if (!PyList_Check(tmp)) {
5564 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5565 goto failed;
5566 }
5567 len = PyList_GET_SIZE(tmp);
5568 keywords = asdl_seq_new(len, arena);
5569 if (keywords == NULL) goto failed;
5570 for (i = 0; i < len; i++) {
5571 keyword_ty value;
5572 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5573 if (res != 0) goto failed;
5574 asdl_seq_SET(keywords, i, value);
5575 }
5576 Py_XDECREF(tmp);
5577 tmp = NULL;
5578 } else {
5579 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5580 return 1;
5581 }
5582 if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
5583 int res;
5584 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5585 if (tmp == NULL) goto failed;
5586 res = obj2ast_expr(tmp, &starargs, arena);
5587 if (res != 0) goto failed;
5588 Py_XDECREF(tmp);
5589 tmp = NULL;
5590 } else {
5591 starargs = NULL;
5592 }
5593 if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
5594 int res;
5595 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5596 if (tmp == NULL) goto failed;
5597 res = obj2ast_expr(tmp, &kwargs, arena);
5598 if (res != 0) goto failed;
5599 Py_XDECREF(tmp);
5600 tmp = NULL;
5601 } else {
5602 kwargs = NULL;
5603 }
5604 *out = Call(func, args, keywords, starargs, kwargs, lineno, col_offset,
5605 arena);
5606 if (*out == NULL) goto failed;
5607 return 0;
5608 }
5609 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5610 if (isinstance == -1) {
5611 return 1;
5612 }
5613 if (isinstance) {
5614 object n;
5615
5616 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5617 int res;
5618 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5619 if (tmp == NULL) goto failed;
5620 res = obj2ast_object(tmp, &n, arena);
5621 if (res != 0) goto failed;
5622 Py_XDECREF(tmp);
5623 tmp = NULL;
5624 } else {
5625 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5626 return 1;
5627 }
5628 *out = Num(n, lineno, col_offset, arena);
5629 if (*out == NULL) goto failed;
5630 return 0;
5631 }
5632 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5633 if (isinstance == -1) {
5634 return 1;
5635 }
5636 if (isinstance) {
5637 string s;
5638
5639 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5640 int res;
5641 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5642 if (tmp == NULL) goto failed;
5643 res = obj2ast_string(tmp, &s, arena);
5644 if (res != 0) goto failed;
5645 Py_XDECREF(tmp);
5646 tmp = NULL;
5647 } else {
5648 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5649 return 1;
5650 }
5651 *out = Str(s, lineno, col_offset, arena);
5652 if (*out == NULL) goto failed;
5653 return 0;
5654 }
5655 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5656 if (isinstance == -1) {
5657 return 1;
5658 }
5659 if (isinstance) {
5660 bytes s;
5661
5662 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5663 int res;
5664 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5665 if (tmp == NULL) goto failed;
5666 res = obj2ast_bytes(tmp, &s, arena);
5667 if (res != 0) goto failed;
5668 Py_XDECREF(tmp);
5669 tmp = NULL;
5670 } else {
5671 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5672 return 1;
5673 }
5674 *out = Bytes(s, lineno, col_offset, arena);
5675 if (*out == NULL) goto failed;
5676 return 0;
5677 }
5678 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5679 if (isinstance == -1) {
5680 return 1;
5681 }
5682 if (isinstance) {
5683
5684 *out = Ellipsis(lineno, col_offset, arena);
5685 if (*out == NULL) goto failed;
5686 return 0;
5687 }
5688 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5689 if (isinstance == -1) {
5690 return 1;
5691 }
5692 if (isinstance) {
5693 expr_ty value;
5694 identifier attr;
5695 expr_context_ty ctx;
5696
5697 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5698 int res;
5699 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5700 if (tmp == NULL) goto failed;
5701 res = obj2ast_expr(tmp, &value, arena);
5702 if (res != 0) goto failed;
5703 Py_XDECREF(tmp);
5704 tmp = NULL;
5705 } else {
5706 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5707 return 1;
5708 }
5709 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5710 int res;
5711 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5712 if (tmp == NULL) goto failed;
5713 res = obj2ast_identifier(tmp, &attr, arena);
5714 if (res != 0) goto failed;
5715 Py_XDECREF(tmp);
5716 tmp = NULL;
5717 } else {
5718 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5719 return 1;
5720 }
5721 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5722 int res;
5723 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5724 if (tmp == NULL) goto failed;
5725 res = obj2ast_expr_context(tmp, &ctx, arena);
5726 if (res != 0) goto failed;
5727 Py_XDECREF(tmp);
5728 tmp = NULL;
5729 } else {
5730 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5731 return 1;
5732 }
5733 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5734 if (*out == NULL) goto failed;
5735 return 0;
5736 }
5737 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5738 if (isinstance == -1) {
5739 return 1;
5740 }
5741 if (isinstance) {
5742 expr_ty value;
5743 slice_ty slice;
5744 expr_context_ty ctx;
5745
5746 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5747 int res;
5748 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5749 if (tmp == NULL) goto failed;
5750 res = obj2ast_expr(tmp, &value, arena);
5751 if (res != 0) goto failed;
5752 Py_XDECREF(tmp);
5753 tmp = NULL;
5754 } else {
5755 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5756 return 1;
5757 }
5758 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5759 int res;
5760 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5761 if (tmp == NULL) goto failed;
5762 res = obj2ast_slice(tmp, &slice, arena);
5763 if (res != 0) goto failed;
5764 Py_XDECREF(tmp);
5765 tmp = NULL;
5766 } else {
5767 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5768 return 1;
5769 }
5770 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5771 int res;
5772 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5773 if (tmp == NULL) goto failed;
5774 res = obj2ast_expr_context(tmp, &ctx, arena);
5775 if (res != 0) goto failed;
5776 Py_XDECREF(tmp);
5777 tmp = NULL;
5778 } else {
5779 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5780 return 1;
5781 }
5782 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5783 if (*out == NULL) goto failed;
5784 return 0;
5785 }
5786 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5787 if (isinstance == -1) {
5788 return 1;
5789 }
5790 if (isinstance) {
5791 expr_ty value;
5792 expr_context_ty ctx;
5793
5794 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5795 int res;
5796 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5797 if (tmp == NULL) goto failed;
5798 res = obj2ast_expr(tmp, &value, arena);
5799 if (res != 0) goto failed;
5800 Py_XDECREF(tmp);
5801 tmp = NULL;
5802 } else {
5803 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5804 return 1;
5805 }
5806 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5807 int res;
5808 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5809 if (tmp == NULL) goto failed;
5810 res = obj2ast_expr_context(tmp, &ctx, arena);
5811 if (res != 0) goto failed;
5812 Py_XDECREF(tmp);
5813 tmp = NULL;
5814 } else {
5815 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5816 return 1;
5817 }
5818 *out = Starred(value, ctx, lineno, col_offset, arena);
5819 if (*out == NULL) goto failed;
5820 return 0;
5821 }
5822 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5823 if (isinstance == -1) {
5824 return 1;
5825 }
5826 if (isinstance) {
5827 identifier id;
5828 expr_context_ty ctx;
5829
5830 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5831 int res;
5832 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5833 if (tmp == NULL) goto failed;
5834 res = obj2ast_identifier(tmp, &id, arena);
5835 if (res != 0) goto failed;
5836 Py_XDECREF(tmp);
5837 tmp = NULL;
5838 } else {
5839 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5840 return 1;
5841 }
5842 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5843 int res;
5844 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5845 if (tmp == NULL) goto failed;
5846 res = obj2ast_expr_context(tmp, &ctx, arena);
5847 if (res != 0) goto failed;
5848 Py_XDECREF(tmp);
5849 tmp = NULL;
5850 } else {
5851 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5852 return 1;
5853 }
5854 *out = Name(id, ctx, lineno, col_offset, arena);
5855 if (*out == NULL) goto failed;
5856 return 0;
5857 }
5858 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5859 if (isinstance == -1) {
5860 return 1;
5861 }
5862 if (isinstance) {
5863 asdl_seq* elts;
5864 expr_context_ty ctx;
5865
5866 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5867 int res;
5868 Py_ssize_t len;
5869 Py_ssize_t i;
5870 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5871 if (tmp == NULL) goto failed;
5872 if (!PyList_Check(tmp)) {
5873 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5874 goto failed;
5875 }
5876 len = PyList_GET_SIZE(tmp);
5877 elts = asdl_seq_new(len, arena);
5878 if (elts == NULL) goto failed;
5879 for (i = 0; i < len; i++) {
5880 expr_ty value;
5881 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5882 if (res != 0) goto failed;
5883 asdl_seq_SET(elts, i, value);
5884 }
5885 Py_XDECREF(tmp);
5886 tmp = NULL;
5887 } else {
5888 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5889 return 1;
5890 }
5891 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5892 int res;
5893 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5894 if (tmp == NULL) goto failed;
5895 res = obj2ast_expr_context(tmp, &ctx, arena);
5896 if (res != 0) goto failed;
5897 Py_XDECREF(tmp);
5898 tmp = NULL;
5899 } else {
5900 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5901 return 1;
5902 }
5903 *out = List(elts, ctx, lineno, col_offset, arena);
5904 if (*out == NULL) goto failed;
5905 return 0;
5906 }
5907 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5908 if (isinstance == -1) {
5909 return 1;
5910 }
5911 if (isinstance) {
5912 asdl_seq* elts;
5913 expr_context_ty ctx;
5914
5915 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5916 int res;
5917 Py_ssize_t len;
5918 Py_ssize_t i;
5919 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5920 if (tmp == NULL) goto failed;
5921 if (!PyList_Check(tmp)) {
5922 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5923 goto failed;
5924 }
5925 len = PyList_GET_SIZE(tmp);
5926 elts = asdl_seq_new(len, arena);
5927 if (elts == NULL) goto failed;
5928 for (i = 0; i < len; i++) {
5929 expr_ty value;
5930 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5931 if (res != 0) goto failed;
5932 asdl_seq_SET(elts, i, value);
5933 }
5934 Py_XDECREF(tmp);
5935 tmp = NULL;
5936 } else {
5937 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5938 return 1;
5939 }
5940 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5941 int res;
5942 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5943 if (tmp == NULL) goto failed;
5944 res = obj2ast_expr_context(tmp, &ctx, arena);
5945 if (res != 0) goto failed;
5946 Py_XDECREF(tmp);
5947 tmp = NULL;
5948 } else {
5949 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5950 return 1;
5951 }
5952 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5953 if (*out == NULL) goto failed;
5954 return 0;
5955 }
5956
5957 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5958 failed:
5959 Py_XDECREF(tmp);
5960 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005961}
5962
5963int
5964obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5965{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005966 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005967
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005968 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5969 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005970 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005971 }
5972 if (isinstance) {
5973 *out = Load;
5974 return 0;
5975 }
5976 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5977 if (isinstance == -1) {
5978 return 1;
5979 }
5980 if (isinstance) {
5981 *out = Store;
5982 return 0;
5983 }
5984 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5985 if (isinstance == -1) {
5986 return 1;
5987 }
5988 if (isinstance) {
5989 *out = Del;
5990 return 0;
5991 }
5992 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5993 if (isinstance == -1) {
5994 return 1;
5995 }
5996 if (isinstance) {
5997 *out = AugLoad;
5998 return 0;
5999 }
6000 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6001 if (isinstance == -1) {
6002 return 1;
6003 }
6004 if (isinstance) {
6005 *out = AugStore;
6006 return 0;
6007 }
6008 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6009 if (isinstance == -1) {
6010 return 1;
6011 }
6012 if (isinstance) {
6013 *out = Param;
6014 return 0;
6015 }
6016
6017 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6018 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006019}
6020
6021int
6022obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6023{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006024 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006025
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006026 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006027
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006028 if (obj == Py_None) {
6029 *out = NULL;
6030 return 0;
6031 }
6032 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6033 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006034 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006035 }
6036 if (isinstance) {
6037 expr_ty lower;
6038 expr_ty upper;
6039 expr_ty step;
6040
6041 if (_PyObject_HasAttrId(obj, &PyId_lower)) {
6042 int res;
6043 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6044 if (tmp == NULL) goto failed;
6045 res = obj2ast_expr(tmp, &lower, arena);
6046 if (res != 0) goto failed;
6047 Py_XDECREF(tmp);
6048 tmp = NULL;
6049 } else {
6050 lower = NULL;
6051 }
6052 if (_PyObject_HasAttrId(obj, &PyId_upper)) {
6053 int res;
6054 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6055 if (tmp == NULL) goto failed;
6056 res = obj2ast_expr(tmp, &upper, arena);
6057 if (res != 0) goto failed;
6058 Py_XDECREF(tmp);
6059 tmp = NULL;
6060 } else {
6061 upper = NULL;
6062 }
6063 if (_PyObject_HasAttrId(obj, &PyId_step)) {
6064 int res;
6065 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6066 if (tmp == NULL) goto failed;
6067 res = obj2ast_expr(tmp, &step, arena);
6068 if (res != 0) goto failed;
6069 Py_XDECREF(tmp);
6070 tmp = NULL;
6071 } else {
6072 step = NULL;
6073 }
6074 *out = Slice(lower, upper, step, arena);
6075 if (*out == NULL) goto failed;
6076 return 0;
6077 }
6078 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6079 if (isinstance == -1) {
6080 return 1;
6081 }
6082 if (isinstance) {
6083 asdl_seq* dims;
6084
6085 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6086 int res;
6087 Py_ssize_t len;
6088 Py_ssize_t i;
6089 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6090 if (tmp == NULL) goto failed;
6091 if (!PyList_Check(tmp)) {
6092 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6093 goto failed;
6094 }
6095 len = PyList_GET_SIZE(tmp);
6096 dims = asdl_seq_new(len, arena);
6097 if (dims == NULL) goto failed;
6098 for (i = 0; i < len; i++) {
6099 slice_ty value;
6100 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6101 if (res != 0) goto failed;
6102 asdl_seq_SET(dims, i, value);
6103 }
6104 Py_XDECREF(tmp);
6105 tmp = NULL;
6106 } else {
6107 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6108 return 1;
6109 }
6110 *out = ExtSlice(dims, arena);
6111 if (*out == NULL) goto failed;
6112 return 0;
6113 }
6114 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6115 if (isinstance == -1) {
6116 return 1;
6117 }
6118 if (isinstance) {
6119 expr_ty value;
6120
6121 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6122 int res;
6123 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6124 if (tmp == NULL) goto failed;
6125 res = obj2ast_expr(tmp, &value, arena);
6126 if (res != 0) goto failed;
6127 Py_XDECREF(tmp);
6128 tmp = NULL;
6129 } else {
6130 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6131 return 1;
6132 }
6133 *out = Index(value, arena);
6134 if (*out == NULL) goto failed;
6135 return 0;
6136 }
6137
6138 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6139 failed:
6140 Py_XDECREF(tmp);
6141 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006142}
6143
6144int
6145obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6146{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006147 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006148
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006149 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6150 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006151 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006152 }
6153 if (isinstance) {
6154 *out = And;
6155 return 0;
6156 }
6157 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6158 if (isinstance == -1) {
6159 return 1;
6160 }
6161 if (isinstance) {
6162 *out = Or;
6163 return 0;
6164 }
6165
6166 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6167 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006168}
6169
6170int
6171obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6172{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006173 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006174
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006175 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6176 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006177 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006178 }
6179 if (isinstance) {
6180 *out = Add;
6181 return 0;
6182 }
6183 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6184 if (isinstance == -1) {
6185 return 1;
6186 }
6187 if (isinstance) {
6188 *out = Sub;
6189 return 0;
6190 }
6191 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6192 if (isinstance == -1) {
6193 return 1;
6194 }
6195 if (isinstance) {
6196 *out = Mult;
6197 return 0;
6198 }
6199 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6200 if (isinstance == -1) {
6201 return 1;
6202 }
6203 if (isinstance) {
6204 *out = Div;
6205 return 0;
6206 }
6207 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6208 if (isinstance == -1) {
6209 return 1;
6210 }
6211 if (isinstance) {
6212 *out = Mod;
6213 return 0;
6214 }
6215 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6216 if (isinstance == -1) {
6217 return 1;
6218 }
6219 if (isinstance) {
6220 *out = Pow;
6221 return 0;
6222 }
6223 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6224 if (isinstance == -1) {
6225 return 1;
6226 }
6227 if (isinstance) {
6228 *out = LShift;
6229 return 0;
6230 }
6231 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6232 if (isinstance == -1) {
6233 return 1;
6234 }
6235 if (isinstance) {
6236 *out = RShift;
6237 return 0;
6238 }
6239 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6240 if (isinstance == -1) {
6241 return 1;
6242 }
6243 if (isinstance) {
6244 *out = BitOr;
6245 return 0;
6246 }
6247 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6248 if (isinstance == -1) {
6249 return 1;
6250 }
6251 if (isinstance) {
6252 *out = BitXor;
6253 return 0;
6254 }
6255 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6256 if (isinstance == -1) {
6257 return 1;
6258 }
6259 if (isinstance) {
6260 *out = BitAnd;
6261 return 0;
6262 }
6263 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6264 if (isinstance == -1) {
6265 return 1;
6266 }
6267 if (isinstance) {
6268 *out = FloorDiv;
6269 return 0;
6270 }
6271
6272 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6273 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006274}
6275
6276int
6277obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6278{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006279 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006280
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006281 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6282 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006283 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006284 }
6285 if (isinstance) {
6286 *out = Invert;
6287 return 0;
6288 }
6289 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6290 if (isinstance == -1) {
6291 return 1;
6292 }
6293 if (isinstance) {
6294 *out = Not;
6295 return 0;
6296 }
6297 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6298 if (isinstance == -1) {
6299 return 1;
6300 }
6301 if (isinstance) {
6302 *out = UAdd;
6303 return 0;
6304 }
6305 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6306 if (isinstance == -1) {
6307 return 1;
6308 }
6309 if (isinstance) {
6310 *out = USub;
6311 return 0;
6312 }
6313
6314 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6315 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006316}
6317
6318int
6319obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6320{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006321 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006322
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006323 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6324 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006325 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006326 }
6327 if (isinstance) {
6328 *out = Eq;
6329 return 0;
6330 }
6331 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6332 if (isinstance == -1) {
6333 return 1;
6334 }
6335 if (isinstance) {
6336 *out = NotEq;
6337 return 0;
6338 }
6339 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6340 if (isinstance == -1) {
6341 return 1;
6342 }
6343 if (isinstance) {
6344 *out = Lt;
6345 return 0;
6346 }
6347 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6348 if (isinstance == -1) {
6349 return 1;
6350 }
6351 if (isinstance) {
6352 *out = LtE;
6353 return 0;
6354 }
6355 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6356 if (isinstance == -1) {
6357 return 1;
6358 }
6359 if (isinstance) {
6360 *out = Gt;
6361 return 0;
6362 }
6363 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6364 if (isinstance == -1) {
6365 return 1;
6366 }
6367 if (isinstance) {
6368 *out = GtE;
6369 return 0;
6370 }
6371 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6372 if (isinstance == -1) {
6373 return 1;
6374 }
6375 if (isinstance) {
6376 *out = Is;
6377 return 0;
6378 }
6379 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6380 if (isinstance == -1) {
6381 return 1;
6382 }
6383 if (isinstance) {
6384 *out = IsNot;
6385 return 0;
6386 }
6387 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6388 if (isinstance == -1) {
6389 return 1;
6390 }
6391 if (isinstance) {
6392 *out = In;
6393 return 0;
6394 }
6395 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6396 if (isinstance == -1) {
6397 return 1;
6398 }
6399 if (isinstance) {
6400 *out = NotIn;
6401 return 0;
6402 }
6403
6404 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6405 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006406}
6407
6408int
6409obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6410{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006411 PyObject* tmp = NULL;
6412 expr_ty target;
6413 expr_ty iter;
6414 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006415
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006416 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6417 int res;
6418 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6419 if (tmp == NULL) goto failed;
6420 res = obj2ast_expr(tmp, &target, arena);
6421 if (res != 0) goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006422 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006423 tmp = NULL;
6424 } else {
6425 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006426 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006427 }
6428 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6429 int res;
6430 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6431 if (tmp == NULL) goto failed;
6432 res = obj2ast_expr(tmp, &iter, arena);
6433 if (res != 0) goto failed;
6434 Py_XDECREF(tmp);
6435 tmp = NULL;
6436 } else {
6437 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6438 return 1;
6439 }
6440 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6441 int res;
6442 Py_ssize_t len;
6443 Py_ssize_t i;
6444 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6445 if (tmp == NULL) goto failed;
6446 if (!PyList_Check(tmp)) {
6447 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6448 goto failed;
6449 }
6450 len = PyList_GET_SIZE(tmp);
6451 ifs = asdl_seq_new(len, arena);
6452 if (ifs == NULL) goto failed;
6453 for (i = 0; i < len; i++) {
6454 expr_ty value;
6455 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6456 if (res != 0) goto failed;
6457 asdl_seq_SET(ifs, i, value);
6458 }
6459 Py_XDECREF(tmp);
6460 tmp = NULL;
6461 } else {
6462 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6463 return 1;
6464 }
6465 *out = comprehension(target, iter, ifs, arena);
6466 return 0;
6467failed:
6468 Py_XDECREF(tmp);
6469 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006470}
6471
6472int
6473obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6474{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006475 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006476
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006477 PyObject *tmp = NULL;
6478 int lineno;
6479 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006480
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006481 if (obj == Py_None) {
6482 *out = NULL;
6483 return 0;
6484 }
6485 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6486 int res;
6487 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6488 if (tmp == NULL) goto failed;
6489 res = obj2ast_int(tmp, &lineno, arena);
6490 if (res != 0) goto failed;
Benjamin Peterson0a4dae52010-11-21 15:12:34 +00006491 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006492 tmp = NULL;
6493 } else {
6494 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006495 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006496 }
6497 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6498 int res;
6499 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6500 if (tmp == NULL) goto failed;
6501 res = obj2ast_int(tmp, &col_offset, arena);
6502 if (res != 0) goto failed;
6503 Py_XDECREF(tmp);
6504 tmp = NULL;
6505 } else {
6506 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6507 return 1;
6508 }
6509 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6510 if (isinstance == -1) {
6511 return 1;
6512 }
6513 if (isinstance) {
6514 expr_ty type;
6515 identifier name;
6516 asdl_seq* body;
6517
6518 if (_PyObject_HasAttrId(obj, &PyId_type)) {
6519 int res;
6520 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6521 if (tmp == NULL) goto failed;
6522 res = obj2ast_expr(tmp, &type, arena);
6523 if (res != 0) goto failed;
6524 Py_XDECREF(tmp);
6525 tmp = NULL;
6526 } else {
6527 type = NULL;
6528 }
6529 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6530 int res;
6531 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6532 if (tmp == NULL) goto failed;
6533 res = obj2ast_identifier(tmp, &name, arena);
6534 if (res != 0) goto failed;
6535 Py_XDECREF(tmp);
6536 tmp = NULL;
6537 } else {
6538 name = NULL;
6539 }
6540 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6541 int res;
6542 Py_ssize_t len;
6543 Py_ssize_t i;
6544 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6545 if (tmp == NULL) goto failed;
6546 if (!PyList_Check(tmp)) {
6547 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6548 goto failed;
6549 }
6550 len = PyList_GET_SIZE(tmp);
6551 body = asdl_seq_new(len, arena);
6552 if (body == NULL) goto failed;
6553 for (i = 0; i < len; i++) {
6554 stmt_ty value;
6555 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6556 if (res != 0) goto failed;
6557 asdl_seq_SET(body, i, value);
6558 }
6559 Py_XDECREF(tmp);
6560 tmp = NULL;
6561 } else {
6562 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6563 return 1;
6564 }
6565 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6566 if (*out == NULL) goto failed;
6567 return 0;
6568 }
6569
6570 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6571 failed:
6572 Py_XDECREF(tmp);
6573 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006574}
6575
6576int
6577obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6578{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006579 PyObject* tmp = NULL;
6580 asdl_seq* args;
6581 identifier vararg;
6582 expr_ty varargannotation;
6583 asdl_seq* kwonlyargs;
6584 identifier kwarg;
6585 expr_ty kwargannotation;
6586 asdl_seq* defaults;
6587 asdl_seq* kw_defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006588
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6590 int res;
6591 Py_ssize_t len;
6592 Py_ssize_t i;
6593 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6594 if (tmp == NULL) goto failed;
6595 if (!PyList_Check(tmp)) {
6596 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6597 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006598 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006599 len = PyList_GET_SIZE(tmp);
6600 args = asdl_seq_new(len, arena);
6601 if (args == NULL) goto failed;
6602 for (i = 0; i < len; i++) {
6603 arg_ty value;
6604 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6605 if (res != 0) goto failed;
6606 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006607 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006608 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006609 tmp = NULL;
6610 } else {
6611 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006612 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006613 }
6614 if (_PyObject_HasAttrId(obj, &PyId_vararg)) {
6615 int res;
6616 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6617 if (tmp == NULL) goto failed;
6618 res = obj2ast_identifier(tmp, &vararg, arena);
6619 if (res != 0) goto failed;
6620 Py_XDECREF(tmp);
6621 tmp = NULL;
6622 } else {
6623 vararg = NULL;
6624 }
6625 if (_PyObject_HasAttrId(obj, &PyId_varargannotation)) {
6626 int res;
6627 tmp = _PyObject_GetAttrId(obj, &PyId_varargannotation);
6628 if (tmp == NULL) goto failed;
6629 res = obj2ast_expr(tmp, &varargannotation, arena);
6630 if (res != 0) goto failed;
6631 Py_XDECREF(tmp);
6632 tmp = NULL;
6633 } else {
6634 varargannotation = NULL;
6635 }
6636 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6637 int res;
6638 Py_ssize_t len;
6639 Py_ssize_t i;
6640 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6641 if (tmp == NULL) goto failed;
6642 if (!PyList_Check(tmp)) {
6643 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6644 goto failed;
6645 }
6646 len = PyList_GET_SIZE(tmp);
6647 kwonlyargs = asdl_seq_new(len, arena);
6648 if (kwonlyargs == NULL) goto failed;
6649 for (i = 0; i < len; i++) {
6650 arg_ty value;
6651 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6652 if (res != 0) goto failed;
6653 asdl_seq_SET(kwonlyargs, i, value);
6654 }
6655 Py_XDECREF(tmp);
6656 tmp = NULL;
6657 } else {
6658 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6659 return 1;
6660 }
6661 if (_PyObject_HasAttrId(obj, &PyId_kwarg)) {
6662 int res;
6663 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6664 if (tmp == NULL) goto failed;
6665 res = obj2ast_identifier(tmp, &kwarg, arena);
6666 if (res != 0) goto failed;
6667 Py_XDECREF(tmp);
6668 tmp = NULL;
6669 } else {
6670 kwarg = NULL;
6671 }
6672 if (_PyObject_HasAttrId(obj, &PyId_kwargannotation)) {
6673 int res;
6674 tmp = _PyObject_GetAttrId(obj, &PyId_kwargannotation);
6675 if (tmp == NULL) goto failed;
6676 res = obj2ast_expr(tmp, &kwargannotation, arena);
6677 if (res != 0) goto failed;
6678 Py_XDECREF(tmp);
6679 tmp = NULL;
6680 } else {
6681 kwargannotation = NULL;
6682 }
6683 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6684 int res;
6685 Py_ssize_t len;
6686 Py_ssize_t i;
6687 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6688 if (tmp == NULL) goto failed;
6689 if (!PyList_Check(tmp)) {
6690 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6691 goto failed;
6692 }
6693 len = PyList_GET_SIZE(tmp);
6694 defaults = asdl_seq_new(len, arena);
6695 if (defaults == NULL) goto failed;
6696 for (i = 0; i < len; i++) {
6697 expr_ty value;
6698 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6699 if (res != 0) goto failed;
6700 asdl_seq_SET(defaults, i, value);
6701 }
6702 Py_XDECREF(tmp);
6703 tmp = NULL;
6704 } else {
6705 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6706 return 1;
6707 }
6708 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6709 int res;
6710 Py_ssize_t len;
6711 Py_ssize_t i;
6712 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6713 if (tmp == NULL) goto failed;
6714 if (!PyList_Check(tmp)) {
6715 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6716 goto failed;
6717 }
6718 len = PyList_GET_SIZE(tmp);
6719 kw_defaults = asdl_seq_new(len, arena);
6720 if (kw_defaults == NULL) goto failed;
6721 for (i = 0; i < len; i++) {
6722 expr_ty value;
6723 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6724 if (res != 0) goto failed;
6725 asdl_seq_SET(kw_defaults, i, value);
6726 }
6727 Py_XDECREF(tmp);
6728 tmp = NULL;
6729 } else {
6730 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6731 return 1;
6732 }
6733 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6734 kwargannotation, defaults, kw_defaults, arena);
6735 return 0;
6736failed:
6737 Py_XDECREF(tmp);
6738 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006739}
6740
6741int
6742obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6743{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006744 PyObject* tmp = NULL;
6745 identifier arg;
6746 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006747
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006748 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6749 int res;
6750 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6751 if (tmp == NULL) goto failed;
6752 res = obj2ast_identifier(tmp, &arg, arena);
6753 if (res != 0) goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006754 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006755 tmp = NULL;
6756 } else {
6757 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006758 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006759 }
6760 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
6761 int res;
6762 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6763 if (tmp == NULL) goto failed;
6764 res = obj2ast_expr(tmp, &annotation, arena);
6765 if (res != 0) goto failed;
6766 Py_XDECREF(tmp);
6767 tmp = NULL;
6768 } else {
6769 annotation = NULL;
6770 }
6771 *out = arg(arg, annotation, arena);
6772 return 0;
6773failed:
6774 Py_XDECREF(tmp);
6775 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006776}
6777
6778int
6779obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6780{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006781 PyObject* tmp = NULL;
6782 identifier arg;
6783 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006784
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006785 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6786 int res;
6787 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6788 if (tmp == NULL) goto failed;
6789 res = obj2ast_identifier(tmp, &arg, arena);
6790 if (res != 0) goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006791 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006792 tmp = NULL;
6793 } else {
6794 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006795 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006796 }
6797 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6798 int res;
6799 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6800 if (tmp == NULL) goto failed;
6801 res = obj2ast_expr(tmp, &value, arena);
6802 if (res != 0) goto failed;
6803 Py_XDECREF(tmp);
6804 tmp = NULL;
6805 } else {
6806 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6807 return 1;
6808 }
6809 *out = keyword(arg, value, arena);
6810 return 0;
6811failed:
6812 Py_XDECREF(tmp);
6813 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006814}
6815
6816int
6817obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6818{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006819 PyObject* tmp = NULL;
6820 identifier name;
6821 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006822
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006823 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6824 int res;
6825 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6826 if (tmp == NULL) goto failed;
6827 res = obj2ast_identifier(tmp, &name, arena);
6828 if (res != 0) goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006829 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006830 tmp = NULL;
6831 } else {
6832 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006833 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006834 }
6835 if (_PyObject_HasAttrId(obj, &PyId_asname)) {
6836 int res;
6837 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6838 if (tmp == NULL) goto failed;
6839 res = obj2ast_identifier(tmp, &asname, arena);
6840 if (res != 0) goto failed;
6841 Py_XDECREF(tmp);
6842 tmp = NULL;
6843 } else {
6844 asname = NULL;
6845 }
6846 *out = alias(name, asname, arena);
6847 return 0;
6848failed:
6849 Py_XDECREF(tmp);
6850 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006851}
6852
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006853int
6854obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6855{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006856 PyObject* tmp = NULL;
6857 expr_ty context_expr;
6858 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006859
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006860 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6861 int res;
6862 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6863 if (tmp == NULL) goto failed;
6864 res = obj2ast_expr(tmp, &context_expr, arena);
6865 if (res != 0) goto failed;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006866 Py_XDECREF(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006867 tmp = NULL;
6868 } else {
6869 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006870 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006871 }
6872 if (_PyObject_HasAttrId(obj, &PyId_optional_vars)) {
6873 int res;
6874 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6875 if (tmp == NULL) goto failed;
6876 res = obj2ast_expr(tmp, &optional_vars, arena);
6877 if (res != 0) goto failed;
6878 Py_XDECREF(tmp);
6879 tmp = NULL;
6880 } else {
6881 optional_vars = NULL;
6882 }
6883 *out = withitem(context_expr, optional_vars, arena);
6884 return 0;
6885failed:
6886 Py_XDECREF(tmp);
6887 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006888}
6889
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006890
Martin v. Löwis1a214512008-06-11 05:26:20 +00006891static struct PyModuleDef _astmodule = {
6892 PyModuleDef_HEAD_INIT, "_ast"
6893};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006894PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006895PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006896{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006897 PyObject *m, *d;
6898 if (!init_types()) return NULL;
6899 m = PyModule_Create(&_astmodule);
6900 if (!m) return NULL;
6901 d = PyModule_GetDict(m);
6902 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
6903 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6904 return NULL;
6905 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6906 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6907 NULL;
6908 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6909 0) return NULL;
6910 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6911 return NULL;
6912 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6913 NULL;
6914 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6915 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6916 0) return NULL;
6917 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6918 return NULL;
6919 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6920 NULL;
6921 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6924 NULL;
6925 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6926 return NULL;
6927 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6928 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6929 NULL;
6930 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6931 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6932 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6933 NULL;
6934 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6935 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6936 NULL;
6937 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6938 NULL;
6939 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6940 return NULL;
6941 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6942 NULL;
6943 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6944 return NULL;
6945 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6946 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6947 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6948 NULL;
6949 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6950 return NULL;
6951 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6952 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6953 NULL;
6954 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6955 NULL;
6956 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6957 NULL;
6958 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6959 NULL;
6960 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6961 NULL;
6962 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6963 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6964 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6965 return NULL;
6966 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6967 NULL;
6968 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6969 return NULL;
6970 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6971 0) return NULL;
6972 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6973 NULL;
6974 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6975 return NULL;
6976 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6977 NULL;
6978 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6979 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6980 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6981 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6982 NULL;
6983 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6984 return NULL;
6985 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6986 return NULL;
6987 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6988 return NULL;
6989 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6990 NULL;
6991 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6992 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6993 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6994 NULL;
6995 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6996 0) return NULL;
6997 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6998 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6999 NULL;
7000 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7001 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7002 NULL;
7003 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7004 return NULL;
7005 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7006 NULL;
7007 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7008 NULL;
7009 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7010 NULL;
7011 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7012 return NULL;
7013 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7014 NULL;
7015 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7016 NULL;
7017 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7018 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7019 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7020 return NULL;
7021 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7022 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7023 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
7024 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7025 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7026 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7027 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7028 NULL;
7029 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7030 NULL;
7031 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7032 NULL;
7033 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7034 NULL;
7035 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7036 NULL;
7037 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7038 return NULL;
7039 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7040 NULL;
7041 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7042 NULL;
7043 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7044 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7045 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7046 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7047 NULL;
7048 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7049 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7050 NULL;
7051 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7052 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7053 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7054 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7055 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7056 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7057 NULL;
7058 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7059 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7060 NULL;
7061 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7062 < 0) return NULL;
7063 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7064 < 0) return NULL;
7065 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7066 < 0) return NULL;
7067 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7068 return NULL;
7069 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7070 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7071 NULL;
7072 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7073 NULL;
7074 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7075 return NULL;
7076 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007077}
7078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007079
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007080PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007081{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007082 init_types();
7083 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007084}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007085
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007086/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7087mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007088{
7089 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007090 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
7091 (PyObject*)Interactive_type};
7092 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007093 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007094 assert(0 <= mode && mode <= 2);
7095
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007096 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007097
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007098 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7099 if (isinstance == -1)
7100 return NULL;
7101 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007102 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7103 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007104 return NULL;
7105 }
7106 if (obj2ast_mod(ast, &res, arena) != 0)
7107 return NULL;
7108 else
7109 return res;
7110}
7111
7112int PyAST_Check(PyObject* obj)
7113{
7114 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00007115 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007116}
7117
Martin v. Löwis5b222132007-06-10 09:51:05 +00007118