blob: 49d19dacd6ec542c9d348a943c4baccaf8734ac9 [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;
707 if (obj)
708 PyArena_AddPyObject(arena, obj);
709 Py_XINCREF(obj);
710 *out = obj;
711 return 0;
712}
713
Benjamin Peterson180e6352011-07-22 11:09:07 -0500714static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500715{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500716 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
717 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500718 return 1;
719 }
720 return obj2ast_object(obj, out, arena);
721}
722
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500723static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
724{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400725 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500726 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
727 return 1;
728 }
729 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500730}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000731
Benjamin Petersone2498412011-08-09 16:08:39 -0500732static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
733{
734 if (!PyBytes_CheckExact(obj)) {
735 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
736 return 1;
737 }
738 return obj2ast_object(obj, out, arena);
739}
740
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000741static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
742{
743 int i;
744 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100745 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000746 return 1;
747 }
748
749 i = (int)PyLong_AsLong(obj);
750 if (i == -1 && PyErr_Occurred())
751 return 1;
752 *out = i;
753 return 0;
754}
755
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000756static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000757{
758 PyObject *empty_tuple, *d;
759 if (PyType_Ready(&AST_type) < 0)
760 return -1;
761 d = AST_type.tp_dict;
762 empty_tuple = PyTuple_New(0);
763 if (!empty_tuple ||
764 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
765 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
766 Py_XDECREF(empty_tuple);
767 return -1;
768 }
769 Py_DECREF(empty_tuple);
770 return 0;
771}
772
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700773static int exists_not_none(PyObject *obj, _Py_Identifier *id)
774{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700775 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700776 PyObject *attr = _PyObject_GetAttrId(obj, id);
777 if (!attr) {
778 PyErr_Clear();
779 return 0;
780 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700781 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700782 Py_DECREF(attr);
783 return !isnone;
784}
785
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000786
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787static int init_types(void)
788{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200789 static int initialized;
790 if (initialized) return 1;
791 if (add_ast_fields() < 0) return 0;
792 mod_type = make_type("mod", &AST_type, NULL, 0);
793 if (!mod_type) return 0;
794 if (!add_attributes(mod_type, NULL, 0)) return 0;
795 Module_type = make_type("Module", mod_type, Module_fields, 1);
796 if (!Module_type) return 0;
797 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
798 1);
799 if (!Interactive_type) return 0;
800 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
801 if (!Expression_type) return 0;
802 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
803 if (!Suite_type) return 0;
804 stmt_type = make_type("stmt", &AST_type, NULL, 0);
805 if (!stmt_type) return 0;
806 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
807 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
808 5);
809 if (!FunctionDef_type) return 0;
810 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
811 if (!ClassDef_type) return 0;
812 Return_type = make_type("Return", stmt_type, Return_fields, 1);
813 if (!Return_type) return 0;
814 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
815 if (!Delete_type) return 0;
816 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
817 if (!Assign_type) return 0;
818 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
819 if (!AugAssign_type) return 0;
820 For_type = make_type("For", stmt_type, For_fields, 4);
821 if (!For_type) return 0;
822 While_type = make_type("While", stmt_type, While_fields, 3);
823 if (!While_type) return 0;
824 If_type = make_type("If", stmt_type, If_fields, 3);
825 if (!If_type) return 0;
826 With_type = make_type("With", stmt_type, With_fields, 2);
827 if (!With_type) return 0;
828 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
829 if (!Raise_type) return 0;
830 Try_type = make_type("Try", stmt_type, Try_fields, 4);
831 if (!Try_type) return 0;
832 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
833 if (!Assert_type) return 0;
834 Import_type = make_type("Import", stmt_type, Import_fields, 1);
835 if (!Import_type) return 0;
836 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
837 if (!ImportFrom_type) return 0;
838 Global_type = make_type("Global", stmt_type, Global_fields, 1);
839 if (!Global_type) return 0;
840 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
841 if (!Nonlocal_type) return 0;
842 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
843 if (!Expr_type) return 0;
844 Pass_type = make_type("Pass", stmt_type, NULL, 0);
845 if (!Pass_type) return 0;
846 Break_type = make_type("Break", stmt_type, NULL, 0);
847 if (!Break_type) return 0;
848 Continue_type = make_type("Continue", stmt_type, NULL, 0);
849 if (!Continue_type) return 0;
850 expr_type = make_type("expr", &AST_type, NULL, 0);
851 if (!expr_type) return 0;
852 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
853 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
854 if (!BoolOp_type) return 0;
855 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
856 if (!BinOp_type) return 0;
857 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
858 if (!UnaryOp_type) return 0;
859 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
860 if (!Lambda_type) return 0;
861 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
862 if (!IfExp_type) return 0;
863 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
864 if (!Dict_type) return 0;
865 Set_type = make_type("Set", expr_type, Set_fields, 1);
866 if (!Set_type) return 0;
867 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
868 if (!ListComp_type) return 0;
869 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
870 if (!SetComp_type) return 0;
871 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
872 if (!DictComp_type) return 0;
873 GeneratorExp_type = make_type("GeneratorExp", expr_type,
874 GeneratorExp_fields, 2);
875 if (!GeneratorExp_type) return 0;
876 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
877 if (!Yield_type) return 0;
878 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
879 if (!YieldFrom_type) return 0;
880 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
881 if (!Compare_type) return 0;
882 Call_type = make_type("Call", expr_type, Call_fields, 5);
883 if (!Call_type) return 0;
884 Num_type = make_type("Num", expr_type, Num_fields, 1);
885 if (!Num_type) return 0;
886 Str_type = make_type("Str", expr_type, Str_fields, 1);
887 if (!Str_type) return 0;
888 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
889 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200890 NameConstant_type = make_type("NameConstant", expr_type,
891 NameConstant_fields, 1);
892 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200893 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
894 if (!Ellipsis_type) return 0;
895 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
896 if (!Attribute_type) return 0;
897 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
898 if (!Subscript_type) return 0;
899 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
900 if (!Starred_type) return 0;
901 Name_type = make_type("Name", expr_type, Name_fields, 2);
902 if (!Name_type) return 0;
903 List_type = make_type("List", expr_type, List_fields, 2);
904 if (!List_type) return 0;
905 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
906 if (!Tuple_type) return 0;
907 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
908 if (!expr_context_type) return 0;
909 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
910 Load_type = make_type("Load", expr_context_type, NULL, 0);
911 if (!Load_type) return 0;
912 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
913 if (!Load_singleton) return 0;
914 Store_type = make_type("Store", expr_context_type, NULL, 0);
915 if (!Store_type) return 0;
916 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
917 if (!Store_singleton) return 0;
918 Del_type = make_type("Del", expr_context_type, NULL, 0);
919 if (!Del_type) return 0;
920 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
921 if (!Del_singleton) return 0;
922 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
923 if (!AugLoad_type) return 0;
924 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
925 if (!AugLoad_singleton) return 0;
926 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
927 if (!AugStore_type) return 0;
928 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
929 if (!AugStore_singleton) return 0;
930 Param_type = make_type("Param", expr_context_type, NULL, 0);
931 if (!Param_type) return 0;
932 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
933 if (!Param_singleton) return 0;
934 slice_type = make_type("slice", &AST_type, NULL, 0);
935 if (!slice_type) return 0;
936 if (!add_attributes(slice_type, NULL, 0)) return 0;
937 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
938 if (!Slice_type) return 0;
939 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
940 if (!ExtSlice_type) return 0;
941 Index_type = make_type("Index", slice_type, Index_fields, 1);
942 if (!Index_type) return 0;
943 boolop_type = make_type("boolop", &AST_type, NULL, 0);
944 if (!boolop_type) return 0;
945 if (!add_attributes(boolop_type, NULL, 0)) return 0;
946 And_type = make_type("And", boolop_type, NULL, 0);
947 if (!And_type) return 0;
948 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
949 if (!And_singleton) return 0;
950 Or_type = make_type("Or", boolop_type, NULL, 0);
951 if (!Or_type) return 0;
952 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
953 if (!Or_singleton) return 0;
954 operator_type = make_type("operator", &AST_type, NULL, 0);
955 if (!operator_type) return 0;
956 if (!add_attributes(operator_type, NULL, 0)) return 0;
957 Add_type = make_type("Add", operator_type, NULL, 0);
958 if (!Add_type) return 0;
959 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
960 if (!Add_singleton) return 0;
961 Sub_type = make_type("Sub", operator_type, NULL, 0);
962 if (!Sub_type) return 0;
963 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
964 if (!Sub_singleton) return 0;
965 Mult_type = make_type("Mult", operator_type, NULL, 0);
966 if (!Mult_type) return 0;
967 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
968 if (!Mult_singleton) return 0;
969 Div_type = make_type("Div", operator_type, NULL, 0);
970 if (!Div_type) return 0;
971 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
972 if (!Div_singleton) return 0;
973 Mod_type = make_type("Mod", operator_type, NULL, 0);
974 if (!Mod_type) return 0;
975 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
976 if (!Mod_singleton) return 0;
977 Pow_type = make_type("Pow", operator_type, NULL, 0);
978 if (!Pow_type) return 0;
979 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
980 if (!Pow_singleton) return 0;
981 LShift_type = make_type("LShift", operator_type, NULL, 0);
982 if (!LShift_type) return 0;
983 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
984 if (!LShift_singleton) return 0;
985 RShift_type = make_type("RShift", operator_type, NULL, 0);
986 if (!RShift_type) return 0;
987 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
988 if (!RShift_singleton) return 0;
989 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
990 if (!BitOr_type) return 0;
991 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
992 if (!BitOr_singleton) return 0;
993 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
994 if (!BitXor_type) return 0;
995 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
996 if (!BitXor_singleton) return 0;
997 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
998 if (!BitAnd_type) return 0;
999 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1000 if (!BitAnd_singleton) return 0;
1001 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1002 if (!FloorDiv_type) return 0;
1003 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1004 if (!FloorDiv_singleton) return 0;
1005 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1006 if (!unaryop_type) return 0;
1007 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1008 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1009 if (!Invert_type) return 0;
1010 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1011 if (!Invert_singleton) return 0;
1012 Not_type = make_type("Not", unaryop_type, NULL, 0);
1013 if (!Not_type) return 0;
1014 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1015 if (!Not_singleton) return 0;
1016 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1017 if (!UAdd_type) return 0;
1018 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1019 if (!UAdd_singleton) return 0;
1020 USub_type = make_type("USub", unaryop_type, NULL, 0);
1021 if (!USub_type) return 0;
1022 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1023 if (!USub_singleton) return 0;
1024 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1025 if (!cmpop_type) return 0;
1026 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1027 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1028 if (!Eq_type) return 0;
1029 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1030 if (!Eq_singleton) return 0;
1031 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1032 if (!NotEq_type) return 0;
1033 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1034 if (!NotEq_singleton) return 0;
1035 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1036 if (!Lt_type) return 0;
1037 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1038 if (!Lt_singleton) return 0;
1039 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1040 if (!LtE_type) return 0;
1041 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1042 if (!LtE_singleton) return 0;
1043 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1044 if (!Gt_type) return 0;
1045 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1046 if (!Gt_singleton) return 0;
1047 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1048 if (!GtE_type) return 0;
1049 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1050 if (!GtE_singleton) return 0;
1051 Is_type = make_type("Is", cmpop_type, NULL, 0);
1052 if (!Is_type) return 0;
1053 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1054 if (!Is_singleton) return 0;
1055 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1056 if (!IsNot_type) return 0;
1057 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1058 if (!IsNot_singleton) return 0;
1059 In_type = make_type("In", cmpop_type, NULL, 0);
1060 if (!In_type) return 0;
1061 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1062 if (!In_singleton) return 0;
1063 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1064 if (!NotIn_type) return 0;
1065 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1066 if (!NotIn_singleton) return 0;
1067 comprehension_type = make_type("comprehension", &AST_type,
1068 comprehension_fields, 3);
1069 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001070 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001071 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1072 if (!excepthandler_type) return 0;
1073 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1074 return 0;
1075 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1076 ExceptHandler_fields, 3);
1077 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001078 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001079 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001080 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001081 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1082 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001083 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001084 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1085 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001086 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001087 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1088 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001089 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001090 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1091 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001092 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001093 initialized = 1;
1094 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001095}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001096
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001097static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1098static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1099static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1100static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1101 arena);
1102static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1103static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1104static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1105static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1106static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1107static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1108 arena);
1109static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1110 arena);
1111static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1112static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1113static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1114static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001115static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001118Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001120 mod_ty p;
1121 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1122 if (!p)
1123 return NULL;
1124 p->kind = Module_kind;
1125 p->v.Module.body = body;
1126 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127}
1128
1129mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001132 mod_ty p;
1133 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1134 if (!p)
1135 return NULL;
1136 p->kind = Interactive_kind;
1137 p->v.Interactive.body = body;
1138 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139}
1140
1141mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001144 mod_ty p;
1145 if (!body) {
1146 PyErr_SetString(PyExc_ValueError,
1147 "field body is required for Expression");
1148 return NULL;
1149 }
1150 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1151 if (!p)
1152 return NULL;
1153 p->kind = Expression_kind;
1154 p->v.Expression.body = body;
1155 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156}
1157
1158mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001161 mod_ty p;
1162 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1163 if (!p)
1164 return NULL;
1165 p->kind = Suite_kind;
1166 p->v.Suite.body = body;
1167 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168}
1169
1170stmt_ty
1171FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001172 decorator_list, expr_ty returns, int lineno, int col_offset,
1173 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 stmt_ty p;
1176 if (!name) {
1177 PyErr_SetString(PyExc_ValueError,
1178 "field name is required for FunctionDef");
1179 return NULL;
1180 }
1181 if (!args) {
1182 PyErr_SetString(PyExc_ValueError,
1183 "field args is required for FunctionDef");
1184 return NULL;
1185 }
1186 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1187 if (!p)
1188 return NULL;
1189 p->kind = FunctionDef_kind;
1190 p->v.FunctionDef.name = name;
1191 p->v.FunctionDef.args = args;
1192 p->v.FunctionDef.body = body;
1193 p->v.FunctionDef.decorator_list = decorator_list;
1194 p->v.FunctionDef.returns = returns;
1195 p->lineno = lineno;
1196 p->col_offset = col_offset;
1197 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
1200stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001201ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001202 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1203 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001205 stmt_ty p;
1206 if (!name) {
1207 PyErr_SetString(PyExc_ValueError,
1208 "field name is required for ClassDef");
1209 return NULL;
1210 }
1211 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1212 if (!p)
1213 return NULL;
1214 p->kind = ClassDef_kind;
1215 p->v.ClassDef.name = name;
1216 p->v.ClassDef.bases = bases;
1217 p->v.ClassDef.keywords = keywords;
1218 p->v.ClassDef.starargs = starargs;
1219 p->v.ClassDef.kwargs = kwargs;
1220 p->v.ClassDef.body = body;
1221 p->v.ClassDef.decorator_list = decorator_list;
1222 p->lineno = lineno;
1223 p->col_offset = col_offset;
1224 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225}
1226
1227stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001228Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001230 stmt_ty p;
1231 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1232 if (!p)
1233 return NULL;
1234 p->kind = Return_kind;
1235 p->v.Return.value = value;
1236 p->lineno = lineno;
1237 p->col_offset = col_offset;
1238 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239}
1240
1241stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001242Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001244 stmt_ty p;
1245 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1246 if (!p)
1247 return NULL;
1248 p->kind = Delete_kind;
1249 p->v.Delete.targets = targets;
1250 p->lineno = lineno;
1251 p->col_offset = col_offset;
1252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001256Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1257 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001259 stmt_ty p;
1260 if (!value) {
1261 PyErr_SetString(PyExc_ValueError,
1262 "field value is required for Assign");
1263 return NULL;
1264 }
1265 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1266 if (!p)
1267 return NULL;
1268 p->kind = Assign_kind;
1269 p->v.Assign.targets = targets;
1270 p->v.Assign.value = value;
1271 p->lineno = lineno;
1272 p->col_offset = col_offset;
1273 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274}
1275
1276stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001277AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1278 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001280 stmt_ty p;
1281 if (!target) {
1282 PyErr_SetString(PyExc_ValueError,
1283 "field target is required for AugAssign");
1284 return NULL;
1285 }
1286 if (!op) {
1287 PyErr_SetString(PyExc_ValueError,
1288 "field op is required for AugAssign");
1289 return NULL;
1290 }
1291 if (!value) {
1292 PyErr_SetString(PyExc_ValueError,
1293 "field value is required for AugAssign");
1294 return NULL;
1295 }
1296 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1297 if (!p)
1298 return NULL;
1299 p->kind = AugAssign_kind;
1300 p->v.AugAssign.target = target;
1301 p->v.AugAssign.op = op;
1302 p->v.AugAssign.value = value;
1303 p->lineno = lineno;
1304 p->col_offset = col_offset;
1305 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306}
1307
1308stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001310 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001312 stmt_ty p;
1313 if (!target) {
1314 PyErr_SetString(PyExc_ValueError,
1315 "field target is required for For");
1316 return NULL;
1317 }
1318 if (!iter) {
1319 PyErr_SetString(PyExc_ValueError,
1320 "field iter is required for For");
1321 return NULL;
1322 }
1323 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1324 if (!p)
1325 return NULL;
1326 p->kind = For_kind;
1327 p->v.For.target = target;
1328 p->v.For.iter = iter;
1329 p->v.For.body = body;
1330 p->v.For.orelse = orelse;
1331 p->lineno = lineno;
1332 p->col_offset = col_offset;
1333 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334}
1335
1336stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001337While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1338 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001340 stmt_ty p;
1341 if (!test) {
1342 PyErr_SetString(PyExc_ValueError,
1343 "field test is required for While");
1344 return NULL;
1345 }
1346 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1347 if (!p)
1348 return NULL;
1349 p->kind = While_kind;
1350 p->v.While.test = test;
1351 p->v.While.body = body;
1352 p->v.While.orelse = orelse;
1353 p->lineno = lineno;
1354 p->col_offset = col_offset;
1355 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356}
1357
1358stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001359If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1360 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001362 stmt_ty p;
1363 if (!test) {
1364 PyErr_SetString(PyExc_ValueError,
1365 "field test is required for If");
1366 return NULL;
1367 }
1368 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1369 if (!p)
1370 return NULL;
1371 p->kind = If_kind;
1372 p->v.If.test = test;
1373 p->v.If.body = body;
1374 p->v.If.orelse = orelse;
1375 p->lineno = lineno;
1376 p->col_offset = col_offset;
1377 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378}
1379
1380stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001381With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1382 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001383{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001384 stmt_ty p;
1385 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1386 if (!p)
1387 return NULL;
1388 p->kind = With_kind;
1389 p->v.With.items = items;
1390 p->v.With.body = body;
1391 p->lineno = lineno;
1392 p->col_offset = col_offset;
1393 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001394}
1395
1396stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001397Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001399 stmt_ty p;
1400 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1401 if (!p)
1402 return NULL;
1403 p->kind = Raise_kind;
1404 p->v.Raise.exc = exc;
1405 p->v.Raise.cause = cause;
1406 p->lineno = lineno;
1407 p->col_offset = col_offset;
1408 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409}
1410
1411stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001412Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1413 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001415 stmt_ty p;
1416 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1417 if (!p)
1418 return NULL;
1419 p->kind = Try_kind;
1420 p->v.Try.body = body;
1421 p->v.Try.handlers = handlers;
1422 p->v.Try.orelse = orelse;
1423 p->v.Try.finalbody = finalbody;
1424 p->lineno = lineno;
1425 p->col_offset = col_offset;
1426 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427}
1428
1429stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001430Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001432 stmt_ty p;
1433 if (!test) {
1434 PyErr_SetString(PyExc_ValueError,
1435 "field test is required for Assert");
1436 return NULL;
1437 }
1438 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1439 if (!p)
1440 return NULL;
1441 p->kind = Assert_kind;
1442 p->v.Assert.test = test;
1443 p->v.Assert.msg = msg;
1444 p->lineno = lineno;
1445 p->col_offset = col_offset;
1446 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447}
1448
1449stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001452 stmt_ty p;
1453 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1454 if (!p)
1455 return NULL;
1456 p->kind = Import_kind;
1457 p->v.Import.names = names;
1458 p->lineno = lineno;
1459 p->col_offset = col_offset;
1460 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461}
1462
1463stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001464ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1465 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001467 stmt_ty p;
1468 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1469 if (!p)
1470 return NULL;
1471 p->kind = ImportFrom_kind;
1472 p->v.ImportFrom.module = module;
1473 p->v.ImportFrom.names = names;
1474 p->v.ImportFrom.level = level;
1475 p->lineno = lineno;
1476 p->col_offset = col_offset;
1477 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478}
1479
1480stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001481Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001483 stmt_ty p;
1484 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1485 if (!p)
1486 return NULL;
1487 p->kind = Global_kind;
1488 p->v.Global.names = names;
1489 p->lineno = lineno;
1490 p->col_offset = col_offset;
1491 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492}
1493
1494stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001495Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1496{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001497 stmt_ty p;
1498 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1499 if (!p)
1500 return NULL;
1501 p->kind = Nonlocal_kind;
1502 p->v.Nonlocal.names = names;
1503 p->lineno = lineno;
1504 p->col_offset = col_offset;
1505 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001506}
1507
1508stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001509Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001511 stmt_ty p;
1512 if (!value) {
1513 PyErr_SetString(PyExc_ValueError,
1514 "field value is required for Expr");
1515 return NULL;
1516 }
1517 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1518 if (!p)
1519 return NULL;
1520 p->kind = Expr_kind;
1521 p->v.Expr.value = value;
1522 p->lineno = lineno;
1523 p->col_offset = col_offset;
1524 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001528Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001530 stmt_ty p;
1531 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1532 if (!p)
1533 return NULL;
1534 p->kind = Pass_kind;
1535 p->lineno = lineno;
1536 p->col_offset = col_offset;
1537 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
1540stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001541Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001543 stmt_ty p;
1544 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1545 if (!p)
1546 return NULL;
1547 p->kind = Break_kind;
1548 p->lineno = lineno;
1549 p->col_offset = col_offset;
1550 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551}
1552
1553stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001554Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001556 stmt_ty p;
1557 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1558 if (!p)
1559 return NULL;
1560 p->kind = Continue_kind;
1561 p->lineno = lineno;
1562 p->col_offset = col_offset;
1563 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
1566expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001567BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1568 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001570 expr_ty p;
1571 if (!op) {
1572 PyErr_SetString(PyExc_ValueError,
1573 "field op is required for BoolOp");
1574 return NULL;
1575 }
1576 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1577 if (!p)
1578 return NULL;
1579 p->kind = BoolOp_kind;
1580 p->v.BoolOp.op = op;
1581 p->v.BoolOp.values = values;
1582 p->lineno = lineno;
1583 p->col_offset = col_offset;
1584 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585}
1586
1587expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001588BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1589 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001591 expr_ty p;
1592 if (!left) {
1593 PyErr_SetString(PyExc_ValueError,
1594 "field left is required for BinOp");
1595 return NULL;
1596 }
1597 if (!op) {
1598 PyErr_SetString(PyExc_ValueError,
1599 "field op is required for BinOp");
1600 return NULL;
1601 }
1602 if (!right) {
1603 PyErr_SetString(PyExc_ValueError,
1604 "field right is required for BinOp");
1605 return NULL;
1606 }
1607 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1608 if (!p)
1609 return NULL;
1610 p->kind = BinOp_kind;
1611 p->v.BinOp.left = left;
1612 p->v.BinOp.op = op;
1613 p->v.BinOp.right = right;
1614 p->lineno = lineno;
1615 p->col_offset = col_offset;
1616 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617}
1618
1619expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001620UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1621 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001623 expr_ty p;
1624 if (!op) {
1625 PyErr_SetString(PyExc_ValueError,
1626 "field op is required for UnaryOp");
1627 return NULL;
1628 }
1629 if (!operand) {
1630 PyErr_SetString(PyExc_ValueError,
1631 "field operand is required for UnaryOp");
1632 return NULL;
1633 }
1634 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1635 if (!p)
1636 return NULL;
1637 p->kind = UnaryOp_kind;
1638 p->v.UnaryOp.op = op;
1639 p->v.UnaryOp.operand = operand;
1640 p->lineno = lineno;
1641 p->col_offset = col_offset;
1642 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643}
1644
1645expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1647 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001649 expr_ty p;
1650 if (!args) {
1651 PyErr_SetString(PyExc_ValueError,
1652 "field args is required for Lambda");
1653 return NULL;
1654 }
1655 if (!body) {
1656 PyErr_SetString(PyExc_ValueError,
1657 "field body is required for Lambda");
1658 return NULL;
1659 }
1660 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1661 if (!p)
1662 return NULL;
1663 p->kind = Lambda_kind;
1664 p->v.Lambda.args = args;
1665 p->v.Lambda.body = body;
1666 p->lineno = lineno;
1667 p->col_offset = col_offset;
1668 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
1671expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001672IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1673 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001674{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001675 expr_ty p;
1676 if (!test) {
1677 PyErr_SetString(PyExc_ValueError,
1678 "field test is required for IfExp");
1679 return NULL;
1680 }
1681 if (!body) {
1682 PyErr_SetString(PyExc_ValueError,
1683 "field body is required for IfExp");
1684 return NULL;
1685 }
1686 if (!orelse) {
1687 PyErr_SetString(PyExc_ValueError,
1688 "field orelse is required for IfExp");
1689 return NULL;
1690 }
1691 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1692 if (!p)
1693 return NULL;
1694 p->kind = IfExp_kind;
1695 p->v.IfExp.test = test;
1696 p->v.IfExp.body = body;
1697 p->v.IfExp.orelse = orelse;
1698 p->lineno = lineno;
1699 p->col_offset = col_offset;
1700 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001701}
1702
1703expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001704Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1705 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001707 expr_ty p;
1708 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1709 if (!p)
1710 return NULL;
1711 p->kind = Dict_kind;
1712 p->v.Dict.keys = keys;
1713 p->v.Dict.values = values;
1714 p->lineno = lineno;
1715 p->col_offset = col_offset;
1716 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717}
1718
1719expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001720Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1721{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001722 expr_ty p;
1723 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1724 if (!p)
1725 return NULL;
1726 p->kind = Set_kind;
1727 p->v.Set.elts = elts;
1728 p->lineno = lineno;
1729 p->col_offset = col_offset;
1730 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001731}
1732
1733expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001734ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1735 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001737 expr_ty p;
1738 if (!elt) {
1739 PyErr_SetString(PyExc_ValueError,
1740 "field elt is required for ListComp");
1741 return NULL;
1742 }
1743 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1744 if (!p)
1745 return NULL;
1746 p->kind = ListComp_kind;
1747 p->v.ListComp.elt = elt;
1748 p->v.ListComp.generators = generators;
1749 p->lineno = lineno;
1750 p->col_offset = col_offset;
1751 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752}
1753
1754expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001755SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1756 *arena)
1757{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001758 expr_ty p;
1759 if (!elt) {
1760 PyErr_SetString(PyExc_ValueError,
1761 "field elt is required for SetComp");
1762 return NULL;
1763 }
1764 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1765 if (!p)
1766 return NULL;
1767 p->kind = SetComp_kind;
1768 p->v.SetComp.elt = elt;
1769 p->v.SetComp.generators = generators;
1770 p->lineno = lineno;
1771 p->col_offset = col_offset;
1772 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001773}
1774
1775expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001776DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1777 col_offset, PyArena *arena)
1778{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001779 expr_ty p;
1780 if (!key) {
1781 PyErr_SetString(PyExc_ValueError,
1782 "field key is required for DictComp");
1783 return NULL;
1784 }
1785 if (!value) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field value is required for DictComp");
1788 return NULL;
1789 }
1790 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1791 if (!p)
1792 return NULL;
1793 p->kind = DictComp_kind;
1794 p->v.DictComp.key = key;
1795 p->v.DictComp.value = value;
1796 p->v.DictComp.generators = generators;
1797 p->lineno = lineno;
1798 p->col_offset = col_offset;
1799 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001800}
1801
1802expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001803GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1804 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001806 expr_ty p;
1807 if (!elt) {
1808 PyErr_SetString(PyExc_ValueError,
1809 "field elt is required for GeneratorExp");
1810 return NULL;
1811 }
1812 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1813 if (!p)
1814 return NULL;
1815 p->kind = GeneratorExp_kind;
1816 p->v.GeneratorExp.elt = elt;
1817 p->v.GeneratorExp.generators = generators;
1818 p->lineno = lineno;
1819 p->col_offset = col_offset;
1820 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821}
1822
1823expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001824Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001826 expr_ty p;
1827 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1828 if (!p)
1829 return NULL;
1830 p->kind = Yield_kind;
1831 p->v.Yield.value = value;
1832 p->lineno = lineno;
1833 p->col_offset = col_offset;
1834 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835}
1836
1837expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001838YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1839{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001840 expr_ty p;
1841 if (!value) {
1842 PyErr_SetString(PyExc_ValueError,
1843 "field value is required for YieldFrom");
1844 return NULL;
1845 }
1846 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1847 if (!p)
1848 return NULL;
1849 p->kind = YieldFrom_kind;
1850 p->v.YieldFrom.value = value;
1851 p->lineno = lineno;
1852 p->col_offset = col_offset;
1853 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001854}
1855
1856expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001857Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1858 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001860 expr_ty p;
1861 if (!left) {
1862 PyErr_SetString(PyExc_ValueError,
1863 "field left is required for Compare");
1864 return NULL;
1865 }
1866 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1867 if (!p)
1868 return NULL;
1869 p->kind = Compare_kind;
1870 p->v.Compare.left = left;
1871 p->v.Compare.ops = ops;
1872 p->v.Compare.comparators = comparators;
1873 p->lineno = lineno;
1874 p->col_offset = col_offset;
1875 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876}
1877
1878expr_ty
1879Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001880 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001882 expr_ty p;
1883 if (!func) {
1884 PyErr_SetString(PyExc_ValueError,
1885 "field func is required for Call");
1886 return NULL;
1887 }
1888 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1889 if (!p)
1890 return NULL;
1891 p->kind = Call_kind;
1892 p->v.Call.func = func;
1893 p->v.Call.args = args;
1894 p->v.Call.keywords = keywords;
1895 p->v.Call.starargs = starargs;
1896 p->v.Call.kwargs = kwargs;
1897 p->lineno = lineno;
1898 p->col_offset = col_offset;
1899 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
1902expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001903Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001905 expr_ty p;
1906 if (!n) {
1907 PyErr_SetString(PyExc_ValueError,
1908 "field n is required for Num");
1909 return NULL;
1910 }
1911 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1912 if (!p)
1913 return NULL;
1914 p->kind = Num_kind;
1915 p->v.Num.n = n;
1916 p->lineno = lineno;
1917 p->col_offset = col_offset;
1918 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
1921expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001922Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001924 expr_ty p;
1925 if (!s) {
1926 PyErr_SetString(PyExc_ValueError,
1927 "field s is required for Str");
1928 return NULL;
1929 }
1930 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1931 if (!p)
1932 return NULL;
1933 p->kind = Str_kind;
1934 p->v.Str.s = s;
1935 p->lineno = lineno;
1936 p->col_offset = col_offset;
1937 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938}
1939
1940expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001941Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001942{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001943 expr_ty p;
1944 if (!s) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field s is required for Bytes");
1947 return NULL;
1948 }
1949 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1950 if (!p)
1951 return NULL;
1952 p->kind = Bytes_kind;
1953 p->v.Bytes.s = s;
1954 p->lineno = lineno;
1955 p->col_offset = col_offset;
1956 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001957}
1958
1959expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05001960NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
1961{
Victor Stinneree4b59c2013-07-27 00:01:35 +02001962 expr_ty p;
1963 if (!value) {
1964 PyErr_SetString(PyExc_ValueError,
1965 "field value is required for NameConstant");
1966 return NULL;
1967 }
1968 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1969 if (!p)
1970 return NULL;
1971 p->kind = NameConstant_kind;
1972 p->v.NameConstant.value = value;
1973 p->lineno = lineno;
1974 p->col_offset = col_offset;
1975 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001976}
1977
1978expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001979Ellipsis(int lineno, int col_offset, PyArena *arena)
1980{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001981 expr_ty p;
1982 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1983 if (!p)
1984 return NULL;
1985 p->kind = Ellipsis_kind;
1986 p->lineno = lineno;
1987 p->col_offset = col_offset;
1988 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001989}
1990
1991expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001992Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1993 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001995 expr_ty p;
1996 if (!value) {
1997 PyErr_SetString(PyExc_ValueError,
1998 "field value is required for Attribute");
1999 return NULL;
2000 }
2001 if (!attr) {
2002 PyErr_SetString(PyExc_ValueError,
2003 "field attr is required for Attribute");
2004 return NULL;
2005 }
2006 if (!ctx) {
2007 PyErr_SetString(PyExc_ValueError,
2008 "field ctx is required for Attribute");
2009 return NULL;
2010 }
2011 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2012 if (!p)
2013 return NULL;
2014 p->kind = Attribute_kind;
2015 p->v.Attribute.value = value;
2016 p->v.Attribute.attr = attr;
2017 p->v.Attribute.ctx = ctx;
2018 p->lineno = lineno;
2019 p->col_offset = col_offset;
2020 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021}
2022
2023expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002024Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2025 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002027 expr_ty p;
2028 if (!value) {
2029 PyErr_SetString(PyExc_ValueError,
2030 "field value is required for Subscript");
2031 return NULL;
2032 }
2033 if (!slice) {
2034 PyErr_SetString(PyExc_ValueError,
2035 "field slice is required for Subscript");
2036 return NULL;
2037 }
2038 if (!ctx) {
2039 PyErr_SetString(PyExc_ValueError,
2040 "field ctx is required for Subscript");
2041 return NULL;
2042 }
2043 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2044 if (!p)
2045 return NULL;
2046 p->kind = Subscript_kind;
2047 p->v.Subscript.value = value;
2048 p->v.Subscript.slice = slice;
2049 p->v.Subscript.ctx = ctx;
2050 p->lineno = lineno;
2051 p->col_offset = col_offset;
2052 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053}
2054
2055expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002056Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2057 *arena)
2058{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002059 expr_ty p;
2060 if (!value) {
2061 PyErr_SetString(PyExc_ValueError,
2062 "field value is required for Starred");
2063 return NULL;
2064 }
2065 if (!ctx) {
2066 PyErr_SetString(PyExc_ValueError,
2067 "field ctx is required for Starred");
2068 return NULL;
2069 }
2070 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2071 if (!p)
2072 return NULL;
2073 p->kind = Starred_kind;
2074 p->v.Starred.value = value;
2075 p->v.Starred.ctx = ctx;
2076 p->lineno = lineno;
2077 p->col_offset = col_offset;
2078 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002079}
2080
2081expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002082Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2083 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002085 expr_ty p;
2086 if (!id) {
2087 PyErr_SetString(PyExc_ValueError,
2088 "field id is required for Name");
2089 return NULL;
2090 }
2091 if (!ctx) {
2092 PyErr_SetString(PyExc_ValueError,
2093 "field ctx is required for Name");
2094 return NULL;
2095 }
2096 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2097 if (!p)
2098 return NULL;
2099 p->kind = Name_kind;
2100 p->v.Name.id = id;
2101 p->v.Name.ctx = ctx;
2102 p->lineno = lineno;
2103 p->col_offset = col_offset;
2104 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105}
2106
2107expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002108List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2109 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002111 expr_ty p;
2112 if (!ctx) {
2113 PyErr_SetString(PyExc_ValueError,
2114 "field ctx is required for List");
2115 return NULL;
2116 }
2117 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2118 if (!p)
2119 return NULL;
2120 p->kind = List_kind;
2121 p->v.List.elts = elts;
2122 p->v.List.ctx = ctx;
2123 p->lineno = lineno;
2124 p->col_offset = col_offset;
2125 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126}
2127
2128expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002129Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2130 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002132 expr_ty p;
2133 if (!ctx) {
2134 PyErr_SetString(PyExc_ValueError,
2135 "field ctx is required for Tuple");
2136 return NULL;
2137 }
2138 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2139 if (!p)
2140 return NULL;
2141 p->kind = Tuple_kind;
2142 p->v.Tuple.elts = elts;
2143 p->v.Tuple.ctx = ctx;
2144 p->lineno = lineno;
2145 p->col_offset = col_offset;
2146 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147}
2148
2149slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002150Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002152 slice_ty p;
2153 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2154 if (!p)
2155 return NULL;
2156 p->kind = Slice_kind;
2157 p->v.Slice.lower = lower;
2158 p->v.Slice.upper = upper;
2159 p->v.Slice.step = step;
2160 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161}
2162
2163slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002166 slice_ty p;
2167 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2168 if (!p)
2169 return NULL;
2170 p->kind = ExtSlice_kind;
2171 p->v.ExtSlice.dims = dims;
2172 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173}
2174
2175slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002176Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002178 slice_ty p;
2179 if (!value) {
2180 PyErr_SetString(PyExc_ValueError,
2181 "field value is required for Index");
2182 return NULL;
2183 }
2184 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2185 if (!p)
2186 return NULL;
2187 p->kind = Index_kind;
2188 p->v.Index.value = value;
2189 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190}
2191
2192comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002193comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002195 comprehension_ty p;
2196 if (!target) {
2197 PyErr_SetString(PyExc_ValueError,
2198 "field target is required for comprehension");
2199 return NULL;
2200 }
2201 if (!iter) {
2202 PyErr_SetString(PyExc_ValueError,
2203 "field iter is required for comprehension");
2204 return NULL;
2205 }
2206 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2207 if (!p)
2208 return NULL;
2209 p->target = target;
2210 p->iter = iter;
2211 p->ifs = ifs;
2212 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213}
2214
2215excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002216ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002217 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002219 excepthandler_ty p;
2220 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2221 if (!p)
2222 return NULL;
2223 p->kind = ExceptHandler_kind;
2224 p->v.ExceptHandler.type = type;
2225 p->v.ExceptHandler.name = name;
2226 p->v.ExceptHandler.body = body;
2227 p->lineno = lineno;
2228 p->col_offset = col_offset;
2229 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
2232arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002233arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2234 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002236 arguments_ty p;
2237 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2238 if (!p)
2239 return NULL;
2240 p->args = args;
2241 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002242 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002243 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002244 p->kwarg = kwarg;
2245 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002246 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247}
2248
Neal Norwitzc1505362006-12-28 06:47:50 +00002249arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002250arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002251{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002252 arg_ty p;
2253 if (!arg) {
2254 PyErr_SetString(PyExc_ValueError,
2255 "field arg is required for arg");
2256 return NULL;
2257 }
2258 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2259 if (!p)
2260 return NULL;
2261 p->arg = arg;
2262 p->annotation = annotation;
2263 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002264}
2265
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002267keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002269 keyword_ty p;
2270 if (!arg) {
2271 PyErr_SetString(PyExc_ValueError,
2272 "field arg is required for keyword");
2273 return NULL;
2274 }
2275 if (!value) {
2276 PyErr_SetString(PyExc_ValueError,
2277 "field value is required for keyword");
2278 return NULL;
2279 }
2280 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2281 if (!p)
2282 return NULL;
2283 p->arg = arg;
2284 p->value = value;
2285 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286}
2287
2288alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002291 alias_ty p;
2292 if (!name) {
2293 PyErr_SetString(PyExc_ValueError,
2294 "field name is required for alias");
2295 return NULL;
2296 }
2297 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2298 if (!p)
2299 return NULL;
2300 p->name = name;
2301 p->asname = asname;
2302 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303}
2304
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002305withitem_ty
2306withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2307{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002308 withitem_ty p;
2309 if (!context_expr) {
2310 PyErr_SetString(PyExc_ValueError,
2311 "field context_expr is required for withitem");
2312 return NULL;
2313 }
2314 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2315 if (!p)
2316 return NULL;
2317 p->context_expr = context_expr;
2318 p->optional_vars = optional_vars;
2319 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002320}
2321
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002322
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002323PyObject*
2324ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002325{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002326 mod_ty o = (mod_ty)_o;
2327 PyObject *result = NULL, *value = NULL;
2328 if (!o) {
2329 Py_INCREF(Py_None);
2330 return Py_None;
2331 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002332
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002333 switch (o->kind) {
2334 case Module_kind:
2335 result = PyType_GenericNew(Module_type, NULL, NULL);
2336 if (!result) goto failed;
2337 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2338 if (!value) goto failed;
2339 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2340 goto failed;
2341 Py_DECREF(value);
2342 break;
2343 case Interactive_kind:
2344 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2345 if (!result) goto failed;
2346 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2347 if (!value) goto failed;
2348 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2349 goto failed;
2350 Py_DECREF(value);
2351 break;
2352 case Expression_kind:
2353 result = PyType_GenericNew(Expression_type, NULL, NULL);
2354 if (!result) goto failed;
2355 value = ast2obj_expr(o->v.Expression.body);
2356 if (!value) goto failed;
2357 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2358 goto failed;
2359 Py_DECREF(value);
2360 break;
2361 case Suite_kind:
2362 result = PyType_GenericNew(Suite_type, NULL, NULL);
2363 if (!result) goto failed;
2364 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2365 if (!value) goto failed;
2366 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2367 goto failed;
2368 Py_DECREF(value);
2369 break;
2370 }
2371 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002372failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002373 Py_XDECREF(value);
2374 Py_XDECREF(result);
2375 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376}
2377
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002378PyObject*
2379ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002381 stmt_ty o = (stmt_ty)_o;
2382 PyObject *result = NULL, *value = NULL;
2383 if (!o) {
2384 Py_INCREF(Py_None);
2385 return Py_None;
2386 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002387
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002388 switch (o->kind) {
2389 case FunctionDef_kind:
2390 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2391 if (!result) goto failed;
2392 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002393 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002394 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2395 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002396 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002397 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002398 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002399 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2400 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002401 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002402 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2403 if (!value) goto failed;
2404 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2405 goto failed;
2406 Py_DECREF(value);
2407 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2408 if (!value) goto failed;
2409 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2410 goto failed;
2411 Py_DECREF(value);
2412 value = ast2obj_expr(o->v.FunctionDef.returns);
2413 if (!value) goto failed;
2414 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2415 goto failed;
2416 Py_DECREF(value);
2417 break;
2418 case ClassDef_kind:
2419 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2420 if (!result) goto failed;
2421 value = ast2obj_identifier(o->v.ClassDef.name);
2422 if (!value) goto failed;
2423 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2424 goto failed;
2425 Py_DECREF(value);
2426 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2427 if (!value) goto failed;
2428 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2429 goto failed;
2430 Py_DECREF(value);
2431 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2432 if (!value) goto failed;
2433 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2434 goto failed;
2435 Py_DECREF(value);
2436 value = ast2obj_expr(o->v.ClassDef.starargs);
2437 if (!value) goto failed;
2438 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2439 goto failed;
2440 Py_DECREF(value);
2441 value = ast2obj_expr(o->v.ClassDef.kwargs);
2442 if (!value) goto failed;
2443 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2444 goto failed;
2445 Py_DECREF(value);
2446 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2447 if (!value) goto failed;
2448 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2449 goto failed;
2450 Py_DECREF(value);
2451 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2452 if (!value) goto failed;
2453 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2454 goto failed;
2455 Py_DECREF(value);
2456 break;
2457 case Return_kind:
2458 result = PyType_GenericNew(Return_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_expr(o->v.Return.value);
2461 if (!value) goto failed;
2462 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 break;
2466 case Delete_kind:
2467 result = PyType_GenericNew(Delete_type, NULL, NULL);
2468 if (!result) goto failed;
2469 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2470 if (!value) goto failed;
2471 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
2474 break;
2475 case Assign_kind:
2476 result = PyType_GenericNew(Assign_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2479 if (!value) goto failed;
2480 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 value = ast2obj_expr(o->v.Assign.value);
2484 if (!value) goto failed;
2485 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
2488 break;
2489 case AugAssign_kind:
2490 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2491 if (!result) goto failed;
2492 value = ast2obj_expr(o->v.AugAssign.target);
2493 if (!value) goto failed;
2494 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
2497 value = ast2obj_operator(o->v.AugAssign.op);
2498 if (!value) goto failed;
2499 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2500 goto failed;
2501 Py_DECREF(value);
2502 value = ast2obj_expr(o->v.AugAssign.value);
2503 if (!value) goto failed;
2504 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
2507 break;
2508 case For_kind:
2509 result = PyType_GenericNew(For_type, NULL, NULL);
2510 if (!result) goto failed;
2511 value = ast2obj_expr(o->v.For.target);
2512 if (!value) goto failed;
2513 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 value = ast2obj_expr(o->v.For.iter);
2517 if (!value) goto failed;
2518 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
2521 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2522 if (!value) goto failed;
2523 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2524 goto failed;
2525 Py_DECREF(value);
2526 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2527 if (!value) goto failed;
2528 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2529 goto failed;
2530 Py_DECREF(value);
2531 break;
2532 case While_kind:
2533 result = PyType_GenericNew(While_type, NULL, NULL);
2534 if (!result) goto failed;
2535 value = ast2obj_expr(o->v.While.test);
2536 if (!value) goto failed;
2537 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2538 goto failed;
2539 Py_DECREF(value);
2540 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2541 if (!value) goto failed;
2542 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2543 goto failed;
2544 Py_DECREF(value);
2545 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2546 if (!value) goto failed;
2547 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2548 goto failed;
2549 Py_DECREF(value);
2550 break;
2551 case If_kind:
2552 result = PyType_GenericNew(If_type, NULL, NULL);
2553 if (!result) goto failed;
2554 value = ast2obj_expr(o->v.If.test);
2555 if (!value) goto failed;
2556 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2557 goto failed;
2558 Py_DECREF(value);
2559 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2560 if (!value) goto failed;
2561 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2562 goto failed;
2563 Py_DECREF(value);
2564 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2565 if (!value) goto failed;
2566 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2567 goto failed;
2568 Py_DECREF(value);
2569 break;
2570 case With_kind:
2571 result = PyType_GenericNew(With_type, NULL, NULL);
2572 if (!result) goto failed;
2573 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2574 if (!value) goto failed;
2575 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2576 goto failed;
2577 Py_DECREF(value);
2578 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2579 if (!value) goto failed;
2580 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2581 goto failed;
2582 Py_DECREF(value);
2583 break;
2584 case Raise_kind:
2585 result = PyType_GenericNew(Raise_type, NULL, NULL);
2586 if (!result) goto failed;
2587 value = ast2obj_expr(o->v.Raise.exc);
2588 if (!value) goto failed;
2589 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 value = ast2obj_expr(o->v.Raise.cause);
2593 if (!value) goto failed;
2594 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2595 goto failed;
2596 Py_DECREF(value);
2597 break;
2598 case Try_kind:
2599 result = PyType_GenericNew(Try_type, NULL, NULL);
2600 if (!result) goto failed;
2601 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2602 if (!value) goto failed;
2603 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2607 if (!value) goto failed;
2608 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2609 goto failed;
2610 Py_DECREF(value);
2611 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2612 if (!value) goto failed;
2613 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
2616 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2617 if (!value) goto failed;
2618 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2619 goto failed;
2620 Py_DECREF(value);
2621 break;
2622 case Assert_kind:
2623 result = PyType_GenericNew(Assert_type, NULL, NULL);
2624 if (!result) goto failed;
2625 value = ast2obj_expr(o->v.Assert.test);
2626 if (!value) goto failed;
2627 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2628 goto failed;
2629 Py_DECREF(value);
2630 value = ast2obj_expr(o->v.Assert.msg);
2631 if (!value) goto failed;
2632 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2633 goto failed;
2634 Py_DECREF(value);
2635 break;
2636 case Import_kind:
2637 result = PyType_GenericNew(Import_type, NULL, NULL);
2638 if (!result) goto failed;
2639 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2640 if (!value) goto failed;
2641 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2642 goto failed;
2643 Py_DECREF(value);
2644 break;
2645 case ImportFrom_kind:
2646 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2647 if (!result) goto failed;
2648 value = ast2obj_identifier(o->v.ImportFrom.module);
2649 if (!value) goto failed;
2650 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2651 goto failed;
2652 Py_DECREF(value);
2653 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2654 if (!value) goto failed;
2655 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2656 goto failed;
2657 Py_DECREF(value);
2658 value = ast2obj_int(o->v.ImportFrom.level);
2659 if (!value) goto failed;
2660 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2661 goto failed;
2662 Py_DECREF(value);
2663 break;
2664 case Global_kind:
2665 result = PyType_GenericNew(Global_type, NULL, NULL);
2666 if (!result) goto failed;
2667 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2668 if (!value) goto failed;
2669 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
2672 break;
2673 case Nonlocal_kind:
2674 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2677 if (!value) goto failed;
2678 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 break;
2682 case Expr_kind:
2683 result = PyType_GenericNew(Expr_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_expr(o->v.Expr.value);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 break;
2691 case Pass_kind:
2692 result = PyType_GenericNew(Pass_type, NULL, NULL);
2693 if (!result) goto failed;
2694 break;
2695 case Break_kind:
2696 result = PyType_GenericNew(Break_type, NULL, NULL);
2697 if (!result) goto failed;
2698 break;
2699 case Continue_kind:
2700 result = PyType_GenericNew(Continue_type, NULL, NULL);
2701 if (!result) goto failed;
2702 break;
2703 }
2704 value = ast2obj_int(o->lineno);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_int(o->col_offset);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2712 goto failed;
2713 Py_DECREF(value);
2714 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002715failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002716 Py_XDECREF(value);
2717 Py_XDECREF(result);
2718 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719}
2720
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002721PyObject*
2722ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002724 expr_ty o = (expr_ty)_o;
2725 PyObject *result = NULL, *value = NULL;
2726 if (!o) {
2727 Py_INCREF(Py_None);
2728 return Py_None;
2729 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002730
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002731 switch (o->kind) {
2732 case BoolOp_kind:
2733 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2734 if (!result) goto failed;
2735 value = ast2obj_boolop(o->v.BoolOp.op);
2736 if (!value) goto failed;
2737 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2738 goto failed;
2739 Py_DECREF(value);
2740 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2741 if (!value) goto failed;
2742 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2743 goto failed;
2744 Py_DECREF(value);
2745 break;
2746 case BinOp_kind:
2747 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2748 if (!result) goto failed;
2749 value = ast2obj_expr(o->v.BinOp.left);
2750 if (!value) goto failed;
2751 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
2754 value = ast2obj_operator(o->v.BinOp.op);
2755 if (!value) goto failed;
2756 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
2759 value = ast2obj_expr(o->v.BinOp.right);
2760 if (!value) goto failed;
2761 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 break;
2765 case UnaryOp_kind:
2766 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2767 if (!result) goto failed;
2768 value = ast2obj_unaryop(o->v.UnaryOp.op);
2769 if (!value) goto failed;
2770 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 value = ast2obj_expr(o->v.UnaryOp.operand);
2774 if (!value) goto failed;
2775 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 break;
2779 case Lambda_kind:
2780 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2781 if (!result) goto failed;
2782 value = ast2obj_arguments(o->v.Lambda.args);
2783 if (!value) goto failed;
2784 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 value = ast2obj_expr(o->v.Lambda.body);
2788 if (!value) goto failed;
2789 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 break;
2793 case IfExp_kind:
2794 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2795 if (!result) goto failed;
2796 value = ast2obj_expr(o->v.IfExp.test);
2797 if (!value) goto failed;
2798 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2799 goto failed;
2800 Py_DECREF(value);
2801 value = ast2obj_expr(o->v.IfExp.body);
2802 if (!value) goto failed;
2803 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
2806 value = ast2obj_expr(o->v.IfExp.orelse);
2807 if (!value) goto failed;
2808 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2809 goto failed;
2810 Py_DECREF(value);
2811 break;
2812 case Dict_kind:
2813 result = PyType_GenericNew(Dict_type, NULL, NULL);
2814 if (!result) goto failed;
2815 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2816 if (!value) goto failed;
2817 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
2820 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2821 if (!value) goto failed;
2822 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
2825 break;
2826 case Set_kind:
2827 result = PyType_GenericNew(Set_type, NULL, NULL);
2828 if (!result) goto failed;
2829 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2830 if (!value) goto failed;
2831 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 break;
2835 case ListComp_kind:
2836 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2837 if (!result) goto failed;
2838 value = ast2obj_expr(o->v.ListComp.elt);
2839 if (!value) goto failed;
2840 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
2843 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2844 if (!value) goto failed;
2845 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
2848 break;
2849 case SetComp_kind:
2850 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2851 if (!result) goto failed;
2852 value = ast2obj_expr(o->v.SetComp.elt);
2853 if (!value) goto failed;
2854 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2855 goto failed;
2856 Py_DECREF(value);
2857 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2858 if (!value) goto failed;
2859 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 break;
2863 case DictComp_kind:
2864 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2865 if (!result) goto failed;
2866 value = ast2obj_expr(o->v.DictComp.key);
2867 if (!value) goto failed;
2868 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 value = ast2obj_expr(o->v.DictComp.value);
2872 if (!value) goto failed;
2873 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
2876 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2877 if (!value) goto failed;
2878 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2879 goto failed;
2880 Py_DECREF(value);
2881 break;
2882 case GeneratorExp_kind:
2883 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2884 if (!result) goto failed;
2885 value = ast2obj_expr(o->v.GeneratorExp.elt);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_list(o->v.GeneratorExp.generators,
2891 ast2obj_comprehension);
2892 if (!value) goto failed;
2893 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
2896 break;
2897 case Yield_kind:
2898 result = PyType_GenericNew(Yield_type, NULL, NULL);
2899 if (!result) goto failed;
2900 value = ast2obj_expr(o->v.Yield.value);
2901 if (!value) goto failed;
2902 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2903 goto failed;
2904 Py_DECREF(value);
2905 break;
2906 case YieldFrom_kind:
2907 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2908 if (!result) goto failed;
2909 value = ast2obj_expr(o->v.YieldFrom.value);
2910 if (!value) goto failed;
2911 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2912 goto failed;
2913 Py_DECREF(value);
2914 break;
2915 case Compare_kind:
2916 result = PyType_GenericNew(Compare_type, NULL, NULL);
2917 if (!result) goto failed;
2918 value = ast2obj_expr(o->v.Compare.left);
2919 if (!value) goto failed;
2920 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2921 goto failed;
2922 Py_DECREF(value);
2923 {
2924 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2925 value = PyList_New(n);
2926 if (!value) goto failed;
2927 for(i = 0; i < n; i++)
2928 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002930 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002931 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2932 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002933 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002934 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002935 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002936 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
2937 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002938 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002939 break;
2940 case Call_kind:
2941 result = PyType_GenericNew(Call_type, NULL, NULL);
2942 if (!result) goto failed;
2943 value = ast2obj_expr(o->v.Call.func);
2944 if (!value) goto failed;
2945 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2946 goto failed;
2947 Py_DECREF(value);
2948 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2949 if (!value) goto failed;
2950 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2951 goto failed;
2952 Py_DECREF(value);
2953 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2954 if (!value) goto failed;
2955 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2956 goto failed;
2957 Py_DECREF(value);
2958 value = ast2obj_expr(o->v.Call.starargs);
2959 if (!value) goto failed;
2960 if (_PyObject_SetAttrId(result, &PyId_starargs, value) == -1)
2961 goto failed;
2962 Py_DECREF(value);
2963 value = ast2obj_expr(o->v.Call.kwargs);
2964 if (!value) goto failed;
2965 if (_PyObject_SetAttrId(result, &PyId_kwargs, value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 break;
2969 case Num_kind:
2970 result = PyType_GenericNew(Num_type, NULL, NULL);
2971 if (!result) goto failed;
2972 value = ast2obj_object(o->v.Num.n);
2973 if (!value) goto failed;
2974 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2975 goto failed;
2976 Py_DECREF(value);
2977 break;
2978 case Str_kind:
2979 result = PyType_GenericNew(Str_type, NULL, NULL);
2980 if (!result) goto failed;
2981 value = ast2obj_string(o->v.Str.s);
2982 if (!value) goto failed;
2983 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2984 goto failed;
2985 Py_DECREF(value);
2986 break;
2987 case Bytes_kind:
2988 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2989 if (!result) goto failed;
2990 value = ast2obj_bytes(o->v.Bytes.s);
2991 if (!value) goto failed;
2992 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2993 goto failed;
2994 Py_DECREF(value);
2995 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002996 case NameConstant_kind:
2997 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
2998 if (!result) goto failed;
2999 value = ast2obj_singleton(o->v.NameConstant.value);
3000 if (!value) goto failed;
3001 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3002 goto failed;
3003 Py_DECREF(value);
3004 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003005 case Ellipsis_kind:
3006 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3007 if (!result) goto failed;
3008 break;
3009 case Attribute_kind:
3010 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3011 if (!result) goto failed;
3012 value = ast2obj_expr(o->v.Attribute.value);
3013 if (!value) goto failed;
3014 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3015 goto failed;
3016 Py_DECREF(value);
3017 value = ast2obj_identifier(o->v.Attribute.attr);
3018 if (!value) goto failed;
3019 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3020 goto failed;
3021 Py_DECREF(value);
3022 value = ast2obj_expr_context(o->v.Attribute.ctx);
3023 if (!value) goto failed;
3024 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3025 goto failed;
3026 Py_DECREF(value);
3027 break;
3028 case Subscript_kind:
3029 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3030 if (!result) goto failed;
3031 value = ast2obj_expr(o->v.Subscript.value);
3032 if (!value) goto failed;
3033 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3034 goto failed;
3035 Py_DECREF(value);
3036 value = ast2obj_slice(o->v.Subscript.slice);
3037 if (!value) goto failed;
3038 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3039 goto failed;
3040 Py_DECREF(value);
3041 value = ast2obj_expr_context(o->v.Subscript.ctx);
3042 if (!value) goto failed;
3043 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3044 goto failed;
3045 Py_DECREF(value);
3046 break;
3047 case Starred_kind:
3048 result = PyType_GenericNew(Starred_type, NULL, NULL);
3049 if (!result) goto failed;
3050 value = ast2obj_expr(o->v.Starred.value);
3051 if (!value) goto failed;
3052 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3053 goto failed;
3054 Py_DECREF(value);
3055 value = ast2obj_expr_context(o->v.Starred.ctx);
3056 if (!value) goto failed;
3057 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3058 goto failed;
3059 Py_DECREF(value);
3060 break;
3061 case Name_kind:
3062 result = PyType_GenericNew(Name_type, NULL, NULL);
3063 if (!result) goto failed;
3064 value = ast2obj_identifier(o->v.Name.id);
3065 if (!value) goto failed;
3066 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3067 goto failed;
3068 Py_DECREF(value);
3069 value = ast2obj_expr_context(o->v.Name.ctx);
3070 if (!value) goto failed;
3071 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3072 goto failed;
3073 Py_DECREF(value);
3074 break;
3075 case List_kind:
3076 result = PyType_GenericNew(List_type, NULL, NULL);
3077 if (!result) goto failed;
3078 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3079 if (!value) goto failed;
3080 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3081 goto failed;
3082 Py_DECREF(value);
3083 value = ast2obj_expr_context(o->v.List.ctx);
3084 if (!value) goto failed;
3085 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3086 goto failed;
3087 Py_DECREF(value);
3088 break;
3089 case Tuple_kind:
3090 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3091 if (!result) goto failed;
3092 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3093 if (!value) goto failed;
3094 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3095 goto failed;
3096 Py_DECREF(value);
3097 value = ast2obj_expr_context(o->v.Tuple.ctx);
3098 if (!value) goto failed;
3099 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3100 goto failed;
3101 Py_DECREF(value);
3102 break;
3103 }
3104 value = ast2obj_int(o->lineno);
3105 if (!value) goto failed;
3106 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3107 goto failed;
3108 Py_DECREF(value);
3109 value = ast2obj_int(o->col_offset);
3110 if (!value) goto failed;
3111 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3112 goto failed;
3113 Py_DECREF(value);
3114 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003115failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003116 Py_XDECREF(value);
3117 Py_XDECREF(result);
3118 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119}
3120
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003121PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003123 switch(o) {
3124 case Load:
3125 Py_INCREF(Load_singleton);
3126 return Load_singleton;
3127 case Store:
3128 Py_INCREF(Store_singleton);
3129 return Store_singleton;
3130 case Del:
3131 Py_INCREF(Del_singleton);
3132 return Del_singleton;
3133 case AugLoad:
3134 Py_INCREF(AugLoad_singleton);
3135 return AugLoad_singleton;
3136 case AugStore:
3137 Py_INCREF(AugStore_singleton);
3138 return AugStore_singleton;
3139 case Param:
3140 Py_INCREF(Param_singleton);
3141 return Param_singleton;
3142 default:
3143 /* should never happen, but just in case ... */
3144 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3145 return NULL;
3146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003148PyObject*
3149ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003151 slice_ty o = (slice_ty)_o;
3152 PyObject *result = NULL, *value = NULL;
3153 if (!o) {
3154 Py_INCREF(Py_None);
3155 return Py_None;
3156 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003157
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003158 switch (o->kind) {
3159 case Slice_kind:
3160 result = PyType_GenericNew(Slice_type, NULL, NULL);
3161 if (!result) goto failed;
3162 value = ast2obj_expr(o->v.Slice.lower);
3163 if (!value) goto failed;
3164 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3165 goto failed;
3166 Py_DECREF(value);
3167 value = ast2obj_expr(o->v.Slice.upper);
3168 if (!value) goto failed;
3169 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3170 goto failed;
3171 Py_DECREF(value);
3172 value = ast2obj_expr(o->v.Slice.step);
3173 if (!value) goto failed;
3174 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3175 goto failed;
3176 Py_DECREF(value);
3177 break;
3178 case ExtSlice_kind:
3179 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3180 if (!result) goto failed;
3181 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3182 if (!value) goto failed;
3183 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3184 goto failed;
3185 Py_DECREF(value);
3186 break;
3187 case Index_kind:
3188 result = PyType_GenericNew(Index_type, NULL, NULL);
3189 if (!result) goto failed;
3190 value = ast2obj_expr(o->v.Index.value);
3191 if (!value) goto failed;
3192 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3193 goto failed;
3194 Py_DECREF(value);
3195 break;
3196 }
3197 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003198failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003199 Py_XDECREF(value);
3200 Py_XDECREF(result);
3201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202}
3203
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003204PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003206 switch(o) {
3207 case And:
3208 Py_INCREF(And_singleton);
3209 return And_singleton;
3210 case Or:
3211 Py_INCREF(Or_singleton);
3212 return Or_singleton;
3213 default:
3214 /* should never happen, but just in case ... */
3215 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3216 return NULL;
3217 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003218}
3219PyObject* ast2obj_operator(operator_ty o)
3220{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003221 switch(o) {
3222 case Add:
3223 Py_INCREF(Add_singleton);
3224 return Add_singleton;
3225 case Sub:
3226 Py_INCREF(Sub_singleton);
3227 return Sub_singleton;
3228 case Mult:
3229 Py_INCREF(Mult_singleton);
3230 return Mult_singleton;
3231 case Div:
3232 Py_INCREF(Div_singleton);
3233 return Div_singleton;
3234 case Mod:
3235 Py_INCREF(Mod_singleton);
3236 return Mod_singleton;
3237 case Pow:
3238 Py_INCREF(Pow_singleton);
3239 return Pow_singleton;
3240 case LShift:
3241 Py_INCREF(LShift_singleton);
3242 return LShift_singleton;
3243 case RShift:
3244 Py_INCREF(RShift_singleton);
3245 return RShift_singleton;
3246 case BitOr:
3247 Py_INCREF(BitOr_singleton);
3248 return BitOr_singleton;
3249 case BitXor:
3250 Py_INCREF(BitXor_singleton);
3251 return BitXor_singleton;
3252 case BitAnd:
3253 Py_INCREF(BitAnd_singleton);
3254 return BitAnd_singleton;
3255 case FloorDiv:
3256 Py_INCREF(FloorDiv_singleton);
3257 return FloorDiv_singleton;
3258 default:
3259 /* should never happen, but just in case ... */
3260 PyErr_Format(PyExc_SystemError, "unknown operator found");
3261 return NULL;
3262 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003263}
3264PyObject* ast2obj_unaryop(unaryop_ty o)
3265{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003266 switch(o) {
3267 case Invert:
3268 Py_INCREF(Invert_singleton);
3269 return Invert_singleton;
3270 case Not:
3271 Py_INCREF(Not_singleton);
3272 return Not_singleton;
3273 case UAdd:
3274 Py_INCREF(UAdd_singleton);
3275 return UAdd_singleton;
3276 case USub:
3277 Py_INCREF(USub_singleton);
3278 return USub_singleton;
3279 default:
3280 /* should never happen, but just in case ... */
3281 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3282 return NULL;
3283 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003284}
3285PyObject* ast2obj_cmpop(cmpop_ty o)
3286{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003287 switch(o) {
3288 case Eq:
3289 Py_INCREF(Eq_singleton);
3290 return Eq_singleton;
3291 case NotEq:
3292 Py_INCREF(NotEq_singleton);
3293 return NotEq_singleton;
3294 case Lt:
3295 Py_INCREF(Lt_singleton);
3296 return Lt_singleton;
3297 case LtE:
3298 Py_INCREF(LtE_singleton);
3299 return LtE_singleton;
3300 case Gt:
3301 Py_INCREF(Gt_singleton);
3302 return Gt_singleton;
3303 case GtE:
3304 Py_INCREF(GtE_singleton);
3305 return GtE_singleton;
3306 case Is:
3307 Py_INCREF(Is_singleton);
3308 return Is_singleton;
3309 case IsNot:
3310 Py_INCREF(IsNot_singleton);
3311 return IsNot_singleton;
3312 case In:
3313 Py_INCREF(In_singleton);
3314 return In_singleton;
3315 case NotIn:
3316 Py_INCREF(NotIn_singleton);
3317 return NotIn_singleton;
3318 default:
3319 /* should never happen, but just in case ... */
3320 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3321 return NULL;
3322 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003323}
3324PyObject*
3325ast2obj_comprehension(void* _o)
3326{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003327 comprehension_ty o = (comprehension_ty)_o;
3328 PyObject *result = NULL, *value = NULL;
3329 if (!o) {
3330 Py_INCREF(Py_None);
3331 return Py_None;
3332 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003333
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003334 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3335 if (!result) return NULL;
3336 value = ast2obj_expr(o->target);
3337 if (!value) goto failed;
3338 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3339 goto failed;
3340 Py_DECREF(value);
3341 value = ast2obj_expr(o->iter);
3342 if (!value) goto failed;
3343 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3344 goto failed;
3345 Py_DECREF(value);
3346 value = ast2obj_list(o->ifs, ast2obj_expr);
3347 if (!value) goto failed;
3348 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3349 goto failed;
3350 Py_DECREF(value);
3351 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003352failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003353 Py_XDECREF(value);
3354 Py_XDECREF(result);
3355 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356}
3357
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003358PyObject*
3359ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003361 excepthandler_ty o = (excepthandler_ty)_o;
3362 PyObject *result = NULL, *value = NULL;
3363 if (!o) {
3364 Py_INCREF(Py_None);
3365 return Py_None;
3366 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003367
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003368 switch (o->kind) {
3369 case ExceptHandler_kind:
3370 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3371 if (!result) goto failed;
3372 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003373 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003374 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3375 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003376 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003377 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003378 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003379 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3380 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003381 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003382 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3383 if (!value) goto failed;
3384 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3385 goto failed;
3386 Py_DECREF(value);
3387 break;
3388 }
3389 value = ast2obj_int(o->lineno);
3390 if (!value) goto failed;
3391 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3392 goto failed;
3393 Py_DECREF(value);
3394 value = ast2obj_int(o->col_offset);
3395 if (!value) goto failed;
3396 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3397 goto failed;
3398 Py_DECREF(value);
3399 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003400failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 Py_XDECREF(value);
3402 Py_XDECREF(result);
3403 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404}
3405
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003406PyObject*
3407ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003409 arguments_ty o = (arguments_ty)_o;
3410 PyObject *result = NULL, *value = NULL;
3411 if (!o) {
3412 Py_INCREF(Py_None);
3413 return Py_None;
3414 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003415
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003416 result = PyType_GenericNew(arguments_type, NULL, NULL);
3417 if (!result) return NULL;
3418 value = ast2obj_list(o->args, ast2obj_arg);
3419 if (!value) goto failed;
3420 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3421 goto failed;
3422 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003423 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003424 if (!value) goto failed;
3425 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3426 goto failed;
3427 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003428 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3429 if (!value) goto failed;
3430 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3431 goto failed;
3432 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003433 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003434 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003435 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003436 goto failed;
3437 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003438 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003439 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003440 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003441 goto failed;
3442 Py_DECREF(value);
3443 value = ast2obj_list(o->defaults, ast2obj_expr);
3444 if (!value) goto failed;
3445 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3446 goto failed;
3447 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003448 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003449failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003450 Py_XDECREF(value);
3451 Py_XDECREF(result);
3452 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453}
3454
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003455PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003456ast2obj_arg(void* _o)
3457{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003458 arg_ty o = (arg_ty)_o;
3459 PyObject *result = NULL, *value = NULL;
3460 if (!o) {
3461 Py_INCREF(Py_None);
3462 return Py_None;
3463 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003464
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003465 result = PyType_GenericNew(arg_type, NULL, NULL);
3466 if (!result) return NULL;
3467 value = ast2obj_identifier(o->arg);
3468 if (!value) goto failed;
3469 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3470 goto failed;
3471 Py_DECREF(value);
3472 value = ast2obj_expr(o->annotation);
3473 if (!value) goto failed;
3474 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3475 goto failed;
3476 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003477 value = ast2obj_int(o->lineno);
3478 if (!value) goto failed;
3479 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3480 goto failed;
3481 Py_DECREF(value);
3482 value = ast2obj_int(o->col_offset);
3483 if (!value) goto failed;
3484 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3485 goto failed;
3486 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003487 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003488failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003489 Py_XDECREF(value);
3490 Py_XDECREF(result);
3491 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003492}
3493
3494PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003495ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003497 keyword_ty o = (keyword_ty)_o;
3498 PyObject *result = NULL, *value = NULL;
3499 if (!o) {
3500 Py_INCREF(Py_None);
3501 return Py_None;
3502 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003503
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003504 result = PyType_GenericNew(keyword_type, NULL, NULL);
3505 if (!result) return NULL;
3506 value = ast2obj_identifier(o->arg);
3507 if (!value) goto failed;
3508 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3509 goto failed;
3510 Py_DECREF(value);
3511 value = ast2obj_expr(o->value);
3512 if (!value) goto failed;
3513 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3514 goto failed;
3515 Py_DECREF(value);
3516 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003517failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003518 Py_XDECREF(value);
3519 Py_XDECREF(result);
3520 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521}
3522
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003523PyObject*
3524ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 alias_ty o = (alias_ty)_o;
3527 PyObject *result = NULL, *value = NULL;
3528 if (!o) {
3529 Py_INCREF(Py_None);
3530 return Py_None;
3531 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003532
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 result = PyType_GenericNew(alias_type, NULL, NULL);
3534 if (!result) return NULL;
3535 value = ast2obj_identifier(o->name);
3536 if (!value) goto failed;
3537 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3538 goto failed;
3539 Py_DECREF(value);
3540 value = ast2obj_identifier(o->asname);
3541 if (!value) goto failed;
3542 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3543 goto failed;
3544 Py_DECREF(value);
3545 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003546failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003547 Py_XDECREF(value);
3548 Py_XDECREF(result);
3549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550}
3551
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003552PyObject*
3553ast2obj_withitem(void* _o)
3554{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003555 withitem_ty o = (withitem_ty)_o;
3556 PyObject *result = NULL, *value = NULL;
3557 if (!o) {
3558 Py_INCREF(Py_None);
3559 return Py_None;
3560 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003561
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 result = PyType_GenericNew(withitem_type, NULL, NULL);
3563 if (!result) return NULL;
3564 value = ast2obj_expr(o->context_expr);
3565 if (!value) goto failed;
3566 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3567 goto failed;
3568 Py_DECREF(value);
3569 value = ast2obj_expr(o->optional_vars);
3570 if (!value) goto failed;
3571 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3572 goto failed;
3573 Py_DECREF(value);
3574 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003575failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003576 Py_XDECREF(value);
3577 Py_XDECREF(result);
3578 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003579}
3580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003582int
3583obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3584{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003585 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003586
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003587 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003588
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003589 if (obj == Py_None) {
3590 *out = NULL;
3591 return 0;
3592 }
3593 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3594 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003595 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 }
3597 if (isinstance) {
3598 asdl_seq* body;
3599
3600 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3601 int res;
3602 Py_ssize_t len;
3603 Py_ssize_t i;
3604 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3605 if (tmp == NULL) goto failed;
3606 if (!PyList_Check(tmp)) {
3607 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3608 goto failed;
3609 }
3610 len = PyList_GET_SIZE(tmp);
3611 body = asdl_seq_new(len, arena);
3612 if (body == NULL) goto failed;
3613 for (i = 0; i < len; i++) {
3614 stmt_ty value;
3615 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3616 if (res != 0) goto failed;
3617 asdl_seq_SET(body, i, value);
3618 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003619 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003620 } else {
3621 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3622 return 1;
3623 }
3624 *out = Module(body, arena);
3625 if (*out == NULL) goto failed;
3626 return 0;
3627 }
3628 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3629 if (isinstance == -1) {
3630 return 1;
3631 }
3632 if (isinstance) {
3633 asdl_seq* body;
3634
3635 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3636 int res;
3637 Py_ssize_t len;
3638 Py_ssize_t i;
3639 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3640 if (tmp == NULL) goto failed;
3641 if (!PyList_Check(tmp)) {
3642 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3643 goto failed;
3644 }
3645 len = PyList_GET_SIZE(tmp);
3646 body = asdl_seq_new(len, arena);
3647 if (body == NULL) goto failed;
3648 for (i = 0; i < len; i++) {
3649 stmt_ty value;
3650 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3651 if (res != 0) goto failed;
3652 asdl_seq_SET(body, i, value);
3653 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003654 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003655 } else {
3656 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3657 return 1;
3658 }
3659 *out = Interactive(body, arena);
3660 if (*out == NULL) goto failed;
3661 return 0;
3662 }
3663 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3664 if (isinstance == -1) {
3665 return 1;
3666 }
3667 if (isinstance) {
3668 expr_ty body;
3669
3670 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3671 int res;
3672 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3673 if (tmp == NULL) goto failed;
3674 res = obj2ast_expr(tmp, &body, arena);
3675 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003676 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003677 } else {
3678 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3679 return 1;
3680 }
3681 *out = Expression(body, arena);
3682 if (*out == NULL) goto failed;
3683 return 0;
3684 }
3685 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3686 if (isinstance == -1) {
3687 return 1;
3688 }
3689 if (isinstance) {
3690 asdl_seq* body;
3691
3692 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3693 int res;
3694 Py_ssize_t len;
3695 Py_ssize_t i;
3696 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3697 if (tmp == NULL) goto failed;
3698 if (!PyList_Check(tmp)) {
3699 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3700 goto failed;
3701 }
3702 len = PyList_GET_SIZE(tmp);
3703 body = asdl_seq_new(len, arena);
3704 if (body == NULL) goto failed;
3705 for (i = 0; i < len; i++) {
3706 stmt_ty value;
3707 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3708 if (res != 0) goto failed;
3709 asdl_seq_SET(body, i, value);
3710 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003711 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003712 } else {
3713 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3714 return 1;
3715 }
3716 *out = Suite(body, arena);
3717 if (*out == NULL) goto failed;
3718 return 0;
3719 }
3720
3721 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3722 failed:
3723 Py_XDECREF(tmp);
3724 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003725}
3726
3727int
3728obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3729{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003730 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003731
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003732 PyObject *tmp = NULL;
3733 int lineno;
3734 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003735
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 if (obj == Py_None) {
3737 *out = NULL;
3738 return 0;
3739 }
3740 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3741 int res;
3742 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3743 if (tmp == NULL) goto failed;
3744 res = obj2ast_int(tmp, &lineno, arena);
3745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003747 } else {
3748 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003749 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003750 }
3751 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3752 int res;
3753 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3754 if (tmp == NULL) goto failed;
3755 res = obj2ast_int(tmp, &col_offset, arena);
3756 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003757 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003758 } else {
3759 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3760 return 1;
3761 }
3762 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3763 if (isinstance == -1) {
3764 return 1;
3765 }
3766 if (isinstance) {
3767 identifier name;
3768 arguments_ty args;
3769 asdl_seq* body;
3770 asdl_seq* decorator_list;
3771 expr_ty returns;
3772
3773 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3774 int res;
3775 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3776 if (tmp == NULL) goto failed;
3777 res = obj2ast_identifier(tmp, &name, arena);
3778 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003779 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3782 return 1;
3783 }
3784 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3785 int res;
3786 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3787 if (tmp == NULL) goto failed;
3788 res = obj2ast_arguments(tmp, &args, arena);
3789 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003790 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003791 } else {
3792 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3793 return 1;
3794 }
3795 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3796 int res;
3797 Py_ssize_t len;
3798 Py_ssize_t i;
3799 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3800 if (tmp == NULL) goto failed;
3801 if (!PyList_Check(tmp)) {
3802 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3803 goto failed;
3804 }
3805 len = PyList_GET_SIZE(tmp);
3806 body = asdl_seq_new(len, arena);
3807 if (body == NULL) goto failed;
3808 for (i = 0; i < len; i++) {
3809 stmt_ty value;
3810 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3811 if (res != 0) goto failed;
3812 asdl_seq_SET(body, i, value);
3813 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003814 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003815 } else {
3816 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3817 return 1;
3818 }
3819 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3820 int res;
3821 Py_ssize_t len;
3822 Py_ssize_t i;
3823 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3824 if (tmp == NULL) goto failed;
3825 if (!PyList_Check(tmp)) {
3826 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3827 goto failed;
3828 }
3829 len = PyList_GET_SIZE(tmp);
3830 decorator_list = asdl_seq_new(len, arena);
3831 if (decorator_list == NULL) goto failed;
3832 for (i = 0; i < len; i++) {
3833 expr_ty value;
3834 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3835 if (res != 0) goto failed;
3836 asdl_seq_SET(decorator_list, i, value);
3837 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003838 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003839 } else {
3840 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3841 return 1;
3842 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003843 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003844 int res;
3845 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3846 if (tmp == NULL) goto failed;
3847 res = obj2ast_expr(tmp, &returns, arena);
3848 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003849 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003850 } else {
3851 returns = NULL;
3852 }
3853 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3854 col_offset, arena);
3855 if (*out == NULL) goto failed;
3856 return 0;
3857 }
3858 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3859 if (isinstance == -1) {
3860 return 1;
3861 }
3862 if (isinstance) {
3863 identifier name;
3864 asdl_seq* bases;
3865 asdl_seq* keywords;
3866 expr_ty starargs;
3867 expr_ty kwargs;
3868 asdl_seq* body;
3869 asdl_seq* decorator_list;
3870
3871 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3872 int res;
3873 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3874 if (tmp == NULL) goto failed;
3875 res = obj2ast_identifier(tmp, &name, arena);
3876 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003877 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 } else {
3879 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3880 return 1;
3881 }
3882 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3883 int res;
3884 Py_ssize_t len;
3885 Py_ssize_t i;
3886 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3887 if (tmp == NULL) goto failed;
3888 if (!PyList_Check(tmp)) {
3889 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3890 goto failed;
3891 }
3892 len = PyList_GET_SIZE(tmp);
3893 bases = asdl_seq_new(len, arena);
3894 if (bases == NULL) goto failed;
3895 for (i = 0; i < len; i++) {
3896 expr_ty value;
3897 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3898 if (res != 0) goto failed;
3899 asdl_seq_SET(bases, i, value);
3900 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003901 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003902 } else {
3903 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3904 return 1;
3905 }
3906 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3907 int res;
3908 Py_ssize_t len;
3909 Py_ssize_t i;
3910 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3911 if (tmp == NULL) goto failed;
3912 if (!PyList_Check(tmp)) {
3913 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3914 goto failed;
3915 }
3916 len = PyList_GET_SIZE(tmp);
3917 keywords = asdl_seq_new(len, arena);
3918 if (keywords == NULL) goto failed;
3919 for (i = 0; i < len; i++) {
3920 keyword_ty value;
3921 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3922 if (res != 0) goto failed;
3923 asdl_seq_SET(keywords, i, value);
3924 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003925 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003926 } else {
3927 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3928 return 1;
3929 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003930 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003931 int res;
3932 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
3933 if (tmp == NULL) goto failed;
3934 res = obj2ast_expr(tmp, &starargs, arena);
3935 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003936 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003937 } else {
3938 starargs = NULL;
3939 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003940 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 int res;
3942 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
3943 if (tmp == NULL) goto failed;
3944 res = obj2ast_expr(tmp, &kwargs, arena);
3945 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003947 } else {
3948 kwargs = NULL;
3949 }
3950 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3951 int res;
3952 Py_ssize_t len;
3953 Py_ssize_t i;
3954 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3955 if (tmp == NULL) goto failed;
3956 if (!PyList_Check(tmp)) {
3957 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3958 goto failed;
3959 }
3960 len = PyList_GET_SIZE(tmp);
3961 body = asdl_seq_new(len, arena);
3962 if (body == NULL) goto failed;
3963 for (i = 0; i < len; i++) {
3964 stmt_ty value;
3965 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3966 if (res != 0) goto failed;
3967 asdl_seq_SET(body, i, value);
3968 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003969 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003970 } else {
3971 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3972 return 1;
3973 }
3974 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3975 int res;
3976 Py_ssize_t len;
3977 Py_ssize_t i;
3978 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3979 if (tmp == NULL) goto failed;
3980 if (!PyList_Check(tmp)) {
3981 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3982 goto failed;
3983 }
3984 len = PyList_GET_SIZE(tmp);
3985 decorator_list = asdl_seq_new(len, arena);
3986 if (decorator_list == NULL) goto failed;
3987 for (i = 0; i < len; i++) {
3988 expr_ty value;
3989 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3990 if (res != 0) goto failed;
3991 asdl_seq_SET(decorator_list, i, value);
3992 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003993 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 } else {
3995 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3996 return 1;
3997 }
3998 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3999 decorator_list, lineno, col_offset, arena);
4000 if (*out == NULL) goto failed;
4001 return 0;
4002 }
4003 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4004 if (isinstance == -1) {
4005 return 1;
4006 }
4007 if (isinstance) {
4008 expr_ty value;
4009
Victor Stinneree4b59c2013-07-27 00:01:35 +02004010 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 int res;
4012 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4013 if (tmp == NULL) goto failed;
4014 res = obj2ast_expr(tmp, &value, arena);
4015 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004016 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004017 } else {
4018 value = NULL;
4019 }
4020 *out = Return(value, lineno, col_offset, arena);
4021 if (*out == NULL) goto failed;
4022 return 0;
4023 }
4024 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4025 if (isinstance == -1) {
4026 return 1;
4027 }
4028 if (isinstance) {
4029 asdl_seq* targets;
4030
4031 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4032 int res;
4033 Py_ssize_t len;
4034 Py_ssize_t i;
4035 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4036 if (tmp == NULL) goto failed;
4037 if (!PyList_Check(tmp)) {
4038 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4039 goto failed;
4040 }
4041 len = PyList_GET_SIZE(tmp);
4042 targets = asdl_seq_new(len, arena);
4043 if (targets == NULL) goto failed;
4044 for (i = 0; i < len; i++) {
4045 expr_ty value;
4046 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4047 if (res != 0) goto failed;
4048 asdl_seq_SET(targets, i, value);
4049 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004050 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004051 } else {
4052 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4053 return 1;
4054 }
4055 *out = Delete(targets, lineno, col_offset, arena);
4056 if (*out == NULL) goto failed;
4057 return 0;
4058 }
4059 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4060 if (isinstance == -1) {
4061 return 1;
4062 }
4063 if (isinstance) {
4064 asdl_seq* targets;
4065 expr_ty value;
4066
4067 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4068 int res;
4069 Py_ssize_t len;
4070 Py_ssize_t i;
4071 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4072 if (tmp == NULL) goto failed;
4073 if (!PyList_Check(tmp)) {
4074 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4075 goto failed;
4076 }
4077 len = PyList_GET_SIZE(tmp);
4078 targets = asdl_seq_new(len, arena);
4079 if (targets == NULL) goto failed;
4080 for (i = 0; i < len; i++) {
4081 expr_ty value;
4082 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4083 if (res != 0) goto failed;
4084 asdl_seq_SET(targets, i, value);
4085 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004086 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004087 } else {
4088 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4089 return 1;
4090 }
4091 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4092 int res;
4093 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4094 if (tmp == NULL) goto failed;
4095 res = obj2ast_expr(tmp, &value, arena);
4096 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004097 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004098 } else {
4099 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4100 return 1;
4101 }
4102 *out = Assign(targets, value, lineno, col_offset, arena);
4103 if (*out == NULL) goto failed;
4104 return 0;
4105 }
4106 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4107 if (isinstance == -1) {
4108 return 1;
4109 }
4110 if (isinstance) {
4111 expr_ty target;
4112 operator_ty op;
4113 expr_ty value;
4114
4115 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4116 int res;
4117 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4118 if (tmp == NULL) goto failed;
4119 res = obj2ast_expr(tmp, &target, arena);
4120 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004121 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004122 } else {
4123 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4124 return 1;
4125 }
4126 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4127 int res;
4128 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4129 if (tmp == NULL) goto failed;
4130 res = obj2ast_operator(tmp, &op, arena);
4131 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004132 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004133 } else {
4134 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4135 return 1;
4136 }
4137 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4138 int res;
4139 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4140 if (tmp == NULL) goto failed;
4141 res = obj2ast_expr(tmp, &value, arena);
4142 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004143 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004144 } else {
4145 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4146 return 1;
4147 }
4148 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4149 if (*out == NULL) goto failed;
4150 return 0;
4151 }
4152 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4153 if (isinstance == -1) {
4154 return 1;
4155 }
4156 if (isinstance) {
4157 expr_ty target;
4158 expr_ty iter;
4159 asdl_seq* body;
4160 asdl_seq* orelse;
4161
4162 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4163 int res;
4164 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4165 if (tmp == NULL) goto failed;
4166 res = obj2ast_expr(tmp, &target, arena);
4167 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004168 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004169 } else {
4170 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4171 return 1;
4172 }
4173 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4174 int res;
4175 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4176 if (tmp == NULL) goto failed;
4177 res = obj2ast_expr(tmp, &iter, arena);
4178 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004179 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004180 } else {
4181 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4182 return 1;
4183 }
4184 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4185 int res;
4186 Py_ssize_t len;
4187 Py_ssize_t i;
4188 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4189 if (tmp == NULL) goto failed;
4190 if (!PyList_Check(tmp)) {
4191 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4192 goto failed;
4193 }
4194 len = PyList_GET_SIZE(tmp);
4195 body = asdl_seq_new(len, arena);
4196 if (body == NULL) goto failed;
4197 for (i = 0; i < len; i++) {
4198 stmt_ty value;
4199 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4200 if (res != 0) goto failed;
4201 asdl_seq_SET(body, i, value);
4202 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004203 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004204 } else {
4205 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4206 return 1;
4207 }
4208 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4209 int res;
4210 Py_ssize_t len;
4211 Py_ssize_t i;
4212 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4213 if (tmp == NULL) goto failed;
4214 if (!PyList_Check(tmp)) {
4215 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4216 goto failed;
4217 }
4218 len = PyList_GET_SIZE(tmp);
4219 orelse = asdl_seq_new(len, arena);
4220 if (orelse == NULL) goto failed;
4221 for (i = 0; i < len; i++) {
4222 stmt_ty value;
4223 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4224 if (res != 0) goto failed;
4225 asdl_seq_SET(orelse, i, value);
4226 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004227 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004228 } else {
4229 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4230 return 1;
4231 }
4232 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4233 if (*out == NULL) goto failed;
4234 return 0;
4235 }
4236 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4237 if (isinstance == -1) {
4238 return 1;
4239 }
4240 if (isinstance) {
4241 expr_ty test;
4242 asdl_seq* body;
4243 asdl_seq* orelse;
4244
4245 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4246 int res;
4247 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4248 if (tmp == NULL) goto failed;
4249 res = obj2ast_expr(tmp, &test, arena);
4250 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004251 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004252 } else {
4253 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4254 return 1;
4255 }
4256 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4257 int res;
4258 Py_ssize_t len;
4259 Py_ssize_t i;
4260 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4261 if (tmp == NULL) goto failed;
4262 if (!PyList_Check(tmp)) {
4263 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4264 goto failed;
4265 }
4266 len = PyList_GET_SIZE(tmp);
4267 body = asdl_seq_new(len, arena);
4268 if (body == NULL) goto failed;
4269 for (i = 0; i < len; i++) {
4270 stmt_ty value;
4271 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4272 if (res != 0) goto failed;
4273 asdl_seq_SET(body, i, value);
4274 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004275 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004276 } else {
4277 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4278 return 1;
4279 }
4280 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4281 int res;
4282 Py_ssize_t len;
4283 Py_ssize_t i;
4284 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4285 if (tmp == NULL) goto failed;
4286 if (!PyList_Check(tmp)) {
4287 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4288 goto failed;
4289 }
4290 len = PyList_GET_SIZE(tmp);
4291 orelse = asdl_seq_new(len, arena);
4292 if (orelse == NULL) goto failed;
4293 for (i = 0; i < len; i++) {
4294 stmt_ty value;
4295 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4296 if (res != 0) goto failed;
4297 asdl_seq_SET(orelse, i, value);
4298 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004299 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004300 } else {
4301 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4302 return 1;
4303 }
4304 *out = While(test, body, orelse, lineno, col_offset, arena);
4305 if (*out == NULL) goto failed;
4306 return 0;
4307 }
4308 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4309 if (isinstance == -1) {
4310 return 1;
4311 }
4312 if (isinstance) {
4313 expr_ty test;
4314 asdl_seq* body;
4315 asdl_seq* orelse;
4316
4317 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4318 int res;
4319 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4320 if (tmp == NULL) goto failed;
4321 res = obj2ast_expr(tmp, &test, arena);
4322 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004323 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4326 return 1;
4327 }
4328 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4329 int res;
4330 Py_ssize_t len;
4331 Py_ssize_t i;
4332 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4333 if (tmp == NULL) goto failed;
4334 if (!PyList_Check(tmp)) {
4335 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4336 goto failed;
4337 }
4338 len = PyList_GET_SIZE(tmp);
4339 body = asdl_seq_new(len, arena);
4340 if (body == NULL) goto failed;
4341 for (i = 0; i < len; i++) {
4342 stmt_ty value;
4343 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4344 if (res != 0) goto failed;
4345 asdl_seq_SET(body, i, value);
4346 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004347 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004348 } else {
4349 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4350 return 1;
4351 }
4352 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4353 int res;
4354 Py_ssize_t len;
4355 Py_ssize_t i;
4356 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4357 if (tmp == NULL) goto failed;
4358 if (!PyList_Check(tmp)) {
4359 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4360 goto failed;
4361 }
4362 len = PyList_GET_SIZE(tmp);
4363 orelse = asdl_seq_new(len, arena);
4364 if (orelse == NULL) goto failed;
4365 for (i = 0; i < len; i++) {
4366 stmt_ty value;
4367 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4368 if (res != 0) goto failed;
4369 asdl_seq_SET(orelse, i, value);
4370 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004371 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004372 } else {
4373 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4374 return 1;
4375 }
4376 *out = If(test, body, orelse, lineno, col_offset, arena);
4377 if (*out == NULL) goto failed;
4378 return 0;
4379 }
4380 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4381 if (isinstance == -1) {
4382 return 1;
4383 }
4384 if (isinstance) {
4385 asdl_seq* items;
4386 asdl_seq* body;
4387
4388 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4389 int res;
4390 Py_ssize_t len;
4391 Py_ssize_t i;
4392 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4393 if (tmp == NULL) goto failed;
4394 if (!PyList_Check(tmp)) {
4395 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4396 goto failed;
4397 }
4398 len = PyList_GET_SIZE(tmp);
4399 items = asdl_seq_new(len, arena);
4400 if (items == NULL) goto failed;
4401 for (i = 0; i < len; i++) {
4402 withitem_ty value;
4403 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4404 if (res != 0) goto failed;
4405 asdl_seq_SET(items, i, value);
4406 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004407 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004408 } else {
4409 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4410 return 1;
4411 }
4412 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4413 int res;
4414 Py_ssize_t len;
4415 Py_ssize_t i;
4416 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4417 if (tmp == NULL) goto failed;
4418 if (!PyList_Check(tmp)) {
4419 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4420 goto failed;
4421 }
4422 len = PyList_GET_SIZE(tmp);
4423 body = asdl_seq_new(len, arena);
4424 if (body == NULL) goto failed;
4425 for (i = 0; i < len; i++) {
4426 stmt_ty value;
4427 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4428 if (res != 0) goto failed;
4429 asdl_seq_SET(body, i, value);
4430 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004431 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004432 } else {
4433 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4434 return 1;
4435 }
4436 *out = With(items, body, lineno, col_offset, arena);
4437 if (*out == NULL) goto failed;
4438 return 0;
4439 }
4440 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4441 if (isinstance == -1) {
4442 return 1;
4443 }
4444 if (isinstance) {
4445 expr_ty exc;
4446 expr_ty cause;
4447
Victor Stinneree4b59c2013-07-27 00:01:35 +02004448 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004449 int res;
4450 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4451 if (tmp == NULL) goto failed;
4452 res = obj2ast_expr(tmp, &exc, arena);
4453 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004454 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004455 } else {
4456 exc = NULL;
4457 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004458 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004459 int res;
4460 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4461 if (tmp == NULL) goto failed;
4462 res = obj2ast_expr(tmp, &cause, arena);
4463 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004464 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004465 } else {
4466 cause = NULL;
4467 }
4468 *out = Raise(exc, cause, lineno, col_offset, arena);
4469 if (*out == NULL) goto failed;
4470 return 0;
4471 }
4472 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4473 if (isinstance == -1) {
4474 return 1;
4475 }
4476 if (isinstance) {
4477 asdl_seq* body;
4478 asdl_seq* handlers;
4479 asdl_seq* orelse;
4480 asdl_seq* finalbody;
4481
4482 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4483 int res;
4484 Py_ssize_t len;
4485 Py_ssize_t i;
4486 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4487 if (tmp == NULL) goto failed;
4488 if (!PyList_Check(tmp)) {
4489 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4490 goto failed;
4491 }
4492 len = PyList_GET_SIZE(tmp);
4493 body = asdl_seq_new(len, arena);
4494 if (body == NULL) goto failed;
4495 for (i = 0; i < len; i++) {
4496 stmt_ty value;
4497 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4498 if (res != 0) goto failed;
4499 asdl_seq_SET(body, i, value);
4500 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004501 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004502 } else {
4503 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4504 return 1;
4505 }
4506 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4507 int res;
4508 Py_ssize_t len;
4509 Py_ssize_t i;
4510 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4511 if (tmp == NULL) goto failed;
4512 if (!PyList_Check(tmp)) {
4513 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4514 goto failed;
4515 }
4516 len = PyList_GET_SIZE(tmp);
4517 handlers = asdl_seq_new(len, arena);
4518 if (handlers == NULL) goto failed;
4519 for (i = 0; i < len; i++) {
4520 excepthandler_ty value;
4521 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4522 if (res != 0) goto failed;
4523 asdl_seq_SET(handlers, i, value);
4524 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004525 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004526 } else {
4527 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4528 return 1;
4529 }
4530 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4531 int res;
4532 Py_ssize_t len;
4533 Py_ssize_t i;
4534 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4535 if (tmp == NULL) goto failed;
4536 if (!PyList_Check(tmp)) {
4537 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4538 goto failed;
4539 }
4540 len = PyList_GET_SIZE(tmp);
4541 orelse = asdl_seq_new(len, arena);
4542 if (orelse == NULL) goto failed;
4543 for (i = 0; i < len; i++) {
4544 stmt_ty value;
4545 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4546 if (res != 0) goto failed;
4547 asdl_seq_SET(orelse, i, value);
4548 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004549 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004550 } else {
4551 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4552 return 1;
4553 }
4554 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4555 int res;
4556 Py_ssize_t len;
4557 Py_ssize_t i;
4558 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4559 if (tmp == NULL) goto failed;
4560 if (!PyList_Check(tmp)) {
4561 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4562 goto failed;
4563 }
4564 len = PyList_GET_SIZE(tmp);
4565 finalbody = asdl_seq_new(len, arena);
4566 if (finalbody == NULL) goto failed;
4567 for (i = 0; i < len; i++) {
4568 stmt_ty value;
4569 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4570 if (res != 0) goto failed;
4571 asdl_seq_SET(finalbody, i, value);
4572 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004573 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004574 } else {
4575 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4576 return 1;
4577 }
4578 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4579 arena);
4580 if (*out == NULL) goto failed;
4581 return 0;
4582 }
4583 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4584 if (isinstance == -1) {
4585 return 1;
4586 }
4587 if (isinstance) {
4588 expr_ty test;
4589 expr_ty msg;
4590
4591 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4592 int res;
4593 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4594 if (tmp == NULL) goto failed;
4595 res = obj2ast_expr(tmp, &test, arena);
4596 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004597 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004598 } else {
4599 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4600 return 1;
4601 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004602 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004603 int res;
4604 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4605 if (tmp == NULL) goto failed;
4606 res = obj2ast_expr(tmp, &msg, arena);
4607 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004608 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004609 } else {
4610 msg = NULL;
4611 }
4612 *out = Assert(test, msg, lineno, col_offset, arena);
4613 if (*out == NULL) goto failed;
4614 return 0;
4615 }
4616 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4617 if (isinstance == -1) {
4618 return 1;
4619 }
4620 if (isinstance) {
4621 asdl_seq* names;
4622
4623 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4624 int res;
4625 Py_ssize_t len;
4626 Py_ssize_t i;
4627 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4628 if (tmp == NULL) goto failed;
4629 if (!PyList_Check(tmp)) {
4630 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4631 goto failed;
4632 }
4633 len = PyList_GET_SIZE(tmp);
4634 names = asdl_seq_new(len, arena);
4635 if (names == NULL) goto failed;
4636 for (i = 0; i < len; i++) {
4637 alias_ty value;
4638 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4639 if (res != 0) goto failed;
4640 asdl_seq_SET(names, i, value);
4641 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004642 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004643 } else {
4644 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4645 return 1;
4646 }
4647 *out = Import(names, lineno, col_offset, arena);
4648 if (*out == NULL) goto failed;
4649 return 0;
4650 }
4651 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4652 if (isinstance == -1) {
4653 return 1;
4654 }
4655 if (isinstance) {
4656 identifier module;
4657 asdl_seq* names;
4658 int level;
4659
Victor Stinneree4b59c2013-07-27 00:01:35 +02004660 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004661 int res;
4662 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4663 if (tmp == NULL) goto failed;
4664 res = obj2ast_identifier(tmp, &module, arena);
4665 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004666 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004667 } else {
4668 module = NULL;
4669 }
4670 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4671 int res;
4672 Py_ssize_t len;
4673 Py_ssize_t i;
4674 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4675 if (tmp == NULL) goto failed;
4676 if (!PyList_Check(tmp)) {
4677 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4678 goto failed;
4679 }
4680 len = PyList_GET_SIZE(tmp);
4681 names = asdl_seq_new(len, arena);
4682 if (names == NULL) goto failed;
4683 for (i = 0; i < len; i++) {
4684 alias_ty value;
4685 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4686 if (res != 0) goto failed;
4687 asdl_seq_SET(names, i, value);
4688 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004689 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004690 } else {
4691 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4692 return 1;
4693 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004694 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004695 int res;
4696 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4697 if (tmp == NULL) goto failed;
4698 res = obj2ast_int(tmp, &level, arena);
4699 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004700 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004701 } else {
4702 level = 0;
4703 }
4704 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4705 if (*out == NULL) goto failed;
4706 return 0;
4707 }
4708 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4709 if (isinstance == -1) {
4710 return 1;
4711 }
4712 if (isinstance) {
4713 asdl_seq* names;
4714
4715 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4716 int res;
4717 Py_ssize_t len;
4718 Py_ssize_t i;
4719 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4720 if (tmp == NULL) goto failed;
4721 if (!PyList_Check(tmp)) {
4722 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4723 goto failed;
4724 }
4725 len = PyList_GET_SIZE(tmp);
4726 names = asdl_seq_new(len, arena);
4727 if (names == NULL) goto failed;
4728 for (i = 0; i < len; i++) {
4729 identifier value;
4730 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4731 if (res != 0) goto failed;
4732 asdl_seq_SET(names, i, value);
4733 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004734 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004735 } else {
4736 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4737 return 1;
4738 }
4739 *out = Global(names, lineno, col_offset, arena);
4740 if (*out == NULL) goto failed;
4741 return 0;
4742 }
4743 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4744 if (isinstance == -1) {
4745 return 1;
4746 }
4747 if (isinstance) {
4748 asdl_seq* names;
4749
4750 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4758 goto failed;
4759 }
4760 len = PyList_GET_SIZE(tmp);
4761 names = asdl_seq_new(len, arena);
4762 if (names == NULL) goto failed;
4763 for (i = 0; i < len; i++) {
4764 identifier value;
4765 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4766 if (res != 0) goto failed;
4767 asdl_seq_SET(names, i, value);
4768 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004770 } else {
4771 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4772 return 1;
4773 }
4774 *out = Nonlocal(names, lineno, col_offset, arena);
4775 if (*out == NULL) goto failed;
4776 return 0;
4777 }
4778 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4779 if (isinstance == -1) {
4780 return 1;
4781 }
4782 if (isinstance) {
4783 expr_ty value;
4784
4785 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4786 int res;
4787 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4788 if (tmp == NULL) goto failed;
4789 res = obj2ast_expr(tmp, &value, arena);
4790 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004791 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004792 } else {
4793 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4794 return 1;
4795 }
4796 *out = Expr(value, lineno, col_offset, arena);
4797 if (*out == NULL) goto failed;
4798 return 0;
4799 }
4800 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4801 if (isinstance == -1) {
4802 return 1;
4803 }
4804 if (isinstance) {
4805
4806 *out = Pass(lineno, col_offset, arena);
4807 if (*out == NULL) goto failed;
4808 return 0;
4809 }
4810 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4811 if (isinstance == -1) {
4812 return 1;
4813 }
4814 if (isinstance) {
4815
4816 *out = Break(lineno, col_offset, arena);
4817 if (*out == NULL) goto failed;
4818 return 0;
4819 }
4820 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4821 if (isinstance == -1) {
4822 return 1;
4823 }
4824 if (isinstance) {
4825
4826 *out = Continue(lineno, col_offset, arena);
4827 if (*out == NULL) goto failed;
4828 return 0;
4829 }
4830
4831 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4832 failed:
4833 Py_XDECREF(tmp);
4834 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004835}
4836
4837int
4838obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4839{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004840 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004841
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004842 PyObject *tmp = NULL;
4843 int lineno;
4844 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004845
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004846 if (obj == Py_None) {
4847 *out = NULL;
4848 return 0;
4849 }
4850 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4851 int res;
4852 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4853 if (tmp == NULL) goto failed;
4854 res = obj2ast_int(tmp, &lineno, arena);
4855 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004856 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004857 } else {
4858 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004859 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004860 }
4861 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4862 int res;
4863 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4864 if (tmp == NULL) goto failed;
4865 res = obj2ast_int(tmp, &col_offset, arena);
4866 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004867 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004868 } else {
4869 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4870 return 1;
4871 }
4872 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4873 if (isinstance == -1) {
4874 return 1;
4875 }
4876 if (isinstance) {
4877 boolop_ty op;
4878 asdl_seq* values;
4879
4880 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4881 int res;
4882 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4883 if (tmp == NULL) goto failed;
4884 res = obj2ast_boolop(tmp, &op, arena);
4885 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004886 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004887 } else {
4888 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4889 return 1;
4890 }
4891 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4892 int res;
4893 Py_ssize_t len;
4894 Py_ssize_t i;
4895 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4896 if (tmp == NULL) goto failed;
4897 if (!PyList_Check(tmp)) {
4898 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4899 goto failed;
4900 }
4901 len = PyList_GET_SIZE(tmp);
4902 values = asdl_seq_new(len, arena);
4903 if (values == NULL) goto failed;
4904 for (i = 0; i < len; i++) {
4905 expr_ty value;
4906 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4907 if (res != 0) goto failed;
4908 asdl_seq_SET(values, i, value);
4909 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004910 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004911 } else {
4912 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4913 return 1;
4914 }
4915 *out = BoolOp(op, values, lineno, col_offset, arena);
4916 if (*out == NULL) goto failed;
4917 return 0;
4918 }
4919 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4920 if (isinstance == -1) {
4921 return 1;
4922 }
4923 if (isinstance) {
4924 expr_ty left;
4925 operator_ty op;
4926 expr_ty right;
4927
4928 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4929 int res;
4930 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4931 if (tmp == NULL) goto failed;
4932 res = obj2ast_expr(tmp, &left, arena);
4933 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004934 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004935 } else {
4936 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4937 return 1;
4938 }
4939 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4940 int res;
4941 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4942 if (tmp == NULL) goto failed;
4943 res = obj2ast_operator(tmp, &op, arena);
4944 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004945 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004946 } else {
4947 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4948 return 1;
4949 }
4950 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4951 int res;
4952 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4953 if (tmp == NULL) goto failed;
4954 res = obj2ast_expr(tmp, &right, arena);
4955 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004956 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004957 } else {
4958 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4959 return 1;
4960 }
4961 *out = BinOp(left, op, right, lineno, col_offset, arena);
4962 if (*out == NULL) goto failed;
4963 return 0;
4964 }
4965 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4966 if (isinstance == -1) {
4967 return 1;
4968 }
4969 if (isinstance) {
4970 unaryop_ty op;
4971 expr_ty operand;
4972
4973 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4974 int res;
4975 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4976 if (tmp == NULL) goto failed;
4977 res = obj2ast_unaryop(tmp, &op, arena);
4978 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004979 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004980 } else {
4981 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4982 return 1;
4983 }
4984 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4985 int res;
4986 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4987 if (tmp == NULL) goto failed;
4988 res = obj2ast_expr(tmp, &operand, arena);
4989 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004990 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004991 } else {
4992 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4993 return 1;
4994 }
4995 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4996 if (*out == NULL) goto failed;
4997 return 0;
4998 }
4999 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5000 if (isinstance == -1) {
5001 return 1;
5002 }
5003 if (isinstance) {
5004 arguments_ty args;
5005 expr_ty body;
5006
5007 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5008 int res;
5009 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5010 if (tmp == NULL) goto failed;
5011 res = obj2ast_arguments(tmp, &args, arena);
5012 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005013 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005014 } else {
5015 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5016 return 1;
5017 }
5018 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5019 int res;
5020 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5021 if (tmp == NULL) goto failed;
5022 res = obj2ast_expr(tmp, &body, arena);
5023 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005024 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005025 } else {
5026 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5027 return 1;
5028 }
5029 *out = Lambda(args, body, lineno, col_offset, arena);
5030 if (*out == NULL) goto failed;
5031 return 0;
5032 }
5033 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5034 if (isinstance == -1) {
5035 return 1;
5036 }
5037 if (isinstance) {
5038 expr_ty test;
5039 expr_ty body;
5040 expr_ty orelse;
5041
5042 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5043 int res;
5044 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5045 if (tmp == NULL) goto failed;
5046 res = obj2ast_expr(tmp, &test, arena);
5047 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005048 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005049 } else {
5050 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5051 return 1;
5052 }
5053 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5054 int res;
5055 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5056 if (tmp == NULL) goto failed;
5057 res = obj2ast_expr(tmp, &body, arena);
5058 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005059 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005060 } else {
5061 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5062 return 1;
5063 }
5064 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5065 int res;
5066 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5067 if (tmp == NULL) goto failed;
5068 res = obj2ast_expr(tmp, &orelse, arena);
5069 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005070 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005071 } else {
5072 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5073 return 1;
5074 }
5075 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5076 if (*out == NULL) goto failed;
5077 return 0;
5078 }
5079 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5080 if (isinstance == -1) {
5081 return 1;
5082 }
5083 if (isinstance) {
5084 asdl_seq* keys;
5085 asdl_seq* values;
5086
5087 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5088 int res;
5089 Py_ssize_t len;
5090 Py_ssize_t i;
5091 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5092 if (tmp == NULL) goto failed;
5093 if (!PyList_Check(tmp)) {
5094 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5095 goto failed;
5096 }
5097 len = PyList_GET_SIZE(tmp);
5098 keys = asdl_seq_new(len, arena);
5099 if (keys == NULL) goto failed;
5100 for (i = 0; i < len; i++) {
5101 expr_ty value;
5102 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5103 if (res != 0) goto failed;
5104 asdl_seq_SET(keys, i, value);
5105 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005106 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005107 } else {
5108 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5109 return 1;
5110 }
5111 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5112 int res;
5113 Py_ssize_t len;
5114 Py_ssize_t i;
5115 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5116 if (tmp == NULL) goto failed;
5117 if (!PyList_Check(tmp)) {
5118 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5119 goto failed;
5120 }
5121 len = PyList_GET_SIZE(tmp);
5122 values = asdl_seq_new(len, arena);
5123 if (values == NULL) goto failed;
5124 for (i = 0; i < len; i++) {
5125 expr_ty value;
5126 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5127 if (res != 0) goto failed;
5128 asdl_seq_SET(values, i, value);
5129 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005130 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005131 } else {
5132 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5133 return 1;
5134 }
5135 *out = Dict(keys, values, lineno, col_offset, arena);
5136 if (*out == NULL) goto failed;
5137 return 0;
5138 }
5139 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5140 if (isinstance == -1) {
5141 return 1;
5142 }
5143 if (isinstance) {
5144 asdl_seq* elts;
5145
5146 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5147 int res;
5148 Py_ssize_t len;
5149 Py_ssize_t i;
5150 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5151 if (tmp == NULL) goto failed;
5152 if (!PyList_Check(tmp)) {
5153 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5154 goto failed;
5155 }
5156 len = PyList_GET_SIZE(tmp);
5157 elts = asdl_seq_new(len, arena);
5158 if (elts == NULL) goto failed;
5159 for (i = 0; i < len; i++) {
5160 expr_ty value;
5161 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5162 if (res != 0) goto failed;
5163 asdl_seq_SET(elts, i, value);
5164 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005165 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005166 } else {
5167 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5168 return 1;
5169 }
5170 *out = Set(elts, lineno, col_offset, arena);
5171 if (*out == NULL) goto failed;
5172 return 0;
5173 }
5174 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5175 if (isinstance == -1) {
5176 return 1;
5177 }
5178 if (isinstance) {
5179 expr_ty elt;
5180 asdl_seq* generators;
5181
5182 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5183 int res;
5184 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5185 if (tmp == NULL) goto failed;
5186 res = obj2ast_expr(tmp, &elt, arena);
5187 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005188 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005189 } else {
5190 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5191 return 1;
5192 }
5193 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5194 int res;
5195 Py_ssize_t len;
5196 Py_ssize_t i;
5197 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5198 if (tmp == NULL) goto failed;
5199 if (!PyList_Check(tmp)) {
5200 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5201 goto failed;
5202 }
5203 len = PyList_GET_SIZE(tmp);
5204 generators = asdl_seq_new(len, arena);
5205 if (generators == NULL) goto failed;
5206 for (i = 0; i < len; i++) {
5207 comprehension_ty value;
5208 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5209 if (res != 0) goto failed;
5210 asdl_seq_SET(generators, i, value);
5211 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005212 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005213 } else {
5214 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5215 return 1;
5216 }
5217 *out = ListComp(elt, generators, lineno, col_offset, arena);
5218 if (*out == NULL) goto failed;
5219 return 0;
5220 }
5221 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5222 if (isinstance == -1) {
5223 return 1;
5224 }
5225 if (isinstance) {
5226 expr_ty elt;
5227 asdl_seq* generators;
5228
5229 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5230 int res;
5231 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5232 if (tmp == NULL) goto failed;
5233 res = obj2ast_expr(tmp, &elt, arena);
5234 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005235 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005236 } else {
5237 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5238 return 1;
5239 }
5240 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5241 int res;
5242 Py_ssize_t len;
5243 Py_ssize_t i;
5244 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5245 if (tmp == NULL) goto failed;
5246 if (!PyList_Check(tmp)) {
5247 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5248 goto failed;
5249 }
5250 len = PyList_GET_SIZE(tmp);
5251 generators = asdl_seq_new(len, arena);
5252 if (generators == NULL) goto failed;
5253 for (i = 0; i < len; i++) {
5254 comprehension_ty value;
5255 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5256 if (res != 0) goto failed;
5257 asdl_seq_SET(generators, i, value);
5258 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005259 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005260 } else {
5261 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5262 return 1;
5263 }
5264 *out = SetComp(elt, generators, lineno, col_offset, arena);
5265 if (*out == NULL) goto failed;
5266 return 0;
5267 }
5268 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5269 if (isinstance == -1) {
5270 return 1;
5271 }
5272 if (isinstance) {
5273 expr_ty key;
5274 expr_ty value;
5275 asdl_seq* generators;
5276
5277 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5278 int res;
5279 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5280 if (tmp == NULL) goto failed;
5281 res = obj2ast_expr(tmp, &key, arena);
5282 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005283 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005284 } else {
5285 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5286 return 1;
5287 }
5288 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5289 int res;
5290 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5291 if (tmp == NULL) goto failed;
5292 res = obj2ast_expr(tmp, &value, arena);
5293 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005294 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005295 } else {
5296 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5297 return 1;
5298 }
5299 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5300 int res;
5301 Py_ssize_t len;
5302 Py_ssize_t i;
5303 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5304 if (tmp == NULL) goto failed;
5305 if (!PyList_Check(tmp)) {
5306 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5307 goto failed;
5308 }
5309 len = PyList_GET_SIZE(tmp);
5310 generators = asdl_seq_new(len, arena);
5311 if (generators == NULL) goto failed;
5312 for (i = 0; i < len; i++) {
5313 comprehension_ty value;
5314 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5315 if (res != 0) goto failed;
5316 asdl_seq_SET(generators, i, value);
5317 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005318 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005319 } else {
5320 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5321 return 1;
5322 }
5323 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5324 if (*out == NULL) goto failed;
5325 return 0;
5326 }
5327 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5328 if (isinstance == -1) {
5329 return 1;
5330 }
5331 if (isinstance) {
5332 expr_ty elt;
5333 asdl_seq* generators;
5334
5335 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5336 int res;
5337 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5338 if (tmp == NULL) goto failed;
5339 res = obj2ast_expr(tmp, &elt, arena);
5340 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005341 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005342 } else {
5343 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5344 return 1;
5345 }
5346 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5347 int res;
5348 Py_ssize_t len;
5349 Py_ssize_t i;
5350 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5351 if (tmp == NULL) goto failed;
5352 if (!PyList_Check(tmp)) {
5353 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5354 goto failed;
5355 }
5356 len = PyList_GET_SIZE(tmp);
5357 generators = asdl_seq_new(len, arena);
5358 if (generators == NULL) goto failed;
5359 for (i = 0; i < len; i++) {
5360 comprehension_ty value;
5361 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5362 if (res != 0) goto failed;
5363 asdl_seq_SET(generators, i, value);
5364 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005365 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005366 } else {
5367 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5368 return 1;
5369 }
5370 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5371 if (*out == NULL) goto failed;
5372 return 0;
5373 }
5374 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5375 if (isinstance == -1) {
5376 return 1;
5377 }
5378 if (isinstance) {
5379 expr_ty value;
5380
Victor Stinneree4b59c2013-07-27 00:01:35 +02005381 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005382 int res;
5383 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5384 if (tmp == NULL) goto failed;
5385 res = obj2ast_expr(tmp, &value, arena);
5386 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005387 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005388 } else {
5389 value = NULL;
5390 }
5391 *out = Yield(value, lineno, col_offset, arena);
5392 if (*out == NULL) goto failed;
5393 return 0;
5394 }
5395 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5396 if (isinstance == -1) {
5397 return 1;
5398 }
5399 if (isinstance) {
5400 expr_ty value;
5401
5402 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5403 int res;
5404 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5405 if (tmp == NULL) goto failed;
5406 res = obj2ast_expr(tmp, &value, arena);
5407 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005408 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005409 } else {
5410 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5411 return 1;
5412 }
5413 *out = YieldFrom(value, lineno, col_offset, arena);
5414 if (*out == NULL) goto failed;
5415 return 0;
5416 }
5417 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5418 if (isinstance == -1) {
5419 return 1;
5420 }
5421 if (isinstance) {
5422 expr_ty left;
5423 asdl_int_seq* ops;
5424 asdl_seq* comparators;
5425
5426 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5427 int res;
5428 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5429 if (tmp == NULL) goto failed;
5430 res = obj2ast_expr(tmp, &left, arena);
5431 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005432 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005433 } else {
5434 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5435 return 1;
5436 }
5437 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5438 int res;
5439 Py_ssize_t len;
5440 Py_ssize_t i;
5441 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5442 if (tmp == NULL) goto failed;
5443 if (!PyList_Check(tmp)) {
5444 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5445 goto failed;
5446 }
5447 len = PyList_GET_SIZE(tmp);
5448 ops = asdl_int_seq_new(len, arena);
5449 if (ops == NULL) goto failed;
5450 for (i = 0; i < len; i++) {
5451 cmpop_ty value;
5452 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5453 if (res != 0) goto failed;
5454 asdl_seq_SET(ops, i, value);
5455 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005456 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005457 } else {
5458 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5459 return 1;
5460 }
5461 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5462 int res;
5463 Py_ssize_t len;
5464 Py_ssize_t i;
5465 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5466 if (tmp == NULL) goto failed;
5467 if (!PyList_Check(tmp)) {
5468 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5469 goto failed;
5470 }
5471 len = PyList_GET_SIZE(tmp);
5472 comparators = asdl_seq_new(len, arena);
5473 if (comparators == NULL) goto failed;
5474 for (i = 0; i < len; i++) {
5475 expr_ty value;
5476 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5477 if (res != 0) goto failed;
5478 asdl_seq_SET(comparators, i, value);
5479 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005480 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005481 } else {
5482 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5483 return 1;
5484 }
5485 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5486 if (*out == NULL) goto failed;
5487 return 0;
5488 }
5489 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5490 if (isinstance == -1) {
5491 return 1;
5492 }
5493 if (isinstance) {
5494 expr_ty func;
5495 asdl_seq* args;
5496 asdl_seq* keywords;
5497 expr_ty starargs;
5498 expr_ty kwargs;
5499
5500 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5501 int res;
5502 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5503 if (tmp == NULL) goto failed;
5504 res = obj2ast_expr(tmp, &func, arena);
5505 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005507 } else {
5508 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5509 return 1;
5510 }
5511 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5512 int res;
5513 Py_ssize_t len;
5514 Py_ssize_t i;
5515 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5516 if (tmp == NULL) goto failed;
5517 if (!PyList_Check(tmp)) {
5518 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5519 goto failed;
5520 }
5521 len = PyList_GET_SIZE(tmp);
5522 args = asdl_seq_new(len, arena);
5523 if (args == NULL) goto failed;
5524 for (i = 0; i < len; i++) {
5525 expr_ty value;
5526 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5527 if (res != 0) goto failed;
5528 asdl_seq_SET(args, i, value);
5529 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005530 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005531 } else {
5532 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5533 return 1;
5534 }
5535 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5536 int res;
5537 Py_ssize_t len;
5538 Py_ssize_t i;
5539 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5540 if (tmp == NULL) goto failed;
5541 if (!PyList_Check(tmp)) {
5542 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5543 goto failed;
5544 }
5545 len = PyList_GET_SIZE(tmp);
5546 keywords = asdl_seq_new(len, arena);
5547 if (keywords == NULL) goto failed;
5548 for (i = 0; i < len; i++) {
5549 keyword_ty value;
5550 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5551 if (res != 0) goto failed;
5552 asdl_seq_SET(keywords, i, value);
5553 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005554 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005555 } else {
5556 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5557 return 1;
5558 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005559 if (exists_not_none(obj, &PyId_starargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005560 int res;
5561 tmp = _PyObject_GetAttrId(obj, &PyId_starargs);
5562 if (tmp == NULL) goto failed;
5563 res = obj2ast_expr(tmp, &starargs, arena);
5564 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005565 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005566 } else {
5567 starargs = NULL;
5568 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005569 if (exists_not_none(obj, &PyId_kwargs)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005570 int res;
5571 tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);
5572 if (tmp == NULL) goto failed;
5573 res = obj2ast_expr(tmp, &kwargs, arena);
5574 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005575 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005576 } else {
5577 kwargs = NULL;
5578 }
5579 *out = Call(func, args, keywords, starargs, kwargs, lineno, col_offset,
5580 arena);
5581 if (*out == NULL) goto failed;
5582 return 0;
5583 }
5584 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5585 if (isinstance == -1) {
5586 return 1;
5587 }
5588 if (isinstance) {
5589 object n;
5590
5591 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5592 int res;
5593 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5594 if (tmp == NULL) goto failed;
5595 res = obj2ast_object(tmp, &n, arena);
5596 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005597 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005598 } else {
5599 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5600 return 1;
5601 }
5602 *out = Num(n, lineno, col_offset, arena);
5603 if (*out == NULL) goto failed;
5604 return 0;
5605 }
5606 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5607 if (isinstance == -1) {
5608 return 1;
5609 }
5610 if (isinstance) {
5611 string s;
5612
5613 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5614 int res;
5615 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5616 if (tmp == NULL) goto failed;
5617 res = obj2ast_string(tmp, &s, arena);
5618 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005619 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005620 } else {
5621 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5622 return 1;
5623 }
5624 *out = Str(s, lineno, col_offset, arena);
5625 if (*out == NULL) goto failed;
5626 return 0;
5627 }
5628 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5629 if (isinstance == -1) {
5630 return 1;
5631 }
5632 if (isinstance) {
5633 bytes s;
5634
5635 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5636 int res;
5637 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5638 if (tmp == NULL) goto failed;
5639 res = obj2ast_bytes(tmp, &s, arena);
5640 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005641 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005642 } else {
5643 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5644 return 1;
5645 }
5646 *out = Bytes(s, lineno, col_offset, arena);
5647 if (*out == NULL) goto failed;
5648 return 0;
5649 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005650 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
5651 if (isinstance == -1) {
5652 return 1;
5653 }
5654 if (isinstance) {
5655 singleton value;
5656
5657 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5658 int res;
5659 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5660 if (tmp == NULL) goto failed;
5661 res = obj2ast_singleton(tmp, &value, arena);
5662 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02005663 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02005664 } else {
5665 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
5666 return 1;
5667 }
5668 *out = NameConstant(value, lineno, col_offset, arena);
5669 if (*out == NULL) goto failed;
5670 return 0;
5671 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005672 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5673 if (isinstance == -1) {
5674 return 1;
5675 }
5676 if (isinstance) {
5677
5678 *out = Ellipsis(lineno, col_offset, arena);
5679 if (*out == NULL) goto failed;
5680 return 0;
5681 }
5682 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5683 if (isinstance == -1) {
5684 return 1;
5685 }
5686 if (isinstance) {
5687 expr_ty value;
5688 identifier attr;
5689 expr_context_ty ctx;
5690
5691 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5692 int res;
5693 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5694 if (tmp == NULL) goto failed;
5695 res = obj2ast_expr(tmp, &value, arena);
5696 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005697 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005698 } else {
5699 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5700 return 1;
5701 }
5702 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5703 int res;
5704 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5705 if (tmp == NULL) goto failed;
5706 res = obj2ast_identifier(tmp, &attr, arena);
5707 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005708 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005709 } else {
5710 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5711 return 1;
5712 }
5713 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5714 int res;
5715 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5716 if (tmp == NULL) goto failed;
5717 res = obj2ast_expr_context(tmp, &ctx, arena);
5718 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005719 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005720 } else {
5721 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5722 return 1;
5723 }
5724 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5725 if (*out == NULL) goto failed;
5726 return 0;
5727 }
5728 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5729 if (isinstance == -1) {
5730 return 1;
5731 }
5732 if (isinstance) {
5733 expr_ty value;
5734 slice_ty slice;
5735 expr_context_ty ctx;
5736
5737 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5738 int res;
5739 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5740 if (tmp == NULL) goto failed;
5741 res = obj2ast_expr(tmp, &value, arena);
5742 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005743 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005744 } else {
5745 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5746 return 1;
5747 }
5748 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5749 int res;
5750 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5751 if (tmp == NULL) goto failed;
5752 res = obj2ast_slice(tmp, &slice, arena);
5753 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005754 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005755 } else {
5756 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5757 return 1;
5758 }
5759 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5760 int res;
5761 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5762 if (tmp == NULL) goto failed;
5763 res = obj2ast_expr_context(tmp, &ctx, arena);
5764 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005765 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005766 } else {
5767 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5768 return 1;
5769 }
5770 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5771 if (*out == NULL) goto failed;
5772 return 0;
5773 }
5774 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5775 if (isinstance == -1) {
5776 return 1;
5777 }
5778 if (isinstance) {
5779 expr_ty value;
5780 expr_context_ty ctx;
5781
5782 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5783 int res;
5784 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5785 if (tmp == NULL) goto failed;
5786 res = obj2ast_expr(tmp, &value, arena);
5787 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005788 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005789 } else {
5790 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5791 return 1;
5792 }
5793 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5794 int res;
5795 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5796 if (tmp == NULL) goto failed;
5797 res = obj2ast_expr_context(tmp, &ctx, arena);
5798 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005799 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005800 } else {
5801 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5802 return 1;
5803 }
5804 *out = Starred(value, ctx, lineno, col_offset, arena);
5805 if (*out == NULL) goto failed;
5806 return 0;
5807 }
5808 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5809 if (isinstance == -1) {
5810 return 1;
5811 }
5812 if (isinstance) {
5813 identifier id;
5814 expr_context_ty ctx;
5815
5816 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5817 int res;
5818 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5819 if (tmp == NULL) goto failed;
5820 res = obj2ast_identifier(tmp, &id, arena);
5821 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005822 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005823 } else {
5824 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5825 return 1;
5826 }
5827 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5828 int res;
5829 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5830 if (tmp == NULL) goto failed;
5831 res = obj2ast_expr_context(tmp, &ctx, arena);
5832 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005833 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005834 } else {
5835 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5836 return 1;
5837 }
5838 *out = Name(id, ctx, lineno, col_offset, arena);
5839 if (*out == NULL) goto failed;
5840 return 0;
5841 }
5842 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5843 if (isinstance == -1) {
5844 return 1;
5845 }
5846 if (isinstance) {
5847 asdl_seq* elts;
5848 expr_context_ty ctx;
5849
5850 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5851 int res;
5852 Py_ssize_t len;
5853 Py_ssize_t i;
5854 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5855 if (tmp == NULL) goto failed;
5856 if (!PyList_Check(tmp)) {
5857 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5858 goto failed;
5859 }
5860 len = PyList_GET_SIZE(tmp);
5861 elts = asdl_seq_new(len, arena);
5862 if (elts == NULL) goto failed;
5863 for (i = 0; i < len; i++) {
5864 expr_ty value;
5865 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5866 if (res != 0) goto failed;
5867 asdl_seq_SET(elts, i, value);
5868 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005869 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005870 } else {
5871 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5872 return 1;
5873 }
5874 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5875 int res;
5876 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5877 if (tmp == NULL) goto failed;
5878 res = obj2ast_expr_context(tmp, &ctx, arena);
5879 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005880 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005881 } else {
5882 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5883 return 1;
5884 }
5885 *out = List(elts, ctx, lineno, col_offset, arena);
5886 if (*out == NULL) goto failed;
5887 return 0;
5888 }
5889 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5890 if (isinstance == -1) {
5891 return 1;
5892 }
5893 if (isinstance) {
5894 asdl_seq* elts;
5895 expr_context_ty ctx;
5896
5897 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5898 int res;
5899 Py_ssize_t len;
5900 Py_ssize_t i;
5901 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5902 if (tmp == NULL) goto failed;
5903 if (!PyList_Check(tmp)) {
5904 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5905 goto failed;
5906 }
5907 len = PyList_GET_SIZE(tmp);
5908 elts = asdl_seq_new(len, arena);
5909 if (elts == NULL) goto failed;
5910 for (i = 0; i < len; i++) {
5911 expr_ty value;
5912 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5913 if (res != 0) goto failed;
5914 asdl_seq_SET(elts, i, value);
5915 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005916 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005917 } else {
5918 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5919 return 1;
5920 }
5921 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5922 int res;
5923 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5924 if (tmp == NULL) goto failed;
5925 res = obj2ast_expr_context(tmp, &ctx, arena);
5926 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005927 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005928 } else {
5929 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5930 return 1;
5931 }
5932 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5933 if (*out == NULL) goto failed;
5934 return 0;
5935 }
5936
5937 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5938 failed:
5939 Py_XDECREF(tmp);
5940 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005941}
5942
5943int
5944obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5945{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005946 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005947
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005948 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5949 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005950 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005951 }
5952 if (isinstance) {
5953 *out = Load;
5954 return 0;
5955 }
5956 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5957 if (isinstance == -1) {
5958 return 1;
5959 }
5960 if (isinstance) {
5961 *out = Store;
5962 return 0;
5963 }
5964 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5965 if (isinstance == -1) {
5966 return 1;
5967 }
5968 if (isinstance) {
5969 *out = Del;
5970 return 0;
5971 }
5972 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5973 if (isinstance == -1) {
5974 return 1;
5975 }
5976 if (isinstance) {
5977 *out = AugLoad;
5978 return 0;
5979 }
5980 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5981 if (isinstance == -1) {
5982 return 1;
5983 }
5984 if (isinstance) {
5985 *out = AugStore;
5986 return 0;
5987 }
5988 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5989 if (isinstance == -1) {
5990 return 1;
5991 }
5992 if (isinstance) {
5993 *out = Param;
5994 return 0;
5995 }
5996
5997 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
5998 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005999}
6000
6001int
6002obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6003{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006004 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006005
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006006 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006007
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006008 if (obj == Py_None) {
6009 *out = NULL;
6010 return 0;
6011 }
6012 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6013 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006014 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006015 }
6016 if (isinstance) {
6017 expr_ty lower;
6018 expr_ty upper;
6019 expr_ty step;
6020
Victor Stinneree4b59c2013-07-27 00:01:35 +02006021 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006022 int res;
6023 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6024 if (tmp == NULL) goto failed;
6025 res = obj2ast_expr(tmp, &lower, arena);
6026 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006027 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006028 } else {
6029 lower = NULL;
6030 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006031 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006032 int res;
6033 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6034 if (tmp == NULL) goto failed;
6035 res = obj2ast_expr(tmp, &upper, arena);
6036 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006037 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006038 } else {
6039 upper = NULL;
6040 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006041 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006042 int res;
6043 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6044 if (tmp == NULL) goto failed;
6045 res = obj2ast_expr(tmp, &step, arena);
6046 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006048 } else {
6049 step = NULL;
6050 }
6051 *out = Slice(lower, upper, step, arena);
6052 if (*out == NULL) goto failed;
6053 return 0;
6054 }
6055 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6056 if (isinstance == -1) {
6057 return 1;
6058 }
6059 if (isinstance) {
6060 asdl_seq* dims;
6061
6062 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6063 int res;
6064 Py_ssize_t len;
6065 Py_ssize_t i;
6066 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6067 if (tmp == NULL) goto failed;
6068 if (!PyList_Check(tmp)) {
6069 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6070 goto failed;
6071 }
6072 len = PyList_GET_SIZE(tmp);
6073 dims = asdl_seq_new(len, arena);
6074 if (dims == NULL) goto failed;
6075 for (i = 0; i < len; i++) {
6076 slice_ty value;
6077 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6078 if (res != 0) goto failed;
6079 asdl_seq_SET(dims, i, value);
6080 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006081 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006082 } else {
6083 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6084 return 1;
6085 }
6086 *out = ExtSlice(dims, arena);
6087 if (*out == NULL) goto failed;
6088 return 0;
6089 }
6090 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6091 if (isinstance == -1) {
6092 return 1;
6093 }
6094 if (isinstance) {
6095 expr_ty value;
6096
6097 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6098 int res;
6099 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6100 if (tmp == NULL) goto failed;
6101 res = obj2ast_expr(tmp, &value, arena);
6102 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006103 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006104 } else {
6105 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6106 return 1;
6107 }
6108 *out = Index(value, arena);
6109 if (*out == NULL) goto failed;
6110 return 0;
6111 }
6112
6113 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6114 failed:
6115 Py_XDECREF(tmp);
6116 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006117}
6118
6119int
6120obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6121{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006122 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006123
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006124 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6125 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006126 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006127 }
6128 if (isinstance) {
6129 *out = And;
6130 return 0;
6131 }
6132 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6133 if (isinstance == -1) {
6134 return 1;
6135 }
6136 if (isinstance) {
6137 *out = Or;
6138 return 0;
6139 }
6140
6141 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6142 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006143}
6144
6145int
6146obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6147{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006148 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006149
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006150 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6151 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006152 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006153 }
6154 if (isinstance) {
6155 *out = Add;
6156 return 0;
6157 }
6158 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6159 if (isinstance == -1) {
6160 return 1;
6161 }
6162 if (isinstance) {
6163 *out = Sub;
6164 return 0;
6165 }
6166 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6167 if (isinstance == -1) {
6168 return 1;
6169 }
6170 if (isinstance) {
6171 *out = Mult;
6172 return 0;
6173 }
6174 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6175 if (isinstance == -1) {
6176 return 1;
6177 }
6178 if (isinstance) {
6179 *out = Div;
6180 return 0;
6181 }
6182 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6183 if (isinstance == -1) {
6184 return 1;
6185 }
6186 if (isinstance) {
6187 *out = Mod;
6188 return 0;
6189 }
6190 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6191 if (isinstance == -1) {
6192 return 1;
6193 }
6194 if (isinstance) {
6195 *out = Pow;
6196 return 0;
6197 }
6198 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6199 if (isinstance == -1) {
6200 return 1;
6201 }
6202 if (isinstance) {
6203 *out = LShift;
6204 return 0;
6205 }
6206 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6207 if (isinstance == -1) {
6208 return 1;
6209 }
6210 if (isinstance) {
6211 *out = RShift;
6212 return 0;
6213 }
6214 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6215 if (isinstance == -1) {
6216 return 1;
6217 }
6218 if (isinstance) {
6219 *out = BitOr;
6220 return 0;
6221 }
6222 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6223 if (isinstance == -1) {
6224 return 1;
6225 }
6226 if (isinstance) {
6227 *out = BitXor;
6228 return 0;
6229 }
6230 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6231 if (isinstance == -1) {
6232 return 1;
6233 }
6234 if (isinstance) {
6235 *out = BitAnd;
6236 return 0;
6237 }
6238 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6239 if (isinstance == -1) {
6240 return 1;
6241 }
6242 if (isinstance) {
6243 *out = FloorDiv;
6244 return 0;
6245 }
6246
6247 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6248 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006249}
6250
6251int
6252obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6253{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006254 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006255
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006256 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6257 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006258 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006259 }
6260 if (isinstance) {
6261 *out = Invert;
6262 return 0;
6263 }
6264 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6265 if (isinstance == -1) {
6266 return 1;
6267 }
6268 if (isinstance) {
6269 *out = Not;
6270 return 0;
6271 }
6272 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6273 if (isinstance == -1) {
6274 return 1;
6275 }
6276 if (isinstance) {
6277 *out = UAdd;
6278 return 0;
6279 }
6280 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6281 if (isinstance == -1) {
6282 return 1;
6283 }
6284 if (isinstance) {
6285 *out = USub;
6286 return 0;
6287 }
6288
6289 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6290 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006291}
6292
6293int
6294obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6295{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006296 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006297
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006298 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6299 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006300 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006301 }
6302 if (isinstance) {
6303 *out = Eq;
6304 return 0;
6305 }
6306 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6307 if (isinstance == -1) {
6308 return 1;
6309 }
6310 if (isinstance) {
6311 *out = NotEq;
6312 return 0;
6313 }
6314 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6315 if (isinstance == -1) {
6316 return 1;
6317 }
6318 if (isinstance) {
6319 *out = Lt;
6320 return 0;
6321 }
6322 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6323 if (isinstance == -1) {
6324 return 1;
6325 }
6326 if (isinstance) {
6327 *out = LtE;
6328 return 0;
6329 }
6330 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6331 if (isinstance == -1) {
6332 return 1;
6333 }
6334 if (isinstance) {
6335 *out = Gt;
6336 return 0;
6337 }
6338 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6339 if (isinstance == -1) {
6340 return 1;
6341 }
6342 if (isinstance) {
6343 *out = GtE;
6344 return 0;
6345 }
6346 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6347 if (isinstance == -1) {
6348 return 1;
6349 }
6350 if (isinstance) {
6351 *out = Is;
6352 return 0;
6353 }
6354 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6355 if (isinstance == -1) {
6356 return 1;
6357 }
6358 if (isinstance) {
6359 *out = IsNot;
6360 return 0;
6361 }
6362 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6363 if (isinstance == -1) {
6364 return 1;
6365 }
6366 if (isinstance) {
6367 *out = In;
6368 return 0;
6369 }
6370 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6371 if (isinstance == -1) {
6372 return 1;
6373 }
6374 if (isinstance) {
6375 *out = NotIn;
6376 return 0;
6377 }
6378
6379 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6380 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006381}
6382
6383int
6384obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6385{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386 PyObject* tmp = NULL;
6387 expr_ty target;
6388 expr_ty iter;
6389 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006390
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006391 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6392 int res;
6393 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6394 if (tmp == NULL) goto failed;
6395 res = obj2ast_expr(tmp, &target, arena);
6396 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006397 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006398 } else {
6399 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006400 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006401 }
6402 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6403 int res;
6404 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6405 if (tmp == NULL) goto failed;
6406 res = obj2ast_expr(tmp, &iter, arena);
6407 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006408 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006409 } else {
6410 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6411 return 1;
6412 }
6413 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6414 int res;
6415 Py_ssize_t len;
6416 Py_ssize_t i;
6417 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6418 if (tmp == NULL) goto failed;
6419 if (!PyList_Check(tmp)) {
6420 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6421 goto failed;
6422 }
6423 len = PyList_GET_SIZE(tmp);
6424 ifs = asdl_seq_new(len, arena);
6425 if (ifs == NULL) goto failed;
6426 for (i = 0; i < len; i++) {
6427 expr_ty value;
6428 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6429 if (res != 0) goto failed;
6430 asdl_seq_SET(ifs, i, value);
6431 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006432 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006433 } else {
6434 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6435 return 1;
6436 }
6437 *out = comprehension(target, iter, ifs, arena);
6438 return 0;
6439failed:
6440 Py_XDECREF(tmp);
6441 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006442}
6443
6444int
6445obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6446{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006447 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006448
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006449 PyObject *tmp = NULL;
6450 int lineno;
6451 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006452
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006453 if (obj == Py_None) {
6454 *out = NULL;
6455 return 0;
6456 }
6457 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6458 int res;
6459 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6460 if (tmp == NULL) goto failed;
6461 res = obj2ast_int(tmp, &lineno, arena);
6462 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006463 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006464 } else {
6465 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006466 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006467 }
6468 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6469 int res;
6470 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6471 if (tmp == NULL) goto failed;
6472 res = obj2ast_int(tmp, &col_offset, arena);
6473 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006474 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006475 } else {
6476 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6477 return 1;
6478 }
6479 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6480 if (isinstance == -1) {
6481 return 1;
6482 }
6483 if (isinstance) {
6484 expr_ty type;
6485 identifier name;
6486 asdl_seq* body;
6487
Victor Stinneree4b59c2013-07-27 00:01:35 +02006488 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006489 int res;
6490 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6491 if (tmp == NULL) goto failed;
6492 res = obj2ast_expr(tmp, &type, arena);
6493 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006494 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006495 } else {
6496 type = NULL;
6497 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006498 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006499 int res;
6500 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6501 if (tmp == NULL) goto failed;
6502 res = obj2ast_identifier(tmp, &name, arena);
6503 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006504 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006505 } else {
6506 name = NULL;
6507 }
6508 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6509 int res;
6510 Py_ssize_t len;
6511 Py_ssize_t i;
6512 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6513 if (tmp == NULL) goto failed;
6514 if (!PyList_Check(tmp)) {
6515 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6516 goto failed;
6517 }
6518 len = PyList_GET_SIZE(tmp);
6519 body = asdl_seq_new(len, arena);
6520 if (body == NULL) goto failed;
6521 for (i = 0; i < len; i++) {
6522 stmt_ty value;
6523 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6524 if (res != 0) goto failed;
6525 asdl_seq_SET(body, i, value);
6526 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006527 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006528 } else {
6529 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6530 return 1;
6531 }
6532 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6533 if (*out == NULL) goto failed;
6534 return 0;
6535 }
6536
6537 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6538 failed:
6539 Py_XDECREF(tmp);
6540 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006541}
6542
6543int
6544obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6545{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006546 PyObject* tmp = NULL;
6547 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006548 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006549 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006551 arg_ty kwarg;
6552 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006553
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006554 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6555 int res;
6556 Py_ssize_t len;
6557 Py_ssize_t i;
6558 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6559 if (tmp == NULL) goto failed;
6560 if (!PyList_Check(tmp)) {
6561 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6562 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006563 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006564 len = PyList_GET_SIZE(tmp);
6565 args = asdl_seq_new(len, arena);
6566 if (args == NULL) goto failed;
6567 for (i = 0; i < len; i++) {
6568 arg_ty value;
6569 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6570 if (res != 0) goto failed;
6571 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006572 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006573 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006574 } else {
6575 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006576 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006577 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006578 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006579 int res;
6580 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6581 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006582 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006584 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006585 } else {
6586 vararg = NULL;
6587 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006588 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6589 int res;
6590 Py_ssize_t len;
6591 Py_ssize_t i;
6592 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6593 if (tmp == NULL) goto failed;
6594 if (!PyList_Check(tmp)) {
6595 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6596 goto failed;
6597 }
6598 len = PyList_GET_SIZE(tmp);
6599 kwonlyargs = asdl_seq_new(len, arena);
6600 if (kwonlyargs == NULL) goto failed;
6601 for (i = 0; i < len; i++) {
6602 arg_ty value;
6603 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6604 if (res != 0) goto failed;
6605 asdl_seq_SET(kwonlyargs, i, value);
6606 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006607 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006608 } else {
6609 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6610 return 1;
6611 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006612 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6613 int res;
6614 Py_ssize_t len;
6615 Py_ssize_t i;
6616 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6617 if (tmp == NULL) goto failed;
6618 if (!PyList_Check(tmp)) {
6619 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6620 goto failed;
6621 }
6622 len = PyList_GET_SIZE(tmp);
6623 kw_defaults = asdl_seq_new(len, arena);
6624 if (kw_defaults == NULL) goto failed;
6625 for (i = 0; i < len; i++) {
6626 expr_ty value;
6627 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6628 if (res != 0) goto failed;
6629 asdl_seq_SET(kw_defaults, i, value);
6630 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006631 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006632 } else {
6633 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6634 return 1;
6635 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006636 if (exists_not_none(obj, &PyId_kwarg)) {
6637 int res;
6638 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6639 if (tmp == NULL) goto failed;
6640 res = obj2ast_arg(tmp, &kwarg, arena);
6641 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006642 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006643 } else {
6644 kwarg = NULL;
6645 }
6646 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6647 int res;
6648 Py_ssize_t len;
6649 Py_ssize_t i;
6650 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6651 if (tmp == NULL) goto failed;
6652 if (!PyList_Check(tmp)) {
6653 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6654 goto failed;
6655 }
6656 len = PyList_GET_SIZE(tmp);
6657 defaults = asdl_seq_new(len, arena);
6658 if (defaults == NULL) goto failed;
6659 for (i = 0; i < len; i++) {
6660 expr_ty value;
6661 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6662 if (res != 0) goto failed;
6663 asdl_seq_SET(defaults, i, value);
6664 }
Victor Stinnerb3189902013-07-27 00:04:42 +02006665 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006666 } else {
6667 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6668 return 1;
6669 }
6670 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
6671 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006672 return 0;
6673failed:
6674 Py_XDECREF(tmp);
6675 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006676}
6677
6678int
6679obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6680{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006681 PyObject* tmp = NULL;
6682 identifier arg;
6683 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006684
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006685 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6686 int res;
6687 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6688 if (tmp == NULL) goto failed;
6689 res = obj2ast_identifier(tmp, &arg, arena);
6690 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006691 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006692 } else {
6693 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006694 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006695 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006696 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006697 int res;
6698 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6699 if (tmp == NULL) goto failed;
6700 res = obj2ast_expr(tmp, &annotation, arena);
6701 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006702 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006703 } else {
6704 annotation = NULL;
6705 }
6706 *out = arg(arg, annotation, arena);
6707 return 0;
6708failed:
6709 Py_XDECREF(tmp);
6710 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006711}
6712
6713int
6714obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6715{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006716 PyObject* tmp = NULL;
6717 identifier arg;
6718 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006719
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006720 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6721 int res;
6722 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6723 if (tmp == NULL) goto failed;
6724 res = obj2ast_identifier(tmp, &arg, arena);
6725 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006726 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006727 } else {
6728 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006729 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006730 }
6731 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6732 int res;
6733 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6734 if (tmp == NULL) goto failed;
6735 res = obj2ast_expr(tmp, &value, arena);
6736 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006737 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006738 } else {
6739 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6740 return 1;
6741 }
6742 *out = keyword(arg, value, arena);
6743 return 0;
6744failed:
6745 Py_XDECREF(tmp);
6746 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006747}
6748
6749int
6750obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6751{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006752 PyObject* tmp = NULL;
6753 identifier name;
6754 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006755
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006756 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6757 int res;
6758 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6759 if (tmp == NULL) goto failed;
6760 res = obj2ast_identifier(tmp, &name, arena);
6761 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006762 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006763 } else {
6764 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006765 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006766 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006767 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006768 int res;
6769 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6770 if (tmp == NULL) goto failed;
6771 res = obj2ast_identifier(tmp, &asname, arena);
6772 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006773 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006774 } else {
6775 asname = NULL;
6776 }
6777 *out = alias(name, asname, arena);
6778 return 0;
6779failed:
6780 Py_XDECREF(tmp);
6781 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006782}
6783
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006784int
6785obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6786{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006787 PyObject* tmp = NULL;
6788 expr_ty context_expr;
6789 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006790
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006791 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6792 int res;
6793 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6794 if (tmp == NULL) goto failed;
6795 res = obj2ast_expr(tmp, &context_expr, arena);
6796 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006798 } else {
6799 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006800 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006801 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006802 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006803 int res;
6804 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6805 if (tmp == NULL) goto failed;
6806 res = obj2ast_expr(tmp, &optional_vars, arena);
6807 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006808 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006809 } else {
6810 optional_vars = NULL;
6811 }
6812 *out = withitem(context_expr, optional_vars, arena);
6813 return 0;
6814failed:
6815 Py_XDECREF(tmp);
6816 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006817}
6818
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006819
Martin v. Löwis1a214512008-06-11 05:26:20 +00006820static struct PyModuleDef _astmodule = {
6821 PyModuleDef_HEAD_INIT, "_ast"
6822};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006823PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006824PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006825{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006826 PyObject *m, *d;
6827 if (!init_types()) return NULL;
6828 m = PyModule_Create(&_astmodule);
6829 if (!m) return NULL;
6830 d = PyModule_GetDict(m);
6831 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006832 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 return NULL;
6834 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6835 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6836 NULL;
6837 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6838 0) return NULL;
6839 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6840 return NULL;
6841 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6842 NULL;
6843 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6844 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6845 0) return NULL;
6846 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6847 return NULL;
6848 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6849 NULL;
6850 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6851 NULL;
6852 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6853 NULL;
6854 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6855 return NULL;
6856 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6857 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6858 NULL;
6859 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6860 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6861 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6862 NULL;
6863 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6864 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6865 NULL;
6866 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6867 NULL;
6868 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6869 return NULL;
6870 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6871 NULL;
6872 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6873 return NULL;
6874 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6875 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6876 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6877 NULL;
6878 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6879 return NULL;
6880 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6881 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6882 NULL;
6883 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6884 NULL;
6885 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6886 NULL;
6887 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6888 NULL;
6889 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6890 NULL;
6891 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6892 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6893 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6894 return NULL;
6895 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6896 NULL;
6897 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6898 return NULL;
6899 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6900 0) return NULL;
6901 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6904 return NULL;
6905 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6908 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6909 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6910 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6911 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006912 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
6913 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006914 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6915 return NULL;
6916 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6917 return NULL;
6918 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6919 return NULL;
6920 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6921 NULL;
6922 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6923 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6924 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6925 NULL;
6926 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6927 0) return NULL;
6928 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6929 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6930 NULL;
6931 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
6932 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
6933 NULL;
6934 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6935 return NULL;
6936 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6937 NULL;
6938 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6939 NULL;
6940 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6941 NULL;
6942 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6943 return NULL;
6944 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6945 NULL;
6946 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
6947 NULL;
6948 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
6949 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
6950 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6951 return NULL;
6952 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
6953 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
6954 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
6955 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
6956 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
6957 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
6958 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
6959 NULL;
6960 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
6961 NULL;
6962 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6963 NULL;
6964 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
6965 NULL;
6966 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
6967 NULL;
6968 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6969 return NULL;
6970 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
6971 NULL;
6972 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
6973 NULL;
6974 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
6975 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
6976 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
6977 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6978 NULL;
6979 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6980 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6981 NULL;
6982 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6983 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
6984 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6985 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
6986 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6987 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6988 NULL;
6989 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6990 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6991 NULL;
6992 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
6993 < 0) return NULL;
6994 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
6995 < 0) return NULL;
6996 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
6997 < 0) return NULL;
6998 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
6999 return NULL;
7000 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7001 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7002 NULL;
7003 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7004 NULL;
7005 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7006 return NULL;
7007 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007008}
7009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007010
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007011PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007012{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007013 if (!init_types())
7014 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007015 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007016}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007017
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007018/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7019mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007020{
7021 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007022 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
7023 (PyObject*)Interactive_type};
7024 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007025 int isinstance;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007026 assert(0 <= mode && mode <= 2);
7027
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007028 if (!init_types())
7029 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007030
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007031 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7032 if (isinstance == -1)
7033 return NULL;
7034 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007035 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7036 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007037 return NULL;
7038 }
7039 if (obj2ast_mod(ast, &res, arena) != 0)
7040 return NULL;
7041 else
7042 return res;
7043}
7044
7045int PyAST_Check(PyObject* obj)
7046{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007047 if (!init_types())
7048 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007049 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007050}
7051
Martin v. Löwis5b222132007-06-10 09:51:05 +00007052