blob: 7bf2c5092d3f31e93147ca2a25f41399fdde302b [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;
Christian Heimes70c94e72013-07-27 00:33:13 +0200691 if (obj) {
692 if (PyArena_AddPyObject(arena, obj) < 0) {
693 *out = NULL;
694 return -1;
695 }
696 Py_INCREF(obj);
697 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000698 *out = obj;
699 return 0;
700}
701
Benjamin Peterson180e6352011-07-22 11:09:07 -0500702static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500703{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500704 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
705 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500706 return 1;
707 }
708 return obj2ast_object(obj, out, arena);
709}
710
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500711static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
712{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400713 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500714 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
715 return 1;
716 }
717 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500718}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000719
Benjamin Petersone2498412011-08-09 16:08:39 -0500720static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
721{
722 if (!PyBytes_CheckExact(obj)) {
723 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
724 return 1;
725 }
726 return obj2ast_object(obj, out, arena);
727}
728
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000729static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
730{
731 int i;
732 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100733 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000734 return 1;
735 }
736
737 i = (int)PyLong_AsLong(obj);
738 if (i == -1 && PyErr_Occurred())
739 return 1;
740 *out = i;
741 return 0;
742}
743
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000744static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000745{
746 PyObject *empty_tuple, *d;
747 if (PyType_Ready(&AST_type) < 0)
748 return -1;
749 d = AST_type.tp_dict;
750 empty_tuple = PyTuple_New(0);
751 if (!empty_tuple ||
752 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
753 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
754 Py_XDECREF(empty_tuple);
755 return -1;
756 }
757 Py_DECREF(empty_tuple);
758 return 0;
759}
760
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000761
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762static int init_types(void)
763{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200764 static int initialized;
765 if (initialized) return 1;
766 if (add_ast_fields() < 0) return 0;
767 mod_type = make_type("mod", &AST_type, NULL, 0);
768 if (!mod_type) return 0;
769 if (!add_attributes(mod_type, NULL, 0)) return 0;
770 Module_type = make_type("Module", mod_type, Module_fields, 1);
771 if (!Module_type) return 0;
772 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
773 1);
774 if (!Interactive_type) return 0;
775 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
776 if (!Expression_type) return 0;
777 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
778 if (!Suite_type) return 0;
779 stmt_type = make_type("stmt", &AST_type, NULL, 0);
780 if (!stmt_type) return 0;
781 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
782 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
783 5);
784 if (!FunctionDef_type) return 0;
785 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
786 if (!ClassDef_type) return 0;
787 Return_type = make_type("Return", stmt_type, Return_fields, 1);
788 if (!Return_type) return 0;
789 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
790 if (!Delete_type) return 0;
791 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
792 if (!Assign_type) return 0;
793 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
794 if (!AugAssign_type) return 0;
795 For_type = make_type("For", stmt_type, For_fields, 4);
796 if (!For_type) return 0;
797 While_type = make_type("While", stmt_type, While_fields, 3);
798 if (!While_type) return 0;
799 If_type = make_type("If", stmt_type, If_fields, 3);
800 if (!If_type) return 0;
801 With_type = make_type("With", stmt_type, With_fields, 2);
802 if (!With_type) return 0;
803 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
804 if (!Raise_type) return 0;
805 Try_type = make_type("Try", stmt_type, Try_fields, 4);
806 if (!Try_type) return 0;
807 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
808 if (!Assert_type) return 0;
809 Import_type = make_type("Import", stmt_type, Import_fields, 1);
810 if (!Import_type) return 0;
811 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
812 if (!ImportFrom_type) return 0;
813 Global_type = make_type("Global", stmt_type, Global_fields, 1);
814 if (!Global_type) return 0;
815 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
816 if (!Nonlocal_type) return 0;
817 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
818 if (!Expr_type) return 0;
819 Pass_type = make_type("Pass", stmt_type, NULL, 0);
820 if (!Pass_type) return 0;
821 Break_type = make_type("Break", stmt_type, NULL, 0);
822 if (!Break_type) return 0;
823 Continue_type = make_type("Continue", stmt_type, NULL, 0);
824 if (!Continue_type) return 0;
825 expr_type = make_type("expr", &AST_type, NULL, 0);
826 if (!expr_type) return 0;
827 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
828 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
829 if (!BoolOp_type) return 0;
830 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
831 if (!BinOp_type) return 0;
832 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
833 if (!UnaryOp_type) return 0;
834 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
835 if (!Lambda_type) return 0;
836 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
837 if (!IfExp_type) return 0;
838 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
839 if (!Dict_type) return 0;
840 Set_type = make_type("Set", expr_type, Set_fields, 1);
841 if (!Set_type) return 0;
842 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
843 if (!ListComp_type) return 0;
844 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
845 if (!SetComp_type) return 0;
846 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
847 if (!DictComp_type) return 0;
848 GeneratorExp_type = make_type("GeneratorExp", expr_type,
849 GeneratorExp_fields, 2);
850 if (!GeneratorExp_type) return 0;
851 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
852 if (!Yield_type) return 0;
853 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
854 if (!YieldFrom_type) return 0;
855 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
856 if (!Compare_type) return 0;
857 Call_type = make_type("Call", expr_type, Call_fields, 5);
858 if (!Call_type) return 0;
859 Num_type = make_type("Num", expr_type, Num_fields, 1);
860 if (!Num_type) return 0;
861 Str_type = make_type("Str", expr_type, Str_fields, 1);
862 if (!Str_type) return 0;
863 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
864 if (!Bytes_type) return 0;
865 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
866 if (!Ellipsis_type) return 0;
867 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
868 if (!Attribute_type) return 0;
869 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
870 if (!Subscript_type) return 0;
871 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
872 if (!Starred_type) return 0;
873 Name_type = make_type("Name", expr_type, Name_fields, 2);
874 if (!Name_type) return 0;
875 List_type = make_type("List", expr_type, List_fields, 2);
876 if (!List_type) return 0;
877 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
878 if (!Tuple_type) return 0;
879 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
880 if (!expr_context_type) return 0;
881 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
882 Load_type = make_type("Load", expr_context_type, NULL, 0);
883 if (!Load_type) return 0;
884 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
885 if (!Load_singleton) return 0;
886 Store_type = make_type("Store", expr_context_type, NULL, 0);
887 if (!Store_type) return 0;
888 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
889 if (!Store_singleton) return 0;
890 Del_type = make_type("Del", expr_context_type, NULL, 0);
891 if (!Del_type) return 0;
892 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
893 if (!Del_singleton) return 0;
894 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
895 if (!AugLoad_type) return 0;
896 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
897 if (!AugLoad_singleton) return 0;
898 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
899 if (!AugStore_type) return 0;
900 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
901 if (!AugStore_singleton) return 0;
902 Param_type = make_type("Param", expr_context_type, NULL, 0);
903 if (!Param_type) return 0;
904 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
905 if (!Param_singleton) return 0;
906 slice_type = make_type("slice", &AST_type, NULL, 0);
907 if (!slice_type) return 0;
908 if (!add_attributes(slice_type, NULL, 0)) return 0;
909 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
910 if (!Slice_type) return 0;
911 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
912 if (!ExtSlice_type) return 0;
913 Index_type = make_type("Index", slice_type, Index_fields, 1);
914 if (!Index_type) return 0;
915 boolop_type = make_type("boolop", &AST_type, NULL, 0);
916 if (!boolop_type) return 0;
917 if (!add_attributes(boolop_type, NULL, 0)) return 0;
918 And_type = make_type("And", boolop_type, NULL, 0);
919 if (!And_type) return 0;
920 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
921 if (!And_singleton) return 0;
922 Or_type = make_type("Or", boolop_type, NULL, 0);
923 if (!Or_type) return 0;
924 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
925 if (!Or_singleton) return 0;
926 operator_type = make_type("operator", &AST_type, NULL, 0);
927 if (!operator_type) return 0;
928 if (!add_attributes(operator_type, NULL, 0)) return 0;
929 Add_type = make_type("Add", operator_type, NULL, 0);
930 if (!Add_type) return 0;
931 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
932 if (!Add_singleton) return 0;
933 Sub_type = make_type("Sub", operator_type, NULL, 0);
934 if (!Sub_type) return 0;
935 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
936 if (!Sub_singleton) return 0;
937 Mult_type = make_type("Mult", operator_type, NULL, 0);
938 if (!Mult_type) return 0;
939 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
940 if (!Mult_singleton) return 0;
941 Div_type = make_type("Div", operator_type, NULL, 0);
942 if (!Div_type) return 0;
943 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
944 if (!Div_singleton) return 0;
945 Mod_type = make_type("Mod", operator_type, NULL, 0);
946 if (!Mod_type) return 0;
947 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
948 if (!Mod_singleton) return 0;
949 Pow_type = make_type("Pow", operator_type, NULL, 0);
950 if (!Pow_type) return 0;
951 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
952 if (!Pow_singleton) return 0;
953 LShift_type = make_type("LShift", operator_type, NULL, 0);
954 if (!LShift_type) return 0;
955 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
956 if (!LShift_singleton) return 0;
957 RShift_type = make_type("RShift", operator_type, NULL, 0);
958 if (!RShift_type) return 0;
959 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
960 if (!RShift_singleton) return 0;
961 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
962 if (!BitOr_type) return 0;
963 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
964 if (!BitOr_singleton) return 0;
965 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
966 if (!BitXor_type) return 0;
967 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
968 if (!BitXor_singleton) return 0;
969 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
970 if (!BitAnd_type) return 0;
971 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
972 if (!BitAnd_singleton) return 0;
973 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
974 if (!FloorDiv_type) return 0;
975 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
976 if (!FloorDiv_singleton) return 0;
977 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
978 if (!unaryop_type) return 0;
979 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
980 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
981 if (!Invert_type) return 0;
982 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
983 if (!Invert_singleton) return 0;
984 Not_type = make_type("Not", unaryop_type, NULL, 0);
985 if (!Not_type) return 0;
986 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
987 if (!Not_singleton) return 0;
988 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
989 if (!UAdd_type) return 0;
990 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
991 if (!UAdd_singleton) return 0;
992 USub_type = make_type("USub", unaryop_type, NULL, 0);
993 if (!USub_type) return 0;
994 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
995 if (!USub_singleton) return 0;
996 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
997 if (!cmpop_type) return 0;
998 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
999 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1000 if (!Eq_type) return 0;
1001 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1002 if (!Eq_singleton) return 0;
1003 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1004 if (!NotEq_type) return 0;
1005 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1006 if (!NotEq_singleton) return 0;
1007 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1008 if (!Lt_type) return 0;
1009 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1010 if (!Lt_singleton) return 0;
1011 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1012 if (!LtE_type) return 0;
1013 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1014 if (!LtE_singleton) return 0;
1015 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1016 if (!Gt_type) return 0;
1017 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1018 if (!Gt_singleton) return 0;
1019 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1020 if (!GtE_type) return 0;
1021 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1022 if (!GtE_singleton) return 0;
1023 Is_type = make_type("Is", cmpop_type, NULL, 0);
1024 if (!Is_type) return 0;
1025 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1026 if (!Is_singleton) return 0;
1027 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1028 if (!IsNot_type) return 0;
1029 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1030 if (!IsNot_singleton) return 0;
1031 In_type = make_type("In", cmpop_type, NULL, 0);
1032 if (!In_type) return 0;
1033 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1034 if (!In_singleton) return 0;
1035 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1036 if (!NotIn_type) return 0;
1037 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1038 if (!NotIn_singleton) return 0;
1039 comprehension_type = make_type("comprehension", &AST_type,
1040 comprehension_fields, 3);
1041 if (!comprehension_type) return 0;
1042 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1043 if (!excepthandler_type) return 0;
1044 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1045 return 0;
1046 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1047 ExceptHandler_fields, 3);
1048 if (!ExceptHandler_type) return 0;
1049 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
1050 if (!arguments_type) return 0;
1051 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1052 if (!arg_type) return 0;
1053 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1054 if (!keyword_type) return 0;
1055 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1056 if (!alias_type) return 0;
1057 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1058 if (!withitem_type) return 0;
1059 initialized = 1;
1060 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001061}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001062
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001063static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1064static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1065static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1066static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1067 arena);
1068static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1069static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1070static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1071static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1072static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1073static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1074 arena);
1075static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1076 arena);
1077static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1078static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1079static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1080static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001081static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001084Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001086 mod_ty p;
1087 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1088 if (!p)
1089 return NULL;
1090 p->kind = Module_kind;
1091 p->v.Module.body = body;
1092 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093}
1094
1095mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001098 mod_ty p;
1099 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1100 if (!p)
1101 return NULL;
1102 p->kind = Interactive_kind;
1103 p->v.Interactive.body = body;
1104 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105}
1106
1107mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001108Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001110 mod_ty p;
1111 if (!body) {
1112 PyErr_SetString(PyExc_ValueError,
1113 "field body is required for Expression");
1114 return NULL;
1115 }
1116 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1117 if (!p)
1118 return NULL;
1119 p->kind = Expression_kind;
1120 p->v.Expression.body = body;
1121 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122}
1123
1124mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001127 mod_ty p;
1128 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1129 if (!p)
1130 return NULL;
1131 p->kind = Suite_kind;
1132 p->v.Suite.body = body;
1133 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134}
1135
1136stmt_ty
1137FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001138 decorator_list, expr_ty returns, int lineno, int col_offset,
1139 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001141 stmt_ty p;
1142 if (!name) {
1143 PyErr_SetString(PyExc_ValueError,
1144 "field name is required for FunctionDef");
1145 return NULL;
1146 }
1147 if (!args) {
1148 PyErr_SetString(PyExc_ValueError,
1149 "field args is required for FunctionDef");
1150 return NULL;
1151 }
1152 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1153 if (!p)
1154 return NULL;
1155 p->kind = FunctionDef_kind;
1156 p->v.FunctionDef.name = name;
1157 p->v.FunctionDef.args = args;
1158 p->v.FunctionDef.body = body;
1159 p->v.FunctionDef.decorator_list = decorator_list;
1160 p->v.FunctionDef.returns = returns;
1161 p->lineno = lineno;
1162 p->col_offset = col_offset;
1163 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164}
1165
1166stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001167ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001168 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1169 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001171 stmt_ty p;
1172 if (!name) {
1173 PyErr_SetString(PyExc_ValueError,
1174 "field name is required for ClassDef");
1175 return NULL;
1176 }
1177 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1178 if (!p)
1179 return NULL;
1180 p->kind = ClassDef_kind;
1181 p->v.ClassDef.name = name;
1182 p->v.ClassDef.bases = bases;
1183 p->v.ClassDef.keywords = keywords;
1184 p->v.ClassDef.starargs = starargs;
1185 p->v.ClassDef.kwargs = kwargs;
1186 p->v.ClassDef.body = body;
1187 p->v.ClassDef.decorator_list = decorator_list;
1188 p->lineno = lineno;
1189 p->col_offset = col_offset;
1190 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191}
1192
1193stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001194Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001196 stmt_ty p;
1197 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1198 if (!p)
1199 return NULL;
1200 p->kind = Return_kind;
1201 p->v.Return.value = value;
1202 p->lineno = lineno;
1203 p->col_offset = col_offset;
1204 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205}
1206
1207stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001210 stmt_ty p;
1211 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1212 if (!p)
1213 return NULL;
1214 p->kind = Delete_kind;
1215 p->v.Delete.targets = targets;
1216 p->lineno = lineno;
1217 p->col_offset = col_offset;
1218 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219}
1220
1221stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1223 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001225 stmt_ty p;
1226 if (!value) {
1227 PyErr_SetString(PyExc_ValueError,
1228 "field value is required for Assign");
1229 return NULL;
1230 }
1231 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1232 if (!p)
1233 return NULL;
1234 p->kind = Assign_kind;
1235 p->v.Assign.targets = targets;
1236 p->v.Assign.value = value;
1237 p->lineno = lineno;
1238 p->col_offset = col_offset;
1239 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240}
1241
1242stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001243AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1244 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001246 stmt_ty p;
1247 if (!target) {
1248 PyErr_SetString(PyExc_ValueError,
1249 "field target is required for AugAssign");
1250 return NULL;
1251 }
1252 if (!op) {
1253 PyErr_SetString(PyExc_ValueError,
1254 "field op is required for AugAssign");
1255 return NULL;
1256 }
1257 if (!value) {
1258 PyErr_SetString(PyExc_ValueError,
1259 "field value is required for AugAssign");
1260 return NULL;
1261 }
1262 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1263 if (!p)
1264 return NULL;
1265 p->kind = AugAssign_kind;
1266 p->v.AugAssign.target = target;
1267 p->v.AugAssign.op = op;
1268 p->v.AugAssign.value = value;
1269 p->lineno = lineno;
1270 p->col_offset = col_offset;
1271 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272}
1273
1274stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001276 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001278 stmt_ty p;
1279 if (!target) {
1280 PyErr_SetString(PyExc_ValueError,
1281 "field target is required for For");
1282 return NULL;
1283 }
1284 if (!iter) {
1285 PyErr_SetString(PyExc_ValueError,
1286 "field iter is required for For");
1287 return NULL;
1288 }
1289 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1290 if (!p)
1291 return NULL;
1292 p->kind = For_kind;
1293 p->v.For.target = target;
1294 p->v.For.iter = iter;
1295 p->v.For.body = body;
1296 p->v.For.orelse = orelse;
1297 p->lineno = lineno;
1298 p->col_offset = col_offset;
1299 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300}
1301
1302stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001303While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1304 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001306 stmt_ty p;
1307 if (!test) {
1308 PyErr_SetString(PyExc_ValueError,
1309 "field test is required for While");
1310 return NULL;
1311 }
1312 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1313 if (!p)
1314 return NULL;
1315 p->kind = While_kind;
1316 p->v.While.test = test;
1317 p->v.While.body = body;
1318 p->v.While.orelse = orelse;
1319 p->lineno = lineno;
1320 p->col_offset = col_offset;
1321 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322}
1323
1324stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001325If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1326 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001328 stmt_ty p;
1329 if (!test) {
1330 PyErr_SetString(PyExc_ValueError,
1331 "field test is required for If");
1332 return NULL;
1333 }
1334 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1335 if (!p)
1336 return NULL;
1337 p->kind = If_kind;
1338 p->v.If.test = test;
1339 p->v.If.body = body;
1340 p->v.If.orelse = orelse;
1341 p->lineno = lineno;
1342 p->col_offset = col_offset;
1343 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344}
1345
1346stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001347With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1348 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001349{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001350 stmt_ty p;
1351 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1352 if (!p)
1353 return NULL;
1354 p->kind = With_kind;
1355 p->v.With.items = items;
1356 p->v.With.body = body;
1357 p->lineno = lineno;
1358 p->col_offset = col_offset;
1359 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001360}
1361
1362stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001363Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001365 stmt_ty p;
1366 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1367 if (!p)
1368 return NULL;
1369 p->kind = Raise_kind;
1370 p->v.Raise.exc = exc;
1371 p->v.Raise.cause = cause;
1372 p->lineno = lineno;
1373 p->col_offset = col_offset;
1374 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375}
1376
1377stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001378Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1379 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001381 stmt_ty p;
1382 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1383 if (!p)
1384 return NULL;
1385 p->kind = Try_kind;
1386 p->v.Try.body = body;
1387 p->v.Try.handlers = handlers;
1388 p->v.Try.orelse = orelse;
1389 p->v.Try.finalbody = finalbody;
1390 p->lineno = lineno;
1391 p->col_offset = col_offset;
1392 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393}
1394
1395stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001396Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001398 stmt_ty p;
1399 if (!test) {
1400 PyErr_SetString(PyExc_ValueError,
1401 "field test is required for Assert");
1402 return NULL;
1403 }
1404 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1405 if (!p)
1406 return NULL;
1407 p->kind = Assert_kind;
1408 p->v.Assert.test = test;
1409 p->v.Assert.msg = msg;
1410 p->lineno = lineno;
1411 p->col_offset = col_offset;
1412 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413}
1414
1415stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001418 stmt_ty p;
1419 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1420 if (!p)
1421 return NULL;
1422 p->kind = Import_kind;
1423 p->v.Import.names = names;
1424 p->lineno = lineno;
1425 p->col_offset = col_offset;
1426 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427}
1428
1429stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001430ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1431 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001433 stmt_ty p;
1434 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1435 if (!p)
1436 return NULL;
1437 p->kind = ImportFrom_kind;
1438 p->v.ImportFrom.module = module;
1439 p->v.ImportFrom.names = names;
1440 p->v.ImportFrom.level = level;
1441 p->lineno = lineno;
1442 p->col_offset = col_offset;
1443 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
1446stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001447Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001449 stmt_ty p;
1450 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1451 if (!p)
1452 return NULL;
1453 p->kind = Global_kind;
1454 p->v.Global.names = names;
1455 p->lineno = lineno;
1456 p->col_offset = col_offset;
1457 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458}
1459
1460stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001461Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1462{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001463 stmt_ty p;
1464 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1465 if (!p)
1466 return NULL;
1467 p->kind = Nonlocal_kind;
1468 p->v.Nonlocal.names = names;
1469 p->lineno = lineno;
1470 p->col_offset = col_offset;
1471 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001472}
1473
1474stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001475Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001477 stmt_ty p;
1478 if (!value) {
1479 PyErr_SetString(PyExc_ValueError,
1480 "field value is required for Expr");
1481 return NULL;
1482 }
1483 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1484 if (!p)
1485 return NULL;
1486 p->kind = Expr_kind;
1487 p->v.Expr.value = value;
1488 p->lineno = lineno;
1489 p->col_offset = col_offset;
1490 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491}
1492
1493stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001496 stmt_ty p;
1497 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1498 if (!p)
1499 return NULL;
1500 p->kind = Pass_kind;
1501 p->lineno = lineno;
1502 p->col_offset = col_offset;
1503 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
1506stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001507Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001509 stmt_ty p;
1510 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1511 if (!p)
1512 return NULL;
1513 p->kind = Break_kind;
1514 p->lineno = lineno;
1515 p->col_offset = col_offset;
1516 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517}
1518
1519stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001520Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001522 stmt_ty p;
1523 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1524 if (!p)
1525 return NULL;
1526 p->kind = Continue_kind;
1527 p->lineno = lineno;
1528 p->col_offset = col_offset;
1529 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
1531
1532expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001533BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1534 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001536 expr_ty p;
1537 if (!op) {
1538 PyErr_SetString(PyExc_ValueError,
1539 "field op is required for BoolOp");
1540 return NULL;
1541 }
1542 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1543 if (!p)
1544 return NULL;
1545 p->kind = BoolOp_kind;
1546 p->v.BoolOp.op = op;
1547 p->v.BoolOp.values = values;
1548 p->lineno = lineno;
1549 p->col_offset = col_offset;
1550 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551}
1552
1553expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001554BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1555 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001557 expr_ty p;
1558 if (!left) {
1559 PyErr_SetString(PyExc_ValueError,
1560 "field left is required for BinOp");
1561 return NULL;
1562 }
1563 if (!op) {
1564 PyErr_SetString(PyExc_ValueError,
1565 "field op is required for BinOp");
1566 return NULL;
1567 }
1568 if (!right) {
1569 PyErr_SetString(PyExc_ValueError,
1570 "field right is required for BinOp");
1571 return NULL;
1572 }
1573 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1574 if (!p)
1575 return NULL;
1576 p->kind = BinOp_kind;
1577 p->v.BinOp.left = left;
1578 p->v.BinOp.op = op;
1579 p->v.BinOp.right = right;
1580 p->lineno = lineno;
1581 p->col_offset = col_offset;
1582 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583}
1584
1585expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001586UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1587 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001589 expr_ty p;
1590 if (!op) {
1591 PyErr_SetString(PyExc_ValueError,
1592 "field op is required for UnaryOp");
1593 return NULL;
1594 }
1595 if (!operand) {
1596 PyErr_SetString(PyExc_ValueError,
1597 "field operand is required for UnaryOp");
1598 return NULL;
1599 }
1600 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1601 if (!p)
1602 return NULL;
1603 p->kind = UnaryOp_kind;
1604 p->v.UnaryOp.op = op;
1605 p->v.UnaryOp.operand = operand;
1606 p->lineno = lineno;
1607 p->col_offset = col_offset;
1608 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609}
1610
1611expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001612Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1613 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001615 expr_ty p;
1616 if (!args) {
1617 PyErr_SetString(PyExc_ValueError,
1618 "field args is required for Lambda");
1619 return NULL;
1620 }
1621 if (!body) {
1622 PyErr_SetString(PyExc_ValueError,
1623 "field body is required for Lambda");
1624 return NULL;
1625 }
1626 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1627 if (!p)
1628 return NULL;
1629 p->kind = Lambda_kind;
1630 p->v.Lambda.args = args;
1631 p->v.Lambda.body = body;
1632 p->lineno = lineno;
1633 p->col_offset = col_offset;
1634 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635}
1636
1637expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001638IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1639 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001640{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001641 expr_ty p;
1642 if (!test) {
1643 PyErr_SetString(PyExc_ValueError,
1644 "field test is required for IfExp");
1645 return NULL;
1646 }
1647 if (!body) {
1648 PyErr_SetString(PyExc_ValueError,
1649 "field body is required for IfExp");
1650 return NULL;
1651 }
1652 if (!orelse) {
1653 PyErr_SetString(PyExc_ValueError,
1654 "field orelse is required for IfExp");
1655 return NULL;
1656 }
1657 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1658 if (!p)
1659 return NULL;
1660 p->kind = IfExp_kind;
1661 p->v.IfExp.test = test;
1662 p->v.IfExp.body = body;
1663 p->v.IfExp.orelse = orelse;
1664 p->lineno = lineno;
1665 p->col_offset = col_offset;
1666 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001667}
1668
1669expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001670Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1671 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001673 expr_ty p;
1674 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1675 if (!p)
1676 return NULL;
1677 p->kind = Dict_kind;
1678 p->v.Dict.keys = keys;
1679 p->v.Dict.values = values;
1680 p->lineno = lineno;
1681 p->col_offset = col_offset;
1682 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683}
1684
1685expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001686Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1687{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001688 expr_ty p;
1689 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1690 if (!p)
1691 return NULL;
1692 p->kind = Set_kind;
1693 p->v.Set.elts = elts;
1694 p->lineno = lineno;
1695 p->col_offset = col_offset;
1696 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001697}
1698
1699expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001700ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1701 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001703 expr_ty p;
1704 if (!elt) {
1705 PyErr_SetString(PyExc_ValueError,
1706 "field elt is required for ListComp");
1707 return NULL;
1708 }
1709 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1710 if (!p)
1711 return NULL;
1712 p->kind = ListComp_kind;
1713 p->v.ListComp.elt = elt;
1714 p->v.ListComp.generators = generators;
1715 p->lineno = lineno;
1716 p->col_offset = col_offset;
1717 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
1720expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1722 *arena)
1723{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001724 expr_ty p;
1725 if (!elt) {
1726 PyErr_SetString(PyExc_ValueError,
1727 "field elt is required for SetComp");
1728 return NULL;
1729 }
1730 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1731 if (!p)
1732 return NULL;
1733 p->kind = SetComp_kind;
1734 p->v.SetComp.elt = elt;
1735 p->v.SetComp.generators = generators;
1736 p->lineno = lineno;
1737 p->col_offset = col_offset;
1738 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001739}
1740
1741expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001742DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1743 col_offset, PyArena *arena)
1744{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001745 expr_ty p;
1746 if (!key) {
1747 PyErr_SetString(PyExc_ValueError,
1748 "field key is required for DictComp");
1749 return NULL;
1750 }
1751 if (!value) {
1752 PyErr_SetString(PyExc_ValueError,
1753 "field value is required for DictComp");
1754 return NULL;
1755 }
1756 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1757 if (!p)
1758 return NULL;
1759 p->kind = DictComp_kind;
1760 p->v.DictComp.key = key;
1761 p->v.DictComp.value = value;
1762 p->v.DictComp.generators = generators;
1763 p->lineno = lineno;
1764 p->col_offset = col_offset;
1765 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766}
1767
1768expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001769GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1770 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001772 expr_ty p;
1773 if (!elt) {
1774 PyErr_SetString(PyExc_ValueError,
1775 "field elt is required for GeneratorExp");
1776 return NULL;
1777 }
1778 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1779 if (!p)
1780 return NULL;
1781 p->kind = GeneratorExp_kind;
1782 p->v.GeneratorExp.elt = elt;
1783 p->v.GeneratorExp.generators = generators;
1784 p->lineno = lineno;
1785 p->col_offset = col_offset;
1786 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787}
1788
1789expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001790Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001792 expr_ty p;
1793 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1794 if (!p)
1795 return NULL;
1796 p->kind = Yield_kind;
1797 p->v.Yield.value = value;
1798 p->lineno = lineno;
1799 p->col_offset = col_offset;
1800 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801}
1802
1803expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001804YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001806 expr_ty p;
1807 if (!value) {
1808 PyErr_SetString(PyExc_ValueError,
1809 "field value is required for YieldFrom");
1810 return NULL;
1811 }
1812 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1813 if (!p)
1814 return NULL;
1815 p->kind = YieldFrom_kind;
1816 p->v.YieldFrom.value = value;
1817 p->lineno = lineno;
1818 p->col_offset = col_offset;
1819 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001820}
1821
1822expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001823Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1824 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001826 expr_ty p;
1827 if (!left) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "field left is required for Compare");
1830 return NULL;
1831 }
1832 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1833 if (!p)
1834 return NULL;
1835 p->kind = Compare_kind;
1836 p->v.Compare.left = left;
1837 p->v.Compare.ops = ops;
1838 p->v.Compare.comparators = comparators;
1839 p->lineno = lineno;
1840 p->col_offset = col_offset;
1841 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842}
1843
1844expr_ty
1845Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001846 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001848 expr_ty p;
1849 if (!func) {
1850 PyErr_SetString(PyExc_ValueError,
1851 "field func is required for Call");
1852 return NULL;
1853 }
1854 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1855 if (!p)
1856 return NULL;
1857 p->kind = Call_kind;
1858 p->v.Call.func = func;
1859 p->v.Call.args = args;
1860 p->v.Call.keywords = keywords;
1861 p->v.Call.starargs = starargs;
1862 p->v.Call.kwargs = kwargs;
1863 p->lineno = lineno;
1864 p->col_offset = col_offset;
1865 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866}
1867
1868expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001869Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001871 expr_ty p;
1872 if (!n) {
1873 PyErr_SetString(PyExc_ValueError,
1874 "field n is required for Num");
1875 return NULL;
1876 }
1877 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1878 if (!p)
1879 return NULL;
1880 p->kind = Num_kind;
1881 p->v.Num.n = n;
1882 p->lineno = lineno;
1883 p->col_offset = col_offset;
1884 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885}
1886
1887expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001888Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001890 expr_ty p;
1891 if (!s) {
1892 PyErr_SetString(PyExc_ValueError,
1893 "field s is required for Str");
1894 return NULL;
1895 }
1896 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1897 if (!p)
1898 return NULL;
1899 p->kind = Str_kind;
1900 p->v.Str.s = s;
1901 p->lineno = lineno;
1902 p->col_offset = col_offset;
1903 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904}
1905
1906expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001907Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001908{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001909 expr_ty p;
1910 if (!s) {
1911 PyErr_SetString(PyExc_ValueError,
1912 "field s is required for Bytes");
1913 return NULL;
1914 }
1915 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1916 if (!p)
1917 return NULL;
1918 p->kind = Bytes_kind;
1919 p->v.Bytes.s = s;
1920 p->lineno = lineno;
1921 p->col_offset = col_offset;
1922 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001923}
1924
1925expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001926Ellipsis(int lineno, int col_offset, PyArena *arena)
1927{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001928 expr_ty p;
1929 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1930 if (!p)
1931 return NULL;
1932 p->kind = Ellipsis_kind;
1933 p->lineno = lineno;
1934 p->col_offset = col_offset;
1935 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001936}
1937
1938expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001939Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1940 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001942 expr_ty p;
1943 if (!value) {
1944 PyErr_SetString(PyExc_ValueError,
1945 "field value is required for Attribute");
1946 return NULL;
1947 }
1948 if (!attr) {
1949 PyErr_SetString(PyExc_ValueError,
1950 "field attr is required for Attribute");
1951 return NULL;
1952 }
1953 if (!ctx) {
1954 PyErr_SetString(PyExc_ValueError,
1955 "field ctx is required for Attribute");
1956 return NULL;
1957 }
1958 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1959 if (!p)
1960 return NULL;
1961 p->kind = Attribute_kind;
1962 p->v.Attribute.value = value;
1963 p->v.Attribute.attr = attr;
1964 p->v.Attribute.ctx = ctx;
1965 p->lineno = lineno;
1966 p->col_offset = col_offset;
1967 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968}
1969
1970expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001971Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1972 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001974 expr_ty p;
1975 if (!value) {
1976 PyErr_SetString(PyExc_ValueError,
1977 "field value is required for Subscript");
1978 return NULL;
1979 }
1980 if (!slice) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field slice is required for Subscript");
1983 return NULL;
1984 }
1985 if (!ctx) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "field ctx is required for Subscript");
1988 return NULL;
1989 }
1990 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1991 if (!p)
1992 return NULL;
1993 p->kind = Subscript_kind;
1994 p->v.Subscript.value = value;
1995 p->v.Subscript.slice = slice;
1996 p->v.Subscript.ctx = ctx;
1997 p->lineno = lineno;
1998 p->col_offset = col_offset;
1999 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000}
2001
2002expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002003Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2004 *arena)
2005{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002006 expr_ty p;
2007 if (!value) {
2008 PyErr_SetString(PyExc_ValueError,
2009 "field value is required for Starred");
2010 return NULL;
2011 }
2012 if (!ctx) {
2013 PyErr_SetString(PyExc_ValueError,
2014 "field ctx is required for Starred");
2015 return NULL;
2016 }
2017 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2018 if (!p)
2019 return NULL;
2020 p->kind = Starred_kind;
2021 p->v.Starred.value = value;
2022 p->v.Starred.ctx = ctx;
2023 p->lineno = lineno;
2024 p->col_offset = col_offset;
2025 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002026}
2027
2028expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002029Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2030 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002032 expr_ty p;
2033 if (!id) {
2034 PyErr_SetString(PyExc_ValueError,
2035 "field id is required for Name");
2036 return NULL;
2037 }
2038 if (!ctx) {
2039 PyErr_SetString(PyExc_ValueError,
2040 "field ctx is required for Name");
2041 return NULL;
2042 }
2043 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2044 if (!p)
2045 return NULL;
2046 p->kind = Name_kind;
2047 p->v.Name.id = id;
2048 p->v.Name.ctx = ctx;
2049 p->lineno = lineno;
2050 p->col_offset = col_offset;
2051 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052}
2053
2054expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002055List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2056 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002058 expr_ty p;
2059 if (!ctx) {
2060 PyErr_SetString(PyExc_ValueError,
2061 "field ctx is required for List");
2062 return NULL;
2063 }
2064 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2065 if (!p)
2066 return NULL;
2067 p->kind = List_kind;
2068 p->v.List.elts = elts;
2069 p->v.List.ctx = ctx;
2070 p->lineno = lineno;
2071 p->col_offset = col_offset;
2072 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073}
2074
2075expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002076Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2077 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002079 expr_ty p;
2080 if (!ctx) {
2081 PyErr_SetString(PyExc_ValueError,
2082 "field ctx is required for Tuple");
2083 return NULL;
2084 }
2085 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2086 if (!p)
2087 return NULL;
2088 p->kind = Tuple_kind;
2089 p->v.Tuple.elts = elts;
2090 p->v.Tuple.ctx = ctx;
2091 p->lineno = lineno;
2092 p->col_offset = col_offset;
2093 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094}
2095
2096slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002099 slice_ty p;
2100 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2101 if (!p)
2102 return NULL;
2103 p->kind = Slice_kind;
2104 p->v.Slice.lower = lower;
2105 p->v.Slice.upper = upper;
2106 p->v.Slice.step = step;
2107 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108}
2109
2110slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002111ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002113 slice_ty p;
2114 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2115 if (!p)
2116 return NULL;
2117 p->kind = ExtSlice_kind;
2118 p->v.ExtSlice.dims = dims;
2119 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120}
2121
2122slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002123Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002125 slice_ty p;
2126 if (!value) {
2127 PyErr_SetString(PyExc_ValueError,
2128 "field value is required for Index");
2129 return NULL;
2130 }
2131 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2132 if (!p)
2133 return NULL;
2134 p->kind = Index_kind;
2135 p->v.Index.value = value;
2136 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137}
2138
2139comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002140comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002142 comprehension_ty p;
2143 if (!target) {
2144 PyErr_SetString(PyExc_ValueError,
2145 "field target is required for comprehension");
2146 return NULL;
2147 }
2148 if (!iter) {
2149 PyErr_SetString(PyExc_ValueError,
2150 "field iter is required for comprehension");
2151 return NULL;
2152 }
2153 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2154 if (!p)
2155 return NULL;
2156 p->target = target;
2157 p->iter = iter;
2158 p->ifs = ifs;
2159 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160}
2161
2162excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002163ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002164 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002166 excepthandler_ty p;
2167 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2168 if (!p)
2169 return NULL;
2170 p->kind = ExceptHandler_kind;
2171 p->v.ExceptHandler.type = type;
2172 p->v.ExceptHandler.name = name;
2173 p->v.ExceptHandler.body = body;
2174 p->lineno = lineno;
2175 p->col_offset = col_offset;
2176 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177}
2178
2179arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002180arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2181 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2182 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002184 arguments_ty p;
2185 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2186 if (!p)
2187 return NULL;
2188 p->args = args;
2189 p->vararg = vararg;
2190 p->varargannotation = varargannotation;
2191 p->kwonlyargs = kwonlyargs;
2192 p->kwarg = kwarg;
2193 p->kwargannotation = kwargannotation;
2194 p->defaults = defaults;
2195 p->kw_defaults = kw_defaults;
2196 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197}
2198
Neal Norwitzc1505362006-12-28 06:47:50 +00002199arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002200arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002201{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002202 arg_ty p;
2203 if (!arg) {
2204 PyErr_SetString(PyExc_ValueError,
2205 "field arg is required for arg");
2206 return NULL;
2207 }
2208 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2209 if (!p)
2210 return NULL;
2211 p->arg = arg;
2212 p->annotation = annotation;
2213 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002214}
2215
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002217keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002219 keyword_ty p;
2220 if (!arg) {
2221 PyErr_SetString(PyExc_ValueError,
2222 "field arg is required for keyword");
2223 return NULL;
2224 }
2225 if (!value) {
2226 PyErr_SetString(PyExc_ValueError,
2227 "field value is required for keyword");
2228 return NULL;
2229 }
2230 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2231 if (!p)
2232 return NULL;
2233 p->arg = arg;
2234 p->value = value;
2235 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236}
2237
2238alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002239alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002241 alias_ty p;
2242 if (!name) {
2243 PyErr_SetString(PyExc_ValueError,
2244 "field name is required for alias");
2245 return NULL;
2246 }
2247 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2248 if (!p)
2249 return NULL;
2250 p->name = name;
2251 p->asname = asname;
2252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253}
2254
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002255withitem_ty
2256withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2257{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002258 withitem_ty p;
2259 if (!context_expr) {
2260 PyErr_SetString(PyExc_ValueError,
2261 "field context_expr is required for withitem");
2262 return NULL;
2263 }
2264 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2265 if (!p)
2266 return NULL;
2267 p->context_expr = context_expr;
2268 p->optional_vars = optional_vars;
2269 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002270}
2271
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002272
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002273PyObject*
2274ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002275{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002276 mod_ty o = (mod_ty)_o;
2277 PyObject *result = NULL, *value = NULL;
2278 if (!o) {
2279 Py_INCREF(Py_None);
2280 return Py_None;
2281 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002282
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002283 switch (o->kind) {
2284 case Module_kind:
2285 result = PyType_GenericNew(Module_type, NULL, NULL);
2286 if (!result) goto failed;
2287 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2288 if (!value) goto failed;
2289 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2290 goto failed;
2291 Py_DECREF(value);
2292 break;
2293 case Interactive_kind:
2294 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2295 if (!result) goto failed;
2296 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2297 if (!value) goto failed;
2298 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2299 goto failed;
2300 Py_DECREF(value);
2301 break;
2302 case Expression_kind:
2303 result = PyType_GenericNew(Expression_type, NULL, NULL);
2304 if (!result) goto failed;
2305 value = ast2obj_expr(o->v.Expression.body);
2306 if (!value) goto failed;
2307 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 break;
2311 case Suite_kind:
2312 result = PyType_GenericNew(Suite_type, NULL, NULL);
2313 if (!result) goto failed;
2314 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2315 if (!value) goto failed;
2316 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2317 goto failed;
2318 Py_DECREF(value);
2319 break;
2320 }
2321 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002322failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002323 Py_XDECREF(value);
2324 Py_XDECREF(result);
2325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326}
2327
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002328PyObject*
2329ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002331 stmt_ty o = (stmt_ty)_o;
2332 PyObject *result = NULL, *value = NULL;
2333 if (!o) {
2334 Py_INCREF(Py_None);
2335 return Py_None;
2336 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002337
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002338 switch (o->kind) {
2339 case FunctionDef_kind:
2340 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2341 if (!result) goto failed;
2342 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002343 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002344 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2345 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002346 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002347 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002348 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002349 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2350 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002351 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002352 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2353 if (!value) goto failed;
2354 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2355 goto failed;
2356 Py_DECREF(value);
2357 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2358 if (!value) goto failed;
2359 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2360 goto failed;
2361 Py_DECREF(value);
2362 value = ast2obj_expr(o->v.FunctionDef.returns);
2363 if (!value) goto failed;
2364 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2365 goto failed;
2366 Py_DECREF(value);
2367 break;
2368 case ClassDef_kind:
2369 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2370 if (!result) goto failed;
2371 value = ast2obj_identifier(o->v.ClassDef.name);
2372 if (!value) goto failed;
2373 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2374 goto failed;
2375 Py_DECREF(value);
2376 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2377 if (!value) goto failed;
2378 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2379 goto failed;
2380 Py_DECREF(value);
2381 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2382 if (!value) goto failed;
2383 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2384 goto failed;
2385 Py_DECREF(value);
2386 value = ast2obj_expr(o->v.ClassDef.starargs);
2387 if (!value) goto failed;
2388 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
2391 value = ast2obj_expr(o->v.ClassDef.kwargs);
2392 if (!value) goto failed;
2393 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2397 if (!value) goto failed;
2398 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
2401 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2402 if (!value) goto failed;
2403 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2404 goto failed;
2405 Py_DECREF(value);
2406 break;
2407 case Return_kind:
2408 result = PyType_GenericNew(Return_type, NULL, NULL);
2409 if (!result) goto failed;
2410 value = ast2obj_expr(o->v.Return.value);
2411 if (!value) goto failed;
2412 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
2415 break;
2416 case Delete_kind:
2417 result = PyType_GenericNew(Delete_type, NULL, NULL);
2418 if (!result) goto failed;
2419 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2420 if (!value) goto failed;
2421 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
2424 break;
2425 case Assign_kind:
2426 result = PyType_GenericNew(Assign_type, NULL, NULL);
2427 if (!result) goto failed;
2428 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2429 if (!value) goto failed;
2430 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2431 goto failed;
2432 Py_DECREF(value);
2433 value = ast2obj_expr(o->v.Assign.value);
2434 if (!value) goto failed;
2435 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
2438 break;
2439 case AugAssign_kind:
2440 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2441 if (!result) goto failed;
2442 value = ast2obj_expr(o->v.AugAssign.target);
2443 if (!value) goto failed;
2444 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
2447 value = ast2obj_operator(o->v.AugAssign.op);
2448 if (!value) goto failed;
2449 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2450 goto failed;
2451 Py_DECREF(value);
2452 value = ast2obj_expr(o->v.AugAssign.value);
2453 if (!value) goto failed;
2454 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2455 goto failed;
2456 Py_DECREF(value);
2457 break;
2458 case For_kind:
2459 result = PyType_GenericNew(For_type, NULL, NULL);
2460 if (!result) goto failed;
2461 value = ast2obj_expr(o->v.For.target);
2462 if (!value) goto failed;
2463 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2464 goto failed;
2465 Py_DECREF(value);
2466 value = ast2obj_expr(o->v.For.iter);
2467 if (!value) goto failed;
2468 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2469 goto failed;
2470 Py_DECREF(value);
2471 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2472 if (!value) goto failed;
2473 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2474 goto failed;
2475 Py_DECREF(value);
2476 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2477 if (!value) goto failed;
2478 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
2481 break;
2482 case While_kind:
2483 result = PyType_GenericNew(While_type, NULL, NULL);
2484 if (!result) goto failed;
2485 value = ast2obj_expr(o->v.While.test);
2486 if (!value) goto failed;
2487 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2488 goto failed;
2489 Py_DECREF(value);
2490 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2491 if (!value) goto failed;
2492 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2493 goto failed;
2494 Py_DECREF(value);
2495 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2496 if (!value) goto failed;
2497 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2498 goto failed;
2499 Py_DECREF(value);
2500 break;
2501 case If_kind:
2502 result = PyType_GenericNew(If_type, NULL, NULL);
2503 if (!result) goto failed;
2504 value = ast2obj_expr(o->v.If.test);
2505 if (!value) goto failed;
2506 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2507 goto failed;
2508 Py_DECREF(value);
2509 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2510 if (!value) goto failed;
2511 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2512 goto failed;
2513 Py_DECREF(value);
2514 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2515 if (!value) goto failed;
2516 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2517 goto failed;
2518 Py_DECREF(value);
2519 break;
2520 case With_kind:
2521 result = PyType_GenericNew(With_type, NULL, NULL);
2522 if (!result) goto failed;
2523 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2524 if (!value) goto failed;
2525 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2526 goto failed;
2527 Py_DECREF(value);
2528 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2529 if (!value) goto failed;
2530 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2531 goto failed;
2532 Py_DECREF(value);
2533 break;
2534 case Raise_kind:
2535 result = PyType_GenericNew(Raise_type, NULL, NULL);
2536 if (!result) goto failed;
2537 value = ast2obj_expr(o->v.Raise.exc);
2538 if (!value) goto failed;
2539 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2540 goto failed;
2541 Py_DECREF(value);
2542 value = ast2obj_expr(o->v.Raise.cause);
2543 if (!value) goto failed;
2544 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2545 goto failed;
2546 Py_DECREF(value);
2547 break;
2548 case Try_kind:
2549 result = PyType_GenericNew(Try_type, NULL, NULL);
2550 if (!result) goto failed;
2551 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2552 if (!value) goto failed;
2553 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2554 goto failed;
2555 Py_DECREF(value);
2556 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2557 if (!value) goto failed;
2558 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2559 goto failed;
2560 Py_DECREF(value);
2561 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2562 if (!value) goto failed;
2563 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2564 goto failed;
2565 Py_DECREF(value);
2566 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2567 if (!value) goto failed;
2568 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2569 goto failed;
2570 Py_DECREF(value);
2571 break;
2572 case Assert_kind:
2573 result = PyType_GenericNew(Assert_type, NULL, NULL);
2574 if (!result) goto failed;
2575 value = ast2obj_expr(o->v.Assert.test);
2576 if (!value) goto failed;
2577 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2578 goto failed;
2579 Py_DECREF(value);
2580 value = ast2obj_expr(o->v.Assert.msg);
2581 if (!value) goto failed;
2582 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
2585 break;
2586 case Import_kind:
2587 result = PyType_GenericNew(Import_type, NULL, NULL);
2588 if (!result) goto failed;
2589 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2590 if (!value) goto failed;
2591 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 break;
2595 case ImportFrom_kind:
2596 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2597 if (!result) goto failed;
2598 value = ast2obj_identifier(o->v.ImportFrom.module);
2599 if (!value) goto failed;
2600 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2601 goto failed;
2602 Py_DECREF(value);
2603 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2604 if (!value) goto failed;
2605 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 value = ast2obj_int(o->v.ImportFrom.level);
2609 if (!value) goto failed;
2610 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 break;
2614 case Global_kind:
2615 result = PyType_GenericNew(Global_type, NULL, NULL);
2616 if (!result) goto failed;
2617 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2618 if (!value) goto failed;
2619 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 break;
2623 case Nonlocal_kind:
2624 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2625 if (!result) goto failed;
2626 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2627 if (!value) goto failed;
2628 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
2631 break;
2632 case Expr_kind:
2633 result = PyType_GenericNew(Expr_type, NULL, NULL);
2634 if (!result) goto failed;
2635 value = ast2obj_expr(o->v.Expr.value);
2636 if (!value) goto failed;
2637 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2638 goto failed;
2639 Py_DECREF(value);
2640 break;
2641 case Pass_kind:
2642 result = PyType_GenericNew(Pass_type, NULL, NULL);
2643 if (!result) goto failed;
2644 break;
2645 case Break_kind:
2646 result = PyType_GenericNew(Break_type, NULL, NULL);
2647 if (!result) goto failed;
2648 break;
2649 case Continue_kind:
2650 result = PyType_GenericNew(Continue_type, NULL, NULL);
2651 if (!result) goto failed;
2652 break;
2653 }
2654 value = ast2obj_int(o->lineno);
2655 if (!value) goto failed;
2656 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2657 goto failed;
2658 Py_DECREF(value);
2659 value = ast2obj_int(o->col_offset);
2660 if (!value) goto failed;
2661 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2662 goto failed;
2663 Py_DECREF(value);
2664 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002665failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002666 Py_XDECREF(value);
2667 Py_XDECREF(result);
2668 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669}
2670
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002671PyObject*
2672ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002674 expr_ty o = (expr_ty)_o;
2675 PyObject *result = NULL, *value = NULL;
2676 if (!o) {
2677 Py_INCREF(Py_None);
2678 return Py_None;
2679 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002680
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002681 switch (o->kind) {
2682 case BoolOp_kind:
2683 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_boolop(o->v.BoolOp.op);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2691 if (!value) goto failed;
2692 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 break;
2696 case BinOp_kind:
2697 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2698 if (!result) goto failed;
2699 value = ast2obj_expr(o->v.BinOp.left);
2700 if (!value) goto failed;
2701 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 value = ast2obj_operator(o->v.BinOp.op);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_expr(o->v.BinOp.right);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 break;
2715 case UnaryOp_kind:
2716 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2717 if (!result) goto failed;
2718 value = ast2obj_unaryop(o->v.UnaryOp.op);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
2723 value = ast2obj_expr(o->v.UnaryOp.operand);
2724 if (!value) goto failed;
2725 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 break;
2729 case Lambda_kind:
2730 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2731 if (!result) goto failed;
2732 value = ast2obj_arguments(o->v.Lambda.args);
2733 if (!value) goto failed;
2734 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2735 goto failed;
2736 Py_DECREF(value);
2737 value = ast2obj_expr(o->v.Lambda.body);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 break;
2743 case IfExp_kind:
2744 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2745 if (!result) goto failed;
2746 value = ast2obj_expr(o->v.IfExp.test);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 value = ast2obj_expr(o->v.IfExp.body);
2752 if (!value) goto failed;
2753 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 value = ast2obj_expr(o->v.IfExp.orelse);
2757 if (!value) goto failed;
2758 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
2761 break;
2762 case Dict_kind:
2763 result = PyType_GenericNew(Dict_type, NULL, NULL);
2764 if (!result) goto failed;
2765 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2766 if (!value) goto failed;
2767 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
2770 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2771 if (!value) goto failed;
2772 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 break;
2776 case Set_kind:
2777 result = PyType_GenericNew(Set_type, NULL, NULL);
2778 if (!result) goto failed;
2779 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 break;
2785 case ListComp_kind:
2786 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2787 if (!result) goto failed;
2788 value = ast2obj_expr(o->v.ListComp.elt);
2789 if (!value) goto failed;
2790 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2791 goto failed;
2792 Py_DECREF(value);
2793 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2794 if (!value) goto failed;
2795 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 break;
2799 case SetComp_kind:
2800 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2801 if (!result) goto failed;
2802 value = ast2obj_expr(o->v.SetComp.elt);
2803 if (!value) goto failed;
2804 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
2807 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2808 if (!value) goto failed;
2809 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
2812 break;
2813 case DictComp_kind:
2814 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2815 if (!result) goto failed;
2816 value = ast2obj_expr(o->v.DictComp.key);
2817 if (!value) goto failed;
2818 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2819 goto failed;
2820 Py_DECREF(value);
2821 value = ast2obj_expr(o->v.DictComp.value);
2822 if (!value) goto failed;
2823 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2824 goto failed;
2825 Py_DECREF(value);
2826 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2827 if (!value) goto failed;
2828 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
2831 break;
2832 case GeneratorExp_kind:
2833 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2834 if (!result) goto failed;
2835 value = ast2obj_expr(o->v.GeneratorExp.elt);
2836 if (!value) goto failed;
2837 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2838 goto failed;
2839 Py_DECREF(value);
2840 value = ast2obj_list(o->v.GeneratorExp.generators,
2841 ast2obj_comprehension);
2842 if (!value) goto failed;
2843 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2844 goto failed;
2845 Py_DECREF(value);
2846 break;
2847 case Yield_kind:
2848 result = PyType_GenericNew(Yield_type, NULL, NULL);
2849 if (!result) goto failed;
2850 value = ast2obj_expr(o->v.Yield.value);
2851 if (!value) goto failed;
2852 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 break;
2856 case YieldFrom_kind:
2857 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2858 if (!result) goto failed;
2859 value = ast2obj_expr(o->v.YieldFrom.value);
2860 if (!value) goto failed;
2861 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2862 goto failed;
2863 Py_DECREF(value);
2864 break;
2865 case Compare_kind:
2866 result = PyType_GenericNew(Compare_type, NULL, NULL);
2867 if (!result) goto failed;
2868 value = ast2obj_expr(o->v.Compare.left);
2869 if (!value) goto failed;
2870 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2871 goto failed;
2872 Py_DECREF(value);
2873 {
2874 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2875 value = PyList_New(n);
2876 if (!value) goto failed;
2877 for(i = 0; i < n; i++)
2878 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002880 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002881 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2882 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002883 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002884 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002885 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002886 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
2887 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002888 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002889 break;
2890 case Call_kind:
2891 result = PyType_GenericNew(Call_type, NULL, NULL);
2892 if (!result) goto failed;
2893 value = ast2obj_expr(o->v.Call.func);
2894 if (!value) goto failed;
2895 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2896 goto failed;
2897 Py_DECREF(value);
2898 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2904 if (!value) goto failed;
2905 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 value = ast2obj_expr(o->v.Call.starargs);
2909 if (!value) goto failed;
2910 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2911 goto failed;
2912 Py_DECREF(value);
2913 value = ast2obj_expr(o->v.Call.kwargs);
2914 if (!value) goto failed;
2915 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2916 goto failed;
2917 Py_DECREF(value);
2918 break;
2919 case Num_kind:
2920 result = PyType_GenericNew(Num_type, NULL, NULL);
2921 if (!result) goto failed;
2922 value = ast2obj_object(o->v.Num.n);
2923 if (!value) goto failed;
2924 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2925 goto failed;
2926 Py_DECREF(value);
2927 break;
2928 case Str_kind:
2929 result = PyType_GenericNew(Str_type, NULL, NULL);
2930 if (!result) goto failed;
2931 value = ast2obj_string(o->v.Str.s);
2932 if (!value) goto failed;
2933 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
2936 break;
2937 case Bytes_kind:
2938 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2939 if (!result) goto failed;
2940 value = ast2obj_bytes(o->v.Bytes.s);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 break;
2946 case Ellipsis_kind:
2947 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2948 if (!result) goto failed;
2949 break;
2950 case Attribute_kind:
2951 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2952 if (!result) goto failed;
2953 value = ast2obj_expr(o->v.Attribute.value);
2954 if (!value) goto failed;
2955 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2956 goto failed;
2957 Py_DECREF(value);
2958 value = ast2obj_identifier(o->v.Attribute.attr);
2959 if (!value) goto failed;
2960 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
2961 goto failed;
2962 Py_DECREF(value);
2963 value = ast2obj_expr_context(o->v.Attribute.ctx);
2964 if (!value) goto failed;
2965 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 break;
2969 case Subscript_kind:
2970 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2971 if (!result) goto failed;
2972 value = ast2obj_expr(o->v.Subscript.value);
2973 if (!value) goto failed;
2974 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2975 goto failed;
2976 Py_DECREF(value);
2977 value = ast2obj_slice(o->v.Subscript.slice);
2978 if (!value) goto failed;
2979 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
2980 goto failed;
2981 Py_DECREF(value);
2982 value = ast2obj_expr_context(o->v.Subscript.ctx);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 break;
2988 case Starred_kind:
2989 result = PyType_GenericNew(Starred_type, NULL, NULL);
2990 if (!result) goto failed;
2991 value = ast2obj_expr(o->v.Starred.value);
2992 if (!value) goto failed;
2993 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2994 goto failed;
2995 Py_DECREF(value);
2996 value = ast2obj_expr_context(o->v.Starred.ctx);
2997 if (!value) goto failed;
2998 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 break;
3002 case Name_kind:
3003 result = PyType_GenericNew(Name_type, NULL, NULL);
3004 if (!result) goto failed;
3005 value = ast2obj_identifier(o->v.Name.id);
3006 if (!value) goto failed;
3007 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3008 goto failed;
3009 Py_DECREF(value);
3010 value = ast2obj_expr_context(o->v.Name.ctx);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 break;
3016 case List_kind:
3017 result = PyType_GenericNew(List_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3020 if (!value) goto failed;
3021 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
3024 value = ast2obj_expr_context(o->v.List.ctx);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 break;
3030 case Tuple_kind:
3031 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3032 if (!result) goto failed;
3033 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3034 if (!value) goto failed;
3035 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3036 goto failed;
3037 Py_DECREF(value);
3038 value = ast2obj_expr_context(o->v.Tuple.ctx);
3039 if (!value) goto failed;
3040 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3041 goto failed;
3042 Py_DECREF(value);
3043 break;
3044 }
3045 value = ast2obj_int(o->lineno);
3046 if (!value) goto failed;
3047 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3048 goto failed;
3049 Py_DECREF(value);
3050 value = ast2obj_int(o->col_offset);
3051 if (!value) goto failed;
3052 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3053 goto failed;
3054 Py_DECREF(value);
3055 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003056failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003057 Py_XDECREF(value);
3058 Py_XDECREF(result);
3059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060}
3061
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003062PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003064 switch(o) {
3065 case Load:
3066 Py_INCREF(Load_singleton);
3067 return Load_singleton;
3068 case Store:
3069 Py_INCREF(Store_singleton);
3070 return Store_singleton;
3071 case Del:
3072 Py_INCREF(Del_singleton);
3073 return Del_singleton;
3074 case AugLoad:
3075 Py_INCREF(AugLoad_singleton);
3076 return AugLoad_singleton;
3077 case AugStore:
3078 Py_INCREF(AugStore_singleton);
3079 return AugStore_singleton;
3080 case Param:
3081 Py_INCREF(Param_singleton);
3082 return Param_singleton;
3083 default:
3084 /* should never happen, but just in case ... */
3085 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3086 return NULL;
3087 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003089PyObject*
3090ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003092 slice_ty o = (slice_ty)_o;
3093 PyObject *result = NULL, *value = NULL;
3094 if (!o) {
3095 Py_INCREF(Py_None);
3096 return Py_None;
3097 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003098
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003099 switch (o->kind) {
3100 case Slice_kind:
3101 result = PyType_GenericNew(Slice_type, NULL, NULL);
3102 if (!result) goto failed;
3103 value = ast2obj_expr(o->v.Slice.lower);
3104 if (!value) goto failed;
3105 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3106 goto failed;
3107 Py_DECREF(value);
3108 value = ast2obj_expr(o->v.Slice.upper);
3109 if (!value) goto failed;
3110 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3111 goto failed;
3112 Py_DECREF(value);
3113 value = ast2obj_expr(o->v.Slice.step);
3114 if (!value) goto failed;
3115 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3116 goto failed;
3117 Py_DECREF(value);
3118 break;
3119 case ExtSlice_kind:
3120 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3121 if (!result) goto failed;
3122 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3123 if (!value) goto failed;
3124 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3125 goto failed;
3126 Py_DECREF(value);
3127 break;
3128 case Index_kind:
3129 result = PyType_GenericNew(Index_type, NULL, NULL);
3130 if (!result) goto failed;
3131 value = ast2obj_expr(o->v.Index.value);
3132 if (!value) goto failed;
3133 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3134 goto failed;
3135 Py_DECREF(value);
3136 break;
3137 }
3138 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003139failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003140 Py_XDECREF(value);
3141 Py_XDECREF(result);
3142 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143}
3144
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003145PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003147 switch(o) {
3148 case And:
3149 Py_INCREF(And_singleton);
3150 return And_singleton;
3151 case Or:
3152 Py_INCREF(Or_singleton);
3153 return Or_singleton;
3154 default:
3155 /* should never happen, but just in case ... */
3156 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3157 return NULL;
3158 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003159}
3160PyObject* ast2obj_operator(operator_ty o)
3161{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003162 switch(o) {
3163 case Add:
3164 Py_INCREF(Add_singleton);
3165 return Add_singleton;
3166 case Sub:
3167 Py_INCREF(Sub_singleton);
3168 return Sub_singleton;
3169 case Mult:
3170 Py_INCREF(Mult_singleton);
3171 return Mult_singleton;
3172 case Div:
3173 Py_INCREF(Div_singleton);
3174 return Div_singleton;
3175 case Mod:
3176 Py_INCREF(Mod_singleton);
3177 return Mod_singleton;
3178 case Pow:
3179 Py_INCREF(Pow_singleton);
3180 return Pow_singleton;
3181 case LShift:
3182 Py_INCREF(LShift_singleton);
3183 return LShift_singleton;
3184 case RShift:
3185 Py_INCREF(RShift_singleton);
3186 return RShift_singleton;
3187 case BitOr:
3188 Py_INCREF(BitOr_singleton);
3189 return BitOr_singleton;
3190 case BitXor:
3191 Py_INCREF(BitXor_singleton);
3192 return BitXor_singleton;
3193 case BitAnd:
3194 Py_INCREF(BitAnd_singleton);
3195 return BitAnd_singleton;
3196 case FloorDiv:
3197 Py_INCREF(FloorDiv_singleton);
3198 return FloorDiv_singleton;
3199 default:
3200 /* should never happen, but just in case ... */
3201 PyErr_Format(PyExc_SystemError, "unknown operator found");
3202 return NULL;
3203 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003204}
3205PyObject* ast2obj_unaryop(unaryop_ty o)
3206{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003207 switch(o) {
3208 case Invert:
3209 Py_INCREF(Invert_singleton);
3210 return Invert_singleton;
3211 case Not:
3212 Py_INCREF(Not_singleton);
3213 return Not_singleton;
3214 case UAdd:
3215 Py_INCREF(UAdd_singleton);
3216 return UAdd_singleton;
3217 case USub:
3218 Py_INCREF(USub_singleton);
3219 return USub_singleton;
3220 default:
3221 /* should never happen, but just in case ... */
3222 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3223 return NULL;
3224 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003225}
3226PyObject* ast2obj_cmpop(cmpop_ty o)
3227{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003228 switch(o) {
3229 case Eq:
3230 Py_INCREF(Eq_singleton);
3231 return Eq_singleton;
3232 case NotEq:
3233 Py_INCREF(NotEq_singleton);
3234 return NotEq_singleton;
3235 case Lt:
3236 Py_INCREF(Lt_singleton);
3237 return Lt_singleton;
3238 case LtE:
3239 Py_INCREF(LtE_singleton);
3240 return LtE_singleton;
3241 case Gt:
3242 Py_INCREF(Gt_singleton);
3243 return Gt_singleton;
3244 case GtE:
3245 Py_INCREF(GtE_singleton);
3246 return GtE_singleton;
3247 case Is:
3248 Py_INCREF(Is_singleton);
3249 return Is_singleton;
3250 case IsNot:
3251 Py_INCREF(IsNot_singleton);
3252 return IsNot_singleton;
3253 case In:
3254 Py_INCREF(In_singleton);
3255 return In_singleton;
3256 case NotIn:
3257 Py_INCREF(NotIn_singleton);
3258 return NotIn_singleton;
3259 default:
3260 /* should never happen, but just in case ... */
3261 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3262 return NULL;
3263 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003264}
3265PyObject*
3266ast2obj_comprehension(void* _o)
3267{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003268 comprehension_ty o = (comprehension_ty)_o;
3269 PyObject *result = NULL, *value = NULL;
3270 if (!o) {
3271 Py_INCREF(Py_None);
3272 return Py_None;
3273 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003274
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003275 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3276 if (!result) return NULL;
3277 value = ast2obj_expr(o->target);
3278 if (!value) goto failed;
3279 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
3282 value = ast2obj_expr(o->iter);
3283 if (!value) goto failed;
3284 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3285 goto failed;
3286 Py_DECREF(value);
3287 value = ast2obj_list(o->ifs, ast2obj_expr);
3288 if (!value) goto failed;
3289 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3290 goto failed;
3291 Py_DECREF(value);
3292 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003293failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003294 Py_XDECREF(value);
3295 Py_XDECREF(result);
3296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297}
3298
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003299PyObject*
3300ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003302 excepthandler_ty o = (excepthandler_ty)_o;
3303 PyObject *result = NULL, *value = NULL;
3304 if (!o) {
3305 Py_INCREF(Py_None);
3306 return Py_None;
3307 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003308
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003309 switch (o->kind) {
3310 case ExceptHandler_kind:
3311 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3312 if (!result) goto failed;
3313 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003314 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003315 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3316 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003318 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003319 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003320 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3321 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003322 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003323 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3324 if (!value) goto failed;
3325 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3326 goto failed;
3327 Py_DECREF(value);
3328 break;
3329 }
3330 value = ast2obj_int(o->lineno);
3331 if (!value) goto failed;
3332 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3333 goto failed;
3334 Py_DECREF(value);
3335 value = ast2obj_int(o->col_offset);
3336 if (!value) goto failed;
3337 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3338 goto failed;
3339 Py_DECREF(value);
3340 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003341failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003342 Py_XDECREF(value);
3343 Py_XDECREF(result);
3344 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345}
3346
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003347PyObject*
3348ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003350 arguments_ty o = (arguments_ty)_o;
3351 PyObject *result = NULL, *value = NULL;
3352 if (!o) {
3353 Py_INCREF(Py_None);
3354 return Py_None;
3355 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003356
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003357 result = PyType_GenericNew(arguments_type, NULL, NULL);
3358 if (!result) return NULL;
3359 value = ast2obj_list(o->args, ast2obj_arg);
3360 if (!value) goto failed;
3361 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3362 goto failed;
3363 Py_DECREF(value);
3364 value = ast2obj_identifier(o->vararg);
3365 if (!value) goto failed;
3366 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3367 goto failed;
3368 Py_DECREF(value);
3369 value = ast2obj_expr(o->varargannotation);
3370 if (!value) goto failed;
3371 if (_PyObject_SetAttrId(result, &PyId_varargannotation, value) == -1)
3372 goto failed;
3373 Py_DECREF(value);
3374 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3375 if (!value) goto failed;
3376 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3377 goto failed;
3378 Py_DECREF(value);
3379 value = ast2obj_identifier(o->kwarg);
3380 if (!value) goto failed;
3381 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
3382 goto failed;
3383 Py_DECREF(value);
3384 value = ast2obj_expr(o->kwargannotation);
3385 if (!value) goto failed;
3386 if (_PyObject_SetAttrId(result, &PyId_kwargannotation, value) == -1)
3387 goto failed;
3388 Py_DECREF(value);
3389 value = ast2obj_list(o->defaults, ast2obj_expr);
3390 if (!value) goto failed;
3391 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3392 goto failed;
3393 Py_DECREF(value);
3394 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3395 if (!value) goto failed;
3396 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
3397 goto failed;
3398 Py_DECREF(value);
3399 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003400failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 Py_XDECREF(value);
3402 Py_XDECREF(result);
3403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404}
3405
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003406PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003407ast2obj_arg(void* _o)
3408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003409 arg_ty o = (arg_ty)_o;
3410 PyObject *result = NULL, *value = NULL;
3411 if (!o) {
3412 Py_INCREF(Py_None);
3413 return Py_None;
3414 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003415
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003416 result = PyType_GenericNew(arg_type, NULL, NULL);
3417 if (!result) return NULL;
3418 value = ast2obj_identifier(o->arg);
3419 if (!value) goto failed;
3420 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3421 goto failed;
3422 Py_DECREF(value);
3423 value = ast2obj_expr(o->annotation);
3424 if (!value) goto failed;
3425 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3426 goto failed;
3427 Py_DECREF(value);
3428 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003429failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003430 Py_XDECREF(value);
3431 Py_XDECREF(result);
3432 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003433}
3434
3435PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003436ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003438 keyword_ty o = (keyword_ty)_o;
3439 PyObject *result = NULL, *value = NULL;
3440 if (!o) {
3441 Py_INCREF(Py_None);
3442 return Py_None;
3443 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003444
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003445 result = PyType_GenericNew(keyword_type, NULL, NULL);
3446 if (!result) return NULL;
3447 value = ast2obj_identifier(o->arg);
3448 if (!value) goto failed;
3449 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3450 goto failed;
3451 Py_DECREF(value);
3452 value = ast2obj_expr(o->value);
3453 if (!value) goto failed;
3454 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3455 goto failed;
3456 Py_DECREF(value);
3457 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003458failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003459 Py_XDECREF(value);
3460 Py_XDECREF(result);
3461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462}
3463
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003464PyObject*
3465ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003467 alias_ty o = (alias_ty)_o;
3468 PyObject *result = NULL, *value = NULL;
3469 if (!o) {
3470 Py_INCREF(Py_None);
3471 return Py_None;
3472 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003473
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003474 result = PyType_GenericNew(alias_type, NULL, NULL);
3475 if (!result) return NULL;
3476 value = ast2obj_identifier(o->name);
3477 if (!value) goto failed;
3478 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3479 goto failed;
3480 Py_DECREF(value);
3481 value = ast2obj_identifier(o->asname);
3482 if (!value) goto failed;
3483 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3484 goto failed;
3485 Py_DECREF(value);
3486 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003487failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003488 Py_XDECREF(value);
3489 Py_XDECREF(result);
3490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491}
3492
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003493PyObject*
3494ast2obj_withitem(void* _o)
3495{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003496 withitem_ty o = (withitem_ty)_o;
3497 PyObject *result = NULL, *value = NULL;
3498 if (!o) {
3499 Py_INCREF(Py_None);
3500 return Py_None;
3501 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003502
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003503 result = PyType_GenericNew(withitem_type, NULL, NULL);
3504 if (!result) return NULL;
3505 value = ast2obj_expr(o->context_expr);
3506 if (!value) goto failed;
3507 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3508 goto failed;
3509 Py_DECREF(value);
3510 value = ast2obj_expr(o->optional_vars);
3511 if (!value) goto failed;
3512 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3513 goto failed;
3514 Py_DECREF(value);
3515 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003516failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003517 Py_XDECREF(value);
3518 Py_XDECREF(result);
3519 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003520}
3521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003523int
3524obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3525{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003527
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003528 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003529
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 if (obj == Py_None) {
3531 *out = NULL;
3532 return 0;
3533 }
3534 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3535 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003536 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003537 }
3538 if (isinstance) {
3539 asdl_seq* body;
3540
3541 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3542 int res;
3543 Py_ssize_t len;
3544 Py_ssize_t i;
3545 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3546 if (tmp == NULL) goto failed;
3547 if (!PyList_Check(tmp)) {
3548 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3549 goto failed;
3550 }
3551 len = PyList_GET_SIZE(tmp);
3552 body = asdl_seq_new(len, arena);
3553 if (body == NULL) goto failed;
3554 for (i = 0; i < len; i++) {
3555 stmt_ty value;
3556 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3557 if (res != 0) goto failed;
3558 asdl_seq_SET(body, i, value);
3559 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003560 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003561 } else {
3562 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3563 return 1;
3564 }
3565 *out = Module(body, arena);
3566 if (*out == NULL) goto failed;
3567 return 0;
3568 }
3569 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3570 if (isinstance == -1) {
3571 return 1;
3572 }
3573 if (isinstance) {
3574 asdl_seq* body;
3575
3576 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3577 int res;
3578 Py_ssize_t len;
3579 Py_ssize_t i;
3580 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3581 if (tmp == NULL) goto failed;
3582 if (!PyList_Check(tmp)) {
3583 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3584 goto failed;
3585 }
3586 len = PyList_GET_SIZE(tmp);
3587 body = asdl_seq_new(len, arena);
3588 if (body == NULL) goto failed;
3589 for (i = 0; i < len; i++) {
3590 stmt_ty value;
3591 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3592 if (res != 0) goto failed;
3593 asdl_seq_SET(body, i, value);
3594 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003595 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 } else {
3597 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3598 return 1;
3599 }
3600 *out = Interactive(body, arena);
3601 if (*out == NULL) goto failed;
3602 return 0;
3603 }
3604 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3605 if (isinstance == -1) {
3606 return 1;
3607 }
3608 if (isinstance) {
3609 expr_ty body;
3610
3611 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3612 int res;
3613 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3614 if (tmp == NULL) goto failed;
3615 res = obj2ast_expr(tmp, &body, arena);
3616 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003617 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003618 } else {
3619 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3620 return 1;
3621 }
3622 *out = Expression(body, arena);
3623 if (*out == NULL) goto failed;
3624 return 0;
3625 }
3626 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3627 if (isinstance == -1) {
3628 return 1;
3629 }
3630 if (isinstance) {
3631 asdl_seq* body;
3632
3633 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3634 int res;
3635 Py_ssize_t len;
3636 Py_ssize_t i;
3637 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3638 if (tmp == NULL) goto failed;
3639 if (!PyList_Check(tmp)) {
3640 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3641 goto failed;
3642 }
3643 len = PyList_GET_SIZE(tmp);
3644 body = asdl_seq_new(len, arena);
3645 if (body == NULL) goto failed;
3646 for (i = 0; i < len; i++) {
3647 stmt_ty value;
3648 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3649 if (res != 0) goto failed;
3650 asdl_seq_SET(body, i, value);
3651 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003652 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003653 } 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;
Victor Stinner1acc1292013-07-27 00:03:47 +02003687 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003688 } else {
3689 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003690 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003691 }
3692 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3693 int res;
3694 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3695 if (tmp == NULL) goto failed;
3696 res = obj2ast_int(tmp, &col_offset, arena);
3697 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003698 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003699 } else {
3700 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3701 return 1;
3702 }
3703 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3704 if (isinstance == -1) {
3705 return 1;
3706 }
3707 if (isinstance) {
3708 identifier name;
3709 arguments_ty args;
3710 asdl_seq* body;
3711 asdl_seq* decorator_list;
3712 expr_ty returns;
3713
3714 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3715 int res;
3716 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3717 if (tmp == NULL) goto failed;
3718 res = obj2ast_identifier(tmp, &name, arena);
3719 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003720 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003721 } else {
3722 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3723 return 1;
3724 }
3725 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3726 int res;
3727 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3728 if (tmp == NULL) goto failed;
3729 res = obj2ast_arguments(tmp, &args, arena);
3730 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003731 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003732 } else {
3733 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3734 return 1;
3735 }
3736 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3737 int res;
3738 Py_ssize_t len;
3739 Py_ssize_t i;
3740 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3741 if (tmp == NULL) goto failed;
3742 if (!PyList_Check(tmp)) {
3743 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3744 goto failed;
3745 }
3746 len = PyList_GET_SIZE(tmp);
3747 body = asdl_seq_new(len, arena);
3748 if (body == NULL) goto failed;
3749 for (i = 0; i < len; i++) {
3750 stmt_ty value;
3751 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3752 if (res != 0) goto failed;
3753 asdl_seq_SET(body, i, value);
3754 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003755 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003756 } else {
3757 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3758 return 1;
3759 }
3760 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3761 int res;
3762 Py_ssize_t len;
3763 Py_ssize_t i;
3764 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3765 if (tmp == NULL) goto failed;
3766 if (!PyList_Check(tmp)) {
3767 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3768 goto failed;
3769 }
3770 len = PyList_GET_SIZE(tmp);
3771 decorator_list = asdl_seq_new(len, arena);
3772 if (decorator_list == NULL) goto failed;
3773 for (i = 0; i < len; i++) {
3774 expr_ty value;
3775 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3776 if (res != 0) goto failed;
3777 asdl_seq_SET(decorator_list, i, value);
3778 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003779 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3782 return 1;
3783 }
3784 if (_PyObject_HasAttrId(obj, &PyId_returns)) {
3785 int res;
3786 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3787 if (tmp == NULL) goto failed;
3788 res = obj2ast_expr(tmp, &returns, arena);
3789 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003790 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003791 } else {
3792 returns = NULL;
3793 }
3794 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3795 col_offset, arena);
3796 if (*out == NULL) goto failed;
3797 return 0;
3798 }
3799 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3800 if (isinstance == -1) {
3801 return 1;
3802 }
3803 if (isinstance) {
3804 identifier name;
3805 asdl_seq* bases;
3806 asdl_seq* keywords;
3807 expr_ty starargs;
3808 expr_ty kwargs;
3809 asdl_seq* body;
3810 asdl_seq* decorator_list;
3811
3812 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3813 int res;
3814 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3815 if (tmp == NULL) goto failed;
3816 res = obj2ast_identifier(tmp, &name, arena);
3817 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003818 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 } else {
3820 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3821 return 1;
3822 }
3823 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3824 int res;
3825 Py_ssize_t len;
3826 Py_ssize_t i;
3827 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3828 if (tmp == NULL) goto failed;
3829 if (!PyList_Check(tmp)) {
3830 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3831 goto failed;
3832 }
3833 len = PyList_GET_SIZE(tmp);
3834 bases = asdl_seq_new(len, arena);
3835 if (bases == NULL) goto failed;
3836 for (i = 0; i < len; i++) {
3837 expr_ty value;
3838 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3839 if (res != 0) goto failed;
3840 asdl_seq_SET(bases, i, value);
3841 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003842 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 } else {
3844 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3845 return 1;
3846 }
3847 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3848 int res;
3849 Py_ssize_t len;
3850 Py_ssize_t i;
3851 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3852 if (tmp == NULL) goto failed;
3853 if (!PyList_Check(tmp)) {
3854 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3855 goto failed;
3856 }
3857 len = PyList_GET_SIZE(tmp);
3858 keywords = asdl_seq_new(len, arena);
3859 if (keywords == NULL) goto failed;
3860 for (i = 0; i < len; i++) {
3861 keyword_ty value;
3862 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3863 if (res != 0) goto failed;
3864 asdl_seq_SET(keywords, i, value);
3865 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003866 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003867 } else {
3868 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3869 return 1;
3870 }
3871 if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
3872 int res;
3873 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3874 if (tmp == NULL) goto failed;
3875 res = obj2ast_expr(tmp, &starargs, arena);
3876 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003877 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 } else {
3879 starargs = NULL;
3880 }
3881 if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
3882 int res;
3883 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3884 if (tmp == NULL) goto failed;
3885 res = obj2ast_expr(tmp, &kwargs, arena);
3886 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003887 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003888 } else {
3889 kwargs = NULL;
3890 }
3891 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3892 int res;
3893 Py_ssize_t len;
3894 Py_ssize_t i;
3895 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3896 if (tmp == NULL) goto failed;
3897 if (!PyList_Check(tmp)) {
3898 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3899 goto failed;
3900 }
3901 len = PyList_GET_SIZE(tmp);
3902 body = asdl_seq_new(len, arena);
3903 if (body == NULL) goto failed;
3904 for (i = 0; i < len; i++) {
3905 stmt_ty value;
3906 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3907 if (res != 0) goto failed;
3908 asdl_seq_SET(body, i, value);
3909 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003910 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003911 } else {
3912 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3913 return 1;
3914 }
3915 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3916 int res;
3917 Py_ssize_t len;
3918 Py_ssize_t i;
3919 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3920 if (tmp == NULL) goto failed;
3921 if (!PyList_Check(tmp)) {
3922 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3923 goto failed;
3924 }
3925 len = PyList_GET_SIZE(tmp);
3926 decorator_list = asdl_seq_new(len, arena);
3927 if (decorator_list == NULL) goto failed;
3928 for (i = 0; i < len; i++) {
3929 expr_ty value;
3930 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3931 if (res != 0) goto failed;
3932 asdl_seq_SET(decorator_list, i, value);
3933 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003934 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003935 } else {
3936 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3937 return 1;
3938 }
3939 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3940 decorator_list, lineno, col_offset, arena);
3941 if (*out == NULL) goto failed;
3942 return 0;
3943 }
3944 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3945 if (isinstance == -1) {
3946 return 1;
3947 }
3948 if (isinstance) {
3949 expr_ty value;
3950
3951 if (_PyObject_HasAttrId(obj, &PyId_value)) {
3952 int res;
3953 tmp = _PyObject_GetAttrId(obj, &PyId_value);
3954 if (tmp == NULL) goto failed;
3955 res = obj2ast_expr(tmp, &value, arena);
3956 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003957 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003958 } else {
3959 value = NULL;
3960 }
3961 *out = Return(value, lineno, col_offset, arena);
3962 if (*out == NULL) goto failed;
3963 return 0;
3964 }
3965 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3966 if (isinstance == -1) {
3967 return 1;
3968 }
3969 if (isinstance) {
3970 asdl_seq* targets;
3971
3972 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
3973 int res;
3974 Py_ssize_t len;
3975 Py_ssize_t i;
3976 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
3977 if (tmp == NULL) goto failed;
3978 if (!PyList_Check(tmp)) {
3979 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3980 goto failed;
3981 }
3982 len = PyList_GET_SIZE(tmp);
3983 targets = asdl_seq_new(len, arena);
3984 if (targets == NULL) goto failed;
3985 for (i = 0; i < len; i++) {
3986 expr_ty value;
3987 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3988 if (res != 0) goto failed;
3989 asdl_seq_SET(targets, i, value);
3990 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003991 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003992 } else {
3993 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3994 return 1;
3995 }
3996 *out = Delete(targets, lineno, col_offset, arena);
3997 if (*out == NULL) goto failed;
3998 return 0;
3999 }
4000 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4001 if (isinstance == -1) {
4002 return 1;
4003 }
4004 if (isinstance) {
4005 asdl_seq* targets;
4006 expr_ty value;
4007
4008 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4009 int res;
4010 Py_ssize_t len;
4011 Py_ssize_t i;
4012 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4013 if (tmp == NULL) goto failed;
4014 if (!PyList_Check(tmp)) {
4015 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4016 goto failed;
4017 }
4018 len = PyList_GET_SIZE(tmp);
4019 targets = asdl_seq_new(len, arena);
4020 if (targets == NULL) goto failed;
4021 for (i = 0; i < len; i++) {
4022 expr_ty value;
4023 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4024 if (res != 0) goto failed;
4025 asdl_seq_SET(targets, i, value);
4026 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004027 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004028 } else {
4029 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4030 return 1;
4031 }
4032 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4033 int res;
4034 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4035 if (tmp == NULL) goto failed;
4036 res = obj2ast_expr(tmp, &value, arena);
4037 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004038 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004039 } else {
4040 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4041 return 1;
4042 }
4043 *out = Assign(targets, value, lineno, col_offset, arena);
4044 if (*out == NULL) goto failed;
4045 return 0;
4046 }
4047 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4048 if (isinstance == -1) {
4049 return 1;
4050 }
4051 if (isinstance) {
4052 expr_ty target;
4053 operator_ty op;
4054 expr_ty value;
4055
4056 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4057 int res;
4058 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4059 if (tmp == NULL) goto failed;
4060 res = obj2ast_expr(tmp, &target, arena);
4061 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004062 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004063 } else {
4064 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4065 return 1;
4066 }
4067 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4068 int res;
4069 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4070 if (tmp == NULL) goto failed;
4071 res = obj2ast_operator(tmp, &op, arena);
4072 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004073 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004074 } else {
4075 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4076 return 1;
4077 }
4078 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4079 int res;
4080 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4081 if (tmp == NULL) goto failed;
4082 res = obj2ast_expr(tmp, &value, arena);
4083 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004084 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004085 } else {
4086 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4087 return 1;
4088 }
4089 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4090 if (*out == NULL) goto failed;
4091 return 0;
4092 }
4093 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4094 if (isinstance == -1) {
4095 return 1;
4096 }
4097 if (isinstance) {
4098 expr_ty target;
4099 expr_ty iter;
4100 asdl_seq* body;
4101 asdl_seq* orelse;
4102
4103 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4104 int res;
4105 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4106 if (tmp == NULL) goto failed;
4107 res = obj2ast_expr(tmp, &target, arena);
4108 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004109 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004110 } else {
4111 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4112 return 1;
4113 }
4114 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4115 int res;
4116 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4117 if (tmp == NULL) goto failed;
4118 res = obj2ast_expr(tmp, &iter, arena);
4119 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004120 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004121 } else {
4122 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4123 return 1;
4124 }
4125 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4126 int res;
4127 Py_ssize_t len;
4128 Py_ssize_t i;
4129 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4130 if (tmp == NULL) goto failed;
4131 if (!PyList_Check(tmp)) {
4132 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4133 goto failed;
4134 }
4135 len = PyList_GET_SIZE(tmp);
4136 body = asdl_seq_new(len, arena);
4137 if (body == NULL) goto failed;
4138 for (i = 0; i < len; i++) {
4139 stmt_ty value;
4140 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4141 if (res != 0) goto failed;
4142 asdl_seq_SET(body, i, value);
4143 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004144 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004145 } else {
4146 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4147 return 1;
4148 }
4149 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4150 int res;
4151 Py_ssize_t len;
4152 Py_ssize_t i;
4153 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4154 if (tmp == NULL) goto failed;
4155 if (!PyList_Check(tmp)) {
4156 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4157 goto failed;
4158 }
4159 len = PyList_GET_SIZE(tmp);
4160 orelse = asdl_seq_new(len, arena);
4161 if (orelse == NULL) goto failed;
4162 for (i = 0; i < len; i++) {
4163 stmt_ty value;
4164 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4165 if (res != 0) goto failed;
4166 asdl_seq_SET(orelse, i, value);
4167 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004168 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004169 } else {
4170 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4171 return 1;
4172 }
4173 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4174 if (*out == NULL) goto failed;
4175 return 0;
4176 }
4177 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4178 if (isinstance == -1) {
4179 return 1;
4180 }
4181 if (isinstance) {
4182 expr_ty test;
4183 asdl_seq* body;
4184 asdl_seq* orelse;
4185
4186 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4187 int res;
4188 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4189 if (tmp == NULL) goto failed;
4190 res = obj2ast_expr(tmp, &test, arena);
4191 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004192 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004193 } else {
4194 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4195 return 1;
4196 }
4197 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4198 int res;
4199 Py_ssize_t len;
4200 Py_ssize_t i;
4201 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4202 if (tmp == NULL) goto failed;
4203 if (!PyList_Check(tmp)) {
4204 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4205 goto failed;
4206 }
4207 len = PyList_GET_SIZE(tmp);
4208 body = asdl_seq_new(len, arena);
4209 if (body == NULL) goto failed;
4210 for (i = 0; i < len; i++) {
4211 stmt_ty value;
4212 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4213 if (res != 0) goto failed;
4214 asdl_seq_SET(body, i, value);
4215 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004216 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004217 } else {
4218 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4219 return 1;
4220 }
4221 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4222 int res;
4223 Py_ssize_t len;
4224 Py_ssize_t i;
4225 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4226 if (tmp == NULL) goto failed;
4227 if (!PyList_Check(tmp)) {
4228 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4229 goto failed;
4230 }
4231 len = PyList_GET_SIZE(tmp);
4232 orelse = asdl_seq_new(len, arena);
4233 if (orelse == NULL) goto failed;
4234 for (i = 0; i < len; i++) {
4235 stmt_ty value;
4236 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4237 if (res != 0) goto failed;
4238 asdl_seq_SET(orelse, i, value);
4239 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004240 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004241 } else {
4242 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4243 return 1;
4244 }
4245 *out = While(test, body, orelse, lineno, col_offset, arena);
4246 if (*out == NULL) goto failed;
4247 return 0;
4248 }
4249 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4250 if (isinstance == -1) {
4251 return 1;
4252 }
4253 if (isinstance) {
4254 expr_ty test;
4255 asdl_seq* body;
4256 asdl_seq* orelse;
4257
4258 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4259 int res;
4260 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4261 if (tmp == NULL) goto failed;
4262 res = obj2ast_expr(tmp, &test, arena);
4263 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004264 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004265 } else {
4266 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4267 return 1;
4268 }
4269 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4270 int res;
4271 Py_ssize_t len;
4272 Py_ssize_t i;
4273 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4274 if (tmp == NULL) goto failed;
4275 if (!PyList_Check(tmp)) {
4276 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4277 goto failed;
4278 }
4279 len = PyList_GET_SIZE(tmp);
4280 body = asdl_seq_new(len, arena);
4281 if (body == NULL) goto failed;
4282 for (i = 0; i < len; i++) {
4283 stmt_ty value;
4284 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4285 if (res != 0) goto failed;
4286 asdl_seq_SET(body, i, value);
4287 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004288 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004289 } else {
4290 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4291 return 1;
4292 }
4293 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4294 int res;
4295 Py_ssize_t len;
4296 Py_ssize_t i;
4297 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4298 if (tmp == NULL) goto failed;
4299 if (!PyList_Check(tmp)) {
4300 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4301 goto failed;
4302 }
4303 len = PyList_GET_SIZE(tmp);
4304 orelse = asdl_seq_new(len, arena);
4305 if (orelse == NULL) goto failed;
4306 for (i = 0; i < len; i++) {
4307 stmt_ty value;
4308 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4309 if (res != 0) goto failed;
4310 asdl_seq_SET(orelse, i, value);
4311 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004312 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004313 } else {
4314 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4315 return 1;
4316 }
4317 *out = If(test, body, orelse, lineno, col_offset, arena);
4318 if (*out == NULL) goto failed;
4319 return 0;
4320 }
4321 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4322 if (isinstance == -1) {
4323 return 1;
4324 }
4325 if (isinstance) {
4326 asdl_seq* items;
4327 asdl_seq* body;
4328
4329 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4330 int res;
4331 Py_ssize_t len;
4332 Py_ssize_t i;
4333 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4334 if (tmp == NULL) goto failed;
4335 if (!PyList_Check(tmp)) {
4336 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4337 goto failed;
4338 }
4339 len = PyList_GET_SIZE(tmp);
4340 items = asdl_seq_new(len, arena);
4341 if (items == NULL) goto failed;
4342 for (i = 0; i < len; i++) {
4343 withitem_ty value;
4344 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4345 if (res != 0) goto failed;
4346 asdl_seq_SET(items, i, value);
4347 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004348 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004349 } else {
4350 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4351 return 1;
4352 }
4353 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4354 int res;
4355 Py_ssize_t len;
4356 Py_ssize_t i;
4357 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4358 if (tmp == NULL) goto failed;
4359 if (!PyList_Check(tmp)) {
4360 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4361 goto failed;
4362 }
4363 len = PyList_GET_SIZE(tmp);
4364 body = asdl_seq_new(len, arena);
4365 if (body == NULL) goto failed;
4366 for (i = 0; i < len; i++) {
4367 stmt_ty value;
4368 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4369 if (res != 0) goto failed;
4370 asdl_seq_SET(body, i, value);
4371 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004372 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004373 } else {
4374 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4375 return 1;
4376 }
4377 *out = With(items, body, lineno, col_offset, arena);
4378 if (*out == NULL) goto failed;
4379 return 0;
4380 }
4381 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4382 if (isinstance == -1) {
4383 return 1;
4384 }
4385 if (isinstance) {
4386 expr_ty exc;
4387 expr_ty cause;
4388
4389 if (_PyObject_HasAttrId(obj, &PyId_exc)) {
4390 int res;
4391 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4392 if (tmp == NULL) goto failed;
4393 res = obj2ast_expr(tmp, &exc, arena);
4394 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004395 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004396 } else {
4397 exc = NULL;
4398 }
4399 if (_PyObject_HasAttrId(obj, &PyId_cause)) {
4400 int res;
4401 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4402 if (tmp == NULL) goto failed;
4403 res = obj2ast_expr(tmp, &cause, arena);
4404 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004405 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004406 } else {
4407 cause = NULL;
4408 }
4409 *out = Raise(exc, cause, lineno, col_offset, arena);
4410 if (*out == NULL) goto failed;
4411 return 0;
4412 }
4413 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4414 if (isinstance == -1) {
4415 return 1;
4416 }
4417 if (isinstance) {
4418 asdl_seq* body;
4419 asdl_seq* handlers;
4420 asdl_seq* orelse;
4421 asdl_seq* finalbody;
4422
4423 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4424 int res;
4425 Py_ssize_t len;
4426 Py_ssize_t i;
4427 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4428 if (tmp == NULL) goto failed;
4429 if (!PyList_Check(tmp)) {
4430 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4431 goto failed;
4432 }
4433 len = PyList_GET_SIZE(tmp);
4434 body = asdl_seq_new(len, arena);
4435 if (body == NULL) goto failed;
4436 for (i = 0; i < len; i++) {
4437 stmt_ty value;
4438 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4439 if (res != 0) goto failed;
4440 asdl_seq_SET(body, i, value);
4441 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004442 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004443 } else {
4444 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4445 return 1;
4446 }
4447 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4448 int res;
4449 Py_ssize_t len;
4450 Py_ssize_t i;
4451 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4452 if (tmp == NULL) goto failed;
4453 if (!PyList_Check(tmp)) {
4454 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4455 goto failed;
4456 }
4457 len = PyList_GET_SIZE(tmp);
4458 handlers = asdl_seq_new(len, arena);
4459 if (handlers == NULL) goto failed;
4460 for (i = 0; i < len; i++) {
4461 excepthandler_ty value;
4462 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4463 if (res != 0) goto failed;
4464 asdl_seq_SET(handlers, i, value);
4465 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004466 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 } else {
4468 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4469 return 1;
4470 }
4471 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4472 int res;
4473 Py_ssize_t len;
4474 Py_ssize_t i;
4475 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4476 if (tmp == NULL) goto failed;
4477 if (!PyList_Check(tmp)) {
4478 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479 goto failed;
4480 }
4481 len = PyList_GET_SIZE(tmp);
4482 orelse = asdl_seq_new(len, arena);
4483 if (orelse == NULL) goto failed;
4484 for (i = 0; i < len; i++) {
4485 stmt_ty value;
4486 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4487 if (res != 0) goto failed;
4488 asdl_seq_SET(orelse, i, value);
4489 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004490 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004491 } else {
4492 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4493 return 1;
4494 }
4495 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4496 int res;
4497 Py_ssize_t len;
4498 Py_ssize_t i;
4499 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4500 if (tmp == NULL) goto failed;
4501 if (!PyList_Check(tmp)) {
4502 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4503 goto failed;
4504 }
4505 len = PyList_GET_SIZE(tmp);
4506 finalbody = asdl_seq_new(len, arena);
4507 if (finalbody == NULL) goto failed;
4508 for (i = 0; i < len; i++) {
4509 stmt_ty value;
4510 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4511 if (res != 0) goto failed;
4512 asdl_seq_SET(finalbody, i, value);
4513 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004514 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004515 } else {
4516 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4517 return 1;
4518 }
4519 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4520 arena);
4521 if (*out == NULL) goto failed;
4522 return 0;
4523 }
4524 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4525 if (isinstance == -1) {
4526 return 1;
4527 }
4528 if (isinstance) {
4529 expr_ty test;
4530 expr_ty msg;
4531
4532 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4533 int res;
4534 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4535 if (tmp == NULL) goto failed;
4536 res = obj2ast_expr(tmp, &test, arena);
4537 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004539 } else {
4540 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4541 return 1;
4542 }
4543 if (_PyObject_HasAttrId(obj, &PyId_msg)) {
4544 int res;
4545 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4546 if (tmp == NULL) goto failed;
4547 res = obj2ast_expr(tmp, &msg, arena);
4548 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004549 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004550 } else {
4551 msg = NULL;
4552 }
4553 *out = Assert(test, msg, lineno, col_offset, arena);
4554 if (*out == NULL) goto failed;
4555 return 0;
4556 }
4557 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4558 if (isinstance == -1) {
4559 return 1;
4560 }
4561 if (isinstance) {
4562 asdl_seq* names;
4563
4564 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4565 int res;
4566 Py_ssize_t len;
4567 Py_ssize_t i;
4568 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4569 if (tmp == NULL) goto failed;
4570 if (!PyList_Check(tmp)) {
4571 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4572 goto failed;
4573 }
4574 len = PyList_GET_SIZE(tmp);
4575 names = asdl_seq_new(len, arena);
4576 if (names == NULL) goto failed;
4577 for (i = 0; i < len; i++) {
4578 alias_ty value;
4579 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4580 if (res != 0) goto failed;
4581 asdl_seq_SET(names, i, value);
4582 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004583 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004584 } else {
4585 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4586 return 1;
4587 }
4588 *out = Import(names, lineno, col_offset, arena);
4589 if (*out == NULL) goto failed;
4590 return 0;
4591 }
4592 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4593 if (isinstance == -1) {
4594 return 1;
4595 }
4596 if (isinstance) {
4597 identifier module;
4598 asdl_seq* names;
4599 int level;
4600
4601 if (_PyObject_HasAttrId(obj, &PyId_module)) {
4602 int res;
4603 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4604 if (tmp == NULL) goto failed;
4605 res = obj2ast_identifier(tmp, &module, arena);
4606 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004607 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004608 } else {
4609 module = NULL;
4610 }
4611 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4612 int res;
4613 Py_ssize_t len;
4614 Py_ssize_t i;
4615 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4616 if (tmp == NULL) goto failed;
4617 if (!PyList_Check(tmp)) {
4618 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4619 goto failed;
4620 }
4621 len = PyList_GET_SIZE(tmp);
4622 names = asdl_seq_new(len, arena);
4623 if (names == NULL) goto failed;
4624 for (i = 0; i < len; i++) {
4625 alias_ty value;
4626 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4627 if (res != 0) goto failed;
4628 asdl_seq_SET(names, i, value);
4629 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004630 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004631 } else {
4632 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4633 return 1;
4634 }
4635 if (_PyObject_HasAttrId(obj, &PyId_level)) {
4636 int res;
4637 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4638 if (tmp == NULL) goto failed;
4639 res = obj2ast_int(tmp, &level, arena);
4640 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004641 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004642 } else {
4643 level = 0;
4644 }
4645 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4646 if (*out == NULL) goto failed;
4647 return 0;
4648 }
4649 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4650 if (isinstance == -1) {
4651 return 1;
4652 }
4653 if (isinstance) {
4654 asdl_seq* names;
4655
4656 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4657 int res;
4658 Py_ssize_t len;
4659 Py_ssize_t i;
4660 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4661 if (tmp == NULL) goto failed;
4662 if (!PyList_Check(tmp)) {
4663 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4664 goto failed;
4665 }
4666 len = PyList_GET_SIZE(tmp);
4667 names = asdl_seq_new(len, arena);
4668 if (names == NULL) goto failed;
4669 for (i = 0; i < len; i++) {
4670 identifier value;
4671 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4672 if (res != 0) goto failed;
4673 asdl_seq_SET(names, i, value);
4674 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004675 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004676 } else {
4677 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4678 return 1;
4679 }
4680 *out = Global(names, lineno, col_offset, arena);
4681 if (*out == NULL) goto failed;
4682 return 0;
4683 }
4684 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4685 if (isinstance == -1) {
4686 return 1;
4687 }
4688 if (isinstance) {
4689 asdl_seq* names;
4690
4691 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4692 int res;
4693 Py_ssize_t len;
4694 Py_ssize_t i;
4695 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4696 if (tmp == NULL) goto failed;
4697 if (!PyList_Check(tmp)) {
4698 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4699 goto failed;
4700 }
4701 len = PyList_GET_SIZE(tmp);
4702 names = asdl_seq_new(len, arena);
4703 if (names == NULL) goto failed;
4704 for (i = 0; i < len; i++) {
4705 identifier value;
4706 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4707 if (res != 0) goto failed;
4708 asdl_seq_SET(names, i, value);
4709 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004710 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004711 } else {
4712 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4713 return 1;
4714 }
4715 *out = Nonlocal(names, lineno, col_offset, arena);
4716 if (*out == NULL) goto failed;
4717 return 0;
4718 }
4719 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4720 if (isinstance == -1) {
4721 return 1;
4722 }
4723 if (isinstance) {
4724 expr_ty value;
4725
4726 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4727 int res;
4728 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4729 if (tmp == NULL) goto failed;
4730 res = obj2ast_expr(tmp, &value, arena);
4731 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004732 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004733 } else {
4734 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4735 return 1;
4736 }
4737 *out = Expr(value, lineno, col_offset, arena);
4738 if (*out == NULL) goto failed;
4739 return 0;
4740 }
4741 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4742 if (isinstance == -1) {
4743 return 1;
4744 }
4745 if (isinstance) {
4746
4747 *out = Pass(lineno, col_offset, arena);
4748 if (*out == NULL) goto failed;
4749 return 0;
4750 }
4751 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4752 if (isinstance == -1) {
4753 return 1;
4754 }
4755 if (isinstance) {
4756
4757 *out = Break(lineno, col_offset, arena);
4758 if (*out == NULL) goto failed;
4759 return 0;
4760 }
4761 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4762 if (isinstance == -1) {
4763 return 1;
4764 }
4765 if (isinstance) {
4766
4767 *out = Continue(lineno, col_offset, arena);
4768 if (*out == NULL) goto failed;
4769 return 0;
4770 }
4771
4772 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4773 failed:
4774 Py_XDECREF(tmp);
4775 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004776}
4777
4778int
4779obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4780{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004781 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004782
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004783 PyObject *tmp = NULL;
4784 int lineno;
4785 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004786
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004787 if (obj == Py_None) {
4788 *out = NULL;
4789 return 0;
4790 }
4791 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4792 int res;
4793 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4794 if (tmp == NULL) goto failed;
4795 res = obj2ast_int(tmp, &lineno, arena);
4796 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004798 } else {
4799 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004800 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004801 }
4802 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4803 int res;
4804 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4805 if (tmp == NULL) goto failed;
4806 res = obj2ast_int(tmp, &col_offset, arena);
4807 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004808 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004809 } else {
4810 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4811 return 1;
4812 }
4813 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4814 if (isinstance == -1) {
4815 return 1;
4816 }
4817 if (isinstance) {
4818 boolop_ty op;
4819 asdl_seq* values;
4820
4821 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4822 int res;
4823 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4824 if (tmp == NULL) goto failed;
4825 res = obj2ast_boolop(tmp, &op, arena);
4826 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004827 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004828 } else {
4829 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4830 return 1;
4831 }
4832 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4833 int res;
4834 Py_ssize_t len;
4835 Py_ssize_t i;
4836 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4837 if (tmp == NULL) goto failed;
4838 if (!PyList_Check(tmp)) {
4839 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4840 goto failed;
4841 }
4842 len = PyList_GET_SIZE(tmp);
4843 values = asdl_seq_new(len, arena);
4844 if (values == NULL) goto failed;
4845 for (i = 0; i < len; i++) {
4846 expr_ty value;
4847 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4848 if (res != 0) goto failed;
4849 asdl_seq_SET(values, i, value);
4850 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004851 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004852 } else {
4853 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4854 return 1;
4855 }
4856 *out = BoolOp(op, values, lineno, col_offset, arena);
4857 if (*out == NULL) goto failed;
4858 return 0;
4859 }
4860 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4861 if (isinstance == -1) {
4862 return 1;
4863 }
4864 if (isinstance) {
4865 expr_ty left;
4866 operator_ty op;
4867 expr_ty right;
4868
4869 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4870 int res;
4871 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4872 if (tmp == NULL) goto failed;
4873 res = obj2ast_expr(tmp, &left, arena);
4874 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004875 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004876 } else {
4877 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4878 return 1;
4879 }
4880 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4881 int res;
4882 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4883 if (tmp == NULL) goto failed;
4884 res = obj2ast_operator(tmp, &op, arena);
4885 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004886 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004887 } else {
4888 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4889 return 1;
4890 }
4891 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4892 int res;
4893 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4894 if (tmp == NULL) goto failed;
4895 res = obj2ast_expr(tmp, &right, arena);
4896 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004897 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004898 } else {
4899 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4900 return 1;
4901 }
4902 *out = BinOp(left, op, right, lineno, col_offset, arena);
4903 if (*out == NULL) goto failed;
4904 return 0;
4905 }
4906 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4907 if (isinstance == -1) {
4908 return 1;
4909 }
4910 if (isinstance) {
4911 unaryop_ty op;
4912 expr_ty operand;
4913
4914 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4915 int res;
4916 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4917 if (tmp == NULL) goto failed;
4918 res = obj2ast_unaryop(tmp, &op, arena);
4919 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004920 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004921 } else {
4922 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4923 return 1;
4924 }
4925 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4926 int res;
4927 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4928 if (tmp == NULL) goto failed;
4929 res = obj2ast_expr(tmp, &operand, arena);
4930 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004931 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004932 } else {
4933 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4934 return 1;
4935 }
4936 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4937 if (*out == NULL) goto failed;
4938 return 0;
4939 }
4940 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4941 if (isinstance == -1) {
4942 return 1;
4943 }
4944 if (isinstance) {
4945 arguments_ty args;
4946 expr_ty body;
4947
4948 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4949 int res;
4950 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4951 if (tmp == NULL) goto failed;
4952 res = obj2ast_arguments(tmp, &args, arena);
4953 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004954 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004955 } else {
4956 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4957 return 1;
4958 }
4959 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4960 int res;
4961 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4962 if (tmp == NULL) goto failed;
4963 res = obj2ast_expr(tmp, &body, arena);
4964 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004965 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004966 } else {
4967 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4968 return 1;
4969 }
4970 *out = Lambda(args, body, lineno, col_offset, arena);
4971 if (*out == NULL) goto failed;
4972 return 0;
4973 }
4974 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4975 if (isinstance == -1) {
4976 return 1;
4977 }
4978 if (isinstance) {
4979 expr_ty test;
4980 expr_ty body;
4981 expr_ty orelse;
4982
4983 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4984 int res;
4985 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4986 if (tmp == NULL) goto failed;
4987 res = obj2ast_expr(tmp, &test, arena);
4988 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004989 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004990 } else {
4991 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4992 return 1;
4993 }
4994 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4995 int res;
4996 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4997 if (tmp == NULL) goto failed;
4998 res = obj2ast_expr(tmp, &body, arena);
4999 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005000 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005001 } else {
5002 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5003 return 1;
5004 }
5005 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5006 int res;
5007 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5008 if (tmp == NULL) goto failed;
5009 res = obj2ast_expr(tmp, &orelse, arena);
5010 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005011 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005012 } else {
5013 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5014 return 1;
5015 }
5016 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5017 if (*out == NULL) goto failed;
5018 return 0;
5019 }
5020 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5021 if (isinstance == -1) {
5022 return 1;
5023 }
5024 if (isinstance) {
5025 asdl_seq* keys;
5026 asdl_seq* values;
5027
5028 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5029 int res;
5030 Py_ssize_t len;
5031 Py_ssize_t i;
5032 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5033 if (tmp == NULL) goto failed;
5034 if (!PyList_Check(tmp)) {
5035 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5036 goto failed;
5037 }
5038 len = PyList_GET_SIZE(tmp);
5039 keys = asdl_seq_new(len, arena);
5040 if (keys == NULL) goto failed;
5041 for (i = 0; i < len; i++) {
5042 expr_ty value;
5043 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5044 if (res != 0) goto failed;
5045 asdl_seq_SET(keys, i, value);
5046 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005048 } else {
5049 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5050 return 1;
5051 }
5052 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5053 int res;
5054 Py_ssize_t len;
5055 Py_ssize_t i;
5056 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5057 if (tmp == NULL) goto failed;
5058 if (!PyList_Check(tmp)) {
5059 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5060 goto failed;
5061 }
5062 len = PyList_GET_SIZE(tmp);
5063 values = asdl_seq_new(len, arena);
5064 if (values == NULL) goto failed;
5065 for (i = 0; i < len; i++) {
5066 expr_ty value;
5067 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5068 if (res != 0) goto failed;
5069 asdl_seq_SET(values, i, value);
5070 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005071 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005072 } else {
5073 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5074 return 1;
5075 }
5076 *out = Dict(keys, values, lineno, col_offset, arena);
5077 if (*out == NULL) goto failed;
5078 return 0;
5079 }
5080 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5081 if (isinstance == -1) {
5082 return 1;
5083 }
5084 if (isinstance) {
5085 asdl_seq* elts;
5086
5087 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5088 int res;
5089 Py_ssize_t len;
5090 Py_ssize_t i;
5091 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5092 if (tmp == NULL) goto failed;
5093 if (!PyList_Check(tmp)) {
5094 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5095 goto failed;
5096 }
5097 len = PyList_GET_SIZE(tmp);
5098 elts = asdl_seq_new(len, arena);
5099 if (elts == NULL) goto failed;
5100 for (i = 0; i < len; i++) {
5101 expr_ty value;
5102 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5103 if (res != 0) goto failed;
5104 asdl_seq_SET(elts, i, value);
5105 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005106 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005107 } else {
5108 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5109 return 1;
5110 }
5111 *out = Set(elts, lineno, col_offset, arena);
5112 if (*out == NULL) goto failed;
5113 return 0;
5114 }
5115 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5116 if (isinstance == -1) {
5117 return 1;
5118 }
5119 if (isinstance) {
5120 expr_ty elt;
5121 asdl_seq* generators;
5122
5123 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5124 int res;
5125 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5126 if (tmp == NULL) goto failed;
5127 res = obj2ast_expr(tmp, &elt, arena);
5128 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005129 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005130 } else {
5131 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5132 return 1;
5133 }
5134 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5135 int res;
5136 Py_ssize_t len;
5137 Py_ssize_t i;
5138 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5139 if (tmp == NULL) goto failed;
5140 if (!PyList_Check(tmp)) {
5141 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5142 goto failed;
5143 }
5144 len = PyList_GET_SIZE(tmp);
5145 generators = asdl_seq_new(len, arena);
5146 if (generators == NULL) goto failed;
5147 for (i = 0; i < len; i++) {
5148 comprehension_ty value;
5149 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5150 if (res != 0) goto failed;
5151 asdl_seq_SET(generators, i, value);
5152 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005153 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005154 } else {
5155 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5156 return 1;
5157 }
5158 *out = ListComp(elt, generators, lineno, col_offset, arena);
5159 if (*out == NULL) goto failed;
5160 return 0;
5161 }
5162 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5163 if (isinstance == -1) {
5164 return 1;
5165 }
5166 if (isinstance) {
5167 expr_ty elt;
5168 asdl_seq* generators;
5169
5170 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5171 int res;
5172 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5173 if (tmp == NULL) goto failed;
5174 res = obj2ast_expr(tmp, &elt, arena);
5175 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005176 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005177 } else {
5178 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5179 return 1;
5180 }
5181 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5182 int res;
5183 Py_ssize_t len;
5184 Py_ssize_t i;
5185 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5186 if (tmp == NULL) goto failed;
5187 if (!PyList_Check(tmp)) {
5188 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5189 goto failed;
5190 }
5191 len = PyList_GET_SIZE(tmp);
5192 generators = asdl_seq_new(len, arena);
5193 if (generators == NULL) goto failed;
5194 for (i = 0; i < len; i++) {
5195 comprehension_ty value;
5196 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5197 if (res != 0) goto failed;
5198 asdl_seq_SET(generators, i, value);
5199 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005200 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005201 } else {
5202 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5203 return 1;
5204 }
5205 *out = SetComp(elt, generators, lineno, col_offset, arena);
5206 if (*out == NULL) goto failed;
5207 return 0;
5208 }
5209 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5210 if (isinstance == -1) {
5211 return 1;
5212 }
5213 if (isinstance) {
5214 expr_ty key;
5215 expr_ty value;
5216 asdl_seq* generators;
5217
5218 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5219 int res;
5220 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5221 if (tmp == NULL) goto failed;
5222 res = obj2ast_expr(tmp, &key, arena);
5223 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005224 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005225 } else {
5226 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5227 return 1;
5228 }
5229 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5230 int res;
5231 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5232 if (tmp == NULL) goto failed;
5233 res = obj2ast_expr(tmp, &value, arena);
5234 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005235 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005236 } else {
5237 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5238 return 1;
5239 }
5240 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5241 int res;
5242 Py_ssize_t len;
5243 Py_ssize_t i;
5244 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5245 if (tmp == NULL) goto failed;
5246 if (!PyList_Check(tmp)) {
5247 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5248 goto failed;
5249 }
5250 len = PyList_GET_SIZE(tmp);
5251 generators = asdl_seq_new(len, arena);
5252 if (generators == NULL) goto failed;
5253 for (i = 0; i < len; i++) {
5254 comprehension_ty value;
5255 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5256 if (res != 0) goto failed;
5257 asdl_seq_SET(generators, i, value);
5258 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005259 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005260 } else {
5261 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5262 return 1;
5263 }
5264 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5265 if (*out == NULL) goto failed;
5266 return 0;
5267 }
5268 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5269 if (isinstance == -1) {
5270 return 1;
5271 }
5272 if (isinstance) {
5273 expr_ty elt;
5274 asdl_seq* generators;
5275
5276 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5277 int res;
5278 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5279 if (tmp == NULL) goto failed;
5280 res = obj2ast_expr(tmp, &elt, arena);
5281 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005282 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005283 } else {
5284 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5285 return 1;
5286 }
5287 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5288 int res;
5289 Py_ssize_t len;
5290 Py_ssize_t i;
5291 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5292 if (tmp == NULL) goto failed;
5293 if (!PyList_Check(tmp)) {
5294 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5295 goto failed;
5296 }
5297 len = PyList_GET_SIZE(tmp);
5298 generators = asdl_seq_new(len, arena);
5299 if (generators == NULL) goto failed;
5300 for (i = 0; i < len; i++) {
5301 comprehension_ty value;
5302 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5303 if (res != 0) goto failed;
5304 asdl_seq_SET(generators, i, value);
5305 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005306 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005307 } else {
5308 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5309 return 1;
5310 }
5311 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5312 if (*out == NULL) goto failed;
5313 return 0;
5314 }
5315 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5316 if (isinstance == -1) {
5317 return 1;
5318 }
5319 if (isinstance) {
5320 expr_ty value;
5321
5322 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5323 int res;
5324 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5325 if (tmp == NULL) goto failed;
5326 res = obj2ast_expr(tmp, &value, arena);
5327 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005328 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005329 } else {
5330 value = NULL;
5331 }
5332 *out = Yield(value, lineno, col_offset, arena);
5333 if (*out == NULL) goto failed;
5334 return 0;
5335 }
5336 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5337 if (isinstance == -1) {
5338 return 1;
5339 }
5340 if (isinstance) {
5341 expr_ty value;
5342
5343 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5344 int res;
5345 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5346 if (tmp == NULL) goto failed;
5347 res = obj2ast_expr(tmp, &value, arena);
5348 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005349 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005350 } else {
5351 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5352 return 1;
5353 }
5354 *out = YieldFrom(value, lineno, col_offset, arena);
5355 if (*out == NULL) goto failed;
5356 return 0;
5357 }
5358 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5359 if (isinstance == -1) {
5360 return 1;
5361 }
5362 if (isinstance) {
5363 expr_ty left;
5364 asdl_int_seq* ops;
5365 asdl_seq* comparators;
5366
5367 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5368 int res;
5369 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5370 if (tmp == NULL) goto failed;
5371 res = obj2ast_expr(tmp, &left, arena);
5372 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005373 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005374 } else {
5375 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5376 return 1;
5377 }
5378 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5379 int res;
5380 Py_ssize_t len;
5381 Py_ssize_t i;
5382 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5383 if (tmp == NULL) goto failed;
5384 if (!PyList_Check(tmp)) {
5385 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5386 goto failed;
5387 }
5388 len = PyList_GET_SIZE(tmp);
5389 ops = asdl_int_seq_new(len, arena);
5390 if (ops == NULL) goto failed;
5391 for (i = 0; i < len; i++) {
5392 cmpop_ty value;
5393 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5394 if (res != 0) goto failed;
5395 asdl_seq_SET(ops, i, value);
5396 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005397 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005398 } else {
5399 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5400 return 1;
5401 }
5402 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5403 int res;
5404 Py_ssize_t len;
5405 Py_ssize_t i;
5406 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5407 if (tmp == NULL) goto failed;
5408 if (!PyList_Check(tmp)) {
5409 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5410 goto failed;
5411 }
5412 len = PyList_GET_SIZE(tmp);
5413 comparators = asdl_seq_new(len, arena);
5414 if (comparators == NULL) goto failed;
5415 for (i = 0; i < len; i++) {
5416 expr_ty value;
5417 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5418 if (res != 0) goto failed;
5419 asdl_seq_SET(comparators, i, value);
5420 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005421 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005422 } else {
5423 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5424 return 1;
5425 }
5426 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5427 if (*out == NULL) goto failed;
5428 return 0;
5429 }
5430 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5431 if (isinstance == -1) {
5432 return 1;
5433 }
5434 if (isinstance) {
5435 expr_ty func;
5436 asdl_seq* args;
5437 asdl_seq* keywords;
5438 expr_ty starargs;
5439 expr_ty kwargs;
5440
5441 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5442 int res;
5443 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5444 if (tmp == NULL) goto failed;
5445 res = obj2ast_expr(tmp, &func, arena);
5446 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005447 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005448 } else {
5449 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5450 return 1;
5451 }
5452 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5453 int res;
5454 Py_ssize_t len;
5455 Py_ssize_t i;
5456 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5457 if (tmp == NULL) goto failed;
5458 if (!PyList_Check(tmp)) {
5459 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5460 goto failed;
5461 }
5462 len = PyList_GET_SIZE(tmp);
5463 args = asdl_seq_new(len, arena);
5464 if (args == NULL) goto failed;
5465 for (i = 0; i < len; i++) {
5466 expr_ty value;
5467 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5468 if (res != 0) goto failed;
5469 asdl_seq_SET(args, i, value);
5470 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005471 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005472 } else {
5473 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5474 return 1;
5475 }
5476 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5477 int res;
5478 Py_ssize_t len;
5479 Py_ssize_t i;
5480 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5481 if (tmp == NULL) goto failed;
5482 if (!PyList_Check(tmp)) {
5483 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5484 goto failed;
5485 }
5486 len = PyList_GET_SIZE(tmp);
5487 keywords = asdl_seq_new(len, arena);
5488 if (keywords == NULL) goto failed;
5489 for (i = 0; i < len; i++) {
5490 keyword_ty value;
5491 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5492 if (res != 0) goto failed;
5493 asdl_seq_SET(keywords, i, value);
5494 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005495 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005496 } else {
5497 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5498 return 1;
5499 }
5500 if (_PyObject_HasAttrId(obj, &PyId_starargs)) {
5501 int res;
5502 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5503 if (tmp == NULL) goto failed;
5504 res = obj2ast_expr(tmp, &starargs, arena);
5505 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005507 } else {
5508 starargs = NULL;
5509 }
5510 if (_PyObject_HasAttrId(obj, &PyId_kwargs)) {
5511 int res;
5512 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5513 if (tmp == NULL) goto failed;
5514 res = obj2ast_expr(tmp, &kwargs, arena);
5515 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005516 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005517 } else {
5518 kwargs = NULL;
5519 }
5520 *out = Call(func, args, keywords, starargs, kwargs, lineno, col_offset,
5521 arena);
5522 if (*out == NULL) goto failed;
5523 return 0;
5524 }
5525 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5526 if (isinstance == -1) {
5527 return 1;
5528 }
5529 if (isinstance) {
5530 object n;
5531
5532 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5533 int res;
5534 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5535 if (tmp == NULL) goto failed;
5536 res = obj2ast_object(tmp, &n, arena);
5537 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005539 } else {
5540 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5541 return 1;
5542 }
5543 *out = Num(n, lineno, col_offset, arena);
5544 if (*out == NULL) goto failed;
5545 return 0;
5546 }
5547 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5548 if (isinstance == -1) {
5549 return 1;
5550 }
5551 if (isinstance) {
5552 string s;
5553
5554 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5555 int res;
5556 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5557 if (tmp == NULL) goto failed;
5558 res = obj2ast_string(tmp, &s, arena);
5559 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005560 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005561 } else {
5562 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5563 return 1;
5564 }
5565 *out = Str(s, lineno, col_offset, arena);
5566 if (*out == NULL) goto failed;
5567 return 0;
5568 }
5569 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5570 if (isinstance == -1) {
5571 return 1;
5572 }
5573 if (isinstance) {
5574 bytes s;
5575
5576 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5577 int res;
5578 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5579 if (tmp == NULL) goto failed;
5580 res = obj2ast_bytes(tmp, &s, arena);
5581 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005582 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005583 } else {
5584 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5585 return 1;
5586 }
5587 *out = Bytes(s, lineno, col_offset, arena);
5588 if (*out == NULL) goto failed;
5589 return 0;
5590 }
5591 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5592 if (isinstance == -1) {
5593 return 1;
5594 }
5595 if (isinstance) {
5596
5597 *out = Ellipsis(lineno, col_offset, arena);
5598 if (*out == NULL) goto failed;
5599 return 0;
5600 }
5601 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5602 if (isinstance == -1) {
5603 return 1;
5604 }
5605 if (isinstance) {
5606 expr_ty value;
5607 identifier attr;
5608 expr_context_ty ctx;
5609
5610 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5611 int res;
5612 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5613 if (tmp == NULL) goto failed;
5614 res = obj2ast_expr(tmp, &value, arena);
5615 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005616 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005617 } else {
5618 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5619 return 1;
5620 }
5621 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5622 int res;
5623 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5624 if (tmp == NULL) goto failed;
5625 res = obj2ast_identifier(tmp, &attr, arena);
5626 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005627 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005628 } else {
5629 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5630 return 1;
5631 }
5632 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5633 int res;
5634 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5635 if (tmp == NULL) goto failed;
5636 res = obj2ast_expr_context(tmp, &ctx, arena);
5637 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005638 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005639 } else {
5640 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5641 return 1;
5642 }
5643 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5644 if (*out == NULL) goto failed;
5645 return 0;
5646 }
5647 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5648 if (isinstance == -1) {
5649 return 1;
5650 }
5651 if (isinstance) {
5652 expr_ty value;
5653 slice_ty slice;
5654 expr_context_ty ctx;
5655
5656 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5657 int res;
5658 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5659 if (tmp == NULL) goto failed;
5660 res = obj2ast_expr(tmp, &value, arena);
5661 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005662 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005663 } else {
5664 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5665 return 1;
5666 }
5667 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5668 int res;
5669 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5670 if (tmp == NULL) goto failed;
5671 res = obj2ast_slice(tmp, &slice, arena);
5672 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005673 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005674 } else {
5675 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5676 return 1;
5677 }
5678 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5679 int res;
5680 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5681 if (tmp == NULL) goto failed;
5682 res = obj2ast_expr_context(tmp, &ctx, arena);
5683 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005684 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005685 } else {
5686 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5687 return 1;
5688 }
5689 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5690 if (*out == NULL) goto failed;
5691 return 0;
5692 }
5693 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5694 if (isinstance == -1) {
5695 return 1;
5696 }
5697 if (isinstance) {
5698 expr_ty value;
5699 expr_context_ty ctx;
5700
5701 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5702 int res;
5703 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5704 if (tmp == NULL) goto failed;
5705 res = obj2ast_expr(tmp, &value, arena);
5706 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005707 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005708 } else {
5709 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5710 return 1;
5711 }
5712 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5713 int res;
5714 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5715 if (tmp == NULL) goto failed;
5716 res = obj2ast_expr_context(tmp, &ctx, arena);
5717 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005719 } else {
5720 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5721 return 1;
5722 }
5723 *out = Starred(value, ctx, lineno, col_offset, arena);
5724 if (*out == NULL) goto failed;
5725 return 0;
5726 }
5727 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5728 if (isinstance == -1) {
5729 return 1;
5730 }
5731 if (isinstance) {
5732 identifier id;
5733 expr_context_ty ctx;
5734
5735 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5736 int res;
5737 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5738 if (tmp == NULL) goto failed;
5739 res = obj2ast_identifier(tmp, &id, arena);
5740 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005741 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005742 } else {
5743 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5744 return 1;
5745 }
5746 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5747 int res;
5748 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5749 if (tmp == NULL) goto failed;
5750 res = obj2ast_expr_context(tmp, &ctx, arena);
5751 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005752 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005753 } else {
5754 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5755 return 1;
5756 }
5757 *out = Name(id, ctx, lineno, col_offset, arena);
5758 if (*out == NULL) goto failed;
5759 return 0;
5760 }
5761 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5762 if (isinstance == -1) {
5763 return 1;
5764 }
5765 if (isinstance) {
5766 asdl_seq* elts;
5767 expr_context_ty ctx;
5768
5769 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5770 int res;
5771 Py_ssize_t len;
5772 Py_ssize_t i;
5773 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5774 if (tmp == NULL) goto failed;
5775 if (!PyList_Check(tmp)) {
5776 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5777 goto failed;
5778 }
5779 len = PyList_GET_SIZE(tmp);
5780 elts = asdl_seq_new(len, arena);
5781 if (elts == NULL) goto failed;
5782 for (i = 0; i < len; i++) {
5783 expr_ty value;
5784 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5785 if (res != 0) goto failed;
5786 asdl_seq_SET(elts, i, value);
5787 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005788 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005789 } else {
5790 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5791 return 1;
5792 }
5793 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5794 int res;
5795 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5796 if (tmp == NULL) goto failed;
5797 res = obj2ast_expr_context(tmp, &ctx, arena);
5798 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005799 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005800 } else {
5801 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5802 return 1;
5803 }
5804 *out = List(elts, ctx, lineno, col_offset, arena);
5805 if (*out == NULL) goto failed;
5806 return 0;
5807 }
5808 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5809 if (isinstance == -1) {
5810 return 1;
5811 }
5812 if (isinstance) {
5813 asdl_seq* elts;
5814 expr_context_ty ctx;
5815
5816 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5817 int res;
5818 Py_ssize_t len;
5819 Py_ssize_t i;
5820 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5821 if (tmp == NULL) goto failed;
5822 if (!PyList_Check(tmp)) {
5823 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5824 goto failed;
5825 }
5826 len = PyList_GET_SIZE(tmp);
5827 elts = asdl_seq_new(len, arena);
5828 if (elts == NULL) goto failed;
5829 for (i = 0; i < len; i++) {
5830 expr_ty value;
5831 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5832 if (res != 0) goto failed;
5833 asdl_seq_SET(elts, i, value);
5834 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005835 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005836 } else {
5837 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5838 return 1;
5839 }
5840 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5841 int res;
5842 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5843 if (tmp == NULL) goto failed;
5844 res = obj2ast_expr_context(tmp, &ctx, arena);
5845 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005846 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005847 } else {
5848 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5849 return 1;
5850 }
5851 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5852 if (*out == NULL) goto failed;
5853 return 0;
5854 }
5855
5856 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5857 failed:
5858 Py_XDECREF(tmp);
5859 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005860}
5861
5862int
5863obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5864{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005865 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005866
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005867 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5868 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005869 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005870 }
5871 if (isinstance) {
5872 *out = Load;
5873 return 0;
5874 }
5875 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5876 if (isinstance == -1) {
5877 return 1;
5878 }
5879 if (isinstance) {
5880 *out = Store;
5881 return 0;
5882 }
5883 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5884 if (isinstance == -1) {
5885 return 1;
5886 }
5887 if (isinstance) {
5888 *out = Del;
5889 return 0;
5890 }
5891 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5892 if (isinstance == -1) {
5893 return 1;
5894 }
5895 if (isinstance) {
5896 *out = AugLoad;
5897 return 0;
5898 }
5899 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5900 if (isinstance == -1) {
5901 return 1;
5902 }
5903 if (isinstance) {
5904 *out = AugStore;
5905 return 0;
5906 }
5907 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5908 if (isinstance == -1) {
5909 return 1;
5910 }
5911 if (isinstance) {
5912 *out = Param;
5913 return 0;
5914 }
5915
5916 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
5917 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005918}
5919
5920int
5921obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5922{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005923 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005924
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005925 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005926
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005927 if (obj == Py_None) {
5928 *out = NULL;
5929 return 0;
5930 }
5931 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5932 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005933 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005934 }
5935 if (isinstance) {
5936 expr_ty lower;
5937 expr_ty upper;
5938 expr_ty step;
5939
5940 if (_PyObject_HasAttrId(obj, &PyId_lower)) {
5941 int res;
5942 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
5943 if (tmp == NULL) goto failed;
5944 res = obj2ast_expr(tmp, &lower, arena);
5945 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005947 } else {
5948 lower = NULL;
5949 }
5950 if (_PyObject_HasAttrId(obj, &PyId_upper)) {
5951 int res;
5952 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
5953 if (tmp == NULL) goto failed;
5954 res = obj2ast_expr(tmp, &upper, arena);
5955 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005956 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005957 } else {
5958 upper = NULL;
5959 }
5960 if (_PyObject_HasAttrId(obj, &PyId_step)) {
5961 int res;
5962 tmp = _PyObject_GetAttrId(obj, &PyId_step);
5963 if (tmp == NULL) goto failed;
5964 res = obj2ast_expr(tmp, &step, arena);
5965 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005966 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005967 } else {
5968 step = NULL;
5969 }
5970 *out = Slice(lower, upper, step, arena);
5971 if (*out == NULL) goto failed;
5972 return 0;
5973 }
5974 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5975 if (isinstance == -1) {
5976 return 1;
5977 }
5978 if (isinstance) {
5979 asdl_seq* dims;
5980
5981 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
5982 int res;
5983 Py_ssize_t len;
5984 Py_ssize_t i;
5985 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
5986 if (tmp == NULL) goto failed;
5987 if (!PyList_Check(tmp)) {
5988 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5989 goto failed;
5990 }
5991 len = PyList_GET_SIZE(tmp);
5992 dims = asdl_seq_new(len, arena);
5993 if (dims == NULL) goto failed;
5994 for (i = 0; i < len; i++) {
5995 slice_ty value;
5996 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5997 if (res != 0) goto failed;
5998 asdl_seq_SET(dims, i, value);
5999 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006000 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006001 } else {
6002 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6003 return 1;
6004 }
6005 *out = ExtSlice(dims, arena);
6006 if (*out == NULL) goto failed;
6007 return 0;
6008 }
6009 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6010 if (isinstance == -1) {
6011 return 1;
6012 }
6013 if (isinstance) {
6014 expr_ty value;
6015
6016 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6017 int res;
6018 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6019 if (tmp == NULL) goto failed;
6020 res = obj2ast_expr(tmp, &value, arena);
6021 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006022 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006023 } else {
6024 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6025 return 1;
6026 }
6027 *out = Index(value, arena);
6028 if (*out == NULL) goto failed;
6029 return 0;
6030 }
6031
6032 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6033 failed:
6034 Py_XDECREF(tmp);
6035 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006036}
6037
6038int
6039obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6040{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006041 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006042
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006043 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6044 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006045 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006046 }
6047 if (isinstance) {
6048 *out = And;
6049 return 0;
6050 }
6051 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6052 if (isinstance == -1) {
6053 return 1;
6054 }
6055 if (isinstance) {
6056 *out = Or;
6057 return 0;
6058 }
6059
6060 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6061 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006062}
6063
6064int
6065obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6066{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006067 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006068
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006069 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6070 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006071 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006072 }
6073 if (isinstance) {
6074 *out = Add;
6075 return 0;
6076 }
6077 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6078 if (isinstance == -1) {
6079 return 1;
6080 }
6081 if (isinstance) {
6082 *out = Sub;
6083 return 0;
6084 }
6085 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6086 if (isinstance == -1) {
6087 return 1;
6088 }
6089 if (isinstance) {
6090 *out = Mult;
6091 return 0;
6092 }
6093 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6094 if (isinstance == -1) {
6095 return 1;
6096 }
6097 if (isinstance) {
6098 *out = Div;
6099 return 0;
6100 }
6101 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6102 if (isinstance == -1) {
6103 return 1;
6104 }
6105 if (isinstance) {
6106 *out = Mod;
6107 return 0;
6108 }
6109 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6110 if (isinstance == -1) {
6111 return 1;
6112 }
6113 if (isinstance) {
6114 *out = Pow;
6115 return 0;
6116 }
6117 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6118 if (isinstance == -1) {
6119 return 1;
6120 }
6121 if (isinstance) {
6122 *out = LShift;
6123 return 0;
6124 }
6125 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6126 if (isinstance == -1) {
6127 return 1;
6128 }
6129 if (isinstance) {
6130 *out = RShift;
6131 return 0;
6132 }
6133 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6134 if (isinstance == -1) {
6135 return 1;
6136 }
6137 if (isinstance) {
6138 *out = BitOr;
6139 return 0;
6140 }
6141 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6142 if (isinstance == -1) {
6143 return 1;
6144 }
6145 if (isinstance) {
6146 *out = BitXor;
6147 return 0;
6148 }
6149 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6150 if (isinstance == -1) {
6151 return 1;
6152 }
6153 if (isinstance) {
6154 *out = BitAnd;
6155 return 0;
6156 }
6157 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6158 if (isinstance == -1) {
6159 return 1;
6160 }
6161 if (isinstance) {
6162 *out = FloorDiv;
6163 return 0;
6164 }
6165
6166 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6167 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006168}
6169
6170int
6171obj2ast_unaryop(PyObject* obj, unaryop_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 *)Invert_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 = Invert;
6181 return 0;
6182 }
6183 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6184 if (isinstance == -1) {
6185 return 1;
6186 }
6187 if (isinstance) {
6188 *out = Not;
6189 return 0;
6190 }
6191 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6192 if (isinstance == -1) {
6193 return 1;
6194 }
6195 if (isinstance) {
6196 *out = UAdd;
6197 return 0;
6198 }
6199 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6200 if (isinstance == -1) {
6201 return 1;
6202 }
6203 if (isinstance) {
6204 *out = USub;
6205 return 0;
6206 }
6207
6208 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6209 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006210}
6211
6212int
6213obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6214{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006215 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006216
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006217 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6218 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006219 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006220 }
6221 if (isinstance) {
6222 *out = Eq;
6223 return 0;
6224 }
6225 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6226 if (isinstance == -1) {
6227 return 1;
6228 }
6229 if (isinstance) {
6230 *out = NotEq;
6231 return 0;
6232 }
6233 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6234 if (isinstance == -1) {
6235 return 1;
6236 }
6237 if (isinstance) {
6238 *out = Lt;
6239 return 0;
6240 }
6241 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6242 if (isinstance == -1) {
6243 return 1;
6244 }
6245 if (isinstance) {
6246 *out = LtE;
6247 return 0;
6248 }
6249 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6250 if (isinstance == -1) {
6251 return 1;
6252 }
6253 if (isinstance) {
6254 *out = Gt;
6255 return 0;
6256 }
6257 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6258 if (isinstance == -1) {
6259 return 1;
6260 }
6261 if (isinstance) {
6262 *out = GtE;
6263 return 0;
6264 }
6265 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6266 if (isinstance == -1) {
6267 return 1;
6268 }
6269 if (isinstance) {
6270 *out = Is;
6271 return 0;
6272 }
6273 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6274 if (isinstance == -1) {
6275 return 1;
6276 }
6277 if (isinstance) {
6278 *out = IsNot;
6279 return 0;
6280 }
6281 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6282 if (isinstance == -1) {
6283 return 1;
6284 }
6285 if (isinstance) {
6286 *out = In;
6287 return 0;
6288 }
6289 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6290 if (isinstance == -1) {
6291 return 1;
6292 }
6293 if (isinstance) {
6294 *out = NotIn;
6295 return 0;
6296 }
6297
6298 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6299 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006300}
6301
6302int
6303obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6304{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006305 PyObject* tmp = NULL;
6306 expr_ty target;
6307 expr_ty iter;
6308 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006309
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006310 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6311 int res;
6312 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6313 if (tmp == NULL) goto failed;
6314 res = obj2ast_expr(tmp, &target, arena);
6315 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006316 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006317 } else {
6318 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006319 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006320 }
6321 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6322 int res;
6323 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6324 if (tmp == NULL) goto failed;
6325 res = obj2ast_expr(tmp, &iter, arena);
6326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006327 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006328 } else {
6329 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6330 return 1;
6331 }
6332 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6333 int res;
6334 Py_ssize_t len;
6335 Py_ssize_t i;
6336 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6337 if (tmp == NULL) goto failed;
6338 if (!PyList_Check(tmp)) {
6339 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6340 goto failed;
6341 }
6342 len = PyList_GET_SIZE(tmp);
6343 ifs = asdl_seq_new(len, arena);
6344 if (ifs == NULL) goto failed;
6345 for (i = 0; i < len; i++) {
6346 expr_ty value;
6347 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6348 if (res != 0) goto failed;
6349 asdl_seq_SET(ifs, i, value);
6350 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006351 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006352 } else {
6353 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6354 return 1;
6355 }
6356 *out = comprehension(target, iter, ifs, arena);
6357 return 0;
6358failed:
6359 Py_XDECREF(tmp);
6360 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006361}
6362
6363int
6364obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6365{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006366 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006367
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006368 PyObject *tmp = NULL;
6369 int lineno;
6370 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006371
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006372 if (obj == Py_None) {
6373 *out = NULL;
6374 return 0;
6375 }
6376 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6377 int res;
6378 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6379 if (tmp == NULL) goto failed;
6380 res = obj2ast_int(tmp, &lineno, arena);
6381 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006382 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006383 } else {
6384 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006385 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386 }
6387 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6388 int res;
6389 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6390 if (tmp == NULL) goto failed;
6391 res = obj2ast_int(tmp, &col_offset, arena);
6392 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006393 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006394 } else {
6395 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6396 return 1;
6397 }
6398 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6399 if (isinstance == -1) {
6400 return 1;
6401 }
6402 if (isinstance) {
6403 expr_ty type;
6404 identifier name;
6405 asdl_seq* body;
6406
6407 if (_PyObject_HasAttrId(obj, &PyId_type)) {
6408 int res;
6409 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6410 if (tmp == NULL) goto failed;
6411 res = obj2ast_expr(tmp, &type, arena);
6412 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006413 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006414 } else {
6415 type = NULL;
6416 }
6417 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6418 int res;
6419 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6420 if (tmp == NULL) goto failed;
6421 res = obj2ast_identifier(tmp, &name, arena);
6422 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006423 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006424 } else {
6425 name = NULL;
6426 }
6427 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6428 int res;
6429 Py_ssize_t len;
6430 Py_ssize_t i;
6431 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6432 if (tmp == NULL) goto failed;
6433 if (!PyList_Check(tmp)) {
6434 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6435 goto failed;
6436 }
6437 len = PyList_GET_SIZE(tmp);
6438 body = asdl_seq_new(len, arena);
6439 if (body == NULL) goto failed;
6440 for (i = 0; i < len; i++) {
6441 stmt_ty value;
6442 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6443 if (res != 0) goto failed;
6444 asdl_seq_SET(body, i, value);
6445 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006446 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006447 } else {
6448 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6449 return 1;
6450 }
6451 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6452 if (*out == NULL) goto failed;
6453 return 0;
6454 }
6455
6456 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6457 failed:
6458 Py_XDECREF(tmp);
6459 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006460}
6461
6462int
6463obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6464{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006465 PyObject* tmp = NULL;
6466 asdl_seq* args;
6467 identifier vararg;
6468 expr_ty varargannotation;
6469 asdl_seq* kwonlyargs;
6470 identifier kwarg;
6471 expr_ty kwargannotation;
6472 asdl_seq* defaults;
6473 asdl_seq* kw_defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006474
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006475 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6476 int res;
6477 Py_ssize_t len;
6478 Py_ssize_t i;
6479 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6480 if (tmp == NULL) goto failed;
6481 if (!PyList_Check(tmp)) {
6482 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6483 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006484 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006485 len = PyList_GET_SIZE(tmp);
6486 args = asdl_seq_new(len, arena);
6487 if (args == NULL) goto failed;
6488 for (i = 0; i < len; i++) {
6489 arg_ty value;
6490 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6491 if (res != 0) goto failed;
6492 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006493 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006494 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006495 } else {
6496 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006497 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006498 }
6499 if (_PyObject_HasAttrId(obj, &PyId_vararg)) {
6500 int res;
6501 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6502 if (tmp == NULL) goto failed;
6503 res = obj2ast_identifier(tmp, &vararg, arena);
6504 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006505 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006506 } else {
6507 vararg = NULL;
6508 }
6509 if (_PyObject_HasAttrId(obj, &PyId_varargannotation)) {
6510 int res;
6511 tmp = _PyObject_GetAttrId(obj, &PyId_varargannotation);
6512 if (tmp == NULL) goto failed;
6513 res = obj2ast_expr(tmp, &varargannotation, arena);
6514 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006515 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006516 } else {
6517 varargannotation = NULL;
6518 }
6519 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6520 int res;
6521 Py_ssize_t len;
6522 Py_ssize_t i;
6523 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6524 if (tmp == NULL) goto failed;
6525 if (!PyList_Check(tmp)) {
6526 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6527 goto failed;
6528 }
6529 len = PyList_GET_SIZE(tmp);
6530 kwonlyargs = asdl_seq_new(len, arena);
6531 if (kwonlyargs == NULL) goto failed;
6532 for (i = 0; i < len; i++) {
6533 arg_ty value;
6534 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6535 if (res != 0) goto failed;
6536 asdl_seq_SET(kwonlyargs, i, value);
6537 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006539 } else {
6540 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6541 return 1;
6542 }
6543 if (_PyObject_HasAttrId(obj, &PyId_kwarg)) {
6544 int res;
6545 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6546 if (tmp == NULL) goto failed;
6547 res = obj2ast_identifier(tmp, &kwarg, arena);
6548 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006549 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 } else {
6551 kwarg = NULL;
6552 }
6553 if (_PyObject_HasAttrId(obj, &PyId_kwargannotation)) {
6554 int res;
6555 tmp = _PyObject_GetAttrId(obj, &PyId_kwargannotation);
6556 if (tmp == NULL) goto failed;
6557 res = obj2ast_expr(tmp, &kwargannotation, arena);
6558 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006559 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006560 } else {
6561 kwargannotation = NULL;
6562 }
6563 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6564 int res;
6565 Py_ssize_t len;
6566 Py_ssize_t i;
6567 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6568 if (tmp == NULL) goto failed;
6569 if (!PyList_Check(tmp)) {
6570 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6571 goto failed;
6572 }
6573 len = PyList_GET_SIZE(tmp);
6574 defaults = asdl_seq_new(len, arena);
6575 if (defaults == NULL) goto failed;
6576 for (i = 0; i < len; i++) {
6577 expr_ty value;
6578 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6579 if (res != 0) goto failed;
6580 asdl_seq_SET(defaults, i, value);
6581 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006582 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 } else {
6584 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6585 return 1;
6586 }
6587 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6588 int res;
6589 Py_ssize_t len;
6590 Py_ssize_t i;
6591 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6592 if (tmp == NULL) goto failed;
6593 if (!PyList_Check(tmp)) {
6594 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6595 goto failed;
6596 }
6597 len = PyList_GET_SIZE(tmp);
6598 kw_defaults = asdl_seq_new(len, arena);
6599 if (kw_defaults == NULL) goto failed;
6600 for (i = 0; i < len; i++) {
6601 expr_ty value;
6602 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6603 if (res != 0) goto failed;
6604 asdl_seq_SET(kw_defaults, i, value);
6605 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006606 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006607 } else {
6608 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6609 return 1;
6610 }
6611 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6612 kwargannotation, defaults, kw_defaults, arena);
6613 return 0;
6614failed:
6615 Py_XDECREF(tmp);
6616 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006617}
6618
6619int
6620obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6621{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006622 PyObject* tmp = NULL;
6623 identifier arg;
6624 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006625
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006626 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6627 int res;
6628 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6629 if (tmp == NULL) goto failed;
6630 res = obj2ast_identifier(tmp, &arg, arena);
6631 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006632 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006633 } else {
6634 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006635 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006636 }
6637 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
6638 int res;
6639 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6640 if (tmp == NULL) goto failed;
6641 res = obj2ast_expr(tmp, &annotation, arena);
6642 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006643 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006644 } else {
6645 annotation = NULL;
6646 }
6647 *out = arg(arg, annotation, arena);
6648 return 0;
6649failed:
6650 Py_XDECREF(tmp);
6651 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006652}
6653
6654int
6655obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6656{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006657 PyObject* tmp = NULL;
6658 identifier arg;
6659 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006660
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006661 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6662 int res;
6663 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6664 if (tmp == NULL) goto failed;
6665 res = obj2ast_identifier(tmp, &arg, arena);
6666 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006667 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006668 } else {
6669 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006670 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006671 }
6672 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6673 int res;
6674 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6675 if (tmp == NULL) goto failed;
6676 res = obj2ast_expr(tmp, &value, arena);
6677 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006678 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006679 } else {
6680 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6681 return 1;
6682 }
6683 *out = keyword(arg, value, arena);
6684 return 0;
6685failed:
6686 Py_XDECREF(tmp);
6687 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006688}
6689
6690int
6691obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6692{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006693 PyObject* tmp = NULL;
6694 identifier name;
6695 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006696
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006697 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6698 int res;
6699 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6700 if (tmp == NULL) goto failed;
6701 res = obj2ast_identifier(tmp, &name, arena);
6702 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006703 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006704 } else {
6705 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006706 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006707 }
6708 if (_PyObject_HasAttrId(obj, &PyId_asname)) {
6709 int res;
6710 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6711 if (tmp == NULL) goto failed;
6712 res = obj2ast_identifier(tmp, &asname, arena);
6713 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006714 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006715 } else {
6716 asname = NULL;
6717 }
6718 *out = alias(name, asname, arena);
6719 return 0;
6720failed:
6721 Py_XDECREF(tmp);
6722 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006723}
6724
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006725int
6726obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6727{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006728 PyObject* tmp = NULL;
6729 expr_ty context_expr;
6730 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006731
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006732 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6733 int res;
6734 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6735 if (tmp == NULL) goto failed;
6736 res = obj2ast_expr(tmp, &context_expr, arena);
6737 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006738 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006739 } else {
6740 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006741 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006742 }
6743 if (_PyObject_HasAttrId(obj, &PyId_optional_vars)) {
6744 int res;
6745 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6746 if (tmp == NULL) goto failed;
6747 res = obj2ast_expr(tmp, &optional_vars, arena);
6748 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006749 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006750 } else {
6751 optional_vars = NULL;
6752 }
6753 *out = withitem(context_expr, optional_vars, arena);
6754 return 0;
6755failed:
6756 Py_XDECREF(tmp);
6757 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006758}
6759
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006760
Martin v. Löwis1a214512008-06-11 05:26:20 +00006761static struct PyModuleDef _astmodule = {
6762 PyModuleDef_HEAD_INIT, "_ast"
6763};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006764PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006765PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006766{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006767 PyObject *m, *d;
6768 if (!init_types()) return NULL;
6769 m = PyModule_Create(&_astmodule);
6770 if (!m) return NULL;
6771 d = PyModule_GetDict(m);
6772 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
6773 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6774 return NULL;
6775 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6776 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6777 NULL;
6778 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6779 0) return NULL;
6780 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6781 return NULL;
6782 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6783 NULL;
6784 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6785 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6786 0) return NULL;
6787 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6788 return NULL;
6789 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6790 NULL;
6791 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6792 NULL;
6793 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6794 NULL;
6795 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6796 return NULL;
6797 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6798 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6799 NULL;
6800 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6801 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6802 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6803 NULL;
6804 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6805 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6806 NULL;
6807 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6808 NULL;
6809 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6810 return NULL;
6811 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6812 NULL;
6813 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6814 return NULL;
6815 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6816 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6817 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6818 NULL;
6819 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6820 return NULL;
6821 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6822 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6823 NULL;
6824 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6825 NULL;
6826 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6827 NULL;
6828 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6829 NULL;
6830 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6831 NULL;
6832 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6833 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6834 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6835 return NULL;
6836 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6837 NULL;
6838 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6839 return NULL;
6840 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6841 0) return NULL;
6842 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6843 NULL;
6844 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6845 return NULL;
6846 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6847 NULL;
6848 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6849 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6850 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6851 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6852 NULL;
6853 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6854 return NULL;
6855 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6856 return NULL;
6857 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6858 return NULL;
6859 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6860 NULL;
6861 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6862 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6863 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6864 NULL;
6865 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6866 0) return NULL;
6867 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6868 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
6871 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
6872 NULL;
6873 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6874 return NULL;
6875 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6876 NULL;
6877 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6878 NULL;
6879 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6880 NULL;
6881 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6882 return NULL;
6883 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6884 NULL;
6885 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
6886 NULL;
6887 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
6888 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
6889 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6890 return NULL;
6891 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
6892 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
6893 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
6894 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
6895 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
6896 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
6897 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
6898 NULL;
6899 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
6904 NULL;
6905 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6908 return NULL;
6909 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
6910 NULL;
6911 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
6912 NULL;
6913 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
6914 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
6915 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
6916 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6917 NULL;
6918 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6919 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6920 NULL;
6921 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6922 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
6923 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6924 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
6925 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6926 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6927 NULL;
6928 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6929 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6930 NULL;
6931 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
6932 < 0) return NULL;
6933 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
6934 < 0) return NULL;
6935 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
6936 < 0) return NULL;
6937 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
6938 return NULL;
6939 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
6940 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
6941 NULL;
6942 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6943 NULL;
6944 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6945 return NULL;
6946 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006947}
6948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006949
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006950PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006951{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006952 init_types();
6953 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006954}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006955
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006956/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6957mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006958{
6959 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006960 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6961 (PyObject*)Interactive_type};
6962 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006963 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006964 assert(0 <= mode && mode <= 2);
6965
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006966 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006967
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006968 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6969 if (isinstance == -1)
6970 return NULL;
6971 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006972 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6973 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006974 return NULL;
6975 }
6976 if (obj2ast_mod(ast, &res, arena) != 0)
6977 return NULL;
6978 else
6979 return res;
6980}
6981
6982int PyAST_Check(PyObject* obj)
6983{
6984 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006985 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006986}
6987
Martin v. Löwis5b222132007-06-10 09:51:05 +00006988