blob: 44fdafc50aefb9c8de3b93224fbfed6ad3bdf36b [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,
352*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
353*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
354*FloorDiv_singleton;
355static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static PyTypeObject *Add_type;
357static PyTypeObject *Sub_type;
358static PyTypeObject *Mult_type;
359static PyTypeObject *Div_type;
360static PyTypeObject *Mod_type;
361static PyTypeObject *Pow_type;
362static PyTypeObject *LShift_type;
363static PyTypeObject *RShift_type;
364static PyTypeObject *BitOr_type;
365static PyTypeObject *BitXor_type;
366static PyTypeObject *BitAnd_type;
367static PyTypeObject *FloorDiv_type;
368static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
370*USub_singleton;
371static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000372static PyTypeObject *Invert_type;
373static PyTypeObject *Not_type;
374static PyTypeObject *UAdd_type;
375static PyTypeObject *USub_type;
376static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000377static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
378*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
379*NotIn_singleton;
380static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000381static PyTypeObject *Eq_type;
382static PyTypeObject *NotEq_type;
383static PyTypeObject *Lt_type;
384static PyTypeObject *LtE_type;
385static PyTypeObject *Gt_type;
386static PyTypeObject *GtE_type;
387static PyTypeObject *Is_type;
388static PyTypeObject *IsNot_type;
389static PyTypeObject *In_type;
390static PyTypeObject *NotIn_type;
391static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000392static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200393_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000394static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200395 "target",
396 "iter",
397 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000398};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000399static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000400static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200401 "lineno",
402 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000403};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000404static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000405static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200406_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000407static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200408 "type",
409 "name",
410 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000411};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000412static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000413static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200414_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200415_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200416_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700417_Py_IDENTIFIER(kwarg);
418_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000419static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200420 "args",
421 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200422 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200423 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200424 "kwarg",
425 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000426};
Neal Norwitzc1505362006-12-28 06:47:50 +0000427static PyTypeObject *arg_type;
428static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700429static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200430 "lineno",
431 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700432};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200433_Py_IDENTIFIER(arg);
434_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000435static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200436 "arg",
437 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000438};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000439static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000440static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000441static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200442 "arg",
443 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000444};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000445static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000446static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200447_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000448static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200449 "name",
450 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000451};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500452static PyTypeObject *withitem_type;
453static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200454_Py_IDENTIFIER(context_expr);
455_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500456static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200457 "context_expr",
458 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500459};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000460
461
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700462typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100463 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700464 PyObject *dict;
465} AST_object;
466
Benjamin Peterson1767e022012-03-14 21:50:29 -0500467static void
468ast_dealloc(AST_object *self)
469{
470 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200471 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500472}
473
Neal Norwitz207c9f32008-03-31 04:42:11 +0000474static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700475ast_traverse(AST_object *self, visitproc visit, void *arg)
476{
477 Py_VISIT(self->dict);
478 return 0;
479}
480
481static void
482ast_clear(AST_object *self)
483{
484 Py_CLEAR(self->dict);
485}
486
487static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000488ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
489{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200490 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000491 Py_ssize_t i, numfields = 0;
492 int res = -1;
493 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200494 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000495 if (!fields)
496 PyErr_Clear();
497 if (fields) {
498 numfields = PySequence_Size(fields);
499 if (numfields == -1)
500 goto cleanup;
501 }
502 res = 0; /* if no error occurs, this stays 0 to the end */
503 if (PyTuple_GET_SIZE(args) > 0) {
504 if (numfields != PyTuple_GET_SIZE(args)) {
505 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000506 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000507 Py_TYPE(self)->tp_name,
508 numfields == 0 ? "" : "either 0 or ",
509 numfields, numfields == 1 ? "" : "s");
510 res = -1;
511 goto cleanup;
512 }
513 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
514 /* cannot be reached when fields is NULL */
515 PyObject *name = PySequence_GetItem(fields, i);
516 if (!name) {
517 res = -1;
518 goto cleanup;
519 }
520 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
521 Py_DECREF(name);
522 if (res < 0)
523 goto cleanup;
524 }
525 }
526 if (kw) {
527 i = 0; /* needed by PyDict_Next */
528 while (PyDict_Next(kw, &i, &key, &value)) {
529 res = PyObject_SetAttr(self, key, value);
530 if (res < 0)
531 goto cleanup;
532 }
533 }
534 cleanup:
535 Py_XDECREF(fields);
536 return res;
537}
538
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000539/* Pickling support */
540static PyObject *
541ast_type_reduce(PyObject *self, PyObject *unused)
542{
543 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200544 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200545 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000546 if (dict == NULL) {
547 if (PyErr_ExceptionMatches(PyExc_AttributeError))
548 PyErr_Clear();
549 else
550 return NULL;
551 }
552 if (dict) {
553 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
554 Py_DECREF(dict);
555 return res;
556 }
557 return Py_BuildValue("O()", Py_TYPE(self));
558}
559
560static PyMethodDef ast_type_methods[] = {
561 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
562 {NULL}
563};
564
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700565static PyGetSetDef ast_type_getsets[] = {
566 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
567 {NULL}
568};
569
Neal Norwitz207c9f32008-03-31 04:42:11 +0000570static PyTypeObject AST_type = {
571 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000572 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700573 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000574 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500575 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000576 0, /* tp_print */
577 0, /* tp_getattr */
578 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000579 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000580 0, /* tp_repr */
581 0, /* tp_as_number */
582 0, /* tp_as_sequence */
583 0, /* tp_as_mapping */
584 0, /* tp_hash */
585 0, /* tp_call */
586 0, /* tp_str */
587 PyObject_GenericGetAttr, /* tp_getattro */
588 PyObject_GenericSetAttr, /* tp_setattro */
589 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700590 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000591 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700592 (traverseproc)ast_traverse, /* tp_traverse */
593 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000594 0, /* tp_richcompare */
595 0, /* tp_weaklistoffset */
596 0, /* tp_iter */
597 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000598 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000599 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700600 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000601 0, /* tp_base */
602 0, /* tp_dict */
603 0, /* tp_descr_get */
604 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700605 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000606 (initproc)ast_type_init, /* tp_init */
607 PyType_GenericAlloc, /* tp_alloc */
608 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700609 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000610};
611
612
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000613static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
614{
615 PyObject *fnames, *result;
616 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000617 fnames = PyTuple_New(num_fields);
618 if (!fnames) return NULL;
619 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000620 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000621 if (!field) {
622 Py_DECREF(fnames);
623 return NULL;
624 }
625 PyTuple_SET_ITEM(fnames, i, field);
626 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000627 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000629 Py_DECREF(fnames);
630 return (PyTypeObject*)result;
631}
632
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000633static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
634{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000635 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200636 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000637 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000638 if (!l)
639 return 0;
640 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000641 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000642 if (!s) {
643 Py_DECREF(l);
644 return 0;
645 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000646 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200648 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000649 Py_DECREF(l);
650 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651}
652
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000653/* Conversion AST -> Python */
654
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000655static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
656{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700657 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 PyObject *result = PyList_New(n);
659 PyObject *value;
660 if (!result)
661 return NULL;
662 for (i = 0; i < n; i++) {
663 value = func(asdl_seq_GET(seq, i));
664 if (!value) {
665 Py_DECREF(result);
666 return NULL;
667 }
668 PyList_SET_ITEM(result, i, value);
669 }
670 return result;
671}
672
673static PyObject* ast2obj_object(void *o)
674{
675 if (!o)
676 o = Py_None;
677 Py_INCREF((PyObject*)o);
678 return (PyObject*)o;
679}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500680#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681#define ast2obj_identifier ast2obj_object
682#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500683#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000685static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686{
Christian Heimes217cfd12007-12-02 14:31:20 +0000687 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688}
689
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000690/* Conversion Python -> AST */
691
Benjamin Peterson442f2092012-12-06 17:41:04 -0500692static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
693{
694 if (obj != Py_None && obj != Py_True && obj != Py_False) {
695 PyErr_SetString(PyExc_ValueError,
696 "AST singleton must be True, False, or None");
697 return 1;
698 }
699 *out = obj;
700 return 0;
701}
702
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000703static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
704{
705 if (obj == Py_None)
706 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200707 if (obj) {
708 if (PyArena_AddPyObject(arena, obj) < 0) {
709 *out = NULL;
710 return -1;
711 }
712 Py_INCREF(obj);
713 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000714 *out = obj;
715 return 0;
716}
717
Benjamin Peterson180e6352011-07-22 11:09:07 -0500718static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500719{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500720 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
721 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500722 return 1;
723 }
724 return obj2ast_object(obj, out, arena);
725}
726
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500727static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
728{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400729 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500730 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
731 return 1;
732 }
733 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500734}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000735
Benjamin Petersone2498412011-08-09 16:08:39 -0500736static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
737{
738 if (!PyBytes_CheckExact(obj)) {
739 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
740 return 1;
741 }
742 return obj2ast_object(obj, out, arena);
743}
744
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000745static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
746{
747 int i;
748 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100749 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000750 return 1;
751 }
752
753 i = (int)PyLong_AsLong(obj);
754 if (i == -1 && PyErr_Occurred())
755 return 1;
756 *out = i;
757 return 0;
758}
759
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000760static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000761{
762 PyObject *empty_tuple, *d;
763 if (PyType_Ready(&AST_type) < 0)
764 return -1;
765 d = AST_type.tp_dict;
766 empty_tuple = PyTuple_New(0);
767 if (!empty_tuple ||
768 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
769 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
770 Py_XDECREF(empty_tuple);
771 return -1;
772 }
773 Py_DECREF(empty_tuple);
774 return 0;
775}
776
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700777static int exists_not_none(PyObject *obj, _Py_Identifier *id)
778{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700779 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700780 PyObject *attr = _PyObject_GetAttrId(obj, id);
781 if (!attr) {
782 PyErr_Clear();
783 return 0;
784 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700785 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700786 Py_DECREF(attr);
787 return !isnone;
788}
789
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000790
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791static int init_types(void)
792{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200793 static int initialized;
794 if (initialized) return 1;
795 if (add_ast_fields() < 0) return 0;
796 mod_type = make_type("mod", &AST_type, NULL, 0);
797 if (!mod_type) return 0;
798 if (!add_attributes(mod_type, NULL, 0)) return 0;
799 Module_type = make_type("Module", mod_type, Module_fields, 1);
800 if (!Module_type) return 0;
801 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
802 1);
803 if (!Interactive_type) return 0;
804 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
805 if (!Expression_type) return 0;
806 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
807 if (!Suite_type) return 0;
808 stmt_type = make_type("stmt", &AST_type, NULL, 0);
809 if (!stmt_type) return 0;
810 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
811 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
812 5);
813 if (!FunctionDef_type) return 0;
814 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
815 if (!ClassDef_type) return 0;
816 Return_type = make_type("Return", stmt_type, Return_fields, 1);
817 if (!Return_type) return 0;
818 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
819 if (!Delete_type) return 0;
820 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
821 if (!Assign_type) return 0;
822 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
823 if (!AugAssign_type) return 0;
824 For_type = make_type("For", stmt_type, For_fields, 4);
825 if (!For_type) return 0;
826 While_type = make_type("While", stmt_type, While_fields, 3);
827 if (!While_type) return 0;
828 If_type = make_type("If", stmt_type, If_fields, 3);
829 if (!If_type) return 0;
830 With_type = make_type("With", stmt_type, With_fields, 2);
831 if (!With_type) return 0;
832 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
833 if (!Raise_type) return 0;
834 Try_type = make_type("Try", stmt_type, Try_fields, 4);
835 if (!Try_type) return 0;
836 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
837 if (!Assert_type) return 0;
838 Import_type = make_type("Import", stmt_type, Import_fields, 1);
839 if (!Import_type) return 0;
840 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
841 if (!ImportFrom_type) return 0;
842 Global_type = make_type("Global", stmt_type, Global_fields, 1);
843 if (!Global_type) return 0;
844 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
845 if (!Nonlocal_type) return 0;
846 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
847 if (!Expr_type) return 0;
848 Pass_type = make_type("Pass", stmt_type, NULL, 0);
849 if (!Pass_type) return 0;
850 Break_type = make_type("Break", stmt_type, NULL, 0);
851 if (!Break_type) return 0;
852 Continue_type = make_type("Continue", stmt_type, NULL, 0);
853 if (!Continue_type) return 0;
854 expr_type = make_type("expr", &AST_type, NULL, 0);
855 if (!expr_type) return 0;
856 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
857 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
858 if (!BoolOp_type) return 0;
859 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
860 if (!BinOp_type) return 0;
861 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
862 if (!UnaryOp_type) return 0;
863 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
864 if (!Lambda_type) return 0;
865 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
866 if (!IfExp_type) return 0;
867 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
868 if (!Dict_type) return 0;
869 Set_type = make_type("Set", expr_type, Set_fields, 1);
870 if (!Set_type) return 0;
871 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
872 if (!ListComp_type) return 0;
873 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
874 if (!SetComp_type) return 0;
875 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
876 if (!DictComp_type) return 0;
877 GeneratorExp_type = make_type("GeneratorExp", expr_type,
878 GeneratorExp_fields, 2);
879 if (!GeneratorExp_type) return 0;
880 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
881 if (!Yield_type) return 0;
882 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
883 if (!YieldFrom_type) return 0;
884 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
885 if (!Compare_type) return 0;
886 Call_type = make_type("Call", expr_type, Call_fields, 5);
887 if (!Call_type) return 0;
888 Num_type = make_type("Num", expr_type, Num_fields, 1);
889 if (!Num_type) return 0;
890 Str_type = make_type("Str", expr_type, Str_fields, 1);
891 if (!Str_type) return 0;
892 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
893 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200894 NameConstant_type = make_type("NameConstant", expr_type,
895 NameConstant_fields, 1);
896 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200897 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
898 if (!Ellipsis_type) return 0;
899 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
900 if (!Attribute_type) return 0;
901 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
902 if (!Subscript_type) return 0;
903 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
904 if (!Starred_type) return 0;
905 Name_type = make_type("Name", expr_type, Name_fields, 2);
906 if (!Name_type) return 0;
907 List_type = make_type("List", expr_type, List_fields, 2);
908 if (!List_type) return 0;
909 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
910 if (!Tuple_type) return 0;
911 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
912 if (!expr_context_type) return 0;
913 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
914 Load_type = make_type("Load", expr_context_type, NULL, 0);
915 if (!Load_type) return 0;
916 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
917 if (!Load_singleton) return 0;
918 Store_type = make_type("Store", expr_context_type, NULL, 0);
919 if (!Store_type) return 0;
920 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
921 if (!Store_singleton) return 0;
922 Del_type = make_type("Del", expr_context_type, NULL, 0);
923 if (!Del_type) return 0;
924 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
925 if (!Del_singleton) return 0;
926 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
927 if (!AugLoad_type) return 0;
928 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
929 if (!AugLoad_singleton) return 0;
930 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
931 if (!AugStore_type) return 0;
932 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
933 if (!AugStore_singleton) return 0;
934 Param_type = make_type("Param", expr_context_type, NULL, 0);
935 if (!Param_type) return 0;
936 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
937 if (!Param_singleton) return 0;
938 slice_type = make_type("slice", &AST_type, NULL, 0);
939 if (!slice_type) return 0;
940 if (!add_attributes(slice_type, NULL, 0)) return 0;
941 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
942 if (!Slice_type) return 0;
943 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
944 if (!ExtSlice_type) return 0;
945 Index_type = make_type("Index", slice_type, Index_fields, 1);
946 if (!Index_type) return 0;
947 boolop_type = make_type("boolop", &AST_type, NULL, 0);
948 if (!boolop_type) return 0;
949 if (!add_attributes(boolop_type, NULL, 0)) return 0;
950 And_type = make_type("And", boolop_type, NULL, 0);
951 if (!And_type) return 0;
952 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
953 if (!And_singleton) return 0;
954 Or_type = make_type("Or", boolop_type, NULL, 0);
955 if (!Or_type) return 0;
956 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
957 if (!Or_singleton) return 0;
958 operator_type = make_type("operator", &AST_type, NULL, 0);
959 if (!operator_type) return 0;
960 if (!add_attributes(operator_type, NULL, 0)) return 0;
961 Add_type = make_type("Add", operator_type, NULL, 0);
962 if (!Add_type) return 0;
963 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
964 if (!Add_singleton) return 0;
965 Sub_type = make_type("Sub", operator_type, NULL, 0);
966 if (!Sub_type) return 0;
967 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
968 if (!Sub_singleton) return 0;
969 Mult_type = make_type("Mult", operator_type, NULL, 0);
970 if (!Mult_type) return 0;
971 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
972 if (!Mult_singleton) return 0;
973 Div_type = make_type("Div", operator_type, NULL, 0);
974 if (!Div_type) return 0;
975 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
976 if (!Div_singleton) return 0;
977 Mod_type = make_type("Mod", operator_type, NULL, 0);
978 if (!Mod_type) return 0;
979 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
980 if (!Mod_singleton) return 0;
981 Pow_type = make_type("Pow", operator_type, NULL, 0);
982 if (!Pow_type) return 0;
983 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
984 if (!Pow_singleton) return 0;
985 LShift_type = make_type("LShift", operator_type, NULL, 0);
986 if (!LShift_type) return 0;
987 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
988 if (!LShift_singleton) return 0;
989 RShift_type = make_type("RShift", operator_type, NULL, 0);
990 if (!RShift_type) return 0;
991 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
992 if (!RShift_singleton) return 0;
993 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
994 if (!BitOr_type) return 0;
995 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
996 if (!BitOr_singleton) return 0;
997 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
998 if (!BitXor_type) return 0;
999 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1000 if (!BitXor_singleton) return 0;
1001 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1002 if (!BitAnd_type) return 0;
1003 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1004 if (!BitAnd_singleton) return 0;
1005 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1006 if (!FloorDiv_type) return 0;
1007 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1008 if (!FloorDiv_singleton) return 0;
1009 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1010 if (!unaryop_type) return 0;
1011 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1012 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1013 if (!Invert_type) return 0;
1014 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1015 if (!Invert_singleton) return 0;
1016 Not_type = make_type("Not", unaryop_type, NULL, 0);
1017 if (!Not_type) return 0;
1018 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1019 if (!Not_singleton) return 0;
1020 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1021 if (!UAdd_type) return 0;
1022 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1023 if (!UAdd_singleton) return 0;
1024 USub_type = make_type("USub", unaryop_type, NULL, 0);
1025 if (!USub_type) return 0;
1026 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1027 if (!USub_singleton) return 0;
1028 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1029 if (!cmpop_type) return 0;
1030 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1031 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1032 if (!Eq_type) return 0;
1033 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1034 if (!Eq_singleton) return 0;
1035 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1036 if (!NotEq_type) return 0;
1037 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1038 if (!NotEq_singleton) return 0;
1039 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1040 if (!Lt_type) return 0;
1041 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1042 if (!Lt_singleton) return 0;
1043 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1044 if (!LtE_type) return 0;
1045 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1046 if (!LtE_singleton) return 0;
1047 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1048 if (!Gt_type) return 0;
1049 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1050 if (!Gt_singleton) return 0;
1051 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1052 if (!GtE_type) return 0;
1053 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1054 if (!GtE_singleton) return 0;
1055 Is_type = make_type("Is", cmpop_type, NULL, 0);
1056 if (!Is_type) return 0;
1057 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1058 if (!Is_singleton) return 0;
1059 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1060 if (!IsNot_type) return 0;
1061 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1062 if (!IsNot_singleton) return 0;
1063 In_type = make_type("In", cmpop_type, NULL, 0);
1064 if (!In_type) return 0;
1065 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1066 if (!In_singleton) return 0;
1067 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1068 if (!NotIn_type) return 0;
1069 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1070 if (!NotIn_singleton) return 0;
1071 comprehension_type = make_type("comprehension", &AST_type,
1072 comprehension_fields, 3);
1073 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001074 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001075 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1076 if (!excepthandler_type) return 0;
1077 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1078 return 0;
1079 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1080 ExceptHandler_fields, 3);
1081 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001082 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001083 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001084 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001085 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1086 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001087 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001088 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1089 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001090 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001091 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1092 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001093 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001094 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1095 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001096 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001097 initialized = 1;
1098 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001099}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001100
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001101static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1102static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1103static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1104static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1105 arena);
1106static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1107static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1108static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1109static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1110static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1111static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1112 arena);
1113static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1114 arena);
1115static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1116static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1117static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1118static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001119static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001124 mod_ty p;
1125 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1126 if (!p)
1127 return NULL;
1128 p->kind = Module_kind;
1129 p->v.Module.body = body;
1130 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131}
1132
1133mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001134Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001136 mod_ty p;
1137 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1138 if (!p)
1139 return NULL;
1140 p->kind = Interactive_kind;
1141 p->v.Interactive.body = body;
1142 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143}
1144
1145mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001146Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001148 mod_ty p;
1149 if (!body) {
1150 PyErr_SetString(PyExc_ValueError,
1151 "field body is required for Expression");
1152 return NULL;
1153 }
1154 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1155 if (!p)
1156 return NULL;
1157 p->kind = Expression_kind;
1158 p->v.Expression.body = body;
1159 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160}
1161
1162mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001163Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001165 mod_ty p;
1166 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1167 if (!p)
1168 return NULL;
1169 p->kind = Suite_kind;
1170 p->v.Suite.body = body;
1171 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172}
1173
1174stmt_ty
1175FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001176 decorator_list, expr_ty returns, int lineno, int col_offset,
1177 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001179 stmt_ty p;
1180 if (!name) {
1181 PyErr_SetString(PyExc_ValueError,
1182 "field name is required for FunctionDef");
1183 return NULL;
1184 }
1185 if (!args) {
1186 PyErr_SetString(PyExc_ValueError,
1187 "field args is required for FunctionDef");
1188 return NULL;
1189 }
1190 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1191 if (!p)
1192 return NULL;
1193 p->kind = FunctionDef_kind;
1194 p->v.FunctionDef.name = name;
1195 p->v.FunctionDef.args = args;
1196 p->v.FunctionDef.body = body;
1197 p->v.FunctionDef.decorator_list = decorator_list;
1198 p->v.FunctionDef.returns = returns;
1199 p->lineno = lineno;
1200 p->col_offset = col_offset;
1201 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202}
1203
1204stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001205ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001206 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1207 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001209 stmt_ty p;
1210 if (!name) {
1211 PyErr_SetString(PyExc_ValueError,
1212 "field name is required for ClassDef");
1213 return NULL;
1214 }
1215 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1216 if (!p)
1217 return NULL;
1218 p->kind = ClassDef_kind;
1219 p->v.ClassDef.name = name;
1220 p->v.ClassDef.bases = bases;
1221 p->v.ClassDef.keywords = keywords;
1222 p->v.ClassDef.starargs = starargs;
1223 p->v.ClassDef.kwargs = kwargs;
1224 p->v.ClassDef.body = body;
1225 p->v.ClassDef.decorator_list = decorator_list;
1226 p->lineno = lineno;
1227 p->col_offset = col_offset;
1228 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229}
1230
1231stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001232Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001234 stmt_ty p;
1235 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1236 if (!p)
1237 return NULL;
1238 p->kind = Return_kind;
1239 p->v.Return.value = value;
1240 p->lineno = lineno;
1241 p->col_offset = col_offset;
1242 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243}
1244
1245stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001248 stmt_ty p;
1249 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1250 if (!p)
1251 return NULL;
1252 p->kind = Delete_kind;
1253 p->v.Delete.targets = targets;
1254 p->lineno = lineno;
1255 p->col_offset = col_offset;
1256 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001260Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1261 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001263 stmt_ty p;
1264 if (!value) {
1265 PyErr_SetString(PyExc_ValueError,
1266 "field value is required for Assign");
1267 return NULL;
1268 }
1269 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1270 if (!p)
1271 return NULL;
1272 p->kind = Assign_kind;
1273 p->v.Assign.targets = targets;
1274 p->v.Assign.value = value;
1275 p->lineno = lineno;
1276 p->col_offset = col_offset;
1277 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278}
1279
1280stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1282 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001284 stmt_ty p;
1285 if (!target) {
1286 PyErr_SetString(PyExc_ValueError,
1287 "field target is required for AugAssign");
1288 return NULL;
1289 }
1290 if (!op) {
1291 PyErr_SetString(PyExc_ValueError,
1292 "field op is required for AugAssign");
1293 return NULL;
1294 }
1295 if (!value) {
1296 PyErr_SetString(PyExc_ValueError,
1297 "field value is required for AugAssign");
1298 return NULL;
1299 }
1300 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1301 if (!p)
1302 return NULL;
1303 p->kind = AugAssign_kind;
1304 p->v.AugAssign.target = target;
1305 p->v.AugAssign.op = op;
1306 p->v.AugAssign.value = value;
1307 p->lineno = lineno;
1308 p->col_offset = col_offset;
1309 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310}
1311
1312stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001314 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001316 stmt_ty p;
1317 if (!target) {
1318 PyErr_SetString(PyExc_ValueError,
1319 "field target is required for For");
1320 return NULL;
1321 }
1322 if (!iter) {
1323 PyErr_SetString(PyExc_ValueError,
1324 "field iter is required for For");
1325 return NULL;
1326 }
1327 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1328 if (!p)
1329 return NULL;
1330 p->kind = For_kind;
1331 p->v.For.target = target;
1332 p->v.For.iter = iter;
1333 p->v.For.body = body;
1334 p->v.For.orelse = orelse;
1335 p->lineno = lineno;
1336 p->col_offset = col_offset;
1337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1342 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001344 stmt_ty p;
1345 if (!test) {
1346 PyErr_SetString(PyExc_ValueError,
1347 "field test is required for While");
1348 return NULL;
1349 }
1350 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1351 if (!p)
1352 return NULL;
1353 p->kind = While_kind;
1354 p->v.While.test = test;
1355 p->v.While.body = body;
1356 p->v.While.orelse = orelse;
1357 p->lineno = lineno;
1358 p->col_offset = col_offset;
1359 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360}
1361
1362stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1364 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001366 stmt_ty p;
1367 if (!test) {
1368 PyErr_SetString(PyExc_ValueError,
1369 "field test is required for If");
1370 return NULL;
1371 }
1372 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1373 if (!p)
1374 return NULL;
1375 p->kind = If_kind;
1376 p->v.If.test = test;
1377 p->v.If.body = body;
1378 p->v.If.orelse = orelse;
1379 p->lineno = lineno;
1380 p->col_offset = col_offset;
1381 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382}
1383
1384stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001385With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1386 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001387{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001388 stmt_ty p;
1389 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1390 if (!p)
1391 return NULL;
1392 p->kind = With_kind;
1393 p->v.With.items = items;
1394 p->v.With.body = body;
1395 p->lineno = lineno;
1396 p->col_offset = col_offset;
1397 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001398}
1399
1400stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001401Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001403 stmt_ty p;
1404 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1405 if (!p)
1406 return NULL;
1407 p->kind = Raise_kind;
1408 p->v.Raise.exc = exc;
1409 p->v.Raise.cause = cause;
1410 p->lineno = lineno;
1411 p->col_offset = col_offset;
1412 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413}
1414
1415stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001416Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1417 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001419 stmt_ty p;
1420 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1421 if (!p)
1422 return NULL;
1423 p->kind = Try_kind;
1424 p->v.Try.body = body;
1425 p->v.Try.handlers = handlers;
1426 p->v.Try.orelse = orelse;
1427 p->v.Try.finalbody = finalbody;
1428 p->lineno = lineno;
1429 p->col_offset = col_offset;
1430 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431}
1432
1433stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001434Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001436 stmt_ty p;
1437 if (!test) {
1438 PyErr_SetString(PyExc_ValueError,
1439 "field test is required for Assert");
1440 return NULL;
1441 }
1442 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1443 if (!p)
1444 return NULL;
1445 p->kind = Assert_kind;
1446 p->v.Assert.test = test;
1447 p->v.Assert.msg = msg;
1448 p->lineno = lineno;
1449 p->col_offset = col_offset;
1450 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451}
1452
1453stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001454Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001456 stmt_ty p;
1457 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1458 if (!p)
1459 return NULL;
1460 p->kind = Import_kind;
1461 p->v.Import.names = names;
1462 p->lineno = lineno;
1463 p->col_offset = col_offset;
1464 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465}
1466
1467stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1469 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001471 stmt_ty p;
1472 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1473 if (!p)
1474 return NULL;
1475 p->kind = ImportFrom_kind;
1476 p->v.ImportFrom.module = module;
1477 p->v.ImportFrom.names = names;
1478 p->v.ImportFrom.level = level;
1479 p->lineno = lineno;
1480 p->col_offset = col_offset;
1481 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482}
1483
1484stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001487 stmt_ty p;
1488 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1489 if (!p)
1490 return NULL;
1491 p->kind = Global_kind;
1492 p->v.Global.names = names;
1493 p->lineno = lineno;
1494 p->col_offset = col_offset;
1495 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496}
1497
1498stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001499Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001501 stmt_ty p;
1502 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1503 if (!p)
1504 return NULL;
1505 p->kind = Nonlocal_kind;
1506 p->v.Nonlocal.names = names;
1507 p->lineno = lineno;
1508 p->col_offset = col_offset;
1509 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001510}
1511
1512stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001513Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001515 stmt_ty p;
1516 if (!value) {
1517 PyErr_SetString(PyExc_ValueError,
1518 "field value is required for Expr");
1519 return NULL;
1520 }
1521 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1522 if (!p)
1523 return NULL;
1524 p->kind = Expr_kind;
1525 p->v.Expr.value = value;
1526 p->lineno = lineno;
1527 p->col_offset = col_offset;
1528 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529}
1530
1531stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001532Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001534 stmt_ty p;
1535 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1536 if (!p)
1537 return NULL;
1538 p->kind = Pass_kind;
1539 p->lineno = lineno;
1540 p->col_offset = col_offset;
1541 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542}
1543
1544stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001545Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001547 stmt_ty p;
1548 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1549 if (!p)
1550 return NULL;
1551 p->kind = Break_kind;
1552 p->lineno = lineno;
1553 p->col_offset = col_offset;
1554 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001560 stmt_ty p;
1561 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1562 if (!p)
1563 return NULL;
1564 p->kind = Continue_kind;
1565 p->lineno = lineno;
1566 p->col_offset = col_offset;
1567 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568}
1569
1570expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001571BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1572 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001574 expr_ty p;
1575 if (!op) {
1576 PyErr_SetString(PyExc_ValueError,
1577 "field op is required for BoolOp");
1578 return NULL;
1579 }
1580 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1581 if (!p)
1582 return NULL;
1583 p->kind = BoolOp_kind;
1584 p->v.BoolOp.op = op;
1585 p->v.BoolOp.values = values;
1586 p->lineno = lineno;
1587 p->col_offset = col_offset;
1588 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
1591expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001592BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1593 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001595 expr_ty p;
1596 if (!left) {
1597 PyErr_SetString(PyExc_ValueError,
1598 "field left is required for BinOp");
1599 return NULL;
1600 }
1601 if (!op) {
1602 PyErr_SetString(PyExc_ValueError,
1603 "field op is required for BinOp");
1604 return NULL;
1605 }
1606 if (!right) {
1607 PyErr_SetString(PyExc_ValueError,
1608 "field right is required for BinOp");
1609 return NULL;
1610 }
1611 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1612 if (!p)
1613 return NULL;
1614 p->kind = BinOp_kind;
1615 p->v.BinOp.left = left;
1616 p->v.BinOp.op = op;
1617 p->v.BinOp.right = right;
1618 p->lineno = lineno;
1619 p->col_offset = col_offset;
1620 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621}
1622
1623expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001624UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1625 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001627 expr_ty p;
1628 if (!op) {
1629 PyErr_SetString(PyExc_ValueError,
1630 "field op is required for UnaryOp");
1631 return NULL;
1632 }
1633 if (!operand) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field operand is required for UnaryOp");
1636 return NULL;
1637 }
1638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = UnaryOp_kind;
1642 p->v.UnaryOp.op = op;
1643 p->v.UnaryOp.operand = operand;
1644 p->lineno = lineno;
1645 p->col_offset = col_offset;
1646 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647}
1648
1649expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001650Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1651 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001653 expr_ty p;
1654 if (!args) {
1655 PyErr_SetString(PyExc_ValueError,
1656 "field args is required for Lambda");
1657 return NULL;
1658 }
1659 if (!body) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "field body is required for Lambda");
1662 return NULL;
1663 }
1664 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1665 if (!p)
1666 return NULL;
1667 p->kind = Lambda_kind;
1668 p->v.Lambda.args = args;
1669 p->v.Lambda.body = body;
1670 p->lineno = lineno;
1671 p->col_offset = col_offset;
1672 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673}
1674
1675expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001676IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1677 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001678{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001679 expr_ty p;
1680 if (!test) {
1681 PyErr_SetString(PyExc_ValueError,
1682 "field test is required for IfExp");
1683 return NULL;
1684 }
1685 if (!body) {
1686 PyErr_SetString(PyExc_ValueError,
1687 "field body is required for IfExp");
1688 return NULL;
1689 }
1690 if (!orelse) {
1691 PyErr_SetString(PyExc_ValueError,
1692 "field orelse is required for IfExp");
1693 return NULL;
1694 }
1695 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1696 if (!p)
1697 return NULL;
1698 p->kind = IfExp_kind;
1699 p->v.IfExp.test = test;
1700 p->v.IfExp.body = body;
1701 p->v.IfExp.orelse = orelse;
1702 p->lineno = lineno;
1703 p->col_offset = col_offset;
1704 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001705}
1706
1707expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001708Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1709 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001711 expr_ty p;
1712 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1713 if (!p)
1714 return NULL;
1715 p->kind = Dict_kind;
1716 p->v.Dict.keys = keys;
1717 p->v.Dict.values = values;
1718 p->lineno = lineno;
1719 p->col_offset = col_offset;
1720 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721}
1722
1723expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001724Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1725{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001726 expr_ty p;
1727 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1728 if (!p)
1729 return NULL;
1730 p->kind = Set_kind;
1731 p->v.Set.elts = elts;
1732 p->lineno = lineno;
1733 p->col_offset = col_offset;
1734 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001735}
1736
1737expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001738ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1739 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001741 expr_ty p;
1742 if (!elt) {
1743 PyErr_SetString(PyExc_ValueError,
1744 "field elt is required for ListComp");
1745 return NULL;
1746 }
1747 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1748 if (!p)
1749 return NULL;
1750 p->kind = ListComp_kind;
1751 p->v.ListComp.elt = elt;
1752 p->v.ListComp.generators = generators;
1753 p->lineno = lineno;
1754 p->col_offset = col_offset;
1755 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756}
1757
1758expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001759SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1760 *arena)
1761{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001762 expr_ty p;
1763 if (!elt) {
1764 PyErr_SetString(PyExc_ValueError,
1765 "field elt is required for SetComp");
1766 return NULL;
1767 }
1768 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1769 if (!p)
1770 return NULL;
1771 p->kind = SetComp_kind;
1772 p->v.SetComp.elt = elt;
1773 p->v.SetComp.generators = generators;
1774 p->lineno = lineno;
1775 p->col_offset = col_offset;
1776 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001777}
1778
1779expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1781 col_offset, PyArena *arena)
1782{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001783 expr_ty p;
1784 if (!key) {
1785 PyErr_SetString(PyExc_ValueError,
1786 "field key is required for DictComp");
1787 return NULL;
1788 }
1789 if (!value) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field value is required for DictComp");
1792 return NULL;
1793 }
1794 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1795 if (!p)
1796 return NULL;
1797 p->kind = DictComp_kind;
1798 p->v.DictComp.key = key;
1799 p->v.DictComp.value = value;
1800 p->v.DictComp.generators = generators;
1801 p->lineno = lineno;
1802 p->col_offset = col_offset;
1803 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001804}
1805
1806expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001807GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1808 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001810 expr_ty p;
1811 if (!elt) {
1812 PyErr_SetString(PyExc_ValueError,
1813 "field elt is required for GeneratorExp");
1814 return NULL;
1815 }
1816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1817 if (!p)
1818 return NULL;
1819 p->kind = GeneratorExp_kind;
1820 p->v.GeneratorExp.elt = elt;
1821 p->v.GeneratorExp.generators = generators;
1822 p->lineno = lineno;
1823 p->col_offset = col_offset;
1824 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825}
1826
1827expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001828Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001830 expr_ty p;
1831 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1832 if (!p)
1833 return NULL;
1834 p->kind = Yield_kind;
1835 p->v.Yield.value = value;
1836 p->lineno = lineno;
1837 p->col_offset = col_offset;
1838 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839}
1840
1841expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001842YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1843{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001844 expr_ty p;
1845 if (!value) {
1846 PyErr_SetString(PyExc_ValueError,
1847 "field value is required for YieldFrom");
1848 return NULL;
1849 }
1850 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1851 if (!p)
1852 return NULL;
1853 p->kind = YieldFrom_kind;
1854 p->v.YieldFrom.value = value;
1855 p->lineno = lineno;
1856 p->col_offset = col_offset;
1857 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001858}
1859
1860expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001861Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1862 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001864 expr_ty p;
1865 if (!left) {
1866 PyErr_SetString(PyExc_ValueError,
1867 "field left is required for Compare");
1868 return NULL;
1869 }
1870 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1871 if (!p)
1872 return NULL;
1873 p->kind = Compare_kind;
1874 p->v.Compare.left = left;
1875 p->v.Compare.ops = ops;
1876 p->v.Compare.comparators = comparators;
1877 p->lineno = lineno;
1878 p->col_offset = col_offset;
1879 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880}
1881
1882expr_ty
1883Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001884 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001886 expr_ty p;
1887 if (!func) {
1888 PyErr_SetString(PyExc_ValueError,
1889 "field func is required for Call");
1890 return NULL;
1891 }
1892 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1893 if (!p)
1894 return NULL;
1895 p->kind = Call_kind;
1896 p->v.Call.func = func;
1897 p->v.Call.args = args;
1898 p->v.Call.keywords = keywords;
1899 p->v.Call.starargs = starargs;
1900 p->v.Call.kwargs = kwargs;
1901 p->lineno = lineno;
1902 p->col_offset = col_offset;
1903 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904}
1905
1906expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001907Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001909 expr_ty p;
1910 if (!n) {
1911 PyErr_SetString(PyExc_ValueError,
1912 "field n is required for Num");
1913 return NULL;
1914 }
1915 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1916 if (!p)
1917 return NULL;
1918 p->kind = Num_kind;
1919 p->v.Num.n = n;
1920 p->lineno = lineno;
1921 p->col_offset = col_offset;
1922 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923}
1924
1925expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001926Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001928 expr_ty p;
1929 if (!s) {
1930 PyErr_SetString(PyExc_ValueError,
1931 "field s is required for Str");
1932 return NULL;
1933 }
1934 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1935 if (!p)
1936 return NULL;
1937 p->kind = Str_kind;
1938 p->v.Str.s = s;
1939 p->lineno = lineno;
1940 p->col_offset = col_offset;
1941 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942}
1943
1944expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001945Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001946{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001947 expr_ty p;
1948 if (!s) {
1949 PyErr_SetString(PyExc_ValueError,
1950 "field s is required for Bytes");
1951 return NULL;
1952 }
1953 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1954 if (!p)
1955 return NULL;
1956 p->kind = Bytes_kind;
1957 p->v.Bytes.s = s;
1958 p->lineno = lineno;
1959 p->col_offset = col_offset;
1960 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001961}
1962
1963expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05001964NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
1965{
Victor Stinneree4b59c2013-07-27 00:01:35 +02001966 expr_ty p;
1967 if (!value) {
1968 PyErr_SetString(PyExc_ValueError,
1969 "field value is required for NameConstant");
1970 return NULL;
1971 }
1972 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1973 if (!p)
1974 return NULL;
1975 p->kind = NameConstant_kind;
1976 p->v.NameConstant.value = value;
1977 p->lineno = lineno;
1978 p->col_offset = col_offset;
1979 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001980}
1981
1982expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001983Ellipsis(int lineno, int col_offset, PyArena *arena)
1984{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001985 expr_ty p;
1986 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1987 if (!p)
1988 return NULL;
1989 p->kind = Ellipsis_kind;
1990 p->lineno = lineno;
1991 p->col_offset = col_offset;
1992 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001993}
1994
1995expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001996Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1997 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001999 expr_ty p;
2000 if (!value) {
2001 PyErr_SetString(PyExc_ValueError,
2002 "field value is required for Attribute");
2003 return NULL;
2004 }
2005 if (!attr) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field attr is required for Attribute");
2008 return NULL;
2009 }
2010 if (!ctx) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "field ctx is required for Attribute");
2013 return NULL;
2014 }
2015 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2016 if (!p)
2017 return NULL;
2018 p->kind = Attribute_kind;
2019 p->v.Attribute.value = value;
2020 p->v.Attribute.attr = attr;
2021 p->v.Attribute.ctx = ctx;
2022 p->lineno = lineno;
2023 p->col_offset = col_offset;
2024 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002028Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2029 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002031 expr_ty p;
2032 if (!value) {
2033 PyErr_SetString(PyExc_ValueError,
2034 "field value is required for Subscript");
2035 return NULL;
2036 }
2037 if (!slice) {
2038 PyErr_SetString(PyExc_ValueError,
2039 "field slice is required for Subscript");
2040 return NULL;
2041 }
2042 if (!ctx) {
2043 PyErr_SetString(PyExc_ValueError,
2044 "field ctx is required for Subscript");
2045 return NULL;
2046 }
2047 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2048 if (!p)
2049 return NULL;
2050 p->kind = Subscript_kind;
2051 p->v.Subscript.value = value;
2052 p->v.Subscript.slice = slice;
2053 p->v.Subscript.ctx = ctx;
2054 p->lineno = lineno;
2055 p->col_offset = col_offset;
2056 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057}
2058
2059expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002060Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2061 *arena)
2062{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002063 expr_ty p;
2064 if (!value) {
2065 PyErr_SetString(PyExc_ValueError,
2066 "field value is required for Starred");
2067 return NULL;
2068 }
2069 if (!ctx) {
2070 PyErr_SetString(PyExc_ValueError,
2071 "field ctx is required for Starred");
2072 return NULL;
2073 }
2074 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2075 if (!p)
2076 return NULL;
2077 p->kind = Starred_kind;
2078 p->v.Starred.value = value;
2079 p->v.Starred.ctx = ctx;
2080 p->lineno = lineno;
2081 p->col_offset = col_offset;
2082 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002083}
2084
2085expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002086Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2087 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002089 expr_ty p;
2090 if (!id) {
2091 PyErr_SetString(PyExc_ValueError,
2092 "field id is required for Name");
2093 return NULL;
2094 }
2095 if (!ctx) {
2096 PyErr_SetString(PyExc_ValueError,
2097 "field ctx is required for Name");
2098 return NULL;
2099 }
2100 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2101 if (!p)
2102 return NULL;
2103 p->kind = Name_kind;
2104 p->v.Name.id = id;
2105 p->v.Name.ctx = ctx;
2106 p->lineno = lineno;
2107 p->col_offset = col_offset;
2108 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109}
2110
2111expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002112List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2113 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002115 expr_ty p;
2116 if (!ctx) {
2117 PyErr_SetString(PyExc_ValueError,
2118 "field ctx is required for List");
2119 return NULL;
2120 }
2121 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2122 if (!p)
2123 return NULL;
2124 p->kind = List_kind;
2125 p->v.List.elts = elts;
2126 p->v.List.ctx = ctx;
2127 p->lineno = lineno;
2128 p->col_offset = col_offset;
2129 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130}
2131
2132expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002133Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2134 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002136 expr_ty p;
2137 if (!ctx) {
2138 PyErr_SetString(PyExc_ValueError,
2139 "field ctx is required for Tuple");
2140 return NULL;
2141 }
2142 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2143 if (!p)
2144 return NULL;
2145 p->kind = Tuple_kind;
2146 p->v.Tuple.elts = elts;
2147 p->v.Tuple.ctx = ctx;
2148 p->lineno = lineno;
2149 p->col_offset = col_offset;
2150 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151}
2152
2153slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002154Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002156 slice_ty p;
2157 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2158 if (!p)
2159 return NULL;
2160 p->kind = Slice_kind;
2161 p->v.Slice.lower = lower;
2162 p->v.Slice.upper = upper;
2163 p->v.Slice.step = step;
2164 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165}
2166
2167slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002168ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002170 slice_ty p;
2171 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2172 if (!p)
2173 return NULL;
2174 p->kind = ExtSlice_kind;
2175 p->v.ExtSlice.dims = dims;
2176 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177}
2178
2179slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002180Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002182 slice_ty p;
2183 if (!value) {
2184 PyErr_SetString(PyExc_ValueError,
2185 "field value is required for Index");
2186 return NULL;
2187 }
2188 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2189 if (!p)
2190 return NULL;
2191 p->kind = Index_kind;
2192 p->v.Index.value = value;
2193 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194}
2195
2196comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002197comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002199 comprehension_ty p;
2200 if (!target) {
2201 PyErr_SetString(PyExc_ValueError,
2202 "field target is required for comprehension");
2203 return NULL;
2204 }
2205 if (!iter) {
2206 PyErr_SetString(PyExc_ValueError,
2207 "field iter is required for comprehension");
2208 return NULL;
2209 }
2210 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2211 if (!p)
2212 return NULL;
2213 p->target = target;
2214 p->iter = iter;
2215 p->ifs = ifs;
2216 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217}
2218
2219excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002220ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002221 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002223 excepthandler_ty p;
2224 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2225 if (!p)
2226 return NULL;
2227 p->kind = ExceptHandler_kind;
2228 p->v.ExceptHandler.type = type;
2229 p->v.ExceptHandler.name = name;
2230 p->v.ExceptHandler.body = body;
2231 p->lineno = lineno;
2232 p->col_offset = col_offset;
2233 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234}
2235
2236arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002237arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2238 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002240 arguments_ty p;
2241 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2242 if (!p)
2243 return NULL;
2244 p->args = args;
2245 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002246 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002247 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002248 p->kwarg = kwarg;
2249 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
Neal Norwitzc1505362006-12-28 06:47:50 +00002253arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002254arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002255{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002256 arg_ty p;
2257 if (!arg) {
2258 PyErr_SetString(PyExc_ValueError,
2259 "field arg is required for arg");
2260 return NULL;
2261 }
2262 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2263 if (!p)
2264 return NULL;
2265 p->arg = arg;
2266 p->annotation = annotation;
2267 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002268}
2269
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002271keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002273 keyword_ty p;
2274 if (!arg) {
2275 PyErr_SetString(PyExc_ValueError,
2276 "field arg is required for keyword");
2277 return NULL;
2278 }
2279 if (!value) {
2280 PyErr_SetString(PyExc_ValueError,
2281 "field value is required for keyword");
2282 return NULL;
2283 }
2284 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2285 if (!p)
2286 return NULL;
2287 p->arg = arg;
2288 p->value = value;
2289 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290}
2291
2292alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002293alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002295 alias_ty p;
2296 if (!name) {
2297 PyErr_SetString(PyExc_ValueError,
2298 "field name is required for alias");
2299 return NULL;
2300 }
2301 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2302 if (!p)
2303 return NULL;
2304 p->name = name;
2305 p->asname = asname;
2306 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307}
2308
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002309withitem_ty
2310withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2311{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002312 withitem_ty p;
2313 if (!context_expr) {
2314 PyErr_SetString(PyExc_ValueError,
2315 "field context_expr is required for withitem");
2316 return NULL;
2317 }
2318 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2319 if (!p)
2320 return NULL;
2321 p->context_expr = context_expr;
2322 p->optional_vars = optional_vars;
2323 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002324}
2325
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002326
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002327PyObject*
2328ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002329{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002330 mod_ty o = (mod_ty)_o;
2331 PyObject *result = NULL, *value = NULL;
2332 if (!o) {
2333 Py_INCREF(Py_None);
2334 return Py_None;
2335 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002336
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002337 switch (o->kind) {
2338 case Module_kind:
2339 result = PyType_GenericNew(Module_type, NULL, NULL);
2340 if (!result) goto failed;
2341 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2342 if (!value) goto failed;
2343 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2344 goto failed;
2345 Py_DECREF(value);
2346 break;
2347 case Interactive_kind:
2348 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2349 if (!result) goto failed;
2350 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2351 if (!value) goto failed;
2352 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
2355 break;
2356 case Expression_kind:
2357 result = PyType_GenericNew(Expression_type, NULL, NULL);
2358 if (!result) goto failed;
2359 value = ast2obj_expr(o->v.Expression.body);
2360 if (!value) goto failed;
2361 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
2364 break;
2365 case Suite_kind:
2366 result = PyType_GenericNew(Suite_type, NULL, NULL);
2367 if (!result) goto failed;
2368 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2369 if (!value) goto failed;
2370 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
2373 break;
2374 }
2375 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002376failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002377 Py_XDECREF(value);
2378 Py_XDECREF(result);
2379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380}
2381
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002382PyObject*
2383ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002385 stmt_ty o = (stmt_ty)_o;
2386 PyObject *result = NULL, *value = NULL;
2387 if (!o) {
2388 Py_INCREF(Py_None);
2389 return Py_None;
2390 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002391
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002392 switch (o->kind) {
2393 case FunctionDef_kind:
2394 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2395 if (!result) goto failed;
2396 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002397 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002398 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2399 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002400 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002401 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002402 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002403 if (_PyObject_SetAttrId(result, &PyId_args, 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_list(o->v.FunctionDef.body, ast2obj_stmt);
2407 if (!value) goto failed;
2408 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
2411 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2412 if (!value) goto failed;
2413 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2414 goto failed;
2415 Py_DECREF(value);
2416 value = ast2obj_expr(o->v.FunctionDef.returns);
2417 if (!value) goto failed;
2418 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2419 goto failed;
2420 Py_DECREF(value);
2421 break;
2422 case ClassDef_kind:
2423 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2424 if (!result) goto failed;
2425 value = ast2obj_identifier(o->v.ClassDef.name);
2426 if (!value) goto failed;
2427 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2428 goto failed;
2429 Py_DECREF(value);
2430 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2431 if (!value) goto failed;
2432 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2433 goto failed;
2434 Py_DECREF(value);
2435 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2436 if (!value) goto failed;
2437 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2438 goto failed;
2439 Py_DECREF(value);
2440 value = ast2obj_expr(o->v.ClassDef.starargs);
2441 if (!value) goto failed;
2442 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2443 goto failed;
2444 Py_DECREF(value);
2445 value = ast2obj_expr(o->v.ClassDef.kwargs);
2446 if (!value) goto failed;
2447 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2448 goto failed;
2449 Py_DECREF(value);
2450 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2451 if (!value) goto failed;
2452 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
2455 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2456 if (!value) goto failed;
2457 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2458 goto failed;
2459 Py_DECREF(value);
2460 break;
2461 case Return_kind:
2462 result = PyType_GenericNew(Return_type, NULL, NULL);
2463 if (!result) goto failed;
2464 value = ast2obj_expr(o->v.Return.value);
2465 if (!value) goto failed;
2466 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2467 goto failed;
2468 Py_DECREF(value);
2469 break;
2470 case Delete_kind:
2471 result = PyType_GenericNew(Delete_type, NULL, NULL);
2472 if (!result) goto failed;
2473 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2474 if (!value) goto failed;
2475 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2476 goto failed;
2477 Py_DECREF(value);
2478 break;
2479 case Assign_kind:
2480 result = PyType_GenericNew(Assign_type, NULL, NULL);
2481 if (!result) goto failed;
2482 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2483 if (!value) goto failed;
2484 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2485 goto failed;
2486 Py_DECREF(value);
2487 value = ast2obj_expr(o->v.Assign.value);
2488 if (!value) goto failed;
2489 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 break;
2493 case AugAssign_kind:
2494 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2495 if (!result) goto failed;
2496 value = ast2obj_expr(o->v.AugAssign.target);
2497 if (!value) goto failed;
2498 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2499 goto failed;
2500 Py_DECREF(value);
2501 value = ast2obj_operator(o->v.AugAssign.op);
2502 if (!value) goto failed;
2503 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2504 goto failed;
2505 Py_DECREF(value);
2506 value = ast2obj_expr(o->v.AugAssign.value);
2507 if (!value) goto failed;
2508 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2509 goto failed;
2510 Py_DECREF(value);
2511 break;
2512 case For_kind:
2513 result = PyType_GenericNew(For_type, NULL, NULL);
2514 if (!result) goto failed;
2515 value = ast2obj_expr(o->v.For.target);
2516 if (!value) goto failed;
2517 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2518 goto failed;
2519 Py_DECREF(value);
2520 value = ast2obj_expr(o->v.For.iter);
2521 if (!value) goto failed;
2522 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
2525 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2526 if (!value) goto failed;
2527 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2531 if (!value) goto failed;
2532 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 break;
2536 case While_kind:
2537 result = PyType_GenericNew(While_type, NULL, NULL);
2538 if (!result) goto failed;
2539 value = ast2obj_expr(o->v.While.test);
2540 if (!value) goto failed;
2541 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2542 goto failed;
2543 Py_DECREF(value);
2544 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2545 if (!value) goto failed;
2546 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2550 if (!value) goto failed;
2551 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 break;
2555 case If_kind:
2556 result = PyType_GenericNew(If_type, NULL, NULL);
2557 if (!result) goto failed;
2558 value = ast2obj_expr(o->v.If.test);
2559 if (!value) goto failed;
2560 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
2563 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2564 if (!value) goto failed;
2565 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
2568 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2569 if (!value) goto failed;
2570 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2571 goto failed;
2572 Py_DECREF(value);
2573 break;
2574 case With_kind:
2575 result = PyType_GenericNew(With_type, NULL, NULL);
2576 if (!result) goto failed;
2577 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2578 if (!value) goto failed;
2579 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2580 goto failed;
2581 Py_DECREF(value);
2582 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2583 if (!value) goto failed;
2584 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
2587 break;
2588 case Raise_kind:
2589 result = PyType_GenericNew(Raise_type, NULL, NULL);
2590 if (!result) goto failed;
2591 value = ast2obj_expr(o->v.Raise.exc);
2592 if (!value) goto failed;
2593 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2594 goto failed;
2595 Py_DECREF(value);
2596 value = ast2obj_expr(o->v.Raise.cause);
2597 if (!value) goto failed;
2598 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
2601 break;
2602 case Try_kind:
2603 result = PyType_GenericNew(Try_type, NULL, NULL);
2604 if (!result) goto failed;
2605 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2606 if (!value) goto failed;
2607 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2608 goto failed;
2609 Py_DECREF(value);
2610 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2611 if (!value) goto failed;
2612 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
2615 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2616 if (!value) goto failed;
2617 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
2620 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2621 if (!value) goto failed;
2622 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2623 goto failed;
2624 Py_DECREF(value);
2625 break;
2626 case Assert_kind:
2627 result = PyType_GenericNew(Assert_type, NULL, NULL);
2628 if (!result) goto failed;
2629 value = ast2obj_expr(o->v.Assert.test);
2630 if (!value) goto failed;
2631 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 value = ast2obj_expr(o->v.Assert.msg);
2635 if (!value) goto failed;
2636 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2637 goto failed;
2638 Py_DECREF(value);
2639 break;
2640 case Import_kind:
2641 result = PyType_GenericNew(Import_type, NULL, NULL);
2642 if (!result) goto failed;
2643 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2644 if (!value) goto failed;
2645 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
2648 break;
2649 case ImportFrom_kind:
2650 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2651 if (!result) goto failed;
2652 value = ast2obj_identifier(o->v.ImportFrom.module);
2653 if (!value) goto failed;
2654 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2658 if (!value) goto failed;
2659 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 value = ast2obj_int(o->v.ImportFrom.level);
2663 if (!value) goto failed;
2664 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 break;
2668 case Global_kind:
2669 result = PyType_GenericNew(Global_type, NULL, NULL);
2670 if (!result) goto failed;
2671 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2672 if (!value) goto failed;
2673 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
2676 break;
2677 case Nonlocal_kind:
2678 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2679 if (!result) goto failed;
2680 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2681 if (!value) goto failed;
2682 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 break;
2686 case Expr_kind:
2687 result = PyType_GenericNew(Expr_type, NULL, NULL);
2688 if (!result) goto failed;
2689 value = ast2obj_expr(o->v.Expr.value);
2690 if (!value) goto failed;
2691 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2692 goto failed;
2693 Py_DECREF(value);
2694 break;
2695 case Pass_kind:
2696 result = PyType_GenericNew(Pass_type, NULL, NULL);
2697 if (!result) goto failed;
2698 break;
2699 case Break_kind:
2700 result = PyType_GenericNew(Break_type, NULL, NULL);
2701 if (!result) goto failed;
2702 break;
2703 case Continue_kind:
2704 result = PyType_GenericNew(Continue_type, NULL, NULL);
2705 if (!result) goto failed;
2706 break;
2707 }
2708 value = ast2obj_int(o->lineno);
2709 if (!value) goto failed;
2710 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2711 goto failed;
2712 Py_DECREF(value);
2713 value = ast2obj_int(o->col_offset);
2714 if (!value) goto failed;
2715 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2716 goto failed;
2717 Py_DECREF(value);
2718 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002719failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002720 Py_XDECREF(value);
2721 Py_XDECREF(result);
2722 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723}
2724
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002725PyObject*
2726ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002728 expr_ty o = (expr_ty)_o;
2729 PyObject *result = NULL, *value = NULL;
2730 if (!o) {
2731 Py_INCREF(Py_None);
2732 return Py_None;
2733 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002734
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002735 switch (o->kind) {
2736 case BoolOp_kind:
2737 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2738 if (!result) goto failed;
2739 value = ast2obj_boolop(o->v.BoolOp.op);
2740 if (!value) goto failed;
2741 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2742 goto failed;
2743 Py_DECREF(value);
2744 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2745 if (!value) goto failed;
2746 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2747 goto failed;
2748 Py_DECREF(value);
2749 break;
2750 case BinOp_kind:
2751 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2752 if (!result) goto failed;
2753 value = ast2obj_expr(o->v.BinOp.left);
2754 if (!value) goto failed;
2755 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2756 goto failed;
2757 Py_DECREF(value);
2758 value = ast2obj_operator(o->v.BinOp.op);
2759 if (!value) goto failed;
2760 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2761 goto failed;
2762 Py_DECREF(value);
2763 value = ast2obj_expr(o->v.BinOp.right);
2764 if (!value) goto failed;
2765 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
2768 break;
2769 case UnaryOp_kind:
2770 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2771 if (!result) goto failed;
2772 value = ast2obj_unaryop(o->v.UnaryOp.op);
2773 if (!value) goto failed;
2774 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2775 goto failed;
2776 Py_DECREF(value);
2777 value = ast2obj_expr(o->v.UnaryOp.operand);
2778 if (!value) goto failed;
2779 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
2782 break;
2783 case Lambda_kind:
2784 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2785 if (!result) goto failed;
2786 value = ast2obj_arguments(o->v.Lambda.args);
2787 if (!value) goto failed;
2788 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2789 goto failed;
2790 Py_DECREF(value);
2791 value = ast2obj_expr(o->v.Lambda.body);
2792 if (!value) goto failed;
2793 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 break;
2797 case IfExp_kind:
2798 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2799 if (!result) goto failed;
2800 value = ast2obj_expr(o->v.IfExp.test);
2801 if (!value) goto failed;
2802 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2803 goto failed;
2804 Py_DECREF(value);
2805 value = ast2obj_expr(o->v.IfExp.body);
2806 if (!value) goto failed;
2807 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2808 goto failed;
2809 Py_DECREF(value);
2810 value = ast2obj_expr(o->v.IfExp.orelse);
2811 if (!value) goto failed;
2812 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
2815 break;
2816 case Dict_kind:
2817 result = PyType_GenericNew(Dict_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2820 if (!value) goto failed;
2821 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2825 if (!value) goto failed;
2826 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 break;
2830 case Set_kind:
2831 result = PyType_GenericNew(Set_type, NULL, NULL);
2832 if (!result) goto failed;
2833 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2834 if (!value) goto failed;
2835 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2836 goto failed;
2837 Py_DECREF(value);
2838 break;
2839 case ListComp_kind:
2840 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2841 if (!result) goto failed;
2842 value = ast2obj_expr(o->v.ListComp.elt);
2843 if (!value) goto failed;
2844 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2848 if (!value) goto failed;
2849 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
2852 break;
2853 case SetComp_kind:
2854 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2855 if (!result) goto failed;
2856 value = ast2obj_expr(o->v.SetComp.elt);
2857 if (!value) goto failed;
2858 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2862 if (!value) goto failed;
2863 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 break;
2867 case DictComp_kind:
2868 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2869 if (!result) goto failed;
2870 value = ast2obj_expr(o->v.DictComp.key);
2871 if (!value) goto failed;
2872 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 value = ast2obj_expr(o->v.DictComp.value);
2876 if (!value) goto failed;
2877 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2881 if (!value) goto failed;
2882 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 break;
2886 case GeneratorExp_kind:
2887 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2888 if (!result) goto failed;
2889 value = ast2obj_expr(o->v.GeneratorExp.elt);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 value = ast2obj_list(o->v.GeneratorExp.generators,
2895 ast2obj_comprehension);
2896 if (!value) goto failed;
2897 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2898 goto failed;
2899 Py_DECREF(value);
2900 break;
2901 case Yield_kind:
2902 result = PyType_GenericNew(Yield_type, NULL, NULL);
2903 if (!result) goto failed;
2904 value = ast2obj_expr(o->v.Yield.value);
2905 if (!value) goto failed;
2906 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2907 goto failed;
2908 Py_DECREF(value);
2909 break;
2910 case YieldFrom_kind:
2911 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2912 if (!result) goto failed;
2913 value = ast2obj_expr(o->v.YieldFrom.value);
2914 if (!value) goto failed;
2915 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2916 goto failed;
2917 Py_DECREF(value);
2918 break;
2919 case Compare_kind:
2920 result = PyType_GenericNew(Compare_type, NULL, NULL);
2921 if (!result) goto failed;
2922 value = ast2obj_expr(o->v.Compare.left);
2923 if (!value) goto failed;
2924 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2925 goto failed;
2926 Py_DECREF(value);
2927 {
2928 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2929 value = PyList_New(n);
2930 if (!value) goto failed;
2931 for(i = 0; i < n; i++)
2932 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002934 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002935 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2936 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002937 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002938 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002939 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002940 if (_PyObject_SetAttrId(result, &PyId_comparators, 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 break;
2944 case Call_kind:
2945 result = PyType_GenericNew(Call_type, NULL, NULL);
2946 if (!result) goto failed;
2947 value = ast2obj_expr(o->v.Call.func);
2948 if (!value) goto failed;
2949 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2950 goto failed;
2951 Py_DECREF(value);
2952 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2953 if (!value) goto failed;
2954 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2955 goto failed;
2956 Py_DECREF(value);
2957 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2958 if (!value) goto failed;
2959 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2960 goto failed;
2961 Py_DECREF(value);
2962 value = ast2obj_expr(o->v.Call.starargs);
2963 if (!value) goto failed;
2964 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2965 goto failed;
2966 Py_DECREF(value);
2967 value = ast2obj_expr(o->v.Call.kwargs);
2968 if (!value) goto failed;
2969 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2970 goto failed;
2971 Py_DECREF(value);
2972 break;
2973 case Num_kind:
2974 result = PyType_GenericNew(Num_type, NULL, NULL);
2975 if (!result) goto failed;
2976 value = ast2obj_object(o->v.Num.n);
2977 if (!value) goto failed;
2978 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2979 goto failed;
2980 Py_DECREF(value);
2981 break;
2982 case Str_kind:
2983 result = PyType_GenericNew(Str_type, NULL, NULL);
2984 if (!result) goto failed;
2985 value = ast2obj_string(o->v.Str.s);
2986 if (!value) goto failed;
2987 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2988 goto failed;
2989 Py_DECREF(value);
2990 break;
2991 case Bytes_kind:
2992 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2993 if (!result) goto failed;
2994 value = ast2obj_bytes(o->v.Bytes.s);
2995 if (!value) goto failed;
2996 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2997 goto failed;
2998 Py_DECREF(value);
2999 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003000 case NameConstant_kind:
3001 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3002 if (!result) goto failed;
3003 value = ast2obj_singleton(o->v.NameConstant.value);
3004 if (!value) goto failed;
3005 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3006 goto failed;
3007 Py_DECREF(value);
3008 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003009 case Ellipsis_kind:
3010 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3011 if (!result) goto failed;
3012 break;
3013 case Attribute_kind:
3014 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3015 if (!result) goto failed;
3016 value = ast2obj_expr(o->v.Attribute.value);
3017 if (!value) goto failed;
3018 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
3021 value = ast2obj_identifier(o->v.Attribute.attr);
3022 if (!value) goto failed;
3023 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3024 goto failed;
3025 Py_DECREF(value);
3026 value = ast2obj_expr_context(o->v.Attribute.ctx);
3027 if (!value) goto failed;
3028 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3029 goto failed;
3030 Py_DECREF(value);
3031 break;
3032 case Subscript_kind:
3033 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3034 if (!result) goto failed;
3035 value = ast2obj_expr(o->v.Subscript.value);
3036 if (!value) goto failed;
3037 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3038 goto failed;
3039 Py_DECREF(value);
3040 value = ast2obj_slice(o->v.Subscript.slice);
3041 if (!value) goto failed;
3042 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3043 goto failed;
3044 Py_DECREF(value);
3045 value = ast2obj_expr_context(o->v.Subscript.ctx);
3046 if (!value) goto failed;
3047 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3048 goto failed;
3049 Py_DECREF(value);
3050 break;
3051 case Starred_kind:
3052 result = PyType_GenericNew(Starred_type, NULL, NULL);
3053 if (!result) goto failed;
3054 value = ast2obj_expr(o->v.Starred.value);
3055 if (!value) goto failed;
3056 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3057 goto failed;
3058 Py_DECREF(value);
3059 value = ast2obj_expr_context(o->v.Starred.ctx);
3060 if (!value) goto failed;
3061 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3062 goto failed;
3063 Py_DECREF(value);
3064 break;
3065 case Name_kind:
3066 result = PyType_GenericNew(Name_type, NULL, NULL);
3067 if (!result) goto failed;
3068 value = ast2obj_identifier(o->v.Name.id);
3069 if (!value) goto failed;
3070 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3071 goto failed;
3072 Py_DECREF(value);
3073 value = ast2obj_expr_context(o->v.Name.ctx);
3074 if (!value) goto failed;
3075 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3076 goto failed;
3077 Py_DECREF(value);
3078 break;
3079 case List_kind:
3080 result = PyType_GenericNew(List_type, NULL, NULL);
3081 if (!result) goto failed;
3082 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3083 if (!value) goto failed;
3084 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3085 goto failed;
3086 Py_DECREF(value);
3087 value = ast2obj_expr_context(o->v.List.ctx);
3088 if (!value) goto failed;
3089 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3090 goto failed;
3091 Py_DECREF(value);
3092 break;
3093 case Tuple_kind:
3094 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3095 if (!result) goto failed;
3096 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3097 if (!value) goto failed;
3098 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3099 goto failed;
3100 Py_DECREF(value);
3101 value = ast2obj_expr_context(o->v.Tuple.ctx);
3102 if (!value) goto failed;
3103 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3104 goto failed;
3105 Py_DECREF(value);
3106 break;
3107 }
3108 value = ast2obj_int(o->lineno);
3109 if (!value) goto failed;
3110 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3111 goto failed;
3112 Py_DECREF(value);
3113 value = ast2obj_int(o->col_offset);
3114 if (!value) goto failed;
3115 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3116 goto failed;
3117 Py_DECREF(value);
3118 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003119failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003120 Py_XDECREF(value);
3121 Py_XDECREF(result);
3122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123}
3124
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003125PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003127 switch(o) {
3128 case Load:
3129 Py_INCREF(Load_singleton);
3130 return Load_singleton;
3131 case Store:
3132 Py_INCREF(Store_singleton);
3133 return Store_singleton;
3134 case Del:
3135 Py_INCREF(Del_singleton);
3136 return Del_singleton;
3137 case AugLoad:
3138 Py_INCREF(AugLoad_singleton);
3139 return AugLoad_singleton;
3140 case AugStore:
3141 Py_INCREF(AugStore_singleton);
3142 return AugStore_singleton;
3143 case Param:
3144 Py_INCREF(Param_singleton);
3145 return Param_singleton;
3146 default:
3147 /* should never happen, but just in case ... */
3148 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3149 return NULL;
3150 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003152PyObject*
3153ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003155 slice_ty o = (slice_ty)_o;
3156 PyObject *result = NULL, *value = NULL;
3157 if (!o) {
3158 Py_INCREF(Py_None);
3159 return Py_None;
3160 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003161
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003162 switch (o->kind) {
3163 case Slice_kind:
3164 result = PyType_GenericNew(Slice_type, NULL, NULL);
3165 if (!result) goto failed;
3166 value = ast2obj_expr(o->v.Slice.lower);
3167 if (!value) goto failed;
3168 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3169 goto failed;
3170 Py_DECREF(value);
3171 value = ast2obj_expr(o->v.Slice.upper);
3172 if (!value) goto failed;
3173 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3174 goto failed;
3175 Py_DECREF(value);
3176 value = ast2obj_expr(o->v.Slice.step);
3177 if (!value) goto failed;
3178 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3179 goto failed;
3180 Py_DECREF(value);
3181 break;
3182 case ExtSlice_kind:
3183 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3184 if (!result) goto failed;
3185 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3186 if (!value) goto failed;
3187 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3188 goto failed;
3189 Py_DECREF(value);
3190 break;
3191 case Index_kind:
3192 result = PyType_GenericNew(Index_type, NULL, NULL);
3193 if (!result) goto failed;
3194 value = ast2obj_expr(o->v.Index.value);
3195 if (!value) goto failed;
3196 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3197 goto failed;
3198 Py_DECREF(value);
3199 break;
3200 }
3201 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003202failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003203 Py_XDECREF(value);
3204 Py_XDECREF(result);
3205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206}
3207
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003208PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003210 switch(o) {
3211 case And:
3212 Py_INCREF(And_singleton);
3213 return And_singleton;
3214 case Or:
3215 Py_INCREF(Or_singleton);
3216 return Or_singleton;
3217 default:
3218 /* should never happen, but just in case ... */
3219 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3220 return NULL;
3221 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003222}
3223PyObject* ast2obj_operator(operator_ty o)
3224{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003225 switch(o) {
3226 case Add:
3227 Py_INCREF(Add_singleton);
3228 return Add_singleton;
3229 case Sub:
3230 Py_INCREF(Sub_singleton);
3231 return Sub_singleton;
3232 case Mult:
3233 Py_INCREF(Mult_singleton);
3234 return Mult_singleton;
3235 case Div:
3236 Py_INCREF(Div_singleton);
3237 return Div_singleton;
3238 case Mod:
3239 Py_INCREF(Mod_singleton);
3240 return Mod_singleton;
3241 case Pow:
3242 Py_INCREF(Pow_singleton);
3243 return Pow_singleton;
3244 case LShift:
3245 Py_INCREF(LShift_singleton);
3246 return LShift_singleton;
3247 case RShift:
3248 Py_INCREF(RShift_singleton);
3249 return RShift_singleton;
3250 case BitOr:
3251 Py_INCREF(BitOr_singleton);
3252 return BitOr_singleton;
3253 case BitXor:
3254 Py_INCREF(BitXor_singleton);
3255 return BitXor_singleton;
3256 case BitAnd:
3257 Py_INCREF(BitAnd_singleton);
3258 return BitAnd_singleton;
3259 case FloorDiv:
3260 Py_INCREF(FloorDiv_singleton);
3261 return FloorDiv_singleton;
3262 default:
3263 /* should never happen, but just in case ... */
3264 PyErr_Format(PyExc_SystemError, "unknown operator found");
3265 return NULL;
3266 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003267}
3268PyObject* ast2obj_unaryop(unaryop_ty o)
3269{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003270 switch(o) {
3271 case Invert:
3272 Py_INCREF(Invert_singleton);
3273 return Invert_singleton;
3274 case Not:
3275 Py_INCREF(Not_singleton);
3276 return Not_singleton;
3277 case UAdd:
3278 Py_INCREF(UAdd_singleton);
3279 return UAdd_singleton;
3280 case USub:
3281 Py_INCREF(USub_singleton);
3282 return USub_singleton;
3283 default:
3284 /* should never happen, but just in case ... */
3285 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3286 return NULL;
3287 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003288}
3289PyObject* ast2obj_cmpop(cmpop_ty o)
3290{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003291 switch(o) {
3292 case Eq:
3293 Py_INCREF(Eq_singleton);
3294 return Eq_singleton;
3295 case NotEq:
3296 Py_INCREF(NotEq_singleton);
3297 return NotEq_singleton;
3298 case Lt:
3299 Py_INCREF(Lt_singleton);
3300 return Lt_singleton;
3301 case LtE:
3302 Py_INCREF(LtE_singleton);
3303 return LtE_singleton;
3304 case Gt:
3305 Py_INCREF(Gt_singleton);
3306 return Gt_singleton;
3307 case GtE:
3308 Py_INCREF(GtE_singleton);
3309 return GtE_singleton;
3310 case Is:
3311 Py_INCREF(Is_singleton);
3312 return Is_singleton;
3313 case IsNot:
3314 Py_INCREF(IsNot_singleton);
3315 return IsNot_singleton;
3316 case In:
3317 Py_INCREF(In_singleton);
3318 return In_singleton;
3319 case NotIn:
3320 Py_INCREF(NotIn_singleton);
3321 return NotIn_singleton;
3322 default:
3323 /* should never happen, but just in case ... */
3324 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3325 return NULL;
3326 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003327}
3328PyObject*
3329ast2obj_comprehension(void* _o)
3330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003331 comprehension_ty o = (comprehension_ty)_o;
3332 PyObject *result = NULL, *value = NULL;
3333 if (!o) {
3334 Py_INCREF(Py_None);
3335 return Py_None;
3336 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003337
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3339 if (!result) return NULL;
3340 value = ast2obj_expr(o->target);
3341 if (!value) goto failed;
3342 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3343 goto failed;
3344 Py_DECREF(value);
3345 value = ast2obj_expr(o->iter);
3346 if (!value) goto failed;
3347 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3348 goto failed;
3349 Py_DECREF(value);
3350 value = ast2obj_list(o->ifs, ast2obj_expr);
3351 if (!value) goto failed;
3352 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3353 goto failed;
3354 Py_DECREF(value);
3355 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003356failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003357 Py_XDECREF(value);
3358 Py_XDECREF(result);
3359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360}
3361
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003362PyObject*
3363ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003365 excepthandler_ty o = (excepthandler_ty)_o;
3366 PyObject *result = NULL, *value = NULL;
3367 if (!o) {
3368 Py_INCREF(Py_None);
3369 return Py_None;
3370 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003371
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003372 switch (o->kind) {
3373 case ExceptHandler_kind:
3374 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3375 if (!result) goto failed;
3376 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003377 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003378 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3379 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003380 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003381 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003382 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003383 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3384 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003385 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003386 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3387 if (!value) goto failed;
3388 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3389 goto failed;
3390 Py_DECREF(value);
3391 break;
3392 }
3393 value = ast2obj_int(o->lineno);
3394 if (!value) goto failed;
3395 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3396 goto failed;
3397 Py_DECREF(value);
3398 value = ast2obj_int(o->col_offset);
3399 if (!value) goto failed;
3400 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3401 goto failed;
3402 Py_DECREF(value);
3403 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003404failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003405 Py_XDECREF(value);
3406 Py_XDECREF(result);
3407 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
3409
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003410PyObject*
3411ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003413 arguments_ty o = (arguments_ty)_o;
3414 PyObject *result = NULL, *value = NULL;
3415 if (!o) {
3416 Py_INCREF(Py_None);
3417 return Py_None;
3418 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003419
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003420 result = PyType_GenericNew(arguments_type, NULL, NULL);
3421 if (!result) return NULL;
3422 value = ast2obj_list(o->args, ast2obj_arg);
3423 if (!value) goto failed;
3424 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3425 goto failed;
3426 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003427 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003428 if (!value) goto failed;
3429 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3430 goto failed;
3431 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003432 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3433 if (!value) goto failed;
3434 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3435 goto failed;
3436 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003437 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003438 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003439 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003440 goto failed;
3441 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003442 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003443 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003444 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003445 goto failed;
3446 Py_DECREF(value);
3447 value = ast2obj_list(o->defaults, ast2obj_expr);
3448 if (!value) goto failed;
3449 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3450 goto failed;
3451 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003452 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003453failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003454 Py_XDECREF(value);
3455 Py_XDECREF(result);
3456 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457}
3458
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003459PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003460ast2obj_arg(void* _o)
3461{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003462 arg_ty o = (arg_ty)_o;
3463 PyObject *result = NULL, *value = NULL;
3464 if (!o) {
3465 Py_INCREF(Py_None);
3466 return Py_None;
3467 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003468
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003469 result = PyType_GenericNew(arg_type, NULL, NULL);
3470 if (!result) return NULL;
3471 value = ast2obj_identifier(o->arg);
3472 if (!value) goto failed;
3473 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3474 goto failed;
3475 Py_DECREF(value);
3476 value = ast2obj_expr(o->annotation);
3477 if (!value) goto failed;
3478 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3479 goto failed;
3480 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003481 value = ast2obj_int(o->lineno);
3482 if (!value) goto failed;
3483 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3484 goto failed;
3485 Py_DECREF(value);
3486 value = ast2obj_int(o->col_offset);
3487 if (!value) goto failed;
3488 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3489 goto failed;
3490 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003491 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003492failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003493 Py_XDECREF(value);
3494 Py_XDECREF(result);
3495 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003496}
3497
3498PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003499ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003501 keyword_ty o = (keyword_ty)_o;
3502 PyObject *result = NULL, *value = NULL;
3503 if (!o) {
3504 Py_INCREF(Py_None);
3505 return Py_None;
3506 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003507
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003508 result = PyType_GenericNew(keyword_type, NULL, NULL);
3509 if (!result) return NULL;
3510 value = ast2obj_identifier(o->arg);
3511 if (!value) goto failed;
3512 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3513 goto failed;
3514 Py_DECREF(value);
3515 value = ast2obj_expr(o->value);
3516 if (!value) goto failed;
3517 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3518 goto failed;
3519 Py_DECREF(value);
3520 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003521failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003522 Py_XDECREF(value);
3523 Py_XDECREF(result);
3524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
3526
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003527PyObject*
3528ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 alias_ty o = (alias_ty)_o;
3531 PyObject *result = NULL, *value = NULL;
3532 if (!o) {
3533 Py_INCREF(Py_None);
3534 return Py_None;
3535 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003536
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003537 result = PyType_GenericNew(alias_type, NULL, NULL);
3538 if (!result) return NULL;
3539 value = ast2obj_identifier(o->name);
3540 if (!value) goto failed;
3541 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3542 goto failed;
3543 Py_DECREF(value);
3544 value = ast2obj_identifier(o->asname);
3545 if (!value) goto failed;
3546 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3547 goto failed;
3548 Py_DECREF(value);
3549 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003550failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003551 Py_XDECREF(value);
3552 Py_XDECREF(result);
3553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554}
3555
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003556PyObject*
3557ast2obj_withitem(void* _o)
3558{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003559 withitem_ty o = (withitem_ty)_o;
3560 PyObject *result = NULL, *value = NULL;
3561 if (!o) {
3562 Py_INCREF(Py_None);
3563 return Py_None;
3564 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003565
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003566 result = PyType_GenericNew(withitem_type, NULL, NULL);
3567 if (!result) return NULL;
3568 value = ast2obj_expr(o->context_expr);
3569 if (!value) goto failed;
3570 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3571 goto failed;
3572 Py_DECREF(value);
3573 value = ast2obj_expr(o->optional_vars);
3574 if (!value) goto failed;
3575 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3576 goto failed;
3577 Py_DECREF(value);
3578 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003579failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003580 Py_XDECREF(value);
3581 Py_XDECREF(result);
3582 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003583}
3584
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003586int
3587obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3588{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003589 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003590
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003591 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003592
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003593 if (obj == Py_None) {
3594 *out = NULL;
3595 return 0;
3596 }
3597 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3598 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003599 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003600 }
3601 if (isinstance) {
3602 asdl_seq* body;
3603
3604 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3605 int res;
3606 Py_ssize_t len;
3607 Py_ssize_t i;
3608 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3609 if (tmp == NULL) goto failed;
3610 if (!PyList_Check(tmp)) {
3611 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3612 goto failed;
3613 }
3614 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003615 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003616 if (body == NULL) goto failed;
3617 for (i = 0; i < len; i++) {
3618 stmt_ty value;
3619 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3620 if (res != 0) goto failed;
3621 asdl_seq_SET(body, i, value);
3622 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003623 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003624 } else {
3625 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3626 return 1;
3627 }
3628 *out = Module(body, arena);
3629 if (*out == NULL) goto failed;
3630 return 0;
3631 }
3632 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3633 if (isinstance == -1) {
3634 return 1;
3635 }
3636 if (isinstance) {
3637 asdl_seq* body;
3638
3639 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3640 int res;
3641 Py_ssize_t len;
3642 Py_ssize_t i;
3643 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3644 if (tmp == NULL) goto failed;
3645 if (!PyList_Check(tmp)) {
3646 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3647 goto failed;
3648 }
3649 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003650 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003651 if (body == NULL) goto failed;
3652 for (i = 0; i < len; i++) {
3653 stmt_ty value;
3654 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3655 if (res != 0) goto failed;
3656 asdl_seq_SET(body, i, value);
3657 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003658 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003659 } else {
3660 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3661 return 1;
3662 }
3663 *out = Interactive(body, arena);
3664 if (*out == NULL) goto failed;
3665 return 0;
3666 }
3667 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3668 if (isinstance == -1) {
3669 return 1;
3670 }
3671 if (isinstance) {
3672 expr_ty body;
3673
3674 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3675 int res;
3676 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3677 if (tmp == NULL) goto failed;
3678 res = obj2ast_expr(tmp, &body, arena);
3679 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003680 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003681 } else {
3682 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3683 return 1;
3684 }
3685 *out = Expression(body, arena);
3686 if (*out == NULL) goto failed;
3687 return 0;
3688 }
3689 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3690 if (isinstance == -1) {
3691 return 1;
3692 }
3693 if (isinstance) {
3694 asdl_seq* body;
3695
3696 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3697 int res;
3698 Py_ssize_t len;
3699 Py_ssize_t i;
3700 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3701 if (tmp == NULL) goto failed;
3702 if (!PyList_Check(tmp)) {
3703 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3704 goto failed;
3705 }
3706 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003707 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003708 if (body == NULL) goto failed;
3709 for (i = 0; i < len; i++) {
3710 stmt_ty value;
3711 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3712 if (res != 0) goto failed;
3713 asdl_seq_SET(body, i, value);
3714 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003715 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003716 } else {
3717 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3718 return 1;
3719 }
3720 *out = Suite(body, arena);
3721 if (*out == NULL) goto failed;
3722 return 0;
3723 }
3724
3725 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3726 failed:
3727 Py_XDECREF(tmp);
3728 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003729}
3730
3731int
3732obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3733{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003734 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003735
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 PyObject *tmp = NULL;
3737 int lineno;
3738 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003739
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003740 if (obj == Py_None) {
3741 *out = NULL;
3742 return 0;
3743 }
3744 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3745 int res;
3746 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3747 if (tmp == NULL) goto failed;
3748 res = obj2ast_int(tmp, &lineno, arena);
3749 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003750 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 } else {
3752 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003753 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003754 }
3755 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3756 int res;
3757 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3758 if (tmp == NULL) goto failed;
3759 res = obj2ast_int(tmp, &col_offset, arena);
3760 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003761 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 } else {
3763 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3764 return 1;
3765 }
3766 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3767 if (isinstance == -1) {
3768 return 1;
3769 }
3770 if (isinstance) {
3771 identifier name;
3772 arguments_ty args;
3773 asdl_seq* body;
3774 asdl_seq* decorator_list;
3775 expr_ty returns;
3776
3777 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3778 int res;
3779 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3780 if (tmp == NULL) goto failed;
3781 res = obj2ast_identifier(tmp, &name, arena);
3782 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003783 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003784 } else {
3785 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3786 return 1;
3787 }
3788 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3789 int res;
3790 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3791 if (tmp == NULL) goto failed;
3792 res = obj2ast_arguments(tmp, &args, arena);
3793 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003794 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003795 } else {
3796 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3797 return 1;
3798 }
3799 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3800 int res;
3801 Py_ssize_t len;
3802 Py_ssize_t i;
3803 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3804 if (tmp == NULL) goto failed;
3805 if (!PyList_Check(tmp)) {
3806 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3807 goto failed;
3808 }
3809 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003810 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003811 if (body == NULL) goto failed;
3812 for (i = 0; i < len; i++) {
3813 stmt_ty value;
3814 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3815 if (res != 0) goto failed;
3816 asdl_seq_SET(body, i, value);
3817 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003818 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 } else {
3820 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3821 return 1;
3822 }
3823 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3824 int res;
3825 Py_ssize_t len;
3826 Py_ssize_t i;
3827 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3828 if (tmp == NULL) goto failed;
3829 if (!PyList_Check(tmp)) {
3830 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3831 goto failed;
3832 }
3833 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003834 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003835 if (decorator_list == NULL) goto failed;
3836 for (i = 0; i < len; i++) {
3837 expr_ty value;
3838 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3839 if (res != 0) goto failed;
3840 asdl_seq_SET(decorator_list, i, value);
3841 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003842 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 } else {
3844 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3845 return 1;
3846 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003847 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003848 int res;
3849 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3850 if (tmp == NULL) goto failed;
3851 res = obj2ast_expr(tmp, &returns, arena);
3852 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003853 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003854 } else {
3855 returns = NULL;
3856 }
3857 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3858 col_offset, arena);
3859 if (*out == NULL) goto failed;
3860 return 0;
3861 }
3862 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3863 if (isinstance == -1) {
3864 return 1;
3865 }
3866 if (isinstance) {
3867 identifier name;
3868 asdl_seq* bases;
3869 asdl_seq* keywords;
3870 expr_ty starargs;
3871 expr_ty kwargs;
3872 asdl_seq* body;
3873 asdl_seq* decorator_list;
3874
3875 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3876 int res;
3877 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3878 if (tmp == NULL) goto failed;
3879 res = obj2ast_identifier(tmp, &name, arena);
3880 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003881 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003882 } else {
3883 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3884 return 1;
3885 }
3886 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3887 int res;
3888 Py_ssize_t len;
3889 Py_ssize_t i;
3890 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3891 if (tmp == NULL) goto failed;
3892 if (!PyList_Check(tmp)) {
3893 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3894 goto failed;
3895 }
3896 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003897 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003898 if (bases == NULL) goto failed;
3899 for (i = 0; i < len; i++) {
3900 expr_ty value;
3901 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3902 if (res != 0) goto failed;
3903 asdl_seq_SET(bases, i, value);
3904 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003905 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003906 } else {
3907 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3908 return 1;
3909 }
3910 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3911 int res;
3912 Py_ssize_t len;
3913 Py_ssize_t i;
3914 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3915 if (tmp == NULL) goto failed;
3916 if (!PyList_Check(tmp)) {
3917 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3918 goto failed;
3919 }
3920 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003921 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003922 if (keywords == NULL) goto failed;
3923 for (i = 0; i < len; i++) {
3924 keyword_ty value;
3925 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3926 if (res != 0) goto failed;
3927 asdl_seq_SET(keywords, i, value);
3928 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003929 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 } else {
3931 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3932 return 1;
3933 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003934 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003935 int res;
3936 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3937 if (tmp == NULL) goto failed;
3938 res = obj2ast_expr(tmp, &starargs, arena);
3939 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003940 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 } else {
3942 starargs = NULL;
3943 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003944 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 int res;
3946 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3947 if (tmp == NULL) goto failed;
3948 res = obj2ast_expr(tmp, &kwargs, arena);
3949 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003950 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003951 } else {
3952 kwargs = NULL;
3953 }
3954 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3955 int res;
3956 Py_ssize_t len;
3957 Py_ssize_t i;
3958 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3959 if (tmp == NULL) goto failed;
3960 if (!PyList_Check(tmp)) {
3961 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3962 goto failed;
3963 }
3964 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003965 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003966 if (body == NULL) goto failed;
3967 for (i = 0; i < len; i++) {
3968 stmt_ty value;
3969 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3970 if (res != 0) goto failed;
3971 asdl_seq_SET(body, i, value);
3972 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003973 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 } else {
3975 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3976 return 1;
3977 }
3978 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3979 int res;
3980 Py_ssize_t len;
3981 Py_ssize_t i;
3982 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3983 if (tmp == NULL) goto failed;
3984 if (!PyList_Check(tmp)) {
3985 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3986 goto failed;
3987 }
3988 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003989 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003990 if (decorator_list == NULL) goto failed;
3991 for (i = 0; i < len; i++) {
3992 expr_ty value;
3993 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3994 if (res != 0) goto failed;
3995 asdl_seq_SET(decorator_list, i, value);
3996 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003997 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003998 } else {
3999 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4000 return 1;
4001 }
4002 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
4003 decorator_list, lineno, col_offset, arena);
4004 if (*out == NULL) goto failed;
4005 return 0;
4006 }
4007 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4008 if (isinstance == -1) {
4009 return 1;
4010 }
4011 if (isinstance) {
4012 expr_ty value;
4013
Victor Stinneree4b59c2013-07-27 00:01:35 +02004014 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004015 int res;
4016 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4017 if (tmp == NULL) goto failed;
4018 res = obj2ast_expr(tmp, &value, arena);
4019 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004020 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004021 } else {
4022 value = NULL;
4023 }
4024 *out = Return(value, lineno, col_offset, arena);
4025 if (*out == NULL) goto failed;
4026 return 0;
4027 }
4028 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4029 if (isinstance == -1) {
4030 return 1;
4031 }
4032 if (isinstance) {
4033 asdl_seq* targets;
4034
4035 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4036 int res;
4037 Py_ssize_t len;
4038 Py_ssize_t i;
4039 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4040 if (tmp == NULL) goto failed;
4041 if (!PyList_Check(tmp)) {
4042 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4043 goto failed;
4044 }
4045 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004046 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004047 if (targets == NULL) goto failed;
4048 for (i = 0; i < len; i++) {
4049 expr_ty value;
4050 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4051 if (res != 0) goto failed;
4052 asdl_seq_SET(targets, i, value);
4053 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004054 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004055 } else {
4056 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4057 return 1;
4058 }
4059 *out = Delete(targets, lineno, col_offset, arena);
4060 if (*out == NULL) goto failed;
4061 return 0;
4062 }
4063 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4064 if (isinstance == -1) {
4065 return 1;
4066 }
4067 if (isinstance) {
4068 asdl_seq* targets;
4069 expr_ty value;
4070
4071 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4072 int res;
4073 Py_ssize_t len;
4074 Py_ssize_t i;
4075 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4076 if (tmp == NULL) goto failed;
4077 if (!PyList_Check(tmp)) {
4078 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4079 goto failed;
4080 }
4081 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004082 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004083 if (targets == NULL) goto failed;
4084 for (i = 0; i < len; i++) {
4085 expr_ty value;
4086 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4087 if (res != 0) goto failed;
4088 asdl_seq_SET(targets, i, value);
4089 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004090 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004091 } else {
4092 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4093 return 1;
4094 }
4095 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4096 int res;
4097 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4098 if (tmp == NULL) goto failed;
4099 res = obj2ast_expr(tmp, &value, arena);
4100 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004101 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004102 } else {
4103 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4104 return 1;
4105 }
4106 *out = Assign(targets, value, lineno, col_offset, arena);
4107 if (*out == NULL) goto failed;
4108 return 0;
4109 }
4110 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4111 if (isinstance == -1) {
4112 return 1;
4113 }
4114 if (isinstance) {
4115 expr_ty target;
4116 operator_ty op;
4117 expr_ty value;
4118
4119 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4120 int res;
4121 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4122 if (tmp == NULL) goto failed;
4123 res = obj2ast_expr(tmp, &target, arena);
4124 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004125 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004126 } else {
4127 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4128 return 1;
4129 }
4130 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4131 int res;
4132 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4133 if (tmp == NULL) goto failed;
4134 res = obj2ast_operator(tmp, &op, arena);
4135 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004136 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004137 } else {
4138 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4139 return 1;
4140 }
4141 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4142 int res;
4143 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4144 if (tmp == NULL) goto failed;
4145 res = obj2ast_expr(tmp, &value, arena);
4146 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004147 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004148 } else {
4149 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4150 return 1;
4151 }
4152 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4153 if (*out == NULL) goto failed;
4154 return 0;
4155 }
4156 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4157 if (isinstance == -1) {
4158 return 1;
4159 }
4160 if (isinstance) {
4161 expr_ty target;
4162 expr_ty iter;
4163 asdl_seq* body;
4164 asdl_seq* orelse;
4165
4166 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4167 int res;
4168 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4169 if (tmp == NULL) goto failed;
4170 res = obj2ast_expr(tmp, &target, arena);
4171 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004172 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004173 } else {
4174 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4175 return 1;
4176 }
4177 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4178 int res;
4179 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4180 if (tmp == NULL) goto failed;
4181 res = obj2ast_expr(tmp, &iter, arena);
4182 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004183 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004184 } else {
4185 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4186 return 1;
4187 }
4188 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4189 int res;
4190 Py_ssize_t len;
4191 Py_ssize_t i;
4192 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4193 if (tmp == NULL) goto failed;
4194 if (!PyList_Check(tmp)) {
4195 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4196 goto failed;
4197 }
4198 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004199 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004200 if (body == NULL) goto failed;
4201 for (i = 0; i < len; i++) {
4202 stmt_ty value;
4203 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4204 if (res != 0) goto failed;
4205 asdl_seq_SET(body, i, value);
4206 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004207 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004208 } else {
4209 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4210 return 1;
4211 }
4212 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4213 int res;
4214 Py_ssize_t len;
4215 Py_ssize_t i;
4216 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4217 if (tmp == NULL) goto failed;
4218 if (!PyList_Check(tmp)) {
4219 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4220 goto failed;
4221 }
4222 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004223 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004224 if (orelse == NULL) goto failed;
4225 for (i = 0; i < len; i++) {
4226 stmt_ty value;
4227 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4228 if (res != 0) goto failed;
4229 asdl_seq_SET(orelse, i, value);
4230 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004231 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004232 } else {
4233 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4234 return 1;
4235 }
4236 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4237 if (*out == NULL) goto failed;
4238 return 0;
4239 }
4240 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4241 if (isinstance == -1) {
4242 return 1;
4243 }
4244 if (isinstance) {
4245 expr_ty test;
4246 asdl_seq* body;
4247 asdl_seq* orelse;
4248
4249 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4250 int res;
4251 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4252 if (tmp == NULL) goto failed;
4253 res = obj2ast_expr(tmp, &test, arena);
4254 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004255 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004256 } else {
4257 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4258 return 1;
4259 }
4260 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4261 int res;
4262 Py_ssize_t len;
4263 Py_ssize_t i;
4264 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4265 if (tmp == NULL) goto failed;
4266 if (!PyList_Check(tmp)) {
4267 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4268 goto failed;
4269 }
4270 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004271 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004272 if (body == NULL) goto failed;
4273 for (i = 0; i < len; i++) {
4274 stmt_ty value;
4275 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4276 if (res != 0) goto failed;
4277 asdl_seq_SET(body, i, value);
4278 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004279 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004280 } else {
4281 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4282 return 1;
4283 }
4284 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4285 int res;
4286 Py_ssize_t len;
4287 Py_ssize_t i;
4288 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4289 if (tmp == NULL) goto failed;
4290 if (!PyList_Check(tmp)) {
4291 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4292 goto failed;
4293 }
4294 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004295 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004296 if (orelse == NULL) goto failed;
4297 for (i = 0; i < len; i++) {
4298 stmt_ty value;
4299 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4300 if (res != 0) goto failed;
4301 asdl_seq_SET(orelse, i, value);
4302 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004303 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004304 } else {
4305 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4306 return 1;
4307 }
4308 *out = While(test, body, orelse, lineno, col_offset, arena);
4309 if (*out == NULL) goto failed;
4310 return 0;
4311 }
4312 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4313 if (isinstance == -1) {
4314 return 1;
4315 }
4316 if (isinstance) {
4317 expr_ty test;
4318 asdl_seq* body;
4319 asdl_seq* orelse;
4320
4321 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4322 int res;
4323 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4324 if (tmp == NULL) goto failed;
4325 res = obj2ast_expr(tmp, &test, arena);
4326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004327 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004328 } else {
4329 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4330 return 1;
4331 }
4332 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4333 int res;
4334 Py_ssize_t len;
4335 Py_ssize_t i;
4336 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4337 if (tmp == NULL) goto failed;
4338 if (!PyList_Check(tmp)) {
4339 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4340 goto failed;
4341 }
4342 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004343 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004344 if (body == NULL) goto failed;
4345 for (i = 0; i < len; i++) {
4346 stmt_ty value;
4347 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4348 if (res != 0) goto failed;
4349 asdl_seq_SET(body, i, value);
4350 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004351 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004352 } else {
4353 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4354 return 1;
4355 }
4356 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4357 int res;
4358 Py_ssize_t len;
4359 Py_ssize_t i;
4360 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4361 if (tmp == NULL) goto failed;
4362 if (!PyList_Check(tmp)) {
4363 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4364 goto failed;
4365 }
4366 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004367 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004368 if (orelse == NULL) goto failed;
4369 for (i = 0; i < len; i++) {
4370 stmt_ty value;
4371 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4372 if (res != 0) goto failed;
4373 asdl_seq_SET(orelse, i, value);
4374 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004375 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004376 } else {
4377 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4378 return 1;
4379 }
4380 *out = If(test, body, orelse, lineno, col_offset, arena);
4381 if (*out == NULL) goto failed;
4382 return 0;
4383 }
4384 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4385 if (isinstance == -1) {
4386 return 1;
4387 }
4388 if (isinstance) {
4389 asdl_seq* items;
4390 asdl_seq* body;
4391
4392 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4393 int res;
4394 Py_ssize_t len;
4395 Py_ssize_t i;
4396 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4397 if (tmp == NULL) goto failed;
4398 if (!PyList_Check(tmp)) {
4399 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4400 goto failed;
4401 }
4402 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004403 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004404 if (items == NULL) goto failed;
4405 for (i = 0; i < len; i++) {
4406 withitem_ty value;
4407 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4408 if (res != 0) goto failed;
4409 asdl_seq_SET(items, i, value);
4410 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004411 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004412 } else {
4413 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4414 return 1;
4415 }
4416 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4417 int res;
4418 Py_ssize_t len;
4419 Py_ssize_t i;
4420 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4421 if (tmp == NULL) goto failed;
4422 if (!PyList_Check(tmp)) {
4423 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4424 goto failed;
4425 }
4426 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004427 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004428 if (body == NULL) goto failed;
4429 for (i = 0; i < len; i++) {
4430 stmt_ty value;
4431 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4432 if (res != 0) goto failed;
4433 asdl_seq_SET(body, i, value);
4434 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004436 } else {
4437 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4438 return 1;
4439 }
4440 *out = With(items, body, lineno, col_offset, arena);
4441 if (*out == NULL) goto failed;
4442 return 0;
4443 }
4444 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4445 if (isinstance == -1) {
4446 return 1;
4447 }
4448 if (isinstance) {
4449 expr_ty exc;
4450 expr_ty cause;
4451
Victor Stinneree4b59c2013-07-27 00:01:35 +02004452 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004453 int res;
4454 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4455 if (tmp == NULL) goto failed;
4456 res = obj2ast_expr(tmp, &exc, arena);
4457 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004458 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004459 } else {
4460 exc = NULL;
4461 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004462 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004463 int res;
4464 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4465 if (tmp == NULL) goto failed;
4466 res = obj2ast_expr(tmp, &cause, arena);
4467 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004468 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004469 } else {
4470 cause = NULL;
4471 }
4472 *out = Raise(exc, cause, lineno, col_offset, arena);
4473 if (*out == NULL) goto failed;
4474 return 0;
4475 }
4476 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4477 if (isinstance == -1) {
4478 return 1;
4479 }
4480 if (isinstance) {
4481 asdl_seq* body;
4482 asdl_seq* handlers;
4483 asdl_seq* orelse;
4484 asdl_seq* finalbody;
4485
4486 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4487 int res;
4488 Py_ssize_t len;
4489 Py_ssize_t i;
4490 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4491 if (tmp == NULL) goto failed;
4492 if (!PyList_Check(tmp)) {
4493 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4494 goto failed;
4495 }
4496 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004497 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004498 if (body == NULL) goto failed;
4499 for (i = 0; i < len; i++) {
4500 stmt_ty value;
4501 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4502 if (res != 0) goto failed;
4503 asdl_seq_SET(body, i, value);
4504 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004505 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004506 } else {
4507 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4508 return 1;
4509 }
4510 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4511 int res;
4512 Py_ssize_t len;
4513 Py_ssize_t i;
4514 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4515 if (tmp == NULL) goto failed;
4516 if (!PyList_Check(tmp)) {
4517 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4518 goto failed;
4519 }
4520 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004521 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004522 if (handlers == NULL) goto failed;
4523 for (i = 0; i < len; i++) {
4524 excepthandler_ty value;
4525 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4526 if (res != 0) goto failed;
4527 asdl_seq_SET(handlers, i, value);
4528 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004529 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004530 } else {
4531 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4532 return 1;
4533 }
4534 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4535 int res;
4536 Py_ssize_t len;
4537 Py_ssize_t i;
4538 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4539 if (tmp == NULL) goto failed;
4540 if (!PyList_Check(tmp)) {
4541 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4542 goto failed;
4543 }
4544 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004545 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004546 if (orelse == NULL) goto failed;
4547 for (i = 0; i < len; i++) {
4548 stmt_ty value;
4549 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4550 if (res != 0) goto failed;
4551 asdl_seq_SET(orelse, i, value);
4552 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004553 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004554 } else {
4555 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4556 return 1;
4557 }
4558 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4559 int res;
4560 Py_ssize_t len;
4561 Py_ssize_t i;
4562 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4563 if (tmp == NULL) goto failed;
4564 if (!PyList_Check(tmp)) {
4565 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4566 goto failed;
4567 }
4568 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004569 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004570 if (finalbody == NULL) goto failed;
4571 for (i = 0; i < len; i++) {
4572 stmt_ty value;
4573 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4574 if (res != 0) goto failed;
4575 asdl_seq_SET(finalbody, i, value);
4576 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004577 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004578 } else {
4579 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4580 return 1;
4581 }
4582 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4583 arena);
4584 if (*out == NULL) goto failed;
4585 return 0;
4586 }
4587 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4588 if (isinstance == -1) {
4589 return 1;
4590 }
4591 if (isinstance) {
4592 expr_ty test;
4593 expr_ty msg;
4594
4595 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4596 int res;
4597 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4598 if (tmp == NULL) goto failed;
4599 res = obj2ast_expr(tmp, &test, arena);
4600 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004601 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004602 } else {
4603 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4604 return 1;
4605 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004606 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004607 int res;
4608 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4609 if (tmp == NULL) goto failed;
4610 res = obj2ast_expr(tmp, &msg, arena);
4611 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004612 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004613 } else {
4614 msg = NULL;
4615 }
4616 *out = Assert(test, msg, lineno, col_offset, arena);
4617 if (*out == NULL) goto failed;
4618 return 0;
4619 }
4620 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4621 if (isinstance == -1) {
4622 return 1;
4623 }
4624 if (isinstance) {
4625 asdl_seq* names;
4626
4627 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4628 int res;
4629 Py_ssize_t len;
4630 Py_ssize_t i;
4631 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4632 if (tmp == NULL) goto failed;
4633 if (!PyList_Check(tmp)) {
4634 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4635 goto failed;
4636 }
4637 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004638 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004639 if (names == NULL) goto failed;
4640 for (i = 0; i < len; i++) {
4641 alias_ty value;
4642 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4643 if (res != 0) goto failed;
4644 asdl_seq_SET(names, i, value);
4645 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004646 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004647 } else {
4648 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4649 return 1;
4650 }
4651 *out = Import(names, lineno, col_offset, arena);
4652 if (*out == NULL) goto failed;
4653 return 0;
4654 }
4655 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4656 if (isinstance == -1) {
4657 return 1;
4658 }
4659 if (isinstance) {
4660 identifier module;
4661 asdl_seq* names;
4662 int level;
4663
Victor Stinneree4b59c2013-07-27 00:01:35 +02004664 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004665 int res;
4666 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4667 if (tmp == NULL) goto failed;
4668 res = obj2ast_identifier(tmp, &module, arena);
4669 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004670 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004671 } else {
4672 module = NULL;
4673 }
4674 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4675 int res;
4676 Py_ssize_t len;
4677 Py_ssize_t i;
4678 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4679 if (tmp == NULL) goto failed;
4680 if (!PyList_Check(tmp)) {
4681 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4682 goto failed;
4683 }
4684 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004685 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004686 if (names == NULL) goto failed;
4687 for (i = 0; i < len; i++) {
4688 alias_ty value;
4689 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4690 if (res != 0) goto failed;
4691 asdl_seq_SET(names, i, value);
4692 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004693 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004694 } else {
4695 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4696 return 1;
4697 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004698 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004699 int res;
4700 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_int(tmp, &level, arena);
4703 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004704 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004705 } else {
4706 level = 0;
4707 }
4708 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4709 if (*out == NULL) goto failed;
4710 return 0;
4711 }
4712 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4713 if (isinstance == -1) {
4714 return 1;
4715 }
4716 if (isinstance) {
4717 asdl_seq* names;
4718
4719 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4720 int res;
4721 Py_ssize_t len;
4722 Py_ssize_t i;
4723 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4724 if (tmp == NULL) goto failed;
4725 if (!PyList_Check(tmp)) {
4726 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4727 goto failed;
4728 }
4729 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004730 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004731 if (names == NULL) goto failed;
4732 for (i = 0; i < len; i++) {
4733 identifier value;
4734 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4735 if (res != 0) goto failed;
4736 asdl_seq_SET(names, i, value);
4737 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004738 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004739 } else {
4740 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4741 return 1;
4742 }
4743 *out = Global(names, lineno, col_offset, arena);
4744 if (*out == NULL) goto failed;
4745 return 0;
4746 }
4747 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4748 if (isinstance == -1) {
4749 return 1;
4750 }
4751 if (isinstance) {
4752 asdl_seq* names;
4753
4754 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4755 int res;
4756 Py_ssize_t len;
4757 Py_ssize_t i;
4758 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4759 if (tmp == NULL) goto failed;
4760 if (!PyList_Check(tmp)) {
4761 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4762 goto failed;
4763 }
4764 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004765 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004766 if (names == NULL) goto failed;
4767 for (i = 0; i < len; i++) {
4768 identifier value;
4769 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4770 if (res != 0) goto failed;
4771 asdl_seq_SET(names, i, value);
4772 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004773 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004774 } else {
4775 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4776 return 1;
4777 }
4778 *out = Nonlocal(names, lineno, col_offset, arena);
4779 if (*out == NULL) goto failed;
4780 return 0;
4781 }
4782 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4783 if (isinstance == -1) {
4784 return 1;
4785 }
4786 if (isinstance) {
4787 expr_ty value;
4788
4789 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4790 int res;
4791 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4792 if (tmp == NULL) goto failed;
4793 res = obj2ast_expr(tmp, &value, arena);
4794 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004795 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004796 } else {
4797 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4798 return 1;
4799 }
4800 *out = Expr(value, lineno, col_offset, arena);
4801 if (*out == NULL) goto failed;
4802 return 0;
4803 }
4804 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4805 if (isinstance == -1) {
4806 return 1;
4807 }
4808 if (isinstance) {
4809
4810 *out = Pass(lineno, col_offset, arena);
4811 if (*out == NULL) goto failed;
4812 return 0;
4813 }
4814 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4815 if (isinstance == -1) {
4816 return 1;
4817 }
4818 if (isinstance) {
4819
4820 *out = Break(lineno, col_offset, arena);
4821 if (*out == NULL) goto failed;
4822 return 0;
4823 }
4824 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4825 if (isinstance == -1) {
4826 return 1;
4827 }
4828 if (isinstance) {
4829
4830 *out = Continue(lineno, col_offset, arena);
4831 if (*out == NULL) goto failed;
4832 return 0;
4833 }
4834
4835 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4836 failed:
4837 Py_XDECREF(tmp);
4838 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004839}
4840
4841int
4842obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4843{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004844 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004845
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004846 PyObject *tmp = NULL;
4847 int lineno;
4848 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004849
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004850 if (obj == Py_None) {
4851 *out = NULL;
4852 return 0;
4853 }
4854 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4855 int res;
4856 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4857 if (tmp == NULL) goto failed;
4858 res = obj2ast_int(tmp, &lineno, arena);
4859 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004860 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004861 } else {
4862 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004863 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004864 }
4865 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4866 int res;
4867 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4868 if (tmp == NULL) goto failed;
4869 res = obj2ast_int(tmp, &col_offset, arena);
4870 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004871 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004872 } else {
4873 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4874 return 1;
4875 }
4876 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4877 if (isinstance == -1) {
4878 return 1;
4879 }
4880 if (isinstance) {
4881 boolop_ty op;
4882 asdl_seq* values;
4883
4884 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4885 int res;
4886 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4887 if (tmp == NULL) goto failed;
4888 res = obj2ast_boolop(tmp, &op, arena);
4889 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004890 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004891 } else {
4892 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4893 return 1;
4894 }
4895 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4896 int res;
4897 Py_ssize_t len;
4898 Py_ssize_t i;
4899 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4900 if (tmp == NULL) goto failed;
4901 if (!PyList_Check(tmp)) {
4902 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4903 goto failed;
4904 }
4905 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004906 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004907 if (values == NULL) goto failed;
4908 for (i = 0; i < len; i++) {
4909 expr_ty value;
4910 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4911 if (res != 0) goto failed;
4912 asdl_seq_SET(values, i, value);
4913 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004914 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004915 } else {
4916 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4917 return 1;
4918 }
4919 *out = BoolOp(op, values, lineno, col_offset, arena);
4920 if (*out == NULL) goto failed;
4921 return 0;
4922 }
4923 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4924 if (isinstance == -1) {
4925 return 1;
4926 }
4927 if (isinstance) {
4928 expr_ty left;
4929 operator_ty op;
4930 expr_ty right;
4931
4932 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4933 int res;
4934 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4935 if (tmp == NULL) goto failed;
4936 res = obj2ast_expr(tmp, &left, arena);
4937 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004938 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004939 } else {
4940 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4941 return 1;
4942 }
4943 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4944 int res;
4945 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4946 if (tmp == NULL) goto failed;
4947 res = obj2ast_operator(tmp, &op, arena);
4948 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004949 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004950 } else {
4951 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4952 return 1;
4953 }
4954 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4955 int res;
4956 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4957 if (tmp == NULL) goto failed;
4958 res = obj2ast_expr(tmp, &right, arena);
4959 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004960 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004961 } else {
4962 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4963 return 1;
4964 }
4965 *out = BinOp(left, op, right, lineno, col_offset, arena);
4966 if (*out == NULL) goto failed;
4967 return 0;
4968 }
4969 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4970 if (isinstance == -1) {
4971 return 1;
4972 }
4973 if (isinstance) {
4974 unaryop_ty op;
4975 expr_ty operand;
4976
4977 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4978 int res;
4979 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4980 if (tmp == NULL) goto failed;
4981 res = obj2ast_unaryop(tmp, &op, arena);
4982 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004983 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004984 } else {
4985 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4986 return 1;
4987 }
4988 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4989 int res;
4990 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4991 if (tmp == NULL) goto failed;
4992 res = obj2ast_expr(tmp, &operand, arena);
4993 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004994 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004995 } else {
4996 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4997 return 1;
4998 }
4999 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5000 if (*out == NULL) goto failed;
5001 return 0;
5002 }
5003 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5004 if (isinstance == -1) {
5005 return 1;
5006 }
5007 if (isinstance) {
5008 arguments_ty args;
5009 expr_ty body;
5010
5011 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5012 int res;
5013 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5014 if (tmp == NULL) goto failed;
5015 res = obj2ast_arguments(tmp, &args, arena);
5016 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005017 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005018 } else {
5019 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5020 return 1;
5021 }
5022 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5023 int res;
5024 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5025 if (tmp == NULL) goto failed;
5026 res = obj2ast_expr(tmp, &body, arena);
5027 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005028 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005029 } else {
5030 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5031 return 1;
5032 }
5033 *out = Lambda(args, body, lineno, col_offset, arena);
5034 if (*out == NULL) goto failed;
5035 return 0;
5036 }
5037 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5038 if (isinstance == -1) {
5039 return 1;
5040 }
5041 if (isinstance) {
5042 expr_ty test;
5043 expr_ty body;
5044 expr_ty orelse;
5045
5046 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5047 int res;
5048 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5049 if (tmp == NULL) goto failed;
5050 res = obj2ast_expr(tmp, &test, arena);
5051 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005052 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005053 } else {
5054 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5055 return 1;
5056 }
5057 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5058 int res;
5059 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5060 if (tmp == NULL) goto failed;
5061 res = obj2ast_expr(tmp, &body, arena);
5062 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005063 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005064 } else {
5065 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5066 return 1;
5067 }
5068 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5069 int res;
5070 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5071 if (tmp == NULL) goto failed;
5072 res = obj2ast_expr(tmp, &orelse, arena);
5073 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005074 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005075 } else {
5076 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5077 return 1;
5078 }
5079 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5080 if (*out == NULL) goto failed;
5081 return 0;
5082 }
5083 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5084 if (isinstance == -1) {
5085 return 1;
5086 }
5087 if (isinstance) {
5088 asdl_seq* keys;
5089 asdl_seq* values;
5090
5091 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5092 int res;
5093 Py_ssize_t len;
5094 Py_ssize_t i;
5095 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5096 if (tmp == NULL) goto failed;
5097 if (!PyList_Check(tmp)) {
5098 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5099 goto failed;
5100 }
5101 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005102 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005103 if (keys == NULL) goto failed;
5104 for (i = 0; i < len; i++) {
5105 expr_ty value;
5106 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5107 if (res != 0) goto failed;
5108 asdl_seq_SET(keys, i, value);
5109 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005110 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005111 } else {
5112 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5113 return 1;
5114 }
5115 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5116 int res;
5117 Py_ssize_t len;
5118 Py_ssize_t i;
5119 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5120 if (tmp == NULL) goto failed;
5121 if (!PyList_Check(tmp)) {
5122 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5123 goto failed;
5124 }
5125 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005126 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005127 if (values == NULL) goto failed;
5128 for (i = 0; i < len; i++) {
5129 expr_ty value;
5130 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5131 if (res != 0) goto failed;
5132 asdl_seq_SET(values, i, value);
5133 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005134 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005135 } else {
5136 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5137 return 1;
5138 }
5139 *out = Dict(keys, values, lineno, col_offset, arena);
5140 if (*out == NULL) goto failed;
5141 return 0;
5142 }
5143 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5144 if (isinstance == -1) {
5145 return 1;
5146 }
5147 if (isinstance) {
5148 asdl_seq* elts;
5149
5150 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5151 int res;
5152 Py_ssize_t len;
5153 Py_ssize_t i;
5154 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5155 if (tmp == NULL) goto failed;
5156 if (!PyList_Check(tmp)) {
5157 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5158 goto failed;
5159 }
5160 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005161 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005162 if (elts == NULL) goto failed;
5163 for (i = 0; i < len; i++) {
5164 expr_ty value;
5165 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5166 if (res != 0) goto failed;
5167 asdl_seq_SET(elts, i, value);
5168 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005169 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005170 } else {
5171 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5172 return 1;
5173 }
5174 *out = Set(elts, lineno, col_offset, arena);
5175 if (*out == NULL) goto failed;
5176 return 0;
5177 }
5178 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5179 if (isinstance == -1) {
5180 return 1;
5181 }
5182 if (isinstance) {
5183 expr_ty elt;
5184 asdl_seq* generators;
5185
5186 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5187 int res;
5188 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5189 if (tmp == NULL) goto failed;
5190 res = obj2ast_expr(tmp, &elt, arena);
5191 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005192 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005193 } else {
5194 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5195 return 1;
5196 }
5197 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5198 int res;
5199 Py_ssize_t len;
5200 Py_ssize_t i;
5201 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5202 if (tmp == NULL) goto failed;
5203 if (!PyList_Check(tmp)) {
5204 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5205 goto failed;
5206 }
5207 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005208 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005209 if (generators == NULL) goto failed;
5210 for (i = 0; i < len; i++) {
5211 comprehension_ty value;
5212 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5213 if (res != 0) goto failed;
5214 asdl_seq_SET(generators, i, value);
5215 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005216 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005217 } else {
5218 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5219 return 1;
5220 }
5221 *out = ListComp(elt, generators, lineno, col_offset, arena);
5222 if (*out == NULL) goto failed;
5223 return 0;
5224 }
5225 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5226 if (isinstance == -1) {
5227 return 1;
5228 }
5229 if (isinstance) {
5230 expr_ty elt;
5231 asdl_seq* generators;
5232
5233 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5234 int res;
5235 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5236 if (tmp == NULL) goto failed;
5237 res = obj2ast_expr(tmp, &elt, arena);
5238 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005239 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005240 } else {
5241 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5242 return 1;
5243 }
5244 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5245 int res;
5246 Py_ssize_t len;
5247 Py_ssize_t i;
5248 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5249 if (tmp == NULL) goto failed;
5250 if (!PyList_Check(tmp)) {
5251 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5252 goto failed;
5253 }
5254 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005255 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005256 if (generators == NULL) goto failed;
5257 for (i = 0; i < len; i++) {
5258 comprehension_ty value;
5259 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5260 if (res != 0) goto failed;
5261 asdl_seq_SET(generators, i, value);
5262 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005263 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005264 } else {
5265 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5266 return 1;
5267 }
5268 *out = SetComp(elt, generators, lineno, col_offset, arena);
5269 if (*out == NULL) goto failed;
5270 return 0;
5271 }
5272 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5273 if (isinstance == -1) {
5274 return 1;
5275 }
5276 if (isinstance) {
5277 expr_ty key;
5278 expr_ty value;
5279 asdl_seq* generators;
5280
5281 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5282 int res;
5283 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5284 if (tmp == NULL) goto failed;
5285 res = obj2ast_expr(tmp, &key, arena);
5286 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005287 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005288 } else {
5289 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5290 return 1;
5291 }
5292 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5293 int res;
5294 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5295 if (tmp == NULL) goto failed;
5296 res = obj2ast_expr(tmp, &value, arena);
5297 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005298 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005299 } else {
5300 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5301 return 1;
5302 }
5303 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5304 int res;
5305 Py_ssize_t len;
5306 Py_ssize_t i;
5307 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5308 if (tmp == NULL) goto failed;
5309 if (!PyList_Check(tmp)) {
5310 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5311 goto failed;
5312 }
5313 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005314 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005315 if (generators == NULL) goto failed;
5316 for (i = 0; i < len; i++) {
5317 comprehension_ty value;
5318 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5319 if (res != 0) goto failed;
5320 asdl_seq_SET(generators, i, value);
5321 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005322 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005323 } else {
5324 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5325 return 1;
5326 }
5327 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5328 if (*out == NULL) goto failed;
5329 return 0;
5330 }
5331 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5332 if (isinstance == -1) {
5333 return 1;
5334 }
5335 if (isinstance) {
5336 expr_ty elt;
5337 asdl_seq* generators;
5338
5339 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5340 int res;
5341 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5342 if (tmp == NULL) goto failed;
5343 res = obj2ast_expr(tmp, &elt, arena);
5344 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005345 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005346 } else {
5347 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5348 return 1;
5349 }
5350 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5351 int res;
5352 Py_ssize_t len;
5353 Py_ssize_t i;
5354 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5355 if (tmp == NULL) goto failed;
5356 if (!PyList_Check(tmp)) {
5357 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5358 goto failed;
5359 }
5360 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005361 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005362 if (generators == NULL) goto failed;
5363 for (i = 0; i < len; i++) {
5364 comprehension_ty value;
5365 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5366 if (res != 0) goto failed;
5367 asdl_seq_SET(generators, i, value);
5368 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005369 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005370 } else {
5371 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5372 return 1;
5373 }
5374 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5375 if (*out == NULL) goto failed;
5376 return 0;
5377 }
5378 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5379 if (isinstance == -1) {
5380 return 1;
5381 }
5382 if (isinstance) {
5383 expr_ty value;
5384
Victor Stinneree4b59c2013-07-27 00:01:35 +02005385 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005386 int res;
5387 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5388 if (tmp == NULL) goto failed;
5389 res = obj2ast_expr(tmp, &value, arena);
5390 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005391 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005392 } else {
5393 value = NULL;
5394 }
5395 *out = Yield(value, lineno, col_offset, arena);
5396 if (*out == NULL) goto failed;
5397 return 0;
5398 }
5399 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5400 if (isinstance == -1) {
5401 return 1;
5402 }
5403 if (isinstance) {
5404 expr_ty value;
5405
5406 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5407 int res;
5408 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5409 if (tmp == NULL) goto failed;
5410 res = obj2ast_expr(tmp, &value, arena);
5411 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005412 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005413 } else {
5414 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5415 return 1;
5416 }
5417 *out = YieldFrom(value, lineno, col_offset, arena);
5418 if (*out == NULL) goto failed;
5419 return 0;
5420 }
5421 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5422 if (isinstance == -1) {
5423 return 1;
5424 }
5425 if (isinstance) {
5426 expr_ty left;
5427 asdl_int_seq* ops;
5428 asdl_seq* comparators;
5429
5430 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5431 int res;
5432 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5433 if (tmp == NULL) goto failed;
5434 res = obj2ast_expr(tmp, &left, arena);
5435 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005436 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005437 } else {
5438 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5439 return 1;
5440 }
5441 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5442 int res;
5443 Py_ssize_t len;
5444 Py_ssize_t i;
5445 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5446 if (tmp == NULL) goto failed;
5447 if (!PyList_Check(tmp)) {
5448 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5449 goto failed;
5450 }
5451 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005452 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005453 if (ops == NULL) goto failed;
5454 for (i = 0; i < len; i++) {
5455 cmpop_ty value;
5456 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5457 if (res != 0) goto failed;
5458 asdl_seq_SET(ops, i, value);
5459 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005460 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005461 } else {
5462 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5463 return 1;
5464 }
5465 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5466 int res;
5467 Py_ssize_t len;
5468 Py_ssize_t i;
5469 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5470 if (tmp == NULL) goto failed;
5471 if (!PyList_Check(tmp)) {
5472 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5473 goto failed;
5474 }
5475 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005476 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005477 if (comparators == NULL) goto failed;
5478 for (i = 0; i < len; i++) {
5479 expr_ty value;
5480 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5481 if (res != 0) goto failed;
5482 asdl_seq_SET(comparators, i, value);
5483 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005484 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005485 } else {
5486 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5487 return 1;
5488 }
5489 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5490 if (*out == NULL) goto failed;
5491 return 0;
5492 }
5493 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5494 if (isinstance == -1) {
5495 return 1;
5496 }
5497 if (isinstance) {
5498 expr_ty func;
5499 asdl_seq* args;
5500 asdl_seq* keywords;
5501 expr_ty starargs;
5502 expr_ty kwargs;
5503
5504 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5505 int res;
5506 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5507 if (tmp == NULL) goto failed;
5508 res = obj2ast_expr(tmp, &func, arena);
5509 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005510 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005511 } else {
5512 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5513 return 1;
5514 }
5515 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5516 int res;
5517 Py_ssize_t len;
5518 Py_ssize_t i;
5519 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5520 if (tmp == NULL) goto failed;
5521 if (!PyList_Check(tmp)) {
5522 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5523 goto failed;
5524 }
5525 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005526 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005527 if (args == NULL) goto failed;
5528 for (i = 0; i < len; i++) {
5529 expr_ty value;
5530 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5531 if (res != 0) goto failed;
5532 asdl_seq_SET(args, i, value);
5533 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005534 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005535 } else {
5536 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5537 return 1;
5538 }
5539 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5540 int res;
5541 Py_ssize_t len;
5542 Py_ssize_t i;
5543 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5544 if (tmp == NULL) goto failed;
5545 if (!PyList_Check(tmp)) {
5546 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5547 goto failed;
5548 }
5549 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005550 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005551 if (keywords == NULL) goto failed;
5552 for (i = 0; i < len; i++) {
5553 keyword_ty value;
5554 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5555 if (res != 0) goto failed;
5556 asdl_seq_SET(keywords, i, value);
5557 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005558 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005559 } else {
5560 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5561 return 1;
5562 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005563 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005564 int res;
5565 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5566 if (tmp == NULL) goto failed;
5567 res = obj2ast_expr(tmp, &starargs, arena);
5568 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005569 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005570 } else {
5571 starargs = NULL;
5572 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005573 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005574 int res;
5575 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5576 if (tmp == NULL) goto failed;
5577 res = obj2ast_expr(tmp, &kwargs, arena);
5578 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005579 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005580 } else {
5581 kwargs = NULL;
5582 }
5583 *out = Call(func, args, keywords, starargs, kwargs, lineno, col_offset,
5584 arena);
5585 if (*out == NULL) goto failed;
5586 return 0;
5587 }
5588 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5589 if (isinstance == -1) {
5590 return 1;
5591 }
5592 if (isinstance) {
5593 object n;
5594
5595 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5596 int res;
5597 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5598 if (tmp == NULL) goto failed;
5599 res = obj2ast_object(tmp, &n, arena);
5600 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005601 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005602 } else {
5603 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5604 return 1;
5605 }
5606 *out = Num(n, lineno, col_offset, arena);
5607 if (*out == NULL) goto failed;
5608 return 0;
5609 }
5610 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5611 if (isinstance == -1) {
5612 return 1;
5613 }
5614 if (isinstance) {
5615 string s;
5616
5617 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5618 int res;
5619 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5620 if (tmp == NULL) goto failed;
5621 res = obj2ast_string(tmp, &s, arena);
5622 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005623 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005624 } else {
5625 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5626 return 1;
5627 }
5628 *out = Str(s, lineno, col_offset, arena);
5629 if (*out == NULL) goto failed;
5630 return 0;
5631 }
5632 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5633 if (isinstance == -1) {
5634 return 1;
5635 }
5636 if (isinstance) {
5637 bytes s;
5638
5639 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5640 int res;
5641 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5642 if (tmp == NULL) goto failed;
5643 res = obj2ast_bytes(tmp, &s, arena);
5644 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005645 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005646 } else {
5647 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5648 return 1;
5649 }
5650 *out = Bytes(s, lineno, col_offset, arena);
5651 if (*out == NULL) goto failed;
5652 return 0;
5653 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005654 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
5655 if (isinstance == -1) {
5656 return 1;
5657 }
5658 if (isinstance) {
5659 singleton value;
5660
5661 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5662 int res;
5663 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5664 if (tmp == NULL) goto failed;
5665 res = obj2ast_singleton(tmp, &value, arena);
5666 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02005667 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02005668 } else {
5669 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
5670 return 1;
5671 }
5672 *out = NameConstant(value, lineno, col_offset, arena);
5673 if (*out == NULL) goto failed;
5674 return 0;
5675 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005676 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5677 if (isinstance == -1) {
5678 return 1;
5679 }
5680 if (isinstance) {
5681
5682 *out = Ellipsis(lineno, col_offset, arena);
5683 if (*out == NULL) goto failed;
5684 return 0;
5685 }
5686 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5687 if (isinstance == -1) {
5688 return 1;
5689 }
5690 if (isinstance) {
5691 expr_ty value;
5692 identifier attr;
5693 expr_context_ty ctx;
5694
5695 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5696 int res;
5697 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5698 if (tmp == NULL) goto failed;
5699 res = obj2ast_expr(tmp, &value, arena);
5700 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005701 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005702 } else {
5703 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5704 return 1;
5705 }
5706 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5707 int res;
5708 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5709 if (tmp == NULL) goto failed;
5710 res = obj2ast_identifier(tmp, &attr, arena);
5711 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005712 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005713 } else {
5714 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5715 return 1;
5716 }
5717 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5718 int res;
5719 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5720 if (tmp == NULL) goto failed;
5721 res = obj2ast_expr_context(tmp, &ctx, arena);
5722 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005723 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005724 } else {
5725 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5726 return 1;
5727 }
5728 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5729 if (*out == NULL) goto failed;
5730 return 0;
5731 }
5732 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5733 if (isinstance == -1) {
5734 return 1;
5735 }
5736 if (isinstance) {
5737 expr_ty value;
5738 slice_ty slice;
5739 expr_context_ty ctx;
5740
5741 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5742 int res;
5743 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5744 if (tmp == NULL) goto failed;
5745 res = obj2ast_expr(tmp, &value, arena);
5746 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005747 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005748 } else {
5749 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5750 return 1;
5751 }
5752 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5753 int res;
5754 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5755 if (tmp == NULL) goto failed;
5756 res = obj2ast_slice(tmp, &slice, arena);
5757 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005758 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005759 } else {
5760 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5761 return 1;
5762 }
5763 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5764 int res;
5765 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5766 if (tmp == NULL) goto failed;
5767 res = obj2ast_expr_context(tmp, &ctx, arena);
5768 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005770 } else {
5771 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5772 return 1;
5773 }
5774 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5775 if (*out == NULL) goto failed;
5776 return 0;
5777 }
5778 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5779 if (isinstance == -1) {
5780 return 1;
5781 }
5782 if (isinstance) {
5783 expr_ty value;
5784 expr_context_ty ctx;
5785
5786 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5787 int res;
5788 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5789 if (tmp == NULL) goto failed;
5790 res = obj2ast_expr(tmp, &value, arena);
5791 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005792 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005793 } else {
5794 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5795 return 1;
5796 }
5797 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5798 int res;
5799 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5800 if (tmp == NULL) goto failed;
5801 res = obj2ast_expr_context(tmp, &ctx, arena);
5802 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005803 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005804 } else {
5805 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5806 return 1;
5807 }
5808 *out = Starred(value, ctx, lineno, col_offset, arena);
5809 if (*out == NULL) goto failed;
5810 return 0;
5811 }
5812 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5813 if (isinstance == -1) {
5814 return 1;
5815 }
5816 if (isinstance) {
5817 identifier id;
5818 expr_context_ty ctx;
5819
5820 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5821 int res;
5822 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5823 if (tmp == NULL) goto failed;
5824 res = obj2ast_identifier(tmp, &id, arena);
5825 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005827 } else {
5828 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5829 return 1;
5830 }
5831 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5832 int res;
5833 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5834 if (tmp == NULL) goto failed;
5835 res = obj2ast_expr_context(tmp, &ctx, arena);
5836 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005837 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005838 } else {
5839 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5840 return 1;
5841 }
5842 *out = Name(id, ctx, lineno, col_offset, arena);
5843 if (*out == NULL) goto failed;
5844 return 0;
5845 }
5846 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5847 if (isinstance == -1) {
5848 return 1;
5849 }
5850 if (isinstance) {
5851 asdl_seq* elts;
5852 expr_context_ty ctx;
5853
5854 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5855 int res;
5856 Py_ssize_t len;
5857 Py_ssize_t i;
5858 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5859 if (tmp == NULL) goto failed;
5860 if (!PyList_Check(tmp)) {
5861 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5862 goto failed;
5863 }
5864 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005865 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005866 if (elts == NULL) goto failed;
5867 for (i = 0; i < len; i++) {
5868 expr_ty value;
5869 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5870 if (res != 0) goto failed;
5871 asdl_seq_SET(elts, i, value);
5872 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005873 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005874 } else {
5875 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5876 return 1;
5877 }
5878 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5879 int res;
5880 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5881 if (tmp == NULL) goto failed;
5882 res = obj2ast_expr_context(tmp, &ctx, arena);
5883 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005884 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005885 } else {
5886 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5887 return 1;
5888 }
5889 *out = List(elts, ctx, lineno, col_offset, arena);
5890 if (*out == NULL) goto failed;
5891 return 0;
5892 }
5893 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5894 if (isinstance == -1) {
5895 return 1;
5896 }
5897 if (isinstance) {
5898 asdl_seq* elts;
5899 expr_context_ty ctx;
5900
5901 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5902 int res;
5903 Py_ssize_t len;
5904 Py_ssize_t i;
5905 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5906 if (tmp == NULL) goto failed;
5907 if (!PyList_Check(tmp)) {
5908 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5909 goto failed;
5910 }
5911 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005912 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005913 if (elts == NULL) goto failed;
5914 for (i = 0; i < len; i++) {
5915 expr_ty value;
5916 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5917 if (res != 0) goto failed;
5918 asdl_seq_SET(elts, i, value);
5919 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005920 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005921 } else {
5922 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5923 return 1;
5924 }
5925 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5926 int res;
5927 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5928 if (tmp == NULL) goto failed;
5929 res = obj2ast_expr_context(tmp, &ctx, arena);
5930 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005931 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005932 } else {
5933 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5934 return 1;
5935 }
5936 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5937 if (*out == NULL) goto failed;
5938 return 0;
5939 }
5940
5941 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5942 failed:
5943 Py_XDECREF(tmp);
5944 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005945}
5946
5947int
5948obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5949{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005950 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005951
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005952 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5953 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005954 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005955 }
5956 if (isinstance) {
5957 *out = Load;
5958 return 0;
5959 }
5960 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5961 if (isinstance == -1) {
5962 return 1;
5963 }
5964 if (isinstance) {
5965 *out = Store;
5966 return 0;
5967 }
5968 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5969 if (isinstance == -1) {
5970 return 1;
5971 }
5972 if (isinstance) {
5973 *out = Del;
5974 return 0;
5975 }
5976 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5977 if (isinstance == -1) {
5978 return 1;
5979 }
5980 if (isinstance) {
5981 *out = AugLoad;
5982 return 0;
5983 }
5984 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5985 if (isinstance == -1) {
5986 return 1;
5987 }
5988 if (isinstance) {
5989 *out = AugStore;
5990 return 0;
5991 }
5992 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5993 if (isinstance == -1) {
5994 return 1;
5995 }
5996 if (isinstance) {
5997 *out = Param;
5998 return 0;
5999 }
6000
6001 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6002 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006003}
6004
6005int
6006obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6007{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006008 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006009
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006010 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006011
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006012 if (obj == Py_None) {
6013 *out = NULL;
6014 return 0;
6015 }
6016 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6017 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006018 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006019 }
6020 if (isinstance) {
6021 expr_ty lower;
6022 expr_ty upper;
6023 expr_ty step;
6024
Victor Stinneree4b59c2013-07-27 00:01:35 +02006025 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006026 int res;
6027 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6028 if (tmp == NULL) goto failed;
6029 res = obj2ast_expr(tmp, &lower, arena);
6030 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006031 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006032 } else {
6033 lower = NULL;
6034 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006035 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 int res;
6037 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6038 if (tmp == NULL) goto failed;
6039 res = obj2ast_expr(tmp, &upper, arena);
6040 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006042 } else {
6043 upper = NULL;
6044 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006045 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006046 int res;
6047 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6048 if (tmp == NULL) goto failed;
6049 res = obj2ast_expr(tmp, &step, arena);
6050 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006051 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006052 } else {
6053 step = NULL;
6054 }
6055 *out = Slice(lower, upper, step, arena);
6056 if (*out == NULL) goto failed;
6057 return 0;
6058 }
6059 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6060 if (isinstance == -1) {
6061 return 1;
6062 }
6063 if (isinstance) {
6064 asdl_seq* dims;
6065
6066 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6067 int res;
6068 Py_ssize_t len;
6069 Py_ssize_t i;
6070 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6071 if (tmp == NULL) goto failed;
6072 if (!PyList_Check(tmp)) {
6073 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6074 goto failed;
6075 }
6076 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006077 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006078 if (dims == NULL) goto failed;
6079 for (i = 0; i < len; i++) {
6080 slice_ty value;
6081 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6082 if (res != 0) goto failed;
6083 asdl_seq_SET(dims, i, value);
6084 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006085 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006086 } else {
6087 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6088 return 1;
6089 }
6090 *out = ExtSlice(dims, arena);
6091 if (*out == NULL) goto failed;
6092 return 0;
6093 }
6094 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6095 if (isinstance == -1) {
6096 return 1;
6097 }
6098 if (isinstance) {
6099 expr_ty value;
6100
6101 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6102 int res;
6103 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6104 if (tmp == NULL) goto failed;
6105 res = obj2ast_expr(tmp, &value, arena);
6106 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006107 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006108 } else {
6109 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6110 return 1;
6111 }
6112 *out = Index(value, arena);
6113 if (*out == NULL) goto failed;
6114 return 0;
6115 }
6116
6117 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6118 failed:
6119 Py_XDECREF(tmp);
6120 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006121}
6122
6123int
6124obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6125{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006126 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006127
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006128 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6129 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006130 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006131 }
6132 if (isinstance) {
6133 *out = And;
6134 return 0;
6135 }
6136 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6137 if (isinstance == -1) {
6138 return 1;
6139 }
6140 if (isinstance) {
6141 *out = Or;
6142 return 0;
6143 }
6144
6145 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6146 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006147}
6148
6149int
6150obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6151{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006152 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006153
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006154 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6155 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006156 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006157 }
6158 if (isinstance) {
6159 *out = Add;
6160 return 0;
6161 }
6162 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6163 if (isinstance == -1) {
6164 return 1;
6165 }
6166 if (isinstance) {
6167 *out = Sub;
6168 return 0;
6169 }
6170 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
6175 *out = Mult;
6176 return 0;
6177 }
6178 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6179 if (isinstance == -1) {
6180 return 1;
6181 }
6182 if (isinstance) {
6183 *out = Div;
6184 return 0;
6185 }
6186 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
6191 *out = Mod;
6192 return 0;
6193 }
6194 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6195 if (isinstance == -1) {
6196 return 1;
6197 }
6198 if (isinstance) {
6199 *out = Pow;
6200 return 0;
6201 }
6202 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6203 if (isinstance == -1) {
6204 return 1;
6205 }
6206 if (isinstance) {
6207 *out = LShift;
6208 return 0;
6209 }
6210 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6211 if (isinstance == -1) {
6212 return 1;
6213 }
6214 if (isinstance) {
6215 *out = RShift;
6216 return 0;
6217 }
6218 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6219 if (isinstance == -1) {
6220 return 1;
6221 }
6222 if (isinstance) {
6223 *out = BitOr;
6224 return 0;
6225 }
6226 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6227 if (isinstance == -1) {
6228 return 1;
6229 }
6230 if (isinstance) {
6231 *out = BitXor;
6232 return 0;
6233 }
6234 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6235 if (isinstance == -1) {
6236 return 1;
6237 }
6238 if (isinstance) {
6239 *out = BitAnd;
6240 return 0;
6241 }
6242 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6243 if (isinstance == -1) {
6244 return 1;
6245 }
6246 if (isinstance) {
6247 *out = FloorDiv;
6248 return 0;
6249 }
6250
6251 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6252 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006253}
6254
6255int
6256obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6257{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006258 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006259
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006260 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6261 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006262 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006263 }
6264 if (isinstance) {
6265 *out = Invert;
6266 return 0;
6267 }
6268 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6269 if (isinstance == -1) {
6270 return 1;
6271 }
6272 if (isinstance) {
6273 *out = Not;
6274 return 0;
6275 }
6276 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6277 if (isinstance == -1) {
6278 return 1;
6279 }
6280 if (isinstance) {
6281 *out = UAdd;
6282 return 0;
6283 }
6284 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6285 if (isinstance == -1) {
6286 return 1;
6287 }
6288 if (isinstance) {
6289 *out = USub;
6290 return 0;
6291 }
6292
6293 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6294 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006295}
6296
6297int
6298obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6299{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006300 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006301
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006302 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6303 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006304 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006305 }
6306 if (isinstance) {
6307 *out = Eq;
6308 return 0;
6309 }
6310 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6311 if (isinstance == -1) {
6312 return 1;
6313 }
6314 if (isinstance) {
6315 *out = NotEq;
6316 return 0;
6317 }
6318 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6319 if (isinstance == -1) {
6320 return 1;
6321 }
6322 if (isinstance) {
6323 *out = Lt;
6324 return 0;
6325 }
6326 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6327 if (isinstance == -1) {
6328 return 1;
6329 }
6330 if (isinstance) {
6331 *out = LtE;
6332 return 0;
6333 }
6334 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6335 if (isinstance == -1) {
6336 return 1;
6337 }
6338 if (isinstance) {
6339 *out = Gt;
6340 return 0;
6341 }
6342 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6343 if (isinstance == -1) {
6344 return 1;
6345 }
6346 if (isinstance) {
6347 *out = GtE;
6348 return 0;
6349 }
6350 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6351 if (isinstance == -1) {
6352 return 1;
6353 }
6354 if (isinstance) {
6355 *out = Is;
6356 return 0;
6357 }
6358 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6359 if (isinstance == -1) {
6360 return 1;
6361 }
6362 if (isinstance) {
6363 *out = IsNot;
6364 return 0;
6365 }
6366 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6367 if (isinstance == -1) {
6368 return 1;
6369 }
6370 if (isinstance) {
6371 *out = In;
6372 return 0;
6373 }
6374 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6375 if (isinstance == -1) {
6376 return 1;
6377 }
6378 if (isinstance) {
6379 *out = NotIn;
6380 return 0;
6381 }
6382
6383 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6384 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006385}
6386
6387int
6388obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6389{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006390 PyObject* tmp = NULL;
6391 expr_ty target;
6392 expr_ty iter;
6393 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006394
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006395 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6396 int res;
6397 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6398 if (tmp == NULL) goto failed;
6399 res = obj2ast_expr(tmp, &target, arena);
6400 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006401 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006402 } else {
6403 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006404 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006405 }
6406 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6407 int res;
6408 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6409 if (tmp == NULL) goto failed;
6410 res = obj2ast_expr(tmp, &iter, arena);
6411 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006412 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006413 } else {
6414 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6415 return 1;
6416 }
6417 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6418 int res;
6419 Py_ssize_t len;
6420 Py_ssize_t i;
6421 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6422 if (tmp == NULL) goto failed;
6423 if (!PyList_Check(tmp)) {
6424 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6425 goto failed;
6426 }
6427 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006428 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006429 if (ifs == NULL) goto failed;
6430 for (i = 0; i < len; i++) {
6431 expr_ty value;
6432 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6433 if (res != 0) goto failed;
6434 asdl_seq_SET(ifs, i, value);
6435 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006436 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006437 } else {
6438 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6439 return 1;
6440 }
6441 *out = comprehension(target, iter, ifs, arena);
6442 return 0;
6443failed:
6444 Py_XDECREF(tmp);
6445 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006446}
6447
6448int
6449obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6450{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006451 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006452
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006453 PyObject *tmp = NULL;
6454 int lineno;
6455 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006456
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006457 if (obj == Py_None) {
6458 *out = NULL;
6459 return 0;
6460 }
6461 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6462 int res;
6463 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6464 if (tmp == NULL) goto failed;
6465 res = obj2ast_int(tmp, &lineno, arena);
6466 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006467 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006468 } else {
6469 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006470 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006471 }
6472 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6473 int res;
6474 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6475 if (tmp == NULL) goto failed;
6476 res = obj2ast_int(tmp, &col_offset, arena);
6477 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006478 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006479 } else {
6480 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6481 return 1;
6482 }
6483 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6484 if (isinstance == -1) {
6485 return 1;
6486 }
6487 if (isinstance) {
6488 expr_ty type;
6489 identifier name;
6490 asdl_seq* body;
6491
Victor Stinneree4b59c2013-07-27 00:01:35 +02006492 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006493 int res;
6494 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6495 if (tmp == NULL) goto failed;
6496 res = obj2ast_expr(tmp, &type, arena);
6497 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006498 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006499 } else {
6500 type = NULL;
6501 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006502 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006503 int res;
6504 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6505 if (tmp == NULL) goto failed;
6506 res = obj2ast_identifier(tmp, &name, arena);
6507 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006508 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006509 } else {
6510 name = NULL;
6511 }
6512 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6513 int res;
6514 Py_ssize_t len;
6515 Py_ssize_t i;
6516 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6517 if (tmp == NULL) goto failed;
6518 if (!PyList_Check(tmp)) {
6519 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6520 goto failed;
6521 }
6522 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006523 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006524 if (body == NULL) goto failed;
6525 for (i = 0; i < len; i++) {
6526 stmt_ty value;
6527 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6528 if (res != 0) goto failed;
6529 asdl_seq_SET(body, i, value);
6530 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006531 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006532 } else {
6533 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6534 return 1;
6535 }
6536 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6537 if (*out == NULL) goto failed;
6538 return 0;
6539 }
6540
6541 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6542 failed:
6543 Py_XDECREF(tmp);
6544 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006545}
6546
6547int
6548obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6549{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 PyObject* tmp = NULL;
6551 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006552 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006553 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006554 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006555 arg_ty kwarg;
6556 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006557
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006558 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6559 int res;
6560 Py_ssize_t len;
6561 Py_ssize_t i;
6562 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6563 if (tmp == NULL) goto failed;
6564 if (!PyList_Check(tmp)) {
6565 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6566 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006567 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006568 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006569 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006570 if (args == NULL) goto failed;
6571 for (i = 0; i < len; i++) {
6572 arg_ty value;
6573 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6574 if (res != 0) goto failed;
6575 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006576 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006577 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006578 } else {
6579 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006580 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006581 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006582 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 int res;
6584 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6585 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006586 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006587 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006588 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 } else {
6590 vararg = NULL;
6591 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006592 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6593 int res;
6594 Py_ssize_t len;
6595 Py_ssize_t i;
6596 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6597 if (tmp == NULL) goto failed;
6598 if (!PyList_Check(tmp)) {
6599 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6600 goto failed;
6601 }
6602 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006603 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006604 if (kwonlyargs == NULL) goto failed;
6605 for (i = 0; i < len; i++) {
6606 arg_ty value;
6607 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6608 if (res != 0) goto failed;
6609 asdl_seq_SET(kwonlyargs, i, value);
6610 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006611 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006612 } else {
6613 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6614 return 1;
6615 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006616 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6617 int res;
6618 Py_ssize_t len;
6619 Py_ssize_t i;
6620 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6621 if (tmp == NULL) goto failed;
6622 if (!PyList_Check(tmp)) {
6623 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6624 goto failed;
6625 }
6626 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006627 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006628 if (kw_defaults == NULL) goto failed;
6629 for (i = 0; i < len; i++) {
6630 expr_ty value;
6631 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6632 if (res != 0) goto failed;
6633 asdl_seq_SET(kw_defaults, i, value);
6634 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006635 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006636 } else {
6637 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6638 return 1;
6639 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006640 if (exists_not_none(obj, &PyId_kwarg)) {
6641 int res;
6642 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6643 if (tmp == NULL) goto failed;
6644 res = obj2ast_arg(tmp, &kwarg, arena);
6645 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006646 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006647 } else {
6648 kwarg = NULL;
6649 }
6650 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6651 int res;
6652 Py_ssize_t len;
6653 Py_ssize_t i;
6654 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6655 if (tmp == NULL) goto failed;
6656 if (!PyList_Check(tmp)) {
6657 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6658 goto failed;
6659 }
6660 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006661 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006662 if (defaults == NULL) goto failed;
6663 for (i = 0; i < len; i++) {
6664 expr_ty value;
6665 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6666 if (res != 0) goto failed;
6667 asdl_seq_SET(defaults, i, value);
6668 }
Victor Stinnerb3189902013-07-27 00:04:42 +02006669 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006670 } else {
6671 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6672 return 1;
6673 }
6674 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
6675 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006676 return 0;
6677failed:
6678 Py_XDECREF(tmp);
6679 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006680}
6681
6682int
6683obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6684{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006685 PyObject* tmp = NULL;
6686 identifier arg;
6687 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006688
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006689 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6690 int res;
6691 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6692 if (tmp == NULL) goto failed;
6693 res = obj2ast_identifier(tmp, &arg, arena);
6694 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006696 } else {
6697 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006698 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006699 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006700 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006701 int res;
6702 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6703 if (tmp == NULL) goto failed;
6704 res = obj2ast_expr(tmp, &annotation, arena);
6705 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006706 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006707 } else {
6708 annotation = NULL;
6709 }
6710 *out = arg(arg, annotation, arena);
6711 return 0;
6712failed:
6713 Py_XDECREF(tmp);
6714 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006715}
6716
6717int
6718obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6719{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006720 PyObject* tmp = NULL;
6721 identifier arg;
6722 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006723
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006724 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6725 int res;
6726 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6727 if (tmp == NULL) goto failed;
6728 res = obj2ast_identifier(tmp, &arg, arena);
6729 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006730 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006731 } else {
6732 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006733 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006734 }
6735 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6736 int res;
6737 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6738 if (tmp == NULL) goto failed;
6739 res = obj2ast_expr(tmp, &value, arena);
6740 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006741 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006742 } else {
6743 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6744 return 1;
6745 }
6746 *out = keyword(arg, value, arena);
6747 return 0;
6748failed:
6749 Py_XDECREF(tmp);
6750 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006751}
6752
6753int
6754obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6755{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006756 PyObject* tmp = NULL;
6757 identifier name;
6758 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006759
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006760 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6761 int res;
6762 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6763 if (tmp == NULL) goto failed;
6764 res = obj2ast_identifier(tmp, &name, arena);
6765 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006767 } else {
6768 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006769 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006770 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006771 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006772 int res;
6773 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6774 if (tmp == NULL) goto failed;
6775 res = obj2ast_identifier(tmp, &asname, arena);
6776 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006777 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006778 } else {
6779 asname = NULL;
6780 }
6781 *out = alias(name, asname, arena);
6782 return 0;
6783failed:
6784 Py_XDECREF(tmp);
6785 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006786}
6787
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006788int
6789obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6790{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006791 PyObject* tmp = NULL;
6792 expr_ty context_expr;
6793 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006794
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006795 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6796 int res;
6797 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6798 if (tmp == NULL) goto failed;
6799 res = obj2ast_expr(tmp, &context_expr, arena);
6800 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006801 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006802 } else {
6803 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006804 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006805 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006806 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006807 int res;
6808 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6809 if (tmp == NULL) goto failed;
6810 res = obj2ast_expr(tmp, &optional_vars, arena);
6811 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006812 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006813 } else {
6814 optional_vars = NULL;
6815 }
6816 *out = withitem(context_expr, optional_vars, arena);
6817 return 0;
6818failed:
6819 Py_XDECREF(tmp);
6820 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006821}
6822
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006823
Martin v. Löwis1a214512008-06-11 05:26:20 +00006824static struct PyModuleDef _astmodule = {
6825 PyModuleDef_HEAD_INIT, "_ast"
6826};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006827PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006828PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006829{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006830 PyObject *m, *d;
6831 if (!init_types()) return NULL;
6832 m = PyModule_Create(&_astmodule);
6833 if (!m) return NULL;
6834 d = PyModule_GetDict(m);
6835 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006836 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006837 return NULL;
6838 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6839 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6840 NULL;
6841 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6842 0) return NULL;
6843 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6844 return NULL;
6845 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6846 NULL;
6847 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6848 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6849 0) return NULL;
6850 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6851 return NULL;
6852 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6853 NULL;
6854 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6855 NULL;
6856 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6857 NULL;
6858 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6859 return NULL;
6860 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6861 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6862 NULL;
6863 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6864 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6865 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6866 NULL;
6867 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6868 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6873 return NULL;
6874 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6875 NULL;
6876 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6877 return NULL;
6878 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6879 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6880 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6881 NULL;
6882 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6883 return NULL;
6884 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6885 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6886 NULL;
6887 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6888 NULL;
6889 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6890 NULL;
6891 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6892 NULL;
6893 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6894 NULL;
6895 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6896 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6897 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6898 return NULL;
6899 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6902 return NULL;
6903 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6904 0) return NULL;
6905 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6908 return NULL;
6909 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6910 NULL;
6911 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6912 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6913 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6914 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6915 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006916 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
6917 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006918 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6919 return NULL;
6920 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6921 return NULL;
6922 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6923 return NULL;
6924 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6925 NULL;
6926 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6927 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6928 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6929 NULL;
6930 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6931 0) return NULL;
6932 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6933 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6934 NULL;
6935 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
6936 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
6937 NULL;
6938 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6939 return NULL;
6940 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6941 NULL;
6942 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6943 NULL;
6944 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6945 NULL;
6946 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6947 return NULL;
6948 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6949 NULL;
6950 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
6951 NULL;
6952 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
6953 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
6954 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6955 return NULL;
6956 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
6957 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
6958 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
6959 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
6960 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
6961 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
6962 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
6963 NULL;
6964 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
6965 NULL;
6966 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6967 NULL;
6968 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
6969 NULL;
6970 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
6971 NULL;
6972 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6973 return NULL;
6974 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
6975 NULL;
6976 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
6977 NULL;
6978 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
6979 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
6980 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
6981 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6982 NULL;
6983 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6984 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6985 NULL;
6986 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6987 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
6988 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6989 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
6990 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6991 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6992 NULL;
6993 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6994 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6995 NULL;
6996 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
6997 < 0) return NULL;
6998 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
6999 < 0) return NULL;
7000 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7001 < 0) return NULL;
7002 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7003 return NULL;
7004 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7005 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7006 NULL;
7007 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7008 NULL;
7009 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7010 return NULL;
7011 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007012}
7013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007014
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007015PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007016{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007017 if (!init_types())
7018 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007019 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007020}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007021
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007022/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7023mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007024{
7025 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007026 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007027 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007028 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007029
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007030 req_type[0] = (PyObject*)Module_type;
7031 req_type[1] = (PyObject*)Expression_type;
7032 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007033
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007034 assert(0 <= mode && mode <= 2);
7035
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007036 if (!init_types())
7037 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007038
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007039 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7040 if (isinstance == -1)
7041 return NULL;
7042 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007043 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7044 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007045 return NULL;
7046 }
7047 if (obj2ast_mod(ast, &res, arena) != 0)
7048 return NULL;
7049 else
7050 return res;
7051}
7052
7053int PyAST_Check(PyObject* obj)
7054{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007055 if (!init_types())
7056 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007057 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007058}
7059
Martin v. Löwis5b222132007-06-10 09:51:05 +00007060