blob: 994e72133759ce865a58a4cb8c71abca0bae246a [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};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500274static PyTypeObject *NameConstant_type;
275static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200276 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500277};
Georg Brandl52318d62006-09-06 07:06:08 +0000278static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000279static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200280_Py_IDENTIFIER(attr);
281_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000282static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200283 "value",
284 "attr",
285 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000287static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200288_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000289static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "value",
291 "slice",
292 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000293};
Guido van Rossum0368b722007-05-11 16:50:42 +0000294static PyTypeObject *Starred_type;
295static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200296 "value",
297 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000298};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000299static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200300_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000301static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200302 "id",
303 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000304};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000305static PyTypeObject *List_type;
306static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200307 "elts",
308 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000309};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000310static PyTypeObject *Tuple_type;
311static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200312 "elts",
313 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000314};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000315static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000316static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
317*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
318static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000319static PyTypeObject *Load_type;
320static PyTypeObject *Store_type;
321static PyTypeObject *Del_type;
322static PyTypeObject *AugLoad_type;
323static PyTypeObject *AugStore_type;
324static PyTypeObject *Param_type;
325static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000326static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200328_Py_IDENTIFIER(lower);
329_Py_IDENTIFIER(upper);
330_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000331static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200332 "lower",
333 "upper",
334 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000335};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000336static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200337_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000338static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200339 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000340};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000341static PyTypeObject *Index_type;
342static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200343 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000345static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346static PyObject *And_singleton, *Or_singleton;
347static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static PyTypeObject *And_type;
349static PyTypeObject *Or_type;
350static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000351static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400352*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
353*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
354*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000355static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static PyTypeObject *Add_type;
357static PyTypeObject *Sub_type;
358static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400359static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000360static PyTypeObject *Div_type;
361static PyTypeObject *Mod_type;
362static PyTypeObject *Pow_type;
363static PyTypeObject *LShift_type;
364static PyTypeObject *RShift_type;
365static PyTypeObject *BitOr_type;
366static PyTypeObject *BitXor_type;
367static PyTypeObject *BitAnd_type;
368static PyTypeObject *FloorDiv_type;
369static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000370static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
371*USub_singleton;
372static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000373static PyTypeObject *Invert_type;
374static PyTypeObject *Not_type;
375static PyTypeObject *UAdd_type;
376static PyTypeObject *USub_type;
377static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000378static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
379*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
380*NotIn_singleton;
381static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000382static PyTypeObject *Eq_type;
383static PyTypeObject *NotEq_type;
384static PyTypeObject *Lt_type;
385static PyTypeObject *LtE_type;
386static PyTypeObject *Gt_type;
387static PyTypeObject *GtE_type;
388static PyTypeObject *Is_type;
389static PyTypeObject *IsNot_type;
390static PyTypeObject *In_type;
391static PyTypeObject *NotIn_type;
392static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000393static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200394_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000395static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200396 "target",
397 "iter",
398 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000399};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000400static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000401static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200402 "lineno",
403 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000404};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000405static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000406static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200407_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000408static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200409 "type",
410 "name",
411 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000412};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000413static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000414static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200415_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200416_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200417_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700418_Py_IDENTIFIER(kwarg);
419_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000420static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200421 "args",
422 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200423 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200424 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200425 "kwarg",
426 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000427};
Neal Norwitzc1505362006-12-28 06:47:50 +0000428static PyTypeObject *arg_type;
429static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700430static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200431 "lineno",
432 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700433};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200434_Py_IDENTIFIER(arg);
435_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000436static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200437 "arg",
438 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000439};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000440static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000441static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000442static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200443 "arg",
444 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000445};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000446static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000447static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200448_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000449static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200450 "name",
451 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000452};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500453static PyTypeObject *withitem_type;
454static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200455_Py_IDENTIFIER(context_expr);
456_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500457static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200458 "context_expr",
459 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500460};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000461
462
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700463typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100464 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700465 PyObject *dict;
466} AST_object;
467
Benjamin Peterson1767e022012-03-14 21:50:29 -0500468static void
469ast_dealloc(AST_object *self)
470{
471 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200472 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500473}
474
Neal Norwitz207c9f32008-03-31 04:42:11 +0000475static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700476ast_traverse(AST_object *self, visitproc visit, void *arg)
477{
478 Py_VISIT(self->dict);
479 return 0;
480}
481
482static void
483ast_clear(AST_object *self)
484{
485 Py_CLEAR(self->dict);
486}
487
488static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000489ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
490{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200491 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000492 Py_ssize_t i, numfields = 0;
493 int res = -1;
494 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200495 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000496 if (!fields)
497 PyErr_Clear();
498 if (fields) {
499 numfields = PySequence_Size(fields);
500 if (numfields == -1)
501 goto cleanup;
502 }
503 res = 0; /* if no error occurs, this stays 0 to the end */
504 if (PyTuple_GET_SIZE(args) > 0) {
505 if (numfields != PyTuple_GET_SIZE(args)) {
506 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000507 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000508 Py_TYPE(self)->tp_name,
509 numfields == 0 ? "" : "either 0 or ",
510 numfields, numfields == 1 ? "" : "s");
511 res = -1;
512 goto cleanup;
513 }
514 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
515 /* cannot be reached when fields is NULL */
516 PyObject *name = PySequence_GetItem(fields, i);
517 if (!name) {
518 res = -1;
519 goto cleanup;
520 }
521 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
522 Py_DECREF(name);
523 if (res < 0)
524 goto cleanup;
525 }
526 }
527 if (kw) {
528 i = 0; /* needed by PyDict_Next */
529 while (PyDict_Next(kw, &i, &key, &value)) {
530 res = PyObject_SetAttr(self, key, value);
531 if (res < 0)
532 goto cleanup;
533 }
534 }
535 cleanup:
536 Py_XDECREF(fields);
537 return res;
538}
539
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000540/* Pickling support */
541static PyObject *
542ast_type_reduce(PyObject *self, PyObject *unused)
543{
544 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200545 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200546 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000547 if (dict == NULL) {
548 if (PyErr_ExceptionMatches(PyExc_AttributeError))
549 PyErr_Clear();
550 else
551 return NULL;
552 }
553 if (dict) {
554 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
555 Py_DECREF(dict);
556 return res;
557 }
558 return Py_BuildValue("O()", Py_TYPE(self));
559}
560
561static PyMethodDef ast_type_methods[] = {
562 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
563 {NULL}
564};
565
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700566static PyGetSetDef ast_type_getsets[] = {
567 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
568 {NULL}
569};
570
Neal Norwitz207c9f32008-03-31 04:42:11 +0000571static PyTypeObject AST_type = {
572 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000573 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700574 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000575 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500576 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000577 0, /* tp_print */
578 0, /* tp_getattr */
579 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000580 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000581 0, /* tp_repr */
582 0, /* tp_as_number */
583 0, /* tp_as_sequence */
584 0, /* tp_as_mapping */
585 0, /* tp_hash */
586 0, /* tp_call */
587 0, /* tp_str */
588 PyObject_GenericGetAttr, /* tp_getattro */
589 PyObject_GenericSetAttr, /* tp_setattro */
590 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700591 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000592 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700593 (traverseproc)ast_traverse, /* tp_traverse */
594 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000595 0, /* tp_richcompare */
596 0, /* tp_weaklistoffset */
597 0, /* tp_iter */
598 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000599 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000600 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700601 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000602 0, /* tp_base */
603 0, /* tp_dict */
604 0, /* tp_descr_get */
605 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700606 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000607 (initproc)ast_type_init, /* tp_init */
608 PyType_GenericAlloc, /* tp_alloc */
609 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700610 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000611};
612
613
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000614static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
615{
616 PyObject *fnames, *result;
617 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000618 fnames = PyTuple_New(num_fields);
619 if (!fnames) return NULL;
620 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000621 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000622 if (!field) {
623 Py_DECREF(fnames);
624 return NULL;
625 }
626 PyTuple_SET_ITEM(fnames, i, field);
627 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000628 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000629 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000630 Py_DECREF(fnames);
631 return (PyTypeObject*)result;
632}
633
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000634static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
635{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200637 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000638 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000639 if (!l)
640 return 0;
641 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000642 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 if (!s) {
644 Py_DECREF(l);
645 return 0;
646 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000647 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200649 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650 Py_DECREF(l);
651 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652}
653
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000654/* Conversion AST -> Python */
655
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
657{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700658 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000659 PyObject *result = PyList_New(n);
660 PyObject *value;
661 if (!result)
662 return NULL;
663 for (i = 0; i < n; i++) {
664 value = func(asdl_seq_GET(seq, i));
665 if (!value) {
666 Py_DECREF(result);
667 return NULL;
668 }
669 PyList_SET_ITEM(result, i, value);
670 }
671 return result;
672}
673
674static PyObject* ast2obj_object(void *o)
675{
676 if (!o)
677 o = Py_None;
678 Py_INCREF((PyObject*)o);
679 return (PyObject*)o;
680}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500681#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682#define ast2obj_identifier ast2obj_object
683#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500684#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000686static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687{
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689}
690
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000691/* Conversion Python -> AST */
692
Benjamin Peterson442f2092012-12-06 17:41:04 -0500693static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
694{
695 if (obj != Py_None && obj != Py_True && obj != Py_False) {
696 PyErr_SetString(PyExc_ValueError,
697 "AST singleton must be True, False, or None");
698 return 1;
699 }
700 *out = obj;
701 return 0;
702}
703
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000704static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
705{
706 if (obj == Py_None)
707 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200708 if (obj) {
709 if (PyArena_AddPyObject(arena, obj) < 0) {
710 *out = NULL;
711 return -1;
712 }
713 Py_INCREF(obj);
714 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000715 *out = obj;
716 return 0;
717}
718
Benjamin Peterson180e6352011-07-22 11:09:07 -0500719static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500720{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500721 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
722 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500723 return 1;
724 }
725 return obj2ast_object(obj, out, arena);
726}
727
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500728static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
729{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400730 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500731 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
732 return 1;
733 }
734 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500735}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000736
Benjamin Petersone2498412011-08-09 16:08:39 -0500737static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
738{
739 if (!PyBytes_CheckExact(obj)) {
740 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
741 return 1;
742 }
743 return obj2ast_object(obj, out, arena);
744}
745
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000746static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
747{
748 int i;
749 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100750 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000751 return 1;
752 }
753
754 i = (int)PyLong_AsLong(obj);
755 if (i == -1 && PyErr_Occurred())
756 return 1;
757 *out = i;
758 return 0;
759}
760
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000761static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000762{
763 PyObject *empty_tuple, *d;
764 if (PyType_Ready(&AST_type) < 0)
765 return -1;
766 d = AST_type.tp_dict;
767 empty_tuple = PyTuple_New(0);
768 if (!empty_tuple ||
769 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
770 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
771 Py_XDECREF(empty_tuple);
772 return -1;
773 }
774 Py_DECREF(empty_tuple);
775 return 0;
776}
777
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700778static int exists_not_none(PyObject *obj, _Py_Identifier *id)
779{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700780 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700781 PyObject *attr = _PyObject_GetAttrId(obj, id);
782 if (!attr) {
783 PyErr_Clear();
784 return 0;
785 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700786 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700787 Py_DECREF(attr);
788 return !isnone;
789}
790
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000791
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000792static int init_types(void)
793{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200794 static int initialized;
795 if (initialized) return 1;
796 if (add_ast_fields() < 0) return 0;
797 mod_type = make_type("mod", &AST_type, NULL, 0);
798 if (!mod_type) return 0;
799 if (!add_attributes(mod_type, NULL, 0)) return 0;
800 Module_type = make_type("Module", mod_type, Module_fields, 1);
801 if (!Module_type) return 0;
802 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
803 1);
804 if (!Interactive_type) return 0;
805 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
806 if (!Expression_type) return 0;
807 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
808 if (!Suite_type) return 0;
809 stmt_type = make_type("stmt", &AST_type, NULL, 0);
810 if (!stmt_type) return 0;
811 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
812 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
813 5);
814 if (!FunctionDef_type) return 0;
815 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
816 if (!ClassDef_type) return 0;
817 Return_type = make_type("Return", stmt_type, Return_fields, 1);
818 if (!Return_type) return 0;
819 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
820 if (!Delete_type) return 0;
821 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
822 if (!Assign_type) return 0;
823 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
824 if (!AugAssign_type) return 0;
825 For_type = make_type("For", stmt_type, For_fields, 4);
826 if (!For_type) return 0;
827 While_type = make_type("While", stmt_type, While_fields, 3);
828 if (!While_type) return 0;
829 If_type = make_type("If", stmt_type, If_fields, 3);
830 if (!If_type) return 0;
831 With_type = make_type("With", stmt_type, With_fields, 2);
832 if (!With_type) return 0;
833 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
834 if (!Raise_type) return 0;
835 Try_type = make_type("Try", stmt_type, Try_fields, 4);
836 if (!Try_type) return 0;
837 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
838 if (!Assert_type) return 0;
839 Import_type = make_type("Import", stmt_type, Import_fields, 1);
840 if (!Import_type) return 0;
841 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
842 if (!ImportFrom_type) return 0;
843 Global_type = make_type("Global", stmt_type, Global_fields, 1);
844 if (!Global_type) return 0;
845 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
846 if (!Nonlocal_type) return 0;
847 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
848 if (!Expr_type) return 0;
849 Pass_type = make_type("Pass", stmt_type, NULL, 0);
850 if (!Pass_type) return 0;
851 Break_type = make_type("Break", stmt_type, NULL, 0);
852 if (!Break_type) return 0;
853 Continue_type = make_type("Continue", stmt_type, NULL, 0);
854 if (!Continue_type) return 0;
855 expr_type = make_type("expr", &AST_type, NULL, 0);
856 if (!expr_type) return 0;
857 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
858 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
859 if (!BoolOp_type) return 0;
860 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
861 if (!BinOp_type) return 0;
862 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
863 if (!UnaryOp_type) return 0;
864 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
865 if (!Lambda_type) return 0;
866 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
867 if (!IfExp_type) return 0;
868 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
869 if (!Dict_type) return 0;
870 Set_type = make_type("Set", expr_type, Set_fields, 1);
871 if (!Set_type) return 0;
872 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
873 if (!ListComp_type) return 0;
874 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
875 if (!SetComp_type) return 0;
876 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
877 if (!DictComp_type) return 0;
878 GeneratorExp_type = make_type("GeneratorExp", expr_type,
879 GeneratorExp_fields, 2);
880 if (!GeneratorExp_type) return 0;
881 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
882 if (!Yield_type) return 0;
883 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
884 if (!YieldFrom_type) return 0;
885 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
886 if (!Compare_type) return 0;
887 Call_type = make_type("Call", expr_type, Call_fields, 5);
888 if (!Call_type) return 0;
889 Num_type = make_type("Num", expr_type, Num_fields, 1);
890 if (!Num_type) return 0;
891 Str_type = make_type("Str", expr_type, Str_fields, 1);
892 if (!Str_type) return 0;
893 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
894 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200895 NameConstant_type = make_type("NameConstant", expr_type,
896 NameConstant_fields, 1);
897 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200898 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
899 if (!Ellipsis_type) return 0;
900 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
901 if (!Attribute_type) return 0;
902 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
903 if (!Subscript_type) return 0;
904 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
905 if (!Starred_type) return 0;
906 Name_type = make_type("Name", expr_type, Name_fields, 2);
907 if (!Name_type) return 0;
908 List_type = make_type("List", expr_type, List_fields, 2);
909 if (!List_type) return 0;
910 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
911 if (!Tuple_type) return 0;
912 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
913 if (!expr_context_type) return 0;
914 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
915 Load_type = make_type("Load", expr_context_type, NULL, 0);
916 if (!Load_type) return 0;
917 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
918 if (!Load_singleton) return 0;
919 Store_type = make_type("Store", expr_context_type, NULL, 0);
920 if (!Store_type) return 0;
921 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
922 if (!Store_singleton) return 0;
923 Del_type = make_type("Del", expr_context_type, NULL, 0);
924 if (!Del_type) return 0;
925 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
926 if (!Del_singleton) return 0;
927 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
928 if (!AugLoad_type) return 0;
929 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
930 if (!AugLoad_singleton) return 0;
931 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
932 if (!AugStore_type) return 0;
933 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
934 if (!AugStore_singleton) return 0;
935 Param_type = make_type("Param", expr_context_type, NULL, 0);
936 if (!Param_type) return 0;
937 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
938 if (!Param_singleton) return 0;
939 slice_type = make_type("slice", &AST_type, NULL, 0);
940 if (!slice_type) return 0;
941 if (!add_attributes(slice_type, NULL, 0)) return 0;
942 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
943 if (!Slice_type) return 0;
944 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
945 if (!ExtSlice_type) return 0;
946 Index_type = make_type("Index", slice_type, Index_fields, 1);
947 if (!Index_type) return 0;
948 boolop_type = make_type("boolop", &AST_type, NULL, 0);
949 if (!boolop_type) return 0;
950 if (!add_attributes(boolop_type, NULL, 0)) return 0;
951 And_type = make_type("And", boolop_type, NULL, 0);
952 if (!And_type) return 0;
953 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
954 if (!And_singleton) return 0;
955 Or_type = make_type("Or", boolop_type, NULL, 0);
956 if (!Or_type) return 0;
957 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
958 if (!Or_singleton) return 0;
959 operator_type = make_type("operator", &AST_type, NULL, 0);
960 if (!operator_type) return 0;
961 if (!add_attributes(operator_type, NULL, 0)) return 0;
962 Add_type = make_type("Add", operator_type, NULL, 0);
963 if (!Add_type) return 0;
964 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
965 if (!Add_singleton) return 0;
966 Sub_type = make_type("Sub", operator_type, NULL, 0);
967 if (!Sub_type) return 0;
968 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
969 if (!Sub_singleton) return 0;
970 Mult_type = make_type("Mult", operator_type, NULL, 0);
971 if (!Mult_type) return 0;
972 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
973 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400974 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
975 if (!MatMult_type) return 0;
976 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
977 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200978 Div_type = make_type("Div", operator_type, NULL, 0);
979 if (!Div_type) return 0;
980 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
981 if (!Div_singleton) return 0;
982 Mod_type = make_type("Mod", operator_type, NULL, 0);
983 if (!Mod_type) return 0;
984 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
985 if (!Mod_singleton) return 0;
986 Pow_type = make_type("Pow", operator_type, NULL, 0);
987 if (!Pow_type) return 0;
988 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
989 if (!Pow_singleton) return 0;
990 LShift_type = make_type("LShift", operator_type, NULL, 0);
991 if (!LShift_type) return 0;
992 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
993 if (!LShift_singleton) return 0;
994 RShift_type = make_type("RShift", operator_type, NULL, 0);
995 if (!RShift_type) return 0;
996 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
997 if (!RShift_singleton) return 0;
998 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
999 if (!BitOr_type) return 0;
1000 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1001 if (!BitOr_singleton) return 0;
1002 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1003 if (!BitXor_type) return 0;
1004 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1005 if (!BitXor_singleton) return 0;
1006 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1007 if (!BitAnd_type) return 0;
1008 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1009 if (!BitAnd_singleton) return 0;
1010 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1011 if (!FloorDiv_type) return 0;
1012 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1013 if (!FloorDiv_singleton) return 0;
1014 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1015 if (!unaryop_type) return 0;
1016 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1017 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1018 if (!Invert_type) return 0;
1019 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1020 if (!Invert_singleton) return 0;
1021 Not_type = make_type("Not", unaryop_type, NULL, 0);
1022 if (!Not_type) return 0;
1023 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1024 if (!Not_singleton) return 0;
1025 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1026 if (!UAdd_type) return 0;
1027 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1028 if (!UAdd_singleton) return 0;
1029 USub_type = make_type("USub", unaryop_type, NULL, 0);
1030 if (!USub_type) return 0;
1031 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1032 if (!USub_singleton) return 0;
1033 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1034 if (!cmpop_type) return 0;
1035 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1036 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1037 if (!Eq_type) return 0;
1038 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1039 if (!Eq_singleton) return 0;
1040 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1041 if (!NotEq_type) return 0;
1042 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1043 if (!NotEq_singleton) return 0;
1044 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1045 if (!Lt_type) return 0;
1046 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1047 if (!Lt_singleton) return 0;
1048 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1049 if (!LtE_type) return 0;
1050 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1051 if (!LtE_singleton) return 0;
1052 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1053 if (!Gt_type) return 0;
1054 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1055 if (!Gt_singleton) return 0;
1056 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1057 if (!GtE_type) return 0;
1058 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1059 if (!GtE_singleton) return 0;
1060 Is_type = make_type("Is", cmpop_type, NULL, 0);
1061 if (!Is_type) return 0;
1062 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1063 if (!Is_singleton) return 0;
1064 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1065 if (!IsNot_type) return 0;
1066 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1067 if (!IsNot_singleton) return 0;
1068 In_type = make_type("In", cmpop_type, NULL, 0);
1069 if (!In_type) return 0;
1070 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1071 if (!In_singleton) return 0;
1072 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1073 if (!NotIn_type) return 0;
1074 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1075 if (!NotIn_singleton) return 0;
1076 comprehension_type = make_type("comprehension", &AST_type,
1077 comprehension_fields, 3);
1078 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001079 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001080 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1081 if (!excepthandler_type) return 0;
1082 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1083 return 0;
1084 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1085 ExceptHandler_fields, 3);
1086 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001087 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001088 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001089 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001090 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1091 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001092 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001093 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1094 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001095 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001096 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1097 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001098 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001099 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1100 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001101 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001102 initialized = 1;
1103 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001104}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001105
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001106static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1107static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1108static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1109static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1110 arena);
1111static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1112static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1113static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1114static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1115static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1116static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1117 arena);
1118static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1119 arena);
1120static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1121static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1122static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1123static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001124static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001129 mod_ty p;
1130 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1131 if (!p)
1132 return NULL;
1133 p->kind = Module_kind;
1134 p->v.Module.body = body;
1135 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136}
1137
1138mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001139Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001141 mod_ty p;
1142 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1143 if (!p)
1144 return NULL;
1145 p->kind = Interactive_kind;
1146 p->v.Interactive.body = body;
1147 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148}
1149
1150mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001151Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001153 mod_ty p;
1154 if (!body) {
1155 PyErr_SetString(PyExc_ValueError,
1156 "field body is required for Expression");
1157 return NULL;
1158 }
1159 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1160 if (!p)
1161 return NULL;
1162 p->kind = Expression_kind;
1163 p->v.Expression.body = body;
1164 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165}
1166
1167mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001168Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001170 mod_ty p;
1171 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1172 if (!p)
1173 return NULL;
1174 p->kind = Suite_kind;
1175 p->v.Suite.body = body;
1176 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177}
1178
1179stmt_ty
1180FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001181 decorator_list, expr_ty returns, int lineno, int col_offset,
1182 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001184 stmt_ty p;
1185 if (!name) {
1186 PyErr_SetString(PyExc_ValueError,
1187 "field name is required for FunctionDef");
1188 return NULL;
1189 }
1190 if (!args) {
1191 PyErr_SetString(PyExc_ValueError,
1192 "field args is required for FunctionDef");
1193 return NULL;
1194 }
1195 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1196 if (!p)
1197 return NULL;
1198 p->kind = FunctionDef_kind;
1199 p->v.FunctionDef.name = name;
1200 p->v.FunctionDef.args = args;
1201 p->v.FunctionDef.body = body;
1202 p->v.FunctionDef.decorator_list = decorator_list;
1203 p->v.FunctionDef.returns = returns;
1204 p->lineno = lineno;
1205 p->col_offset = col_offset;
1206 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207}
1208
1209stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001210ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001211 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1212 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001214 stmt_ty p;
1215 if (!name) {
1216 PyErr_SetString(PyExc_ValueError,
1217 "field name is required for ClassDef");
1218 return NULL;
1219 }
1220 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1221 if (!p)
1222 return NULL;
1223 p->kind = ClassDef_kind;
1224 p->v.ClassDef.name = name;
1225 p->v.ClassDef.bases = bases;
1226 p->v.ClassDef.keywords = keywords;
1227 p->v.ClassDef.starargs = starargs;
1228 p->v.ClassDef.kwargs = kwargs;
1229 p->v.ClassDef.body = body;
1230 p->v.ClassDef.decorator_list = decorator_list;
1231 p->lineno = lineno;
1232 p->col_offset = col_offset;
1233 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234}
1235
1236stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001237Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001239 stmt_ty p;
1240 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1241 if (!p)
1242 return NULL;
1243 p->kind = Return_kind;
1244 p->v.Return.value = value;
1245 p->lineno = lineno;
1246 p->col_offset = col_offset;
1247 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248}
1249
1250stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001251Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001253 stmt_ty p;
1254 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1255 if (!p)
1256 return NULL;
1257 p->kind = Delete_kind;
1258 p->v.Delete.targets = targets;
1259 p->lineno = lineno;
1260 p->col_offset = col_offset;
1261 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001265Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1266 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001268 stmt_ty p;
1269 if (!value) {
1270 PyErr_SetString(PyExc_ValueError,
1271 "field value is required for Assign");
1272 return NULL;
1273 }
1274 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1275 if (!p)
1276 return NULL;
1277 p->kind = Assign_kind;
1278 p->v.Assign.targets = targets;
1279 p->v.Assign.value = value;
1280 p->lineno = lineno;
1281 p->col_offset = col_offset;
1282 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283}
1284
1285stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001286AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1287 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001289 stmt_ty p;
1290 if (!target) {
1291 PyErr_SetString(PyExc_ValueError,
1292 "field target is required for AugAssign");
1293 return NULL;
1294 }
1295 if (!op) {
1296 PyErr_SetString(PyExc_ValueError,
1297 "field op is required for AugAssign");
1298 return NULL;
1299 }
1300 if (!value) {
1301 PyErr_SetString(PyExc_ValueError,
1302 "field value is required for AugAssign");
1303 return NULL;
1304 }
1305 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1306 if (!p)
1307 return NULL;
1308 p->kind = AugAssign_kind;
1309 p->v.AugAssign.target = target;
1310 p->v.AugAssign.op = op;
1311 p->v.AugAssign.value = value;
1312 p->lineno = lineno;
1313 p->col_offset = col_offset;
1314 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315}
1316
1317stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001319 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001321 stmt_ty p;
1322 if (!target) {
1323 PyErr_SetString(PyExc_ValueError,
1324 "field target is required for For");
1325 return NULL;
1326 }
1327 if (!iter) {
1328 PyErr_SetString(PyExc_ValueError,
1329 "field iter is required for For");
1330 return NULL;
1331 }
1332 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1333 if (!p)
1334 return NULL;
1335 p->kind = For_kind;
1336 p->v.For.target = target;
1337 p->v.For.iter = iter;
1338 p->v.For.body = body;
1339 p->v.For.orelse = orelse;
1340 p->lineno = lineno;
1341 p->col_offset = col_offset;
1342 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343}
1344
1345stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001346While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1347 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001349 stmt_ty p;
1350 if (!test) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field test is required for While");
1353 return NULL;
1354 }
1355 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1356 if (!p)
1357 return NULL;
1358 p->kind = While_kind;
1359 p->v.While.test = test;
1360 p->v.While.body = body;
1361 p->v.While.orelse = orelse;
1362 p->lineno = lineno;
1363 p->col_offset = col_offset;
1364 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1369 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001371 stmt_ty p;
1372 if (!test) {
1373 PyErr_SetString(PyExc_ValueError,
1374 "field test is required for If");
1375 return NULL;
1376 }
1377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1378 if (!p)
1379 return NULL;
1380 p->kind = If_kind;
1381 p->v.If.test = test;
1382 p->v.If.body = body;
1383 p->v.If.orelse = orelse;
1384 p->lineno = lineno;
1385 p->col_offset = col_offset;
1386 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387}
1388
1389stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001390With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1391 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001392{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001393 stmt_ty p;
1394 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1395 if (!p)
1396 return NULL;
1397 p->kind = With_kind;
1398 p->v.With.items = items;
1399 p->v.With.body = body;
1400 p->lineno = lineno;
1401 p->col_offset = col_offset;
1402 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001403}
1404
1405stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001406Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001408 stmt_ty p;
1409 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1410 if (!p)
1411 return NULL;
1412 p->kind = Raise_kind;
1413 p->v.Raise.exc = exc;
1414 p->v.Raise.cause = cause;
1415 p->lineno = lineno;
1416 p->col_offset = col_offset;
1417 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418}
1419
1420stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001421Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1422 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001424 stmt_ty p;
1425 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1426 if (!p)
1427 return NULL;
1428 p->kind = Try_kind;
1429 p->v.Try.body = body;
1430 p->v.Try.handlers = handlers;
1431 p->v.Try.orelse = orelse;
1432 p->v.Try.finalbody = finalbody;
1433 p->lineno = lineno;
1434 p->col_offset = col_offset;
1435 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436}
1437
1438stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001439Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001441 stmt_ty p;
1442 if (!test) {
1443 PyErr_SetString(PyExc_ValueError,
1444 "field test is required for Assert");
1445 return NULL;
1446 }
1447 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1448 if (!p)
1449 return NULL;
1450 p->kind = Assert_kind;
1451 p->v.Assert.test = test;
1452 p->v.Assert.msg = msg;
1453 p->lineno = lineno;
1454 p->col_offset = col_offset;
1455 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456}
1457
1458stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001459Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001461 stmt_ty p;
1462 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1463 if (!p)
1464 return NULL;
1465 p->kind = Import_kind;
1466 p->v.Import.names = names;
1467 p->lineno = lineno;
1468 p->col_offset = col_offset;
1469 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470}
1471
1472stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001473ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1474 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001476 stmt_ty p;
1477 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1478 if (!p)
1479 return NULL;
1480 p->kind = ImportFrom_kind;
1481 p->v.ImportFrom.module = module;
1482 p->v.ImportFrom.names = names;
1483 p->v.ImportFrom.level = level;
1484 p->lineno = lineno;
1485 p->col_offset = col_offset;
1486 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
1489stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001492 stmt_ty p;
1493 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1494 if (!p)
1495 return NULL;
1496 p->kind = Global_kind;
1497 p->v.Global.names = names;
1498 p->lineno = lineno;
1499 p->col_offset = col_offset;
1500 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501}
1502
1503stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001504Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1505{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001506 stmt_ty p;
1507 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1508 if (!p)
1509 return NULL;
1510 p->kind = Nonlocal_kind;
1511 p->v.Nonlocal.names = names;
1512 p->lineno = lineno;
1513 p->col_offset = col_offset;
1514 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001515}
1516
1517stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001518Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001520 stmt_ty p;
1521 if (!value) {
1522 PyErr_SetString(PyExc_ValueError,
1523 "field value is required for Expr");
1524 return NULL;
1525 }
1526 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1527 if (!p)
1528 return NULL;
1529 p->kind = Expr_kind;
1530 p->v.Expr.value = value;
1531 p->lineno = lineno;
1532 p->col_offset = col_offset;
1533 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
1536stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001537Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001539 stmt_ty p;
1540 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1541 if (!p)
1542 return NULL;
1543 p->kind = Pass_kind;
1544 p->lineno = lineno;
1545 p->col_offset = col_offset;
1546 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001550Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001552 stmt_ty p;
1553 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1554 if (!p)
1555 return NULL;
1556 p->kind = Break_kind;
1557 p->lineno = lineno;
1558 p->col_offset = col_offset;
1559 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560}
1561
1562stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001563Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001565 stmt_ty p;
1566 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1567 if (!p)
1568 return NULL;
1569 p->kind = Continue_kind;
1570 p->lineno = lineno;
1571 p->col_offset = col_offset;
1572 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573}
1574
1575expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001576BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1577 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001579 expr_ty p;
1580 if (!op) {
1581 PyErr_SetString(PyExc_ValueError,
1582 "field op is required for BoolOp");
1583 return NULL;
1584 }
1585 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1586 if (!p)
1587 return NULL;
1588 p->kind = BoolOp_kind;
1589 p->v.BoolOp.op = op;
1590 p->v.BoolOp.values = values;
1591 p->lineno = lineno;
1592 p->col_offset = col_offset;
1593 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001597BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1598 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001600 expr_ty p;
1601 if (!left) {
1602 PyErr_SetString(PyExc_ValueError,
1603 "field left is required for BinOp");
1604 return NULL;
1605 }
1606 if (!op) {
1607 PyErr_SetString(PyExc_ValueError,
1608 "field op is required for BinOp");
1609 return NULL;
1610 }
1611 if (!right) {
1612 PyErr_SetString(PyExc_ValueError,
1613 "field right is required for BinOp");
1614 return NULL;
1615 }
1616 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1617 if (!p)
1618 return NULL;
1619 p->kind = BinOp_kind;
1620 p->v.BinOp.left = left;
1621 p->v.BinOp.op = op;
1622 p->v.BinOp.right = right;
1623 p->lineno = lineno;
1624 p->col_offset = col_offset;
1625 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
1627
1628expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001629UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1630 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001632 expr_ty p;
1633 if (!op) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field op is required for UnaryOp");
1636 return NULL;
1637 }
1638 if (!operand) {
1639 PyErr_SetString(PyExc_ValueError,
1640 "field operand is required for UnaryOp");
1641 return NULL;
1642 }
1643 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1644 if (!p)
1645 return NULL;
1646 p->kind = UnaryOp_kind;
1647 p->v.UnaryOp.op = op;
1648 p->v.UnaryOp.operand = operand;
1649 p->lineno = lineno;
1650 p->col_offset = col_offset;
1651 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652}
1653
1654expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1656 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001658 expr_ty p;
1659 if (!args) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "field args is required for Lambda");
1662 return NULL;
1663 }
1664 if (!body) {
1665 PyErr_SetString(PyExc_ValueError,
1666 "field body is required for Lambda");
1667 return NULL;
1668 }
1669 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1670 if (!p)
1671 return NULL;
1672 p->kind = Lambda_kind;
1673 p->v.Lambda.args = args;
1674 p->v.Lambda.body = body;
1675 p->lineno = lineno;
1676 p->col_offset = col_offset;
1677 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678}
1679
1680expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001681IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1682 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001683{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001684 expr_ty p;
1685 if (!test) {
1686 PyErr_SetString(PyExc_ValueError,
1687 "field test is required for IfExp");
1688 return NULL;
1689 }
1690 if (!body) {
1691 PyErr_SetString(PyExc_ValueError,
1692 "field body is required for IfExp");
1693 return NULL;
1694 }
1695 if (!orelse) {
1696 PyErr_SetString(PyExc_ValueError,
1697 "field orelse is required for IfExp");
1698 return NULL;
1699 }
1700 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1701 if (!p)
1702 return NULL;
1703 p->kind = IfExp_kind;
1704 p->v.IfExp.test = test;
1705 p->v.IfExp.body = body;
1706 p->v.IfExp.orelse = orelse;
1707 p->lineno = lineno;
1708 p->col_offset = col_offset;
1709 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001710}
1711
1712expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1714 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001716 expr_ty p;
1717 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1718 if (!p)
1719 return NULL;
1720 p->kind = Dict_kind;
1721 p->v.Dict.keys = keys;
1722 p->v.Dict.values = values;
1723 p->lineno = lineno;
1724 p->col_offset = col_offset;
1725 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726}
1727
1728expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001729Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1730{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001731 expr_ty p;
1732 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1733 if (!p)
1734 return NULL;
1735 p->kind = Set_kind;
1736 p->v.Set.elts = elts;
1737 p->lineno = lineno;
1738 p->col_offset = col_offset;
1739 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001740}
1741
1742expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001743ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1744 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001746 expr_ty p;
1747 if (!elt) {
1748 PyErr_SetString(PyExc_ValueError,
1749 "field elt is required for ListComp");
1750 return NULL;
1751 }
1752 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1753 if (!p)
1754 return NULL;
1755 p->kind = ListComp_kind;
1756 p->v.ListComp.elt = elt;
1757 p->v.ListComp.generators = generators;
1758 p->lineno = lineno;
1759 p->col_offset = col_offset;
1760 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761}
1762
1763expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001764SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1765 *arena)
1766{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001767 expr_ty p;
1768 if (!elt) {
1769 PyErr_SetString(PyExc_ValueError,
1770 "field elt is required for SetComp");
1771 return NULL;
1772 }
1773 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1774 if (!p)
1775 return NULL;
1776 p->kind = SetComp_kind;
1777 p->v.SetComp.elt = elt;
1778 p->v.SetComp.generators = generators;
1779 p->lineno = lineno;
1780 p->col_offset = col_offset;
1781 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001782}
1783
1784expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001785DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1786 col_offset, PyArena *arena)
1787{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001788 expr_ty p;
1789 if (!key) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field key is required for DictComp");
1792 return NULL;
1793 }
1794 if (!value) {
1795 PyErr_SetString(PyExc_ValueError,
1796 "field value is required for DictComp");
1797 return NULL;
1798 }
1799 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1800 if (!p)
1801 return NULL;
1802 p->kind = DictComp_kind;
1803 p->v.DictComp.key = key;
1804 p->v.DictComp.value = value;
1805 p->v.DictComp.generators = generators;
1806 p->lineno = lineno;
1807 p->col_offset = col_offset;
1808 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001809}
1810
1811expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001812GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1813 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001815 expr_ty p;
1816 if (!elt) {
1817 PyErr_SetString(PyExc_ValueError,
1818 "field elt is required for GeneratorExp");
1819 return NULL;
1820 }
1821 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1822 if (!p)
1823 return NULL;
1824 p->kind = GeneratorExp_kind;
1825 p->v.GeneratorExp.elt = elt;
1826 p->v.GeneratorExp.generators = generators;
1827 p->lineno = lineno;
1828 p->col_offset = col_offset;
1829 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830}
1831
1832expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001833Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001835 expr_ty p;
1836 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1837 if (!p)
1838 return NULL;
1839 p->kind = Yield_kind;
1840 p->v.Yield.value = value;
1841 p->lineno = lineno;
1842 p->col_offset = col_offset;
1843 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844}
1845
1846expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001847YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1848{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001849 expr_ty p;
1850 if (!value) {
1851 PyErr_SetString(PyExc_ValueError,
1852 "field value is required for YieldFrom");
1853 return NULL;
1854 }
1855 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1856 if (!p)
1857 return NULL;
1858 p->kind = YieldFrom_kind;
1859 p->v.YieldFrom.value = value;
1860 p->lineno = lineno;
1861 p->col_offset = col_offset;
1862 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001863}
1864
1865expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001866Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1867 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001869 expr_ty p;
1870 if (!left) {
1871 PyErr_SetString(PyExc_ValueError,
1872 "field left is required for Compare");
1873 return NULL;
1874 }
1875 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1876 if (!p)
1877 return NULL;
1878 p->kind = Compare_kind;
1879 p->v.Compare.left = left;
1880 p->v.Compare.ops = ops;
1881 p->v.Compare.comparators = comparators;
1882 p->lineno = lineno;
1883 p->col_offset = col_offset;
1884 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885}
1886
1887expr_ty
1888Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001889 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001891 expr_ty p;
1892 if (!func) {
1893 PyErr_SetString(PyExc_ValueError,
1894 "field func is required for Call");
1895 return NULL;
1896 }
1897 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1898 if (!p)
1899 return NULL;
1900 p->kind = Call_kind;
1901 p->v.Call.func = func;
1902 p->v.Call.args = args;
1903 p->v.Call.keywords = keywords;
1904 p->v.Call.starargs = starargs;
1905 p->v.Call.kwargs = kwargs;
1906 p->lineno = lineno;
1907 p->col_offset = col_offset;
1908 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909}
1910
1911expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001912Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001914 expr_ty p;
1915 if (!n) {
1916 PyErr_SetString(PyExc_ValueError,
1917 "field n is required for Num");
1918 return NULL;
1919 }
1920 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1921 if (!p)
1922 return NULL;
1923 p->kind = Num_kind;
1924 p->v.Num.n = n;
1925 p->lineno = lineno;
1926 p->col_offset = col_offset;
1927 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928}
1929
1930expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001931Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001933 expr_ty p;
1934 if (!s) {
1935 PyErr_SetString(PyExc_ValueError,
1936 "field s is required for Str");
1937 return NULL;
1938 }
1939 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1940 if (!p)
1941 return NULL;
1942 p->kind = Str_kind;
1943 p->v.Str.s = s;
1944 p->lineno = lineno;
1945 p->col_offset = col_offset;
1946 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947}
1948
1949expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001950Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001951{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001952 expr_ty p;
1953 if (!s) {
1954 PyErr_SetString(PyExc_ValueError,
1955 "field s is required for Bytes");
1956 return NULL;
1957 }
1958 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1959 if (!p)
1960 return NULL;
1961 p->kind = Bytes_kind;
1962 p->v.Bytes.s = s;
1963 p->lineno = lineno;
1964 p->col_offset = col_offset;
1965 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001966}
1967
1968expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05001969NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
1970{
Victor Stinneree4b59c2013-07-27 00:01:35 +02001971 expr_ty p;
1972 if (!value) {
1973 PyErr_SetString(PyExc_ValueError,
1974 "field value is required for NameConstant");
1975 return NULL;
1976 }
1977 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1978 if (!p)
1979 return NULL;
1980 p->kind = NameConstant_kind;
1981 p->v.NameConstant.value = value;
1982 p->lineno = lineno;
1983 p->col_offset = col_offset;
1984 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001985}
1986
1987expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001988Ellipsis(int lineno, int col_offset, PyArena *arena)
1989{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001990 expr_ty p;
1991 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1992 if (!p)
1993 return NULL;
1994 p->kind = Ellipsis_kind;
1995 p->lineno = lineno;
1996 p->col_offset = col_offset;
1997 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001998}
1999
2000expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002001Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2002 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002004 expr_ty p;
2005 if (!value) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field value is required for Attribute");
2008 return NULL;
2009 }
2010 if (!attr) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "field attr is required for Attribute");
2013 return NULL;
2014 }
2015 if (!ctx) {
2016 PyErr_SetString(PyExc_ValueError,
2017 "field ctx is required for Attribute");
2018 return NULL;
2019 }
2020 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2021 if (!p)
2022 return NULL;
2023 p->kind = Attribute_kind;
2024 p->v.Attribute.value = value;
2025 p->v.Attribute.attr = attr;
2026 p->v.Attribute.ctx = ctx;
2027 p->lineno = lineno;
2028 p->col_offset = col_offset;
2029 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030}
2031
2032expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002033Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2034 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002036 expr_ty p;
2037 if (!value) {
2038 PyErr_SetString(PyExc_ValueError,
2039 "field value is required for Subscript");
2040 return NULL;
2041 }
2042 if (!slice) {
2043 PyErr_SetString(PyExc_ValueError,
2044 "field slice is required for Subscript");
2045 return NULL;
2046 }
2047 if (!ctx) {
2048 PyErr_SetString(PyExc_ValueError,
2049 "field ctx is required for Subscript");
2050 return NULL;
2051 }
2052 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2053 if (!p)
2054 return NULL;
2055 p->kind = Subscript_kind;
2056 p->v.Subscript.value = value;
2057 p->v.Subscript.slice = slice;
2058 p->v.Subscript.ctx = ctx;
2059 p->lineno = lineno;
2060 p->col_offset = col_offset;
2061 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062}
2063
2064expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002065Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2066 *arena)
2067{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002068 expr_ty p;
2069 if (!value) {
2070 PyErr_SetString(PyExc_ValueError,
2071 "field value is required for Starred");
2072 return NULL;
2073 }
2074 if (!ctx) {
2075 PyErr_SetString(PyExc_ValueError,
2076 "field ctx is required for Starred");
2077 return NULL;
2078 }
2079 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2080 if (!p)
2081 return NULL;
2082 p->kind = Starred_kind;
2083 p->v.Starred.value = value;
2084 p->v.Starred.ctx = ctx;
2085 p->lineno = lineno;
2086 p->col_offset = col_offset;
2087 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002088}
2089
2090expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002091Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2092 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002094 expr_ty p;
2095 if (!id) {
2096 PyErr_SetString(PyExc_ValueError,
2097 "field id is required for Name");
2098 return NULL;
2099 }
2100 if (!ctx) {
2101 PyErr_SetString(PyExc_ValueError,
2102 "field ctx is required for Name");
2103 return NULL;
2104 }
2105 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2106 if (!p)
2107 return NULL;
2108 p->kind = Name_kind;
2109 p->v.Name.id = id;
2110 p->v.Name.ctx = ctx;
2111 p->lineno = lineno;
2112 p->col_offset = col_offset;
2113 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114}
2115
2116expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2118 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002120 expr_ty p;
2121 if (!ctx) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "field ctx is required for List");
2124 return NULL;
2125 }
2126 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2127 if (!p)
2128 return NULL;
2129 p->kind = List_kind;
2130 p->v.List.elts = elts;
2131 p->v.List.ctx = ctx;
2132 p->lineno = lineno;
2133 p->col_offset = col_offset;
2134 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135}
2136
2137expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002138Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2139 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002141 expr_ty p;
2142 if (!ctx) {
2143 PyErr_SetString(PyExc_ValueError,
2144 "field ctx is required for Tuple");
2145 return NULL;
2146 }
2147 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2148 if (!p)
2149 return NULL;
2150 p->kind = Tuple_kind;
2151 p->v.Tuple.elts = elts;
2152 p->v.Tuple.ctx = ctx;
2153 p->lineno = lineno;
2154 p->col_offset = col_offset;
2155 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156}
2157
2158slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002159Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002161 slice_ty p;
2162 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2163 if (!p)
2164 return NULL;
2165 p->kind = Slice_kind;
2166 p->v.Slice.lower = lower;
2167 p->v.Slice.upper = upper;
2168 p->v.Slice.step = step;
2169 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170}
2171
2172slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002173ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002175 slice_ty p;
2176 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2177 if (!p)
2178 return NULL;
2179 p->kind = ExtSlice_kind;
2180 p->v.ExtSlice.dims = dims;
2181 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182}
2183
2184slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002187 slice_ty p;
2188 if (!value) {
2189 PyErr_SetString(PyExc_ValueError,
2190 "field value is required for Index");
2191 return NULL;
2192 }
2193 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2194 if (!p)
2195 return NULL;
2196 p->kind = Index_kind;
2197 p->v.Index.value = value;
2198 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199}
2200
2201comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002202comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002204 comprehension_ty p;
2205 if (!target) {
2206 PyErr_SetString(PyExc_ValueError,
2207 "field target is required for comprehension");
2208 return NULL;
2209 }
2210 if (!iter) {
2211 PyErr_SetString(PyExc_ValueError,
2212 "field iter is required for comprehension");
2213 return NULL;
2214 }
2215 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2216 if (!p)
2217 return NULL;
2218 p->target = target;
2219 p->iter = iter;
2220 p->ifs = ifs;
2221 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222}
2223
2224excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002225ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002226 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002228 excepthandler_ty p;
2229 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2230 if (!p)
2231 return NULL;
2232 p->kind = ExceptHandler_kind;
2233 p->v.ExceptHandler.type = type;
2234 p->v.ExceptHandler.name = name;
2235 p->v.ExceptHandler.body = body;
2236 p->lineno = lineno;
2237 p->col_offset = col_offset;
2238 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239}
2240
2241arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002242arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2243 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002245 arguments_ty p;
2246 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2247 if (!p)
2248 return NULL;
2249 p->args = args;
2250 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002251 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002252 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002253 p->kwarg = kwarg;
2254 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002255 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256}
2257
Neal Norwitzc1505362006-12-28 06:47:50 +00002258arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002259arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002260{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002261 arg_ty p;
2262 if (!arg) {
2263 PyErr_SetString(PyExc_ValueError,
2264 "field arg is required for arg");
2265 return NULL;
2266 }
2267 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2268 if (!p)
2269 return NULL;
2270 p->arg = arg;
2271 p->annotation = annotation;
2272 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002273}
2274
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002276keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002278 keyword_ty p;
2279 if (!arg) {
2280 PyErr_SetString(PyExc_ValueError,
2281 "field arg is required for keyword");
2282 return NULL;
2283 }
2284 if (!value) {
2285 PyErr_SetString(PyExc_ValueError,
2286 "field value is required for keyword");
2287 return NULL;
2288 }
2289 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2290 if (!p)
2291 return NULL;
2292 p->arg = arg;
2293 p->value = value;
2294 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295}
2296
2297alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002298alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002300 alias_ty p;
2301 if (!name) {
2302 PyErr_SetString(PyExc_ValueError,
2303 "field name is required for alias");
2304 return NULL;
2305 }
2306 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2307 if (!p)
2308 return NULL;
2309 p->name = name;
2310 p->asname = asname;
2311 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312}
2313
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002314withitem_ty
2315withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2316{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002317 withitem_ty p;
2318 if (!context_expr) {
2319 PyErr_SetString(PyExc_ValueError,
2320 "field context_expr is required for withitem");
2321 return NULL;
2322 }
2323 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2324 if (!p)
2325 return NULL;
2326 p->context_expr = context_expr;
2327 p->optional_vars = optional_vars;
2328 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002329}
2330
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002331
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002332PyObject*
2333ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002334{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002335 mod_ty o = (mod_ty)_o;
2336 PyObject *result = NULL, *value = NULL;
2337 if (!o) {
2338 Py_INCREF(Py_None);
2339 return Py_None;
2340 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002341
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002342 switch (o->kind) {
2343 case Module_kind:
2344 result = PyType_GenericNew(Module_type, NULL, NULL);
2345 if (!result) goto failed;
2346 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2347 if (!value) goto failed;
2348 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2349 goto failed;
2350 Py_DECREF(value);
2351 break;
2352 case Interactive_kind:
2353 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2354 if (!result) goto failed;
2355 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2356 if (!value) goto failed;
2357 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2358 goto failed;
2359 Py_DECREF(value);
2360 break;
2361 case Expression_kind:
2362 result = PyType_GenericNew(Expression_type, NULL, NULL);
2363 if (!result) goto failed;
2364 value = ast2obj_expr(o->v.Expression.body);
2365 if (!value) goto failed;
2366 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2367 goto failed;
2368 Py_DECREF(value);
2369 break;
2370 case Suite_kind:
2371 result = PyType_GenericNew(Suite_type, NULL, NULL);
2372 if (!result) goto failed;
2373 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2374 if (!value) goto failed;
2375 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2376 goto failed;
2377 Py_DECREF(value);
2378 break;
2379 }
2380 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002381failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002382 Py_XDECREF(value);
2383 Py_XDECREF(result);
2384 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385}
2386
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002387PyObject*
2388ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002390 stmt_ty o = (stmt_ty)_o;
2391 PyObject *result = NULL, *value = NULL;
2392 if (!o) {
2393 Py_INCREF(Py_None);
2394 return Py_None;
2395 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002396
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002397 switch (o->kind) {
2398 case FunctionDef_kind:
2399 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2400 if (!result) goto failed;
2401 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002402 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002403 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2404 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002405 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002406 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002407 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002408 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2409 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002410 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002411 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2412 if (!value) goto failed;
2413 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2414 goto failed;
2415 Py_DECREF(value);
2416 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2417 if (!value) goto failed;
2418 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2419 goto failed;
2420 Py_DECREF(value);
2421 value = ast2obj_expr(o->v.FunctionDef.returns);
2422 if (!value) goto failed;
2423 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2424 goto failed;
2425 Py_DECREF(value);
2426 break;
2427 case ClassDef_kind:
2428 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2429 if (!result) goto failed;
2430 value = ast2obj_identifier(o->v.ClassDef.name);
2431 if (!value) goto failed;
2432 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2433 goto failed;
2434 Py_DECREF(value);
2435 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2436 if (!value) goto failed;
2437 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2438 goto failed;
2439 Py_DECREF(value);
2440 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2441 if (!value) goto failed;
2442 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2443 goto failed;
2444 Py_DECREF(value);
2445 value = ast2obj_expr(o->v.ClassDef.starargs);
2446 if (!value) goto failed;
2447 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2448 goto failed;
2449 Py_DECREF(value);
2450 value = ast2obj_expr(o->v.ClassDef.kwargs);
2451 if (!value) goto failed;
2452 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
2455 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2456 if (!value) goto failed;
2457 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2458 goto failed;
2459 Py_DECREF(value);
2460 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2461 if (!value) goto failed;
2462 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 break;
2466 case Return_kind:
2467 result = PyType_GenericNew(Return_type, NULL, NULL);
2468 if (!result) goto failed;
2469 value = ast2obj_expr(o->v.Return.value);
2470 if (!value) goto failed;
2471 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
2474 break;
2475 case Delete_kind:
2476 result = PyType_GenericNew(Delete_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2479 if (!value) goto failed;
2480 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 break;
2484 case Assign_kind:
2485 result = PyType_GenericNew(Assign_type, NULL, NULL);
2486 if (!result) goto failed;
2487 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2488 if (!value) goto failed;
2489 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 value = ast2obj_expr(o->v.Assign.value);
2493 if (!value) goto failed;
2494 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
2497 break;
2498 case AugAssign_kind:
2499 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2500 if (!result) goto failed;
2501 value = ast2obj_expr(o->v.AugAssign.target);
2502 if (!value) goto failed;
2503 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2504 goto failed;
2505 Py_DECREF(value);
2506 value = ast2obj_operator(o->v.AugAssign.op);
2507 if (!value) goto failed;
2508 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2509 goto failed;
2510 Py_DECREF(value);
2511 value = ast2obj_expr(o->v.AugAssign.value);
2512 if (!value) goto failed;
2513 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 break;
2517 case For_kind:
2518 result = PyType_GenericNew(For_type, NULL, NULL);
2519 if (!result) goto failed;
2520 value = ast2obj_expr(o->v.For.target);
2521 if (!value) goto failed;
2522 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
2525 value = ast2obj_expr(o->v.For.iter);
2526 if (!value) goto failed;
2527 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2531 if (!value) goto failed;
2532 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2536 if (!value) goto failed;
2537 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2538 goto failed;
2539 Py_DECREF(value);
2540 break;
2541 case While_kind:
2542 result = PyType_GenericNew(While_type, NULL, NULL);
2543 if (!result) goto failed;
2544 value = ast2obj_expr(o->v.While.test);
2545 if (!value) goto failed;
2546 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2550 if (!value) goto failed;
2551 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2555 if (!value) goto failed;
2556 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2557 goto failed;
2558 Py_DECREF(value);
2559 break;
2560 case If_kind:
2561 result = PyType_GenericNew(If_type, NULL, NULL);
2562 if (!result) goto failed;
2563 value = ast2obj_expr(o->v.If.test);
2564 if (!value) goto failed;
2565 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
2568 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2569 if (!value) goto failed;
2570 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2571 goto failed;
2572 Py_DECREF(value);
2573 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2574 if (!value) goto failed;
2575 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2576 goto failed;
2577 Py_DECREF(value);
2578 break;
2579 case With_kind:
2580 result = PyType_GenericNew(With_type, NULL, NULL);
2581 if (!result) goto failed;
2582 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2583 if (!value) goto failed;
2584 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
2587 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2588 if (!value) goto failed;
2589 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 break;
2593 case Raise_kind:
2594 result = PyType_GenericNew(Raise_type, NULL, NULL);
2595 if (!result) goto failed;
2596 value = ast2obj_expr(o->v.Raise.exc);
2597 if (!value) goto failed;
2598 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
2601 value = ast2obj_expr(o->v.Raise.cause);
2602 if (!value) goto failed;
2603 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 break;
2607 case Try_kind:
2608 result = PyType_GenericNew(Try_type, NULL, NULL);
2609 if (!result) goto failed;
2610 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2611 if (!value) goto failed;
2612 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
2615 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2616 if (!value) goto failed;
2617 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
2620 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2621 if (!value) goto failed;
2622 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2623 goto failed;
2624 Py_DECREF(value);
2625 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2626 if (!value) goto failed;
2627 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2628 goto failed;
2629 Py_DECREF(value);
2630 break;
2631 case Assert_kind:
2632 result = PyType_GenericNew(Assert_type, NULL, NULL);
2633 if (!result) goto failed;
2634 value = ast2obj_expr(o->v.Assert.test);
2635 if (!value) goto failed;
2636 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2637 goto failed;
2638 Py_DECREF(value);
2639 value = ast2obj_expr(o->v.Assert.msg);
2640 if (!value) goto failed;
2641 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2642 goto failed;
2643 Py_DECREF(value);
2644 break;
2645 case Import_kind:
2646 result = PyType_GenericNew(Import_type, NULL, NULL);
2647 if (!result) goto failed;
2648 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2649 if (!value) goto failed;
2650 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2651 goto failed;
2652 Py_DECREF(value);
2653 break;
2654 case ImportFrom_kind:
2655 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2656 if (!result) goto failed;
2657 value = ast2obj_identifier(o->v.ImportFrom.module);
2658 if (!value) goto failed;
2659 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2663 if (!value) goto failed;
2664 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 value = ast2obj_int(o->v.ImportFrom.level);
2668 if (!value) goto failed;
2669 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
2672 break;
2673 case Global_kind:
2674 result = PyType_GenericNew(Global_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2677 if (!value) goto failed;
2678 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 break;
2682 case Nonlocal_kind:
2683 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 break;
2691 case Expr_kind:
2692 result = PyType_GenericNew(Expr_type, NULL, NULL);
2693 if (!result) goto failed;
2694 value = ast2obj_expr(o->v.Expr.value);
2695 if (!value) goto failed;
2696 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 break;
2700 case Pass_kind:
2701 result = PyType_GenericNew(Pass_type, NULL, NULL);
2702 if (!result) goto failed;
2703 break;
2704 case Break_kind:
2705 result = PyType_GenericNew(Break_type, NULL, NULL);
2706 if (!result) goto failed;
2707 break;
2708 case Continue_kind:
2709 result = PyType_GenericNew(Continue_type, NULL, NULL);
2710 if (!result) goto failed;
2711 break;
2712 }
2713 value = ast2obj_int(o->lineno);
2714 if (!value) goto failed;
2715 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2716 goto failed;
2717 Py_DECREF(value);
2718 value = ast2obj_int(o->col_offset);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2721 goto failed;
2722 Py_DECREF(value);
2723 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002724failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002725 Py_XDECREF(value);
2726 Py_XDECREF(result);
2727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728}
2729
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002730PyObject*
2731ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002733 expr_ty o = (expr_ty)_o;
2734 PyObject *result = NULL, *value = NULL;
2735 if (!o) {
2736 Py_INCREF(Py_None);
2737 return Py_None;
2738 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002739
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002740 switch (o->kind) {
2741 case BoolOp_kind:
2742 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2743 if (!result) goto failed;
2744 value = ast2obj_boolop(o->v.BoolOp.op);
2745 if (!value) goto failed;
2746 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2747 goto failed;
2748 Py_DECREF(value);
2749 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2750 if (!value) goto failed;
2751 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
2754 break;
2755 case BinOp_kind:
2756 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2757 if (!result) goto failed;
2758 value = ast2obj_expr(o->v.BinOp.left);
2759 if (!value) goto failed;
2760 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2761 goto failed;
2762 Py_DECREF(value);
2763 value = ast2obj_operator(o->v.BinOp.op);
2764 if (!value) goto failed;
2765 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
2768 value = ast2obj_expr(o->v.BinOp.right);
2769 if (!value) goto failed;
2770 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 break;
2774 case UnaryOp_kind:
2775 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2776 if (!result) goto failed;
2777 value = ast2obj_unaryop(o->v.UnaryOp.op);
2778 if (!value) goto failed;
2779 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
2782 value = ast2obj_expr(o->v.UnaryOp.operand);
2783 if (!value) goto failed;
2784 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 break;
2788 case Lambda_kind:
2789 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2790 if (!result) goto failed;
2791 value = ast2obj_arguments(o->v.Lambda.args);
2792 if (!value) goto failed;
2793 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 value = ast2obj_expr(o->v.Lambda.body);
2797 if (!value) goto failed;
2798 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2799 goto failed;
2800 Py_DECREF(value);
2801 break;
2802 case IfExp_kind:
2803 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2804 if (!result) goto failed;
2805 value = ast2obj_expr(o->v.IfExp.test);
2806 if (!value) goto failed;
2807 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2808 goto failed;
2809 Py_DECREF(value);
2810 value = ast2obj_expr(o->v.IfExp.body);
2811 if (!value) goto failed;
2812 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
2815 value = ast2obj_expr(o->v.IfExp.orelse);
2816 if (!value) goto failed;
2817 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
2820 break;
2821 case Dict_kind:
2822 result = PyType_GenericNew(Dict_type, NULL, NULL);
2823 if (!result) goto failed;
2824 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2825 if (!value) goto failed;
2826 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2830 if (!value) goto failed;
2831 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 break;
2835 case Set_kind:
2836 result = PyType_GenericNew(Set_type, NULL, NULL);
2837 if (!result) goto failed;
2838 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2839 if (!value) goto failed;
2840 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
2843 break;
2844 case ListComp_kind:
2845 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2846 if (!result) goto failed;
2847 value = ast2obj_expr(o->v.ListComp.elt);
2848 if (!value) goto failed;
2849 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
2852 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2853 if (!value) goto failed;
2854 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2855 goto failed;
2856 Py_DECREF(value);
2857 break;
2858 case SetComp_kind:
2859 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2860 if (!result) goto failed;
2861 value = ast2obj_expr(o->v.SetComp.elt);
2862 if (!value) goto failed;
2863 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2867 if (!value) goto failed;
2868 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 break;
2872 case DictComp_kind:
2873 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2874 if (!result) goto failed;
2875 value = ast2obj_expr(o->v.DictComp.key);
2876 if (!value) goto failed;
2877 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 value = ast2obj_expr(o->v.DictComp.value);
2881 if (!value) goto failed;
2882 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 break;
2891 case GeneratorExp_kind:
2892 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2893 if (!result) goto failed;
2894 value = ast2obj_expr(o->v.GeneratorExp.elt);
2895 if (!value) goto failed;
2896 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
2899 value = ast2obj_list(o->v.GeneratorExp.generators,
2900 ast2obj_comprehension);
2901 if (!value) goto failed;
2902 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2903 goto failed;
2904 Py_DECREF(value);
2905 break;
2906 case Yield_kind:
2907 result = PyType_GenericNew(Yield_type, NULL, NULL);
2908 if (!result) goto failed;
2909 value = ast2obj_expr(o->v.Yield.value);
2910 if (!value) goto failed;
2911 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2912 goto failed;
2913 Py_DECREF(value);
2914 break;
2915 case YieldFrom_kind:
2916 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2917 if (!result) goto failed;
2918 value = ast2obj_expr(o->v.YieldFrom.value);
2919 if (!value) goto failed;
2920 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2921 goto failed;
2922 Py_DECREF(value);
2923 break;
2924 case Compare_kind:
2925 result = PyType_GenericNew(Compare_type, NULL, NULL);
2926 if (!result) goto failed;
2927 value = ast2obj_expr(o->v.Compare.left);
2928 if (!value) goto failed;
2929 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2930 goto failed;
2931 Py_DECREF(value);
2932 {
2933 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2934 value = PyList_New(n);
2935 if (!value) goto failed;
2936 for(i = 0; i < n; i++)
2937 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002939 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002940 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2941 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002942 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002943 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002944 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002945 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
2946 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002947 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002948 break;
2949 case Call_kind:
2950 result = PyType_GenericNew(Call_type, NULL, NULL);
2951 if (!result) goto failed;
2952 value = ast2obj_expr(o->v.Call.func);
2953 if (!value) goto failed;
2954 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2955 goto failed;
2956 Py_DECREF(value);
2957 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2958 if (!value) goto failed;
2959 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2960 goto failed;
2961 Py_DECREF(value);
2962 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2963 if (!value) goto failed;
2964 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2965 goto failed;
2966 Py_DECREF(value);
2967 value = ast2obj_expr(o->v.Call.starargs);
2968 if (!value) goto failed;
2969 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2970 goto failed;
2971 Py_DECREF(value);
2972 value = ast2obj_expr(o->v.Call.kwargs);
2973 if (!value) goto failed;
2974 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2975 goto failed;
2976 Py_DECREF(value);
2977 break;
2978 case Num_kind:
2979 result = PyType_GenericNew(Num_type, NULL, NULL);
2980 if (!result) goto failed;
2981 value = ast2obj_object(o->v.Num.n);
2982 if (!value) goto failed;
2983 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2984 goto failed;
2985 Py_DECREF(value);
2986 break;
2987 case Str_kind:
2988 result = PyType_GenericNew(Str_type, NULL, NULL);
2989 if (!result) goto failed;
2990 value = ast2obj_string(o->v.Str.s);
2991 if (!value) goto failed;
2992 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2993 goto failed;
2994 Py_DECREF(value);
2995 break;
2996 case Bytes_kind:
2997 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2998 if (!result) goto failed;
2999 value = ast2obj_bytes(o->v.Bytes.s);
3000 if (!value) goto failed;
3001 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3002 goto failed;
3003 Py_DECREF(value);
3004 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003005 case NameConstant_kind:
3006 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3007 if (!result) goto failed;
3008 value = ast2obj_singleton(o->v.NameConstant.value);
3009 if (!value) goto failed;
3010 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3011 goto failed;
3012 Py_DECREF(value);
3013 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003014 case Ellipsis_kind:
3015 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3016 if (!result) goto failed;
3017 break;
3018 case Attribute_kind:
3019 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3020 if (!result) goto failed;
3021 value = ast2obj_expr(o->v.Attribute.value);
3022 if (!value) goto failed;
3023 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3024 goto failed;
3025 Py_DECREF(value);
3026 value = ast2obj_identifier(o->v.Attribute.attr);
3027 if (!value) goto failed;
3028 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3029 goto failed;
3030 Py_DECREF(value);
3031 value = ast2obj_expr_context(o->v.Attribute.ctx);
3032 if (!value) goto failed;
3033 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3034 goto failed;
3035 Py_DECREF(value);
3036 break;
3037 case Subscript_kind:
3038 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3039 if (!result) goto failed;
3040 value = ast2obj_expr(o->v.Subscript.value);
3041 if (!value) goto failed;
3042 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3043 goto failed;
3044 Py_DECREF(value);
3045 value = ast2obj_slice(o->v.Subscript.slice);
3046 if (!value) goto failed;
3047 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3048 goto failed;
3049 Py_DECREF(value);
3050 value = ast2obj_expr_context(o->v.Subscript.ctx);
3051 if (!value) goto failed;
3052 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3053 goto failed;
3054 Py_DECREF(value);
3055 break;
3056 case Starred_kind:
3057 result = PyType_GenericNew(Starred_type, NULL, NULL);
3058 if (!result) goto failed;
3059 value = ast2obj_expr(o->v.Starred.value);
3060 if (!value) goto failed;
3061 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3062 goto failed;
3063 Py_DECREF(value);
3064 value = ast2obj_expr_context(o->v.Starred.ctx);
3065 if (!value) goto failed;
3066 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3067 goto failed;
3068 Py_DECREF(value);
3069 break;
3070 case Name_kind:
3071 result = PyType_GenericNew(Name_type, NULL, NULL);
3072 if (!result) goto failed;
3073 value = ast2obj_identifier(o->v.Name.id);
3074 if (!value) goto failed;
3075 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3076 goto failed;
3077 Py_DECREF(value);
3078 value = ast2obj_expr_context(o->v.Name.ctx);
3079 if (!value) goto failed;
3080 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3081 goto failed;
3082 Py_DECREF(value);
3083 break;
3084 case List_kind:
3085 result = PyType_GenericNew(List_type, NULL, NULL);
3086 if (!result) goto failed;
3087 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3088 if (!value) goto failed;
3089 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3090 goto failed;
3091 Py_DECREF(value);
3092 value = ast2obj_expr_context(o->v.List.ctx);
3093 if (!value) goto failed;
3094 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3095 goto failed;
3096 Py_DECREF(value);
3097 break;
3098 case Tuple_kind:
3099 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3100 if (!result) goto failed;
3101 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3102 if (!value) goto failed;
3103 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3104 goto failed;
3105 Py_DECREF(value);
3106 value = ast2obj_expr_context(o->v.Tuple.ctx);
3107 if (!value) goto failed;
3108 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3109 goto failed;
3110 Py_DECREF(value);
3111 break;
3112 }
3113 value = ast2obj_int(o->lineno);
3114 if (!value) goto failed;
3115 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3116 goto failed;
3117 Py_DECREF(value);
3118 value = ast2obj_int(o->col_offset);
3119 if (!value) goto failed;
3120 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3121 goto failed;
3122 Py_DECREF(value);
3123 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003124failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003125 Py_XDECREF(value);
3126 Py_XDECREF(result);
3127 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128}
3129
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003130PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003132 switch(o) {
3133 case Load:
3134 Py_INCREF(Load_singleton);
3135 return Load_singleton;
3136 case Store:
3137 Py_INCREF(Store_singleton);
3138 return Store_singleton;
3139 case Del:
3140 Py_INCREF(Del_singleton);
3141 return Del_singleton;
3142 case AugLoad:
3143 Py_INCREF(AugLoad_singleton);
3144 return AugLoad_singleton;
3145 case AugStore:
3146 Py_INCREF(AugStore_singleton);
3147 return AugStore_singleton;
3148 case Param:
3149 Py_INCREF(Param_singleton);
3150 return Param_singleton;
3151 default:
3152 /* should never happen, but just in case ... */
3153 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3154 return NULL;
3155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003157PyObject*
3158ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003160 slice_ty o = (slice_ty)_o;
3161 PyObject *result = NULL, *value = NULL;
3162 if (!o) {
3163 Py_INCREF(Py_None);
3164 return Py_None;
3165 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003166
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003167 switch (o->kind) {
3168 case Slice_kind:
3169 result = PyType_GenericNew(Slice_type, NULL, NULL);
3170 if (!result) goto failed;
3171 value = ast2obj_expr(o->v.Slice.lower);
3172 if (!value) goto failed;
3173 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3174 goto failed;
3175 Py_DECREF(value);
3176 value = ast2obj_expr(o->v.Slice.upper);
3177 if (!value) goto failed;
3178 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3179 goto failed;
3180 Py_DECREF(value);
3181 value = ast2obj_expr(o->v.Slice.step);
3182 if (!value) goto failed;
3183 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3184 goto failed;
3185 Py_DECREF(value);
3186 break;
3187 case ExtSlice_kind:
3188 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3189 if (!result) goto failed;
3190 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3191 if (!value) goto failed;
3192 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3193 goto failed;
3194 Py_DECREF(value);
3195 break;
3196 case Index_kind:
3197 result = PyType_GenericNew(Index_type, NULL, NULL);
3198 if (!result) goto failed;
3199 value = ast2obj_expr(o->v.Index.value);
3200 if (!value) goto failed;
3201 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3202 goto failed;
3203 Py_DECREF(value);
3204 break;
3205 }
3206 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003207failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003208 Py_XDECREF(value);
3209 Py_XDECREF(result);
3210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211}
3212
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003213PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003215 switch(o) {
3216 case And:
3217 Py_INCREF(And_singleton);
3218 return And_singleton;
3219 case Or:
3220 Py_INCREF(Or_singleton);
3221 return Or_singleton;
3222 default:
3223 /* should never happen, but just in case ... */
3224 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3225 return NULL;
3226 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003227}
3228PyObject* ast2obj_operator(operator_ty o)
3229{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003230 switch(o) {
3231 case Add:
3232 Py_INCREF(Add_singleton);
3233 return Add_singleton;
3234 case Sub:
3235 Py_INCREF(Sub_singleton);
3236 return Sub_singleton;
3237 case Mult:
3238 Py_INCREF(Mult_singleton);
3239 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003240 case MatMult:
3241 Py_INCREF(MatMult_singleton);
3242 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003243 case Div:
3244 Py_INCREF(Div_singleton);
3245 return Div_singleton;
3246 case Mod:
3247 Py_INCREF(Mod_singleton);
3248 return Mod_singleton;
3249 case Pow:
3250 Py_INCREF(Pow_singleton);
3251 return Pow_singleton;
3252 case LShift:
3253 Py_INCREF(LShift_singleton);
3254 return LShift_singleton;
3255 case RShift:
3256 Py_INCREF(RShift_singleton);
3257 return RShift_singleton;
3258 case BitOr:
3259 Py_INCREF(BitOr_singleton);
3260 return BitOr_singleton;
3261 case BitXor:
3262 Py_INCREF(BitXor_singleton);
3263 return BitXor_singleton;
3264 case BitAnd:
3265 Py_INCREF(BitAnd_singleton);
3266 return BitAnd_singleton;
3267 case FloorDiv:
3268 Py_INCREF(FloorDiv_singleton);
3269 return FloorDiv_singleton;
3270 default:
3271 /* should never happen, but just in case ... */
3272 PyErr_Format(PyExc_SystemError, "unknown operator found");
3273 return NULL;
3274 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003275}
3276PyObject* ast2obj_unaryop(unaryop_ty o)
3277{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003278 switch(o) {
3279 case Invert:
3280 Py_INCREF(Invert_singleton);
3281 return Invert_singleton;
3282 case Not:
3283 Py_INCREF(Not_singleton);
3284 return Not_singleton;
3285 case UAdd:
3286 Py_INCREF(UAdd_singleton);
3287 return UAdd_singleton;
3288 case USub:
3289 Py_INCREF(USub_singleton);
3290 return USub_singleton;
3291 default:
3292 /* should never happen, but just in case ... */
3293 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3294 return NULL;
3295 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003296}
3297PyObject* ast2obj_cmpop(cmpop_ty o)
3298{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003299 switch(o) {
3300 case Eq:
3301 Py_INCREF(Eq_singleton);
3302 return Eq_singleton;
3303 case NotEq:
3304 Py_INCREF(NotEq_singleton);
3305 return NotEq_singleton;
3306 case Lt:
3307 Py_INCREF(Lt_singleton);
3308 return Lt_singleton;
3309 case LtE:
3310 Py_INCREF(LtE_singleton);
3311 return LtE_singleton;
3312 case Gt:
3313 Py_INCREF(Gt_singleton);
3314 return Gt_singleton;
3315 case GtE:
3316 Py_INCREF(GtE_singleton);
3317 return GtE_singleton;
3318 case Is:
3319 Py_INCREF(Is_singleton);
3320 return Is_singleton;
3321 case IsNot:
3322 Py_INCREF(IsNot_singleton);
3323 return IsNot_singleton;
3324 case In:
3325 Py_INCREF(In_singleton);
3326 return In_singleton;
3327 case NotIn:
3328 Py_INCREF(NotIn_singleton);
3329 return NotIn_singleton;
3330 default:
3331 /* should never happen, but just in case ... */
3332 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3333 return NULL;
3334 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003335}
3336PyObject*
3337ast2obj_comprehension(void* _o)
3338{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003339 comprehension_ty o = (comprehension_ty)_o;
3340 PyObject *result = NULL, *value = NULL;
3341 if (!o) {
3342 Py_INCREF(Py_None);
3343 return Py_None;
3344 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003345
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003346 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3347 if (!result) return NULL;
3348 value = ast2obj_expr(o->target);
3349 if (!value) goto failed;
3350 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3351 goto failed;
3352 Py_DECREF(value);
3353 value = ast2obj_expr(o->iter);
3354 if (!value) goto failed;
3355 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3356 goto failed;
3357 Py_DECREF(value);
3358 value = ast2obj_list(o->ifs, ast2obj_expr);
3359 if (!value) goto failed;
3360 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3361 goto failed;
3362 Py_DECREF(value);
3363 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003364failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003365 Py_XDECREF(value);
3366 Py_XDECREF(result);
3367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368}
3369
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003370PyObject*
3371ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003373 excepthandler_ty o = (excepthandler_ty)_o;
3374 PyObject *result = NULL, *value = NULL;
3375 if (!o) {
3376 Py_INCREF(Py_None);
3377 return Py_None;
3378 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003379
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003380 switch (o->kind) {
3381 case ExceptHandler_kind:
3382 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3383 if (!result) goto failed;
3384 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003385 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003386 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3387 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003388 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003389 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003390 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003391 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3392 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003393 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003394 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3395 if (!value) goto failed;
3396 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3397 goto failed;
3398 Py_DECREF(value);
3399 break;
3400 }
3401 value = ast2obj_int(o->lineno);
3402 if (!value) goto failed;
3403 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3404 goto failed;
3405 Py_DECREF(value);
3406 value = ast2obj_int(o->col_offset);
3407 if (!value) goto failed;
3408 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3409 goto failed;
3410 Py_DECREF(value);
3411 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003412failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003413 Py_XDECREF(value);
3414 Py_XDECREF(result);
3415 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416}
3417
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003418PyObject*
3419ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003421 arguments_ty o = (arguments_ty)_o;
3422 PyObject *result = NULL, *value = NULL;
3423 if (!o) {
3424 Py_INCREF(Py_None);
3425 return Py_None;
3426 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003427
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003428 result = PyType_GenericNew(arguments_type, NULL, NULL);
3429 if (!result) return NULL;
3430 value = ast2obj_list(o->args, ast2obj_arg);
3431 if (!value) goto failed;
3432 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003435 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003436 if (!value) goto failed;
3437 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3438 goto failed;
3439 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003440 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3441 if (!value) goto failed;
3442 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3443 goto failed;
3444 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003445 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003446 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003447 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003448 goto failed;
3449 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003450 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003451 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003452 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003453 goto failed;
3454 Py_DECREF(value);
3455 value = ast2obj_list(o->defaults, ast2obj_expr);
3456 if (!value) goto failed;
3457 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3458 goto failed;
3459 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003460 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003461failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003462 Py_XDECREF(value);
3463 Py_XDECREF(result);
3464 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465}
3466
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003467PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003468ast2obj_arg(void* _o)
3469{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003470 arg_ty o = (arg_ty)_o;
3471 PyObject *result = NULL, *value = NULL;
3472 if (!o) {
3473 Py_INCREF(Py_None);
3474 return Py_None;
3475 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003476
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003477 result = PyType_GenericNew(arg_type, NULL, NULL);
3478 if (!result) return NULL;
3479 value = ast2obj_identifier(o->arg);
3480 if (!value) goto failed;
3481 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3482 goto failed;
3483 Py_DECREF(value);
3484 value = ast2obj_expr(o->annotation);
3485 if (!value) goto failed;
3486 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3487 goto failed;
3488 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003489 value = ast2obj_int(o->lineno);
3490 if (!value) goto failed;
3491 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3492 goto failed;
3493 Py_DECREF(value);
3494 value = ast2obj_int(o->col_offset);
3495 if (!value) goto failed;
3496 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3497 goto failed;
3498 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003499 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003500failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003501 Py_XDECREF(value);
3502 Py_XDECREF(result);
3503 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003504}
3505
3506PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003507ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003509 keyword_ty o = (keyword_ty)_o;
3510 PyObject *result = NULL, *value = NULL;
3511 if (!o) {
3512 Py_INCREF(Py_None);
3513 return Py_None;
3514 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003515
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003516 result = PyType_GenericNew(keyword_type, NULL, NULL);
3517 if (!result) return NULL;
3518 value = ast2obj_identifier(o->arg);
3519 if (!value) goto failed;
3520 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3521 goto failed;
3522 Py_DECREF(value);
3523 value = ast2obj_expr(o->value);
3524 if (!value) goto failed;
3525 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3526 goto failed;
3527 Py_DECREF(value);
3528 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003529failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 Py_XDECREF(value);
3531 Py_XDECREF(result);
3532 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533}
3534
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003535PyObject*
3536ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003538 alias_ty o = (alias_ty)_o;
3539 PyObject *result = NULL, *value = NULL;
3540 if (!o) {
3541 Py_INCREF(Py_None);
3542 return Py_None;
3543 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003544
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003545 result = PyType_GenericNew(alias_type, NULL, NULL);
3546 if (!result) return NULL;
3547 value = ast2obj_identifier(o->name);
3548 if (!value) goto failed;
3549 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3550 goto failed;
3551 Py_DECREF(value);
3552 value = ast2obj_identifier(o->asname);
3553 if (!value) goto failed;
3554 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3555 goto failed;
3556 Py_DECREF(value);
3557 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003558failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003559 Py_XDECREF(value);
3560 Py_XDECREF(result);
3561 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562}
3563
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003564PyObject*
3565ast2obj_withitem(void* _o)
3566{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003567 withitem_ty o = (withitem_ty)_o;
3568 PyObject *result = NULL, *value = NULL;
3569 if (!o) {
3570 Py_INCREF(Py_None);
3571 return Py_None;
3572 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003573
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003574 result = PyType_GenericNew(withitem_type, NULL, NULL);
3575 if (!result) return NULL;
3576 value = ast2obj_expr(o->context_expr);
3577 if (!value) goto failed;
3578 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3579 goto failed;
3580 Py_DECREF(value);
3581 value = ast2obj_expr(o->optional_vars);
3582 if (!value) goto failed;
3583 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3584 goto failed;
3585 Py_DECREF(value);
3586 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003587failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003588 Py_XDECREF(value);
3589 Py_XDECREF(result);
3590 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003591}
3592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003594int
3595obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3596{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003597 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003598
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003599 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003600
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003601 if (obj == Py_None) {
3602 *out = NULL;
3603 return 0;
3604 }
3605 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3606 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003607 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003608 }
3609 if (isinstance) {
3610 asdl_seq* body;
3611
3612 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3613 int res;
3614 Py_ssize_t len;
3615 Py_ssize_t i;
3616 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3617 if (tmp == NULL) goto failed;
3618 if (!PyList_Check(tmp)) {
3619 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3620 goto failed;
3621 }
3622 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003623 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003624 if (body == NULL) goto failed;
3625 for (i = 0; i < len; i++) {
3626 stmt_ty value;
3627 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3628 if (res != 0) goto failed;
3629 asdl_seq_SET(body, i, value);
3630 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003631 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003632 } else {
3633 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3634 return 1;
3635 }
3636 *out = Module(body, arena);
3637 if (*out == NULL) goto failed;
3638 return 0;
3639 }
3640 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3641 if (isinstance == -1) {
3642 return 1;
3643 }
3644 if (isinstance) {
3645 asdl_seq* body;
3646
3647 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3648 int res;
3649 Py_ssize_t len;
3650 Py_ssize_t i;
3651 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3652 if (tmp == NULL) goto failed;
3653 if (!PyList_Check(tmp)) {
3654 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3655 goto failed;
3656 }
3657 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003658 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003659 if (body == NULL) goto failed;
3660 for (i = 0; i < len; i++) {
3661 stmt_ty value;
3662 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3663 if (res != 0) goto failed;
3664 asdl_seq_SET(body, i, value);
3665 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003666 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003667 } else {
3668 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3669 return 1;
3670 }
3671 *out = Interactive(body, arena);
3672 if (*out == NULL) goto failed;
3673 return 0;
3674 }
3675 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3676 if (isinstance == -1) {
3677 return 1;
3678 }
3679 if (isinstance) {
3680 expr_ty body;
3681
3682 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3683 int res;
3684 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3685 if (tmp == NULL) goto failed;
3686 res = obj2ast_expr(tmp, &body, arena);
3687 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003688 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 } else {
3690 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3691 return 1;
3692 }
3693 *out = Expression(body, arena);
3694 if (*out == NULL) goto failed;
3695 return 0;
3696 }
3697 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3698 if (isinstance == -1) {
3699 return 1;
3700 }
3701 if (isinstance) {
3702 asdl_seq* body;
3703
3704 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3705 int res;
3706 Py_ssize_t len;
3707 Py_ssize_t i;
3708 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3709 if (tmp == NULL) goto failed;
3710 if (!PyList_Check(tmp)) {
3711 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3712 goto failed;
3713 }
3714 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003715 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003716 if (body == NULL) goto failed;
3717 for (i = 0; i < len; i++) {
3718 stmt_ty value;
3719 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3720 if (res != 0) goto failed;
3721 asdl_seq_SET(body, i, value);
3722 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003723 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003724 } else {
3725 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3726 return 1;
3727 }
3728 *out = Suite(body, arena);
3729 if (*out == NULL) goto failed;
3730 return 0;
3731 }
3732
3733 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3734 failed:
3735 Py_XDECREF(tmp);
3736 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003737}
3738
3739int
3740obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3741{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003742 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003743
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003744 PyObject *tmp = NULL;
3745 int lineno;
3746 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003747
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003748 if (obj == Py_None) {
3749 *out = NULL;
3750 return 0;
3751 }
3752 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3753 int res;
3754 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3755 if (tmp == NULL) goto failed;
3756 res = obj2ast_int(tmp, &lineno, arena);
3757 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003758 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003759 } else {
3760 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003761 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 }
3763 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3764 int res;
3765 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3766 if (tmp == NULL) goto failed;
3767 res = obj2ast_int(tmp, &col_offset, arena);
3768 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003770 } else {
3771 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3772 return 1;
3773 }
3774 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3775 if (isinstance == -1) {
3776 return 1;
3777 }
3778 if (isinstance) {
3779 identifier name;
3780 arguments_ty args;
3781 asdl_seq* body;
3782 asdl_seq* decorator_list;
3783 expr_ty returns;
3784
3785 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3786 int res;
3787 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3788 if (tmp == NULL) goto failed;
3789 res = obj2ast_identifier(tmp, &name, arena);
3790 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003791 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 } else {
3793 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3794 return 1;
3795 }
3796 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3797 int res;
3798 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3799 if (tmp == NULL) goto failed;
3800 res = obj2ast_arguments(tmp, &args, arena);
3801 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003802 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003803 } else {
3804 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3805 return 1;
3806 }
3807 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3808 int res;
3809 Py_ssize_t len;
3810 Py_ssize_t i;
3811 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3812 if (tmp == NULL) goto failed;
3813 if (!PyList_Check(tmp)) {
3814 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3815 goto failed;
3816 }
3817 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003818 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 if (body == NULL) goto failed;
3820 for (i = 0; i < len; i++) {
3821 stmt_ty value;
3822 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3823 if (res != 0) goto failed;
3824 asdl_seq_SET(body, i, value);
3825 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003827 } else {
3828 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3829 return 1;
3830 }
3831 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3832 int res;
3833 Py_ssize_t len;
3834 Py_ssize_t i;
3835 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3836 if (tmp == NULL) goto failed;
3837 if (!PyList_Check(tmp)) {
3838 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3839 goto failed;
3840 }
3841 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003842 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 if (decorator_list == NULL) goto failed;
3844 for (i = 0; i < len; i++) {
3845 expr_ty value;
3846 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3847 if (res != 0) goto failed;
3848 asdl_seq_SET(decorator_list, i, value);
3849 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003850 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003851 } else {
3852 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3853 return 1;
3854 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003855 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003856 int res;
3857 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3858 if (tmp == NULL) goto failed;
3859 res = obj2ast_expr(tmp, &returns, arena);
3860 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003861 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003862 } else {
3863 returns = NULL;
3864 }
3865 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3866 col_offset, arena);
3867 if (*out == NULL) goto failed;
3868 return 0;
3869 }
3870 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3871 if (isinstance == -1) {
3872 return 1;
3873 }
3874 if (isinstance) {
3875 identifier name;
3876 asdl_seq* bases;
3877 asdl_seq* keywords;
3878 expr_ty starargs;
3879 expr_ty kwargs;
3880 asdl_seq* body;
3881 asdl_seq* decorator_list;
3882
3883 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3884 int res;
3885 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3886 if (tmp == NULL) goto failed;
3887 res = obj2ast_identifier(tmp, &name, arena);
3888 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003889 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003890 } else {
3891 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3892 return 1;
3893 }
3894 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3895 int res;
3896 Py_ssize_t len;
3897 Py_ssize_t i;
3898 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3899 if (tmp == NULL) goto failed;
3900 if (!PyList_Check(tmp)) {
3901 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3902 goto failed;
3903 }
3904 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003905 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003906 if (bases == NULL) goto failed;
3907 for (i = 0; i < len; i++) {
3908 expr_ty value;
3909 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3910 if (res != 0) goto failed;
3911 asdl_seq_SET(bases, i, value);
3912 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003913 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003914 } else {
3915 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3916 return 1;
3917 }
3918 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3919 int res;
3920 Py_ssize_t len;
3921 Py_ssize_t i;
3922 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3923 if (tmp == NULL) goto failed;
3924 if (!PyList_Check(tmp)) {
3925 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3926 goto failed;
3927 }
3928 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003929 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 if (keywords == NULL) goto failed;
3931 for (i = 0; i < len; i++) {
3932 keyword_ty value;
3933 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3934 if (res != 0) goto failed;
3935 asdl_seq_SET(keywords, i, value);
3936 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003937 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3940 return 1;
3941 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003942 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003943 int res;
3944 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3945 if (tmp == NULL) goto failed;
3946 res = obj2ast_expr(tmp, &starargs, arena);
3947 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003948 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003949 } else {
3950 starargs = NULL;
3951 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003952 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003953 int res;
3954 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3955 if (tmp == NULL) goto failed;
3956 res = obj2ast_expr(tmp, &kwargs, arena);
3957 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003958 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 } else {
3960 kwargs = NULL;
3961 }
3962 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3963 int res;
3964 Py_ssize_t len;
3965 Py_ssize_t i;
3966 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3967 if (tmp == NULL) goto failed;
3968 if (!PyList_Check(tmp)) {
3969 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3970 goto failed;
3971 }
3972 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003973 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 if (body == NULL) goto failed;
3975 for (i = 0; i < len; i++) {
3976 stmt_ty value;
3977 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3978 if (res != 0) goto failed;
3979 asdl_seq_SET(body, i, value);
3980 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003981 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003982 } else {
3983 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3984 return 1;
3985 }
3986 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3987 int res;
3988 Py_ssize_t len;
3989 Py_ssize_t i;
3990 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3991 if (tmp == NULL) goto failed;
3992 if (!PyList_Check(tmp)) {
3993 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3994 goto failed;
3995 }
3996 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003997 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003998 if (decorator_list == NULL) goto failed;
3999 for (i = 0; i < len; i++) {
4000 expr_ty value;
4001 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4002 if (res != 0) goto failed;
4003 asdl_seq_SET(decorator_list, i, value);
4004 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004005 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004006 } else {
4007 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4008 return 1;
4009 }
4010 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
4011 decorator_list, lineno, col_offset, arena);
4012 if (*out == NULL) goto failed;
4013 return 0;
4014 }
4015 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4016 if (isinstance == -1) {
4017 return 1;
4018 }
4019 if (isinstance) {
4020 expr_ty value;
4021
Victor Stinneree4b59c2013-07-27 00:01:35 +02004022 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004023 int res;
4024 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4025 if (tmp == NULL) goto failed;
4026 res = obj2ast_expr(tmp, &value, arena);
4027 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004028 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004029 } else {
4030 value = NULL;
4031 }
4032 *out = Return(value, lineno, col_offset, arena);
4033 if (*out == NULL) goto failed;
4034 return 0;
4035 }
4036 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4037 if (isinstance == -1) {
4038 return 1;
4039 }
4040 if (isinstance) {
4041 asdl_seq* targets;
4042
4043 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4044 int res;
4045 Py_ssize_t len;
4046 Py_ssize_t i;
4047 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4048 if (tmp == NULL) goto failed;
4049 if (!PyList_Check(tmp)) {
4050 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4051 goto failed;
4052 }
4053 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004054 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004055 if (targets == NULL) goto failed;
4056 for (i = 0; i < len; i++) {
4057 expr_ty value;
4058 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4059 if (res != 0) goto failed;
4060 asdl_seq_SET(targets, i, value);
4061 }
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 \"targets\" missing from Delete");
4065 return 1;
4066 }
4067 *out = Delete(targets, lineno, col_offset, arena);
4068 if (*out == NULL) goto failed;
4069 return 0;
4070 }
4071 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4072 if (isinstance == -1) {
4073 return 1;
4074 }
4075 if (isinstance) {
4076 asdl_seq* targets;
4077 expr_ty value;
4078
4079 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4080 int res;
4081 Py_ssize_t len;
4082 Py_ssize_t i;
4083 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4084 if (tmp == NULL) goto failed;
4085 if (!PyList_Check(tmp)) {
4086 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4087 goto failed;
4088 }
4089 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004090 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004091 if (targets == NULL) goto failed;
4092 for (i = 0; i < len; i++) {
4093 expr_ty value;
4094 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4095 if (res != 0) goto failed;
4096 asdl_seq_SET(targets, i, value);
4097 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004098 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004099 } else {
4100 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4101 return 1;
4102 }
4103 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4104 int res;
4105 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4106 if (tmp == NULL) goto failed;
4107 res = obj2ast_expr(tmp, &value, 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 \"value\" missing from Assign");
4112 return 1;
4113 }
4114 *out = Assign(targets, value, lineno, col_offset, arena);
4115 if (*out == NULL) goto failed;
4116 return 0;
4117 }
4118 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4119 if (isinstance == -1) {
4120 return 1;
4121 }
4122 if (isinstance) {
4123 expr_ty target;
4124 operator_ty op;
4125 expr_ty value;
4126
4127 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4128 int res;
4129 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4130 if (tmp == NULL) goto failed;
4131 res = obj2ast_expr(tmp, &target, arena);
4132 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004133 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004134 } else {
4135 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4136 return 1;
4137 }
4138 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4139 int res;
4140 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4141 if (tmp == NULL) goto failed;
4142 res = obj2ast_operator(tmp, &op, arena);
4143 if (res != 0) goto failed;
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 \"op\" missing from AugAssign");
4147 return 1;
4148 }
4149 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4150 int res;
4151 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4152 if (tmp == NULL) goto failed;
4153 res = obj2ast_expr(tmp, &value, arena);
4154 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004155 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004156 } else {
4157 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4158 return 1;
4159 }
4160 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4161 if (*out == NULL) goto failed;
4162 return 0;
4163 }
4164 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4165 if (isinstance == -1) {
4166 return 1;
4167 }
4168 if (isinstance) {
4169 expr_ty target;
4170 expr_ty iter;
4171 asdl_seq* body;
4172 asdl_seq* orelse;
4173
4174 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4175 int res;
4176 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4177 if (tmp == NULL) goto failed;
4178 res = obj2ast_expr(tmp, &target, arena);
4179 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004180 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004181 } else {
4182 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4183 return 1;
4184 }
4185 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4186 int res;
4187 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4188 if (tmp == NULL) goto failed;
4189 res = obj2ast_expr(tmp, &iter, arena);
4190 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004191 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004192 } else {
4193 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4194 return 1;
4195 }
4196 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4197 int res;
4198 Py_ssize_t len;
4199 Py_ssize_t i;
4200 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4201 if (tmp == NULL) goto failed;
4202 if (!PyList_Check(tmp)) {
4203 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4204 goto failed;
4205 }
4206 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004207 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004208 if (body == NULL) goto failed;
4209 for (i = 0; i < len; i++) {
4210 stmt_ty value;
4211 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4212 if (res != 0) goto failed;
4213 asdl_seq_SET(body, i, value);
4214 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004215 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004216 } else {
4217 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4218 return 1;
4219 }
4220 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4221 int res;
4222 Py_ssize_t len;
4223 Py_ssize_t i;
4224 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4225 if (tmp == NULL) goto failed;
4226 if (!PyList_Check(tmp)) {
4227 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4228 goto failed;
4229 }
4230 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004231 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004232 if (orelse == NULL) goto failed;
4233 for (i = 0; i < len; i++) {
4234 stmt_ty value;
4235 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4236 if (res != 0) goto failed;
4237 asdl_seq_SET(orelse, i, value);
4238 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004239 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004240 } else {
4241 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4242 return 1;
4243 }
4244 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4245 if (*out == NULL) goto failed;
4246 return 0;
4247 }
4248 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4249 if (isinstance == -1) {
4250 return 1;
4251 }
4252 if (isinstance) {
4253 expr_ty test;
4254 asdl_seq* body;
4255 asdl_seq* orelse;
4256
4257 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4258 int res;
4259 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4260 if (tmp == NULL) goto failed;
4261 res = obj2ast_expr(tmp, &test, arena);
4262 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004263 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004264 } else {
4265 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4266 return 1;
4267 }
4268 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4269 int res;
4270 Py_ssize_t len;
4271 Py_ssize_t i;
4272 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4273 if (tmp == NULL) goto failed;
4274 if (!PyList_Check(tmp)) {
4275 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4276 goto failed;
4277 }
4278 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004279 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004280 if (body == NULL) goto failed;
4281 for (i = 0; i < len; i++) {
4282 stmt_ty value;
4283 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4284 if (res != 0) goto failed;
4285 asdl_seq_SET(body, i, value);
4286 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004287 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004288 } else {
4289 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4290 return 1;
4291 }
4292 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4293 int res;
4294 Py_ssize_t len;
4295 Py_ssize_t i;
4296 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4297 if (tmp == NULL) goto failed;
4298 if (!PyList_Check(tmp)) {
4299 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4300 goto failed;
4301 }
4302 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004303 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004304 if (orelse == NULL) goto failed;
4305 for (i = 0; i < len; i++) {
4306 stmt_ty value;
4307 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4308 if (res != 0) goto failed;
4309 asdl_seq_SET(orelse, i, value);
4310 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004311 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004312 } else {
4313 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4314 return 1;
4315 }
4316 *out = While(test, body, orelse, lineno, col_offset, arena);
4317 if (*out == NULL) goto failed;
4318 return 0;
4319 }
4320 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4321 if (isinstance == -1) {
4322 return 1;
4323 }
4324 if (isinstance) {
4325 expr_ty test;
4326 asdl_seq* body;
4327 asdl_seq* orelse;
4328
4329 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4330 int res;
4331 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4332 if (tmp == NULL) goto failed;
4333 res = obj2ast_expr(tmp, &test, arena);
4334 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004335 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004336 } else {
4337 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4338 return 1;
4339 }
4340 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4341 int res;
4342 Py_ssize_t len;
4343 Py_ssize_t i;
4344 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4345 if (tmp == NULL) goto failed;
4346 if (!PyList_Check(tmp)) {
4347 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4348 goto failed;
4349 }
4350 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004351 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004352 if (body == NULL) goto failed;
4353 for (i = 0; i < len; i++) {
4354 stmt_ty value;
4355 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4356 if (res != 0) goto failed;
4357 asdl_seq_SET(body, i, value);
4358 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004359 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004360 } else {
4361 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4362 return 1;
4363 }
4364 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4365 int res;
4366 Py_ssize_t len;
4367 Py_ssize_t i;
4368 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4369 if (tmp == NULL) goto failed;
4370 if (!PyList_Check(tmp)) {
4371 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4372 goto failed;
4373 }
4374 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004375 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004376 if (orelse == NULL) goto failed;
4377 for (i = 0; i < len; i++) {
4378 stmt_ty value;
4379 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4380 if (res != 0) goto failed;
4381 asdl_seq_SET(orelse, i, value);
4382 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004383 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004384 } else {
4385 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4386 return 1;
4387 }
4388 *out = If(test, body, orelse, lineno, col_offset, arena);
4389 if (*out == NULL) goto failed;
4390 return 0;
4391 }
4392 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4393 if (isinstance == -1) {
4394 return 1;
4395 }
4396 if (isinstance) {
4397 asdl_seq* items;
4398 asdl_seq* body;
4399
4400 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4401 int res;
4402 Py_ssize_t len;
4403 Py_ssize_t i;
4404 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4405 if (tmp == NULL) goto failed;
4406 if (!PyList_Check(tmp)) {
4407 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4408 goto failed;
4409 }
4410 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004411 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004412 if (items == NULL) goto failed;
4413 for (i = 0; i < len; i++) {
4414 withitem_ty value;
4415 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4416 if (res != 0) goto failed;
4417 asdl_seq_SET(items, i, value);
4418 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004419 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004420 } else {
4421 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4422 return 1;
4423 }
4424 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4425 int res;
4426 Py_ssize_t len;
4427 Py_ssize_t i;
4428 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4429 if (tmp == NULL) goto failed;
4430 if (!PyList_Check(tmp)) {
4431 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4432 goto failed;
4433 }
4434 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004435 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004436 if (body == NULL) goto failed;
4437 for (i = 0; i < len; i++) {
4438 stmt_ty value;
4439 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4440 if (res != 0) goto failed;
4441 asdl_seq_SET(body, i, value);
4442 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004443 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004444 } else {
4445 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4446 return 1;
4447 }
4448 *out = With(items, body, lineno, col_offset, arena);
4449 if (*out == NULL) goto failed;
4450 return 0;
4451 }
4452 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4453 if (isinstance == -1) {
4454 return 1;
4455 }
4456 if (isinstance) {
4457 expr_ty exc;
4458 expr_ty cause;
4459
Victor Stinneree4b59c2013-07-27 00:01:35 +02004460 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004461 int res;
4462 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4463 if (tmp == NULL) goto failed;
4464 res = obj2ast_expr(tmp, &exc, arena);
4465 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004466 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 } else {
4468 exc = NULL;
4469 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004470 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004471 int res;
4472 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4473 if (tmp == NULL) goto failed;
4474 res = obj2ast_expr(tmp, &cause, arena);
4475 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004476 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004477 } else {
4478 cause = NULL;
4479 }
4480 *out = Raise(exc, cause, lineno, col_offset, arena);
4481 if (*out == NULL) goto failed;
4482 return 0;
4483 }
4484 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4485 if (isinstance == -1) {
4486 return 1;
4487 }
4488 if (isinstance) {
4489 asdl_seq* body;
4490 asdl_seq* handlers;
4491 asdl_seq* orelse;
4492 asdl_seq* finalbody;
4493
4494 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4495 int res;
4496 Py_ssize_t len;
4497 Py_ssize_t i;
4498 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4499 if (tmp == NULL) goto failed;
4500 if (!PyList_Check(tmp)) {
4501 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4502 goto failed;
4503 }
4504 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004505 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004506 if (body == NULL) goto failed;
4507 for (i = 0; i < len; i++) {
4508 stmt_ty value;
4509 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4510 if (res != 0) goto failed;
4511 asdl_seq_SET(body, i, value);
4512 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004513 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004514 } else {
4515 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4516 return 1;
4517 }
4518 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4519 int res;
4520 Py_ssize_t len;
4521 Py_ssize_t i;
4522 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4523 if (tmp == NULL) goto failed;
4524 if (!PyList_Check(tmp)) {
4525 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4526 goto failed;
4527 }
4528 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004529 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004530 if (handlers == NULL) goto failed;
4531 for (i = 0; i < len; i++) {
4532 excepthandler_ty value;
4533 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4534 if (res != 0) goto failed;
4535 asdl_seq_SET(handlers, i, value);
4536 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004537 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004538 } else {
4539 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4540 return 1;
4541 }
4542 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4543 int res;
4544 Py_ssize_t len;
4545 Py_ssize_t i;
4546 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4547 if (tmp == NULL) goto failed;
4548 if (!PyList_Check(tmp)) {
4549 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4550 goto failed;
4551 }
4552 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004553 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004554 if (orelse == NULL) goto failed;
4555 for (i = 0; i < len; i++) {
4556 stmt_ty value;
4557 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4558 if (res != 0) goto failed;
4559 asdl_seq_SET(orelse, i, value);
4560 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004561 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004562 } else {
4563 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4564 return 1;
4565 }
4566 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4567 int res;
4568 Py_ssize_t len;
4569 Py_ssize_t i;
4570 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4571 if (tmp == NULL) goto failed;
4572 if (!PyList_Check(tmp)) {
4573 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4574 goto failed;
4575 }
4576 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004577 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004578 if (finalbody == NULL) goto failed;
4579 for (i = 0; i < len; i++) {
4580 stmt_ty value;
4581 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4582 if (res != 0) goto failed;
4583 asdl_seq_SET(finalbody, i, value);
4584 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004586 } else {
4587 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4588 return 1;
4589 }
4590 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4591 arena);
4592 if (*out == NULL) goto failed;
4593 return 0;
4594 }
4595 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4596 if (isinstance == -1) {
4597 return 1;
4598 }
4599 if (isinstance) {
4600 expr_ty test;
4601 expr_ty msg;
4602
4603 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4604 int res;
4605 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4606 if (tmp == NULL) goto failed;
4607 res = obj2ast_expr(tmp, &test, arena);
4608 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004609 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004610 } else {
4611 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4612 return 1;
4613 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004614 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004615 int res;
4616 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4617 if (tmp == NULL) goto failed;
4618 res = obj2ast_expr(tmp, &msg, arena);
4619 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004620 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004621 } else {
4622 msg = NULL;
4623 }
4624 *out = Assert(test, msg, lineno, col_offset, arena);
4625 if (*out == NULL) goto failed;
4626 return 0;
4627 }
4628 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4629 if (isinstance == -1) {
4630 return 1;
4631 }
4632 if (isinstance) {
4633 asdl_seq* names;
4634
4635 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4636 int res;
4637 Py_ssize_t len;
4638 Py_ssize_t i;
4639 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4640 if (tmp == NULL) goto failed;
4641 if (!PyList_Check(tmp)) {
4642 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4643 goto failed;
4644 }
4645 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004646 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004647 if (names == NULL) goto failed;
4648 for (i = 0; i < len; i++) {
4649 alias_ty value;
4650 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4651 if (res != 0) goto failed;
4652 asdl_seq_SET(names, i, value);
4653 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004654 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004655 } else {
4656 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4657 return 1;
4658 }
4659 *out = Import(names, lineno, col_offset, arena);
4660 if (*out == NULL) goto failed;
4661 return 0;
4662 }
4663 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4664 if (isinstance == -1) {
4665 return 1;
4666 }
4667 if (isinstance) {
4668 identifier module;
4669 asdl_seq* names;
4670 int level;
4671
Victor Stinneree4b59c2013-07-27 00:01:35 +02004672 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004673 int res;
4674 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4675 if (tmp == NULL) goto failed;
4676 res = obj2ast_identifier(tmp, &module, arena);
4677 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004678 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004679 } else {
4680 module = NULL;
4681 }
4682 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4683 int res;
4684 Py_ssize_t len;
4685 Py_ssize_t i;
4686 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4687 if (tmp == NULL) goto failed;
4688 if (!PyList_Check(tmp)) {
4689 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4690 goto failed;
4691 }
4692 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004693 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004694 if (names == NULL) goto failed;
4695 for (i = 0; i < len; i++) {
4696 alias_ty value;
4697 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4698 if (res != 0) goto failed;
4699 asdl_seq_SET(names, i, value);
4700 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004701 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004702 } else {
4703 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4704 return 1;
4705 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004706 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004707 int res;
4708 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4709 if (tmp == NULL) goto failed;
4710 res = obj2ast_int(tmp, &level, arena);
4711 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004712 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004713 } else {
4714 level = 0;
4715 }
4716 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4717 if (*out == NULL) goto failed;
4718 return 0;
4719 }
4720 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4721 if (isinstance == -1) {
4722 return 1;
4723 }
4724 if (isinstance) {
4725 asdl_seq* names;
4726
4727 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4728 int res;
4729 Py_ssize_t len;
4730 Py_ssize_t i;
4731 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4732 if (tmp == NULL) goto failed;
4733 if (!PyList_Check(tmp)) {
4734 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4735 goto failed;
4736 }
4737 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004738 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004739 if (names == NULL) goto failed;
4740 for (i = 0; i < len; i++) {
4741 identifier value;
4742 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4743 if (res != 0) goto failed;
4744 asdl_seq_SET(names, i, value);
4745 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004747 } else {
4748 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4749 return 1;
4750 }
4751 *out = Global(names, lineno, col_offset, arena);
4752 if (*out == NULL) goto failed;
4753 return 0;
4754 }
4755 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4756 if (isinstance == -1) {
4757 return 1;
4758 }
4759 if (isinstance) {
4760 asdl_seq* names;
4761
4762 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4763 int res;
4764 Py_ssize_t len;
4765 Py_ssize_t i;
4766 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4767 if (tmp == NULL) goto failed;
4768 if (!PyList_Check(tmp)) {
4769 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4770 goto failed;
4771 }
4772 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004773 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004774 if (names == NULL) goto failed;
4775 for (i = 0; i < len; i++) {
4776 identifier value;
4777 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4778 if (res != 0) goto failed;
4779 asdl_seq_SET(names, i, value);
4780 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004781 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004782 } else {
4783 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4784 return 1;
4785 }
4786 *out = Nonlocal(names, lineno, col_offset, arena);
4787 if (*out == NULL) goto failed;
4788 return 0;
4789 }
4790 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4791 if (isinstance == -1) {
4792 return 1;
4793 }
4794 if (isinstance) {
4795 expr_ty value;
4796
4797 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4798 int res;
4799 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4800 if (tmp == NULL) goto failed;
4801 res = obj2ast_expr(tmp, &value, arena);
4802 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004803 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004804 } else {
4805 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4806 return 1;
4807 }
4808 *out = Expr(value, lineno, col_offset, arena);
4809 if (*out == NULL) goto failed;
4810 return 0;
4811 }
4812 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4813 if (isinstance == -1) {
4814 return 1;
4815 }
4816 if (isinstance) {
4817
4818 *out = Pass(lineno, col_offset, arena);
4819 if (*out == NULL) goto failed;
4820 return 0;
4821 }
4822 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4823 if (isinstance == -1) {
4824 return 1;
4825 }
4826 if (isinstance) {
4827
4828 *out = Break(lineno, col_offset, arena);
4829 if (*out == NULL) goto failed;
4830 return 0;
4831 }
4832 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4833 if (isinstance == -1) {
4834 return 1;
4835 }
4836 if (isinstance) {
4837
4838 *out = Continue(lineno, col_offset, arena);
4839 if (*out == NULL) goto failed;
4840 return 0;
4841 }
4842
4843 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4844 failed:
4845 Py_XDECREF(tmp);
4846 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004847}
4848
4849int
4850obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4851{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004852 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004853
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004854 PyObject *tmp = NULL;
4855 int lineno;
4856 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004857
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004858 if (obj == Py_None) {
4859 *out = NULL;
4860 return 0;
4861 }
4862 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4863 int res;
4864 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4865 if (tmp == NULL) goto failed;
4866 res = obj2ast_int(tmp, &lineno, arena);
4867 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004868 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004869 } else {
4870 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004871 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004872 }
4873 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4874 int res;
4875 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4876 if (tmp == NULL) goto failed;
4877 res = obj2ast_int(tmp, &col_offset, arena);
4878 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004880 } else {
4881 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4882 return 1;
4883 }
4884 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4885 if (isinstance == -1) {
4886 return 1;
4887 }
4888 if (isinstance) {
4889 boolop_ty op;
4890 asdl_seq* values;
4891
4892 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4893 int res;
4894 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4895 if (tmp == NULL) goto failed;
4896 res = obj2ast_boolop(tmp, &op, arena);
4897 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004898 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004899 } else {
4900 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4901 return 1;
4902 }
4903 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4904 int res;
4905 Py_ssize_t len;
4906 Py_ssize_t i;
4907 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4908 if (tmp == NULL) goto failed;
4909 if (!PyList_Check(tmp)) {
4910 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4911 goto failed;
4912 }
4913 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004914 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004915 if (values == NULL) goto failed;
4916 for (i = 0; i < len; i++) {
4917 expr_ty value;
4918 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4919 if (res != 0) goto failed;
4920 asdl_seq_SET(values, i, value);
4921 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004922 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004923 } else {
4924 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4925 return 1;
4926 }
4927 *out = BoolOp(op, values, lineno, col_offset, arena);
4928 if (*out == NULL) goto failed;
4929 return 0;
4930 }
4931 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4932 if (isinstance == -1) {
4933 return 1;
4934 }
4935 if (isinstance) {
4936 expr_ty left;
4937 operator_ty op;
4938 expr_ty right;
4939
4940 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4941 int res;
4942 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4943 if (tmp == NULL) goto failed;
4944 res = obj2ast_expr(tmp, &left, arena);
4945 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004947 } else {
4948 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4949 return 1;
4950 }
4951 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4952 int res;
4953 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4954 if (tmp == NULL) goto failed;
4955 res = obj2ast_operator(tmp, &op, arena);
4956 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004957 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004958 } else {
4959 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4960 return 1;
4961 }
4962 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4963 int res;
4964 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4965 if (tmp == NULL) goto failed;
4966 res = obj2ast_expr(tmp, &right, arena);
4967 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004968 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004969 } else {
4970 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4971 return 1;
4972 }
4973 *out = BinOp(left, op, right, lineno, col_offset, arena);
4974 if (*out == NULL) goto failed;
4975 return 0;
4976 }
4977 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4978 if (isinstance == -1) {
4979 return 1;
4980 }
4981 if (isinstance) {
4982 unaryop_ty op;
4983 expr_ty operand;
4984
4985 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4986 int res;
4987 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4988 if (tmp == NULL) goto failed;
4989 res = obj2ast_unaryop(tmp, &op, arena);
4990 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004991 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004992 } else {
4993 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4994 return 1;
4995 }
4996 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4997 int res;
4998 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4999 if (tmp == NULL) goto failed;
5000 res = obj2ast_expr(tmp, &operand, arena);
5001 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005002 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005003 } else {
5004 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5005 return 1;
5006 }
5007 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5008 if (*out == NULL) goto failed;
5009 return 0;
5010 }
5011 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5012 if (isinstance == -1) {
5013 return 1;
5014 }
5015 if (isinstance) {
5016 arguments_ty args;
5017 expr_ty body;
5018
5019 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5020 int res;
5021 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5022 if (tmp == NULL) goto failed;
5023 res = obj2ast_arguments(tmp, &args, arena);
5024 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005025 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005026 } else {
5027 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5028 return 1;
5029 }
5030 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5031 int res;
5032 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5033 if (tmp == NULL) goto failed;
5034 res = obj2ast_expr(tmp, &body, arena);
5035 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005036 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005037 } else {
5038 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5039 return 1;
5040 }
5041 *out = Lambda(args, body, lineno, col_offset, arena);
5042 if (*out == NULL) goto failed;
5043 return 0;
5044 }
5045 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5046 if (isinstance == -1) {
5047 return 1;
5048 }
5049 if (isinstance) {
5050 expr_ty test;
5051 expr_ty body;
5052 expr_ty orelse;
5053
5054 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5055 int res;
5056 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5057 if (tmp == NULL) goto failed;
5058 res = obj2ast_expr(tmp, &test, arena);
5059 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005060 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005061 } else {
5062 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5063 return 1;
5064 }
5065 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5066 int res;
5067 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5068 if (tmp == NULL) goto failed;
5069 res = obj2ast_expr(tmp, &body, arena);
5070 if (res != 0) goto failed;
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 \"body\" missing from IfExp");
5074 return 1;
5075 }
5076 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5077 int res;
5078 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5079 if (tmp == NULL) goto failed;
5080 res = obj2ast_expr(tmp, &orelse, arena);
5081 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005082 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005083 } else {
5084 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5085 return 1;
5086 }
5087 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5088 if (*out == NULL) goto failed;
5089 return 0;
5090 }
5091 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5092 if (isinstance == -1) {
5093 return 1;
5094 }
5095 if (isinstance) {
5096 asdl_seq* keys;
5097 asdl_seq* values;
5098
5099 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5100 int res;
5101 Py_ssize_t len;
5102 Py_ssize_t i;
5103 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5104 if (tmp == NULL) goto failed;
5105 if (!PyList_Check(tmp)) {
5106 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5107 goto failed;
5108 }
5109 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005110 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005111 if (keys == NULL) goto failed;
5112 for (i = 0; i < len; i++) {
5113 expr_ty value;
5114 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5115 if (res != 0) goto failed;
5116 asdl_seq_SET(keys, i, value);
5117 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005118 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005119 } else {
5120 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5121 return 1;
5122 }
5123 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5124 int res;
5125 Py_ssize_t len;
5126 Py_ssize_t i;
5127 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5128 if (tmp == NULL) goto failed;
5129 if (!PyList_Check(tmp)) {
5130 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5131 goto failed;
5132 }
5133 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005134 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005135 if (values == NULL) goto failed;
5136 for (i = 0; i < len; i++) {
5137 expr_ty value;
5138 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5139 if (res != 0) goto failed;
5140 asdl_seq_SET(values, i, value);
5141 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005142 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005143 } else {
5144 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5145 return 1;
5146 }
5147 *out = Dict(keys, values, lineno, col_offset, arena);
5148 if (*out == NULL) goto failed;
5149 return 0;
5150 }
5151 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5152 if (isinstance == -1) {
5153 return 1;
5154 }
5155 if (isinstance) {
5156 asdl_seq* elts;
5157
5158 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5159 int res;
5160 Py_ssize_t len;
5161 Py_ssize_t i;
5162 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5163 if (tmp == NULL) goto failed;
5164 if (!PyList_Check(tmp)) {
5165 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5166 goto failed;
5167 }
5168 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005169 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005170 if (elts == NULL) goto failed;
5171 for (i = 0; i < len; i++) {
5172 expr_ty value;
5173 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5174 if (res != 0) goto failed;
5175 asdl_seq_SET(elts, i, value);
5176 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005177 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005178 } else {
5179 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5180 return 1;
5181 }
5182 *out = Set(elts, lineno, col_offset, arena);
5183 if (*out == NULL) goto failed;
5184 return 0;
5185 }
5186 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5187 if (isinstance == -1) {
5188 return 1;
5189 }
5190 if (isinstance) {
5191 expr_ty elt;
5192 asdl_seq* generators;
5193
5194 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5195 int res;
5196 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5197 if (tmp == NULL) goto failed;
5198 res = obj2ast_expr(tmp, &elt, arena);
5199 if (res != 0) goto failed;
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 \"elt\" missing from ListComp");
5203 return 1;
5204 }
5205 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5206 int res;
5207 Py_ssize_t len;
5208 Py_ssize_t i;
5209 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5210 if (tmp == NULL) goto failed;
5211 if (!PyList_Check(tmp)) {
5212 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5213 goto failed;
5214 }
5215 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005216 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005217 if (generators == NULL) goto failed;
5218 for (i = 0; i < len; i++) {
5219 comprehension_ty value;
5220 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5221 if (res != 0) goto failed;
5222 asdl_seq_SET(generators, i, value);
5223 }
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 \"generators\" missing from ListComp");
5227 return 1;
5228 }
5229 *out = ListComp(elt, generators, lineno, col_offset, arena);
5230 if (*out == NULL) goto failed;
5231 return 0;
5232 }
5233 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5234 if (isinstance == -1) {
5235 return 1;
5236 }
5237 if (isinstance) {
5238 expr_ty elt;
5239 asdl_seq* generators;
5240
5241 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5242 int res;
5243 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5244 if (tmp == NULL) goto failed;
5245 res = obj2ast_expr(tmp, &elt, arena);
5246 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005248 } else {
5249 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5250 return 1;
5251 }
5252 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5253 int res;
5254 Py_ssize_t len;
5255 Py_ssize_t i;
5256 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5257 if (tmp == NULL) goto failed;
5258 if (!PyList_Check(tmp)) {
5259 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5260 goto failed;
5261 }
5262 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005263 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005264 if (generators == NULL) goto failed;
5265 for (i = 0; i < len; i++) {
5266 comprehension_ty value;
5267 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5268 if (res != 0) goto failed;
5269 asdl_seq_SET(generators, i, value);
5270 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005271 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005272 } else {
5273 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5274 return 1;
5275 }
5276 *out = SetComp(elt, generators, lineno, col_offset, arena);
5277 if (*out == NULL) goto failed;
5278 return 0;
5279 }
5280 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5281 if (isinstance == -1) {
5282 return 1;
5283 }
5284 if (isinstance) {
5285 expr_ty key;
5286 expr_ty value;
5287 asdl_seq* generators;
5288
5289 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5290 int res;
5291 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5292 if (tmp == NULL) goto failed;
5293 res = obj2ast_expr(tmp, &key, arena);
5294 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005295 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005296 } else {
5297 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5298 return 1;
5299 }
5300 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5301 int res;
5302 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5303 if (tmp == NULL) goto failed;
5304 res = obj2ast_expr(tmp, &value, arena);
5305 if (res != 0) goto failed;
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 \"value\" missing from DictComp");
5309 return 1;
5310 }
5311 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5312 int res;
5313 Py_ssize_t len;
5314 Py_ssize_t i;
5315 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5316 if (tmp == NULL) goto failed;
5317 if (!PyList_Check(tmp)) {
5318 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5319 goto failed;
5320 }
5321 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005322 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005323 if (generators == NULL) goto failed;
5324 for (i = 0; i < len; i++) {
5325 comprehension_ty value;
5326 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5327 if (res != 0) goto failed;
5328 asdl_seq_SET(generators, i, value);
5329 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005330 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005331 } else {
5332 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5333 return 1;
5334 }
5335 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5336 if (*out == NULL) goto failed;
5337 return 0;
5338 }
5339 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5340 if (isinstance == -1) {
5341 return 1;
5342 }
5343 if (isinstance) {
5344 expr_ty elt;
5345 asdl_seq* generators;
5346
5347 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5348 int res;
5349 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5350 if (tmp == NULL) goto failed;
5351 res = obj2ast_expr(tmp, &elt, arena);
5352 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005353 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005354 } else {
5355 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5356 return 1;
5357 }
5358 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5359 int res;
5360 Py_ssize_t len;
5361 Py_ssize_t i;
5362 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5363 if (tmp == NULL) goto failed;
5364 if (!PyList_Check(tmp)) {
5365 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5366 goto failed;
5367 }
5368 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005369 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005370 if (generators == NULL) goto failed;
5371 for (i = 0; i < len; i++) {
5372 comprehension_ty value;
5373 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5374 if (res != 0) goto failed;
5375 asdl_seq_SET(generators, i, value);
5376 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005377 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005378 } else {
5379 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5380 return 1;
5381 }
5382 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5383 if (*out == NULL) goto failed;
5384 return 0;
5385 }
5386 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5387 if (isinstance == -1) {
5388 return 1;
5389 }
5390 if (isinstance) {
5391 expr_ty value;
5392
Victor Stinneree4b59c2013-07-27 00:01:35 +02005393 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005394 int res;
5395 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5396 if (tmp == NULL) goto failed;
5397 res = obj2ast_expr(tmp, &value, arena);
5398 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005399 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005400 } else {
5401 value = NULL;
5402 }
5403 *out = Yield(value, lineno, col_offset, arena);
5404 if (*out == NULL) goto failed;
5405 return 0;
5406 }
5407 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5408 if (isinstance == -1) {
5409 return 1;
5410 }
5411 if (isinstance) {
5412 expr_ty value;
5413
5414 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5415 int res;
5416 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5417 if (tmp == NULL) goto failed;
5418 res = obj2ast_expr(tmp, &value, arena);
5419 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005420 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005421 } else {
5422 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5423 return 1;
5424 }
5425 *out = YieldFrom(value, lineno, col_offset, arena);
5426 if (*out == NULL) goto failed;
5427 return 0;
5428 }
5429 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5430 if (isinstance == -1) {
5431 return 1;
5432 }
5433 if (isinstance) {
5434 expr_ty left;
5435 asdl_int_seq* ops;
5436 asdl_seq* comparators;
5437
5438 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5439 int res;
5440 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5441 if (tmp == NULL) goto failed;
5442 res = obj2ast_expr(tmp, &left, arena);
5443 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005444 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005445 } else {
5446 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5447 return 1;
5448 }
5449 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5450 int res;
5451 Py_ssize_t len;
5452 Py_ssize_t i;
5453 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5454 if (tmp == NULL) goto failed;
5455 if (!PyList_Check(tmp)) {
5456 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5457 goto failed;
5458 }
5459 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005460 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005461 if (ops == NULL) goto failed;
5462 for (i = 0; i < len; i++) {
5463 cmpop_ty value;
5464 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5465 if (res != 0) goto failed;
5466 asdl_seq_SET(ops, i, value);
5467 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005468 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005469 } else {
5470 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5471 return 1;
5472 }
5473 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5474 int res;
5475 Py_ssize_t len;
5476 Py_ssize_t i;
5477 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5478 if (tmp == NULL) goto failed;
5479 if (!PyList_Check(tmp)) {
5480 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5481 goto failed;
5482 }
5483 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005484 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005485 if (comparators == NULL) goto failed;
5486 for (i = 0; i < len; i++) {
5487 expr_ty value;
5488 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5489 if (res != 0) goto failed;
5490 asdl_seq_SET(comparators, i, value);
5491 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005492 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005493 } else {
5494 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5495 return 1;
5496 }
5497 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5498 if (*out == NULL) goto failed;
5499 return 0;
5500 }
5501 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5502 if (isinstance == -1) {
5503 return 1;
5504 }
5505 if (isinstance) {
5506 expr_ty func;
5507 asdl_seq* args;
5508 asdl_seq* keywords;
5509 expr_ty starargs;
5510 expr_ty kwargs;
5511
5512 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5513 int res;
5514 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5515 if (tmp == NULL) goto failed;
5516 res = obj2ast_expr(tmp, &func, arena);
5517 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005518 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005519 } else {
5520 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5521 return 1;
5522 }
5523 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5524 int res;
5525 Py_ssize_t len;
5526 Py_ssize_t i;
5527 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5528 if (tmp == NULL) goto failed;
5529 if (!PyList_Check(tmp)) {
5530 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5531 goto failed;
5532 }
5533 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005534 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005535 if (args == NULL) goto failed;
5536 for (i = 0; i < len; i++) {
5537 expr_ty value;
5538 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5539 if (res != 0) goto failed;
5540 asdl_seq_SET(args, i, value);
5541 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005542 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005543 } else {
5544 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5545 return 1;
5546 }
5547 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5548 int res;
5549 Py_ssize_t len;
5550 Py_ssize_t i;
5551 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5552 if (tmp == NULL) goto failed;
5553 if (!PyList_Check(tmp)) {
5554 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5555 goto failed;
5556 }
5557 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005558 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005559 if (keywords == NULL) goto failed;
5560 for (i = 0; i < len; i++) {
5561 keyword_ty value;
5562 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5563 if (res != 0) goto failed;
5564 asdl_seq_SET(keywords, i, value);
5565 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005566 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005567 } else {
5568 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5569 return 1;
5570 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005571 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005572 int res;
5573 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5574 if (tmp == NULL) goto failed;
5575 res = obj2ast_expr(tmp, &starargs, arena);
5576 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005577 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005578 } else {
5579 starargs = NULL;
5580 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005581 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005582 int res;
5583 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5584 if (tmp == NULL) goto failed;
5585 res = obj2ast_expr(tmp, &kwargs, arena);
5586 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005587 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005588 } else {
5589 kwargs = NULL;
5590 }
5591 *out = Call(func, args, keywords, starargs, kwargs, lineno, col_offset,
5592 arena);
5593 if (*out == NULL) goto failed;
5594 return 0;
5595 }
5596 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5597 if (isinstance == -1) {
5598 return 1;
5599 }
5600 if (isinstance) {
5601 object n;
5602
5603 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5604 int res;
5605 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5606 if (tmp == NULL) goto failed;
5607 res = obj2ast_object(tmp, &n, arena);
5608 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005609 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005610 } else {
5611 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5612 return 1;
5613 }
5614 *out = Num(n, lineno, col_offset, arena);
5615 if (*out == NULL) goto failed;
5616 return 0;
5617 }
5618 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5619 if (isinstance == -1) {
5620 return 1;
5621 }
5622 if (isinstance) {
5623 string s;
5624
5625 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5626 int res;
5627 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5628 if (tmp == NULL) goto failed;
5629 res = obj2ast_string(tmp, &s, arena);
5630 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005631 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005632 } else {
5633 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5634 return 1;
5635 }
5636 *out = Str(s, lineno, col_offset, arena);
5637 if (*out == NULL) goto failed;
5638 return 0;
5639 }
5640 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5641 if (isinstance == -1) {
5642 return 1;
5643 }
5644 if (isinstance) {
5645 bytes s;
5646
5647 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5648 int res;
5649 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5650 if (tmp == NULL) goto failed;
5651 res = obj2ast_bytes(tmp, &s, arena);
5652 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005653 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005654 } else {
5655 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5656 return 1;
5657 }
5658 *out = Bytes(s, lineno, col_offset, arena);
5659 if (*out == NULL) goto failed;
5660 return 0;
5661 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005662 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
5663 if (isinstance == -1) {
5664 return 1;
5665 }
5666 if (isinstance) {
5667 singleton value;
5668
5669 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5670 int res;
5671 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5672 if (tmp == NULL) goto failed;
5673 res = obj2ast_singleton(tmp, &value, arena);
5674 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02005675 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02005676 } else {
5677 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
5678 return 1;
5679 }
5680 *out = NameConstant(value, lineno, col_offset, arena);
5681 if (*out == NULL) goto failed;
5682 return 0;
5683 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005684 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5685 if (isinstance == -1) {
5686 return 1;
5687 }
5688 if (isinstance) {
5689
5690 *out = Ellipsis(lineno, col_offset, arena);
5691 if (*out == NULL) goto failed;
5692 return 0;
5693 }
5694 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5695 if (isinstance == -1) {
5696 return 1;
5697 }
5698 if (isinstance) {
5699 expr_ty value;
5700 identifier attr;
5701 expr_context_ty ctx;
5702
5703 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5704 int res;
5705 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5706 if (tmp == NULL) goto failed;
5707 res = obj2ast_expr(tmp, &value, arena);
5708 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005710 } else {
5711 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5712 return 1;
5713 }
5714 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5715 int res;
5716 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5717 if (tmp == NULL) goto failed;
5718 res = obj2ast_identifier(tmp, &attr, arena);
5719 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005720 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005721 } else {
5722 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5723 return 1;
5724 }
5725 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5726 int res;
5727 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5728 if (tmp == NULL) goto failed;
5729 res = obj2ast_expr_context(tmp, &ctx, arena);
5730 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005731 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005732 } else {
5733 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5734 return 1;
5735 }
5736 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5737 if (*out == NULL) goto failed;
5738 return 0;
5739 }
5740 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5741 if (isinstance == -1) {
5742 return 1;
5743 }
5744 if (isinstance) {
5745 expr_ty value;
5746 slice_ty slice;
5747 expr_context_ty ctx;
5748
5749 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5750 int res;
5751 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5752 if (tmp == NULL) goto failed;
5753 res = obj2ast_expr(tmp, &value, arena);
5754 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005755 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005756 } else {
5757 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5758 return 1;
5759 }
5760 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5761 int res;
5762 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5763 if (tmp == NULL) goto failed;
5764 res = obj2ast_slice(tmp, &slice, arena);
5765 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005767 } else {
5768 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5769 return 1;
5770 }
5771 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5772 int res;
5773 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5774 if (tmp == NULL) goto failed;
5775 res = obj2ast_expr_context(tmp, &ctx, arena);
5776 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005777 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005778 } else {
5779 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5780 return 1;
5781 }
5782 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5783 if (*out == NULL) goto failed;
5784 return 0;
5785 }
5786 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5787 if (isinstance == -1) {
5788 return 1;
5789 }
5790 if (isinstance) {
5791 expr_ty value;
5792 expr_context_ty ctx;
5793
5794 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5795 int res;
5796 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5797 if (tmp == NULL) goto failed;
5798 res = obj2ast_expr(tmp, &value, arena);
5799 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005800 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005801 } else {
5802 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5803 return 1;
5804 }
5805 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5806 int res;
5807 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5808 if (tmp == NULL) goto failed;
5809 res = obj2ast_expr_context(tmp, &ctx, arena);
5810 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005811 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005812 } else {
5813 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5814 return 1;
5815 }
5816 *out = Starred(value, ctx, lineno, col_offset, arena);
5817 if (*out == NULL) goto failed;
5818 return 0;
5819 }
5820 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5821 if (isinstance == -1) {
5822 return 1;
5823 }
5824 if (isinstance) {
5825 identifier id;
5826 expr_context_ty ctx;
5827
5828 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5829 int res;
5830 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5831 if (tmp == NULL) goto failed;
5832 res = obj2ast_identifier(tmp, &id, arena);
5833 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005834 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005835 } else {
5836 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5837 return 1;
5838 }
5839 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5840 int res;
5841 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5842 if (tmp == NULL) goto failed;
5843 res = obj2ast_expr_context(tmp, &ctx, arena);
5844 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005845 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005846 } else {
5847 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5848 return 1;
5849 }
5850 *out = Name(id, ctx, lineno, col_offset, arena);
5851 if (*out == NULL) goto failed;
5852 return 0;
5853 }
5854 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5855 if (isinstance == -1) {
5856 return 1;
5857 }
5858 if (isinstance) {
5859 asdl_seq* elts;
5860 expr_context_ty ctx;
5861
5862 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5863 int res;
5864 Py_ssize_t len;
5865 Py_ssize_t i;
5866 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5867 if (tmp == NULL) goto failed;
5868 if (!PyList_Check(tmp)) {
5869 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5870 goto failed;
5871 }
5872 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005873 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005874 if (elts == NULL) goto failed;
5875 for (i = 0; i < len; i++) {
5876 expr_ty value;
5877 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5878 if (res != 0) goto failed;
5879 asdl_seq_SET(elts, i, value);
5880 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005881 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005882 } else {
5883 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5884 return 1;
5885 }
5886 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5887 int res;
5888 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5889 if (tmp == NULL) goto failed;
5890 res = obj2ast_expr_context(tmp, &ctx, arena);
5891 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005892 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005893 } else {
5894 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5895 return 1;
5896 }
5897 *out = List(elts, ctx, lineno, col_offset, arena);
5898 if (*out == NULL) goto failed;
5899 return 0;
5900 }
5901 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5902 if (isinstance == -1) {
5903 return 1;
5904 }
5905 if (isinstance) {
5906 asdl_seq* elts;
5907 expr_context_ty ctx;
5908
5909 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5910 int res;
5911 Py_ssize_t len;
5912 Py_ssize_t i;
5913 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5914 if (tmp == NULL) goto failed;
5915 if (!PyList_Check(tmp)) {
5916 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5917 goto failed;
5918 }
5919 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005920 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005921 if (elts == NULL) goto failed;
5922 for (i = 0; i < len; i++) {
5923 expr_ty value;
5924 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5925 if (res != 0) goto failed;
5926 asdl_seq_SET(elts, i, value);
5927 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005928 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005929 } else {
5930 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5931 return 1;
5932 }
5933 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5934 int res;
5935 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5936 if (tmp == NULL) goto failed;
5937 res = obj2ast_expr_context(tmp, &ctx, arena);
5938 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005939 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005940 } else {
5941 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5942 return 1;
5943 }
5944 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5945 if (*out == NULL) goto failed;
5946 return 0;
5947 }
5948
5949 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5950 failed:
5951 Py_XDECREF(tmp);
5952 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005953}
5954
5955int
5956obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5957{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005958 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005959
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005960 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5961 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005962 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005963 }
5964 if (isinstance) {
5965 *out = Load;
5966 return 0;
5967 }
5968 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5969 if (isinstance == -1) {
5970 return 1;
5971 }
5972 if (isinstance) {
5973 *out = Store;
5974 return 0;
5975 }
5976 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5977 if (isinstance == -1) {
5978 return 1;
5979 }
5980 if (isinstance) {
5981 *out = Del;
5982 return 0;
5983 }
5984 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5985 if (isinstance == -1) {
5986 return 1;
5987 }
5988 if (isinstance) {
5989 *out = AugLoad;
5990 return 0;
5991 }
5992 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5993 if (isinstance == -1) {
5994 return 1;
5995 }
5996 if (isinstance) {
5997 *out = AugStore;
5998 return 0;
5999 }
6000 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6001 if (isinstance == -1) {
6002 return 1;
6003 }
6004 if (isinstance) {
6005 *out = Param;
6006 return 0;
6007 }
6008
6009 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6010 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006011}
6012
6013int
6014obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6015{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006016 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006017
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006018 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006019
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006020 if (obj == Py_None) {
6021 *out = NULL;
6022 return 0;
6023 }
6024 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6025 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006026 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006027 }
6028 if (isinstance) {
6029 expr_ty lower;
6030 expr_ty upper;
6031 expr_ty step;
6032
Victor Stinneree4b59c2013-07-27 00:01:35 +02006033 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006034 int res;
6035 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6036 if (tmp == NULL) goto failed;
6037 res = obj2ast_expr(tmp, &lower, arena);
6038 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006039 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006040 } else {
6041 lower = NULL;
6042 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006043 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006044 int res;
6045 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6046 if (tmp == NULL) goto failed;
6047 res = obj2ast_expr(tmp, &upper, arena);
6048 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006049 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006050 } else {
6051 upper = NULL;
6052 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006053 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006054 int res;
6055 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6056 if (tmp == NULL) goto failed;
6057 res = obj2ast_expr(tmp, &step, arena);
6058 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006059 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006060 } else {
6061 step = NULL;
6062 }
6063 *out = Slice(lower, upper, step, arena);
6064 if (*out == NULL) goto failed;
6065 return 0;
6066 }
6067 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6068 if (isinstance == -1) {
6069 return 1;
6070 }
6071 if (isinstance) {
6072 asdl_seq* dims;
6073
6074 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6075 int res;
6076 Py_ssize_t len;
6077 Py_ssize_t i;
6078 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6079 if (tmp == NULL) goto failed;
6080 if (!PyList_Check(tmp)) {
6081 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6082 goto failed;
6083 }
6084 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006085 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006086 if (dims == NULL) goto failed;
6087 for (i = 0; i < len; i++) {
6088 slice_ty value;
6089 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6090 if (res != 0) goto failed;
6091 asdl_seq_SET(dims, i, value);
6092 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006093 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006094 } else {
6095 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6096 return 1;
6097 }
6098 *out = ExtSlice(dims, arena);
6099 if (*out == NULL) goto failed;
6100 return 0;
6101 }
6102 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6103 if (isinstance == -1) {
6104 return 1;
6105 }
6106 if (isinstance) {
6107 expr_ty value;
6108
6109 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6110 int res;
6111 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6112 if (tmp == NULL) goto failed;
6113 res = obj2ast_expr(tmp, &value, arena);
6114 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006115 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006116 } else {
6117 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6118 return 1;
6119 }
6120 *out = Index(value, arena);
6121 if (*out == NULL) goto failed;
6122 return 0;
6123 }
6124
6125 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6126 failed:
6127 Py_XDECREF(tmp);
6128 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006129}
6130
6131int
6132obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6133{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006134 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006135
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006136 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6137 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006138 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006139 }
6140 if (isinstance) {
6141 *out = And;
6142 return 0;
6143 }
6144 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6145 if (isinstance == -1) {
6146 return 1;
6147 }
6148 if (isinstance) {
6149 *out = Or;
6150 return 0;
6151 }
6152
6153 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6154 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006155}
6156
6157int
6158obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6159{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006160 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006161
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006162 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6163 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006164 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006165 }
6166 if (isinstance) {
6167 *out = Add;
6168 return 0;
6169 }
6170 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
6175 *out = Sub;
6176 return 0;
6177 }
6178 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6179 if (isinstance == -1) {
6180 return 1;
6181 }
6182 if (isinstance) {
6183 *out = Mult;
6184 return 0;
6185 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006186 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
6191 *out = MatMult;
6192 return 0;
6193 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006194 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6195 if (isinstance == -1) {
6196 return 1;
6197 }
6198 if (isinstance) {
6199 *out = Div;
6200 return 0;
6201 }
6202 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6203 if (isinstance == -1) {
6204 return 1;
6205 }
6206 if (isinstance) {
6207 *out = Mod;
6208 return 0;
6209 }
6210 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6211 if (isinstance == -1) {
6212 return 1;
6213 }
6214 if (isinstance) {
6215 *out = Pow;
6216 return 0;
6217 }
6218 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6219 if (isinstance == -1) {
6220 return 1;
6221 }
6222 if (isinstance) {
6223 *out = LShift;
6224 return 0;
6225 }
6226 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6227 if (isinstance == -1) {
6228 return 1;
6229 }
6230 if (isinstance) {
6231 *out = RShift;
6232 return 0;
6233 }
6234 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6235 if (isinstance == -1) {
6236 return 1;
6237 }
6238 if (isinstance) {
6239 *out = BitOr;
6240 return 0;
6241 }
6242 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6243 if (isinstance == -1) {
6244 return 1;
6245 }
6246 if (isinstance) {
6247 *out = BitXor;
6248 return 0;
6249 }
6250 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6251 if (isinstance == -1) {
6252 return 1;
6253 }
6254 if (isinstance) {
6255 *out = BitAnd;
6256 return 0;
6257 }
6258 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6259 if (isinstance == -1) {
6260 return 1;
6261 }
6262 if (isinstance) {
6263 *out = FloorDiv;
6264 return 0;
6265 }
6266
6267 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6268 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006269}
6270
6271int
6272obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6273{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006274 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006275
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006276 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6277 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006278 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006279 }
6280 if (isinstance) {
6281 *out = Invert;
6282 return 0;
6283 }
6284 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6285 if (isinstance == -1) {
6286 return 1;
6287 }
6288 if (isinstance) {
6289 *out = Not;
6290 return 0;
6291 }
6292 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6293 if (isinstance == -1) {
6294 return 1;
6295 }
6296 if (isinstance) {
6297 *out = UAdd;
6298 return 0;
6299 }
6300 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6301 if (isinstance == -1) {
6302 return 1;
6303 }
6304 if (isinstance) {
6305 *out = USub;
6306 return 0;
6307 }
6308
6309 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6310 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006311}
6312
6313int
6314obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6315{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006316 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006317
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006318 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6319 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006320 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006321 }
6322 if (isinstance) {
6323 *out = Eq;
6324 return 0;
6325 }
6326 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6327 if (isinstance == -1) {
6328 return 1;
6329 }
6330 if (isinstance) {
6331 *out = NotEq;
6332 return 0;
6333 }
6334 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6335 if (isinstance == -1) {
6336 return 1;
6337 }
6338 if (isinstance) {
6339 *out = Lt;
6340 return 0;
6341 }
6342 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6343 if (isinstance == -1) {
6344 return 1;
6345 }
6346 if (isinstance) {
6347 *out = LtE;
6348 return 0;
6349 }
6350 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6351 if (isinstance == -1) {
6352 return 1;
6353 }
6354 if (isinstance) {
6355 *out = Gt;
6356 return 0;
6357 }
6358 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6359 if (isinstance == -1) {
6360 return 1;
6361 }
6362 if (isinstance) {
6363 *out = GtE;
6364 return 0;
6365 }
6366 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6367 if (isinstance == -1) {
6368 return 1;
6369 }
6370 if (isinstance) {
6371 *out = Is;
6372 return 0;
6373 }
6374 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6375 if (isinstance == -1) {
6376 return 1;
6377 }
6378 if (isinstance) {
6379 *out = IsNot;
6380 return 0;
6381 }
6382 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6383 if (isinstance == -1) {
6384 return 1;
6385 }
6386 if (isinstance) {
6387 *out = In;
6388 return 0;
6389 }
6390 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6391 if (isinstance == -1) {
6392 return 1;
6393 }
6394 if (isinstance) {
6395 *out = NotIn;
6396 return 0;
6397 }
6398
6399 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6400 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006401}
6402
6403int
6404obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6405{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006406 PyObject* tmp = NULL;
6407 expr_ty target;
6408 expr_ty iter;
6409 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006410
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006411 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6412 int res;
6413 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6414 if (tmp == NULL) goto failed;
6415 res = obj2ast_expr(tmp, &target, arena);
6416 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006417 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006418 } else {
6419 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006420 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006421 }
6422 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6423 int res;
6424 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6425 if (tmp == NULL) goto failed;
6426 res = obj2ast_expr(tmp, &iter, arena);
6427 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006428 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006429 } else {
6430 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6431 return 1;
6432 }
6433 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6434 int res;
6435 Py_ssize_t len;
6436 Py_ssize_t i;
6437 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6438 if (tmp == NULL) goto failed;
6439 if (!PyList_Check(tmp)) {
6440 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6441 goto failed;
6442 }
6443 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006444 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006445 if (ifs == NULL) goto failed;
6446 for (i = 0; i < len; i++) {
6447 expr_ty value;
6448 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6449 if (res != 0) goto failed;
6450 asdl_seq_SET(ifs, i, value);
6451 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006452 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006453 } else {
6454 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6455 return 1;
6456 }
6457 *out = comprehension(target, iter, ifs, arena);
6458 return 0;
6459failed:
6460 Py_XDECREF(tmp);
6461 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006462}
6463
6464int
6465obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006467 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006468
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006469 PyObject *tmp = NULL;
6470 int lineno;
6471 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006472
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006473 if (obj == Py_None) {
6474 *out = NULL;
6475 return 0;
6476 }
6477 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6478 int res;
6479 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6480 if (tmp == NULL) goto failed;
6481 res = obj2ast_int(tmp, &lineno, arena);
6482 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006483 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006484 } else {
6485 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006486 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006487 }
6488 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6489 int res;
6490 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6491 if (tmp == NULL) goto failed;
6492 res = obj2ast_int(tmp, &col_offset, arena);
6493 if (res != 0) goto failed;
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 \"col_offset\" missing from excepthandler");
6497 return 1;
6498 }
6499 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6500 if (isinstance == -1) {
6501 return 1;
6502 }
6503 if (isinstance) {
6504 expr_ty type;
6505 identifier name;
6506 asdl_seq* body;
6507
Victor Stinneree4b59c2013-07-27 00:01:35 +02006508 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006509 int res;
6510 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6511 if (tmp == NULL) goto failed;
6512 res = obj2ast_expr(tmp, &type, arena);
6513 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006514 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006515 } else {
6516 type = NULL;
6517 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006518 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006519 int res;
6520 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6521 if (tmp == NULL) goto failed;
6522 res = obj2ast_identifier(tmp, &name, arena);
6523 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006524 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006525 } else {
6526 name = NULL;
6527 }
6528 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6529 int res;
6530 Py_ssize_t len;
6531 Py_ssize_t i;
6532 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6533 if (tmp == NULL) goto failed;
6534 if (!PyList_Check(tmp)) {
6535 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6536 goto failed;
6537 }
6538 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006539 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006540 if (body == NULL) goto failed;
6541 for (i = 0; i < len; i++) {
6542 stmt_ty value;
6543 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6544 if (res != 0) goto failed;
6545 asdl_seq_SET(body, i, value);
6546 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006547 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006548 } else {
6549 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6550 return 1;
6551 }
6552 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6553 if (*out == NULL) goto failed;
6554 return 0;
6555 }
6556
6557 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6558 failed:
6559 Py_XDECREF(tmp);
6560 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006561}
6562
6563int
6564obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6565{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006566 PyObject* tmp = NULL;
6567 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006568 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006569 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006570 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006571 arg_ty kwarg;
6572 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006573
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006574 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6575 int res;
6576 Py_ssize_t len;
6577 Py_ssize_t i;
6578 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6579 if (tmp == NULL) goto failed;
6580 if (!PyList_Check(tmp)) {
6581 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6582 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006583 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006584 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006585 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006586 if (args == NULL) goto failed;
6587 for (i = 0; i < len; i++) {
6588 arg_ty value;
6589 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6590 if (res != 0) goto failed;
6591 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006592 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006593 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006594 } else {
6595 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006596 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006597 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006598 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006599 int res;
6600 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6601 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006602 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006603 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006604 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006605 } else {
6606 vararg = NULL;
6607 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006608 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6609 int res;
6610 Py_ssize_t len;
6611 Py_ssize_t i;
6612 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6613 if (tmp == NULL) goto failed;
6614 if (!PyList_Check(tmp)) {
6615 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6616 goto failed;
6617 }
6618 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006619 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006620 if (kwonlyargs == NULL) goto failed;
6621 for (i = 0; i < len; i++) {
6622 arg_ty value;
6623 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6624 if (res != 0) goto failed;
6625 asdl_seq_SET(kwonlyargs, i, value);
6626 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006627 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006628 } else {
6629 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6630 return 1;
6631 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006632 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6633 int res;
6634 Py_ssize_t len;
6635 Py_ssize_t i;
6636 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6637 if (tmp == NULL) goto failed;
6638 if (!PyList_Check(tmp)) {
6639 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6640 goto failed;
6641 }
6642 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006643 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006644 if (kw_defaults == NULL) goto failed;
6645 for (i = 0; i < len; i++) {
6646 expr_ty value;
6647 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6648 if (res != 0) goto failed;
6649 asdl_seq_SET(kw_defaults, i, value);
6650 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006651 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006652 } else {
6653 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6654 return 1;
6655 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006656 if (exists_not_none(obj, &PyId_kwarg)) {
6657 int res;
6658 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6659 if (tmp == NULL) goto failed;
6660 res = obj2ast_arg(tmp, &kwarg, arena);
6661 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006662 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006663 } else {
6664 kwarg = NULL;
6665 }
6666 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6667 int res;
6668 Py_ssize_t len;
6669 Py_ssize_t i;
6670 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6671 if (tmp == NULL) goto failed;
6672 if (!PyList_Check(tmp)) {
6673 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6674 goto failed;
6675 }
6676 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006677 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006678 if (defaults == NULL) goto failed;
6679 for (i = 0; i < len; i++) {
6680 expr_ty value;
6681 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6682 if (res != 0) goto failed;
6683 asdl_seq_SET(defaults, i, value);
6684 }
Victor Stinnerb3189902013-07-27 00:04:42 +02006685 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006686 } else {
6687 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6688 return 1;
6689 }
6690 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
6691 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006692 return 0;
6693failed:
6694 Py_XDECREF(tmp);
6695 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006696}
6697
6698int
6699obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6700{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006701 PyObject* tmp = NULL;
6702 identifier arg;
6703 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006704
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006705 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6706 int res;
6707 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6708 if (tmp == NULL) goto failed;
6709 res = obj2ast_identifier(tmp, &arg, arena);
6710 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006711 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006712 } else {
6713 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006714 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006715 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006716 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006717 int res;
6718 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6719 if (tmp == NULL) goto failed;
6720 res = obj2ast_expr(tmp, &annotation, arena);
6721 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006722 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006723 } else {
6724 annotation = NULL;
6725 }
6726 *out = arg(arg, annotation, arena);
6727 return 0;
6728failed:
6729 Py_XDECREF(tmp);
6730 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006731}
6732
6733int
6734obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6735{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006736 PyObject* tmp = NULL;
6737 identifier arg;
6738 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006739
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006740 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6741 int res;
6742 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6743 if (tmp == NULL) goto failed;
6744 res = obj2ast_identifier(tmp, &arg, arena);
6745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006747 } else {
6748 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006749 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006750 }
6751 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6752 int res;
6753 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6754 if (tmp == NULL) goto failed;
6755 res = obj2ast_expr(tmp, &value, arena);
6756 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006757 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006758 } else {
6759 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6760 return 1;
6761 }
6762 *out = keyword(arg, value, arena);
6763 return 0;
6764failed:
6765 Py_XDECREF(tmp);
6766 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006767}
6768
6769int
6770obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6771{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006772 PyObject* tmp = NULL;
6773 identifier name;
6774 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006775
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006776 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6777 int res;
6778 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6779 if (tmp == NULL) goto failed;
6780 res = obj2ast_identifier(tmp, &name, arena);
6781 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006782 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006783 } else {
6784 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006785 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006786 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006787 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006788 int res;
6789 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6790 if (tmp == NULL) goto failed;
6791 res = obj2ast_identifier(tmp, &asname, arena);
6792 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006793 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006794 } else {
6795 asname = NULL;
6796 }
6797 *out = alias(name, asname, arena);
6798 return 0;
6799failed:
6800 Py_XDECREF(tmp);
6801 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006802}
6803
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006804int
6805obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6806{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006807 PyObject* tmp = NULL;
6808 expr_ty context_expr;
6809 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006810
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006811 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6812 int res;
6813 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6814 if (tmp == NULL) goto failed;
6815 res = obj2ast_expr(tmp, &context_expr, arena);
6816 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006817 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006818 } else {
6819 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006820 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006821 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006822 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006823 int res;
6824 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6825 if (tmp == NULL) goto failed;
6826 res = obj2ast_expr(tmp, &optional_vars, arena);
6827 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006828 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006829 } else {
6830 optional_vars = NULL;
6831 }
6832 *out = withitem(context_expr, optional_vars, arena);
6833 return 0;
6834failed:
6835 Py_XDECREF(tmp);
6836 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006837}
6838
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006839
Martin v. Löwis1a214512008-06-11 05:26:20 +00006840static struct PyModuleDef _astmodule = {
6841 PyModuleDef_HEAD_INIT, "_ast"
6842};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006843PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006844PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006845{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006846 PyObject *m, *d;
6847 if (!init_types()) return NULL;
6848 m = PyModule_Create(&_astmodule);
6849 if (!m) return NULL;
6850 d = PyModule_GetDict(m);
6851 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006852 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006853 return NULL;
6854 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6855 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6856 NULL;
6857 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6858 0) return NULL;
6859 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6860 return NULL;
6861 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6862 NULL;
6863 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6864 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6865 0) return NULL;
6866 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6867 return NULL;
6868 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6873 NULL;
6874 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6875 return NULL;
6876 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6877 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6878 NULL;
6879 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6880 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6881 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6882 NULL;
6883 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6884 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6885 NULL;
6886 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6887 NULL;
6888 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6889 return NULL;
6890 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6891 NULL;
6892 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6893 return NULL;
6894 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6895 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6896 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6897 NULL;
6898 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6899 return NULL;
6900 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6901 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6904 NULL;
6905 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6908 NULL;
6909 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6910 NULL;
6911 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6912 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6913 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6914 return NULL;
6915 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6916 NULL;
6917 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6918 return NULL;
6919 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6920 0) return NULL;
6921 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6924 return NULL;
6925 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6926 NULL;
6927 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6928 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6929 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6930 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6931 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006932 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
6933 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006934 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6935 return NULL;
6936 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6937 return NULL;
6938 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6939 return NULL;
6940 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6941 NULL;
6942 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6943 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6944 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6945 NULL;
6946 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6947 0) return NULL;
6948 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6949 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6950 NULL;
6951 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
6952 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
6953 NULL;
6954 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6955 return NULL;
6956 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6957 NULL;
6958 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6959 NULL;
6960 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6961 NULL;
6962 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6963 return NULL;
6964 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6965 NULL;
6966 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
6967 NULL;
6968 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
6969 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
6970 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6971 return NULL;
6972 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
6973 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
6974 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04006975 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
6976 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006977 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
6978 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
6979 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
6980 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
6981 NULL;
6982 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
6983 NULL;
6984 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6985 NULL;
6986 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
6987 NULL;
6988 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
6989 NULL;
6990 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6991 return NULL;
6992 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
6993 NULL;
6994 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
6995 NULL;
6996 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
6997 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
6998 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
6999 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7000 NULL;
7001 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7002 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7003 NULL;
7004 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7005 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7006 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7007 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7008 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7009 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7010 NULL;
7011 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7012 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7013 NULL;
7014 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7015 < 0) return NULL;
7016 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7017 < 0) return NULL;
7018 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7019 < 0) return NULL;
7020 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7021 return NULL;
7022 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7023 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7024 NULL;
7025 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7026 NULL;
7027 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7028 return NULL;
7029 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007030}
7031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007032
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007033PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007034{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007035 if (!init_types())
7036 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007037 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007038}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007039
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007040/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7041mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007042{
7043 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007044 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007045 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007046 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007047
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007048 req_type[0] = (PyObject*)Module_type;
7049 req_type[1] = (PyObject*)Expression_type;
7050 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007051
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007052 assert(0 <= mode && mode <= 2);
7053
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007054 if (!init_types())
7055 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007056
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007057 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7058 if (isinstance == -1)
7059 return NULL;
7060 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007061 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7062 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007063 return NULL;
7064 }
7065 if (obj2ast_mod(ast, &res, arena) != 0)
7066 return NULL;
7067 else
7068 return res;
7069}
7070
7071int PyAST_Check(PyObject* obj)
7072{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007073 if (!init_types())
7074 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007075 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007076}
7077
Martin v. Löwis5b222132007-06-10 09:51:05 +00007078