blob: 6dd175f492e8dd56330dcaac3701d418512457b5 [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);
Neal Norwitz53d960c2006-02-28 22:47:29 +000051static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020052 "name",
53 "bases",
54 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020055 "body",
56 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000057};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020059_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000060static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020061 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000062};
Neal Norwitz53d960c2006-02-28 22:47:29 +000063static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020064_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000065static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020066 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000067};
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static PyTypeObject *Assign_type;
69static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020070 "targets",
71 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000072};
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020074_Py_IDENTIFIER(target);
75_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020077 "target",
78 "op",
79 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(iter);
83_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020085 "target",
86 "iter",
87 "body",
88 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000089};
Neal Norwitz53d960c2006-02-28 22:47:29 +000090static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020091_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020093 "test",
94 "body",
95 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000096};
Neal Norwitz53d960c2006-02-28 22:47:29 +000097static PyTypeObject *If_type;
98static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020099 "test",
100 "body",
101 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000102};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000103static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200104_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000105static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200106 "items",
107 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000108};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000109static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200110_Py_IDENTIFIER(exc);
111_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000112static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200113 "exc",
114 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500116static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200117_Py_IDENTIFIER(handlers);
118_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500119static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200120 "body",
121 "handlers",
122 "orelse",
123 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000124};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000125static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200126_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200128 "test",
129 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000130};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000131static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200132_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000133static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200134 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200137_Py_IDENTIFIER(module);
138_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000139static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200140 "module",
141 "names",
142 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000143};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000144static PyTypeObject *Global_type;
145static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200146 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000147};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000148static PyTypeObject *Nonlocal_type;
149static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200150 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000151};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000152static PyTypeObject *Expr_type;
153static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200154 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000155};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static PyTypeObject *Pass_type;
157static PyTypeObject *Break_type;
158static PyTypeObject *Continue_type;
159static PyTypeObject *expr_type;
160static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200161 "lineno",
162 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000163};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000164static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200166_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000167static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200168 "op",
169 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000170};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000171static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200172_Py_IDENTIFIER(left);
173_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000174static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200175 "left",
176 "op",
177 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000178};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000179static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200180_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200182 "op",
183 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *Lambda_type;
186static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200187 "args",
188 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000189};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000190static PyTypeObject *IfExp_type;
191static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200192 "test",
193 "body",
194 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000195};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200197_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000198static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200199 "keys",
200 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000201};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000202static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200203_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000204static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200205 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000206};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000207static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200208_Py_IDENTIFIER(elt);
209_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200211 "elt",
212 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000214static PyTypeObject *SetComp_type;
215static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200216 "elt",
217 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000218};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000219static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200220_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000221static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200222 "key",
223 "value",
224 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000225};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000226static PyTypeObject *GeneratorExp_type;
227static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200228 "elt",
229 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000231static PyTypeObject *Yield_type;
232static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200233 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500234};
235static PyTypeObject *YieldFrom_type;
236static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200237 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000238};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000239static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200240_Py_IDENTIFIER(ops);
241_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000242static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200243 "left",
244 "ops",
245 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000246};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000247static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200248_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000249static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200250 "func",
251 "args",
252 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000253};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000254static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200255_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000256static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200257 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000258};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000259static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200260_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000261static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200262 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000263};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000264static PyTypeObject *Bytes_type;
265static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200266 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000267};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500268static PyTypeObject *NameConstant_type;
269static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200270 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500271};
Georg Brandl52318d62006-09-06 07:06:08 +0000272static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200274_Py_IDENTIFIER(attr);
275_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000276static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200277 "value",
278 "attr",
279 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000280};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200282_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000283static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200284 "value",
285 "slice",
286 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287};
Guido van Rossum0368b722007-05-11 16:50:42 +0000288static PyTypeObject *Starred_type;
289static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "value",
291 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000292};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000293static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200294_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000295static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200296 "id",
297 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000299static PyTypeObject *List_type;
300static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200301 "elts",
302 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000303};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000304static PyTypeObject *Tuple_type;
305static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200306 "elts",
307 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000308};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000309static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000310static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
311*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
312static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000313static PyTypeObject *Load_type;
314static PyTypeObject *Store_type;
315static PyTypeObject *Del_type;
316static PyTypeObject *AugLoad_type;
317static PyTypeObject *AugStore_type;
318static PyTypeObject *Param_type;
319static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000320static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000321static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200322_Py_IDENTIFIER(lower);
323_Py_IDENTIFIER(upper);
324_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000325static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200326 "lower",
327 "upper",
328 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000329};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000330static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200331_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000332static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200333 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000335static PyTypeObject *Index_type;
336static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200337 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000338};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000339static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000340static PyObject *And_singleton, *Or_singleton;
341static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000342static PyTypeObject *And_type;
343static PyTypeObject *Or_type;
344static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000345static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400346*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
347*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
348*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000349static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000350static PyTypeObject *Add_type;
351static PyTypeObject *Sub_type;
352static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400353static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000354static PyTypeObject *Div_type;
355static PyTypeObject *Mod_type;
356static PyTypeObject *Pow_type;
357static PyTypeObject *LShift_type;
358static PyTypeObject *RShift_type;
359static PyTypeObject *BitOr_type;
360static PyTypeObject *BitXor_type;
361static PyTypeObject *BitAnd_type;
362static PyTypeObject *FloorDiv_type;
363static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000364static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
365*USub_singleton;
366static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000367static PyTypeObject *Invert_type;
368static PyTypeObject *Not_type;
369static PyTypeObject *UAdd_type;
370static PyTypeObject *USub_type;
371static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000372static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
373*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
374*NotIn_singleton;
375static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000376static PyTypeObject *Eq_type;
377static PyTypeObject *NotEq_type;
378static PyTypeObject *Lt_type;
379static PyTypeObject *LtE_type;
380static PyTypeObject *Gt_type;
381static PyTypeObject *GtE_type;
382static PyTypeObject *Is_type;
383static PyTypeObject *IsNot_type;
384static PyTypeObject *In_type;
385static PyTypeObject *NotIn_type;
386static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200388_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000389static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200390 "target",
391 "iter",
392 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000393};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000394static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000395static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200396 "lineno",
397 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000398};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000399static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000400static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200401_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000402static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200403 "type",
404 "name",
405 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000406};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000407static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000408static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200409_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200410_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200411_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700412_Py_IDENTIFIER(kwarg);
413_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000414static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200415 "args",
416 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200417 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200418 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200419 "kwarg",
420 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000421};
Neal Norwitzc1505362006-12-28 06:47:50 +0000422static PyTypeObject *arg_type;
423static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700424static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200425 "lineno",
426 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700427};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200428_Py_IDENTIFIER(arg);
429_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000430static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200431 "arg",
432 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000433};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000434static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000435static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000436static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200437 "arg",
438 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000439};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000440static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000441static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200442_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000443static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200444 "name",
445 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000446};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500447static PyTypeObject *withitem_type;
448static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200449_Py_IDENTIFIER(context_expr);
450_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500451static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200452 "context_expr",
453 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500454};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000455
456
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700457typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100458 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700459 PyObject *dict;
460} AST_object;
461
Benjamin Peterson1767e022012-03-14 21:50:29 -0500462static void
463ast_dealloc(AST_object *self)
464{
465 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200466 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500467}
468
Neal Norwitz207c9f32008-03-31 04:42:11 +0000469static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700470ast_traverse(AST_object *self, visitproc visit, void *arg)
471{
472 Py_VISIT(self->dict);
473 return 0;
474}
475
476static void
477ast_clear(AST_object *self)
478{
479 Py_CLEAR(self->dict);
480}
481
482static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000483ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
484{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200485 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000486 Py_ssize_t i, numfields = 0;
487 int res = -1;
488 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200489 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000490 if (!fields)
491 PyErr_Clear();
492 if (fields) {
493 numfields = PySequence_Size(fields);
494 if (numfields == -1)
495 goto cleanup;
496 }
497 res = 0; /* if no error occurs, this stays 0 to the end */
498 if (PyTuple_GET_SIZE(args) > 0) {
499 if (numfields != PyTuple_GET_SIZE(args)) {
500 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000501 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000502 Py_TYPE(self)->tp_name,
503 numfields == 0 ? "" : "either 0 or ",
504 numfields, numfields == 1 ? "" : "s");
505 res = -1;
506 goto cleanup;
507 }
508 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
509 /* cannot be reached when fields is NULL */
510 PyObject *name = PySequence_GetItem(fields, i);
511 if (!name) {
512 res = -1;
513 goto cleanup;
514 }
515 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
516 Py_DECREF(name);
517 if (res < 0)
518 goto cleanup;
519 }
520 }
521 if (kw) {
522 i = 0; /* needed by PyDict_Next */
523 while (PyDict_Next(kw, &i, &key, &value)) {
524 res = PyObject_SetAttr(self, key, value);
525 if (res < 0)
526 goto cleanup;
527 }
528 }
529 cleanup:
530 Py_XDECREF(fields);
531 return res;
532}
533
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000534/* Pickling support */
535static PyObject *
536ast_type_reduce(PyObject *self, PyObject *unused)
537{
538 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200539 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200540 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000541 if (dict == NULL) {
542 if (PyErr_ExceptionMatches(PyExc_AttributeError))
543 PyErr_Clear();
544 else
545 return NULL;
546 }
547 if (dict) {
548 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
549 Py_DECREF(dict);
550 return res;
551 }
552 return Py_BuildValue("O()", Py_TYPE(self));
553}
554
555static PyMethodDef ast_type_methods[] = {
556 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
557 {NULL}
558};
559
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700560static PyGetSetDef ast_type_getsets[] = {
561 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
562 {NULL}
563};
564
Neal Norwitz207c9f32008-03-31 04:42:11 +0000565static PyTypeObject AST_type = {
566 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000567 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700568 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000569 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500570 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000571 0, /* tp_print */
572 0, /* tp_getattr */
573 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000574 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000575 0, /* tp_repr */
576 0, /* tp_as_number */
577 0, /* tp_as_sequence */
578 0, /* tp_as_mapping */
579 0, /* tp_hash */
580 0, /* tp_call */
581 0, /* tp_str */
582 PyObject_GenericGetAttr, /* tp_getattro */
583 PyObject_GenericSetAttr, /* tp_setattro */
584 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700585 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000586 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700587 (traverseproc)ast_traverse, /* tp_traverse */
588 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000589 0, /* tp_richcompare */
590 0, /* tp_weaklistoffset */
591 0, /* tp_iter */
592 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000593 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000594 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700595 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000596 0, /* tp_base */
597 0, /* tp_dict */
598 0, /* tp_descr_get */
599 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700600 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000601 (initproc)ast_type_init, /* tp_init */
602 PyType_GenericAlloc, /* tp_alloc */
603 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700604 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000605};
606
607
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000608static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
609{
610 PyObject *fnames, *result;
611 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000612 fnames = PyTuple_New(num_fields);
613 if (!fnames) return NULL;
614 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000615 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000616 if (!field) {
617 Py_DECREF(fnames);
618 return NULL;
619 }
620 PyTuple_SET_ITEM(fnames, i, field);
621 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000622 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000623 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000624 Py_DECREF(fnames);
625 return (PyTypeObject*)result;
626}
627
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
629{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200631 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000632 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000633 if (!l)
634 return 0;
635 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000636 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000637 if (!s) {
638 Py_DECREF(l);
639 return 0;
640 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000642 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200643 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 Py_DECREF(l);
645 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000646}
647
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000648/* Conversion AST -> Python */
649
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
651{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700652 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 PyObject *result = PyList_New(n);
654 PyObject *value;
655 if (!result)
656 return NULL;
657 for (i = 0; i < n; i++) {
658 value = func(asdl_seq_GET(seq, i));
659 if (!value) {
660 Py_DECREF(result);
661 return NULL;
662 }
663 PyList_SET_ITEM(result, i, value);
664 }
665 return result;
666}
667
668static PyObject* ast2obj_object(void *o)
669{
670 if (!o)
671 o = Py_None;
672 Py_INCREF((PyObject*)o);
673 return (PyObject*)o;
674}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500675#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676#define ast2obj_identifier ast2obj_object
677#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500678#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000680static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681{
Christian Heimes217cfd12007-12-02 14:31:20 +0000682 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683}
684
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000685/* Conversion Python -> AST */
686
Benjamin Peterson442f2092012-12-06 17:41:04 -0500687static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
688{
689 if (obj != Py_None && obj != Py_True && obj != Py_False) {
690 PyErr_SetString(PyExc_ValueError,
691 "AST singleton must be True, False, or None");
692 return 1;
693 }
694 *out = obj;
695 return 0;
696}
697
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000698static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
699{
700 if (obj == Py_None)
701 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200702 if (obj) {
703 if (PyArena_AddPyObject(arena, obj) < 0) {
704 *out = NULL;
705 return -1;
706 }
707 Py_INCREF(obj);
708 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000709 *out = obj;
710 return 0;
711}
712
Benjamin Peterson180e6352011-07-22 11:09:07 -0500713static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500714{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500715 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
716 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500717 return 1;
718 }
719 return obj2ast_object(obj, out, arena);
720}
721
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500722static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
723{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400724 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500725 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
726 return 1;
727 }
728 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500729}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000730
Benjamin Petersone2498412011-08-09 16:08:39 -0500731static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
732{
733 if (!PyBytes_CheckExact(obj)) {
734 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
735 return 1;
736 }
737 return obj2ast_object(obj, out, arena);
738}
739
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000740static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
741{
742 int i;
743 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100744 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000745 return 1;
746 }
747
748 i = (int)PyLong_AsLong(obj);
749 if (i == -1 && PyErr_Occurred())
750 return 1;
751 *out = i;
752 return 0;
753}
754
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000755static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000756{
757 PyObject *empty_tuple, *d;
758 if (PyType_Ready(&AST_type) < 0)
759 return -1;
760 d = AST_type.tp_dict;
761 empty_tuple = PyTuple_New(0);
762 if (!empty_tuple ||
763 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
764 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
765 Py_XDECREF(empty_tuple);
766 return -1;
767 }
768 Py_DECREF(empty_tuple);
769 return 0;
770}
771
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700772static int exists_not_none(PyObject *obj, _Py_Identifier *id)
773{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700774 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700775 PyObject *attr = _PyObject_GetAttrId(obj, id);
776 if (!attr) {
777 PyErr_Clear();
778 return 0;
779 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700780 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700781 Py_DECREF(attr);
782 return !isnone;
783}
784
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000785
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786static int init_types(void)
787{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200788 static int initialized;
789 if (initialized) return 1;
790 if (add_ast_fields() < 0) return 0;
791 mod_type = make_type("mod", &AST_type, NULL, 0);
792 if (!mod_type) return 0;
793 if (!add_attributes(mod_type, NULL, 0)) return 0;
794 Module_type = make_type("Module", mod_type, Module_fields, 1);
795 if (!Module_type) return 0;
796 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
797 1);
798 if (!Interactive_type) return 0;
799 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
800 if (!Expression_type) return 0;
801 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
802 if (!Suite_type) return 0;
803 stmt_type = make_type("stmt", &AST_type, NULL, 0);
804 if (!stmt_type) return 0;
805 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
806 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
807 5);
808 if (!FunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400809 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200810 if (!ClassDef_type) return 0;
811 Return_type = make_type("Return", stmt_type, Return_fields, 1);
812 if (!Return_type) return 0;
813 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
814 if (!Delete_type) return 0;
815 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
816 if (!Assign_type) return 0;
817 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
818 if (!AugAssign_type) return 0;
819 For_type = make_type("For", stmt_type, For_fields, 4);
820 if (!For_type) return 0;
821 While_type = make_type("While", stmt_type, While_fields, 3);
822 if (!While_type) return 0;
823 If_type = make_type("If", stmt_type, If_fields, 3);
824 if (!If_type) return 0;
825 With_type = make_type("With", stmt_type, With_fields, 2);
826 if (!With_type) return 0;
827 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
828 if (!Raise_type) return 0;
829 Try_type = make_type("Try", stmt_type, Try_fields, 4);
830 if (!Try_type) return 0;
831 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
832 if (!Assert_type) return 0;
833 Import_type = make_type("Import", stmt_type, Import_fields, 1);
834 if (!Import_type) return 0;
835 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
836 if (!ImportFrom_type) return 0;
837 Global_type = make_type("Global", stmt_type, Global_fields, 1);
838 if (!Global_type) return 0;
839 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
840 if (!Nonlocal_type) return 0;
841 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
842 if (!Expr_type) return 0;
843 Pass_type = make_type("Pass", stmt_type, NULL, 0);
844 if (!Pass_type) return 0;
845 Break_type = make_type("Break", stmt_type, NULL, 0);
846 if (!Break_type) return 0;
847 Continue_type = make_type("Continue", stmt_type, NULL, 0);
848 if (!Continue_type) return 0;
849 expr_type = make_type("expr", &AST_type, NULL, 0);
850 if (!expr_type) return 0;
851 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
852 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
853 if (!BoolOp_type) return 0;
854 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
855 if (!BinOp_type) return 0;
856 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
857 if (!UnaryOp_type) return 0;
858 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
859 if (!Lambda_type) return 0;
860 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
861 if (!IfExp_type) return 0;
862 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
863 if (!Dict_type) return 0;
864 Set_type = make_type("Set", expr_type, Set_fields, 1);
865 if (!Set_type) return 0;
866 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
867 if (!ListComp_type) return 0;
868 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
869 if (!SetComp_type) return 0;
870 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
871 if (!DictComp_type) return 0;
872 GeneratorExp_type = make_type("GeneratorExp", expr_type,
873 GeneratorExp_fields, 2);
874 if (!GeneratorExp_type) return 0;
875 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
876 if (!Yield_type) return 0;
877 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
878 if (!YieldFrom_type) return 0;
879 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
880 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400881 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200882 if (!Call_type) return 0;
883 Num_type = make_type("Num", expr_type, Num_fields, 1);
884 if (!Num_type) return 0;
885 Str_type = make_type("Str", expr_type, Str_fields, 1);
886 if (!Str_type) return 0;
887 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
888 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200889 NameConstant_type = make_type("NameConstant", expr_type,
890 NameConstant_fields, 1);
891 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200892 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
893 if (!Ellipsis_type) return 0;
894 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
895 if (!Attribute_type) return 0;
896 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
897 if (!Subscript_type) return 0;
898 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
899 if (!Starred_type) return 0;
900 Name_type = make_type("Name", expr_type, Name_fields, 2);
901 if (!Name_type) return 0;
902 List_type = make_type("List", expr_type, List_fields, 2);
903 if (!List_type) return 0;
904 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
905 if (!Tuple_type) return 0;
906 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
907 if (!expr_context_type) return 0;
908 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
909 Load_type = make_type("Load", expr_context_type, NULL, 0);
910 if (!Load_type) return 0;
911 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
912 if (!Load_singleton) return 0;
913 Store_type = make_type("Store", expr_context_type, NULL, 0);
914 if (!Store_type) return 0;
915 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
916 if (!Store_singleton) return 0;
917 Del_type = make_type("Del", expr_context_type, NULL, 0);
918 if (!Del_type) return 0;
919 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
920 if (!Del_singleton) return 0;
921 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
922 if (!AugLoad_type) return 0;
923 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
924 if (!AugLoad_singleton) return 0;
925 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
926 if (!AugStore_type) return 0;
927 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
928 if (!AugStore_singleton) return 0;
929 Param_type = make_type("Param", expr_context_type, NULL, 0);
930 if (!Param_type) return 0;
931 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
932 if (!Param_singleton) return 0;
933 slice_type = make_type("slice", &AST_type, NULL, 0);
934 if (!slice_type) return 0;
935 if (!add_attributes(slice_type, NULL, 0)) return 0;
936 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
937 if (!Slice_type) return 0;
938 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
939 if (!ExtSlice_type) return 0;
940 Index_type = make_type("Index", slice_type, Index_fields, 1);
941 if (!Index_type) return 0;
942 boolop_type = make_type("boolop", &AST_type, NULL, 0);
943 if (!boolop_type) return 0;
944 if (!add_attributes(boolop_type, NULL, 0)) return 0;
945 And_type = make_type("And", boolop_type, NULL, 0);
946 if (!And_type) return 0;
947 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
948 if (!And_singleton) return 0;
949 Or_type = make_type("Or", boolop_type, NULL, 0);
950 if (!Or_type) return 0;
951 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
952 if (!Or_singleton) return 0;
953 operator_type = make_type("operator", &AST_type, NULL, 0);
954 if (!operator_type) return 0;
955 if (!add_attributes(operator_type, NULL, 0)) return 0;
956 Add_type = make_type("Add", operator_type, NULL, 0);
957 if (!Add_type) return 0;
958 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
959 if (!Add_singleton) return 0;
960 Sub_type = make_type("Sub", operator_type, NULL, 0);
961 if (!Sub_type) return 0;
962 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
963 if (!Sub_singleton) return 0;
964 Mult_type = make_type("Mult", operator_type, NULL, 0);
965 if (!Mult_type) return 0;
966 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
967 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400968 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
969 if (!MatMult_type) return 0;
970 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
971 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200972 Div_type = make_type("Div", operator_type, NULL, 0);
973 if (!Div_type) return 0;
974 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
975 if (!Div_singleton) return 0;
976 Mod_type = make_type("Mod", operator_type, NULL, 0);
977 if (!Mod_type) return 0;
978 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
979 if (!Mod_singleton) return 0;
980 Pow_type = make_type("Pow", operator_type, NULL, 0);
981 if (!Pow_type) return 0;
982 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
983 if (!Pow_singleton) return 0;
984 LShift_type = make_type("LShift", operator_type, NULL, 0);
985 if (!LShift_type) return 0;
986 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
987 if (!LShift_singleton) return 0;
988 RShift_type = make_type("RShift", operator_type, NULL, 0);
989 if (!RShift_type) return 0;
990 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
991 if (!RShift_singleton) return 0;
992 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
993 if (!BitOr_type) return 0;
994 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
995 if (!BitOr_singleton) return 0;
996 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
997 if (!BitXor_type) return 0;
998 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
999 if (!BitXor_singleton) return 0;
1000 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1001 if (!BitAnd_type) return 0;
1002 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1003 if (!BitAnd_singleton) return 0;
1004 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1005 if (!FloorDiv_type) return 0;
1006 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1007 if (!FloorDiv_singleton) return 0;
1008 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1009 if (!unaryop_type) return 0;
1010 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1011 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1012 if (!Invert_type) return 0;
1013 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1014 if (!Invert_singleton) return 0;
1015 Not_type = make_type("Not", unaryop_type, NULL, 0);
1016 if (!Not_type) return 0;
1017 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1018 if (!Not_singleton) return 0;
1019 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1020 if (!UAdd_type) return 0;
1021 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1022 if (!UAdd_singleton) return 0;
1023 USub_type = make_type("USub", unaryop_type, NULL, 0);
1024 if (!USub_type) return 0;
1025 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1026 if (!USub_singleton) return 0;
1027 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1028 if (!cmpop_type) return 0;
1029 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1030 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1031 if (!Eq_type) return 0;
1032 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1033 if (!Eq_singleton) return 0;
1034 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1035 if (!NotEq_type) return 0;
1036 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1037 if (!NotEq_singleton) return 0;
1038 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1039 if (!Lt_type) return 0;
1040 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1041 if (!Lt_singleton) return 0;
1042 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1043 if (!LtE_type) return 0;
1044 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1045 if (!LtE_singleton) return 0;
1046 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1047 if (!Gt_type) return 0;
1048 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1049 if (!Gt_singleton) return 0;
1050 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1051 if (!GtE_type) return 0;
1052 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1053 if (!GtE_singleton) return 0;
1054 Is_type = make_type("Is", cmpop_type, NULL, 0);
1055 if (!Is_type) return 0;
1056 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1057 if (!Is_singleton) return 0;
1058 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1059 if (!IsNot_type) return 0;
1060 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1061 if (!IsNot_singleton) return 0;
1062 In_type = make_type("In", cmpop_type, NULL, 0);
1063 if (!In_type) return 0;
1064 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1065 if (!In_singleton) return 0;
1066 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1067 if (!NotIn_type) return 0;
1068 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1069 if (!NotIn_singleton) return 0;
1070 comprehension_type = make_type("comprehension", &AST_type,
1071 comprehension_fields, 3);
1072 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001073 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001074 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1075 if (!excepthandler_type) return 0;
1076 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1077 return 0;
1078 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1079 ExceptHandler_fields, 3);
1080 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001081 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001082 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001083 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001084 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1085 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001086 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001087 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1088 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001089 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001090 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1091 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001092 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001093 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1094 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001095 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001096 initialized = 1;
1097 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001098}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001099
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001100static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1101static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1102static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1103static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1104 arena);
1105static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1106static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1107static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1108static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1109static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1110static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1111 arena);
1112static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1113 arena);
1114static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1115static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1116static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1117static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001118static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001123 mod_ty p;
1124 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1125 if (!p)
1126 return NULL;
1127 p->kind = Module_kind;
1128 p->v.Module.body = body;
1129 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
1132mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001133Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001135 mod_ty p;
1136 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1137 if (!p)
1138 return NULL;
1139 p->kind = Interactive_kind;
1140 p->v.Interactive.body = body;
1141 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142}
1143
1144mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001147 mod_ty p;
1148 if (!body) {
1149 PyErr_SetString(PyExc_ValueError,
1150 "field body is required for Expression");
1151 return NULL;
1152 }
1153 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1154 if (!p)
1155 return NULL;
1156 p->kind = Expression_kind;
1157 p->v.Expression.body = body;
1158 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159}
1160
1161mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001162Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001164 mod_ty p;
1165 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1166 if (!p)
1167 return NULL;
1168 p->kind = Suite_kind;
1169 p->v.Suite.body = body;
1170 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
1173stmt_ty
1174FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001175 decorator_list, expr_ty returns, int lineno, int col_offset,
1176 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001178 stmt_ty p;
1179 if (!name) {
1180 PyErr_SetString(PyExc_ValueError,
1181 "field name is required for FunctionDef");
1182 return NULL;
1183 }
1184 if (!args) {
1185 PyErr_SetString(PyExc_ValueError,
1186 "field args is required for FunctionDef");
1187 return NULL;
1188 }
1189 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1190 if (!p)
1191 return NULL;
1192 p->kind = FunctionDef_kind;
1193 p->v.FunctionDef.name = name;
1194 p->v.FunctionDef.args = args;
1195 p->v.FunctionDef.body = body;
1196 p->v.FunctionDef.decorator_list = decorator_list;
1197 p->v.FunctionDef.returns = returns;
1198 p->lineno = lineno;
1199 p->col_offset = col_offset;
1200 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201}
1202
1203stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001204ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1205 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1206 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001208 stmt_ty p;
1209 if (!name) {
1210 PyErr_SetString(PyExc_ValueError,
1211 "field name is required for ClassDef");
1212 return NULL;
1213 }
1214 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1215 if (!p)
1216 return NULL;
1217 p->kind = ClassDef_kind;
1218 p->v.ClassDef.name = name;
1219 p->v.ClassDef.bases = bases;
1220 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001221 p->v.ClassDef.body = body;
1222 p->v.ClassDef.decorator_list = decorator_list;
1223 p->lineno = lineno;
1224 p->col_offset = col_offset;
1225 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226}
1227
1228stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001229Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001231 stmt_ty p;
1232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1233 if (!p)
1234 return NULL;
1235 p->kind = Return_kind;
1236 p->v.Return.value = value;
1237 p->lineno = lineno;
1238 p->col_offset = col_offset;
1239 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240}
1241
1242stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001243Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001245 stmt_ty p;
1246 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1247 if (!p)
1248 return NULL;
1249 p->kind = Delete_kind;
1250 p->v.Delete.targets = targets;
1251 p->lineno = lineno;
1252 p->col_offset = col_offset;
1253 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254}
1255
1256stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1258 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001260 stmt_ty p;
1261 if (!value) {
1262 PyErr_SetString(PyExc_ValueError,
1263 "field value is required for Assign");
1264 return NULL;
1265 }
1266 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1267 if (!p)
1268 return NULL;
1269 p->kind = Assign_kind;
1270 p->v.Assign.targets = targets;
1271 p->v.Assign.value = value;
1272 p->lineno = lineno;
1273 p->col_offset = col_offset;
1274 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275}
1276
1277stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1279 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001281 stmt_ty p;
1282 if (!target) {
1283 PyErr_SetString(PyExc_ValueError,
1284 "field target is required for AugAssign");
1285 return NULL;
1286 }
1287 if (!op) {
1288 PyErr_SetString(PyExc_ValueError,
1289 "field op is required for AugAssign");
1290 return NULL;
1291 }
1292 if (!value) {
1293 PyErr_SetString(PyExc_ValueError,
1294 "field value is required for AugAssign");
1295 return NULL;
1296 }
1297 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1298 if (!p)
1299 return NULL;
1300 p->kind = AugAssign_kind;
1301 p->v.AugAssign.target = target;
1302 p->v.AugAssign.op = op;
1303 p->v.AugAssign.value = value;
1304 p->lineno = lineno;
1305 p->col_offset = col_offset;
1306 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307}
1308
1309stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001311 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001313 stmt_ty p;
1314 if (!target) {
1315 PyErr_SetString(PyExc_ValueError,
1316 "field target is required for For");
1317 return NULL;
1318 }
1319 if (!iter) {
1320 PyErr_SetString(PyExc_ValueError,
1321 "field iter is required for For");
1322 return NULL;
1323 }
1324 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1325 if (!p)
1326 return NULL;
1327 p->kind = For_kind;
1328 p->v.For.target = target;
1329 p->v.For.iter = iter;
1330 p->v.For.body = body;
1331 p->v.For.orelse = orelse;
1332 p->lineno = lineno;
1333 p->col_offset = col_offset;
1334 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335}
1336
1337stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001338While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1339 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001341 stmt_ty p;
1342 if (!test) {
1343 PyErr_SetString(PyExc_ValueError,
1344 "field test is required for While");
1345 return NULL;
1346 }
1347 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1348 if (!p)
1349 return NULL;
1350 p->kind = While_kind;
1351 p->v.While.test = test;
1352 p->v.While.body = body;
1353 p->v.While.orelse = orelse;
1354 p->lineno = lineno;
1355 p->col_offset = col_offset;
1356 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357}
1358
1359stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001360If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1361 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001363 stmt_ty p;
1364 if (!test) {
1365 PyErr_SetString(PyExc_ValueError,
1366 "field test is required for If");
1367 return NULL;
1368 }
1369 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1370 if (!p)
1371 return NULL;
1372 p->kind = If_kind;
1373 p->v.If.test = test;
1374 p->v.If.body = body;
1375 p->v.If.orelse = orelse;
1376 p->lineno = lineno;
1377 p->col_offset = col_offset;
1378 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379}
1380
1381stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001382With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1383 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001384{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001385 stmt_ty p;
1386 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1387 if (!p)
1388 return NULL;
1389 p->kind = With_kind;
1390 p->v.With.items = items;
1391 p->v.With.body = body;
1392 p->lineno = lineno;
1393 p->col_offset = col_offset;
1394 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001395}
1396
1397stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001398Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001400 stmt_ty p;
1401 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1402 if (!p)
1403 return NULL;
1404 p->kind = Raise_kind;
1405 p->v.Raise.exc = exc;
1406 p->v.Raise.cause = cause;
1407 p->lineno = lineno;
1408 p->col_offset = col_offset;
1409 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410}
1411
1412stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001413Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1414 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001416 stmt_ty p;
1417 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1418 if (!p)
1419 return NULL;
1420 p->kind = Try_kind;
1421 p->v.Try.body = body;
1422 p->v.Try.handlers = handlers;
1423 p->v.Try.orelse = orelse;
1424 p->v.Try.finalbody = finalbody;
1425 p->lineno = lineno;
1426 p->col_offset = col_offset;
1427 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428}
1429
1430stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001433 stmt_ty p;
1434 if (!test) {
1435 PyErr_SetString(PyExc_ValueError,
1436 "field test is required for Assert");
1437 return NULL;
1438 }
1439 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1440 if (!p)
1441 return NULL;
1442 p->kind = Assert_kind;
1443 p->v.Assert.test = test;
1444 p->v.Assert.msg = msg;
1445 p->lineno = lineno;
1446 p->col_offset = col_offset;
1447 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448}
1449
1450stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001451Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001453 stmt_ty p;
1454 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1455 if (!p)
1456 return NULL;
1457 p->kind = Import_kind;
1458 p->v.Import.names = names;
1459 p->lineno = lineno;
1460 p->col_offset = col_offset;
1461 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462}
1463
1464stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001465ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1466 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001468 stmt_ty p;
1469 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1470 if (!p)
1471 return NULL;
1472 p->kind = ImportFrom_kind;
1473 p->v.ImportFrom.module = module;
1474 p->v.ImportFrom.names = names;
1475 p->v.ImportFrom.level = level;
1476 p->lineno = lineno;
1477 p->col_offset = col_offset;
1478 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479}
1480
1481stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001482Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001484 stmt_ty p;
1485 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1486 if (!p)
1487 return NULL;
1488 p->kind = Global_kind;
1489 p->v.Global.names = names;
1490 p->lineno = lineno;
1491 p->col_offset = col_offset;
1492 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001496Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1497{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001498 stmt_ty p;
1499 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1500 if (!p)
1501 return NULL;
1502 p->kind = Nonlocal_kind;
1503 p->v.Nonlocal.names = names;
1504 p->lineno = lineno;
1505 p->col_offset = col_offset;
1506 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001507}
1508
1509stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001512 stmt_ty p;
1513 if (!value) {
1514 PyErr_SetString(PyExc_ValueError,
1515 "field value is required for Expr");
1516 return NULL;
1517 }
1518 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1519 if (!p)
1520 return NULL;
1521 p->kind = Expr_kind;
1522 p->v.Expr.value = value;
1523 p->lineno = lineno;
1524 p->col_offset = col_offset;
1525 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526}
1527
1528stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001529Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001531 stmt_ty p;
1532 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1533 if (!p)
1534 return NULL;
1535 p->kind = Pass_kind;
1536 p->lineno = lineno;
1537 p->col_offset = col_offset;
1538 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539}
1540
1541stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001542Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001544 stmt_ty p;
1545 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1546 if (!p)
1547 return NULL;
1548 p->kind = Break_kind;
1549 p->lineno = lineno;
1550 p->col_offset = col_offset;
1551 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
1554stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001555Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001557 stmt_ty p;
1558 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1559 if (!p)
1560 return NULL;
1561 p->kind = Continue_kind;
1562 p->lineno = lineno;
1563 p->col_offset = col_offset;
1564 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565}
1566
1567expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001568BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1569 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001571 expr_ty p;
1572 if (!op) {
1573 PyErr_SetString(PyExc_ValueError,
1574 "field op is required for BoolOp");
1575 return NULL;
1576 }
1577 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1578 if (!p)
1579 return NULL;
1580 p->kind = BoolOp_kind;
1581 p->v.BoolOp.op = op;
1582 p->v.BoolOp.values = values;
1583 p->lineno = lineno;
1584 p->col_offset = col_offset;
1585 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
1588expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1590 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001592 expr_ty p;
1593 if (!left) {
1594 PyErr_SetString(PyExc_ValueError,
1595 "field left is required for BinOp");
1596 return NULL;
1597 }
1598 if (!op) {
1599 PyErr_SetString(PyExc_ValueError,
1600 "field op is required for BinOp");
1601 return NULL;
1602 }
1603 if (!right) {
1604 PyErr_SetString(PyExc_ValueError,
1605 "field right is required for BinOp");
1606 return NULL;
1607 }
1608 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1609 if (!p)
1610 return NULL;
1611 p->kind = BinOp_kind;
1612 p->v.BinOp.left = left;
1613 p->v.BinOp.op = op;
1614 p->v.BinOp.right = right;
1615 p->lineno = lineno;
1616 p->col_offset = col_offset;
1617 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618}
1619
1620expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001621UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1622 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001624 expr_ty p;
1625 if (!op) {
1626 PyErr_SetString(PyExc_ValueError,
1627 "field op is required for UnaryOp");
1628 return NULL;
1629 }
1630 if (!operand) {
1631 PyErr_SetString(PyExc_ValueError,
1632 "field operand is required for UnaryOp");
1633 return NULL;
1634 }
1635 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1636 if (!p)
1637 return NULL;
1638 p->kind = UnaryOp_kind;
1639 p->v.UnaryOp.op = op;
1640 p->v.UnaryOp.operand = operand;
1641 p->lineno = lineno;
1642 p->col_offset = col_offset;
1643 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644}
1645
1646expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001647Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1648 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001650 expr_ty p;
1651 if (!args) {
1652 PyErr_SetString(PyExc_ValueError,
1653 "field args is required for Lambda");
1654 return NULL;
1655 }
1656 if (!body) {
1657 PyErr_SetString(PyExc_ValueError,
1658 "field body is required for Lambda");
1659 return NULL;
1660 }
1661 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1662 if (!p)
1663 return NULL;
1664 p->kind = Lambda_kind;
1665 p->v.Lambda.args = args;
1666 p->v.Lambda.body = body;
1667 p->lineno = lineno;
1668 p->col_offset = col_offset;
1669 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670}
1671
1672expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1674 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001675{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001676 expr_ty p;
1677 if (!test) {
1678 PyErr_SetString(PyExc_ValueError,
1679 "field test is required for IfExp");
1680 return NULL;
1681 }
1682 if (!body) {
1683 PyErr_SetString(PyExc_ValueError,
1684 "field body is required for IfExp");
1685 return NULL;
1686 }
1687 if (!orelse) {
1688 PyErr_SetString(PyExc_ValueError,
1689 "field orelse is required for IfExp");
1690 return NULL;
1691 }
1692 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1693 if (!p)
1694 return NULL;
1695 p->kind = IfExp_kind;
1696 p->v.IfExp.test = test;
1697 p->v.IfExp.body = body;
1698 p->v.IfExp.orelse = orelse;
1699 p->lineno = lineno;
1700 p->col_offset = col_offset;
1701 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001702}
1703
1704expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001705Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1706 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001708 expr_ty p;
1709 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1710 if (!p)
1711 return NULL;
1712 p->kind = Dict_kind;
1713 p->v.Dict.keys = keys;
1714 p->v.Dict.values = values;
1715 p->lineno = lineno;
1716 p->col_offset = col_offset;
1717 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
1720expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001721Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1722{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001723 expr_ty p;
1724 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1725 if (!p)
1726 return NULL;
1727 p->kind = Set_kind;
1728 p->v.Set.elts = elts;
1729 p->lineno = lineno;
1730 p->col_offset = col_offset;
1731 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001732}
1733
1734expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001735ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1736 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001738 expr_ty p;
1739 if (!elt) {
1740 PyErr_SetString(PyExc_ValueError,
1741 "field elt is required for ListComp");
1742 return NULL;
1743 }
1744 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1745 if (!p)
1746 return NULL;
1747 p->kind = ListComp_kind;
1748 p->v.ListComp.elt = elt;
1749 p->v.ListComp.generators = generators;
1750 p->lineno = lineno;
1751 p->col_offset = col_offset;
1752 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753}
1754
1755expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001756SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1757 *arena)
1758{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001759 expr_ty p;
1760 if (!elt) {
1761 PyErr_SetString(PyExc_ValueError,
1762 "field elt is required for SetComp");
1763 return NULL;
1764 }
1765 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1766 if (!p)
1767 return NULL;
1768 p->kind = SetComp_kind;
1769 p->v.SetComp.elt = elt;
1770 p->v.SetComp.generators = generators;
1771 p->lineno = lineno;
1772 p->col_offset = col_offset;
1773 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001774}
1775
1776expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001777DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1778 col_offset, PyArena *arena)
1779{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001780 expr_ty p;
1781 if (!key) {
1782 PyErr_SetString(PyExc_ValueError,
1783 "field key is required for DictComp");
1784 return NULL;
1785 }
1786 if (!value) {
1787 PyErr_SetString(PyExc_ValueError,
1788 "field value is required for DictComp");
1789 return NULL;
1790 }
1791 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1792 if (!p)
1793 return NULL;
1794 p->kind = DictComp_kind;
1795 p->v.DictComp.key = key;
1796 p->v.DictComp.value = value;
1797 p->v.DictComp.generators = generators;
1798 p->lineno = lineno;
1799 p->col_offset = col_offset;
1800 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001801}
1802
1803expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001804GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1805 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001807 expr_ty p;
1808 if (!elt) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field elt is required for GeneratorExp");
1811 return NULL;
1812 }
1813 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1814 if (!p)
1815 return NULL;
1816 p->kind = GeneratorExp_kind;
1817 p->v.GeneratorExp.elt = elt;
1818 p->v.GeneratorExp.generators = generators;
1819 p->lineno = lineno;
1820 p->col_offset = col_offset;
1821 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822}
1823
1824expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001825Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001827 expr_ty p;
1828 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1829 if (!p)
1830 return NULL;
1831 p->kind = Yield_kind;
1832 p->v.Yield.value = value;
1833 p->lineno = lineno;
1834 p->col_offset = col_offset;
1835 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836}
1837
1838expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001839YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1840{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001841 expr_ty p;
1842 if (!value) {
1843 PyErr_SetString(PyExc_ValueError,
1844 "field value is required for YieldFrom");
1845 return NULL;
1846 }
1847 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1848 if (!p)
1849 return NULL;
1850 p->kind = YieldFrom_kind;
1851 p->v.YieldFrom.value = value;
1852 p->lineno = lineno;
1853 p->col_offset = col_offset;
1854 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001855}
1856
1857expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001858Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1859 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001861 expr_ty p;
1862 if (!left) {
1863 PyErr_SetString(PyExc_ValueError,
1864 "field left is required for Compare");
1865 return NULL;
1866 }
1867 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1868 if (!p)
1869 return NULL;
1870 p->kind = Compare_kind;
1871 p->v.Compare.left = left;
1872 p->v.Compare.ops = ops;
1873 p->v.Compare.comparators = comparators;
1874 p->lineno = lineno;
1875 p->col_offset = col_offset;
1876 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877}
1878
1879expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001880Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
1881 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001883 expr_ty p;
1884 if (!func) {
1885 PyErr_SetString(PyExc_ValueError,
1886 "field func is required for Call");
1887 return NULL;
1888 }
1889 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1890 if (!p)
1891 return NULL;
1892 p->kind = Call_kind;
1893 p->v.Call.func = func;
1894 p->v.Call.args = args;
1895 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001896 p->lineno = lineno;
1897 p->col_offset = col_offset;
1898 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899}
1900
1901expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001902Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001904 expr_ty p;
1905 if (!n) {
1906 PyErr_SetString(PyExc_ValueError,
1907 "field n is required for Num");
1908 return NULL;
1909 }
1910 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1911 if (!p)
1912 return NULL;
1913 p->kind = Num_kind;
1914 p->v.Num.n = n;
1915 p->lineno = lineno;
1916 p->col_offset = col_offset;
1917 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
1920expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001921Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001923 expr_ty p;
1924 if (!s) {
1925 PyErr_SetString(PyExc_ValueError,
1926 "field s is required for Str");
1927 return NULL;
1928 }
1929 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1930 if (!p)
1931 return NULL;
1932 p->kind = Str_kind;
1933 p->v.Str.s = s;
1934 p->lineno = lineno;
1935 p->col_offset = col_offset;
1936 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937}
1938
1939expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05001940Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001941{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001942 expr_ty p;
1943 if (!s) {
1944 PyErr_SetString(PyExc_ValueError,
1945 "field s is required for Bytes");
1946 return NULL;
1947 }
1948 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1949 if (!p)
1950 return NULL;
1951 p->kind = Bytes_kind;
1952 p->v.Bytes.s = s;
1953 p->lineno = lineno;
1954 p->col_offset = col_offset;
1955 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001956}
1957
1958expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05001959NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
1960{
Victor Stinneree4b59c2013-07-27 00:01:35 +02001961 expr_ty p;
1962 if (!value) {
1963 PyErr_SetString(PyExc_ValueError,
1964 "field value is required for NameConstant");
1965 return NULL;
1966 }
1967 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1968 if (!p)
1969 return NULL;
1970 p->kind = NameConstant_kind;
1971 p->v.NameConstant.value = value;
1972 p->lineno = lineno;
1973 p->col_offset = col_offset;
1974 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001975}
1976
1977expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001978Ellipsis(int lineno, int col_offset, PyArena *arena)
1979{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001980 expr_ty p;
1981 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1982 if (!p)
1983 return NULL;
1984 p->kind = Ellipsis_kind;
1985 p->lineno = lineno;
1986 p->col_offset = col_offset;
1987 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00001988}
1989
1990expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001991Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1992 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001994 expr_ty p;
1995 if (!value) {
1996 PyErr_SetString(PyExc_ValueError,
1997 "field value is required for Attribute");
1998 return NULL;
1999 }
2000 if (!attr) {
2001 PyErr_SetString(PyExc_ValueError,
2002 "field attr is required for Attribute");
2003 return NULL;
2004 }
2005 if (!ctx) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field ctx is required for Attribute");
2008 return NULL;
2009 }
2010 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2011 if (!p)
2012 return NULL;
2013 p->kind = Attribute_kind;
2014 p->v.Attribute.value = value;
2015 p->v.Attribute.attr = attr;
2016 p->v.Attribute.ctx = ctx;
2017 p->lineno = lineno;
2018 p->col_offset = col_offset;
2019 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020}
2021
2022expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002023Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2024 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002026 expr_ty p;
2027 if (!value) {
2028 PyErr_SetString(PyExc_ValueError,
2029 "field value is required for Subscript");
2030 return NULL;
2031 }
2032 if (!slice) {
2033 PyErr_SetString(PyExc_ValueError,
2034 "field slice is required for Subscript");
2035 return NULL;
2036 }
2037 if (!ctx) {
2038 PyErr_SetString(PyExc_ValueError,
2039 "field ctx is required for Subscript");
2040 return NULL;
2041 }
2042 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2043 if (!p)
2044 return NULL;
2045 p->kind = Subscript_kind;
2046 p->v.Subscript.value = value;
2047 p->v.Subscript.slice = slice;
2048 p->v.Subscript.ctx = ctx;
2049 p->lineno = lineno;
2050 p->col_offset = col_offset;
2051 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052}
2053
2054expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002055Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2056 *arena)
2057{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002058 expr_ty p;
2059 if (!value) {
2060 PyErr_SetString(PyExc_ValueError,
2061 "field value is required for Starred");
2062 return NULL;
2063 }
2064 if (!ctx) {
2065 PyErr_SetString(PyExc_ValueError,
2066 "field ctx is required for Starred");
2067 return NULL;
2068 }
2069 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2070 if (!p)
2071 return NULL;
2072 p->kind = Starred_kind;
2073 p->v.Starred.value = value;
2074 p->v.Starred.ctx = ctx;
2075 p->lineno = lineno;
2076 p->col_offset = col_offset;
2077 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002078}
2079
2080expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002081Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2082 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002084 expr_ty p;
2085 if (!id) {
2086 PyErr_SetString(PyExc_ValueError,
2087 "field id is required for Name");
2088 return NULL;
2089 }
2090 if (!ctx) {
2091 PyErr_SetString(PyExc_ValueError,
2092 "field ctx is required for Name");
2093 return NULL;
2094 }
2095 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2096 if (!p)
2097 return NULL;
2098 p->kind = Name_kind;
2099 p->v.Name.id = id;
2100 p->v.Name.ctx = ctx;
2101 p->lineno = lineno;
2102 p->col_offset = col_offset;
2103 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104}
2105
2106expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002107List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2108 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002110 expr_ty p;
2111 if (!ctx) {
2112 PyErr_SetString(PyExc_ValueError,
2113 "field ctx is required for List");
2114 return NULL;
2115 }
2116 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2117 if (!p)
2118 return NULL;
2119 p->kind = List_kind;
2120 p->v.List.elts = elts;
2121 p->v.List.ctx = ctx;
2122 p->lineno = lineno;
2123 p->col_offset = col_offset;
2124 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125}
2126
2127expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002128Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2129 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002131 expr_ty p;
2132 if (!ctx) {
2133 PyErr_SetString(PyExc_ValueError,
2134 "field ctx is required for Tuple");
2135 return NULL;
2136 }
2137 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2138 if (!p)
2139 return NULL;
2140 p->kind = Tuple_kind;
2141 p->v.Tuple.elts = elts;
2142 p->v.Tuple.ctx = ctx;
2143 p->lineno = lineno;
2144 p->col_offset = col_offset;
2145 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146}
2147
2148slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002149Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002151 slice_ty p;
2152 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2153 if (!p)
2154 return NULL;
2155 p->kind = Slice_kind;
2156 p->v.Slice.lower = lower;
2157 p->v.Slice.upper = upper;
2158 p->v.Slice.step = step;
2159 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160}
2161
2162slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002163ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002165 slice_ty p;
2166 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2167 if (!p)
2168 return NULL;
2169 p->kind = ExtSlice_kind;
2170 p->v.ExtSlice.dims = dims;
2171 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172}
2173
2174slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002175Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002177 slice_ty p;
2178 if (!value) {
2179 PyErr_SetString(PyExc_ValueError,
2180 "field value is required for Index");
2181 return NULL;
2182 }
2183 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2184 if (!p)
2185 return NULL;
2186 p->kind = Index_kind;
2187 p->v.Index.value = value;
2188 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189}
2190
2191comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002192comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002194 comprehension_ty p;
2195 if (!target) {
2196 PyErr_SetString(PyExc_ValueError,
2197 "field target is required for comprehension");
2198 return NULL;
2199 }
2200 if (!iter) {
2201 PyErr_SetString(PyExc_ValueError,
2202 "field iter is required for comprehension");
2203 return NULL;
2204 }
2205 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2206 if (!p)
2207 return NULL;
2208 p->target = target;
2209 p->iter = iter;
2210 p->ifs = ifs;
2211 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212}
2213
2214excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002215ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002216 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002218 excepthandler_ty p;
2219 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2220 if (!p)
2221 return NULL;
2222 p->kind = ExceptHandler_kind;
2223 p->v.ExceptHandler.type = type;
2224 p->v.ExceptHandler.name = name;
2225 p->v.ExceptHandler.body = body;
2226 p->lineno = lineno;
2227 p->col_offset = col_offset;
2228 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229}
2230
2231arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002232arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2233 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002235 arguments_ty p;
2236 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2237 if (!p)
2238 return NULL;
2239 p->args = args;
2240 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002241 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002242 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002243 p->kwarg = kwarg;
2244 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002245 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246}
2247
Neal Norwitzc1505362006-12-28 06:47:50 +00002248arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002249arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002250{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002251 arg_ty p;
2252 if (!arg) {
2253 PyErr_SetString(PyExc_ValueError,
2254 "field arg is required for arg");
2255 return NULL;
2256 }
2257 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2258 if (!p)
2259 return NULL;
2260 p->arg = arg;
2261 p->annotation = annotation;
2262 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002263}
2264
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002266keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002268 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002269 if (!value) {
2270 PyErr_SetString(PyExc_ValueError,
2271 "field value is required for keyword");
2272 return NULL;
2273 }
2274 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2275 if (!p)
2276 return NULL;
2277 p->arg = arg;
2278 p->value = value;
2279 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280}
2281
2282alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002283alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002285 alias_ty p;
2286 if (!name) {
2287 PyErr_SetString(PyExc_ValueError,
2288 "field name is required for alias");
2289 return NULL;
2290 }
2291 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2292 if (!p)
2293 return NULL;
2294 p->name = name;
2295 p->asname = asname;
2296 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297}
2298
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002299withitem_ty
2300withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2301{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002302 withitem_ty p;
2303 if (!context_expr) {
2304 PyErr_SetString(PyExc_ValueError,
2305 "field context_expr is required for withitem");
2306 return NULL;
2307 }
2308 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2309 if (!p)
2310 return NULL;
2311 p->context_expr = context_expr;
2312 p->optional_vars = optional_vars;
2313 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002314}
2315
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002316
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002317PyObject*
2318ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002320 mod_ty o = (mod_ty)_o;
2321 PyObject *result = NULL, *value = NULL;
2322 if (!o) {
2323 Py_INCREF(Py_None);
2324 return Py_None;
2325 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002326
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002327 switch (o->kind) {
2328 case Module_kind:
2329 result = PyType_GenericNew(Module_type, NULL, NULL);
2330 if (!result) goto failed;
2331 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2332 if (!value) goto failed;
2333 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2334 goto failed;
2335 Py_DECREF(value);
2336 break;
2337 case Interactive_kind:
2338 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2339 if (!result) goto failed;
2340 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2341 if (!value) goto failed;
2342 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2343 goto failed;
2344 Py_DECREF(value);
2345 break;
2346 case Expression_kind:
2347 result = PyType_GenericNew(Expression_type, NULL, NULL);
2348 if (!result) goto failed;
2349 value = ast2obj_expr(o->v.Expression.body);
2350 if (!value) goto failed;
2351 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
2354 break;
2355 case Suite_kind:
2356 result = PyType_GenericNew(Suite_type, NULL, NULL);
2357 if (!result) goto failed;
2358 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2359 if (!value) goto failed;
2360 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 break;
2364 }
2365 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002366failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002367 Py_XDECREF(value);
2368 Py_XDECREF(result);
2369 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370}
2371
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002372PyObject*
2373ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002375 stmt_ty o = (stmt_ty)_o;
2376 PyObject *result = NULL, *value = NULL;
2377 if (!o) {
2378 Py_INCREF(Py_None);
2379 return Py_None;
2380 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002381
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002382 switch (o->kind) {
2383 case FunctionDef_kind:
2384 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2385 if (!result) goto failed;
2386 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002387 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002388 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2389 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002390 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002391 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002392 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002393 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2394 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002395 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002396 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2397 if (!value) goto failed;
2398 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
2401 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2402 if (!value) goto failed;
2403 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2404 goto failed;
2405 Py_DECREF(value);
2406 value = ast2obj_expr(o->v.FunctionDef.returns);
2407 if (!value) goto failed;
2408 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
2411 break;
2412 case ClassDef_kind:
2413 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2414 if (!result) goto failed;
2415 value = ast2obj_identifier(o->v.ClassDef.name);
2416 if (!value) goto failed;
2417 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2418 goto failed;
2419 Py_DECREF(value);
2420 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2421 if (!value) goto failed;
2422 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2423 goto failed;
2424 Py_DECREF(value);
2425 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2426 if (!value) goto failed;
2427 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2428 goto failed;
2429 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002430 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2431 if (!value) goto failed;
2432 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2433 goto failed;
2434 Py_DECREF(value);
2435 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2436 if (!value) goto failed;
2437 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2438 goto failed;
2439 Py_DECREF(value);
2440 break;
2441 case Return_kind:
2442 result = PyType_GenericNew(Return_type, NULL, NULL);
2443 if (!result) goto failed;
2444 value = ast2obj_expr(o->v.Return.value);
2445 if (!value) goto failed;
2446 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2447 goto failed;
2448 Py_DECREF(value);
2449 break;
2450 case Delete_kind:
2451 result = PyType_GenericNew(Delete_type, NULL, NULL);
2452 if (!result) goto failed;
2453 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2454 if (!value) goto failed;
2455 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2456 goto failed;
2457 Py_DECREF(value);
2458 break;
2459 case Assign_kind:
2460 result = PyType_GenericNew(Assign_type, NULL, NULL);
2461 if (!result) goto failed;
2462 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2463 if (!value) goto failed;
2464 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
2467 value = ast2obj_expr(o->v.Assign.value);
2468 if (!value) goto failed;
2469 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2470 goto failed;
2471 Py_DECREF(value);
2472 break;
2473 case AugAssign_kind:
2474 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2475 if (!result) goto failed;
2476 value = ast2obj_expr(o->v.AugAssign.target);
2477 if (!value) goto failed;
2478 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
2481 value = ast2obj_operator(o->v.AugAssign.op);
2482 if (!value) goto failed;
2483 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
2486 value = ast2obj_expr(o->v.AugAssign.value);
2487 if (!value) goto failed;
2488 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2489 goto failed;
2490 Py_DECREF(value);
2491 break;
2492 case For_kind:
2493 result = PyType_GenericNew(For_type, NULL, NULL);
2494 if (!result) goto failed;
2495 value = ast2obj_expr(o->v.For.target);
2496 if (!value) goto failed;
2497 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2498 goto failed;
2499 Py_DECREF(value);
2500 value = ast2obj_expr(o->v.For.iter);
2501 if (!value) goto failed;
2502 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2503 goto failed;
2504 Py_DECREF(value);
2505 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2506 if (!value) goto failed;
2507 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2508 goto failed;
2509 Py_DECREF(value);
2510 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2511 if (!value) goto failed;
2512 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2513 goto failed;
2514 Py_DECREF(value);
2515 break;
2516 case While_kind:
2517 result = PyType_GenericNew(While_type, NULL, NULL);
2518 if (!result) goto failed;
2519 value = ast2obj_expr(o->v.While.test);
2520 if (!value) goto failed;
2521 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2522 goto failed;
2523 Py_DECREF(value);
2524 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2525 if (!value) goto failed;
2526 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2527 goto failed;
2528 Py_DECREF(value);
2529 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2530 if (!value) goto failed;
2531 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2532 goto failed;
2533 Py_DECREF(value);
2534 break;
2535 case If_kind:
2536 result = PyType_GenericNew(If_type, NULL, NULL);
2537 if (!result) goto failed;
2538 value = ast2obj_expr(o->v.If.test);
2539 if (!value) goto failed;
2540 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2541 goto failed;
2542 Py_DECREF(value);
2543 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2544 if (!value) goto failed;
2545 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2546 goto failed;
2547 Py_DECREF(value);
2548 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2549 if (!value) goto failed;
2550 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2551 goto failed;
2552 Py_DECREF(value);
2553 break;
2554 case With_kind:
2555 result = PyType_GenericNew(With_type, NULL, NULL);
2556 if (!result) goto failed;
2557 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2558 if (!value) goto failed;
2559 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2560 goto failed;
2561 Py_DECREF(value);
2562 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2563 if (!value) goto failed;
2564 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 break;
2568 case Raise_kind:
2569 result = PyType_GenericNew(Raise_type, NULL, NULL);
2570 if (!result) goto failed;
2571 value = ast2obj_expr(o->v.Raise.exc);
2572 if (!value) goto failed;
2573 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2574 goto failed;
2575 Py_DECREF(value);
2576 value = ast2obj_expr(o->v.Raise.cause);
2577 if (!value) goto failed;
2578 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 break;
2582 case Try_kind:
2583 result = PyType_GenericNew(Try_type, NULL, NULL);
2584 if (!result) goto failed;
2585 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2586 if (!value) goto failed;
2587 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2588 goto failed;
2589 Py_DECREF(value);
2590 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2591 if (!value) goto failed;
2592 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2593 goto failed;
2594 Py_DECREF(value);
2595 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2596 if (!value) goto failed;
2597 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2598 goto failed;
2599 Py_DECREF(value);
2600 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2601 if (!value) goto failed;
2602 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2603 goto failed;
2604 Py_DECREF(value);
2605 break;
2606 case Assert_kind:
2607 result = PyType_GenericNew(Assert_type, NULL, NULL);
2608 if (!result) goto failed;
2609 value = ast2obj_expr(o->v.Assert.test);
2610 if (!value) goto failed;
2611 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 value = ast2obj_expr(o->v.Assert.msg);
2615 if (!value) goto failed;
2616 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2617 goto failed;
2618 Py_DECREF(value);
2619 break;
2620 case Import_kind:
2621 result = PyType_GenericNew(Import_type, NULL, NULL);
2622 if (!result) goto failed;
2623 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2624 if (!value) goto failed;
2625 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 break;
2629 case ImportFrom_kind:
2630 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2631 if (!result) goto failed;
2632 value = ast2obj_identifier(o->v.ImportFrom.module);
2633 if (!value) goto failed;
2634 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2638 if (!value) goto failed;
2639 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2640 goto failed;
2641 Py_DECREF(value);
2642 value = ast2obj_int(o->v.ImportFrom.level);
2643 if (!value) goto failed;
2644 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2645 goto failed;
2646 Py_DECREF(value);
2647 break;
2648 case Global_kind:
2649 result = PyType_GenericNew(Global_type, NULL, NULL);
2650 if (!result) goto failed;
2651 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2652 if (!value) goto failed;
2653 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2654 goto failed;
2655 Py_DECREF(value);
2656 break;
2657 case Nonlocal_kind:
2658 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2659 if (!result) goto failed;
2660 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2661 if (!value) goto failed;
2662 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2663 goto failed;
2664 Py_DECREF(value);
2665 break;
2666 case Expr_kind:
2667 result = PyType_GenericNew(Expr_type, NULL, NULL);
2668 if (!result) goto failed;
2669 value = ast2obj_expr(o->v.Expr.value);
2670 if (!value) goto failed;
2671 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
2675 case Pass_kind:
2676 result = PyType_GenericNew(Pass_type, NULL, NULL);
2677 if (!result) goto failed;
2678 break;
2679 case Break_kind:
2680 result = PyType_GenericNew(Break_type, NULL, NULL);
2681 if (!result) goto failed;
2682 break;
2683 case Continue_kind:
2684 result = PyType_GenericNew(Continue_type, NULL, NULL);
2685 if (!result) goto failed;
2686 break;
2687 }
2688 value = ast2obj_int(o->lineno);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_int(o->col_offset);
2694 if (!value) goto failed;
2695 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2696 goto failed;
2697 Py_DECREF(value);
2698 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002699failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002700 Py_XDECREF(value);
2701 Py_XDECREF(result);
2702 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703}
2704
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002705PyObject*
2706ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002708 expr_ty o = (expr_ty)_o;
2709 PyObject *result = NULL, *value = NULL;
2710 if (!o) {
2711 Py_INCREF(Py_None);
2712 return Py_None;
2713 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002714
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002715 switch (o->kind) {
2716 case BoolOp_kind:
2717 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2718 if (!result) goto failed;
2719 value = ast2obj_boolop(o->v.BoolOp.op);
2720 if (!value) goto failed;
2721 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2725 if (!value) goto failed;
2726 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2727 goto failed;
2728 Py_DECREF(value);
2729 break;
2730 case BinOp_kind:
2731 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2732 if (!result) goto failed;
2733 value = ast2obj_expr(o->v.BinOp.left);
2734 if (!value) goto failed;
2735 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2736 goto failed;
2737 Py_DECREF(value);
2738 value = ast2obj_operator(o->v.BinOp.op);
2739 if (!value) goto failed;
2740 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2741 goto failed;
2742 Py_DECREF(value);
2743 value = ast2obj_expr(o->v.BinOp.right);
2744 if (!value) goto failed;
2745 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2746 goto failed;
2747 Py_DECREF(value);
2748 break;
2749 case UnaryOp_kind:
2750 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2751 if (!result) goto failed;
2752 value = ast2obj_unaryop(o->v.UnaryOp.op);
2753 if (!value) goto failed;
2754 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2755 goto failed;
2756 Py_DECREF(value);
2757 value = ast2obj_expr(o->v.UnaryOp.operand);
2758 if (!value) goto failed;
2759 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2760 goto failed;
2761 Py_DECREF(value);
2762 break;
2763 case Lambda_kind:
2764 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2765 if (!result) goto failed;
2766 value = ast2obj_arguments(o->v.Lambda.args);
2767 if (!value) goto failed;
2768 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2769 goto failed;
2770 Py_DECREF(value);
2771 value = ast2obj_expr(o->v.Lambda.body);
2772 if (!value) goto failed;
2773 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2774 goto failed;
2775 Py_DECREF(value);
2776 break;
2777 case IfExp_kind:
2778 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2779 if (!result) goto failed;
2780 value = ast2obj_expr(o->v.IfExp.test);
2781 if (!value) goto failed;
2782 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2783 goto failed;
2784 Py_DECREF(value);
2785 value = ast2obj_expr(o->v.IfExp.body);
2786 if (!value) goto failed;
2787 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2788 goto failed;
2789 Py_DECREF(value);
2790 value = ast2obj_expr(o->v.IfExp.orelse);
2791 if (!value) goto failed;
2792 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2793 goto failed;
2794 Py_DECREF(value);
2795 break;
2796 case Dict_kind:
2797 result = PyType_GenericNew(Dict_type, NULL, NULL);
2798 if (!result) goto failed;
2799 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2800 if (!value) goto failed;
2801 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2802 goto failed;
2803 Py_DECREF(value);
2804 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2805 if (!value) goto failed;
2806 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2807 goto failed;
2808 Py_DECREF(value);
2809 break;
2810 case Set_kind:
2811 result = PyType_GenericNew(Set_type, NULL, NULL);
2812 if (!result) goto failed;
2813 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2814 if (!value) goto failed;
2815 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
2816 goto failed;
2817 Py_DECREF(value);
2818 break;
2819 case ListComp_kind:
2820 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2821 if (!result) goto failed;
2822 value = ast2obj_expr(o->v.ListComp.elt);
2823 if (!value) goto failed;
2824 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2825 goto failed;
2826 Py_DECREF(value);
2827 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
2828 if (!value) goto failed;
2829 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2830 goto failed;
2831 Py_DECREF(value);
2832 break;
2833 case SetComp_kind:
2834 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2835 if (!result) goto failed;
2836 value = ast2obj_expr(o->v.SetComp.elt);
2837 if (!value) goto failed;
2838 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2839 goto failed;
2840 Py_DECREF(value);
2841 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
2842 if (!value) goto failed;
2843 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2844 goto failed;
2845 Py_DECREF(value);
2846 break;
2847 case DictComp_kind:
2848 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2849 if (!result) goto failed;
2850 value = ast2obj_expr(o->v.DictComp.key);
2851 if (!value) goto failed;
2852 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 value = ast2obj_expr(o->v.DictComp.value);
2856 if (!value) goto failed;
2857 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2858 goto failed;
2859 Py_DECREF(value);
2860 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
2861 if (!value) goto failed;
2862 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 break;
2866 case GeneratorExp_kind:
2867 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2868 if (!result) goto failed;
2869 value = ast2obj_expr(o->v.GeneratorExp.elt);
2870 if (!value) goto failed;
2871 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
2872 goto failed;
2873 Py_DECREF(value);
2874 value = ast2obj_list(o->v.GeneratorExp.generators,
2875 ast2obj_comprehension);
2876 if (!value) goto failed;
2877 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 break;
2881 case Yield_kind:
2882 result = PyType_GenericNew(Yield_type, NULL, NULL);
2883 if (!result) goto failed;
2884 value = ast2obj_expr(o->v.Yield.value);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 break;
2890 case YieldFrom_kind:
2891 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
2892 if (!result) goto failed;
2893 value = ast2obj_expr(o->v.YieldFrom.value);
2894 if (!value) goto failed;
2895 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2896 goto failed;
2897 Py_DECREF(value);
2898 break;
2899 case Compare_kind:
2900 result = PyType_GenericNew(Compare_type, NULL, NULL);
2901 if (!result) goto failed;
2902 value = ast2obj_expr(o->v.Compare.left);
2903 if (!value) goto failed;
2904 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2905 goto failed;
2906 Py_DECREF(value);
2907 {
2908 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
2909 value = PyList_New(n);
2910 if (!value) goto failed;
2911 for(i = 0; i < n; i++)
2912 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002914 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002915 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
2916 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002917 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002918 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002919 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002920 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
2921 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002922 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002923 break;
2924 case Call_kind:
2925 result = PyType_GenericNew(Call_type, NULL, NULL);
2926 if (!result) goto failed;
2927 value = ast2obj_expr(o->v.Call.func);
2928 if (!value) goto failed;
2929 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
2930 goto failed;
2931 Py_DECREF(value);
2932 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2933 if (!value) goto failed;
2934 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2935 goto failed;
2936 Py_DECREF(value);
2937 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2938 if (!value) goto failed;
2939 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2940 goto failed;
2941 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002942 break;
2943 case Num_kind:
2944 result = PyType_GenericNew(Num_type, NULL, NULL);
2945 if (!result) goto failed;
2946 value = ast2obj_object(o->v.Num.n);
2947 if (!value) goto failed;
2948 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
2949 goto failed;
2950 Py_DECREF(value);
2951 break;
2952 case Str_kind:
2953 result = PyType_GenericNew(Str_type, NULL, NULL);
2954 if (!result) goto failed;
2955 value = ast2obj_string(o->v.Str.s);
2956 if (!value) goto failed;
2957 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2958 goto failed;
2959 Py_DECREF(value);
2960 break;
2961 case Bytes_kind:
2962 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2963 if (!result) goto failed;
2964 value = ast2obj_bytes(o->v.Bytes.s);
2965 if (!value) goto failed;
2966 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002970 case NameConstant_kind:
2971 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
2972 if (!result) goto failed;
2973 value = ast2obj_singleton(o->v.NameConstant.value);
2974 if (!value) goto failed;
2975 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002979 case Ellipsis_kind:
2980 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2981 if (!result) goto failed;
2982 break;
2983 case Attribute_kind:
2984 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2985 if (!result) goto failed;
2986 value = ast2obj_expr(o->v.Attribute.value);
2987 if (!value) goto failed;
2988 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2989 goto failed;
2990 Py_DECREF(value);
2991 value = ast2obj_identifier(o->v.Attribute.attr);
2992 if (!value) goto failed;
2993 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
2994 goto failed;
2995 Py_DECREF(value);
2996 value = ast2obj_expr_context(o->v.Attribute.ctx);
2997 if (!value) goto failed;
2998 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 break;
3002 case Subscript_kind:
3003 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3004 if (!result) goto failed;
3005 value = ast2obj_expr(o->v.Subscript.value);
3006 if (!value) goto failed;
3007 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3008 goto failed;
3009 Py_DECREF(value);
3010 value = ast2obj_slice(o->v.Subscript.slice);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 value = ast2obj_expr_context(o->v.Subscript.ctx);
3016 if (!value) goto failed;
3017 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
3020 break;
3021 case Starred_kind:
3022 result = PyType_GenericNew(Starred_type, NULL, NULL);
3023 if (!result) goto failed;
3024 value = ast2obj_expr(o->v.Starred.value);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 value = ast2obj_expr_context(o->v.Starred.ctx);
3030 if (!value) goto failed;
3031 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3032 goto failed;
3033 Py_DECREF(value);
3034 break;
3035 case Name_kind:
3036 result = PyType_GenericNew(Name_type, NULL, NULL);
3037 if (!result) goto failed;
3038 value = ast2obj_identifier(o->v.Name.id);
3039 if (!value) goto failed;
3040 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3041 goto failed;
3042 Py_DECREF(value);
3043 value = ast2obj_expr_context(o->v.Name.ctx);
3044 if (!value) goto failed;
3045 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3046 goto failed;
3047 Py_DECREF(value);
3048 break;
3049 case List_kind:
3050 result = PyType_GenericNew(List_type, NULL, NULL);
3051 if (!result) goto failed;
3052 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3053 if (!value) goto failed;
3054 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3055 goto failed;
3056 Py_DECREF(value);
3057 value = ast2obj_expr_context(o->v.List.ctx);
3058 if (!value) goto failed;
3059 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3060 goto failed;
3061 Py_DECREF(value);
3062 break;
3063 case Tuple_kind:
3064 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3065 if (!result) goto failed;
3066 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3067 if (!value) goto failed;
3068 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3069 goto failed;
3070 Py_DECREF(value);
3071 value = ast2obj_expr_context(o->v.Tuple.ctx);
3072 if (!value) goto failed;
3073 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3074 goto failed;
3075 Py_DECREF(value);
3076 break;
3077 }
3078 value = ast2obj_int(o->lineno);
3079 if (!value) goto failed;
3080 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3081 goto failed;
3082 Py_DECREF(value);
3083 value = ast2obj_int(o->col_offset);
3084 if (!value) goto failed;
3085 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3086 goto failed;
3087 Py_DECREF(value);
3088 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003089failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003090 Py_XDECREF(value);
3091 Py_XDECREF(result);
3092 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093}
3094
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003095PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003097 switch(o) {
3098 case Load:
3099 Py_INCREF(Load_singleton);
3100 return Load_singleton;
3101 case Store:
3102 Py_INCREF(Store_singleton);
3103 return Store_singleton;
3104 case Del:
3105 Py_INCREF(Del_singleton);
3106 return Del_singleton;
3107 case AugLoad:
3108 Py_INCREF(AugLoad_singleton);
3109 return AugLoad_singleton;
3110 case AugStore:
3111 Py_INCREF(AugStore_singleton);
3112 return AugStore_singleton;
3113 case Param:
3114 Py_INCREF(Param_singleton);
3115 return Param_singleton;
3116 default:
3117 /* should never happen, but just in case ... */
3118 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3119 return NULL;
3120 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003122PyObject*
3123ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003125 slice_ty o = (slice_ty)_o;
3126 PyObject *result = NULL, *value = NULL;
3127 if (!o) {
3128 Py_INCREF(Py_None);
3129 return Py_None;
3130 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003131
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003132 switch (o->kind) {
3133 case Slice_kind:
3134 result = PyType_GenericNew(Slice_type, NULL, NULL);
3135 if (!result) goto failed;
3136 value = ast2obj_expr(o->v.Slice.lower);
3137 if (!value) goto failed;
3138 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3139 goto failed;
3140 Py_DECREF(value);
3141 value = ast2obj_expr(o->v.Slice.upper);
3142 if (!value) goto failed;
3143 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3144 goto failed;
3145 Py_DECREF(value);
3146 value = ast2obj_expr(o->v.Slice.step);
3147 if (!value) goto failed;
3148 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3149 goto failed;
3150 Py_DECREF(value);
3151 break;
3152 case ExtSlice_kind:
3153 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3154 if (!result) goto failed;
3155 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3156 if (!value) goto failed;
3157 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3158 goto failed;
3159 Py_DECREF(value);
3160 break;
3161 case Index_kind:
3162 result = PyType_GenericNew(Index_type, NULL, NULL);
3163 if (!result) goto failed;
3164 value = ast2obj_expr(o->v.Index.value);
3165 if (!value) goto failed;
3166 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
3169 break;
3170 }
3171 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003172failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003173 Py_XDECREF(value);
3174 Py_XDECREF(result);
3175 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176}
3177
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003178PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003180 switch(o) {
3181 case And:
3182 Py_INCREF(And_singleton);
3183 return And_singleton;
3184 case Or:
3185 Py_INCREF(Or_singleton);
3186 return Or_singleton;
3187 default:
3188 /* should never happen, but just in case ... */
3189 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3190 return NULL;
3191 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003192}
3193PyObject* ast2obj_operator(operator_ty o)
3194{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003195 switch(o) {
3196 case Add:
3197 Py_INCREF(Add_singleton);
3198 return Add_singleton;
3199 case Sub:
3200 Py_INCREF(Sub_singleton);
3201 return Sub_singleton;
3202 case Mult:
3203 Py_INCREF(Mult_singleton);
3204 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003205 case MatMult:
3206 Py_INCREF(MatMult_singleton);
3207 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003208 case Div:
3209 Py_INCREF(Div_singleton);
3210 return Div_singleton;
3211 case Mod:
3212 Py_INCREF(Mod_singleton);
3213 return Mod_singleton;
3214 case Pow:
3215 Py_INCREF(Pow_singleton);
3216 return Pow_singleton;
3217 case LShift:
3218 Py_INCREF(LShift_singleton);
3219 return LShift_singleton;
3220 case RShift:
3221 Py_INCREF(RShift_singleton);
3222 return RShift_singleton;
3223 case BitOr:
3224 Py_INCREF(BitOr_singleton);
3225 return BitOr_singleton;
3226 case BitXor:
3227 Py_INCREF(BitXor_singleton);
3228 return BitXor_singleton;
3229 case BitAnd:
3230 Py_INCREF(BitAnd_singleton);
3231 return BitAnd_singleton;
3232 case FloorDiv:
3233 Py_INCREF(FloorDiv_singleton);
3234 return FloorDiv_singleton;
3235 default:
3236 /* should never happen, but just in case ... */
3237 PyErr_Format(PyExc_SystemError, "unknown operator found");
3238 return NULL;
3239 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003240}
3241PyObject* ast2obj_unaryop(unaryop_ty o)
3242{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003243 switch(o) {
3244 case Invert:
3245 Py_INCREF(Invert_singleton);
3246 return Invert_singleton;
3247 case Not:
3248 Py_INCREF(Not_singleton);
3249 return Not_singleton;
3250 case UAdd:
3251 Py_INCREF(UAdd_singleton);
3252 return UAdd_singleton;
3253 case USub:
3254 Py_INCREF(USub_singleton);
3255 return USub_singleton;
3256 default:
3257 /* should never happen, but just in case ... */
3258 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3259 return NULL;
3260 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003261}
3262PyObject* ast2obj_cmpop(cmpop_ty o)
3263{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003264 switch(o) {
3265 case Eq:
3266 Py_INCREF(Eq_singleton);
3267 return Eq_singleton;
3268 case NotEq:
3269 Py_INCREF(NotEq_singleton);
3270 return NotEq_singleton;
3271 case Lt:
3272 Py_INCREF(Lt_singleton);
3273 return Lt_singleton;
3274 case LtE:
3275 Py_INCREF(LtE_singleton);
3276 return LtE_singleton;
3277 case Gt:
3278 Py_INCREF(Gt_singleton);
3279 return Gt_singleton;
3280 case GtE:
3281 Py_INCREF(GtE_singleton);
3282 return GtE_singleton;
3283 case Is:
3284 Py_INCREF(Is_singleton);
3285 return Is_singleton;
3286 case IsNot:
3287 Py_INCREF(IsNot_singleton);
3288 return IsNot_singleton;
3289 case In:
3290 Py_INCREF(In_singleton);
3291 return In_singleton;
3292 case NotIn:
3293 Py_INCREF(NotIn_singleton);
3294 return NotIn_singleton;
3295 default:
3296 /* should never happen, but just in case ... */
3297 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3298 return NULL;
3299 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003300}
3301PyObject*
3302ast2obj_comprehension(void* _o)
3303{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003304 comprehension_ty o = (comprehension_ty)_o;
3305 PyObject *result = NULL, *value = NULL;
3306 if (!o) {
3307 Py_INCREF(Py_None);
3308 return Py_None;
3309 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003310
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003311 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3312 if (!result) return NULL;
3313 value = ast2obj_expr(o->target);
3314 if (!value) goto failed;
3315 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
3318 value = ast2obj_expr(o->iter);
3319 if (!value) goto failed;
3320 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3321 goto failed;
3322 Py_DECREF(value);
3323 value = ast2obj_list(o->ifs, ast2obj_expr);
3324 if (!value) goto failed;
3325 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3326 goto failed;
3327 Py_DECREF(value);
3328 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003329failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003330 Py_XDECREF(value);
3331 Py_XDECREF(result);
3332 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333}
3334
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003335PyObject*
3336ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 excepthandler_ty o = (excepthandler_ty)_o;
3339 PyObject *result = NULL, *value = NULL;
3340 if (!o) {
3341 Py_INCREF(Py_None);
3342 return Py_None;
3343 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003344
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003345 switch (o->kind) {
3346 case ExceptHandler_kind:
3347 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3348 if (!result) goto failed;
3349 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003350 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003351 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3352 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003353 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003354 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003355 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003356 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3357 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003358 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003359 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3360 if (!value) goto failed;
3361 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3362 goto failed;
3363 Py_DECREF(value);
3364 break;
3365 }
3366 value = ast2obj_int(o->lineno);
3367 if (!value) goto failed;
3368 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3369 goto failed;
3370 Py_DECREF(value);
3371 value = ast2obj_int(o->col_offset);
3372 if (!value) goto failed;
3373 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3374 goto failed;
3375 Py_DECREF(value);
3376 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003377failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003378 Py_XDECREF(value);
3379 Py_XDECREF(result);
3380 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
3382
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003383PyObject*
3384ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003386 arguments_ty o = (arguments_ty)_o;
3387 PyObject *result = NULL, *value = NULL;
3388 if (!o) {
3389 Py_INCREF(Py_None);
3390 return Py_None;
3391 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003392
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003393 result = PyType_GenericNew(arguments_type, NULL, NULL);
3394 if (!result) return NULL;
3395 value = ast2obj_list(o->args, ast2obj_arg);
3396 if (!value) goto failed;
3397 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3398 goto failed;
3399 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003400 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 if (!value) goto failed;
3402 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3403 goto failed;
3404 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003405 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3406 if (!value) goto failed;
3407 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3408 goto failed;
3409 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003410 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003411 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003412 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003413 goto failed;
3414 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003415 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003416 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003417 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003418 goto failed;
3419 Py_DECREF(value);
3420 value = ast2obj_list(o->defaults, ast2obj_expr);
3421 if (!value) goto failed;
3422 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3423 goto failed;
3424 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003425 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003426failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003427 Py_XDECREF(value);
3428 Py_XDECREF(result);
3429 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430}
3431
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003432PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003433ast2obj_arg(void* _o)
3434{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003435 arg_ty o = (arg_ty)_o;
3436 PyObject *result = NULL, *value = NULL;
3437 if (!o) {
3438 Py_INCREF(Py_None);
3439 return Py_None;
3440 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003441
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003442 result = PyType_GenericNew(arg_type, NULL, NULL);
3443 if (!result) return NULL;
3444 value = ast2obj_identifier(o->arg);
3445 if (!value) goto failed;
3446 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3447 goto failed;
3448 Py_DECREF(value);
3449 value = ast2obj_expr(o->annotation);
3450 if (!value) goto failed;
3451 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3452 goto failed;
3453 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003454 value = ast2obj_int(o->lineno);
3455 if (!value) goto failed;
3456 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3457 goto failed;
3458 Py_DECREF(value);
3459 value = ast2obj_int(o->col_offset);
3460 if (!value) goto failed;
3461 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3462 goto failed;
3463 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003464 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003465failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003466 Py_XDECREF(value);
3467 Py_XDECREF(result);
3468 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003469}
3470
3471PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003472ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003474 keyword_ty o = (keyword_ty)_o;
3475 PyObject *result = NULL, *value = NULL;
3476 if (!o) {
3477 Py_INCREF(Py_None);
3478 return Py_None;
3479 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003480
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003481 result = PyType_GenericNew(keyword_type, NULL, NULL);
3482 if (!result) return NULL;
3483 value = ast2obj_identifier(o->arg);
3484 if (!value) goto failed;
3485 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3486 goto failed;
3487 Py_DECREF(value);
3488 value = ast2obj_expr(o->value);
3489 if (!value) goto failed;
3490 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3491 goto failed;
3492 Py_DECREF(value);
3493 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003494failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003495 Py_XDECREF(value);
3496 Py_XDECREF(result);
3497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498}
3499
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003500PyObject*
3501ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003503 alias_ty o = (alias_ty)_o;
3504 PyObject *result = NULL, *value = NULL;
3505 if (!o) {
3506 Py_INCREF(Py_None);
3507 return Py_None;
3508 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003509
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003510 result = PyType_GenericNew(alias_type, NULL, NULL);
3511 if (!result) return NULL;
3512 value = ast2obj_identifier(o->name);
3513 if (!value) goto failed;
3514 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3515 goto failed;
3516 Py_DECREF(value);
3517 value = ast2obj_identifier(o->asname);
3518 if (!value) goto failed;
3519 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3520 goto failed;
3521 Py_DECREF(value);
3522 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003523failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003524 Py_XDECREF(value);
3525 Py_XDECREF(result);
3526 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527}
3528
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003529PyObject*
3530ast2obj_withitem(void* _o)
3531{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003532 withitem_ty o = (withitem_ty)_o;
3533 PyObject *result = NULL, *value = NULL;
3534 if (!o) {
3535 Py_INCREF(Py_None);
3536 return Py_None;
3537 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003538
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003539 result = PyType_GenericNew(withitem_type, NULL, NULL);
3540 if (!result) return NULL;
3541 value = ast2obj_expr(o->context_expr);
3542 if (!value) goto failed;
3543 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3544 goto failed;
3545 Py_DECREF(value);
3546 value = ast2obj_expr(o->optional_vars);
3547 if (!value) goto failed;
3548 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3549 goto failed;
3550 Py_DECREF(value);
3551 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003552failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003553 Py_XDECREF(value);
3554 Py_XDECREF(result);
3555 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003556}
3557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003559int
3560obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3561{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003563
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003564 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003565
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003566 if (obj == Py_None) {
3567 *out = NULL;
3568 return 0;
3569 }
3570 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3571 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003572 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003573 }
3574 if (isinstance) {
3575 asdl_seq* body;
3576
3577 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3578 int res;
3579 Py_ssize_t len;
3580 Py_ssize_t i;
3581 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3582 if (tmp == NULL) goto failed;
3583 if (!PyList_Check(tmp)) {
3584 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3585 goto failed;
3586 }
3587 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003588 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003589 if (body == NULL) goto failed;
3590 for (i = 0; i < len; i++) {
3591 stmt_ty value;
3592 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3593 if (res != 0) goto failed;
3594 asdl_seq_SET(body, i, value);
3595 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003596 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003597 } else {
3598 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3599 return 1;
3600 }
3601 *out = Module(body, arena);
3602 if (*out == NULL) goto failed;
3603 return 0;
3604 }
3605 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3606 if (isinstance == -1) {
3607 return 1;
3608 }
3609 if (isinstance) {
3610 asdl_seq* body;
3611
3612 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3613 int res;
3614 Py_ssize_t len;
3615 Py_ssize_t i;
3616 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3617 if (tmp == NULL) goto failed;
3618 if (!PyList_Check(tmp)) {
3619 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3620 goto failed;
3621 }
3622 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003623 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003624 if (body == NULL) goto failed;
3625 for (i = 0; i < len; i++) {
3626 stmt_ty value;
3627 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3628 if (res != 0) goto failed;
3629 asdl_seq_SET(body, i, value);
3630 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003631 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003632 } else {
3633 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3634 return 1;
3635 }
3636 *out = Interactive(body, arena);
3637 if (*out == NULL) goto failed;
3638 return 0;
3639 }
3640 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3641 if (isinstance == -1) {
3642 return 1;
3643 }
3644 if (isinstance) {
3645 expr_ty body;
3646
3647 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3648 int res;
3649 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3650 if (tmp == NULL) goto failed;
3651 res = obj2ast_expr(tmp, &body, arena);
3652 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003653 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003654 } else {
3655 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3656 return 1;
3657 }
3658 *out = Expression(body, arena);
3659 if (*out == NULL) goto failed;
3660 return 0;
3661 }
3662 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3663 if (isinstance == -1) {
3664 return 1;
3665 }
3666 if (isinstance) {
3667 asdl_seq* body;
3668
3669 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3670 int res;
3671 Py_ssize_t len;
3672 Py_ssize_t i;
3673 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3674 if (tmp == NULL) goto failed;
3675 if (!PyList_Check(tmp)) {
3676 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3677 goto failed;
3678 }
3679 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003680 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003681 if (body == NULL) goto failed;
3682 for (i = 0; i < len; i++) {
3683 stmt_ty value;
3684 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3685 if (res != 0) goto failed;
3686 asdl_seq_SET(body, i, value);
3687 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003688 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 } else {
3690 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3691 return 1;
3692 }
3693 *out = Suite(body, arena);
3694 if (*out == NULL) goto failed;
3695 return 0;
3696 }
3697
3698 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3699 failed:
3700 Py_XDECREF(tmp);
3701 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003702}
3703
3704int
3705obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3706{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003707 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003708
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003709 PyObject *tmp = NULL;
3710 int lineno;
3711 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003712
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003713 if (obj == Py_None) {
3714 *out = NULL;
3715 return 0;
3716 }
3717 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3718 int res;
3719 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3720 if (tmp == NULL) goto failed;
3721 res = obj2ast_int(tmp, &lineno, arena);
3722 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003723 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003724 } else {
3725 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003726 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003727 }
3728 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3729 int res;
3730 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3731 if (tmp == NULL) goto failed;
3732 res = obj2ast_int(tmp, &col_offset, arena);
3733 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003734 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003735 } else {
3736 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3737 return 1;
3738 }
3739 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3740 if (isinstance == -1) {
3741 return 1;
3742 }
3743 if (isinstance) {
3744 identifier name;
3745 arguments_ty args;
3746 asdl_seq* body;
3747 asdl_seq* decorator_list;
3748 expr_ty returns;
3749
3750 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3751 int res;
3752 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3753 if (tmp == NULL) goto failed;
3754 res = obj2ast_identifier(tmp, &name, arena);
3755 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003756 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003757 } else {
3758 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3759 return 1;
3760 }
3761 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3762 int res;
3763 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3764 if (tmp == NULL) goto failed;
3765 res = obj2ast_arguments(tmp, &args, arena);
3766 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003767 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003768 } else {
3769 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3770 return 1;
3771 }
3772 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3773 int res;
3774 Py_ssize_t len;
3775 Py_ssize_t i;
3776 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3777 if (tmp == NULL) goto failed;
3778 if (!PyList_Check(tmp)) {
3779 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3780 goto failed;
3781 }
3782 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003783 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003784 if (body == NULL) goto failed;
3785 for (i = 0; i < len; i++) {
3786 stmt_ty value;
3787 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3788 if (res != 0) goto failed;
3789 asdl_seq_SET(body, i, value);
3790 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003791 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 } else {
3793 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3794 return 1;
3795 }
3796 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3797 int res;
3798 Py_ssize_t len;
3799 Py_ssize_t i;
3800 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3801 if (tmp == NULL) goto failed;
3802 if (!PyList_Check(tmp)) {
3803 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3804 goto failed;
3805 }
3806 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003807 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003808 if (decorator_list == NULL) goto failed;
3809 for (i = 0; i < len; i++) {
3810 expr_ty value;
3811 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3812 if (res != 0) goto failed;
3813 asdl_seq_SET(decorator_list, i, value);
3814 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003815 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003816 } else {
3817 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3818 return 1;
3819 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02003820 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003821 int res;
3822 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
3823 if (tmp == NULL) goto failed;
3824 res = obj2ast_expr(tmp, &returns, arena);
3825 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003827 } else {
3828 returns = NULL;
3829 }
3830 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
3831 col_offset, arena);
3832 if (*out == NULL) goto failed;
3833 return 0;
3834 }
3835 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3836 if (isinstance == -1) {
3837 return 1;
3838 }
3839 if (isinstance) {
3840 identifier name;
3841 asdl_seq* bases;
3842 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 asdl_seq* body;
3844 asdl_seq* decorator_list;
3845
3846 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3847 int res;
3848 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3849 if (tmp == NULL) goto failed;
3850 res = obj2ast_identifier(tmp, &name, arena);
3851 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003852 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003853 } else {
3854 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3855 return 1;
3856 }
3857 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
3858 int res;
3859 Py_ssize_t len;
3860 Py_ssize_t i;
3861 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
3862 if (tmp == NULL) goto failed;
3863 if (!PyList_Check(tmp)) {
3864 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3865 goto failed;
3866 }
3867 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003868 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003869 if (bases == NULL) goto failed;
3870 for (i = 0; i < len; i++) {
3871 expr_ty value;
3872 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3873 if (res != 0) goto failed;
3874 asdl_seq_SET(bases, i, value);
3875 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003876 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003877 } else {
3878 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3879 return 1;
3880 }
3881 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
3882 int res;
3883 Py_ssize_t len;
3884 Py_ssize_t i;
3885 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
3886 if (tmp == NULL) goto failed;
3887 if (!PyList_Check(tmp)) {
3888 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3889 goto failed;
3890 }
3891 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003892 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003893 if (keywords == NULL) goto failed;
3894 for (i = 0; i < len; i++) {
3895 keyword_ty value;
3896 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3897 if (res != 0) goto failed;
3898 asdl_seq_SET(keywords, i, value);
3899 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003900 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003901 } else {
3902 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3903 return 1;
3904 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003905 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3906 int res;
3907 Py_ssize_t len;
3908 Py_ssize_t i;
3909 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3910 if (tmp == NULL) goto failed;
3911 if (!PyList_Check(tmp)) {
3912 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3913 goto failed;
3914 }
3915 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003916 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003917 if (body == NULL) goto failed;
3918 for (i = 0; i < len; i++) {
3919 stmt_ty value;
3920 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3921 if (res != 0) goto failed;
3922 asdl_seq_SET(body, i, value);
3923 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003924 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003925 } else {
3926 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3927 return 1;
3928 }
3929 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
3930 int res;
3931 Py_ssize_t len;
3932 Py_ssize_t i;
3933 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
3934 if (tmp == NULL) goto failed;
3935 if (!PyList_Check(tmp)) {
3936 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3937 goto failed;
3938 }
3939 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003940 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 if (decorator_list == NULL) goto failed;
3942 for (i = 0; i < len; i++) {
3943 expr_ty value;
3944 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3945 if (res != 0) goto failed;
3946 asdl_seq_SET(decorator_list, i, value);
3947 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003948 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003949 } else {
3950 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3951 return 1;
3952 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003953 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
3954 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003955 if (*out == NULL) goto failed;
3956 return 0;
3957 }
3958 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3959 if (isinstance == -1) {
3960 return 1;
3961 }
3962 if (isinstance) {
3963 expr_ty value;
3964
Victor Stinneree4b59c2013-07-27 00:01:35 +02003965 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003966 int res;
3967 tmp = _PyObject_GetAttrId(obj, &PyId_value);
3968 if (tmp == NULL) goto failed;
3969 res = obj2ast_expr(tmp, &value, arena);
3970 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003971 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003972 } else {
3973 value = NULL;
3974 }
3975 *out = Return(value, lineno, col_offset, arena);
3976 if (*out == NULL) goto failed;
3977 return 0;
3978 }
3979 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3980 if (isinstance == -1) {
3981 return 1;
3982 }
3983 if (isinstance) {
3984 asdl_seq* targets;
3985
3986 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
3987 int res;
3988 Py_ssize_t len;
3989 Py_ssize_t i;
3990 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
3991 if (tmp == NULL) goto failed;
3992 if (!PyList_Check(tmp)) {
3993 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3994 goto failed;
3995 }
3996 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003997 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003998 if (targets == NULL) goto failed;
3999 for (i = 0; i < len; i++) {
4000 expr_ty value;
4001 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4002 if (res != 0) goto failed;
4003 asdl_seq_SET(targets, i, value);
4004 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004005 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004006 } else {
4007 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4008 return 1;
4009 }
4010 *out = Delete(targets, lineno, col_offset, arena);
4011 if (*out == NULL) goto failed;
4012 return 0;
4013 }
4014 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4015 if (isinstance == -1) {
4016 return 1;
4017 }
4018 if (isinstance) {
4019 asdl_seq* targets;
4020 expr_ty value;
4021
4022 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4023 int res;
4024 Py_ssize_t len;
4025 Py_ssize_t i;
4026 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4027 if (tmp == NULL) goto failed;
4028 if (!PyList_Check(tmp)) {
4029 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4030 goto failed;
4031 }
4032 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004033 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004034 if (targets == NULL) goto failed;
4035 for (i = 0; i < len; i++) {
4036 expr_ty value;
4037 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4038 if (res != 0) goto failed;
4039 asdl_seq_SET(targets, i, value);
4040 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004042 } else {
4043 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4044 return 1;
4045 }
4046 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4047 int res;
4048 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4049 if (tmp == NULL) goto failed;
4050 res = obj2ast_expr(tmp, &value, arena);
4051 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004052 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004053 } else {
4054 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4055 return 1;
4056 }
4057 *out = Assign(targets, value, lineno, col_offset, arena);
4058 if (*out == NULL) goto failed;
4059 return 0;
4060 }
4061 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4062 if (isinstance == -1) {
4063 return 1;
4064 }
4065 if (isinstance) {
4066 expr_ty target;
4067 operator_ty op;
4068 expr_ty value;
4069
4070 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4071 int res;
4072 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4073 if (tmp == NULL) goto failed;
4074 res = obj2ast_expr(tmp, &target, arena);
4075 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004076 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004077 } else {
4078 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4079 return 1;
4080 }
4081 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4082 int res;
4083 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4084 if (tmp == NULL) goto failed;
4085 res = obj2ast_operator(tmp, &op, arena);
4086 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004087 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004088 } else {
4089 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4090 return 1;
4091 }
4092 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4093 int res;
4094 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4095 if (tmp == NULL) goto failed;
4096 res = obj2ast_expr(tmp, &value, arena);
4097 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004098 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004099 } else {
4100 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4101 return 1;
4102 }
4103 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4104 if (*out == NULL) goto failed;
4105 return 0;
4106 }
4107 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4108 if (isinstance == -1) {
4109 return 1;
4110 }
4111 if (isinstance) {
4112 expr_ty target;
4113 expr_ty iter;
4114 asdl_seq* body;
4115 asdl_seq* orelse;
4116
4117 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4118 int res;
4119 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4120 if (tmp == NULL) goto failed;
4121 res = obj2ast_expr(tmp, &target, arena);
4122 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004123 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004124 } else {
4125 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4126 return 1;
4127 }
4128 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4129 int res;
4130 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4131 if (tmp == NULL) goto failed;
4132 res = obj2ast_expr(tmp, &iter, arena);
4133 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004134 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004135 } else {
4136 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4137 return 1;
4138 }
4139 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4140 int res;
4141 Py_ssize_t len;
4142 Py_ssize_t i;
4143 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4144 if (tmp == NULL) goto failed;
4145 if (!PyList_Check(tmp)) {
4146 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4147 goto failed;
4148 }
4149 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004150 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004151 if (body == NULL) goto failed;
4152 for (i = 0; i < len; i++) {
4153 stmt_ty value;
4154 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4155 if (res != 0) goto failed;
4156 asdl_seq_SET(body, i, value);
4157 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004158 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004159 } else {
4160 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4161 return 1;
4162 }
4163 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4164 int res;
4165 Py_ssize_t len;
4166 Py_ssize_t i;
4167 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4168 if (tmp == NULL) goto failed;
4169 if (!PyList_Check(tmp)) {
4170 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4171 goto failed;
4172 }
4173 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004174 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004175 if (orelse == NULL) goto failed;
4176 for (i = 0; i < len; i++) {
4177 stmt_ty value;
4178 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4179 if (res != 0) goto failed;
4180 asdl_seq_SET(orelse, i, value);
4181 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004182 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004183 } else {
4184 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4185 return 1;
4186 }
4187 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4188 if (*out == NULL) goto failed;
4189 return 0;
4190 }
4191 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4192 if (isinstance == -1) {
4193 return 1;
4194 }
4195 if (isinstance) {
4196 expr_ty test;
4197 asdl_seq* body;
4198 asdl_seq* orelse;
4199
4200 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4201 int res;
4202 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4203 if (tmp == NULL) goto failed;
4204 res = obj2ast_expr(tmp, &test, arena);
4205 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004206 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004207 } else {
4208 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4209 return 1;
4210 }
4211 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4212 int res;
4213 Py_ssize_t len;
4214 Py_ssize_t i;
4215 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4216 if (tmp == NULL) goto failed;
4217 if (!PyList_Check(tmp)) {
4218 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4219 goto failed;
4220 }
4221 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004222 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004223 if (body == NULL) goto failed;
4224 for (i = 0; i < len; i++) {
4225 stmt_ty value;
4226 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4227 if (res != 0) goto failed;
4228 asdl_seq_SET(body, i, value);
4229 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004230 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004231 } else {
4232 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4233 return 1;
4234 }
4235 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4236 int res;
4237 Py_ssize_t len;
4238 Py_ssize_t i;
4239 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4240 if (tmp == NULL) goto failed;
4241 if (!PyList_Check(tmp)) {
4242 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4243 goto failed;
4244 }
4245 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004246 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004247 if (orelse == NULL) goto failed;
4248 for (i = 0; i < len; i++) {
4249 stmt_ty value;
4250 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4251 if (res != 0) goto failed;
4252 asdl_seq_SET(orelse, i, value);
4253 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004254 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004255 } else {
4256 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4257 return 1;
4258 }
4259 *out = While(test, body, orelse, lineno, col_offset, arena);
4260 if (*out == NULL) goto failed;
4261 return 0;
4262 }
4263 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4264 if (isinstance == -1) {
4265 return 1;
4266 }
4267 if (isinstance) {
4268 expr_ty test;
4269 asdl_seq* body;
4270 asdl_seq* orelse;
4271
4272 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4273 int res;
4274 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4275 if (tmp == NULL) goto failed;
4276 res = obj2ast_expr(tmp, &test, arena);
4277 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004278 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004279 } else {
4280 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4281 return 1;
4282 }
4283 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4284 int res;
4285 Py_ssize_t len;
4286 Py_ssize_t i;
4287 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4288 if (tmp == NULL) goto failed;
4289 if (!PyList_Check(tmp)) {
4290 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4291 goto failed;
4292 }
4293 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004294 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004295 if (body == NULL) goto failed;
4296 for (i = 0; i < len; i++) {
4297 stmt_ty value;
4298 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4299 if (res != 0) goto failed;
4300 asdl_seq_SET(body, i, value);
4301 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004302 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004303 } else {
4304 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4305 return 1;
4306 }
4307 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4308 int res;
4309 Py_ssize_t len;
4310 Py_ssize_t i;
4311 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4312 if (tmp == NULL) goto failed;
4313 if (!PyList_Check(tmp)) {
4314 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4315 goto failed;
4316 }
4317 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004318 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004319 if (orelse == NULL) goto failed;
4320 for (i = 0; i < len; i++) {
4321 stmt_ty value;
4322 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4323 if (res != 0) goto failed;
4324 asdl_seq_SET(orelse, i, value);
4325 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004326 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004327 } else {
4328 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4329 return 1;
4330 }
4331 *out = If(test, body, orelse, lineno, col_offset, arena);
4332 if (*out == NULL) goto failed;
4333 return 0;
4334 }
4335 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4336 if (isinstance == -1) {
4337 return 1;
4338 }
4339 if (isinstance) {
4340 asdl_seq* items;
4341 asdl_seq* body;
4342
4343 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4344 int res;
4345 Py_ssize_t len;
4346 Py_ssize_t i;
4347 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4348 if (tmp == NULL) goto failed;
4349 if (!PyList_Check(tmp)) {
4350 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4351 goto failed;
4352 }
4353 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004354 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004355 if (items == NULL) goto failed;
4356 for (i = 0; i < len; i++) {
4357 withitem_ty value;
4358 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4359 if (res != 0) goto failed;
4360 asdl_seq_SET(items, i, value);
4361 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004362 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004363 } else {
4364 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4365 return 1;
4366 }
4367 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4368 int res;
4369 Py_ssize_t len;
4370 Py_ssize_t i;
4371 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4372 if (tmp == NULL) goto failed;
4373 if (!PyList_Check(tmp)) {
4374 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4375 goto failed;
4376 }
4377 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004378 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004379 if (body == NULL) goto failed;
4380 for (i = 0; i < len; i++) {
4381 stmt_ty value;
4382 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4383 if (res != 0) goto failed;
4384 asdl_seq_SET(body, i, value);
4385 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004386 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004387 } else {
4388 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4389 return 1;
4390 }
4391 *out = With(items, body, lineno, col_offset, arena);
4392 if (*out == NULL) goto failed;
4393 return 0;
4394 }
4395 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4396 if (isinstance == -1) {
4397 return 1;
4398 }
4399 if (isinstance) {
4400 expr_ty exc;
4401 expr_ty cause;
4402
Victor Stinneree4b59c2013-07-27 00:01:35 +02004403 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004404 int res;
4405 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4406 if (tmp == NULL) goto failed;
4407 res = obj2ast_expr(tmp, &exc, arena);
4408 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004409 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004410 } else {
4411 exc = NULL;
4412 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004413 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004414 int res;
4415 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4416 if (tmp == NULL) goto failed;
4417 res = obj2ast_expr(tmp, &cause, arena);
4418 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004419 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004420 } else {
4421 cause = NULL;
4422 }
4423 *out = Raise(exc, cause, lineno, col_offset, arena);
4424 if (*out == NULL) goto failed;
4425 return 0;
4426 }
4427 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4428 if (isinstance == -1) {
4429 return 1;
4430 }
4431 if (isinstance) {
4432 asdl_seq* body;
4433 asdl_seq* handlers;
4434 asdl_seq* orelse;
4435 asdl_seq* finalbody;
4436
4437 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4438 int res;
4439 Py_ssize_t len;
4440 Py_ssize_t i;
4441 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4442 if (tmp == NULL) goto failed;
4443 if (!PyList_Check(tmp)) {
4444 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4445 goto failed;
4446 }
4447 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004448 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004449 if (body == NULL) goto failed;
4450 for (i = 0; i < len; i++) {
4451 stmt_ty value;
4452 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4453 if (res != 0) goto failed;
4454 asdl_seq_SET(body, i, value);
4455 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004456 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004457 } else {
4458 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4459 return 1;
4460 }
4461 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4462 int res;
4463 Py_ssize_t len;
4464 Py_ssize_t i;
4465 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4466 if (tmp == NULL) goto failed;
4467 if (!PyList_Check(tmp)) {
4468 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4469 goto failed;
4470 }
4471 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004472 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004473 if (handlers == NULL) goto failed;
4474 for (i = 0; i < len; i++) {
4475 excepthandler_ty value;
4476 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4477 if (res != 0) goto failed;
4478 asdl_seq_SET(handlers, i, value);
4479 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004480 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004481 } else {
4482 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4483 return 1;
4484 }
4485 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4486 int res;
4487 Py_ssize_t len;
4488 Py_ssize_t i;
4489 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4490 if (tmp == NULL) goto failed;
4491 if (!PyList_Check(tmp)) {
4492 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4493 goto failed;
4494 }
4495 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004496 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004497 if (orelse == NULL) goto failed;
4498 for (i = 0; i < len; i++) {
4499 stmt_ty value;
4500 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4501 if (res != 0) goto failed;
4502 asdl_seq_SET(orelse, i, value);
4503 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004504 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004505 } else {
4506 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4507 return 1;
4508 }
4509 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4510 int res;
4511 Py_ssize_t len;
4512 Py_ssize_t i;
4513 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4514 if (tmp == NULL) goto failed;
4515 if (!PyList_Check(tmp)) {
4516 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4517 goto failed;
4518 }
4519 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004520 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004521 if (finalbody == NULL) goto failed;
4522 for (i = 0; i < len; i++) {
4523 stmt_ty value;
4524 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4525 if (res != 0) goto failed;
4526 asdl_seq_SET(finalbody, i, value);
4527 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004528 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004529 } else {
4530 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4531 return 1;
4532 }
4533 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4534 arena);
4535 if (*out == NULL) goto failed;
4536 return 0;
4537 }
4538 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4539 if (isinstance == -1) {
4540 return 1;
4541 }
4542 if (isinstance) {
4543 expr_ty test;
4544 expr_ty msg;
4545
4546 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4547 int res;
4548 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4549 if (tmp == NULL) goto failed;
4550 res = obj2ast_expr(tmp, &test, arena);
4551 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004552 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004553 } else {
4554 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4555 return 1;
4556 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004557 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004558 int res;
4559 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
4560 if (tmp == NULL) goto failed;
4561 res = obj2ast_expr(tmp, &msg, arena);
4562 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004563 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004564 } else {
4565 msg = NULL;
4566 }
4567 *out = Assert(test, msg, lineno, col_offset, arena);
4568 if (*out == NULL) goto failed;
4569 return 0;
4570 }
4571 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4572 if (isinstance == -1) {
4573 return 1;
4574 }
4575 if (isinstance) {
4576 asdl_seq* names;
4577
4578 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4579 int res;
4580 Py_ssize_t len;
4581 Py_ssize_t i;
4582 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4583 if (tmp == NULL) goto failed;
4584 if (!PyList_Check(tmp)) {
4585 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4586 goto failed;
4587 }
4588 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004589 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004590 if (names == NULL) goto failed;
4591 for (i = 0; i < len; i++) {
4592 alias_ty value;
4593 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4594 if (res != 0) goto failed;
4595 asdl_seq_SET(names, i, value);
4596 }
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 \"names\" missing from Import");
4600 return 1;
4601 }
4602 *out = Import(names, lineno, col_offset, arena);
4603 if (*out == NULL) goto failed;
4604 return 0;
4605 }
4606 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4607 if (isinstance == -1) {
4608 return 1;
4609 }
4610 if (isinstance) {
4611 identifier module;
4612 asdl_seq* names;
4613 int level;
4614
Victor Stinneree4b59c2013-07-27 00:01:35 +02004615 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004616 int res;
4617 tmp = _PyObject_GetAttrId(obj, &PyId_module);
4618 if (tmp == NULL) goto failed;
4619 res = obj2ast_identifier(tmp, &module, arena);
4620 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004621 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004622 } else {
4623 module = NULL;
4624 }
4625 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4626 int res;
4627 Py_ssize_t len;
4628 Py_ssize_t i;
4629 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4630 if (tmp == NULL) goto failed;
4631 if (!PyList_Check(tmp)) {
4632 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4633 goto failed;
4634 }
4635 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004636 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004637 if (names == NULL) goto failed;
4638 for (i = 0; i < len; i++) {
4639 alias_ty value;
4640 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4641 if (res != 0) goto failed;
4642 asdl_seq_SET(names, i, value);
4643 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004644 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004645 } else {
4646 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4647 return 1;
4648 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004649 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004650 int res;
4651 tmp = _PyObject_GetAttrId(obj, &PyId_level);
4652 if (tmp == NULL) goto failed;
4653 res = obj2ast_int(tmp, &level, arena);
4654 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004655 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004656 } else {
4657 level = 0;
4658 }
4659 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
4660 if (*out == NULL) goto failed;
4661 return 0;
4662 }
4663 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4664 if (isinstance == -1) {
4665 return 1;
4666 }
4667 if (isinstance) {
4668 asdl_seq* names;
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, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4678 goto failed;
4679 }
4680 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004681 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004682 if (names == NULL) goto failed;
4683 for (i = 0; i < len; i++) {
4684 identifier value;
4685 res = obj2ast_identifier(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 Global");
4692 return 1;
4693 }
4694 *out = Global(names, lineno, col_offset, arena);
4695 if (*out == NULL) goto failed;
4696 return 0;
4697 }
4698 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
4699 if (isinstance == -1) {
4700 return 1;
4701 }
4702 if (isinstance) {
4703 asdl_seq* names;
4704
4705 if (_PyObject_HasAttrId(obj, &PyId_names)) {
4706 int res;
4707 Py_ssize_t len;
4708 Py_ssize_t i;
4709 tmp = _PyObject_GetAttrId(obj, &PyId_names);
4710 if (tmp == NULL) goto failed;
4711 if (!PyList_Check(tmp)) {
4712 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4713 goto failed;
4714 }
4715 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004716 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004717 if (names == NULL) goto failed;
4718 for (i = 0; i < len; i++) {
4719 identifier value;
4720 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4721 if (res != 0) goto failed;
4722 asdl_seq_SET(names, i, value);
4723 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004724 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004725 } else {
4726 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4727 return 1;
4728 }
4729 *out = Nonlocal(names, lineno, col_offset, arena);
4730 if (*out == NULL) goto failed;
4731 return 0;
4732 }
4733 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4734 if (isinstance == -1) {
4735 return 1;
4736 }
4737 if (isinstance) {
4738 expr_ty value;
4739
4740 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4741 int res;
4742 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4743 if (tmp == NULL) goto failed;
4744 res = obj2ast_expr(tmp, &value, arena);
4745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004747 } else {
4748 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4749 return 1;
4750 }
4751 *out = Expr(value, lineno, col_offset, arena);
4752 if (*out == NULL) goto failed;
4753 return 0;
4754 }
4755 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4756 if (isinstance == -1) {
4757 return 1;
4758 }
4759 if (isinstance) {
4760
4761 *out = Pass(lineno, col_offset, arena);
4762 if (*out == NULL) goto failed;
4763 return 0;
4764 }
4765 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4766 if (isinstance == -1) {
4767 return 1;
4768 }
4769 if (isinstance) {
4770
4771 *out = Break(lineno, col_offset, arena);
4772 if (*out == NULL) goto failed;
4773 return 0;
4774 }
4775 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4776 if (isinstance == -1) {
4777 return 1;
4778 }
4779 if (isinstance) {
4780
4781 *out = Continue(lineno, col_offset, arena);
4782 if (*out == NULL) goto failed;
4783 return 0;
4784 }
4785
4786 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4787 failed:
4788 Py_XDECREF(tmp);
4789 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004790}
4791
4792int
4793obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4794{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004795 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004796
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004797 PyObject *tmp = NULL;
4798 int lineno;
4799 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004800
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004801 if (obj == Py_None) {
4802 *out = NULL;
4803 return 0;
4804 }
4805 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4806 int res;
4807 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4808 if (tmp == NULL) goto failed;
4809 res = obj2ast_int(tmp, &lineno, arena);
4810 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004811 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004812 } else {
4813 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004814 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004815 }
4816 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4817 int res;
4818 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4819 if (tmp == NULL) goto failed;
4820 res = obj2ast_int(tmp, &col_offset, arena);
4821 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004822 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004823 } else {
4824 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4825 return 1;
4826 }
4827 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4828 if (isinstance == -1) {
4829 return 1;
4830 }
4831 if (isinstance) {
4832 boolop_ty op;
4833 asdl_seq* values;
4834
4835 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4836 int res;
4837 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4838 if (tmp == NULL) goto failed;
4839 res = obj2ast_boolop(tmp, &op, arena);
4840 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004841 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004842 } else {
4843 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4844 return 1;
4845 }
4846 if (_PyObject_HasAttrId(obj, &PyId_values)) {
4847 int res;
4848 Py_ssize_t len;
4849 Py_ssize_t i;
4850 tmp = _PyObject_GetAttrId(obj, &PyId_values);
4851 if (tmp == NULL) goto failed;
4852 if (!PyList_Check(tmp)) {
4853 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4854 goto failed;
4855 }
4856 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004857 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004858 if (values == NULL) goto failed;
4859 for (i = 0; i < len; i++) {
4860 expr_ty value;
4861 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4862 if (res != 0) goto failed;
4863 asdl_seq_SET(values, i, value);
4864 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004865 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004866 } else {
4867 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4868 return 1;
4869 }
4870 *out = BoolOp(op, values, lineno, col_offset, arena);
4871 if (*out == NULL) goto failed;
4872 return 0;
4873 }
4874 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4875 if (isinstance == -1) {
4876 return 1;
4877 }
4878 if (isinstance) {
4879 expr_ty left;
4880 operator_ty op;
4881 expr_ty right;
4882
4883 if (_PyObject_HasAttrId(obj, &PyId_left)) {
4884 int res;
4885 tmp = _PyObject_GetAttrId(obj, &PyId_left);
4886 if (tmp == NULL) goto failed;
4887 res = obj2ast_expr(tmp, &left, arena);
4888 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004889 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004890 } else {
4891 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4892 return 1;
4893 }
4894 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4895 int res;
4896 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4897 if (tmp == NULL) goto failed;
4898 res = obj2ast_operator(tmp, &op, arena);
4899 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004900 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004901 } else {
4902 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4903 return 1;
4904 }
4905 if (_PyObject_HasAttrId(obj, &PyId_right)) {
4906 int res;
4907 tmp = _PyObject_GetAttrId(obj, &PyId_right);
4908 if (tmp == NULL) goto failed;
4909 res = obj2ast_expr(tmp, &right, arena);
4910 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004911 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004912 } else {
4913 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4914 return 1;
4915 }
4916 *out = BinOp(left, op, right, lineno, col_offset, arena);
4917 if (*out == NULL) goto failed;
4918 return 0;
4919 }
4920 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4921 if (isinstance == -1) {
4922 return 1;
4923 }
4924 if (isinstance) {
4925 unaryop_ty op;
4926 expr_ty operand;
4927
4928 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4929 int res;
4930 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4931 if (tmp == NULL) goto failed;
4932 res = obj2ast_unaryop(tmp, &op, 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 \"op\" missing from UnaryOp");
4937 return 1;
4938 }
4939 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
4940 int res;
4941 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
4942 if (tmp == NULL) goto failed;
4943 res = obj2ast_expr(tmp, &operand, 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 \"operand\" missing from UnaryOp");
4948 return 1;
4949 }
4950 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4951 if (*out == NULL) goto failed;
4952 return 0;
4953 }
4954 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4955 if (isinstance == -1) {
4956 return 1;
4957 }
4958 if (isinstance) {
4959 arguments_ty args;
4960 expr_ty body;
4961
4962 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4963 int res;
4964 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4965 if (tmp == NULL) goto failed;
4966 res = obj2ast_arguments(tmp, &args, arena);
4967 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004968 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004969 } else {
4970 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4971 return 1;
4972 }
4973 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4974 int res;
4975 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4976 if (tmp == NULL) goto failed;
4977 res = obj2ast_expr(tmp, &body, 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 \"body\" missing from Lambda");
4982 return 1;
4983 }
4984 *out = Lambda(args, body, lineno, col_offset, arena);
4985 if (*out == NULL) goto failed;
4986 return 0;
4987 }
4988 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4989 if (isinstance == -1) {
4990 return 1;
4991 }
4992 if (isinstance) {
4993 expr_ty test;
4994 expr_ty body;
4995 expr_ty orelse;
4996
4997 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4998 int res;
4999 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5000 if (tmp == NULL) goto failed;
5001 res = obj2ast_expr(tmp, &test, arena);
5002 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005003 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005004 } else {
5005 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5006 return 1;
5007 }
5008 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5009 int res;
5010 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5011 if (tmp == NULL) goto failed;
5012 res = obj2ast_expr(tmp, &body, arena);
5013 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005014 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005015 } else {
5016 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5017 return 1;
5018 }
5019 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5020 int res;
5021 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5022 if (tmp == NULL) goto failed;
5023 res = obj2ast_expr(tmp, &orelse, arena);
5024 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005025 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005026 } else {
5027 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5028 return 1;
5029 }
5030 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5031 if (*out == NULL) goto failed;
5032 return 0;
5033 }
5034 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5035 if (isinstance == -1) {
5036 return 1;
5037 }
5038 if (isinstance) {
5039 asdl_seq* keys;
5040 asdl_seq* values;
5041
5042 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5043 int res;
5044 Py_ssize_t len;
5045 Py_ssize_t i;
5046 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5047 if (tmp == NULL) goto failed;
5048 if (!PyList_Check(tmp)) {
5049 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5050 goto failed;
5051 }
5052 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005053 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005054 if (keys == NULL) goto failed;
5055 for (i = 0; i < len; i++) {
5056 expr_ty value;
5057 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5058 if (res != 0) goto failed;
5059 asdl_seq_SET(keys, i, value);
5060 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005061 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005062 } else {
5063 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5064 return 1;
5065 }
5066 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5067 int res;
5068 Py_ssize_t len;
5069 Py_ssize_t i;
5070 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5071 if (tmp == NULL) goto failed;
5072 if (!PyList_Check(tmp)) {
5073 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5074 goto failed;
5075 }
5076 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005077 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005078 if (values == NULL) goto failed;
5079 for (i = 0; i < len; i++) {
5080 expr_ty value;
5081 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5082 if (res != 0) goto failed;
5083 asdl_seq_SET(values, i, value);
5084 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005085 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005086 } else {
5087 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5088 return 1;
5089 }
5090 *out = Dict(keys, values, lineno, col_offset, arena);
5091 if (*out == NULL) goto failed;
5092 return 0;
5093 }
5094 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5095 if (isinstance == -1) {
5096 return 1;
5097 }
5098 if (isinstance) {
5099 asdl_seq* elts;
5100
5101 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5102 int res;
5103 Py_ssize_t len;
5104 Py_ssize_t i;
5105 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5106 if (tmp == NULL) goto failed;
5107 if (!PyList_Check(tmp)) {
5108 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5109 goto failed;
5110 }
5111 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005112 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005113 if (elts == NULL) goto failed;
5114 for (i = 0; i < len; i++) {
5115 expr_ty value;
5116 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5117 if (res != 0) goto failed;
5118 asdl_seq_SET(elts, i, value);
5119 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005120 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005121 } else {
5122 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5123 return 1;
5124 }
5125 *out = Set(elts, lineno, col_offset, arena);
5126 if (*out == NULL) goto failed;
5127 return 0;
5128 }
5129 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5130 if (isinstance == -1) {
5131 return 1;
5132 }
5133 if (isinstance) {
5134 expr_ty elt;
5135 asdl_seq* generators;
5136
5137 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5138 int res;
5139 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5140 if (tmp == NULL) goto failed;
5141 res = obj2ast_expr(tmp, &elt, arena);
5142 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005143 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005144 } else {
5145 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5146 return 1;
5147 }
5148 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5149 int res;
5150 Py_ssize_t len;
5151 Py_ssize_t i;
5152 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5153 if (tmp == NULL) goto failed;
5154 if (!PyList_Check(tmp)) {
5155 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5156 goto failed;
5157 }
5158 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005159 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005160 if (generators == NULL) goto failed;
5161 for (i = 0; i < len; i++) {
5162 comprehension_ty value;
5163 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5164 if (res != 0) goto failed;
5165 asdl_seq_SET(generators, i, value);
5166 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005167 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005168 } else {
5169 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5170 return 1;
5171 }
5172 *out = ListComp(elt, generators, lineno, col_offset, arena);
5173 if (*out == NULL) goto failed;
5174 return 0;
5175 }
5176 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5177 if (isinstance == -1) {
5178 return 1;
5179 }
5180 if (isinstance) {
5181 expr_ty elt;
5182 asdl_seq* generators;
5183
5184 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5185 int res;
5186 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5187 if (tmp == NULL) goto failed;
5188 res = obj2ast_expr(tmp, &elt, arena);
5189 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005190 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005191 } else {
5192 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5193 return 1;
5194 }
5195 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5196 int res;
5197 Py_ssize_t len;
5198 Py_ssize_t i;
5199 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5200 if (tmp == NULL) goto failed;
5201 if (!PyList_Check(tmp)) {
5202 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5203 goto failed;
5204 }
5205 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005206 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005207 if (generators == NULL) goto failed;
5208 for (i = 0; i < len; i++) {
5209 comprehension_ty value;
5210 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5211 if (res != 0) goto failed;
5212 asdl_seq_SET(generators, i, value);
5213 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005214 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005215 } else {
5216 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5217 return 1;
5218 }
5219 *out = SetComp(elt, generators, lineno, col_offset, arena);
5220 if (*out == NULL) goto failed;
5221 return 0;
5222 }
5223 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5224 if (isinstance == -1) {
5225 return 1;
5226 }
5227 if (isinstance) {
5228 expr_ty key;
5229 expr_ty value;
5230 asdl_seq* generators;
5231
5232 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5233 int res;
5234 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5235 if (tmp == NULL) goto failed;
5236 res = obj2ast_expr(tmp, &key, arena);
5237 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005238 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005239 } else {
5240 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5241 return 1;
5242 }
5243 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5244 int res;
5245 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5246 if (tmp == NULL) goto failed;
5247 res = obj2ast_expr(tmp, &value, arena);
5248 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005249 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005250 } else {
5251 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5252 return 1;
5253 }
5254 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5255 int res;
5256 Py_ssize_t len;
5257 Py_ssize_t i;
5258 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5259 if (tmp == NULL) goto failed;
5260 if (!PyList_Check(tmp)) {
5261 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5262 goto failed;
5263 }
5264 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005265 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005266 if (generators == NULL) goto failed;
5267 for (i = 0; i < len; i++) {
5268 comprehension_ty value;
5269 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5270 if (res != 0) goto failed;
5271 asdl_seq_SET(generators, i, value);
5272 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005273 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005274 } else {
5275 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5276 return 1;
5277 }
5278 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5279 if (*out == NULL) goto failed;
5280 return 0;
5281 }
5282 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5283 if (isinstance == -1) {
5284 return 1;
5285 }
5286 if (isinstance) {
5287 expr_ty elt;
5288 asdl_seq* generators;
5289
5290 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5291 int res;
5292 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5293 if (tmp == NULL) goto failed;
5294 res = obj2ast_expr(tmp, &elt, arena);
5295 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005296 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005297 } else {
5298 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5299 return 1;
5300 }
5301 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5302 int res;
5303 Py_ssize_t len;
5304 Py_ssize_t i;
5305 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5306 if (tmp == NULL) goto failed;
5307 if (!PyList_Check(tmp)) {
5308 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5309 goto failed;
5310 }
5311 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005312 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005313 if (generators == NULL) goto failed;
5314 for (i = 0; i < len; i++) {
5315 comprehension_ty value;
5316 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5317 if (res != 0) goto failed;
5318 asdl_seq_SET(generators, i, value);
5319 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005320 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005321 } else {
5322 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5323 return 1;
5324 }
5325 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5326 if (*out == NULL) goto failed;
5327 return 0;
5328 }
5329 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5330 if (isinstance == -1) {
5331 return 1;
5332 }
5333 if (isinstance) {
5334 expr_ty value;
5335
Victor Stinneree4b59c2013-07-27 00:01:35 +02005336 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005337 int res;
5338 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5339 if (tmp == NULL) goto failed;
5340 res = obj2ast_expr(tmp, &value, arena);
5341 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005342 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005343 } else {
5344 value = NULL;
5345 }
5346 *out = Yield(value, lineno, col_offset, arena);
5347 if (*out == NULL) goto failed;
5348 return 0;
5349 }
5350 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5351 if (isinstance == -1) {
5352 return 1;
5353 }
5354 if (isinstance) {
5355 expr_ty value;
5356
5357 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5358 int res;
5359 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5360 if (tmp == NULL) goto failed;
5361 res = obj2ast_expr(tmp, &value, arena);
5362 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005363 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005364 } else {
5365 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5366 return 1;
5367 }
5368 *out = YieldFrom(value, lineno, col_offset, arena);
5369 if (*out == NULL) goto failed;
5370 return 0;
5371 }
5372 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5373 if (isinstance == -1) {
5374 return 1;
5375 }
5376 if (isinstance) {
5377 expr_ty left;
5378 asdl_int_seq* ops;
5379 asdl_seq* comparators;
5380
5381 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5382 int res;
5383 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5384 if (tmp == NULL) goto failed;
5385 res = obj2ast_expr(tmp, &left, 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 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5390 return 1;
5391 }
5392 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5393 int res;
5394 Py_ssize_t len;
5395 Py_ssize_t i;
5396 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5397 if (tmp == NULL) goto failed;
5398 if (!PyList_Check(tmp)) {
5399 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5400 goto failed;
5401 }
5402 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005403 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005404 if (ops == NULL) goto failed;
5405 for (i = 0; i < len; i++) {
5406 cmpop_ty value;
5407 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5408 if (res != 0) goto failed;
5409 asdl_seq_SET(ops, i, value);
5410 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005411 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005412 } else {
5413 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5414 return 1;
5415 }
5416 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5417 int res;
5418 Py_ssize_t len;
5419 Py_ssize_t i;
5420 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5421 if (tmp == NULL) goto failed;
5422 if (!PyList_Check(tmp)) {
5423 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5424 goto failed;
5425 }
5426 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005427 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005428 if (comparators == NULL) goto failed;
5429 for (i = 0; i < len; i++) {
5430 expr_ty value;
5431 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5432 if (res != 0) goto failed;
5433 asdl_seq_SET(comparators, i, value);
5434 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005436 } else {
5437 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5438 return 1;
5439 }
5440 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5441 if (*out == NULL) goto failed;
5442 return 0;
5443 }
5444 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5445 if (isinstance == -1) {
5446 return 1;
5447 }
5448 if (isinstance) {
5449 expr_ty func;
5450 asdl_seq* args;
5451 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005452
5453 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5454 int res;
5455 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5456 if (tmp == NULL) goto failed;
5457 res = obj2ast_expr(tmp, &func, arena);
5458 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005459 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005460 } else {
5461 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5462 return 1;
5463 }
5464 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5465 int res;
5466 Py_ssize_t len;
5467 Py_ssize_t i;
5468 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5469 if (tmp == NULL) goto failed;
5470 if (!PyList_Check(tmp)) {
5471 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5472 goto failed;
5473 }
5474 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005475 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005476 if (args == NULL) goto failed;
5477 for (i = 0; i < len; i++) {
5478 expr_ty value;
5479 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5480 if (res != 0) goto failed;
5481 asdl_seq_SET(args, i, value);
5482 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005483 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005484 } else {
5485 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5486 return 1;
5487 }
5488 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5489 int res;
5490 Py_ssize_t len;
5491 Py_ssize_t i;
5492 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5493 if (tmp == NULL) goto failed;
5494 if (!PyList_Check(tmp)) {
5495 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5496 goto failed;
5497 }
5498 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005499 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005500 if (keywords == NULL) goto failed;
5501 for (i = 0; i < len; i++) {
5502 keyword_ty value;
5503 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5504 if (res != 0) goto failed;
5505 asdl_seq_SET(keywords, i, value);
5506 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005507 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005508 } else {
5509 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5510 return 1;
5511 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005512 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005513 if (*out == NULL) goto failed;
5514 return 0;
5515 }
5516 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5517 if (isinstance == -1) {
5518 return 1;
5519 }
5520 if (isinstance) {
5521 object n;
5522
5523 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5524 int res;
5525 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5526 if (tmp == NULL) goto failed;
5527 res = obj2ast_object(tmp, &n, arena);
5528 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005529 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005530 } else {
5531 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5532 return 1;
5533 }
5534 *out = Num(n, lineno, col_offset, arena);
5535 if (*out == NULL) goto failed;
5536 return 0;
5537 }
5538 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5539 if (isinstance == -1) {
5540 return 1;
5541 }
5542 if (isinstance) {
5543 string s;
5544
5545 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5546 int res;
5547 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5548 if (tmp == NULL) goto failed;
5549 res = obj2ast_string(tmp, &s, arena);
5550 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005551 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005552 } else {
5553 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5554 return 1;
5555 }
5556 *out = Str(s, lineno, col_offset, arena);
5557 if (*out == NULL) goto failed;
5558 return 0;
5559 }
5560 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
5561 if (isinstance == -1) {
5562 return 1;
5563 }
5564 if (isinstance) {
5565 bytes s;
5566
5567 if (_PyObject_HasAttrId(obj, &PyId_s)) {
5568 int res;
5569 tmp = _PyObject_GetAttrId(obj, &PyId_s);
5570 if (tmp == NULL) goto failed;
5571 res = obj2ast_bytes(tmp, &s, arena);
5572 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005573 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005574 } else {
5575 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5576 return 1;
5577 }
5578 *out = Bytes(s, lineno, col_offset, arena);
5579 if (*out == NULL) goto failed;
5580 return 0;
5581 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005582 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
5583 if (isinstance == -1) {
5584 return 1;
5585 }
5586 if (isinstance) {
5587 singleton value;
5588
5589 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5590 int res;
5591 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5592 if (tmp == NULL) goto failed;
5593 res = obj2ast_singleton(tmp, &value, arena);
5594 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02005595 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02005596 } else {
5597 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
5598 return 1;
5599 }
5600 *out = NameConstant(value, lineno, col_offset, arena);
5601 if (*out == NULL) goto failed;
5602 return 0;
5603 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005604 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5605 if (isinstance == -1) {
5606 return 1;
5607 }
5608 if (isinstance) {
5609
5610 *out = Ellipsis(lineno, col_offset, arena);
5611 if (*out == NULL) goto failed;
5612 return 0;
5613 }
5614 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5615 if (isinstance == -1) {
5616 return 1;
5617 }
5618 if (isinstance) {
5619 expr_ty value;
5620 identifier attr;
5621 expr_context_ty ctx;
5622
5623 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5624 int res;
5625 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5626 if (tmp == NULL) goto failed;
5627 res = obj2ast_expr(tmp, &value, arena);
5628 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005629 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005630 } else {
5631 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5632 return 1;
5633 }
5634 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
5635 int res;
5636 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
5637 if (tmp == NULL) goto failed;
5638 res = obj2ast_identifier(tmp, &attr, arena);
5639 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005640 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005641 } else {
5642 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5643 return 1;
5644 }
5645 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5646 int res;
5647 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5648 if (tmp == NULL) goto failed;
5649 res = obj2ast_expr_context(tmp, &ctx, arena);
5650 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005651 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005652 } else {
5653 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5654 return 1;
5655 }
5656 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5657 if (*out == NULL) goto failed;
5658 return 0;
5659 }
5660 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5661 if (isinstance == -1) {
5662 return 1;
5663 }
5664 if (isinstance) {
5665 expr_ty value;
5666 slice_ty slice;
5667 expr_context_ty ctx;
5668
5669 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5670 int res;
5671 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5672 if (tmp == NULL) goto failed;
5673 res = obj2ast_expr(tmp, &value, arena);
5674 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005675 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005676 } else {
5677 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5678 return 1;
5679 }
5680 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
5681 int res;
5682 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
5683 if (tmp == NULL) goto failed;
5684 res = obj2ast_slice(tmp, &slice, arena);
5685 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005686 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005687 } else {
5688 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5689 return 1;
5690 }
5691 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5692 int res;
5693 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5694 if (tmp == NULL) goto failed;
5695 res = obj2ast_expr_context(tmp, &ctx, 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 \"ctx\" missing from Subscript");
5700 return 1;
5701 }
5702 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5703 if (*out == NULL) goto failed;
5704 return 0;
5705 }
5706 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
5707 if (isinstance == -1) {
5708 return 1;
5709 }
5710 if (isinstance) {
5711 expr_ty value;
5712 expr_context_ty ctx;
5713
5714 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5715 int res;
5716 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5717 if (tmp == NULL) goto failed;
5718 res = obj2ast_expr(tmp, &value, arena);
5719 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005720 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005721 } else {
5722 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5723 return 1;
5724 }
5725 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5726 int res;
5727 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5728 if (tmp == NULL) goto failed;
5729 res = obj2ast_expr_context(tmp, &ctx, arena);
5730 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005731 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005732 } else {
5733 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5734 return 1;
5735 }
5736 *out = Starred(value, ctx, lineno, col_offset, arena);
5737 if (*out == NULL) goto failed;
5738 return 0;
5739 }
5740 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5741 if (isinstance == -1) {
5742 return 1;
5743 }
5744 if (isinstance) {
5745 identifier id;
5746 expr_context_ty ctx;
5747
5748 if (_PyObject_HasAttrId(obj, &PyId_id)) {
5749 int res;
5750 tmp = _PyObject_GetAttrId(obj, &PyId_id);
5751 if (tmp == NULL) goto failed;
5752 res = obj2ast_identifier(tmp, &id, 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 \"id\" missing from Name");
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 Name");
5768 return 1;
5769 }
5770 *out = Name(id, ctx, lineno, col_offset, arena);
5771 if (*out == NULL) goto failed;
5772 return 0;
5773 }
5774 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5775 if (isinstance == -1) {
5776 return 1;
5777 }
5778 if (isinstance) {
5779 asdl_seq* elts;
5780 expr_context_ty ctx;
5781
5782 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5783 int res;
5784 Py_ssize_t len;
5785 Py_ssize_t i;
5786 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5787 if (tmp == NULL) goto failed;
5788 if (!PyList_Check(tmp)) {
5789 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5790 goto failed;
5791 }
5792 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005793 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005794 if (elts == NULL) goto failed;
5795 for (i = 0; i < len; i++) {
5796 expr_ty value;
5797 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5798 if (res != 0) goto failed;
5799 asdl_seq_SET(elts, i, value);
5800 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005801 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005802 } else {
5803 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5804 return 1;
5805 }
5806 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5807 int res;
5808 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5809 if (tmp == NULL) goto failed;
5810 res = obj2ast_expr_context(tmp, &ctx, arena);
5811 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005812 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005813 } else {
5814 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5815 return 1;
5816 }
5817 *out = List(elts, ctx, lineno, col_offset, arena);
5818 if (*out == NULL) goto failed;
5819 return 0;
5820 }
5821 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5822 if (isinstance == -1) {
5823 return 1;
5824 }
5825 if (isinstance) {
5826 asdl_seq* elts;
5827 expr_context_ty ctx;
5828
5829 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5830 int res;
5831 Py_ssize_t len;
5832 Py_ssize_t i;
5833 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5834 if (tmp == NULL) goto failed;
5835 if (!PyList_Check(tmp)) {
5836 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5837 goto failed;
5838 }
5839 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005840 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 if (elts == NULL) goto failed;
5842 for (i = 0; i < len; i++) {
5843 expr_ty value;
5844 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5845 if (res != 0) goto failed;
5846 asdl_seq_SET(elts, i, value);
5847 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005848 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005849 } else {
5850 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5851 return 1;
5852 }
5853 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
5854 int res;
5855 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
5856 if (tmp == NULL) goto failed;
5857 res = obj2ast_expr_context(tmp, &ctx, arena);
5858 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005859 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005860 } else {
5861 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5862 return 1;
5863 }
5864 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5865 if (*out == NULL) goto failed;
5866 return 0;
5867 }
5868
5869 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5870 failed:
5871 Py_XDECREF(tmp);
5872 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005873}
5874
5875int
5876obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5877{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005878 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005879
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005880 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5881 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005882 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005883 }
5884 if (isinstance) {
5885 *out = Load;
5886 return 0;
5887 }
5888 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5889 if (isinstance == -1) {
5890 return 1;
5891 }
5892 if (isinstance) {
5893 *out = Store;
5894 return 0;
5895 }
5896 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5897 if (isinstance == -1) {
5898 return 1;
5899 }
5900 if (isinstance) {
5901 *out = Del;
5902 return 0;
5903 }
5904 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5905 if (isinstance == -1) {
5906 return 1;
5907 }
5908 if (isinstance) {
5909 *out = AugLoad;
5910 return 0;
5911 }
5912 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5913 if (isinstance == -1) {
5914 return 1;
5915 }
5916 if (isinstance) {
5917 *out = AugStore;
5918 return 0;
5919 }
5920 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5921 if (isinstance == -1) {
5922 return 1;
5923 }
5924 if (isinstance) {
5925 *out = Param;
5926 return 0;
5927 }
5928
5929 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
5930 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005931}
5932
5933int
5934obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5935{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005936 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005937
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005938 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005939
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005940 if (obj == Py_None) {
5941 *out = NULL;
5942 return 0;
5943 }
5944 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5945 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005946 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005947 }
5948 if (isinstance) {
5949 expr_ty lower;
5950 expr_ty upper;
5951 expr_ty step;
5952
Victor Stinneree4b59c2013-07-27 00:01:35 +02005953 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005954 int res;
5955 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
5956 if (tmp == NULL) goto failed;
5957 res = obj2ast_expr(tmp, &lower, arena);
5958 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005959 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005960 } else {
5961 lower = NULL;
5962 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005963 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005964 int res;
5965 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
5966 if (tmp == NULL) goto failed;
5967 res = obj2ast_expr(tmp, &upper, arena);
5968 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005969 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005970 } else {
5971 upper = NULL;
5972 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005973 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005974 int res;
5975 tmp = _PyObject_GetAttrId(obj, &PyId_step);
5976 if (tmp == NULL) goto failed;
5977 res = obj2ast_expr(tmp, &step, arena);
5978 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005979 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005980 } else {
5981 step = NULL;
5982 }
5983 *out = Slice(lower, upper, step, arena);
5984 if (*out == NULL) goto failed;
5985 return 0;
5986 }
5987 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5988 if (isinstance == -1) {
5989 return 1;
5990 }
5991 if (isinstance) {
5992 asdl_seq* dims;
5993
5994 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
5995 int res;
5996 Py_ssize_t len;
5997 Py_ssize_t i;
5998 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
5999 if (tmp == NULL) goto failed;
6000 if (!PyList_Check(tmp)) {
6001 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6002 goto failed;
6003 }
6004 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006005 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006006 if (dims == NULL) goto failed;
6007 for (i = 0; i < len; i++) {
6008 slice_ty value;
6009 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6010 if (res != 0) goto failed;
6011 asdl_seq_SET(dims, i, value);
6012 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006013 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006014 } else {
6015 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6016 return 1;
6017 }
6018 *out = ExtSlice(dims, arena);
6019 if (*out == NULL) goto failed;
6020 return 0;
6021 }
6022 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6023 if (isinstance == -1) {
6024 return 1;
6025 }
6026 if (isinstance) {
6027 expr_ty value;
6028
6029 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6030 int res;
6031 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6032 if (tmp == NULL) goto failed;
6033 res = obj2ast_expr(tmp, &value, arena);
6034 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006035 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 } else {
6037 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6038 return 1;
6039 }
6040 *out = Index(value, arena);
6041 if (*out == NULL) goto failed;
6042 return 0;
6043 }
6044
6045 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6046 failed:
6047 Py_XDECREF(tmp);
6048 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006049}
6050
6051int
6052obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6053{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006054 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006055
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006056 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6057 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006058 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006059 }
6060 if (isinstance) {
6061 *out = And;
6062 return 0;
6063 }
6064 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6065 if (isinstance == -1) {
6066 return 1;
6067 }
6068 if (isinstance) {
6069 *out = Or;
6070 return 0;
6071 }
6072
6073 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6074 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006075}
6076
6077int
6078obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6079{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006080 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006081
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006082 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6083 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006084 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006085 }
6086 if (isinstance) {
6087 *out = Add;
6088 return 0;
6089 }
6090 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6091 if (isinstance == -1) {
6092 return 1;
6093 }
6094 if (isinstance) {
6095 *out = Sub;
6096 return 0;
6097 }
6098 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6099 if (isinstance == -1) {
6100 return 1;
6101 }
6102 if (isinstance) {
6103 *out = Mult;
6104 return 0;
6105 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006106 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6107 if (isinstance == -1) {
6108 return 1;
6109 }
6110 if (isinstance) {
6111 *out = MatMult;
6112 return 0;
6113 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006114 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6115 if (isinstance == -1) {
6116 return 1;
6117 }
6118 if (isinstance) {
6119 *out = Div;
6120 return 0;
6121 }
6122 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6123 if (isinstance == -1) {
6124 return 1;
6125 }
6126 if (isinstance) {
6127 *out = Mod;
6128 return 0;
6129 }
6130 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6131 if (isinstance == -1) {
6132 return 1;
6133 }
6134 if (isinstance) {
6135 *out = Pow;
6136 return 0;
6137 }
6138 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6139 if (isinstance == -1) {
6140 return 1;
6141 }
6142 if (isinstance) {
6143 *out = LShift;
6144 return 0;
6145 }
6146 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6147 if (isinstance == -1) {
6148 return 1;
6149 }
6150 if (isinstance) {
6151 *out = RShift;
6152 return 0;
6153 }
6154 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6155 if (isinstance == -1) {
6156 return 1;
6157 }
6158 if (isinstance) {
6159 *out = BitOr;
6160 return 0;
6161 }
6162 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6163 if (isinstance == -1) {
6164 return 1;
6165 }
6166 if (isinstance) {
6167 *out = BitXor;
6168 return 0;
6169 }
6170 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
6175 *out = BitAnd;
6176 return 0;
6177 }
6178 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6179 if (isinstance == -1) {
6180 return 1;
6181 }
6182 if (isinstance) {
6183 *out = FloorDiv;
6184 return 0;
6185 }
6186
6187 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6188 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006189}
6190
6191int
6192obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6193{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006194 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006195
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006196 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6197 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006198 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006199 }
6200 if (isinstance) {
6201 *out = Invert;
6202 return 0;
6203 }
6204 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6205 if (isinstance == -1) {
6206 return 1;
6207 }
6208 if (isinstance) {
6209 *out = Not;
6210 return 0;
6211 }
6212 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6213 if (isinstance == -1) {
6214 return 1;
6215 }
6216 if (isinstance) {
6217 *out = UAdd;
6218 return 0;
6219 }
6220 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6221 if (isinstance == -1) {
6222 return 1;
6223 }
6224 if (isinstance) {
6225 *out = USub;
6226 return 0;
6227 }
6228
6229 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6230 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006231}
6232
6233int
6234obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6235{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006236 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006237
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006238 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6239 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006240 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006241 }
6242 if (isinstance) {
6243 *out = Eq;
6244 return 0;
6245 }
6246 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6247 if (isinstance == -1) {
6248 return 1;
6249 }
6250 if (isinstance) {
6251 *out = NotEq;
6252 return 0;
6253 }
6254 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6255 if (isinstance == -1) {
6256 return 1;
6257 }
6258 if (isinstance) {
6259 *out = Lt;
6260 return 0;
6261 }
6262 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6263 if (isinstance == -1) {
6264 return 1;
6265 }
6266 if (isinstance) {
6267 *out = LtE;
6268 return 0;
6269 }
6270 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6271 if (isinstance == -1) {
6272 return 1;
6273 }
6274 if (isinstance) {
6275 *out = Gt;
6276 return 0;
6277 }
6278 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6279 if (isinstance == -1) {
6280 return 1;
6281 }
6282 if (isinstance) {
6283 *out = GtE;
6284 return 0;
6285 }
6286 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6287 if (isinstance == -1) {
6288 return 1;
6289 }
6290 if (isinstance) {
6291 *out = Is;
6292 return 0;
6293 }
6294 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6295 if (isinstance == -1) {
6296 return 1;
6297 }
6298 if (isinstance) {
6299 *out = IsNot;
6300 return 0;
6301 }
6302 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6303 if (isinstance == -1) {
6304 return 1;
6305 }
6306 if (isinstance) {
6307 *out = In;
6308 return 0;
6309 }
6310 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6311 if (isinstance == -1) {
6312 return 1;
6313 }
6314 if (isinstance) {
6315 *out = NotIn;
6316 return 0;
6317 }
6318
6319 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6320 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006321}
6322
6323int
6324obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6325{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006326 PyObject* tmp = NULL;
6327 expr_ty target;
6328 expr_ty iter;
6329 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006330
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006331 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6332 int res;
6333 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6334 if (tmp == NULL) goto failed;
6335 res = obj2ast_expr(tmp, &target, arena);
6336 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006337 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006338 } else {
6339 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006340 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006341 }
6342 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6343 int res;
6344 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6345 if (tmp == NULL) goto failed;
6346 res = obj2ast_expr(tmp, &iter, arena);
6347 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006348 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006349 } else {
6350 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6351 return 1;
6352 }
6353 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6354 int res;
6355 Py_ssize_t len;
6356 Py_ssize_t i;
6357 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6358 if (tmp == NULL) goto failed;
6359 if (!PyList_Check(tmp)) {
6360 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6361 goto failed;
6362 }
6363 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006364 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006365 if (ifs == NULL) goto failed;
6366 for (i = 0; i < len; i++) {
6367 expr_ty value;
6368 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6369 if (res != 0) goto failed;
6370 asdl_seq_SET(ifs, i, value);
6371 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006372 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006373 } else {
6374 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6375 return 1;
6376 }
6377 *out = comprehension(target, iter, ifs, arena);
6378 return 0;
6379failed:
6380 Py_XDECREF(tmp);
6381 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006382}
6383
6384int
6385obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6386{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006387 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006388
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006389 PyObject *tmp = NULL;
6390 int lineno;
6391 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006392
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006393 if (obj == Py_None) {
6394 *out = NULL;
6395 return 0;
6396 }
6397 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6398 int res;
6399 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6400 if (tmp == NULL) goto failed;
6401 res = obj2ast_int(tmp, &lineno, arena);
6402 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006403 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006404 } else {
6405 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006406 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006407 }
6408 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6409 int res;
6410 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6411 if (tmp == NULL) goto failed;
6412 res = obj2ast_int(tmp, &col_offset, arena);
6413 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006414 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006415 } else {
6416 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6417 return 1;
6418 }
6419 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6420 if (isinstance == -1) {
6421 return 1;
6422 }
6423 if (isinstance) {
6424 expr_ty type;
6425 identifier name;
6426 asdl_seq* body;
6427
Victor Stinneree4b59c2013-07-27 00:01:35 +02006428 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006429 int res;
6430 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6431 if (tmp == NULL) goto failed;
6432 res = obj2ast_expr(tmp, &type, arena);
6433 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006434 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006435 } else {
6436 type = NULL;
6437 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006438 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006439 int res;
6440 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6441 if (tmp == NULL) goto failed;
6442 res = obj2ast_identifier(tmp, &name, arena);
6443 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006444 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006445 } else {
6446 name = NULL;
6447 }
6448 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6449 int res;
6450 Py_ssize_t len;
6451 Py_ssize_t i;
6452 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6453 if (tmp == NULL) goto failed;
6454 if (!PyList_Check(tmp)) {
6455 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6456 goto failed;
6457 }
6458 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006459 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006460 if (body == NULL) goto failed;
6461 for (i = 0; i < len; i++) {
6462 stmt_ty value;
6463 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6464 if (res != 0) goto failed;
6465 asdl_seq_SET(body, i, value);
6466 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006467 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006468 } else {
6469 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6470 return 1;
6471 }
6472 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6473 if (*out == NULL) goto failed;
6474 return 0;
6475 }
6476
6477 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6478 failed:
6479 Py_XDECREF(tmp);
6480 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006481}
6482
6483int
6484obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6485{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006486 PyObject* tmp = NULL;
6487 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006488 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006489 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006490 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006491 arg_ty kwarg;
6492 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006493
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006494 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6495 int res;
6496 Py_ssize_t len;
6497 Py_ssize_t i;
6498 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6499 if (tmp == NULL) goto failed;
6500 if (!PyList_Check(tmp)) {
6501 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6502 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006503 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006505 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006506 if (args == NULL) goto failed;
6507 for (i = 0; i < len; i++) {
6508 arg_ty value;
6509 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6510 if (res != 0) goto failed;
6511 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006512 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006513 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006514 } else {
6515 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006516 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006517 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006518 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006519 int res;
6520 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6521 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006522 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006523 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006524 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006525 } else {
6526 vararg = NULL;
6527 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006528 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6529 int res;
6530 Py_ssize_t len;
6531 Py_ssize_t i;
6532 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6533 if (tmp == NULL) goto failed;
6534 if (!PyList_Check(tmp)) {
6535 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6536 goto failed;
6537 }
6538 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006539 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006540 if (kwonlyargs == NULL) goto failed;
6541 for (i = 0; i < len; i++) {
6542 arg_ty value;
6543 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6544 if (res != 0) goto failed;
6545 asdl_seq_SET(kwonlyargs, i, value);
6546 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006547 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006548 } else {
6549 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6550 return 1;
6551 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006552 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
6553 int res;
6554 Py_ssize_t len;
6555 Py_ssize_t i;
6556 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
6557 if (tmp == NULL) goto failed;
6558 if (!PyList_Check(tmp)) {
6559 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6560 goto failed;
6561 }
6562 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006563 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006564 if (kw_defaults == NULL) goto failed;
6565 for (i = 0; i < len; i++) {
6566 expr_ty value;
6567 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6568 if (res != 0) goto failed;
6569 asdl_seq_SET(kw_defaults, i, value);
6570 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006571 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006572 } else {
6573 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6574 return 1;
6575 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006576 if (exists_not_none(obj, &PyId_kwarg)) {
6577 int res;
6578 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
6579 if (tmp == NULL) goto failed;
6580 res = obj2ast_arg(tmp, &kwarg, arena);
6581 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006582 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006583 } else {
6584 kwarg = NULL;
6585 }
6586 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
6587 int res;
6588 Py_ssize_t len;
6589 Py_ssize_t i;
6590 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
6591 if (tmp == NULL) goto failed;
6592 if (!PyList_Check(tmp)) {
6593 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6594 goto failed;
6595 }
6596 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006597 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006598 if (defaults == NULL) goto failed;
6599 for (i = 0; i < len; i++) {
6600 expr_ty value;
6601 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6602 if (res != 0) goto failed;
6603 asdl_seq_SET(defaults, i, value);
6604 }
Victor Stinnerb3189902013-07-27 00:04:42 +02006605 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006606 } else {
6607 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6608 return 1;
6609 }
6610 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
6611 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006612 return 0;
6613failed:
6614 Py_XDECREF(tmp);
6615 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006616}
6617
6618int
6619obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6620{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006621 PyObject* tmp = NULL;
6622 identifier arg;
6623 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006624
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006625 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
6626 int res;
6627 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6628 if (tmp == NULL) goto failed;
6629 res = obj2ast_identifier(tmp, &arg, arena);
6630 if (res != 0) goto failed;
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 \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006634 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006635 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006636 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006637 int res;
6638 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
6639 if (tmp == NULL) goto failed;
6640 res = obj2ast_expr(tmp, &annotation, arena);
6641 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006642 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006643 } else {
6644 annotation = NULL;
6645 }
6646 *out = arg(arg, annotation, arena);
6647 return 0;
6648failed:
6649 Py_XDECREF(tmp);
6650 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006651}
6652
6653int
6654obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6655{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006656 PyObject* tmp = NULL;
6657 identifier arg;
6658 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006659
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006660 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006661 int res;
6662 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
6663 if (tmp == NULL) goto failed;
6664 res = obj2ast_identifier(tmp, &arg, arena);
6665 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006666 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006667 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006668 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006669 }
6670 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6671 int res;
6672 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6673 if (tmp == NULL) goto failed;
6674 res = obj2ast_expr(tmp, &value, arena);
6675 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006676 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006677 } else {
6678 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6679 return 1;
6680 }
6681 *out = keyword(arg, value, arena);
6682 return 0;
6683failed:
6684 Py_XDECREF(tmp);
6685 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006686}
6687
6688int
6689obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6690{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006691 PyObject* tmp = NULL;
6692 identifier name;
6693 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006694
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006695 if (_PyObject_HasAttrId(obj, &PyId_name)) {
6696 int res;
6697 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6698 if (tmp == NULL) goto failed;
6699 res = obj2ast_identifier(tmp, &name, arena);
6700 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006701 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006702 } else {
6703 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006704 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006705 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006706 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006707 int res;
6708 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
6709 if (tmp == NULL) goto failed;
6710 res = obj2ast_identifier(tmp, &asname, arena);
6711 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006712 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006713 } else {
6714 asname = NULL;
6715 }
6716 *out = alias(name, asname, arena);
6717 return 0;
6718failed:
6719 Py_XDECREF(tmp);
6720 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006721}
6722
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006723int
6724obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
6725{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006726 PyObject* tmp = NULL;
6727 expr_ty context_expr;
6728 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006729
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006730 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
6731 int res;
6732 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
6733 if (tmp == NULL) goto failed;
6734 res = obj2ast_expr(tmp, &context_expr, arena);
6735 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006736 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006737 } else {
6738 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006739 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006740 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006741 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006742 int res;
6743 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
6744 if (tmp == NULL) goto failed;
6745 res = obj2ast_expr(tmp, &optional_vars, arena);
6746 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006747 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006748 } else {
6749 optional_vars = NULL;
6750 }
6751 *out = withitem(context_expr, optional_vars, arena);
6752 return 0;
6753failed:
6754 Py_XDECREF(tmp);
6755 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05006756}
6757
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006758
Martin v. Löwis1a214512008-06-11 05:26:20 +00006759static struct PyModuleDef _astmodule = {
6760 PyModuleDef_HEAD_INIT, "_ast"
6761};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006762PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00006763PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006764{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006765 PyObject *m, *d;
6766 if (!init_types()) return NULL;
6767 m = PyModule_Create(&_astmodule);
6768 if (!m) return NULL;
6769 d = PyModule_GetDict(m);
6770 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006771 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006772 return NULL;
6773 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
6774 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
6775 NULL;
6776 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
6777 0) return NULL;
6778 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
6779 return NULL;
6780 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
6781 NULL;
6782 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
6783 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
6784 0) return NULL;
6785 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6786 return NULL;
6787 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
6788 NULL;
6789 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
6790 NULL;
6791 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
6792 NULL;
6793 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
6794 return NULL;
6795 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
6796 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
6797 NULL;
6798 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
6799 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
6800 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
6801 NULL;
6802 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
6803 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
6804 NULL;
6805 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
6806 NULL;
6807 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
6808 return NULL;
6809 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
6810 NULL;
6811 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6812 return NULL;
6813 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
6814 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
6815 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
6816 NULL;
6817 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6818 return NULL;
6819 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
6820 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
6821 NULL;
6822 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
6823 NULL;
6824 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
6825 NULL;
6826 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
6827 NULL;
6828 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
6829 NULL;
6830 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
6831 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
6832 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6833 return NULL;
6834 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
6835 NULL;
6836 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6837 return NULL;
6838 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
6839 0) return NULL;
6840 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
6841 NULL;
6842 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
6843 return NULL;
6844 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
6845 NULL;
6846 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
6847 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
6848 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
6849 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
6850 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006851 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
6852 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006853 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6854 return NULL;
6855 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
6856 return NULL;
6857 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
6858 return NULL;
6859 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
6860 NULL;
6861 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
6862 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
6863 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
6864 NULL;
6865 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
6866 0) return NULL;
6867 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
6868 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
6869 NULL;
6870 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
6871 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
6872 NULL;
6873 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6874 return NULL;
6875 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
6876 NULL;
6877 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
6878 NULL;
6879 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
6880 NULL;
6881 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6882 return NULL;
6883 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
6884 NULL;
6885 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
6886 NULL;
6887 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
6888 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
6889 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6890 return NULL;
6891 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
6892 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
6893 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04006894 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
6895 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006896 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
6897 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
6898 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
6899 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
6900 NULL;
6901 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
6902 NULL;
6903 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
6904 NULL;
6905 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
6906 NULL;
6907 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
6908 NULL;
6909 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6910 return NULL;
6911 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
6912 NULL;
6913 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
6914 NULL;
6915 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
6916 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
6917 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
6918 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
6919 NULL;
6920 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
6921 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
6922 NULL;
6923 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
6924 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
6925 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
6926 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
6927 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
6928 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
6929 NULL;
6930 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
6931 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
6932 NULL;
6933 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
6934 < 0) return NULL;
6935 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
6936 < 0) return NULL;
6937 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
6938 < 0) return NULL;
6939 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
6940 return NULL;
6941 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
6942 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
6943 NULL;
6944 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
6945 NULL;
6946 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
6947 return NULL;
6948 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006949}
6950
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006951
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006952PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006953{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02006954 if (!init_types())
6955 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006956 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006957}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006958
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006959/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6960mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006961{
6962 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05006963 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006964 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006965 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05006966
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05006967 req_type[0] = (PyObject*)Module_type;
6968 req_type[1] = (PyObject*)Expression_type;
6969 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05006970
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006971 assert(0 <= mode && mode <= 2);
6972
Victor Stinnerbdf630c2013-07-17 00:17:15 +02006973 if (!init_types())
6974 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006975
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00006976 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6977 if (isinstance == -1)
6978 return NULL;
6979 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006980 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6981 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006982 return NULL;
6983 }
6984 if (obj2ast_mod(ast, &res, arena) != 0)
6985 return NULL;
6986 else
6987 return res;
6988}
6989
6990int PyAST_Check(PyObject* obj)
6991{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02006992 if (!init_types())
6993 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00006994 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006995}
6996
Martin v. Löwis5b222132007-06-10 09:51:05 +00006997