blob: 6ab57dfe8e68adfb1227f8a540e265506cb3a1c3 [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};
Yury Selivanov75445082015-05-11 22:57:16 -040048static PyTypeObject *AsyncFunctionDef_type;
49static char *AsyncFunctionDef_fields[]={
50 "name",
51 "args",
52 "body",
53 "decorator_list",
54 "returns",
55};
Neal Norwitz53d960c2006-02-28 22:47:29 +000056static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020057_Py_IDENTIFIER(bases);
58_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000059static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020060 "name",
61 "bases",
62 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020063 "body",
64 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000065};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020067_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020069 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075};
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static PyTypeObject *Assign_type;
77static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020078 "targets",
79 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(target);
83_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020085 "target",
86 "op",
87 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088};
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070089static PyTypeObject *AnnAssign_type;
90_Py_IDENTIFIER(annotation);
91_Py_IDENTIFIER(simple);
92static char *AnnAssign_fields[]={
93 "target",
94 "annotation",
95 "value",
96 "simple",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020099_Py_IDENTIFIER(iter);
100_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000101static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200102 "target",
103 "iter",
104 "body",
105 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Yury Selivanov75445082015-05-11 22:57:16 -0400107static PyTypeObject *AsyncFor_type;
108static char *AsyncFor_fields[]={
109 "target",
110 "iter",
111 "body",
112 "orelse",
113};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000114static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200115_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200117 "test",
118 "body",
119 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *If_type;
122static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200123 "test",
124 "body",
125 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200128_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000129static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200130 "items",
131 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000132};
Yury Selivanov75445082015-05-11 22:57:16 -0400133static PyTypeObject *AsyncWith_type;
134static char *AsyncWith_fields[]={
135 "items",
136 "body",
137};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000138static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200139_Py_IDENTIFIER(exc);
140_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000141static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200142 "exc",
143 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500145static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146_Py_IDENTIFIER(handlers);
147_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500148static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200149 "body",
150 "handlers",
151 "orelse",
152 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200155_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200157 "test",
158 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000159};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000160static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200161_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000162static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200163 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200166_Py_IDENTIFIER(module);
167_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000168static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200169 "module",
170 "names",
171 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000173static PyTypeObject *Global_type;
174static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200175 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000176};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000177static PyTypeObject *Nonlocal_type;
178static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200179 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000180};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static PyTypeObject *Expr_type;
182static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200183 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *Pass_type;
186static PyTypeObject *Break_type;
187static PyTypeObject *Continue_type;
188static PyTypeObject *expr_type;
189static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200190 "lineno",
191 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000192};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000194static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200195_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200197 "op",
198 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000199};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000200static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200201_Py_IDENTIFIER(left);
202_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000203static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200204 "left",
205 "op",
206 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000208static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200209_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200211 "op",
212 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000214static PyTypeObject *Lambda_type;
215static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200216 "args",
217 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *IfExp_type;
220static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200221 "test",
222 "body",
223 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000224};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000225static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200226_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200228 "keys",
229 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000231static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200232_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000233static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200234 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000235};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000236static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200237_Py_IDENTIFIER(elt);
238_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000239static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200240 "elt",
241 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000242};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000243static PyTypeObject *SetComp_type;
244static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200245 "elt",
246 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000247};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000248static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200249_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000250static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200251 "key",
252 "value",
253 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000254};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000255static PyTypeObject *GeneratorExp_type;
256static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200257 "elt",
258 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259};
Yury Selivanov75445082015-05-11 22:57:16 -0400260static PyTypeObject *Await_type;
261static char *Await_fields[]={
262 "value",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *Yield_type;
265static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200266 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500267};
268static PyTypeObject *YieldFrom_type;
269static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200270 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000272static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200273_Py_IDENTIFIER(ops);
274_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000275static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200276 "left",
277 "ops",
278 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000279};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000280static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200281_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000282static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200283 "func",
284 "args",
285 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000287static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200288_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000289static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200293_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000294static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200295 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000296};
Eric V. Smith235a6f02015-09-19 14:51:32 -0400297static PyTypeObject *FormattedValue_type;
298_Py_IDENTIFIER(conversion);
299_Py_IDENTIFIER(format_spec);
300static char *FormattedValue_fields[]={
301 "value",
302 "conversion",
303 "format_spec",
304};
305static PyTypeObject *JoinedStr_type;
306static char *JoinedStr_fields[]={
307 "values",
308};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000309static PyTypeObject *Bytes_type;
310static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200311 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000312};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500313static PyTypeObject *NameConstant_type;
314static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200315 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500316};
Georg Brandl52318d62006-09-06 07:06:08 +0000317static PyTypeObject *Ellipsis_type;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100318static PyTypeObject *Constant_type;
319static char *Constant_fields[]={
320 "value",
321};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000322static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200323_Py_IDENTIFIER(attr);
324_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000325static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200326 "value",
327 "attr",
328 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000329};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000330static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200331_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000332static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200333 "value",
334 "slice",
335 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000336};
Guido van Rossum0368b722007-05-11 16:50:42 +0000337static PyTypeObject *Starred_type;
338static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200339 "value",
340 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000341};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000342static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200343_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000344static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200345 "id",
346 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static PyTypeObject *List_type;
349static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200350 "elts",
351 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *Tuple_type;
354static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200355 "elts",
356 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000359static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
360*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
361static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000362static PyTypeObject *Load_type;
363static PyTypeObject *Store_type;
364static PyTypeObject *Del_type;
365static PyTypeObject *AugLoad_type;
366static PyTypeObject *AugStore_type;
367static PyTypeObject *Param_type;
368static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000370static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200371_Py_IDENTIFIER(lower);
372_Py_IDENTIFIER(upper);
373_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000374static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200375 "lower",
376 "upper",
377 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000378};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000379static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200380_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000381static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200382 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000383};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static PyTypeObject *Index_type;
385static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200386 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000388static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000389static PyObject *And_singleton, *Or_singleton;
390static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static PyTypeObject *And_type;
392static PyTypeObject *Or_type;
393static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000394static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400395*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
396*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
397*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000398static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000399static PyTypeObject *Add_type;
400static PyTypeObject *Sub_type;
401static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400402static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000403static PyTypeObject *Div_type;
404static PyTypeObject *Mod_type;
405static PyTypeObject *Pow_type;
406static PyTypeObject *LShift_type;
407static PyTypeObject *RShift_type;
408static PyTypeObject *BitOr_type;
409static PyTypeObject *BitXor_type;
410static PyTypeObject *BitAnd_type;
411static PyTypeObject *FloorDiv_type;
412static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000413static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
414*USub_singleton;
415static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000416static PyTypeObject *Invert_type;
417static PyTypeObject *Not_type;
418static PyTypeObject *UAdd_type;
419static PyTypeObject *USub_type;
420static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000421static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
422*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
423*NotIn_singleton;
424static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000425static PyTypeObject *Eq_type;
426static PyTypeObject *NotEq_type;
427static PyTypeObject *Lt_type;
428static PyTypeObject *LtE_type;
429static PyTypeObject *Gt_type;
430static PyTypeObject *GtE_type;
431static PyTypeObject *Is_type;
432static PyTypeObject *IsNot_type;
433static PyTypeObject *In_type;
434static PyTypeObject *NotIn_type;
435static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000436static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200437_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000438static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200439 "target",
440 "iter",
441 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000442};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000443static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000444static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200445 "lineno",
446 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000447};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000448static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000449static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200450_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000451static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200452 "type",
453 "name",
454 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000455};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000456static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000457static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200458_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200459_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200460_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700461_Py_IDENTIFIER(kwarg);
462_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000463static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200464 "args",
465 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200466 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200467 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200468 "kwarg",
469 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000470};
Neal Norwitzc1505362006-12-28 06:47:50 +0000471static PyTypeObject *arg_type;
472static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700473static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200474 "lineno",
475 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700476};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200477_Py_IDENTIFIER(arg);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000478static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200479 "arg",
480 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000481};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000482static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000483static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000484static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200485 "arg",
486 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000487};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000488static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000489static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200490_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000491static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200492 "name",
493 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000494};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500495static PyTypeObject *withitem_type;
496static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200497_Py_IDENTIFIER(context_expr);
498_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500499static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200500 "context_expr",
501 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500502};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000503
504
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700505typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100506 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700507 PyObject *dict;
508} AST_object;
509
Benjamin Peterson1767e022012-03-14 21:50:29 -0500510static void
511ast_dealloc(AST_object *self)
512{
513 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200514 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500515}
516
Neal Norwitz207c9f32008-03-31 04:42:11 +0000517static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700518ast_traverse(AST_object *self, visitproc visit, void *arg)
519{
520 Py_VISIT(self->dict);
521 return 0;
522}
523
524static void
525ast_clear(AST_object *self)
526{
527 Py_CLEAR(self->dict);
528}
529
530static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000531ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
532{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200533 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000534 Py_ssize_t i, numfields = 0;
535 int res = -1;
536 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200537 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000538 if (!fields)
539 PyErr_Clear();
540 if (fields) {
541 numfields = PySequence_Size(fields);
542 if (numfields == -1)
543 goto cleanup;
544 }
545 res = 0; /* if no error occurs, this stays 0 to the end */
546 if (PyTuple_GET_SIZE(args) > 0) {
547 if (numfields != PyTuple_GET_SIZE(args)) {
548 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000549 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000550 Py_TYPE(self)->tp_name,
551 numfields == 0 ? "" : "either 0 or ",
552 numfields, numfields == 1 ? "" : "s");
553 res = -1;
554 goto cleanup;
555 }
556 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
557 /* cannot be reached when fields is NULL */
558 PyObject *name = PySequence_GetItem(fields, i);
559 if (!name) {
560 res = -1;
561 goto cleanup;
562 }
563 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
564 Py_DECREF(name);
565 if (res < 0)
566 goto cleanup;
567 }
568 }
569 if (kw) {
570 i = 0; /* needed by PyDict_Next */
571 while (PyDict_Next(kw, &i, &key, &value)) {
572 res = PyObject_SetAttr(self, key, value);
573 if (res < 0)
574 goto cleanup;
575 }
576 }
577 cleanup:
578 Py_XDECREF(fields);
579 return res;
580}
581
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000582/* Pickling support */
583static PyObject *
584ast_type_reduce(PyObject *self, PyObject *unused)
585{
586 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200587 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200588 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000589 if (dict == NULL) {
590 if (PyErr_ExceptionMatches(PyExc_AttributeError))
591 PyErr_Clear();
592 else
593 return NULL;
594 }
595 if (dict) {
596 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
597 Py_DECREF(dict);
598 return res;
599 }
600 return Py_BuildValue("O()", Py_TYPE(self));
601}
602
603static PyMethodDef ast_type_methods[] = {
604 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
605 {NULL}
606};
607
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700608static PyGetSetDef ast_type_getsets[] = {
609 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
610 {NULL}
611};
612
Neal Norwitz207c9f32008-03-31 04:42:11 +0000613static PyTypeObject AST_type = {
614 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000615 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700616 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000617 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500618 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000619 0, /* tp_print */
620 0, /* tp_getattr */
621 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000622 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000623 0, /* tp_repr */
624 0, /* tp_as_number */
625 0, /* tp_as_sequence */
626 0, /* tp_as_mapping */
627 0, /* tp_hash */
628 0, /* tp_call */
629 0, /* tp_str */
630 PyObject_GenericGetAttr, /* tp_getattro */
631 PyObject_GenericSetAttr, /* tp_setattro */
632 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000634 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700635 (traverseproc)ast_traverse, /* tp_traverse */
636 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000637 0, /* tp_richcompare */
638 0, /* tp_weaklistoffset */
639 0, /* tp_iter */
640 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000641 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000642 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700643 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000644 0, /* tp_base */
645 0, /* tp_dict */
646 0, /* tp_descr_get */
647 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700648 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000649 (initproc)ast_type_init, /* tp_init */
650 PyType_GenericAlloc, /* tp_alloc */
651 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700652 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000653};
654
655
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
657{
658 PyObject *fnames, *result;
659 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000660 fnames = PyTuple_New(num_fields);
661 if (!fnames) return NULL;
662 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000663 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 if (!field) {
665 Py_DECREF(fnames);
666 return NULL;
667 }
668 PyTuple_SET_ITEM(fnames, i, field);
669 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000670 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 Py_DECREF(fnames);
673 return (PyTypeObject*)result;
674}
675
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000676static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
677{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200679 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000680 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000681 if (!l)
682 return 0;
683 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000684 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!s) {
686 Py_DECREF(l);
687 return 0;
688 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000689 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200691 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 Py_DECREF(l);
693 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694}
695
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000696/* Conversion AST -> Python */
697
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
699{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700700 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 PyObject *result = PyList_New(n);
702 PyObject *value;
703 if (!result)
704 return NULL;
705 for (i = 0; i < n; i++) {
706 value = func(asdl_seq_GET(seq, i));
707 if (!value) {
708 Py_DECREF(result);
709 return NULL;
710 }
711 PyList_SET_ITEM(result, i, value);
712 }
713 return result;
714}
715
716static PyObject* ast2obj_object(void *o)
717{
718 if (!o)
719 o = Py_None;
720 Py_INCREF((PyObject*)o);
721 return (PyObject*)o;
722}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500723#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100724#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000725#define ast2obj_identifier ast2obj_object
726#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500727#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000729static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730{
Christian Heimes217cfd12007-12-02 14:31:20 +0000731 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732}
733
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000734/* Conversion Python -> AST */
735
Benjamin Peterson442f2092012-12-06 17:41:04 -0500736static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
737{
738 if (obj != Py_None && obj != Py_True && obj != Py_False) {
739 PyErr_SetString(PyExc_ValueError,
740 "AST singleton must be True, False, or None");
741 return 1;
742 }
743 *out = obj;
744 return 0;
745}
746
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000747static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
748{
749 if (obj == Py_None)
750 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200751 if (obj) {
752 if (PyArena_AddPyObject(arena, obj) < 0) {
753 *out = NULL;
754 return -1;
755 }
756 Py_INCREF(obj);
757 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000758 *out = obj;
759 return 0;
760}
761
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100762static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
763{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100764 if (obj) {
765 if (PyArena_AddPyObject(arena, obj) < 0) {
766 *out = NULL;
767 return -1;
768 }
769 Py_INCREF(obj);
770 }
771 *out = obj;
772 return 0;
773}
774
Benjamin Peterson180e6352011-07-22 11:09:07 -0500775static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500776{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500777 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
778 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500779 return 1;
780 }
781 return obj2ast_object(obj, out, arena);
782}
783
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500784static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
785{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400786 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500787 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
788 return 1;
789 }
790 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500791}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000792
Benjamin Petersone2498412011-08-09 16:08:39 -0500793static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
794{
795 if (!PyBytes_CheckExact(obj)) {
796 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
797 return 1;
798 }
799 return obj2ast_object(obj, out, arena);
800}
801
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000802static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
803{
804 int i;
805 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100806 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000807 return 1;
808 }
809
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300810 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000811 if (i == -1 && PyErr_Occurred())
812 return 1;
813 *out = i;
814 return 0;
815}
816
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000817static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000818{
819 PyObject *empty_tuple, *d;
820 if (PyType_Ready(&AST_type) < 0)
821 return -1;
822 d = AST_type.tp_dict;
823 empty_tuple = PyTuple_New(0);
824 if (!empty_tuple ||
825 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
826 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
827 Py_XDECREF(empty_tuple);
828 return -1;
829 }
830 Py_DECREF(empty_tuple);
831 return 0;
832}
833
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700834static int exists_not_none(PyObject *obj, _Py_Identifier *id)
835{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700836 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700837 PyObject *attr = _PyObject_GetAttrId(obj, id);
838 if (!attr) {
839 PyErr_Clear();
840 return 0;
841 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700842 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700843 Py_DECREF(attr);
844 return !isnone;
845}
846
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000847
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848static int init_types(void)
849{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200850 static int initialized;
851 if (initialized) return 1;
852 if (add_ast_fields() < 0) return 0;
853 mod_type = make_type("mod", &AST_type, NULL, 0);
854 if (!mod_type) return 0;
855 if (!add_attributes(mod_type, NULL, 0)) return 0;
856 Module_type = make_type("Module", mod_type, Module_fields, 1);
857 if (!Module_type) return 0;
858 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
859 1);
860 if (!Interactive_type) return 0;
861 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
862 if (!Expression_type) return 0;
863 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
864 if (!Suite_type) return 0;
865 stmt_type = make_type("stmt", &AST_type, NULL, 0);
866 if (!stmt_type) return 0;
867 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
868 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
869 5);
870 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400871 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
872 AsyncFunctionDef_fields, 5);
873 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400874 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200875 if (!ClassDef_type) return 0;
876 Return_type = make_type("Return", stmt_type, Return_fields, 1);
877 if (!Return_type) return 0;
878 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
879 if (!Delete_type) return 0;
880 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
881 if (!Assign_type) return 0;
882 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
883 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700884 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
885 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200886 For_type = make_type("For", stmt_type, For_fields, 4);
887 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400888 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
889 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200890 While_type = make_type("While", stmt_type, While_fields, 3);
891 if (!While_type) return 0;
892 If_type = make_type("If", stmt_type, If_fields, 3);
893 if (!If_type) return 0;
894 With_type = make_type("With", stmt_type, With_fields, 2);
895 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400896 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
897 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200898 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
899 if (!Raise_type) return 0;
900 Try_type = make_type("Try", stmt_type, Try_fields, 4);
901 if (!Try_type) return 0;
902 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
903 if (!Assert_type) return 0;
904 Import_type = make_type("Import", stmt_type, Import_fields, 1);
905 if (!Import_type) return 0;
906 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
907 if (!ImportFrom_type) return 0;
908 Global_type = make_type("Global", stmt_type, Global_fields, 1);
909 if (!Global_type) return 0;
910 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
911 if (!Nonlocal_type) return 0;
912 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
913 if (!Expr_type) return 0;
914 Pass_type = make_type("Pass", stmt_type, NULL, 0);
915 if (!Pass_type) return 0;
916 Break_type = make_type("Break", stmt_type, NULL, 0);
917 if (!Break_type) return 0;
918 Continue_type = make_type("Continue", stmt_type, NULL, 0);
919 if (!Continue_type) return 0;
920 expr_type = make_type("expr", &AST_type, NULL, 0);
921 if (!expr_type) return 0;
922 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
923 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
924 if (!BoolOp_type) return 0;
925 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
926 if (!BinOp_type) return 0;
927 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
928 if (!UnaryOp_type) return 0;
929 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
930 if (!Lambda_type) return 0;
931 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
932 if (!IfExp_type) return 0;
933 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
934 if (!Dict_type) return 0;
935 Set_type = make_type("Set", expr_type, Set_fields, 1);
936 if (!Set_type) return 0;
937 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
938 if (!ListComp_type) return 0;
939 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
940 if (!SetComp_type) return 0;
941 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
942 if (!DictComp_type) return 0;
943 GeneratorExp_type = make_type("GeneratorExp", expr_type,
944 GeneratorExp_fields, 2);
945 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400946 Await_type = make_type("Await", expr_type, Await_fields, 1);
947 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200948 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
949 if (!Yield_type) return 0;
950 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
951 if (!YieldFrom_type) return 0;
952 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
953 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400954 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200955 if (!Call_type) return 0;
956 Num_type = make_type("Num", expr_type, Num_fields, 1);
957 if (!Num_type) return 0;
958 Str_type = make_type("Str", expr_type, Str_fields, 1);
959 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400960 FormattedValue_type = make_type("FormattedValue", expr_type,
961 FormattedValue_fields, 3);
962 if (!FormattedValue_type) return 0;
963 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
964 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200965 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
966 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200967 NameConstant_type = make_type("NameConstant", expr_type,
968 NameConstant_fields, 1);
969 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200970 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
971 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100972 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
973 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200974 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
975 if (!Attribute_type) return 0;
976 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
977 if (!Subscript_type) return 0;
978 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
979 if (!Starred_type) return 0;
980 Name_type = make_type("Name", expr_type, Name_fields, 2);
981 if (!Name_type) return 0;
982 List_type = make_type("List", expr_type, List_fields, 2);
983 if (!List_type) return 0;
984 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
985 if (!Tuple_type) return 0;
986 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
987 if (!expr_context_type) return 0;
988 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
989 Load_type = make_type("Load", expr_context_type, NULL, 0);
990 if (!Load_type) return 0;
991 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
992 if (!Load_singleton) return 0;
993 Store_type = make_type("Store", expr_context_type, NULL, 0);
994 if (!Store_type) return 0;
995 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
996 if (!Store_singleton) return 0;
997 Del_type = make_type("Del", expr_context_type, NULL, 0);
998 if (!Del_type) return 0;
999 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1000 if (!Del_singleton) return 0;
1001 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1002 if (!AugLoad_type) return 0;
1003 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1004 if (!AugLoad_singleton) return 0;
1005 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1006 if (!AugStore_type) return 0;
1007 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1008 if (!AugStore_singleton) return 0;
1009 Param_type = make_type("Param", expr_context_type, NULL, 0);
1010 if (!Param_type) return 0;
1011 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1012 if (!Param_singleton) return 0;
1013 slice_type = make_type("slice", &AST_type, NULL, 0);
1014 if (!slice_type) return 0;
1015 if (!add_attributes(slice_type, NULL, 0)) return 0;
1016 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1017 if (!Slice_type) return 0;
1018 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1019 if (!ExtSlice_type) return 0;
1020 Index_type = make_type("Index", slice_type, Index_fields, 1);
1021 if (!Index_type) return 0;
1022 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1023 if (!boolop_type) return 0;
1024 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1025 And_type = make_type("And", boolop_type, NULL, 0);
1026 if (!And_type) return 0;
1027 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1028 if (!And_singleton) return 0;
1029 Or_type = make_type("Or", boolop_type, NULL, 0);
1030 if (!Or_type) return 0;
1031 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1032 if (!Or_singleton) return 0;
1033 operator_type = make_type("operator", &AST_type, NULL, 0);
1034 if (!operator_type) return 0;
1035 if (!add_attributes(operator_type, NULL, 0)) return 0;
1036 Add_type = make_type("Add", operator_type, NULL, 0);
1037 if (!Add_type) return 0;
1038 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1039 if (!Add_singleton) return 0;
1040 Sub_type = make_type("Sub", operator_type, NULL, 0);
1041 if (!Sub_type) return 0;
1042 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1043 if (!Sub_singleton) return 0;
1044 Mult_type = make_type("Mult", operator_type, NULL, 0);
1045 if (!Mult_type) return 0;
1046 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1047 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001048 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1049 if (!MatMult_type) return 0;
1050 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1051 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001052 Div_type = make_type("Div", operator_type, NULL, 0);
1053 if (!Div_type) return 0;
1054 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1055 if (!Div_singleton) return 0;
1056 Mod_type = make_type("Mod", operator_type, NULL, 0);
1057 if (!Mod_type) return 0;
1058 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1059 if (!Mod_singleton) return 0;
1060 Pow_type = make_type("Pow", operator_type, NULL, 0);
1061 if (!Pow_type) return 0;
1062 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1063 if (!Pow_singleton) return 0;
1064 LShift_type = make_type("LShift", operator_type, NULL, 0);
1065 if (!LShift_type) return 0;
1066 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1067 if (!LShift_singleton) return 0;
1068 RShift_type = make_type("RShift", operator_type, NULL, 0);
1069 if (!RShift_type) return 0;
1070 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1071 if (!RShift_singleton) return 0;
1072 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1073 if (!BitOr_type) return 0;
1074 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1075 if (!BitOr_singleton) return 0;
1076 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1077 if (!BitXor_type) return 0;
1078 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1079 if (!BitXor_singleton) return 0;
1080 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1081 if (!BitAnd_type) return 0;
1082 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1083 if (!BitAnd_singleton) return 0;
1084 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1085 if (!FloorDiv_type) return 0;
1086 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1087 if (!FloorDiv_singleton) return 0;
1088 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1089 if (!unaryop_type) return 0;
1090 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1091 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1092 if (!Invert_type) return 0;
1093 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1094 if (!Invert_singleton) return 0;
1095 Not_type = make_type("Not", unaryop_type, NULL, 0);
1096 if (!Not_type) return 0;
1097 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1098 if (!Not_singleton) return 0;
1099 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1100 if (!UAdd_type) return 0;
1101 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1102 if (!UAdd_singleton) return 0;
1103 USub_type = make_type("USub", unaryop_type, NULL, 0);
1104 if (!USub_type) return 0;
1105 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1106 if (!USub_singleton) return 0;
1107 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1108 if (!cmpop_type) return 0;
1109 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1110 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1111 if (!Eq_type) return 0;
1112 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1113 if (!Eq_singleton) return 0;
1114 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1115 if (!NotEq_type) return 0;
1116 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1117 if (!NotEq_singleton) return 0;
1118 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1119 if (!Lt_type) return 0;
1120 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1121 if (!Lt_singleton) return 0;
1122 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1123 if (!LtE_type) return 0;
1124 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1125 if (!LtE_singleton) return 0;
1126 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1127 if (!Gt_type) return 0;
1128 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1129 if (!Gt_singleton) return 0;
1130 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1131 if (!GtE_type) return 0;
1132 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1133 if (!GtE_singleton) return 0;
1134 Is_type = make_type("Is", cmpop_type, NULL, 0);
1135 if (!Is_type) return 0;
1136 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1137 if (!Is_singleton) return 0;
1138 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1139 if (!IsNot_type) return 0;
1140 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1141 if (!IsNot_singleton) return 0;
1142 In_type = make_type("In", cmpop_type, NULL, 0);
1143 if (!In_type) return 0;
1144 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1145 if (!In_singleton) return 0;
1146 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1147 if (!NotIn_type) return 0;
1148 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1149 if (!NotIn_singleton) return 0;
1150 comprehension_type = make_type("comprehension", &AST_type,
1151 comprehension_fields, 3);
1152 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001153 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1155 if (!excepthandler_type) return 0;
1156 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1157 return 0;
1158 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1159 ExceptHandler_fields, 3);
1160 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001161 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001162 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001163 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001164 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1165 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001166 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001167 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1168 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001169 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001170 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1171 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001172 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001173 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1174 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001175 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001176 initialized = 1;
1177 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001178}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001179
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001180static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1181static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1182static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1183static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1184 arena);
1185static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1186static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1187static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1188static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1189static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1190static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1191 arena);
1192static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1193 arena);
1194static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1195static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1196static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1197static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001198static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001201Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001203 mod_ty p;
1204 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1205 if (!p)
1206 return NULL;
1207 p->kind = Module_kind;
1208 p->v.Module.body = body;
1209 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210}
1211
1212mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001215 mod_ty p;
1216 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1217 if (!p)
1218 return NULL;
1219 p->kind = Interactive_kind;
1220 p->v.Interactive.body = body;
1221 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
1224mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001225Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001227 mod_ty p;
1228 if (!body) {
1229 PyErr_SetString(PyExc_ValueError,
1230 "field body is required for Expression");
1231 return NULL;
1232 }
1233 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1234 if (!p)
1235 return NULL;
1236 p->kind = Expression_kind;
1237 p->v.Expression.body = body;
1238 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239}
1240
1241mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001242Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001244 mod_ty p;
1245 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1246 if (!p)
1247 return NULL;
1248 p->kind = Suite_kind;
1249 p->v.Suite.body = body;
1250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253stmt_ty
1254FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001255 decorator_list, expr_ty returns, int lineno, int col_offset,
1256 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001258 stmt_ty p;
1259 if (!name) {
1260 PyErr_SetString(PyExc_ValueError,
1261 "field name is required for FunctionDef");
1262 return NULL;
1263 }
1264 if (!args) {
1265 PyErr_SetString(PyExc_ValueError,
1266 "field args is required for FunctionDef");
1267 return NULL;
1268 }
1269 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1270 if (!p)
1271 return NULL;
1272 p->kind = FunctionDef_kind;
1273 p->v.FunctionDef.name = name;
1274 p->v.FunctionDef.args = args;
1275 p->v.FunctionDef.body = body;
1276 p->v.FunctionDef.decorator_list = decorator_list;
1277 p->v.FunctionDef.returns = returns;
1278 p->lineno = lineno;
1279 p->col_offset = col_offset;
1280 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281}
1282
1283stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001284AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1285 * decorator_list, expr_ty returns, int lineno, int col_offset,
1286 PyArena *arena)
1287{
1288 stmt_ty p;
1289 if (!name) {
1290 PyErr_SetString(PyExc_ValueError,
1291 "field name is required for AsyncFunctionDef");
1292 return NULL;
1293 }
1294 if (!args) {
1295 PyErr_SetString(PyExc_ValueError,
1296 "field args is required for AsyncFunctionDef");
1297 return NULL;
1298 }
1299 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1300 if (!p)
1301 return NULL;
1302 p->kind = AsyncFunctionDef_kind;
1303 p->v.AsyncFunctionDef.name = name;
1304 p->v.AsyncFunctionDef.args = args;
1305 p->v.AsyncFunctionDef.body = body;
1306 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1307 p->v.AsyncFunctionDef.returns = returns;
1308 p->lineno = lineno;
1309 p->col_offset = col_offset;
1310 return p;
1311}
1312
1313stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001314ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1315 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1316 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001318 stmt_ty p;
1319 if (!name) {
1320 PyErr_SetString(PyExc_ValueError,
1321 "field name is required for ClassDef");
1322 return NULL;
1323 }
1324 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1325 if (!p)
1326 return NULL;
1327 p->kind = ClassDef_kind;
1328 p->v.ClassDef.name = name;
1329 p->v.ClassDef.bases = bases;
1330 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001331 p->v.ClassDef.body = body;
1332 p->v.ClassDef.decorator_list = decorator_list;
1333 p->lineno = lineno;
1334 p->col_offset = col_offset;
1335 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336}
1337
1338stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001339Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001341 stmt_ty p;
1342 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1343 if (!p)
1344 return NULL;
1345 p->kind = Return_kind;
1346 p->v.Return.value = value;
1347 p->lineno = lineno;
1348 p->col_offset = col_offset;
1349 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350}
1351
1352stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001353Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001355 stmt_ty p;
1356 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1357 if (!p)
1358 return NULL;
1359 p->kind = Delete_kind;
1360 p->v.Delete.targets = targets;
1361 p->lineno = lineno;
1362 p->col_offset = col_offset;
1363 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364}
1365
1366stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001367Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1368 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001370 stmt_ty p;
1371 if (!value) {
1372 PyErr_SetString(PyExc_ValueError,
1373 "field value is required for Assign");
1374 return NULL;
1375 }
1376 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1377 if (!p)
1378 return NULL;
1379 p->kind = Assign_kind;
1380 p->v.Assign.targets = targets;
1381 p->v.Assign.value = value;
1382 p->lineno = lineno;
1383 p->col_offset = col_offset;
1384 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385}
1386
1387stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001388AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1389 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001391 stmt_ty p;
1392 if (!target) {
1393 PyErr_SetString(PyExc_ValueError,
1394 "field target is required for AugAssign");
1395 return NULL;
1396 }
1397 if (!op) {
1398 PyErr_SetString(PyExc_ValueError,
1399 "field op is required for AugAssign");
1400 return NULL;
1401 }
1402 if (!value) {
1403 PyErr_SetString(PyExc_ValueError,
1404 "field value is required for AugAssign");
1405 return NULL;
1406 }
1407 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1408 if (!p)
1409 return NULL;
1410 p->kind = AugAssign_kind;
1411 p->v.AugAssign.target = target;
1412 p->v.AugAssign.op = op;
1413 p->v.AugAssign.value = value;
1414 p->lineno = lineno;
1415 p->col_offset = col_offset;
1416 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417}
1418
1419stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001420AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1421 lineno, int col_offset, PyArena *arena)
1422{
1423 stmt_ty p;
1424 if (!target) {
1425 PyErr_SetString(PyExc_ValueError,
1426 "field target is required for AnnAssign");
1427 return NULL;
1428 }
1429 if (!annotation) {
1430 PyErr_SetString(PyExc_ValueError,
1431 "field annotation is required for AnnAssign");
1432 return NULL;
1433 }
1434 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1435 if (!p)
1436 return NULL;
1437 p->kind = AnnAssign_kind;
1438 p->v.AnnAssign.target = target;
1439 p->v.AnnAssign.annotation = annotation;
1440 p->v.AnnAssign.value = value;
1441 p->v.AnnAssign.simple = simple;
1442 p->lineno = lineno;
1443 p->col_offset = col_offset;
1444 return p;
1445}
1446
1447stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001449 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001451 stmt_ty p;
1452 if (!target) {
1453 PyErr_SetString(PyExc_ValueError,
1454 "field target is required for For");
1455 return NULL;
1456 }
1457 if (!iter) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "field iter is required for For");
1460 return NULL;
1461 }
1462 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1463 if (!p)
1464 return NULL;
1465 p->kind = For_kind;
1466 p->v.For.target = target;
1467 p->v.For.iter = iter;
1468 p->v.For.body = body;
1469 p->v.For.orelse = orelse;
1470 p->lineno = lineno;
1471 p->col_offset = col_offset;
1472 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473}
1474
1475stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001476AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1477 lineno, int col_offset, PyArena *arena)
1478{
1479 stmt_ty p;
1480 if (!target) {
1481 PyErr_SetString(PyExc_ValueError,
1482 "field target is required for AsyncFor");
1483 return NULL;
1484 }
1485 if (!iter) {
1486 PyErr_SetString(PyExc_ValueError,
1487 "field iter is required for AsyncFor");
1488 return NULL;
1489 }
1490 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1491 if (!p)
1492 return NULL;
1493 p->kind = AsyncFor_kind;
1494 p->v.AsyncFor.target = target;
1495 p->v.AsyncFor.iter = iter;
1496 p->v.AsyncFor.body = body;
1497 p->v.AsyncFor.orelse = orelse;
1498 p->lineno = lineno;
1499 p->col_offset = col_offset;
1500 return p;
1501}
1502
1503stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001504While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1505 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001507 stmt_ty p;
1508 if (!test) {
1509 PyErr_SetString(PyExc_ValueError,
1510 "field test is required for While");
1511 return NULL;
1512 }
1513 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1514 if (!p)
1515 return NULL;
1516 p->kind = While_kind;
1517 p->v.While.test = test;
1518 p->v.While.body = body;
1519 p->v.While.orelse = orelse;
1520 p->lineno = lineno;
1521 p->col_offset = col_offset;
1522 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523}
1524
1525stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001526If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1527 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001529 stmt_ty p;
1530 if (!test) {
1531 PyErr_SetString(PyExc_ValueError,
1532 "field test is required for If");
1533 return NULL;
1534 }
1535 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1536 if (!p)
1537 return NULL;
1538 p->kind = If_kind;
1539 p->v.If.test = test;
1540 p->v.If.body = body;
1541 p->v.If.orelse = orelse;
1542 p->lineno = lineno;
1543 p->col_offset = col_offset;
1544 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
1547stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001548With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1549 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001550{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001551 stmt_ty p;
1552 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1553 if (!p)
1554 return NULL;
1555 p->kind = With_kind;
1556 p->v.With.items = items;
1557 p->v.With.body = body;
1558 p->lineno = lineno;
1559 p->col_offset = col_offset;
1560 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001561}
1562
1563stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001564AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1565 PyArena *arena)
1566{
1567 stmt_ty p;
1568 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1569 if (!p)
1570 return NULL;
1571 p->kind = AsyncWith_kind;
1572 p->v.AsyncWith.items = items;
1573 p->v.AsyncWith.body = body;
1574 p->lineno = lineno;
1575 p->col_offset = col_offset;
1576 return p;
1577}
1578
1579stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001580Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001582 stmt_ty p;
1583 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1584 if (!p)
1585 return NULL;
1586 p->kind = Raise_kind;
1587 p->v.Raise.exc = exc;
1588 p->v.Raise.cause = cause;
1589 p->lineno = lineno;
1590 p->col_offset = col_offset;
1591 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592}
1593
1594stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001595Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1596 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001598 stmt_ty p;
1599 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1600 if (!p)
1601 return NULL;
1602 p->kind = Try_kind;
1603 p->v.Try.body = body;
1604 p->v.Try.handlers = handlers;
1605 p->v.Try.orelse = orelse;
1606 p->v.Try.finalbody = finalbody;
1607 p->lineno = lineno;
1608 p->col_offset = col_offset;
1609 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610}
1611
1612stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001613Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001615 stmt_ty p;
1616 if (!test) {
1617 PyErr_SetString(PyExc_ValueError,
1618 "field test is required for Assert");
1619 return NULL;
1620 }
1621 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1622 if (!p)
1623 return NULL;
1624 p->kind = Assert_kind;
1625 p->v.Assert.test = test;
1626 p->v.Assert.msg = msg;
1627 p->lineno = lineno;
1628 p->col_offset = col_offset;
1629 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630}
1631
1632stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001633Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001635 stmt_ty p;
1636 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1637 if (!p)
1638 return NULL;
1639 p->kind = Import_kind;
1640 p->v.Import.names = names;
1641 p->lineno = lineno;
1642 p->col_offset = col_offset;
1643 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644}
1645
1646stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001647ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1648 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001650 stmt_ty p;
1651 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1652 if (!p)
1653 return NULL;
1654 p->kind = ImportFrom_kind;
1655 p->v.ImportFrom.module = module;
1656 p->v.ImportFrom.names = names;
1657 p->v.ImportFrom.level = level;
1658 p->lineno = lineno;
1659 p->col_offset = col_offset;
1660 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
1662
1663stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001664Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001666 stmt_ty p;
1667 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1668 if (!p)
1669 return NULL;
1670 p->kind = Global_kind;
1671 p->v.Global.names = names;
1672 p->lineno = lineno;
1673 p->col_offset = col_offset;
1674 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675}
1676
1677stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001678Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1679{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001680 stmt_ty p;
1681 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1682 if (!p)
1683 return NULL;
1684 p->kind = Nonlocal_kind;
1685 p->v.Nonlocal.names = names;
1686 p->lineno = lineno;
1687 p->col_offset = col_offset;
1688 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001689}
1690
1691stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001692Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001694 stmt_ty p;
1695 if (!value) {
1696 PyErr_SetString(PyExc_ValueError,
1697 "field value is required for Expr");
1698 return NULL;
1699 }
1700 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1701 if (!p)
1702 return NULL;
1703 p->kind = Expr_kind;
1704 p->v.Expr.value = value;
1705 p->lineno = lineno;
1706 p->col_offset = col_offset;
1707 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708}
1709
1710stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001711Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001713 stmt_ty p;
1714 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1715 if (!p)
1716 return NULL;
1717 p->kind = Pass_kind;
1718 p->lineno = lineno;
1719 p->col_offset = col_offset;
1720 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721}
1722
1723stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001724Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001726 stmt_ty p;
1727 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1728 if (!p)
1729 return NULL;
1730 p->kind = Break_kind;
1731 p->lineno = lineno;
1732 p->col_offset = col_offset;
1733 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734}
1735
1736stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001737Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001739 stmt_ty p;
1740 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1741 if (!p)
1742 return NULL;
1743 p->kind = Continue_kind;
1744 p->lineno = lineno;
1745 p->col_offset = col_offset;
1746 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747}
1748
1749expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001750BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1751 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001753 expr_ty p;
1754 if (!op) {
1755 PyErr_SetString(PyExc_ValueError,
1756 "field op is required for BoolOp");
1757 return NULL;
1758 }
1759 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1760 if (!p)
1761 return NULL;
1762 p->kind = BoolOp_kind;
1763 p->v.BoolOp.op = op;
1764 p->v.BoolOp.values = values;
1765 p->lineno = lineno;
1766 p->col_offset = col_offset;
1767 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768}
1769
1770expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001771BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1772 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001774 expr_ty p;
1775 if (!left) {
1776 PyErr_SetString(PyExc_ValueError,
1777 "field left is required for BinOp");
1778 return NULL;
1779 }
1780 if (!op) {
1781 PyErr_SetString(PyExc_ValueError,
1782 "field op is required for BinOp");
1783 return NULL;
1784 }
1785 if (!right) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field right is required for BinOp");
1788 return NULL;
1789 }
1790 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1791 if (!p)
1792 return NULL;
1793 p->kind = BinOp_kind;
1794 p->v.BinOp.left = left;
1795 p->v.BinOp.op = op;
1796 p->v.BinOp.right = right;
1797 p->lineno = lineno;
1798 p->col_offset = col_offset;
1799 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800}
1801
1802expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001803UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1804 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001806 expr_ty p;
1807 if (!op) {
1808 PyErr_SetString(PyExc_ValueError,
1809 "field op is required for UnaryOp");
1810 return NULL;
1811 }
1812 if (!operand) {
1813 PyErr_SetString(PyExc_ValueError,
1814 "field operand is required for UnaryOp");
1815 return NULL;
1816 }
1817 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1818 if (!p)
1819 return NULL;
1820 p->kind = UnaryOp_kind;
1821 p->v.UnaryOp.op = op;
1822 p->v.UnaryOp.operand = operand;
1823 p->lineno = lineno;
1824 p->col_offset = col_offset;
1825 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826}
1827
1828expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001829Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1830 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001832 expr_ty p;
1833 if (!args) {
1834 PyErr_SetString(PyExc_ValueError,
1835 "field args is required for Lambda");
1836 return NULL;
1837 }
1838 if (!body) {
1839 PyErr_SetString(PyExc_ValueError,
1840 "field body is required for Lambda");
1841 return NULL;
1842 }
1843 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1844 if (!p)
1845 return NULL;
1846 p->kind = Lambda_kind;
1847 p->v.Lambda.args = args;
1848 p->v.Lambda.body = body;
1849 p->lineno = lineno;
1850 p->col_offset = col_offset;
1851 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852}
1853
1854expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001855IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1856 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001857{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001858 expr_ty p;
1859 if (!test) {
1860 PyErr_SetString(PyExc_ValueError,
1861 "field test is required for IfExp");
1862 return NULL;
1863 }
1864 if (!body) {
1865 PyErr_SetString(PyExc_ValueError,
1866 "field body is required for IfExp");
1867 return NULL;
1868 }
1869 if (!orelse) {
1870 PyErr_SetString(PyExc_ValueError,
1871 "field orelse is required for IfExp");
1872 return NULL;
1873 }
1874 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1875 if (!p)
1876 return NULL;
1877 p->kind = IfExp_kind;
1878 p->v.IfExp.test = test;
1879 p->v.IfExp.body = body;
1880 p->v.IfExp.orelse = orelse;
1881 p->lineno = lineno;
1882 p->col_offset = col_offset;
1883 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001884}
1885
1886expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001887Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1888 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001890 expr_ty p;
1891 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1892 if (!p)
1893 return NULL;
1894 p->kind = Dict_kind;
1895 p->v.Dict.keys = keys;
1896 p->v.Dict.values = values;
1897 p->lineno = lineno;
1898 p->col_offset = col_offset;
1899 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900}
1901
1902expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001903Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001905 expr_ty p;
1906 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1907 if (!p)
1908 return NULL;
1909 p->kind = Set_kind;
1910 p->v.Set.elts = elts;
1911 p->lineno = lineno;
1912 p->col_offset = col_offset;
1913 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001914}
1915
1916expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001917ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1918 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001920 expr_ty p;
1921 if (!elt) {
1922 PyErr_SetString(PyExc_ValueError,
1923 "field elt is required for ListComp");
1924 return NULL;
1925 }
1926 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1927 if (!p)
1928 return NULL;
1929 p->kind = ListComp_kind;
1930 p->v.ListComp.elt = elt;
1931 p->v.ListComp.generators = generators;
1932 p->lineno = lineno;
1933 p->col_offset = col_offset;
1934 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935}
1936
1937expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001938SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1939 *arena)
1940{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001941 expr_ty p;
1942 if (!elt) {
1943 PyErr_SetString(PyExc_ValueError,
1944 "field elt is required for SetComp");
1945 return NULL;
1946 }
1947 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1948 if (!p)
1949 return NULL;
1950 p->kind = SetComp_kind;
1951 p->v.SetComp.elt = elt;
1952 p->v.SetComp.generators = generators;
1953 p->lineno = lineno;
1954 p->col_offset = col_offset;
1955 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956}
1957
1958expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001959DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1960 col_offset, PyArena *arena)
1961{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001962 expr_ty p;
1963 if (!key) {
1964 PyErr_SetString(PyExc_ValueError,
1965 "field key is required for DictComp");
1966 return NULL;
1967 }
1968 if (!value) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field value is required for DictComp");
1971 return NULL;
1972 }
1973 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1974 if (!p)
1975 return NULL;
1976 p->kind = DictComp_kind;
1977 p->v.DictComp.key = key;
1978 p->v.DictComp.value = value;
1979 p->v.DictComp.generators = generators;
1980 p->lineno = lineno;
1981 p->col_offset = col_offset;
1982 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001983}
1984
1985expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001986GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1987 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001989 expr_ty p;
1990 if (!elt) {
1991 PyErr_SetString(PyExc_ValueError,
1992 "field elt is required for GeneratorExp");
1993 return NULL;
1994 }
1995 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1996 if (!p)
1997 return NULL;
1998 p->kind = GeneratorExp_kind;
1999 p->v.GeneratorExp.elt = elt;
2000 p->v.GeneratorExp.generators = generators;
2001 p->lineno = lineno;
2002 p->col_offset = col_offset;
2003 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004}
2005
2006expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002007Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2008{
2009 expr_ty p;
2010 if (!value) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "field value is required for Await");
2013 return NULL;
2014 }
2015 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2016 if (!p)
2017 return NULL;
2018 p->kind = Await_kind;
2019 p->v.Await.value = value;
2020 p->lineno = lineno;
2021 p->col_offset = col_offset;
2022 return p;
2023}
2024
2025expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002026Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002028 expr_ty p;
2029 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2030 if (!p)
2031 return NULL;
2032 p->kind = Yield_kind;
2033 p->v.Yield.value = value;
2034 p->lineno = lineno;
2035 p->col_offset = col_offset;
2036 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037}
2038
2039expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002040YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2041{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002042 expr_ty p;
2043 if (!value) {
2044 PyErr_SetString(PyExc_ValueError,
2045 "field value is required for YieldFrom");
2046 return NULL;
2047 }
2048 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2049 if (!p)
2050 return NULL;
2051 p->kind = YieldFrom_kind;
2052 p->v.YieldFrom.value = value;
2053 p->lineno = lineno;
2054 p->col_offset = col_offset;
2055 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002056}
2057
2058expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002059Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2060 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002062 expr_ty p;
2063 if (!left) {
2064 PyErr_SetString(PyExc_ValueError,
2065 "field left is required for Compare");
2066 return NULL;
2067 }
2068 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2069 if (!p)
2070 return NULL;
2071 p->kind = Compare_kind;
2072 p->v.Compare.left = left;
2073 p->v.Compare.ops = ops;
2074 p->v.Compare.comparators = comparators;
2075 p->lineno = lineno;
2076 p->col_offset = col_offset;
2077 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078}
2079
2080expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002081Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2082 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002084 expr_ty p;
2085 if (!func) {
2086 PyErr_SetString(PyExc_ValueError,
2087 "field func is required for Call");
2088 return NULL;
2089 }
2090 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2091 if (!p)
2092 return NULL;
2093 p->kind = Call_kind;
2094 p->v.Call.func = func;
2095 p->v.Call.args = args;
2096 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002097 p->lineno = lineno;
2098 p->col_offset = col_offset;
2099 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100}
2101
2102expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002103Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002105 expr_ty p;
2106 if (!n) {
2107 PyErr_SetString(PyExc_ValueError,
2108 "field n is required for Num");
2109 return NULL;
2110 }
2111 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2112 if (!p)
2113 return NULL;
2114 p->kind = Num_kind;
2115 p->v.Num.n = n;
2116 p->lineno = lineno;
2117 p->col_offset = col_offset;
2118 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119}
2120
2121expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002122Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002124 expr_ty p;
2125 if (!s) {
2126 PyErr_SetString(PyExc_ValueError,
2127 "field s is required for Str");
2128 return NULL;
2129 }
2130 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2131 if (!p)
2132 return NULL;
2133 p->kind = Str_kind;
2134 p->v.Str.s = s;
2135 p->lineno = lineno;
2136 p->col_offset = col_offset;
2137 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138}
2139
2140expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002141FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2142 int col_offset, PyArena *arena)
2143{
2144 expr_ty p;
2145 if (!value) {
2146 PyErr_SetString(PyExc_ValueError,
2147 "field value is required for FormattedValue");
2148 return NULL;
2149 }
2150 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2151 if (!p)
2152 return NULL;
2153 p->kind = FormattedValue_kind;
2154 p->v.FormattedValue.value = value;
2155 p->v.FormattedValue.conversion = conversion;
2156 p->v.FormattedValue.format_spec = format_spec;
2157 p->lineno = lineno;
2158 p->col_offset = col_offset;
2159 return p;
2160}
2161
2162expr_ty
2163JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2164{
2165 expr_ty p;
2166 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2167 if (!p)
2168 return NULL;
2169 p->kind = JoinedStr_kind;
2170 p->v.JoinedStr.values = values;
2171 p->lineno = lineno;
2172 p->col_offset = col_offset;
2173 return p;
2174}
2175
2176expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002177Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002178{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002179 expr_ty p;
2180 if (!s) {
2181 PyErr_SetString(PyExc_ValueError,
2182 "field s is required for Bytes");
2183 return NULL;
2184 }
2185 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2186 if (!p)
2187 return NULL;
2188 p->kind = Bytes_kind;
2189 p->v.Bytes.s = s;
2190 p->lineno = lineno;
2191 p->col_offset = col_offset;
2192 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002193}
2194
2195expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002196NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2197{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002198 expr_ty p;
2199 if (!value) {
2200 PyErr_SetString(PyExc_ValueError,
2201 "field value is required for NameConstant");
2202 return NULL;
2203 }
2204 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2205 if (!p)
2206 return NULL;
2207 p->kind = NameConstant_kind;
2208 p->v.NameConstant.value = value;
2209 p->lineno = lineno;
2210 p->col_offset = col_offset;
2211 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002212}
2213
2214expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002215Ellipsis(int lineno, int col_offset, PyArena *arena)
2216{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002217 expr_ty p;
2218 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2219 if (!p)
2220 return NULL;
2221 p->kind = Ellipsis_kind;
2222 p->lineno = lineno;
2223 p->col_offset = col_offset;
2224 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002225}
2226
2227expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002228Constant(constant value, int lineno, int col_offset, PyArena *arena)
2229{
2230 expr_ty p;
2231 if (!value) {
2232 PyErr_SetString(PyExc_ValueError,
2233 "field value is required for Constant");
2234 return NULL;
2235 }
2236 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2237 if (!p)
2238 return NULL;
2239 p->kind = Constant_kind;
2240 p->v.Constant.value = value;
2241 p->lineno = lineno;
2242 p->col_offset = col_offset;
2243 return p;
2244}
2245
2246expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002247Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2248 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002250 expr_ty p;
2251 if (!value) {
2252 PyErr_SetString(PyExc_ValueError,
2253 "field value is required for Attribute");
2254 return NULL;
2255 }
2256 if (!attr) {
2257 PyErr_SetString(PyExc_ValueError,
2258 "field attr is required for Attribute");
2259 return NULL;
2260 }
2261 if (!ctx) {
2262 PyErr_SetString(PyExc_ValueError,
2263 "field ctx is required for Attribute");
2264 return NULL;
2265 }
2266 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2267 if (!p)
2268 return NULL;
2269 p->kind = Attribute_kind;
2270 p->v.Attribute.value = value;
2271 p->v.Attribute.attr = attr;
2272 p->v.Attribute.ctx = ctx;
2273 p->lineno = lineno;
2274 p->col_offset = col_offset;
2275 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276}
2277
2278expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002279Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2280 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002282 expr_ty p;
2283 if (!value) {
2284 PyErr_SetString(PyExc_ValueError,
2285 "field value is required for Subscript");
2286 return NULL;
2287 }
2288 if (!slice) {
2289 PyErr_SetString(PyExc_ValueError,
2290 "field slice is required for Subscript");
2291 return NULL;
2292 }
2293 if (!ctx) {
2294 PyErr_SetString(PyExc_ValueError,
2295 "field ctx is required for Subscript");
2296 return NULL;
2297 }
2298 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2299 if (!p)
2300 return NULL;
2301 p->kind = Subscript_kind;
2302 p->v.Subscript.value = value;
2303 p->v.Subscript.slice = slice;
2304 p->v.Subscript.ctx = ctx;
2305 p->lineno = lineno;
2306 p->col_offset = col_offset;
2307 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308}
2309
2310expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002311Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2312 *arena)
2313{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002314 expr_ty p;
2315 if (!value) {
2316 PyErr_SetString(PyExc_ValueError,
2317 "field value is required for Starred");
2318 return NULL;
2319 }
2320 if (!ctx) {
2321 PyErr_SetString(PyExc_ValueError,
2322 "field ctx is required for Starred");
2323 return NULL;
2324 }
2325 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2326 if (!p)
2327 return NULL;
2328 p->kind = Starred_kind;
2329 p->v.Starred.value = value;
2330 p->v.Starred.ctx = ctx;
2331 p->lineno = lineno;
2332 p->col_offset = col_offset;
2333 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002334}
2335
2336expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002337Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2338 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002340 expr_ty p;
2341 if (!id) {
2342 PyErr_SetString(PyExc_ValueError,
2343 "field id is required for Name");
2344 return NULL;
2345 }
2346 if (!ctx) {
2347 PyErr_SetString(PyExc_ValueError,
2348 "field ctx is required for Name");
2349 return NULL;
2350 }
2351 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2352 if (!p)
2353 return NULL;
2354 p->kind = Name_kind;
2355 p->v.Name.id = id;
2356 p->v.Name.ctx = ctx;
2357 p->lineno = lineno;
2358 p->col_offset = col_offset;
2359 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360}
2361
2362expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002363List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2364 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002366 expr_ty p;
2367 if (!ctx) {
2368 PyErr_SetString(PyExc_ValueError,
2369 "field ctx is required for List");
2370 return NULL;
2371 }
2372 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2373 if (!p)
2374 return NULL;
2375 p->kind = List_kind;
2376 p->v.List.elts = elts;
2377 p->v.List.ctx = ctx;
2378 p->lineno = lineno;
2379 p->col_offset = col_offset;
2380 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381}
2382
2383expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002384Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2385 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002387 expr_ty p;
2388 if (!ctx) {
2389 PyErr_SetString(PyExc_ValueError,
2390 "field ctx is required for Tuple");
2391 return NULL;
2392 }
2393 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2394 if (!p)
2395 return NULL;
2396 p->kind = Tuple_kind;
2397 p->v.Tuple.elts = elts;
2398 p->v.Tuple.ctx = ctx;
2399 p->lineno = lineno;
2400 p->col_offset = col_offset;
2401 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402}
2403
2404slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002405Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002407 slice_ty p;
2408 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2409 if (!p)
2410 return NULL;
2411 p->kind = Slice_kind;
2412 p->v.Slice.lower = lower;
2413 p->v.Slice.upper = upper;
2414 p->v.Slice.step = step;
2415 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416}
2417
2418slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002419ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002421 slice_ty p;
2422 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2423 if (!p)
2424 return NULL;
2425 p->kind = ExtSlice_kind;
2426 p->v.ExtSlice.dims = dims;
2427 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002431Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002433 slice_ty p;
2434 if (!value) {
2435 PyErr_SetString(PyExc_ValueError,
2436 "field value is required for Index");
2437 return NULL;
2438 }
2439 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2440 if (!p)
2441 return NULL;
2442 p->kind = Index_kind;
2443 p->v.Index.value = value;
2444 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445}
2446
2447comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002448comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002450 comprehension_ty p;
2451 if (!target) {
2452 PyErr_SetString(PyExc_ValueError,
2453 "field target is required for comprehension");
2454 return NULL;
2455 }
2456 if (!iter) {
2457 PyErr_SetString(PyExc_ValueError,
2458 "field iter is required for comprehension");
2459 return NULL;
2460 }
2461 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2462 if (!p)
2463 return NULL;
2464 p->target = target;
2465 p->iter = iter;
2466 p->ifs = ifs;
2467 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468}
2469
2470excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002471ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002472 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002474 excepthandler_ty p;
2475 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2476 if (!p)
2477 return NULL;
2478 p->kind = ExceptHandler_kind;
2479 p->v.ExceptHandler.type = type;
2480 p->v.ExceptHandler.name = name;
2481 p->v.ExceptHandler.body = body;
2482 p->lineno = lineno;
2483 p->col_offset = col_offset;
2484 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485}
2486
2487arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002488arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2489 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002491 arguments_ty p;
2492 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2493 if (!p)
2494 return NULL;
2495 p->args = args;
2496 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002497 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002498 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002499 p->kwarg = kwarg;
2500 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502}
2503
Neal Norwitzc1505362006-12-28 06:47:50 +00002504arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002505arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2506 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002507{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002508 arg_ty p;
2509 if (!arg) {
2510 PyErr_SetString(PyExc_ValueError,
2511 "field arg is required for arg");
2512 return NULL;
2513 }
2514 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2515 if (!p)
2516 return NULL;
2517 p->arg = arg;
2518 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002519 p->lineno = lineno;
2520 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002521 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002522}
2523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002525keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002527 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002528 if (!value) {
2529 PyErr_SetString(PyExc_ValueError,
2530 "field value is required for keyword");
2531 return NULL;
2532 }
2533 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2534 if (!p)
2535 return NULL;
2536 p->arg = arg;
2537 p->value = value;
2538 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539}
2540
2541alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002542alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002544 alias_ty p;
2545 if (!name) {
2546 PyErr_SetString(PyExc_ValueError,
2547 "field name is required for alias");
2548 return NULL;
2549 }
2550 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2551 if (!p)
2552 return NULL;
2553 p->name = name;
2554 p->asname = asname;
2555 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002558withitem_ty
2559withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2560{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002561 withitem_ty p;
2562 if (!context_expr) {
2563 PyErr_SetString(PyExc_ValueError,
2564 "field context_expr is required for withitem");
2565 return NULL;
2566 }
2567 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2568 if (!p)
2569 return NULL;
2570 p->context_expr = context_expr;
2571 p->optional_vars = optional_vars;
2572 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002573}
2574
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002575
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002576PyObject*
2577ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002578{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002579 mod_ty o = (mod_ty)_o;
2580 PyObject *result = NULL, *value = NULL;
2581 if (!o) {
2582 Py_INCREF(Py_None);
2583 return Py_None;
2584 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002585
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002586 switch (o->kind) {
2587 case Module_kind:
2588 result = PyType_GenericNew(Module_type, NULL, NULL);
2589 if (!result) goto failed;
2590 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2591 if (!value) goto failed;
2592 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2593 goto failed;
2594 Py_DECREF(value);
2595 break;
2596 case Interactive_kind:
2597 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2598 if (!result) goto failed;
2599 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2600 if (!value) goto failed;
2601 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
2604 break;
2605 case Expression_kind:
2606 result = PyType_GenericNew(Expression_type, NULL, NULL);
2607 if (!result) goto failed;
2608 value = ast2obj_expr(o->v.Expression.body);
2609 if (!value) goto failed;
2610 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 break;
2614 case Suite_kind:
2615 result = PyType_GenericNew(Suite_type, NULL, NULL);
2616 if (!result) goto failed;
2617 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2618 if (!value) goto failed;
2619 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 break;
2623 }
2624 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002625failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002626 Py_XDECREF(value);
2627 Py_XDECREF(result);
2628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629}
2630
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002631PyObject*
2632ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002634 stmt_ty o = (stmt_ty)_o;
2635 PyObject *result = NULL, *value = NULL;
2636 if (!o) {
2637 Py_INCREF(Py_None);
2638 return Py_None;
2639 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002640
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002641 switch (o->kind) {
2642 case FunctionDef_kind:
2643 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2644 if (!result) goto failed;
2645 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002646 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002647 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2648 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002649 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002650 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002651 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002652 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2653 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002654 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002655 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2656 if (!value) goto failed;
2657 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
2660 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2661 if (!value) goto failed;
2662 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2663 goto failed;
2664 Py_DECREF(value);
2665 value = ast2obj_expr(o->v.FunctionDef.returns);
2666 if (!value) goto failed;
2667 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
2670 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002671 case AsyncFunctionDef_kind:
2672 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2673 if (!result) goto failed;
2674 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2675 if (!value) goto failed;
2676 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2677 goto failed;
2678 Py_DECREF(value);
2679 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2680 if (!value) goto failed;
2681 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
2684 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2685 if (!value) goto failed;
2686 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2687 goto failed;
2688 Py_DECREF(value);
2689 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2690 ast2obj_expr);
2691 if (!value) goto failed;
2692 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2696 if (!value) goto failed;
2697 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002701 case ClassDef_kind:
2702 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2703 if (!result) goto failed;
2704 value = ast2obj_identifier(o->v.ClassDef.name);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2715 if (!value) goto failed;
2716 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002719 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2720 if (!value) goto failed;
2721 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2725 if (!value) goto failed;
2726 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2727 goto failed;
2728 Py_DECREF(value);
2729 break;
2730 case Return_kind:
2731 result = PyType_GenericNew(Return_type, NULL, NULL);
2732 if (!result) goto failed;
2733 value = ast2obj_expr(o->v.Return.value);
2734 if (!value) goto failed;
2735 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2736 goto failed;
2737 Py_DECREF(value);
2738 break;
2739 case Delete_kind:
2740 result = PyType_GenericNew(Delete_type, NULL, NULL);
2741 if (!result) goto failed;
2742 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2743 if (!value) goto failed;
2744 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2745 goto failed;
2746 Py_DECREF(value);
2747 break;
2748 case Assign_kind:
2749 result = PyType_GenericNew(Assign_type, NULL, NULL);
2750 if (!result) goto failed;
2751 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2752 if (!value) goto failed;
2753 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 value = ast2obj_expr(o->v.Assign.value);
2757 if (!value) goto failed;
2758 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
2761 break;
2762 case AugAssign_kind:
2763 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2764 if (!result) goto failed;
2765 value = ast2obj_expr(o->v.AugAssign.target);
2766 if (!value) goto failed;
2767 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
2770 value = ast2obj_operator(o->v.AugAssign.op);
2771 if (!value) goto failed;
2772 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 value = ast2obj_expr(o->v.AugAssign.value);
2776 if (!value) goto failed;
2777 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2778 goto failed;
2779 Py_DECREF(value);
2780 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002781 case AnnAssign_kind:
2782 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2783 if (!result) goto failed;
2784 value = ast2obj_expr(o->v.AnnAssign.target);
2785 if (!value) goto failed;
2786 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
2789 value = ast2obj_expr(o->v.AnnAssign.annotation);
2790 if (!value) goto failed;
2791 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2792 goto failed;
2793 Py_DECREF(value);
2794 value = ast2obj_expr(o->v.AnnAssign.value);
2795 if (!value) goto failed;
2796 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2797 goto failed;
2798 Py_DECREF(value);
2799 value = ast2obj_int(o->v.AnnAssign.simple);
2800 if (!value) goto failed;
2801 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2802 goto failed;
2803 Py_DECREF(value);
2804 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002805 case For_kind:
2806 result = PyType_GenericNew(For_type, NULL, NULL);
2807 if (!result) goto failed;
2808 value = ast2obj_expr(o->v.For.target);
2809 if (!value) goto failed;
2810 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2811 goto failed;
2812 Py_DECREF(value);
2813 value = ast2obj_expr(o->v.For.iter);
2814 if (!value) goto failed;
2815 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2816 goto failed;
2817 Py_DECREF(value);
2818 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2819 if (!value) goto failed;
2820 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2821 goto failed;
2822 Py_DECREF(value);
2823 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2824 if (!value) goto failed;
2825 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2826 goto failed;
2827 Py_DECREF(value);
2828 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002829 case AsyncFor_kind:
2830 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_expr(o->v.AsyncFor.target);
2833 if (!value) goto failed;
2834 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_expr(o->v.AsyncFor.iter);
2838 if (!value) goto failed;
2839 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2843 if (!value) goto failed;
2844 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2848 if (!value) goto failed;
2849 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
2852 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002853 case While_kind:
2854 result = PyType_GenericNew(While_type, NULL, NULL);
2855 if (!result) goto failed;
2856 value = ast2obj_expr(o->v.While.test);
2857 if (!value) goto failed;
2858 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2862 if (!value) goto failed;
2863 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2867 if (!value) goto failed;
2868 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 break;
2872 case If_kind:
2873 result = PyType_GenericNew(If_type, NULL, NULL);
2874 if (!result) goto failed;
2875 value = ast2obj_expr(o->v.If.test);
2876 if (!value) goto failed;
2877 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2881 if (!value) goto failed;
2882 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 break;
2891 case With_kind:
2892 result = PyType_GenericNew(With_type, NULL, NULL);
2893 if (!result) goto failed;
2894 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2895 if (!value) goto failed;
2896 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
2899 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2900 if (!value) goto failed;
2901 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2902 goto failed;
2903 Py_DECREF(value);
2904 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002905 case AsyncWith_kind:
2906 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2907 if (!result) goto failed;
2908 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2909 if (!value) goto failed;
2910 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2911 goto failed;
2912 Py_DECREF(value);
2913 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2914 if (!value) goto failed;
2915 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2916 goto failed;
2917 Py_DECREF(value);
2918 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002919 case Raise_kind:
2920 result = PyType_GenericNew(Raise_type, NULL, NULL);
2921 if (!result) goto failed;
2922 value = ast2obj_expr(o->v.Raise.exc);
2923 if (!value) goto failed;
2924 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2925 goto failed;
2926 Py_DECREF(value);
2927 value = ast2obj_expr(o->v.Raise.cause);
2928 if (!value) goto failed;
2929 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2930 goto failed;
2931 Py_DECREF(value);
2932 break;
2933 case Try_kind:
2934 result = PyType_GenericNew(Try_type, NULL, NULL);
2935 if (!result) goto failed;
2936 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2937 if (!value) goto failed;
2938 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2939 goto failed;
2940 Py_DECREF(value);
2941 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2942 if (!value) goto failed;
2943 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2944 goto failed;
2945 Py_DECREF(value);
2946 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2947 if (!value) goto failed;
2948 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2949 goto failed;
2950 Py_DECREF(value);
2951 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2952 if (!value) goto failed;
2953 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2954 goto failed;
2955 Py_DECREF(value);
2956 break;
2957 case Assert_kind:
2958 result = PyType_GenericNew(Assert_type, NULL, NULL);
2959 if (!result) goto failed;
2960 value = ast2obj_expr(o->v.Assert.test);
2961 if (!value) goto failed;
2962 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
2965 value = ast2obj_expr(o->v.Assert.msg);
2966 if (!value) goto failed;
2967 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2968 goto failed;
2969 Py_DECREF(value);
2970 break;
2971 case Import_kind:
2972 result = PyType_GenericNew(Import_type, NULL, NULL);
2973 if (!result) goto failed;
2974 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2975 if (!value) goto failed;
2976 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2977 goto failed;
2978 Py_DECREF(value);
2979 break;
2980 case ImportFrom_kind:
2981 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2982 if (!result) goto failed;
2983 value = ast2obj_identifier(o->v.ImportFrom.module);
2984 if (!value) goto failed;
2985 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2986 goto failed;
2987 Py_DECREF(value);
2988 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2989 if (!value) goto failed;
2990 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2991 goto failed;
2992 Py_DECREF(value);
2993 value = ast2obj_int(o->v.ImportFrom.level);
2994 if (!value) goto failed;
2995 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2996 goto failed;
2997 Py_DECREF(value);
2998 break;
2999 case Global_kind:
3000 result = PyType_GenericNew(Global_type, NULL, NULL);
3001 if (!result) goto failed;
3002 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3003 if (!value) goto failed;
3004 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
3007 break;
3008 case Nonlocal_kind:
3009 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3010 if (!result) goto failed;
3011 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3012 if (!value) goto failed;
3013 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3014 goto failed;
3015 Py_DECREF(value);
3016 break;
3017 case Expr_kind:
3018 result = PyType_GenericNew(Expr_type, NULL, NULL);
3019 if (!result) goto failed;
3020 value = ast2obj_expr(o->v.Expr.value);
3021 if (!value) goto failed;
3022 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3023 goto failed;
3024 Py_DECREF(value);
3025 break;
3026 case Pass_kind:
3027 result = PyType_GenericNew(Pass_type, NULL, NULL);
3028 if (!result) goto failed;
3029 break;
3030 case Break_kind:
3031 result = PyType_GenericNew(Break_type, NULL, NULL);
3032 if (!result) goto failed;
3033 break;
3034 case Continue_kind:
3035 result = PyType_GenericNew(Continue_type, NULL, NULL);
3036 if (!result) goto failed;
3037 break;
3038 }
3039 value = ast2obj_int(o->lineno);
3040 if (!value) goto failed;
3041 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3042 goto failed;
3043 Py_DECREF(value);
3044 value = ast2obj_int(o->col_offset);
3045 if (!value) goto failed;
3046 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3047 goto failed;
3048 Py_DECREF(value);
3049 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003050failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003051 Py_XDECREF(value);
3052 Py_XDECREF(result);
3053 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054}
3055
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003056PyObject*
3057ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003059 expr_ty o = (expr_ty)_o;
3060 PyObject *result = NULL, *value = NULL;
3061 if (!o) {
3062 Py_INCREF(Py_None);
3063 return Py_None;
3064 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003065
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003066 switch (o->kind) {
3067 case BoolOp_kind:
3068 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3069 if (!result) goto failed;
3070 value = ast2obj_boolop(o->v.BoolOp.op);
3071 if (!value) goto failed;
3072 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3073 goto failed;
3074 Py_DECREF(value);
3075 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3076 if (!value) goto failed;
3077 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3078 goto failed;
3079 Py_DECREF(value);
3080 break;
3081 case BinOp_kind:
3082 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3083 if (!result) goto failed;
3084 value = ast2obj_expr(o->v.BinOp.left);
3085 if (!value) goto failed;
3086 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3087 goto failed;
3088 Py_DECREF(value);
3089 value = ast2obj_operator(o->v.BinOp.op);
3090 if (!value) goto failed;
3091 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3092 goto failed;
3093 Py_DECREF(value);
3094 value = ast2obj_expr(o->v.BinOp.right);
3095 if (!value) goto failed;
3096 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3097 goto failed;
3098 Py_DECREF(value);
3099 break;
3100 case UnaryOp_kind:
3101 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3102 if (!result) goto failed;
3103 value = ast2obj_unaryop(o->v.UnaryOp.op);
3104 if (!value) goto failed;
3105 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3106 goto failed;
3107 Py_DECREF(value);
3108 value = ast2obj_expr(o->v.UnaryOp.operand);
3109 if (!value) goto failed;
3110 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3111 goto failed;
3112 Py_DECREF(value);
3113 break;
3114 case Lambda_kind:
3115 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3116 if (!result) goto failed;
3117 value = ast2obj_arguments(o->v.Lambda.args);
3118 if (!value) goto failed;
3119 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3120 goto failed;
3121 Py_DECREF(value);
3122 value = ast2obj_expr(o->v.Lambda.body);
3123 if (!value) goto failed;
3124 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3125 goto failed;
3126 Py_DECREF(value);
3127 break;
3128 case IfExp_kind:
3129 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3130 if (!result) goto failed;
3131 value = ast2obj_expr(o->v.IfExp.test);
3132 if (!value) goto failed;
3133 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3134 goto failed;
3135 Py_DECREF(value);
3136 value = ast2obj_expr(o->v.IfExp.body);
3137 if (!value) goto failed;
3138 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3139 goto failed;
3140 Py_DECREF(value);
3141 value = ast2obj_expr(o->v.IfExp.orelse);
3142 if (!value) goto failed;
3143 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3144 goto failed;
3145 Py_DECREF(value);
3146 break;
3147 case Dict_kind:
3148 result = PyType_GenericNew(Dict_type, NULL, NULL);
3149 if (!result) goto failed;
3150 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3151 if (!value) goto failed;
3152 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3153 goto failed;
3154 Py_DECREF(value);
3155 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3156 if (!value) goto failed;
3157 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3158 goto failed;
3159 Py_DECREF(value);
3160 break;
3161 case Set_kind:
3162 result = PyType_GenericNew(Set_type, NULL, NULL);
3163 if (!result) goto failed;
3164 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3165 if (!value) goto failed;
3166 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
3169 break;
3170 case ListComp_kind:
3171 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3172 if (!result) goto failed;
3173 value = ast2obj_expr(o->v.ListComp.elt);
3174 if (!value) goto failed;
3175 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3176 goto failed;
3177 Py_DECREF(value);
3178 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3179 if (!value) goto failed;
3180 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3181 goto failed;
3182 Py_DECREF(value);
3183 break;
3184 case SetComp_kind:
3185 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3186 if (!result) goto failed;
3187 value = ast2obj_expr(o->v.SetComp.elt);
3188 if (!value) goto failed;
3189 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3190 goto failed;
3191 Py_DECREF(value);
3192 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3193 if (!value) goto failed;
3194 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3195 goto failed;
3196 Py_DECREF(value);
3197 break;
3198 case DictComp_kind:
3199 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3200 if (!result) goto failed;
3201 value = ast2obj_expr(o->v.DictComp.key);
3202 if (!value) goto failed;
3203 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3204 goto failed;
3205 Py_DECREF(value);
3206 value = ast2obj_expr(o->v.DictComp.value);
3207 if (!value) goto failed;
3208 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3209 goto failed;
3210 Py_DECREF(value);
3211 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3212 if (!value) goto failed;
3213 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 break;
3217 case GeneratorExp_kind:
3218 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3219 if (!result) goto failed;
3220 value = ast2obj_expr(o->v.GeneratorExp.elt);
3221 if (!value) goto failed;
3222 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3223 goto failed;
3224 Py_DECREF(value);
3225 value = ast2obj_list(o->v.GeneratorExp.generators,
3226 ast2obj_comprehension);
3227 if (!value) goto failed;
3228 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3229 goto failed;
3230 Py_DECREF(value);
3231 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003232 case Await_kind:
3233 result = PyType_GenericNew(Await_type, NULL, NULL);
3234 if (!result) goto failed;
3235 value = ast2obj_expr(o->v.Await.value);
3236 if (!value) goto failed;
3237 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
3240 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003241 case Yield_kind:
3242 result = PyType_GenericNew(Yield_type, NULL, NULL);
3243 if (!result) goto failed;
3244 value = ast2obj_expr(o->v.Yield.value);
3245 if (!value) goto failed;
3246 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3247 goto failed;
3248 Py_DECREF(value);
3249 break;
3250 case YieldFrom_kind:
3251 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3252 if (!result) goto failed;
3253 value = ast2obj_expr(o->v.YieldFrom.value);
3254 if (!value) goto failed;
3255 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3256 goto failed;
3257 Py_DECREF(value);
3258 break;
3259 case Compare_kind:
3260 result = PyType_GenericNew(Compare_type, NULL, NULL);
3261 if (!result) goto failed;
3262 value = ast2obj_expr(o->v.Compare.left);
3263 if (!value) goto failed;
3264 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3265 goto failed;
3266 Py_DECREF(value);
3267 {
3268 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3269 value = PyList_New(n);
3270 if (!value) goto failed;
3271 for(i = 0; i < n; i++)
3272 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003274 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003275 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3276 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003277 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003278 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003279 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003280 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3281 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003282 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003283 break;
3284 case Call_kind:
3285 result = PyType_GenericNew(Call_type, NULL, NULL);
3286 if (!result) goto failed;
3287 value = ast2obj_expr(o->v.Call.func);
3288 if (!value) goto failed;
3289 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3290 goto failed;
3291 Py_DECREF(value);
3292 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3293 if (!value) goto failed;
3294 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3295 goto failed;
3296 Py_DECREF(value);
3297 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3298 if (!value) goto failed;
3299 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3300 goto failed;
3301 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003302 break;
3303 case Num_kind:
3304 result = PyType_GenericNew(Num_type, NULL, NULL);
3305 if (!result) goto failed;
3306 value = ast2obj_object(o->v.Num.n);
3307 if (!value) goto failed;
3308 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3309 goto failed;
3310 Py_DECREF(value);
3311 break;
3312 case Str_kind:
3313 result = PyType_GenericNew(Str_type, NULL, NULL);
3314 if (!result) goto failed;
3315 value = ast2obj_string(o->v.Str.s);
3316 if (!value) goto failed;
3317 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3318 goto failed;
3319 Py_DECREF(value);
3320 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003321 case FormattedValue_kind:
3322 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3323 if (!result) goto failed;
3324 value = ast2obj_expr(o->v.FormattedValue.value);
3325 if (!value) goto failed;
3326 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3327 goto failed;
3328 Py_DECREF(value);
3329 value = ast2obj_int(o->v.FormattedValue.conversion);
3330 if (!value) goto failed;
3331 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3332 goto failed;
3333 Py_DECREF(value);
3334 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3335 if (!value) goto failed;
3336 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3337 goto failed;
3338 Py_DECREF(value);
3339 break;
3340 case JoinedStr_kind:
3341 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3342 if (!result) goto failed;
3343 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3344 if (!value) goto failed;
3345 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3346 goto failed;
3347 Py_DECREF(value);
3348 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003349 case Bytes_kind:
3350 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3351 if (!result) goto failed;
3352 value = ast2obj_bytes(o->v.Bytes.s);
3353 if (!value) goto failed;
3354 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3355 goto failed;
3356 Py_DECREF(value);
3357 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003358 case NameConstant_kind:
3359 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3360 if (!result) goto failed;
3361 value = ast2obj_singleton(o->v.NameConstant.value);
3362 if (!value) goto failed;
3363 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3364 goto failed;
3365 Py_DECREF(value);
3366 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003367 case Ellipsis_kind:
3368 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3369 if (!result) goto failed;
3370 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003371 case Constant_kind:
3372 result = PyType_GenericNew(Constant_type, NULL, NULL);
3373 if (!result) goto failed;
3374 value = ast2obj_constant(o->v.Constant.value);
3375 if (!value) goto failed;
3376 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3377 goto failed;
3378 Py_DECREF(value);
3379 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003380 case Attribute_kind:
3381 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3382 if (!result) goto failed;
3383 value = ast2obj_expr(o->v.Attribute.value);
3384 if (!value) goto failed;
3385 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3386 goto failed;
3387 Py_DECREF(value);
3388 value = ast2obj_identifier(o->v.Attribute.attr);
3389 if (!value) goto failed;
3390 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3391 goto failed;
3392 Py_DECREF(value);
3393 value = ast2obj_expr_context(o->v.Attribute.ctx);
3394 if (!value) goto failed;
3395 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3396 goto failed;
3397 Py_DECREF(value);
3398 break;
3399 case Subscript_kind:
3400 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3401 if (!result) goto failed;
3402 value = ast2obj_expr(o->v.Subscript.value);
3403 if (!value) goto failed;
3404 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3405 goto failed;
3406 Py_DECREF(value);
3407 value = ast2obj_slice(o->v.Subscript.slice);
3408 if (!value) goto failed;
3409 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3410 goto failed;
3411 Py_DECREF(value);
3412 value = ast2obj_expr_context(o->v.Subscript.ctx);
3413 if (!value) goto failed;
3414 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3415 goto failed;
3416 Py_DECREF(value);
3417 break;
3418 case Starred_kind:
3419 result = PyType_GenericNew(Starred_type, NULL, NULL);
3420 if (!result) goto failed;
3421 value = ast2obj_expr(o->v.Starred.value);
3422 if (!value) goto failed;
3423 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3424 goto failed;
3425 Py_DECREF(value);
3426 value = ast2obj_expr_context(o->v.Starred.ctx);
3427 if (!value) goto failed;
3428 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3429 goto failed;
3430 Py_DECREF(value);
3431 break;
3432 case Name_kind:
3433 result = PyType_GenericNew(Name_type, NULL, NULL);
3434 if (!result) goto failed;
3435 value = ast2obj_identifier(o->v.Name.id);
3436 if (!value) goto failed;
3437 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3438 goto failed;
3439 Py_DECREF(value);
3440 value = ast2obj_expr_context(o->v.Name.ctx);
3441 if (!value) goto failed;
3442 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3443 goto failed;
3444 Py_DECREF(value);
3445 break;
3446 case List_kind:
3447 result = PyType_GenericNew(List_type, NULL, NULL);
3448 if (!result) goto failed;
3449 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3450 if (!value) goto failed;
3451 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3452 goto failed;
3453 Py_DECREF(value);
3454 value = ast2obj_expr_context(o->v.List.ctx);
3455 if (!value) goto failed;
3456 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3457 goto failed;
3458 Py_DECREF(value);
3459 break;
3460 case Tuple_kind:
3461 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3462 if (!result) goto failed;
3463 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3464 if (!value) goto failed;
3465 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3466 goto failed;
3467 Py_DECREF(value);
3468 value = ast2obj_expr_context(o->v.Tuple.ctx);
3469 if (!value) goto failed;
3470 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3471 goto failed;
3472 Py_DECREF(value);
3473 break;
3474 }
3475 value = ast2obj_int(o->lineno);
3476 if (!value) goto failed;
3477 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3478 goto failed;
3479 Py_DECREF(value);
3480 value = ast2obj_int(o->col_offset);
3481 if (!value) goto failed;
3482 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3483 goto failed;
3484 Py_DECREF(value);
3485 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003486failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003487 Py_XDECREF(value);
3488 Py_XDECREF(result);
3489 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490}
3491
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003492PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003494 switch(o) {
3495 case Load:
3496 Py_INCREF(Load_singleton);
3497 return Load_singleton;
3498 case Store:
3499 Py_INCREF(Store_singleton);
3500 return Store_singleton;
3501 case Del:
3502 Py_INCREF(Del_singleton);
3503 return Del_singleton;
3504 case AugLoad:
3505 Py_INCREF(AugLoad_singleton);
3506 return AugLoad_singleton;
3507 case AugStore:
3508 Py_INCREF(AugStore_singleton);
3509 return AugStore_singleton;
3510 case Param:
3511 Py_INCREF(Param_singleton);
3512 return Param_singleton;
3513 default:
3514 /* should never happen, but just in case ... */
3515 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3516 return NULL;
3517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003519PyObject*
3520ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003522 slice_ty o = (slice_ty)_o;
3523 PyObject *result = NULL, *value = NULL;
3524 if (!o) {
3525 Py_INCREF(Py_None);
3526 return Py_None;
3527 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003528
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003529 switch (o->kind) {
3530 case Slice_kind:
3531 result = PyType_GenericNew(Slice_type, NULL, NULL);
3532 if (!result) goto failed;
3533 value = ast2obj_expr(o->v.Slice.lower);
3534 if (!value) goto failed;
3535 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3536 goto failed;
3537 Py_DECREF(value);
3538 value = ast2obj_expr(o->v.Slice.upper);
3539 if (!value) goto failed;
3540 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3541 goto failed;
3542 Py_DECREF(value);
3543 value = ast2obj_expr(o->v.Slice.step);
3544 if (!value) goto failed;
3545 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3546 goto failed;
3547 Py_DECREF(value);
3548 break;
3549 case ExtSlice_kind:
3550 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3551 if (!result) goto failed;
3552 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3553 if (!value) goto failed;
3554 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3555 goto failed;
3556 Py_DECREF(value);
3557 break;
3558 case Index_kind:
3559 result = PyType_GenericNew(Index_type, NULL, NULL);
3560 if (!result) goto failed;
3561 value = ast2obj_expr(o->v.Index.value);
3562 if (!value) goto failed;
3563 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3564 goto failed;
3565 Py_DECREF(value);
3566 break;
3567 }
3568 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003569failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003570 Py_XDECREF(value);
3571 Py_XDECREF(result);
3572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573}
3574
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003575PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003577 switch(o) {
3578 case And:
3579 Py_INCREF(And_singleton);
3580 return And_singleton;
3581 case Or:
3582 Py_INCREF(Or_singleton);
3583 return Or_singleton;
3584 default:
3585 /* should never happen, but just in case ... */
3586 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3587 return NULL;
3588 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003589}
3590PyObject* ast2obj_operator(operator_ty o)
3591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003592 switch(o) {
3593 case Add:
3594 Py_INCREF(Add_singleton);
3595 return Add_singleton;
3596 case Sub:
3597 Py_INCREF(Sub_singleton);
3598 return Sub_singleton;
3599 case Mult:
3600 Py_INCREF(Mult_singleton);
3601 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003602 case MatMult:
3603 Py_INCREF(MatMult_singleton);
3604 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003605 case Div:
3606 Py_INCREF(Div_singleton);
3607 return Div_singleton;
3608 case Mod:
3609 Py_INCREF(Mod_singleton);
3610 return Mod_singleton;
3611 case Pow:
3612 Py_INCREF(Pow_singleton);
3613 return Pow_singleton;
3614 case LShift:
3615 Py_INCREF(LShift_singleton);
3616 return LShift_singleton;
3617 case RShift:
3618 Py_INCREF(RShift_singleton);
3619 return RShift_singleton;
3620 case BitOr:
3621 Py_INCREF(BitOr_singleton);
3622 return BitOr_singleton;
3623 case BitXor:
3624 Py_INCREF(BitXor_singleton);
3625 return BitXor_singleton;
3626 case BitAnd:
3627 Py_INCREF(BitAnd_singleton);
3628 return BitAnd_singleton;
3629 case FloorDiv:
3630 Py_INCREF(FloorDiv_singleton);
3631 return FloorDiv_singleton;
3632 default:
3633 /* should never happen, but just in case ... */
3634 PyErr_Format(PyExc_SystemError, "unknown operator found");
3635 return NULL;
3636 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003637}
3638PyObject* ast2obj_unaryop(unaryop_ty o)
3639{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003640 switch(o) {
3641 case Invert:
3642 Py_INCREF(Invert_singleton);
3643 return Invert_singleton;
3644 case Not:
3645 Py_INCREF(Not_singleton);
3646 return Not_singleton;
3647 case UAdd:
3648 Py_INCREF(UAdd_singleton);
3649 return UAdd_singleton;
3650 case USub:
3651 Py_INCREF(USub_singleton);
3652 return USub_singleton;
3653 default:
3654 /* should never happen, but just in case ... */
3655 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3656 return NULL;
3657 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003658}
3659PyObject* ast2obj_cmpop(cmpop_ty o)
3660{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003661 switch(o) {
3662 case Eq:
3663 Py_INCREF(Eq_singleton);
3664 return Eq_singleton;
3665 case NotEq:
3666 Py_INCREF(NotEq_singleton);
3667 return NotEq_singleton;
3668 case Lt:
3669 Py_INCREF(Lt_singleton);
3670 return Lt_singleton;
3671 case LtE:
3672 Py_INCREF(LtE_singleton);
3673 return LtE_singleton;
3674 case Gt:
3675 Py_INCREF(Gt_singleton);
3676 return Gt_singleton;
3677 case GtE:
3678 Py_INCREF(GtE_singleton);
3679 return GtE_singleton;
3680 case Is:
3681 Py_INCREF(Is_singleton);
3682 return Is_singleton;
3683 case IsNot:
3684 Py_INCREF(IsNot_singleton);
3685 return IsNot_singleton;
3686 case In:
3687 Py_INCREF(In_singleton);
3688 return In_singleton;
3689 case NotIn:
3690 Py_INCREF(NotIn_singleton);
3691 return NotIn_singleton;
3692 default:
3693 /* should never happen, but just in case ... */
3694 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3695 return NULL;
3696 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003697}
3698PyObject*
3699ast2obj_comprehension(void* _o)
3700{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003701 comprehension_ty o = (comprehension_ty)_o;
3702 PyObject *result = NULL, *value = NULL;
3703 if (!o) {
3704 Py_INCREF(Py_None);
3705 return Py_None;
3706 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003707
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003708 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3709 if (!result) return NULL;
3710 value = ast2obj_expr(o->target);
3711 if (!value) goto failed;
3712 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3713 goto failed;
3714 Py_DECREF(value);
3715 value = ast2obj_expr(o->iter);
3716 if (!value) goto failed;
3717 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3718 goto failed;
3719 Py_DECREF(value);
3720 value = ast2obj_list(o->ifs, ast2obj_expr);
3721 if (!value) goto failed;
3722 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3723 goto failed;
3724 Py_DECREF(value);
3725 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003726failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003727 Py_XDECREF(value);
3728 Py_XDECREF(result);
3729 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730}
3731
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003732PyObject*
3733ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003735 excepthandler_ty o = (excepthandler_ty)_o;
3736 PyObject *result = NULL, *value = NULL;
3737 if (!o) {
3738 Py_INCREF(Py_None);
3739 return Py_None;
3740 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003741
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003742 switch (o->kind) {
3743 case ExceptHandler_kind:
3744 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3745 if (!result) goto failed;
3746 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003747 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003748 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3749 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003750 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003752 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003753 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3754 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003755 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003756 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3757 if (!value) goto failed;
3758 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3759 goto failed;
3760 Py_DECREF(value);
3761 break;
3762 }
3763 value = ast2obj_int(o->lineno);
3764 if (!value) goto failed;
3765 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3766 goto failed;
3767 Py_DECREF(value);
3768 value = ast2obj_int(o->col_offset);
3769 if (!value) goto failed;
3770 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3771 goto failed;
3772 Py_DECREF(value);
3773 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003774failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003775 Py_XDECREF(value);
3776 Py_XDECREF(result);
3777 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778}
3779
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003780PyObject*
3781ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003783 arguments_ty o = (arguments_ty)_o;
3784 PyObject *result = NULL, *value = NULL;
3785 if (!o) {
3786 Py_INCREF(Py_None);
3787 return Py_None;
3788 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003789
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003790 result = PyType_GenericNew(arguments_type, NULL, NULL);
3791 if (!result) return NULL;
3792 value = ast2obj_list(o->args, ast2obj_arg);
3793 if (!value) goto failed;
3794 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3795 goto failed;
3796 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003797 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003798 if (!value) goto failed;
3799 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3800 goto failed;
3801 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003802 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3803 if (!value) goto failed;
3804 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3805 goto failed;
3806 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003807 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003808 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003809 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003810 goto failed;
3811 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003812 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003813 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003814 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003815 goto failed;
3816 Py_DECREF(value);
3817 value = ast2obj_list(o->defaults, ast2obj_expr);
3818 if (!value) goto failed;
3819 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3820 goto failed;
3821 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003822 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003823failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003824 Py_XDECREF(value);
3825 Py_XDECREF(result);
3826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827}
3828
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003829PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003830ast2obj_arg(void* _o)
3831{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003832 arg_ty o = (arg_ty)_o;
3833 PyObject *result = NULL, *value = NULL;
3834 if (!o) {
3835 Py_INCREF(Py_None);
3836 return Py_None;
3837 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003838
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003839 result = PyType_GenericNew(arg_type, NULL, NULL);
3840 if (!result) return NULL;
3841 value = ast2obj_identifier(o->arg);
3842 if (!value) goto failed;
3843 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3844 goto failed;
3845 Py_DECREF(value);
3846 value = ast2obj_expr(o->annotation);
3847 if (!value) goto failed;
3848 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3849 goto failed;
3850 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003851 value = ast2obj_int(o->lineno);
3852 if (!value) goto failed;
3853 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3854 goto failed;
3855 Py_DECREF(value);
3856 value = ast2obj_int(o->col_offset);
3857 if (!value) goto failed;
3858 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3859 goto failed;
3860 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003861 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003862failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003863 Py_XDECREF(value);
3864 Py_XDECREF(result);
3865 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003866}
3867
3868PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003869ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003871 keyword_ty o = (keyword_ty)_o;
3872 PyObject *result = NULL, *value = NULL;
3873 if (!o) {
3874 Py_INCREF(Py_None);
3875 return Py_None;
3876 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003877
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 result = PyType_GenericNew(keyword_type, NULL, NULL);
3879 if (!result) return NULL;
3880 value = ast2obj_identifier(o->arg);
3881 if (!value) goto failed;
3882 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3883 goto failed;
3884 Py_DECREF(value);
3885 value = ast2obj_expr(o->value);
3886 if (!value) goto failed;
3887 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3888 goto failed;
3889 Py_DECREF(value);
3890 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003891failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003892 Py_XDECREF(value);
3893 Py_XDECREF(result);
3894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895}
3896
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003897PyObject*
3898ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003900 alias_ty o = (alias_ty)_o;
3901 PyObject *result = NULL, *value = NULL;
3902 if (!o) {
3903 Py_INCREF(Py_None);
3904 return Py_None;
3905 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003906
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003907 result = PyType_GenericNew(alias_type, NULL, NULL);
3908 if (!result) return NULL;
3909 value = ast2obj_identifier(o->name);
3910 if (!value) goto failed;
3911 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3912 goto failed;
3913 Py_DECREF(value);
3914 value = ast2obj_identifier(o->asname);
3915 if (!value) goto failed;
3916 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3917 goto failed;
3918 Py_DECREF(value);
3919 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003920failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003921 Py_XDECREF(value);
3922 Py_XDECREF(result);
3923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924}
3925
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003926PyObject*
3927ast2obj_withitem(void* _o)
3928{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003929 withitem_ty o = (withitem_ty)_o;
3930 PyObject *result = NULL, *value = NULL;
3931 if (!o) {
3932 Py_INCREF(Py_None);
3933 return Py_None;
3934 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003935
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003936 result = PyType_GenericNew(withitem_type, NULL, NULL);
3937 if (!result) return NULL;
3938 value = ast2obj_expr(o->context_expr);
3939 if (!value) goto failed;
3940 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3941 goto failed;
3942 Py_DECREF(value);
3943 value = ast2obj_expr(o->optional_vars);
3944 if (!value) goto failed;
3945 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3946 goto failed;
3947 Py_DECREF(value);
3948 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003949failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003950 Py_XDECREF(value);
3951 Py_XDECREF(result);
3952 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003953}
3954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003956int
3957obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3958{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003960
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003961 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003962
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003963 if (obj == Py_None) {
3964 *out = NULL;
3965 return 0;
3966 }
3967 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3968 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003969 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003970 }
3971 if (isinstance) {
3972 asdl_seq* body;
3973
3974 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3975 int res;
3976 Py_ssize_t len;
3977 Py_ssize_t i;
3978 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3979 if (tmp == NULL) goto failed;
3980 if (!PyList_Check(tmp)) {
3981 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3982 goto failed;
3983 }
3984 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003985 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003986 if (body == NULL) goto failed;
3987 for (i = 0; i < len; i++) {
3988 stmt_ty value;
3989 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3990 if (res != 0) goto failed;
3991 asdl_seq_SET(body, i, value);
3992 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003993 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 } else {
3995 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3996 return 1;
3997 }
3998 *out = Module(body, arena);
3999 if (*out == NULL) goto failed;
4000 return 0;
4001 }
4002 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4003 if (isinstance == -1) {
4004 return 1;
4005 }
4006 if (isinstance) {
4007 asdl_seq* body;
4008
4009 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4010 int res;
4011 Py_ssize_t len;
4012 Py_ssize_t i;
4013 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4014 if (tmp == NULL) goto failed;
4015 if (!PyList_Check(tmp)) {
4016 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4017 goto failed;
4018 }
4019 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004020 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004021 if (body == NULL) goto failed;
4022 for (i = 0; i < len; i++) {
4023 stmt_ty value;
4024 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4025 if (res != 0) goto failed;
4026 asdl_seq_SET(body, i, value);
4027 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004028 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004029 } else {
4030 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4031 return 1;
4032 }
4033 *out = Interactive(body, arena);
4034 if (*out == NULL) goto failed;
4035 return 0;
4036 }
4037 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4038 if (isinstance == -1) {
4039 return 1;
4040 }
4041 if (isinstance) {
4042 expr_ty body;
4043
4044 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4045 int res;
4046 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4047 if (tmp == NULL) goto failed;
4048 res = obj2ast_expr(tmp, &body, arena);
4049 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004050 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004051 } else {
4052 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4053 return 1;
4054 }
4055 *out = Expression(body, arena);
4056 if (*out == NULL) goto failed;
4057 return 0;
4058 }
4059 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4060 if (isinstance == -1) {
4061 return 1;
4062 }
4063 if (isinstance) {
4064 asdl_seq* body;
4065
4066 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4067 int res;
4068 Py_ssize_t len;
4069 Py_ssize_t i;
4070 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4071 if (tmp == NULL) goto failed;
4072 if (!PyList_Check(tmp)) {
4073 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4074 goto failed;
4075 }
4076 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004077 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004078 if (body == NULL) goto failed;
4079 for (i = 0; i < len; i++) {
4080 stmt_ty value;
4081 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4082 if (res != 0) goto failed;
4083 asdl_seq_SET(body, i, value);
4084 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004085 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004086 } else {
4087 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4088 return 1;
4089 }
4090 *out = Suite(body, arena);
4091 if (*out == NULL) goto failed;
4092 return 0;
4093 }
4094
4095 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4096 failed:
4097 Py_XDECREF(tmp);
4098 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004099}
4100
4101int
4102obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4103{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004104 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004105
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004106 PyObject *tmp = NULL;
4107 int lineno;
4108 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004109
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004110 if (obj == Py_None) {
4111 *out = NULL;
4112 return 0;
4113 }
4114 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4115 int res;
4116 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4117 if (tmp == NULL) goto failed;
4118 res = obj2ast_int(tmp, &lineno, arena);
4119 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004120 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004121 } else {
4122 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004123 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004124 }
4125 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4126 int res;
4127 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4128 if (tmp == NULL) goto failed;
4129 res = obj2ast_int(tmp, &col_offset, arena);
4130 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004131 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004132 } else {
4133 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4134 return 1;
4135 }
4136 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4137 if (isinstance == -1) {
4138 return 1;
4139 }
4140 if (isinstance) {
4141 identifier name;
4142 arguments_ty args;
4143 asdl_seq* body;
4144 asdl_seq* decorator_list;
4145 expr_ty returns;
4146
4147 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4148 int res;
4149 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4150 if (tmp == NULL) goto failed;
4151 res = obj2ast_identifier(tmp, &name, arena);
4152 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004153 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004154 } else {
4155 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4156 return 1;
4157 }
4158 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4159 int res;
4160 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4161 if (tmp == NULL) goto failed;
4162 res = obj2ast_arguments(tmp, &args, arena);
4163 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004164 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004165 } else {
4166 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4167 return 1;
4168 }
4169 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4170 int res;
4171 Py_ssize_t len;
4172 Py_ssize_t i;
4173 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4174 if (tmp == NULL) goto failed;
4175 if (!PyList_Check(tmp)) {
4176 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4177 goto failed;
4178 }
4179 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004180 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004181 if (body == NULL) goto failed;
4182 for (i = 0; i < len; i++) {
4183 stmt_ty value;
4184 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4185 if (res != 0) goto failed;
4186 asdl_seq_SET(body, i, value);
4187 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004188 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004189 } else {
4190 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4191 return 1;
4192 }
4193 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4194 int res;
4195 Py_ssize_t len;
4196 Py_ssize_t i;
4197 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4198 if (tmp == NULL) goto failed;
4199 if (!PyList_Check(tmp)) {
4200 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4201 goto failed;
4202 }
4203 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004204 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004205 if (decorator_list == NULL) goto failed;
4206 for (i = 0; i < len; i++) {
4207 expr_ty value;
4208 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4209 if (res != 0) goto failed;
4210 asdl_seq_SET(decorator_list, i, value);
4211 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004212 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004213 } else {
4214 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4215 return 1;
4216 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004217 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004218 int res;
4219 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4220 if (tmp == NULL) goto failed;
4221 res = obj2ast_expr(tmp, &returns, arena);
4222 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004224 } else {
4225 returns = NULL;
4226 }
4227 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4228 col_offset, arena);
4229 if (*out == NULL) goto failed;
4230 return 0;
4231 }
Yury Selivanov75445082015-05-11 22:57:16 -04004232 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4233 if (isinstance == -1) {
4234 return 1;
4235 }
4236 if (isinstance) {
4237 identifier name;
4238 arguments_ty args;
4239 asdl_seq* body;
4240 asdl_seq* decorator_list;
4241 expr_ty returns;
4242
4243 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4244 int res;
4245 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4246 if (tmp == NULL) goto failed;
4247 res = obj2ast_identifier(tmp, &name, arena);
4248 if (res != 0) goto failed;
4249 Py_CLEAR(tmp);
4250 } else {
4251 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4252 return 1;
4253 }
4254 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4255 int res;
4256 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4257 if (tmp == NULL) goto failed;
4258 res = obj2ast_arguments(tmp, &args, arena);
4259 if (res != 0) goto failed;
4260 Py_CLEAR(tmp);
4261 } else {
4262 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4263 return 1;
4264 }
4265 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4266 int res;
4267 Py_ssize_t len;
4268 Py_ssize_t i;
4269 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4270 if (tmp == NULL) goto failed;
4271 if (!PyList_Check(tmp)) {
4272 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4273 goto failed;
4274 }
4275 len = PyList_GET_SIZE(tmp);
4276 body = _Py_asdl_seq_new(len, arena);
4277 if (body == NULL) goto failed;
4278 for (i = 0; i < len; i++) {
4279 stmt_ty value;
4280 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4281 if (res != 0) goto failed;
4282 asdl_seq_SET(body, i, value);
4283 }
4284 Py_CLEAR(tmp);
4285 } else {
4286 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4287 return 1;
4288 }
4289 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4290 int res;
4291 Py_ssize_t len;
4292 Py_ssize_t i;
4293 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4294 if (tmp == NULL) goto failed;
4295 if (!PyList_Check(tmp)) {
4296 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4297 goto failed;
4298 }
4299 len = PyList_GET_SIZE(tmp);
4300 decorator_list = _Py_asdl_seq_new(len, arena);
4301 if (decorator_list == NULL) goto failed;
4302 for (i = 0; i < len; i++) {
4303 expr_ty value;
4304 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4305 if (res != 0) goto failed;
4306 asdl_seq_SET(decorator_list, i, value);
4307 }
4308 Py_CLEAR(tmp);
4309 } else {
4310 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4311 return 1;
4312 }
4313 if (exists_not_none(obj, &PyId_returns)) {
4314 int res;
4315 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4316 if (tmp == NULL) goto failed;
4317 res = obj2ast_expr(tmp, &returns, arena);
4318 if (res != 0) goto failed;
4319 Py_CLEAR(tmp);
4320 } else {
4321 returns = NULL;
4322 }
4323 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4324 lineno, col_offset, arena);
4325 if (*out == NULL) goto failed;
4326 return 0;
4327 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004328 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4329 if (isinstance == -1) {
4330 return 1;
4331 }
4332 if (isinstance) {
4333 identifier name;
4334 asdl_seq* bases;
4335 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004336 asdl_seq* body;
4337 asdl_seq* decorator_list;
4338
4339 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4340 int res;
4341 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4342 if (tmp == NULL) goto failed;
4343 res = obj2ast_identifier(tmp, &name, arena);
4344 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004345 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004346 } else {
4347 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4348 return 1;
4349 }
4350 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4351 int res;
4352 Py_ssize_t len;
4353 Py_ssize_t i;
4354 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4355 if (tmp == NULL) goto failed;
4356 if (!PyList_Check(tmp)) {
4357 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4358 goto failed;
4359 }
4360 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004361 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004362 if (bases == NULL) goto failed;
4363 for (i = 0; i < len; i++) {
4364 expr_ty value;
4365 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4366 if (res != 0) goto failed;
4367 asdl_seq_SET(bases, i, value);
4368 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004369 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004370 } else {
4371 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4372 return 1;
4373 }
4374 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4375 int res;
4376 Py_ssize_t len;
4377 Py_ssize_t i;
4378 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4379 if (tmp == NULL) goto failed;
4380 if (!PyList_Check(tmp)) {
4381 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4382 goto failed;
4383 }
4384 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004385 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004386 if (keywords == NULL) goto failed;
4387 for (i = 0; i < len; i++) {
4388 keyword_ty value;
4389 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4390 if (res != 0) goto failed;
4391 asdl_seq_SET(keywords, i, value);
4392 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004393 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004394 } else {
4395 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4396 return 1;
4397 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004398 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4399 int res;
4400 Py_ssize_t len;
4401 Py_ssize_t i;
4402 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4403 if (tmp == NULL) goto failed;
4404 if (!PyList_Check(tmp)) {
4405 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4406 goto failed;
4407 }
4408 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004409 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004410 if (body == NULL) goto failed;
4411 for (i = 0; i < len; i++) {
4412 stmt_ty value;
4413 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4414 if (res != 0) goto failed;
4415 asdl_seq_SET(body, i, value);
4416 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004417 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004418 } else {
4419 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4420 return 1;
4421 }
4422 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4423 int res;
4424 Py_ssize_t len;
4425 Py_ssize_t i;
4426 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4427 if (tmp == NULL) goto failed;
4428 if (!PyList_Check(tmp)) {
4429 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4430 goto failed;
4431 }
4432 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004433 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004434 if (decorator_list == NULL) goto failed;
4435 for (i = 0; i < len; i++) {
4436 expr_ty value;
4437 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4438 if (res != 0) goto failed;
4439 asdl_seq_SET(decorator_list, i, value);
4440 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004441 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004442 } else {
4443 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4444 return 1;
4445 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004446 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4447 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004448 if (*out == NULL) goto failed;
4449 return 0;
4450 }
4451 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4452 if (isinstance == -1) {
4453 return 1;
4454 }
4455 if (isinstance) {
4456 expr_ty value;
4457
Victor Stinneree4b59c2013-07-27 00:01:35 +02004458 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004459 int res;
4460 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4461 if (tmp == NULL) goto failed;
4462 res = obj2ast_expr(tmp, &value, arena);
4463 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004464 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004465 } else {
4466 value = NULL;
4467 }
4468 *out = Return(value, lineno, col_offset, arena);
4469 if (*out == NULL) goto failed;
4470 return 0;
4471 }
4472 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4473 if (isinstance == -1) {
4474 return 1;
4475 }
4476 if (isinstance) {
4477 asdl_seq* targets;
4478
4479 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4480 int res;
4481 Py_ssize_t len;
4482 Py_ssize_t i;
4483 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4484 if (tmp == NULL) goto failed;
4485 if (!PyList_Check(tmp)) {
4486 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4487 goto failed;
4488 }
4489 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004490 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004491 if (targets == NULL) goto failed;
4492 for (i = 0; i < len; i++) {
4493 expr_ty value;
4494 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4495 if (res != 0) goto failed;
4496 asdl_seq_SET(targets, i, value);
4497 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004498 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004499 } else {
4500 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4501 return 1;
4502 }
4503 *out = Delete(targets, lineno, col_offset, arena);
4504 if (*out == NULL) goto failed;
4505 return 0;
4506 }
4507 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4508 if (isinstance == -1) {
4509 return 1;
4510 }
4511 if (isinstance) {
4512 asdl_seq* targets;
4513 expr_ty value;
4514
4515 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4516 int res;
4517 Py_ssize_t len;
4518 Py_ssize_t i;
4519 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4520 if (tmp == NULL) goto failed;
4521 if (!PyList_Check(tmp)) {
4522 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4523 goto failed;
4524 }
4525 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004526 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004527 if (targets == NULL) goto failed;
4528 for (i = 0; i < len; i++) {
4529 expr_ty value;
4530 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4531 if (res != 0) goto failed;
4532 asdl_seq_SET(targets, i, value);
4533 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004534 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004535 } else {
4536 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4537 return 1;
4538 }
4539 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4540 int res;
4541 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4542 if (tmp == NULL) goto failed;
4543 res = obj2ast_expr(tmp, &value, arena);
4544 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004545 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004546 } else {
4547 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4548 return 1;
4549 }
4550 *out = Assign(targets, value, lineno, col_offset, arena);
4551 if (*out == NULL) goto failed;
4552 return 0;
4553 }
4554 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4555 if (isinstance == -1) {
4556 return 1;
4557 }
4558 if (isinstance) {
4559 expr_ty target;
4560 operator_ty op;
4561 expr_ty value;
4562
4563 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4564 int res;
4565 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4566 if (tmp == NULL) goto failed;
4567 res = obj2ast_expr(tmp, &target, arena);
4568 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004569 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004570 } else {
4571 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4572 return 1;
4573 }
4574 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4575 int res;
4576 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4577 if (tmp == NULL) goto failed;
4578 res = obj2ast_operator(tmp, &op, arena);
4579 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004580 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004581 } else {
4582 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4583 return 1;
4584 }
4585 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4586 int res;
4587 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4588 if (tmp == NULL) goto failed;
4589 res = obj2ast_expr(tmp, &value, arena);
4590 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004591 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004592 } else {
4593 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4594 return 1;
4595 }
4596 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4597 if (*out == NULL) goto failed;
4598 return 0;
4599 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004600 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4601 if (isinstance == -1) {
4602 return 1;
4603 }
4604 if (isinstance) {
4605 expr_ty target;
4606 expr_ty annotation;
4607 expr_ty value;
4608 int simple;
4609
4610 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4611 int res;
4612 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4613 if (tmp == NULL) goto failed;
4614 res = obj2ast_expr(tmp, &target, arena);
4615 if (res != 0) goto failed;
4616 Py_CLEAR(tmp);
4617 } else {
4618 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4619 return 1;
4620 }
4621 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4622 int res;
4623 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4624 if (tmp == NULL) goto failed;
4625 res = obj2ast_expr(tmp, &annotation, arena);
4626 if (res != 0) goto failed;
4627 Py_CLEAR(tmp);
4628 } else {
4629 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4630 return 1;
4631 }
4632 if (exists_not_none(obj, &PyId_value)) {
4633 int res;
4634 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4635 if (tmp == NULL) goto failed;
4636 res = obj2ast_expr(tmp, &value, arena);
4637 if (res != 0) goto failed;
4638 Py_CLEAR(tmp);
4639 } else {
4640 value = NULL;
4641 }
4642 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4643 int res;
4644 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4645 if (tmp == NULL) goto failed;
4646 res = obj2ast_int(tmp, &simple, arena);
4647 if (res != 0) goto failed;
4648 Py_CLEAR(tmp);
4649 } else {
4650 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4651 return 1;
4652 }
4653 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4654 arena);
4655 if (*out == NULL) goto failed;
4656 return 0;
4657 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004658 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4659 if (isinstance == -1) {
4660 return 1;
4661 }
4662 if (isinstance) {
4663 expr_ty target;
4664 expr_ty iter;
4665 asdl_seq* body;
4666 asdl_seq* orelse;
4667
4668 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4669 int res;
4670 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4671 if (tmp == NULL) goto failed;
4672 res = obj2ast_expr(tmp, &target, arena);
4673 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004674 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004675 } else {
4676 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4677 return 1;
4678 }
4679 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4680 int res;
4681 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4682 if (tmp == NULL) goto failed;
4683 res = obj2ast_expr(tmp, &iter, arena);
4684 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004685 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004686 } else {
4687 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4688 return 1;
4689 }
4690 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4691 int res;
4692 Py_ssize_t len;
4693 Py_ssize_t i;
4694 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4695 if (tmp == NULL) goto failed;
4696 if (!PyList_Check(tmp)) {
4697 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4698 goto failed;
4699 }
4700 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004701 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004702 if (body == NULL) goto failed;
4703 for (i = 0; i < len; i++) {
4704 stmt_ty value;
4705 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4706 if (res != 0) goto failed;
4707 asdl_seq_SET(body, i, value);
4708 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4712 return 1;
4713 }
4714 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4715 int res;
4716 Py_ssize_t len;
4717 Py_ssize_t i;
4718 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4719 if (tmp == NULL) goto failed;
4720 if (!PyList_Check(tmp)) {
4721 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4722 goto failed;
4723 }
4724 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004725 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004726 if (orelse == NULL) goto failed;
4727 for (i = 0; i < len; i++) {
4728 stmt_ty value;
4729 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4730 if (res != 0) goto failed;
4731 asdl_seq_SET(orelse, i, value);
4732 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004733 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004734 } else {
4735 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4736 return 1;
4737 }
4738 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4739 if (*out == NULL) goto failed;
4740 return 0;
4741 }
Yury Selivanov75445082015-05-11 22:57:16 -04004742 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4743 if (isinstance == -1) {
4744 return 1;
4745 }
4746 if (isinstance) {
4747 expr_ty target;
4748 expr_ty iter;
4749 asdl_seq* body;
4750 asdl_seq* orelse;
4751
4752 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4753 int res;
4754 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4755 if (tmp == NULL) goto failed;
4756 res = obj2ast_expr(tmp, &target, arena);
4757 if (res != 0) goto failed;
4758 Py_CLEAR(tmp);
4759 } else {
4760 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4761 return 1;
4762 }
4763 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4764 int res;
4765 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4766 if (tmp == NULL) goto failed;
4767 res = obj2ast_expr(tmp, &iter, arena);
4768 if (res != 0) goto failed;
4769 Py_CLEAR(tmp);
4770 } else {
4771 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4772 return 1;
4773 }
4774 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4775 int res;
4776 Py_ssize_t len;
4777 Py_ssize_t i;
4778 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4779 if (tmp == NULL) goto failed;
4780 if (!PyList_Check(tmp)) {
4781 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4782 goto failed;
4783 }
4784 len = PyList_GET_SIZE(tmp);
4785 body = _Py_asdl_seq_new(len, arena);
4786 if (body == NULL) goto failed;
4787 for (i = 0; i < len; i++) {
4788 stmt_ty value;
4789 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4790 if (res != 0) goto failed;
4791 asdl_seq_SET(body, i, value);
4792 }
4793 Py_CLEAR(tmp);
4794 } else {
4795 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4796 return 1;
4797 }
4798 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4799 int res;
4800 Py_ssize_t len;
4801 Py_ssize_t i;
4802 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4803 if (tmp == NULL) goto failed;
4804 if (!PyList_Check(tmp)) {
4805 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4806 goto failed;
4807 }
4808 len = PyList_GET_SIZE(tmp);
4809 orelse = _Py_asdl_seq_new(len, arena);
4810 if (orelse == NULL) goto failed;
4811 for (i = 0; i < len; i++) {
4812 stmt_ty value;
4813 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4814 if (res != 0) goto failed;
4815 asdl_seq_SET(orelse, i, value);
4816 }
4817 Py_CLEAR(tmp);
4818 } else {
4819 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4820 return 1;
4821 }
4822 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4823 if (*out == NULL) goto failed;
4824 return 0;
4825 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004826 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4827 if (isinstance == -1) {
4828 return 1;
4829 }
4830 if (isinstance) {
4831 expr_ty test;
4832 asdl_seq* body;
4833 asdl_seq* orelse;
4834
4835 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4836 int res;
4837 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4838 if (tmp == NULL) goto failed;
4839 res = obj2ast_expr(tmp, &test, 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 \"test\" missing from While");
4844 return 1;
4845 }
4846 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4847 int res;
4848 Py_ssize_t len;
4849 Py_ssize_t i;
4850 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4851 if (tmp == NULL) goto failed;
4852 if (!PyList_Check(tmp)) {
4853 PyErr_Format(PyExc_TypeError, "While field \"body\" 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 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004858 if (body == NULL) goto failed;
4859 for (i = 0; i < len; i++) {
4860 stmt_ty value;
4861 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4862 if (res != 0) goto failed;
4863 asdl_seq_SET(body, 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 \"body\" missing from While");
4868 return 1;
4869 }
4870 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4871 int res;
4872 Py_ssize_t len;
4873 Py_ssize_t i;
4874 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4875 if (tmp == NULL) goto failed;
4876 if (!PyList_Check(tmp)) {
4877 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4878 goto failed;
4879 }
4880 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004881 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004882 if (orelse == NULL) goto failed;
4883 for (i = 0; i < len; i++) {
4884 stmt_ty value;
4885 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4886 if (res != 0) goto failed;
4887 asdl_seq_SET(orelse, i, value);
4888 }
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 \"orelse\" missing from While");
4892 return 1;
4893 }
4894 *out = While(test, body, orelse, lineno, col_offset, arena);
4895 if (*out == NULL) goto failed;
4896 return 0;
4897 }
4898 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4899 if (isinstance == -1) {
4900 return 1;
4901 }
4902 if (isinstance) {
4903 expr_ty test;
4904 asdl_seq* body;
4905 asdl_seq* orelse;
4906
4907 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4908 int res;
4909 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4910 if (tmp == NULL) goto failed;
4911 res = obj2ast_expr(tmp, &test, arena);
4912 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004913 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004914 } else {
4915 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4916 return 1;
4917 }
4918 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4919 int res;
4920 Py_ssize_t len;
4921 Py_ssize_t i;
4922 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4923 if (tmp == NULL) goto failed;
4924 if (!PyList_Check(tmp)) {
4925 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4926 goto failed;
4927 }
4928 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004929 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004930 if (body == NULL) goto failed;
4931 for (i = 0; i < len; i++) {
4932 stmt_ty value;
4933 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4934 if (res != 0) goto failed;
4935 asdl_seq_SET(body, i, value);
4936 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004937 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004938 } else {
4939 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4940 return 1;
4941 }
4942 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4943 int res;
4944 Py_ssize_t len;
4945 Py_ssize_t i;
4946 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4947 if (tmp == NULL) goto failed;
4948 if (!PyList_Check(tmp)) {
4949 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4950 goto failed;
4951 }
4952 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004953 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004954 if (orelse == NULL) goto failed;
4955 for (i = 0; i < len; i++) {
4956 stmt_ty value;
4957 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4958 if (res != 0) goto failed;
4959 asdl_seq_SET(orelse, i, value);
4960 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004961 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004962 } else {
4963 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4964 return 1;
4965 }
4966 *out = If(test, body, orelse, lineno, col_offset, arena);
4967 if (*out == NULL) goto failed;
4968 return 0;
4969 }
4970 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4971 if (isinstance == -1) {
4972 return 1;
4973 }
4974 if (isinstance) {
4975 asdl_seq* items;
4976 asdl_seq* body;
4977
4978 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4979 int res;
4980 Py_ssize_t len;
4981 Py_ssize_t i;
4982 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4983 if (tmp == NULL) goto failed;
4984 if (!PyList_Check(tmp)) {
4985 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4986 goto failed;
4987 }
4988 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004989 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004990 if (items == NULL) goto failed;
4991 for (i = 0; i < len; i++) {
4992 withitem_ty value;
4993 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4994 if (res != 0) goto failed;
4995 asdl_seq_SET(items, i, value);
4996 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004997 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004998 } else {
4999 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5000 return 1;
5001 }
5002 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5003 int res;
5004 Py_ssize_t len;
5005 Py_ssize_t i;
5006 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5007 if (tmp == NULL) goto failed;
5008 if (!PyList_Check(tmp)) {
5009 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5010 goto failed;
5011 }
5012 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005013 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005014 if (body == NULL) goto failed;
5015 for (i = 0; i < len; i++) {
5016 stmt_ty value;
5017 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5018 if (res != 0) goto failed;
5019 asdl_seq_SET(body, i, value);
5020 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005021 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005022 } else {
5023 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5024 return 1;
5025 }
5026 *out = With(items, body, lineno, col_offset, arena);
5027 if (*out == NULL) goto failed;
5028 return 0;
5029 }
Yury Selivanov75445082015-05-11 22:57:16 -04005030 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5031 if (isinstance == -1) {
5032 return 1;
5033 }
5034 if (isinstance) {
5035 asdl_seq* items;
5036 asdl_seq* body;
5037
5038 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5039 int res;
5040 Py_ssize_t len;
5041 Py_ssize_t i;
5042 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5043 if (tmp == NULL) goto failed;
5044 if (!PyList_Check(tmp)) {
5045 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5046 goto failed;
5047 }
5048 len = PyList_GET_SIZE(tmp);
5049 items = _Py_asdl_seq_new(len, arena);
5050 if (items == NULL) goto failed;
5051 for (i = 0; i < len; i++) {
5052 withitem_ty value;
5053 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5054 if (res != 0) goto failed;
5055 asdl_seq_SET(items, i, value);
5056 }
5057 Py_CLEAR(tmp);
5058 } else {
5059 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5060 return 1;
5061 }
5062 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5063 int res;
5064 Py_ssize_t len;
5065 Py_ssize_t i;
5066 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5067 if (tmp == NULL) goto failed;
5068 if (!PyList_Check(tmp)) {
5069 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5070 goto failed;
5071 }
5072 len = PyList_GET_SIZE(tmp);
5073 body = _Py_asdl_seq_new(len, arena);
5074 if (body == NULL) goto failed;
5075 for (i = 0; i < len; i++) {
5076 stmt_ty value;
5077 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5078 if (res != 0) goto failed;
5079 asdl_seq_SET(body, i, value);
5080 }
5081 Py_CLEAR(tmp);
5082 } else {
5083 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5084 return 1;
5085 }
5086 *out = AsyncWith(items, body, lineno, col_offset, arena);
5087 if (*out == NULL) goto failed;
5088 return 0;
5089 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005090 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5091 if (isinstance == -1) {
5092 return 1;
5093 }
5094 if (isinstance) {
5095 expr_ty exc;
5096 expr_ty cause;
5097
Victor Stinneree4b59c2013-07-27 00:01:35 +02005098 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005099 int res;
5100 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5101 if (tmp == NULL) goto failed;
5102 res = obj2ast_expr(tmp, &exc, arena);
5103 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005104 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005105 } else {
5106 exc = NULL;
5107 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005108 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005109 int res;
5110 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5111 if (tmp == NULL) goto failed;
5112 res = obj2ast_expr(tmp, &cause, arena);
5113 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005114 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005115 } else {
5116 cause = NULL;
5117 }
5118 *out = Raise(exc, cause, lineno, col_offset, arena);
5119 if (*out == NULL) goto failed;
5120 return 0;
5121 }
5122 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5123 if (isinstance == -1) {
5124 return 1;
5125 }
5126 if (isinstance) {
5127 asdl_seq* body;
5128 asdl_seq* handlers;
5129 asdl_seq* orelse;
5130 asdl_seq* finalbody;
5131
5132 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5133 int res;
5134 Py_ssize_t len;
5135 Py_ssize_t i;
5136 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5137 if (tmp == NULL) goto failed;
5138 if (!PyList_Check(tmp)) {
5139 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5140 goto failed;
5141 }
5142 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005143 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005144 if (body == NULL) goto failed;
5145 for (i = 0; i < len; i++) {
5146 stmt_ty value;
5147 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5148 if (res != 0) goto failed;
5149 asdl_seq_SET(body, i, value);
5150 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005151 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005152 } else {
5153 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5154 return 1;
5155 }
5156 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5157 int res;
5158 Py_ssize_t len;
5159 Py_ssize_t i;
5160 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5161 if (tmp == NULL) goto failed;
5162 if (!PyList_Check(tmp)) {
5163 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5164 goto failed;
5165 }
5166 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005167 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005168 if (handlers == NULL) goto failed;
5169 for (i = 0; i < len; i++) {
5170 excepthandler_ty value;
5171 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5172 if (res != 0) goto failed;
5173 asdl_seq_SET(handlers, i, value);
5174 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005175 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005176 } else {
5177 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5178 return 1;
5179 }
5180 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5181 int res;
5182 Py_ssize_t len;
5183 Py_ssize_t i;
5184 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5185 if (tmp == NULL) goto failed;
5186 if (!PyList_Check(tmp)) {
5187 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5188 goto failed;
5189 }
5190 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005191 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005192 if (orelse == NULL) goto failed;
5193 for (i = 0; i < len; i++) {
5194 stmt_ty value;
5195 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5196 if (res != 0) goto failed;
5197 asdl_seq_SET(orelse, i, value);
5198 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005200 } else {
5201 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5202 return 1;
5203 }
5204 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5205 int res;
5206 Py_ssize_t len;
5207 Py_ssize_t i;
5208 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5209 if (tmp == NULL) goto failed;
5210 if (!PyList_Check(tmp)) {
5211 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5212 goto failed;
5213 }
5214 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005215 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005216 if (finalbody == NULL) goto failed;
5217 for (i = 0; i < len; i++) {
5218 stmt_ty value;
5219 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5220 if (res != 0) goto failed;
5221 asdl_seq_SET(finalbody, i, value);
5222 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005224 } else {
5225 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5226 return 1;
5227 }
5228 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5229 arena);
5230 if (*out == NULL) goto failed;
5231 return 0;
5232 }
5233 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5234 if (isinstance == -1) {
5235 return 1;
5236 }
5237 if (isinstance) {
5238 expr_ty test;
5239 expr_ty msg;
5240
5241 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5242 int res;
5243 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5244 if (tmp == NULL) goto failed;
5245 res = obj2ast_expr(tmp, &test, arena);
5246 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005248 } else {
5249 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5250 return 1;
5251 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005252 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005253 int res;
5254 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5255 if (tmp == NULL) goto failed;
5256 res = obj2ast_expr(tmp, &msg, arena);
5257 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005258 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005259 } else {
5260 msg = NULL;
5261 }
5262 *out = Assert(test, msg, lineno, col_offset, arena);
5263 if (*out == NULL) goto failed;
5264 return 0;
5265 }
5266 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5267 if (isinstance == -1) {
5268 return 1;
5269 }
5270 if (isinstance) {
5271 asdl_seq* names;
5272
5273 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5274 int res;
5275 Py_ssize_t len;
5276 Py_ssize_t i;
5277 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5278 if (tmp == NULL) goto failed;
5279 if (!PyList_Check(tmp)) {
5280 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5281 goto failed;
5282 }
5283 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005284 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005285 if (names == NULL) goto failed;
5286 for (i = 0; i < len; i++) {
5287 alias_ty value;
5288 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5289 if (res != 0) goto failed;
5290 asdl_seq_SET(names, i, value);
5291 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005292 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005293 } else {
5294 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5295 return 1;
5296 }
5297 *out = Import(names, lineno, col_offset, arena);
5298 if (*out == NULL) goto failed;
5299 return 0;
5300 }
5301 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5302 if (isinstance == -1) {
5303 return 1;
5304 }
5305 if (isinstance) {
5306 identifier module;
5307 asdl_seq* names;
5308 int level;
5309
Victor Stinneree4b59c2013-07-27 00:01:35 +02005310 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005311 int res;
5312 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5313 if (tmp == NULL) goto failed;
5314 res = obj2ast_identifier(tmp, &module, arena);
5315 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005316 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005317 } else {
5318 module = NULL;
5319 }
5320 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5321 int res;
5322 Py_ssize_t len;
5323 Py_ssize_t i;
5324 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5325 if (tmp == NULL) goto failed;
5326 if (!PyList_Check(tmp)) {
5327 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5328 goto failed;
5329 }
5330 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005331 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005332 if (names == NULL) goto failed;
5333 for (i = 0; i < len; i++) {
5334 alias_ty value;
5335 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5336 if (res != 0) goto failed;
5337 asdl_seq_SET(names, i, value);
5338 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005339 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005340 } else {
5341 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5342 return 1;
5343 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005344 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005345 int res;
5346 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5347 if (tmp == NULL) goto failed;
5348 res = obj2ast_int(tmp, &level, arena);
5349 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005350 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005351 } else {
5352 level = 0;
5353 }
5354 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5355 if (*out == NULL) goto failed;
5356 return 0;
5357 }
5358 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5359 if (isinstance == -1) {
5360 return 1;
5361 }
5362 if (isinstance) {
5363 asdl_seq* names;
5364
5365 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5366 int res;
5367 Py_ssize_t len;
5368 Py_ssize_t i;
5369 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5370 if (tmp == NULL) goto failed;
5371 if (!PyList_Check(tmp)) {
5372 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5373 goto failed;
5374 }
5375 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005376 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005377 if (names == NULL) goto failed;
5378 for (i = 0; i < len; i++) {
5379 identifier value;
5380 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5381 if (res != 0) goto failed;
5382 asdl_seq_SET(names, i, value);
5383 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005384 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005385 } else {
5386 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5387 return 1;
5388 }
5389 *out = Global(names, lineno, col_offset, arena);
5390 if (*out == NULL) goto failed;
5391 return 0;
5392 }
5393 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5394 if (isinstance == -1) {
5395 return 1;
5396 }
5397 if (isinstance) {
5398 asdl_seq* names;
5399
5400 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5401 int res;
5402 Py_ssize_t len;
5403 Py_ssize_t i;
5404 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5405 if (tmp == NULL) goto failed;
5406 if (!PyList_Check(tmp)) {
5407 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5408 goto failed;
5409 }
5410 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005411 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005412 if (names == NULL) goto failed;
5413 for (i = 0; i < len; i++) {
5414 identifier value;
5415 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5416 if (res != 0) goto failed;
5417 asdl_seq_SET(names, i, value);
5418 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005419 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005420 } else {
5421 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5422 return 1;
5423 }
5424 *out = Nonlocal(names, lineno, col_offset, arena);
5425 if (*out == NULL) goto failed;
5426 return 0;
5427 }
5428 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5429 if (isinstance == -1) {
5430 return 1;
5431 }
5432 if (isinstance) {
5433 expr_ty value;
5434
5435 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5436 int res;
5437 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5438 if (tmp == NULL) goto failed;
5439 res = obj2ast_expr(tmp, &value, arena);
5440 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005441 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005442 } else {
5443 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5444 return 1;
5445 }
5446 *out = Expr(value, lineno, col_offset, arena);
5447 if (*out == NULL) goto failed;
5448 return 0;
5449 }
5450 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5451 if (isinstance == -1) {
5452 return 1;
5453 }
5454 if (isinstance) {
5455
5456 *out = Pass(lineno, col_offset, arena);
5457 if (*out == NULL) goto failed;
5458 return 0;
5459 }
5460 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5461 if (isinstance == -1) {
5462 return 1;
5463 }
5464 if (isinstance) {
5465
5466 *out = Break(lineno, col_offset, arena);
5467 if (*out == NULL) goto failed;
5468 return 0;
5469 }
5470 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5471 if (isinstance == -1) {
5472 return 1;
5473 }
5474 if (isinstance) {
5475
5476 *out = Continue(lineno, col_offset, arena);
5477 if (*out == NULL) goto failed;
5478 return 0;
5479 }
5480
5481 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5482 failed:
5483 Py_XDECREF(tmp);
5484 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005485}
5486
5487int
5488obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5489{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005490 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005491
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005492 PyObject *tmp = NULL;
5493 int lineno;
5494 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005495
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005496 if (obj == Py_None) {
5497 *out = NULL;
5498 return 0;
5499 }
5500 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5501 int res;
5502 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5503 if (tmp == NULL) goto failed;
5504 res = obj2ast_int(tmp, &lineno, arena);
5505 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005507 } else {
5508 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005509 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005510 }
5511 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5512 int res;
5513 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5514 if (tmp == NULL) goto failed;
5515 res = obj2ast_int(tmp, &col_offset, arena);
5516 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005517 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005518 } else {
5519 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5520 return 1;
5521 }
5522 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5523 if (isinstance == -1) {
5524 return 1;
5525 }
5526 if (isinstance) {
5527 boolop_ty op;
5528 asdl_seq* values;
5529
5530 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5531 int res;
5532 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5533 if (tmp == NULL) goto failed;
5534 res = obj2ast_boolop(tmp, &op, arena);
5535 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005536 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005537 } else {
5538 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5539 return 1;
5540 }
5541 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5542 int res;
5543 Py_ssize_t len;
5544 Py_ssize_t i;
5545 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5546 if (tmp == NULL) goto failed;
5547 if (!PyList_Check(tmp)) {
5548 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5549 goto failed;
5550 }
5551 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005552 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005553 if (values == NULL) goto failed;
5554 for (i = 0; i < len; i++) {
5555 expr_ty value;
5556 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5557 if (res != 0) goto failed;
5558 asdl_seq_SET(values, i, value);
5559 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005560 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005561 } else {
5562 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5563 return 1;
5564 }
5565 *out = BoolOp(op, values, lineno, col_offset, arena);
5566 if (*out == NULL) goto failed;
5567 return 0;
5568 }
5569 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5570 if (isinstance == -1) {
5571 return 1;
5572 }
5573 if (isinstance) {
5574 expr_ty left;
5575 operator_ty op;
5576 expr_ty right;
5577
5578 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5579 int res;
5580 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5581 if (tmp == NULL) goto failed;
5582 res = obj2ast_expr(tmp, &left, arena);
5583 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005584 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005585 } else {
5586 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5587 return 1;
5588 }
5589 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5590 int res;
5591 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5592 if (tmp == NULL) goto failed;
5593 res = obj2ast_operator(tmp, &op, arena);
5594 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005595 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005596 } else {
5597 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5598 return 1;
5599 }
5600 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5601 int res;
5602 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5603 if (tmp == NULL) goto failed;
5604 res = obj2ast_expr(tmp, &right, arena);
5605 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005606 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005607 } else {
5608 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5609 return 1;
5610 }
5611 *out = BinOp(left, op, right, lineno, col_offset, arena);
5612 if (*out == NULL) goto failed;
5613 return 0;
5614 }
5615 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5616 if (isinstance == -1) {
5617 return 1;
5618 }
5619 if (isinstance) {
5620 unaryop_ty op;
5621 expr_ty operand;
5622
5623 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5624 int res;
5625 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5626 if (tmp == NULL) goto failed;
5627 res = obj2ast_unaryop(tmp, &op, 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 \"op\" missing from UnaryOp");
5632 return 1;
5633 }
5634 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5635 int res;
5636 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5637 if (tmp == NULL) goto failed;
5638 res = obj2ast_expr(tmp, &operand, 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 \"operand\" missing from UnaryOp");
5643 return 1;
5644 }
5645 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5646 if (*out == NULL) goto failed;
5647 return 0;
5648 }
5649 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5650 if (isinstance == -1) {
5651 return 1;
5652 }
5653 if (isinstance) {
5654 arguments_ty args;
5655 expr_ty body;
5656
5657 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5658 int res;
5659 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5660 if (tmp == NULL) goto failed;
5661 res = obj2ast_arguments(tmp, &args, arena);
5662 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005663 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005664 } else {
5665 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5666 return 1;
5667 }
5668 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5669 int res;
5670 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5671 if (tmp == NULL) goto failed;
5672 res = obj2ast_expr(tmp, &body, arena);
5673 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005674 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005675 } else {
5676 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5677 return 1;
5678 }
5679 *out = Lambda(args, body, lineno, col_offset, arena);
5680 if (*out == NULL) goto failed;
5681 return 0;
5682 }
5683 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5684 if (isinstance == -1) {
5685 return 1;
5686 }
5687 if (isinstance) {
5688 expr_ty test;
5689 expr_ty body;
5690 expr_ty orelse;
5691
5692 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5693 int res;
5694 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5695 if (tmp == NULL) goto failed;
5696 res = obj2ast_expr(tmp, &test, arena);
5697 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005698 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005699 } else {
5700 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5701 return 1;
5702 }
5703 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5704 int res;
5705 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5706 if (tmp == NULL) goto failed;
5707 res = obj2ast_expr(tmp, &body, arena);
5708 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005710 } else {
5711 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5712 return 1;
5713 }
5714 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5715 int res;
5716 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5717 if (tmp == NULL) goto failed;
5718 res = obj2ast_expr(tmp, &orelse, 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 \"orelse\" missing from IfExp");
5723 return 1;
5724 }
5725 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5726 if (*out == NULL) goto failed;
5727 return 0;
5728 }
5729 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5730 if (isinstance == -1) {
5731 return 1;
5732 }
5733 if (isinstance) {
5734 asdl_seq* keys;
5735 asdl_seq* values;
5736
5737 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5738 int res;
5739 Py_ssize_t len;
5740 Py_ssize_t i;
5741 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5742 if (tmp == NULL) goto failed;
5743 if (!PyList_Check(tmp)) {
5744 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5745 goto failed;
5746 }
5747 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005748 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005749 if (keys == NULL) goto failed;
5750 for (i = 0; i < len; i++) {
5751 expr_ty value;
5752 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5753 if (res != 0) goto failed;
5754 asdl_seq_SET(keys, i, value);
5755 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005756 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005757 } else {
5758 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5759 return 1;
5760 }
5761 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5762 int res;
5763 Py_ssize_t len;
5764 Py_ssize_t i;
5765 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5766 if (tmp == NULL) goto failed;
5767 if (!PyList_Check(tmp)) {
5768 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5769 goto failed;
5770 }
5771 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005772 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005773 if (values == NULL) goto failed;
5774 for (i = 0; i < len; i++) {
5775 expr_ty value;
5776 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5777 if (res != 0) goto failed;
5778 asdl_seq_SET(values, i, value);
5779 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005780 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005781 } else {
5782 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5783 return 1;
5784 }
5785 *out = Dict(keys, values, lineno, col_offset, arena);
5786 if (*out == NULL) goto failed;
5787 return 0;
5788 }
5789 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5790 if (isinstance == -1) {
5791 return 1;
5792 }
5793 if (isinstance) {
5794 asdl_seq* elts;
5795
5796 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5797 int res;
5798 Py_ssize_t len;
5799 Py_ssize_t i;
5800 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5801 if (tmp == NULL) goto failed;
5802 if (!PyList_Check(tmp)) {
5803 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5804 goto failed;
5805 }
5806 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005807 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005808 if (elts == NULL) goto failed;
5809 for (i = 0; i < len; i++) {
5810 expr_ty value;
5811 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5812 if (res != 0) goto failed;
5813 asdl_seq_SET(elts, i, value);
5814 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005815 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005816 } else {
5817 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5818 return 1;
5819 }
5820 *out = Set(elts, lineno, col_offset, arena);
5821 if (*out == NULL) goto failed;
5822 return 0;
5823 }
5824 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5825 if (isinstance == -1) {
5826 return 1;
5827 }
5828 if (isinstance) {
5829 expr_ty elt;
5830 asdl_seq* generators;
5831
5832 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5833 int res;
5834 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5835 if (tmp == NULL) goto failed;
5836 res = obj2ast_expr(tmp, &elt, arena);
5837 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005838 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005839 } else {
5840 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5841 return 1;
5842 }
5843 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5844 int res;
5845 Py_ssize_t len;
5846 Py_ssize_t i;
5847 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5848 if (tmp == NULL) goto failed;
5849 if (!PyList_Check(tmp)) {
5850 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5851 goto failed;
5852 }
5853 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005854 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005855 if (generators == NULL) goto failed;
5856 for (i = 0; i < len; i++) {
5857 comprehension_ty value;
5858 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5859 if (res != 0) goto failed;
5860 asdl_seq_SET(generators, i, value);
5861 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005862 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005863 } else {
5864 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5865 return 1;
5866 }
5867 *out = ListComp(elt, generators, lineno, col_offset, arena);
5868 if (*out == NULL) goto failed;
5869 return 0;
5870 }
5871 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5872 if (isinstance == -1) {
5873 return 1;
5874 }
5875 if (isinstance) {
5876 expr_ty elt;
5877 asdl_seq* generators;
5878
5879 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5880 int res;
5881 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5882 if (tmp == NULL) goto failed;
5883 res = obj2ast_expr(tmp, &elt, arena);
5884 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005885 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005886 } else {
5887 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5888 return 1;
5889 }
5890 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5891 int res;
5892 Py_ssize_t len;
5893 Py_ssize_t i;
5894 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5895 if (tmp == NULL) goto failed;
5896 if (!PyList_Check(tmp)) {
5897 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5898 goto failed;
5899 }
5900 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005901 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005902 if (generators == NULL) goto failed;
5903 for (i = 0; i < len; i++) {
5904 comprehension_ty value;
5905 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5906 if (res != 0) goto failed;
5907 asdl_seq_SET(generators, i, value);
5908 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005909 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005910 } else {
5911 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5912 return 1;
5913 }
5914 *out = SetComp(elt, generators, lineno, col_offset, arena);
5915 if (*out == NULL) goto failed;
5916 return 0;
5917 }
5918 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5919 if (isinstance == -1) {
5920 return 1;
5921 }
5922 if (isinstance) {
5923 expr_ty key;
5924 expr_ty value;
5925 asdl_seq* generators;
5926
5927 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5928 int res;
5929 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5930 if (tmp == NULL) goto failed;
5931 res = obj2ast_expr(tmp, &key, arena);
5932 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005933 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005934 } else {
5935 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5936 return 1;
5937 }
5938 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5939 int res;
5940 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5941 if (tmp == NULL) goto failed;
5942 res = obj2ast_expr(tmp, &value, arena);
5943 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005944 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005945 } else {
5946 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5947 return 1;
5948 }
5949 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5950 int res;
5951 Py_ssize_t len;
5952 Py_ssize_t i;
5953 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5954 if (tmp == NULL) goto failed;
5955 if (!PyList_Check(tmp)) {
5956 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5957 goto failed;
5958 }
5959 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005960 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005961 if (generators == NULL) goto failed;
5962 for (i = 0; i < len; i++) {
5963 comprehension_ty value;
5964 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5965 if (res != 0) goto failed;
5966 asdl_seq_SET(generators, i, value);
5967 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005968 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005969 } else {
5970 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5971 return 1;
5972 }
5973 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5974 if (*out == NULL) goto failed;
5975 return 0;
5976 }
5977 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5978 if (isinstance == -1) {
5979 return 1;
5980 }
5981 if (isinstance) {
5982 expr_ty elt;
5983 asdl_seq* generators;
5984
5985 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5986 int res;
5987 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5988 if (tmp == NULL) goto failed;
5989 res = obj2ast_expr(tmp, &elt, arena);
5990 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005991 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005992 } else {
5993 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5994 return 1;
5995 }
5996 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5997 int res;
5998 Py_ssize_t len;
5999 Py_ssize_t i;
6000 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6001 if (tmp == NULL) goto failed;
6002 if (!PyList_Check(tmp)) {
6003 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6004 goto failed;
6005 }
6006 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006007 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006008 if (generators == NULL) goto failed;
6009 for (i = 0; i < len; i++) {
6010 comprehension_ty value;
6011 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6012 if (res != 0) goto failed;
6013 asdl_seq_SET(generators, i, value);
6014 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006015 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006016 } else {
6017 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6018 return 1;
6019 }
6020 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6021 if (*out == NULL) goto failed;
6022 return 0;
6023 }
Yury Selivanov75445082015-05-11 22:57:16 -04006024 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6025 if (isinstance == -1) {
6026 return 1;
6027 }
6028 if (isinstance) {
6029 expr_ty value;
6030
6031 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6032 int res;
6033 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6034 if (tmp == NULL) goto failed;
6035 res = obj2ast_expr(tmp, &value, arena);
6036 if (res != 0) goto failed;
6037 Py_CLEAR(tmp);
6038 } else {
6039 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6040 return 1;
6041 }
6042 *out = Await(value, lineno, col_offset, arena);
6043 if (*out == NULL) goto failed;
6044 return 0;
6045 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006046 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6047 if (isinstance == -1) {
6048 return 1;
6049 }
6050 if (isinstance) {
6051 expr_ty value;
6052
Victor Stinneree4b59c2013-07-27 00:01:35 +02006053 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006054 int res;
6055 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6056 if (tmp == NULL) goto failed;
6057 res = obj2ast_expr(tmp, &value, arena);
6058 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006059 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006060 } else {
6061 value = NULL;
6062 }
6063 *out = Yield(value, lineno, col_offset, arena);
6064 if (*out == NULL) goto failed;
6065 return 0;
6066 }
6067 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6068 if (isinstance == -1) {
6069 return 1;
6070 }
6071 if (isinstance) {
6072 expr_ty value;
6073
6074 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6075 int res;
6076 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6077 if (tmp == NULL) goto failed;
6078 res = obj2ast_expr(tmp, &value, arena);
6079 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006080 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006081 } else {
6082 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6083 return 1;
6084 }
6085 *out = YieldFrom(value, lineno, col_offset, arena);
6086 if (*out == NULL) goto failed;
6087 return 0;
6088 }
6089 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6090 if (isinstance == -1) {
6091 return 1;
6092 }
6093 if (isinstance) {
6094 expr_ty left;
6095 asdl_int_seq* ops;
6096 asdl_seq* comparators;
6097
6098 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6099 int res;
6100 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6101 if (tmp == NULL) goto failed;
6102 res = obj2ast_expr(tmp, &left, arena);
6103 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006104 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006105 } else {
6106 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6107 return 1;
6108 }
6109 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6110 int res;
6111 Py_ssize_t len;
6112 Py_ssize_t i;
6113 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6114 if (tmp == NULL) goto failed;
6115 if (!PyList_Check(tmp)) {
6116 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6117 goto failed;
6118 }
6119 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006120 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006121 if (ops == NULL) goto failed;
6122 for (i = 0; i < len; i++) {
6123 cmpop_ty value;
6124 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6125 if (res != 0) goto failed;
6126 asdl_seq_SET(ops, i, value);
6127 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006128 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006129 } else {
6130 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6131 return 1;
6132 }
6133 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6134 int res;
6135 Py_ssize_t len;
6136 Py_ssize_t i;
6137 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6138 if (tmp == NULL) goto failed;
6139 if (!PyList_Check(tmp)) {
6140 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6141 goto failed;
6142 }
6143 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006144 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006145 if (comparators == NULL) goto failed;
6146 for (i = 0; i < len; i++) {
6147 expr_ty value;
6148 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6149 if (res != 0) goto failed;
6150 asdl_seq_SET(comparators, i, value);
6151 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006152 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006153 } else {
6154 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6155 return 1;
6156 }
6157 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6158 if (*out == NULL) goto failed;
6159 return 0;
6160 }
6161 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6162 if (isinstance == -1) {
6163 return 1;
6164 }
6165 if (isinstance) {
6166 expr_ty func;
6167 asdl_seq* args;
6168 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006169
6170 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6171 int res;
6172 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6173 if (tmp == NULL) goto failed;
6174 res = obj2ast_expr(tmp, &func, arena);
6175 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006176 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006177 } else {
6178 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6179 return 1;
6180 }
6181 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6182 int res;
6183 Py_ssize_t len;
6184 Py_ssize_t i;
6185 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6186 if (tmp == NULL) goto failed;
6187 if (!PyList_Check(tmp)) {
6188 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6189 goto failed;
6190 }
6191 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006192 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006193 if (args == NULL) goto failed;
6194 for (i = 0; i < len; i++) {
6195 expr_ty value;
6196 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6197 if (res != 0) goto failed;
6198 asdl_seq_SET(args, i, value);
6199 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006200 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006201 } else {
6202 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6203 return 1;
6204 }
6205 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6206 int res;
6207 Py_ssize_t len;
6208 Py_ssize_t i;
6209 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6210 if (tmp == NULL) goto failed;
6211 if (!PyList_Check(tmp)) {
6212 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6213 goto failed;
6214 }
6215 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006216 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006217 if (keywords == NULL) goto failed;
6218 for (i = 0; i < len; i++) {
6219 keyword_ty value;
6220 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6221 if (res != 0) goto failed;
6222 asdl_seq_SET(keywords, i, value);
6223 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006224 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006225 } else {
6226 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6227 return 1;
6228 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006229 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006230 if (*out == NULL) goto failed;
6231 return 0;
6232 }
6233 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6234 if (isinstance == -1) {
6235 return 1;
6236 }
6237 if (isinstance) {
6238 object n;
6239
6240 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6241 int res;
6242 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6243 if (tmp == NULL) goto failed;
6244 res = obj2ast_object(tmp, &n, arena);
6245 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006246 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006247 } else {
6248 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6249 return 1;
6250 }
6251 *out = Num(n, lineno, col_offset, arena);
6252 if (*out == NULL) goto failed;
6253 return 0;
6254 }
6255 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6256 if (isinstance == -1) {
6257 return 1;
6258 }
6259 if (isinstance) {
6260 string s;
6261
6262 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6263 int res;
6264 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6265 if (tmp == NULL) goto failed;
6266 res = obj2ast_string(tmp, &s, arena);
6267 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006268 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006269 } else {
6270 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6271 return 1;
6272 }
6273 *out = Str(s, lineno, col_offset, arena);
6274 if (*out == NULL) goto failed;
6275 return 0;
6276 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006277 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6278 if (isinstance == -1) {
6279 return 1;
6280 }
6281 if (isinstance) {
6282 expr_ty value;
6283 int conversion;
6284 expr_ty format_spec;
6285
6286 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6287 int res;
6288 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6289 if (tmp == NULL) goto failed;
6290 res = obj2ast_expr(tmp, &value, arena);
6291 if (res != 0) goto failed;
6292 Py_CLEAR(tmp);
6293 } else {
6294 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6295 return 1;
6296 }
6297 if (exists_not_none(obj, &PyId_conversion)) {
6298 int res;
6299 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6300 if (tmp == NULL) goto failed;
6301 res = obj2ast_int(tmp, &conversion, arena);
6302 if (res != 0) goto failed;
6303 Py_CLEAR(tmp);
6304 } else {
6305 conversion = 0;
6306 }
6307 if (exists_not_none(obj, &PyId_format_spec)) {
6308 int res;
6309 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6310 if (tmp == NULL) goto failed;
6311 res = obj2ast_expr(tmp, &format_spec, arena);
6312 if (res != 0) goto failed;
6313 Py_CLEAR(tmp);
6314 } else {
6315 format_spec = NULL;
6316 }
6317 *out = FormattedValue(value, conversion, format_spec, lineno,
6318 col_offset, arena);
6319 if (*out == NULL) goto failed;
6320 return 0;
6321 }
6322 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6323 if (isinstance == -1) {
6324 return 1;
6325 }
6326 if (isinstance) {
6327 asdl_seq* values;
6328
6329 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6330 int res;
6331 Py_ssize_t len;
6332 Py_ssize_t i;
6333 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6334 if (tmp == NULL) goto failed;
6335 if (!PyList_Check(tmp)) {
6336 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6337 goto failed;
6338 }
6339 len = PyList_GET_SIZE(tmp);
6340 values = _Py_asdl_seq_new(len, arena);
6341 if (values == NULL) goto failed;
6342 for (i = 0; i < len; i++) {
6343 expr_ty value;
6344 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6345 if (res != 0) goto failed;
6346 asdl_seq_SET(values, i, value);
6347 }
6348 Py_CLEAR(tmp);
6349 } else {
6350 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6351 return 1;
6352 }
6353 *out = JoinedStr(values, lineno, col_offset, arena);
6354 if (*out == NULL) goto failed;
6355 return 0;
6356 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006357 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6358 if (isinstance == -1) {
6359 return 1;
6360 }
6361 if (isinstance) {
6362 bytes s;
6363
6364 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6365 int res;
6366 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6367 if (tmp == NULL) goto failed;
6368 res = obj2ast_bytes(tmp, &s, arena);
6369 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006370 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006371 } else {
6372 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6373 return 1;
6374 }
6375 *out = Bytes(s, lineno, col_offset, arena);
6376 if (*out == NULL) goto failed;
6377 return 0;
6378 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006379 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6380 if (isinstance == -1) {
6381 return 1;
6382 }
6383 if (isinstance) {
6384 singleton value;
6385
6386 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6387 int res;
6388 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6389 if (tmp == NULL) goto failed;
6390 res = obj2ast_singleton(tmp, &value, arena);
6391 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006392 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006393 } else {
6394 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6395 return 1;
6396 }
6397 *out = NameConstant(value, lineno, col_offset, arena);
6398 if (*out == NULL) goto failed;
6399 return 0;
6400 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006401 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6402 if (isinstance == -1) {
6403 return 1;
6404 }
6405 if (isinstance) {
6406
6407 *out = Ellipsis(lineno, col_offset, arena);
6408 if (*out == NULL) goto failed;
6409 return 0;
6410 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006411 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6412 if (isinstance == -1) {
6413 return 1;
6414 }
6415 if (isinstance) {
6416 constant value;
6417
6418 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6419 int res;
6420 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6421 if (tmp == NULL) goto failed;
6422 res = obj2ast_constant(tmp, &value, arena);
6423 if (res != 0) goto failed;
6424 Py_CLEAR(tmp);
6425 } else {
6426 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6427 return 1;
6428 }
6429 *out = Constant(value, lineno, col_offset, arena);
6430 if (*out == NULL) goto failed;
6431 return 0;
6432 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006433 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6434 if (isinstance == -1) {
6435 return 1;
6436 }
6437 if (isinstance) {
6438 expr_ty value;
6439 identifier attr;
6440 expr_context_ty ctx;
6441
6442 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6443 int res;
6444 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6445 if (tmp == NULL) goto failed;
6446 res = obj2ast_expr(tmp, &value, arena);
6447 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006448 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006449 } else {
6450 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6451 return 1;
6452 }
6453 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6454 int res;
6455 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6456 if (tmp == NULL) goto failed;
6457 res = obj2ast_identifier(tmp, &attr, arena);
6458 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006459 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006460 } else {
6461 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6462 return 1;
6463 }
6464 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6465 int res;
6466 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6467 if (tmp == NULL) goto failed;
6468 res = obj2ast_expr_context(tmp, &ctx, arena);
6469 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006470 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006471 } else {
6472 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6473 return 1;
6474 }
6475 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6476 if (*out == NULL) goto failed;
6477 return 0;
6478 }
6479 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6480 if (isinstance == -1) {
6481 return 1;
6482 }
6483 if (isinstance) {
6484 expr_ty value;
6485 slice_ty slice;
6486 expr_context_ty ctx;
6487
6488 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6489 int res;
6490 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6491 if (tmp == NULL) goto failed;
6492 res = obj2ast_expr(tmp, &value, arena);
6493 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006494 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006495 } else {
6496 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6497 return 1;
6498 }
6499 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6500 int res;
6501 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6502 if (tmp == NULL) goto failed;
6503 res = obj2ast_slice(tmp, &slice, arena);
6504 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006505 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006506 } else {
6507 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6508 return 1;
6509 }
6510 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6511 int res;
6512 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6513 if (tmp == NULL) goto failed;
6514 res = obj2ast_expr_context(tmp, &ctx, arena);
6515 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006516 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006517 } else {
6518 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6519 return 1;
6520 }
6521 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6522 if (*out == NULL) goto failed;
6523 return 0;
6524 }
6525 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6526 if (isinstance == -1) {
6527 return 1;
6528 }
6529 if (isinstance) {
6530 expr_ty value;
6531 expr_context_ty ctx;
6532
6533 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6534 int res;
6535 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6536 if (tmp == NULL) goto failed;
6537 res = obj2ast_expr(tmp, &value, arena);
6538 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006539 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006540 } else {
6541 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6542 return 1;
6543 }
6544 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6545 int res;
6546 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6547 if (tmp == NULL) goto failed;
6548 res = obj2ast_expr_context(tmp, &ctx, arena);
6549 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006550 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006551 } else {
6552 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6553 return 1;
6554 }
6555 *out = Starred(value, ctx, lineno, col_offset, arena);
6556 if (*out == NULL) goto failed;
6557 return 0;
6558 }
6559 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6560 if (isinstance == -1) {
6561 return 1;
6562 }
6563 if (isinstance) {
6564 identifier id;
6565 expr_context_ty ctx;
6566
6567 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6568 int res;
6569 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6570 if (tmp == NULL) goto failed;
6571 res = obj2ast_identifier(tmp, &id, arena);
6572 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006573 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006574 } else {
6575 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6576 return 1;
6577 }
6578 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6579 int res;
6580 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6581 if (tmp == NULL) goto failed;
6582 res = obj2ast_expr_context(tmp, &ctx, arena);
6583 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006584 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006585 } else {
6586 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6587 return 1;
6588 }
6589 *out = Name(id, ctx, lineno, col_offset, arena);
6590 if (*out == NULL) goto failed;
6591 return 0;
6592 }
6593 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6594 if (isinstance == -1) {
6595 return 1;
6596 }
6597 if (isinstance) {
6598 asdl_seq* elts;
6599 expr_context_ty ctx;
6600
6601 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6602 int res;
6603 Py_ssize_t len;
6604 Py_ssize_t i;
6605 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6606 if (tmp == NULL) goto failed;
6607 if (!PyList_Check(tmp)) {
6608 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6609 goto failed;
6610 }
6611 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006612 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006613 if (elts == NULL) goto failed;
6614 for (i = 0; i < len; i++) {
6615 expr_ty value;
6616 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6617 if (res != 0) goto failed;
6618 asdl_seq_SET(elts, i, value);
6619 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006620 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006621 } else {
6622 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6623 return 1;
6624 }
6625 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6626 int res;
6627 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6628 if (tmp == NULL) goto failed;
6629 res = obj2ast_expr_context(tmp, &ctx, 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 \"ctx\" missing from List");
6634 return 1;
6635 }
6636 *out = List(elts, ctx, lineno, col_offset, arena);
6637 if (*out == NULL) goto failed;
6638 return 0;
6639 }
6640 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6641 if (isinstance == -1) {
6642 return 1;
6643 }
6644 if (isinstance) {
6645 asdl_seq* elts;
6646 expr_context_ty ctx;
6647
6648 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6649 int res;
6650 Py_ssize_t len;
6651 Py_ssize_t i;
6652 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6653 if (tmp == NULL) goto failed;
6654 if (!PyList_Check(tmp)) {
6655 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6656 goto failed;
6657 }
6658 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006659 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006660 if (elts == NULL) goto failed;
6661 for (i = 0; i < len; i++) {
6662 expr_ty value;
6663 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6664 if (res != 0) goto failed;
6665 asdl_seq_SET(elts, i, value);
6666 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006667 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006668 } else {
6669 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6670 return 1;
6671 }
6672 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6673 int res;
6674 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6675 if (tmp == NULL) goto failed;
6676 res = obj2ast_expr_context(tmp, &ctx, arena);
6677 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006678 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006679 } else {
6680 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6681 return 1;
6682 }
6683 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6684 if (*out == NULL) goto failed;
6685 return 0;
6686 }
6687
6688 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6689 failed:
6690 Py_XDECREF(tmp);
6691 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006692}
6693
6694int
6695obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6696{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006697 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006698
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006699 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6700 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006701 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006702 }
6703 if (isinstance) {
6704 *out = Load;
6705 return 0;
6706 }
6707 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6708 if (isinstance == -1) {
6709 return 1;
6710 }
6711 if (isinstance) {
6712 *out = Store;
6713 return 0;
6714 }
6715 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6716 if (isinstance == -1) {
6717 return 1;
6718 }
6719 if (isinstance) {
6720 *out = Del;
6721 return 0;
6722 }
6723 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6724 if (isinstance == -1) {
6725 return 1;
6726 }
6727 if (isinstance) {
6728 *out = AugLoad;
6729 return 0;
6730 }
6731 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6732 if (isinstance == -1) {
6733 return 1;
6734 }
6735 if (isinstance) {
6736 *out = AugStore;
6737 return 0;
6738 }
6739 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6740 if (isinstance == -1) {
6741 return 1;
6742 }
6743 if (isinstance) {
6744 *out = Param;
6745 return 0;
6746 }
6747
6748 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6749 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006750}
6751
6752int
6753obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6754{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006755 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006756
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006757 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006758
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006759 if (obj == Py_None) {
6760 *out = NULL;
6761 return 0;
6762 }
6763 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6764 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006765 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006766 }
6767 if (isinstance) {
6768 expr_ty lower;
6769 expr_ty upper;
6770 expr_ty step;
6771
Victor Stinneree4b59c2013-07-27 00:01:35 +02006772 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006773 int res;
6774 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6775 if (tmp == NULL) goto failed;
6776 res = obj2ast_expr(tmp, &lower, arena);
6777 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006778 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006779 } else {
6780 lower = NULL;
6781 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006782 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006783 int res;
6784 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6785 if (tmp == NULL) goto failed;
6786 res = obj2ast_expr(tmp, &upper, arena);
6787 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006788 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006789 } else {
6790 upper = NULL;
6791 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006792 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006793 int res;
6794 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6795 if (tmp == NULL) goto failed;
6796 res = obj2ast_expr(tmp, &step, arena);
6797 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006798 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006799 } else {
6800 step = NULL;
6801 }
6802 *out = Slice(lower, upper, step, arena);
6803 if (*out == NULL) goto failed;
6804 return 0;
6805 }
6806 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6807 if (isinstance == -1) {
6808 return 1;
6809 }
6810 if (isinstance) {
6811 asdl_seq* dims;
6812
6813 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6814 int res;
6815 Py_ssize_t len;
6816 Py_ssize_t i;
6817 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6818 if (tmp == NULL) goto failed;
6819 if (!PyList_Check(tmp)) {
6820 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6821 goto failed;
6822 }
6823 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006824 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006825 if (dims == NULL) goto failed;
6826 for (i = 0; i < len; i++) {
6827 slice_ty value;
6828 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6829 if (res != 0) goto failed;
6830 asdl_seq_SET(dims, i, value);
6831 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006832 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 } else {
6834 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6835 return 1;
6836 }
6837 *out = ExtSlice(dims, arena);
6838 if (*out == NULL) goto failed;
6839 return 0;
6840 }
6841 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6842 if (isinstance == -1) {
6843 return 1;
6844 }
6845 if (isinstance) {
6846 expr_ty value;
6847
6848 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6849 int res;
6850 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6851 if (tmp == NULL) goto failed;
6852 res = obj2ast_expr(tmp, &value, arena);
6853 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006854 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006855 } else {
6856 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6857 return 1;
6858 }
6859 *out = Index(value, arena);
6860 if (*out == NULL) goto failed;
6861 return 0;
6862 }
6863
6864 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6865 failed:
6866 Py_XDECREF(tmp);
6867 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006868}
6869
6870int
6871obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6872{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006873 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006874
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006875 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6876 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006877 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006878 }
6879 if (isinstance) {
6880 *out = And;
6881 return 0;
6882 }
6883 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6884 if (isinstance == -1) {
6885 return 1;
6886 }
6887 if (isinstance) {
6888 *out = Or;
6889 return 0;
6890 }
6891
6892 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6893 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006894}
6895
6896int
6897obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6898{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006899 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006900
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006901 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6902 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006903 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006904 }
6905 if (isinstance) {
6906 *out = Add;
6907 return 0;
6908 }
6909 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6910 if (isinstance == -1) {
6911 return 1;
6912 }
6913 if (isinstance) {
6914 *out = Sub;
6915 return 0;
6916 }
6917 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6918 if (isinstance == -1) {
6919 return 1;
6920 }
6921 if (isinstance) {
6922 *out = Mult;
6923 return 0;
6924 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006925 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6926 if (isinstance == -1) {
6927 return 1;
6928 }
6929 if (isinstance) {
6930 *out = MatMult;
6931 return 0;
6932 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006933 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6934 if (isinstance == -1) {
6935 return 1;
6936 }
6937 if (isinstance) {
6938 *out = Div;
6939 return 0;
6940 }
6941 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6942 if (isinstance == -1) {
6943 return 1;
6944 }
6945 if (isinstance) {
6946 *out = Mod;
6947 return 0;
6948 }
6949 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6950 if (isinstance == -1) {
6951 return 1;
6952 }
6953 if (isinstance) {
6954 *out = Pow;
6955 return 0;
6956 }
6957 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6958 if (isinstance == -1) {
6959 return 1;
6960 }
6961 if (isinstance) {
6962 *out = LShift;
6963 return 0;
6964 }
6965 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6966 if (isinstance == -1) {
6967 return 1;
6968 }
6969 if (isinstance) {
6970 *out = RShift;
6971 return 0;
6972 }
6973 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6974 if (isinstance == -1) {
6975 return 1;
6976 }
6977 if (isinstance) {
6978 *out = BitOr;
6979 return 0;
6980 }
6981 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6982 if (isinstance == -1) {
6983 return 1;
6984 }
6985 if (isinstance) {
6986 *out = BitXor;
6987 return 0;
6988 }
6989 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6990 if (isinstance == -1) {
6991 return 1;
6992 }
6993 if (isinstance) {
6994 *out = BitAnd;
6995 return 0;
6996 }
6997 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6998 if (isinstance == -1) {
6999 return 1;
7000 }
7001 if (isinstance) {
7002 *out = FloorDiv;
7003 return 0;
7004 }
7005
7006 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7007 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007008}
7009
7010int
7011obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7012{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007013 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007014
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007015 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7016 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007017 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007018 }
7019 if (isinstance) {
7020 *out = Invert;
7021 return 0;
7022 }
7023 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7024 if (isinstance == -1) {
7025 return 1;
7026 }
7027 if (isinstance) {
7028 *out = Not;
7029 return 0;
7030 }
7031 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7032 if (isinstance == -1) {
7033 return 1;
7034 }
7035 if (isinstance) {
7036 *out = UAdd;
7037 return 0;
7038 }
7039 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7040 if (isinstance == -1) {
7041 return 1;
7042 }
7043 if (isinstance) {
7044 *out = USub;
7045 return 0;
7046 }
7047
7048 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7049 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007050}
7051
7052int
7053obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7054{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007055 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007056
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007057 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7058 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007059 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007060 }
7061 if (isinstance) {
7062 *out = Eq;
7063 return 0;
7064 }
7065 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7066 if (isinstance == -1) {
7067 return 1;
7068 }
7069 if (isinstance) {
7070 *out = NotEq;
7071 return 0;
7072 }
7073 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7074 if (isinstance == -1) {
7075 return 1;
7076 }
7077 if (isinstance) {
7078 *out = Lt;
7079 return 0;
7080 }
7081 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7082 if (isinstance == -1) {
7083 return 1;
7084 }
7085 if (isinstance) {
7086 *out = LtE;
7087 return 0;
7088 }
7089 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7090 if (isinstance == -1) {
7091 return 1;
7092 }
7093 if (isinstance) {
7094 *out = Gt;
7095 return 0;
7096 }
7097 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7098 if (isinstance == -1) {
7099 return 1;
7100 }
7101 if (isinstance) {
7102 *out = GtE;
7103 return 0;
7104 }
7105 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7106 if (isinstance == -1) {
7107 return 1;
7108 }
7109 if (isinstance) {
7110 *out = Is;
7111 return 0;
7112 }
7113 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7114 if (isinstance == -1) {
7115 return 1;
7116 }
7117 if (isinstance) {
7118 *out = IsNot;
7119 return 0;
7120 }
7121 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7122 if (isinstance == -1) {
7123 return 1;
7124 }
7125 if (isinstance) {
7126 *out = In;
7127 return 0;
7128 }
7129 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7130 if (isinstance == -1) {
7131 return 1;
7132 }
7133 if (isinstance) {
7134 *out = NotIn;
7135 return 0;
7136 }
7137
7138 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7139 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007140}
7141
7142int
7143obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7144{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007145 PyObject* tmp = NULL;
7146 expr_ty target;
7147 expr_ty iter;
7148 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007149
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007150 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7151 int res;
7152 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7153 if (tmp == NULL) goto failed;
7154 res = obj2ast_expr(tmp, &target, arena);
7155 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007156 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007157 } else {
7158 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007159 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007160 }
7161 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7162 int res;
7163 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7164 if (tmp == NULL) goto failed;
7165 res = obj2ast_expr(tmp, &iter, arena);
7166 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007167 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007168 } else {
7169 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7170 return 1;
7171 }
7172 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7173 int res;
7174 Py_ssize_t len;
7175 Py_ssize_t i;
7176 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7177 if (tmp == NULL) goto failed;
7178 if (!PyList_Check(tmp)) {
7179 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7180 goto failed;
7181 }
7182 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007183 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007184 if (ifs == NULL) goto failed;
7185 for (i = 0; i < len; i++) {
7186 expr_ty value;
7187 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7188 if (res != 0) goto failed;
7189 asdl_seq_SET(ifs, i, value);
7190 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007191 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007192 } else {
7193 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7194 return 1;
7195 }
7196 *out = comprehension(target, iter, ifs, arena);
7197 return 0;
7198failed:
7199 Py_XDECREF(tmp);
7200 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007201}
7202
7203int
7204obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7205{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007206 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007207
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007208 PyObject *tmp = NULL;
7209 int lineno;
7210 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007211
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007212 if (obj == Py_None) {
7213 *out = NULL;
7214 return 0;
7215 }
7216 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7217 int res;
7218 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7219 if (tmp == NULL) goto failed;
7220 res = obj2ast_int(tmp, &lineno, arena);
7221 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007222 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007223 } else {
7224 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007225 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007226 }
7227 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7228 int res;
7229 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7230 if (tmp == NULL) goto failed;
7231 res = obj2ast_int(tmp, &col_offset, arena);
7232 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007233 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007234 } else {
7235 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7236 return 1;
7237 }
7238 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7239 if (isinstance == -1) {
7240 return 1;
7241 }
7242 if (isinstance) {
7243 expr_ty type;
7244 identifier name;
7245 asdl_seq* body;
7246
Victor Stinneree4b59c2013-07-27 00:01:35 +02007247 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007248 int res;
7249 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7250 if (tmp == NULL) goto failed;
7251 res = obj2ast_expr(tmp, &type, arena);
7252 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007253 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007254 } else {
7255 type = NULL;
7256 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007257 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007258 int res;
7259 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7260 if (tmp == NULL) goto failed;
7261 res = obj2ast_identifier(tmp, &name, arena);
7262 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007263 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007264 } else {
7265 name = NULL;
7266 }
7267 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7268 int res;
7269 Py_ssize_t len;
7270 Py_ssize_t i;
7271 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7272 if (tmp == NULL) goto failed;
7273 if (!PyList_Check(tmp)) {
7274 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7275 goto failed;
7276 }
7277 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007278 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007279 if (body == NULL) goto failed;
7280 for (i = 0; i < len; i++) {
7281 stmt_ty value;
7282 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7283 if (res != 0) goto failed;
7284 asdl_seq_SET(body, i, value);
7285 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007286 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007287 } else {
7288 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7289 return 1;
7290 }
7291 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7292 if (*out == NULL) goto failed;
7293 return 0;
7294 }
7295
7296 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7297 failed:
7298 Py_XDECREF(tmp);
7299 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007300}
7301
7302int
7303obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7304{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007305 PyObject* tmp = NULL;
7306 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007307 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007308 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007309 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007310 arg_ty kwarg;
7311 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007312
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007313 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7314 int res;
7315 Py_ssize_t len;
7316 Py_ssize_t i;
7317 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7318 if (tmp == NULL) goto failed;
7319 if (!PyList_Check(tmp)) {
7320 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7321 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007322 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007323 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007324 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007325 if (args == NULL) goto failed;
7326 for (i = 0; i < len; i++) {
7327 arg_ty value;
7328 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7329 if (res != 0) goto failed;
7330 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007331 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007332 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007333 } else {
7334 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007335 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007336 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007337 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007338 int res;
7339 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7340 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007341 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007342 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007344 } else {
7345 vararg = NULL;
7346 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007347 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7348 int res;
7349 Py_ssize_t len;
7350 Py_ssize_t i;
7351 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7352 if (tmp == NULL) goto failed;
7353 if (!PyList_Check(tmp)) {
7354 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7355 goto failed;
7356 }
7357 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007358 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007359 if (kwonlyargs == NULL) goto failed;
7360 for (i = 0; i < len; i++) {
7361 arg_ty value;
7362 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7363 if (res != 0) goto failed;
7364 asdl_seq_SET(kwonlyargs, i, value);
7365 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007366 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007367 } else {
7368 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7369 return 1;
7370 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007371 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7372 int res;
7373 Py_ssize_t len;
7374 Py_ssize_t i;
7375 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7376 if (tmp == NULL) goto failed;
7377 if (!PyList_Check(tmp)) {
7378 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7379 goto failed;
7380 }
7381 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007382 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007383 if (kw_defaults == NULL) goto failed;
7384 for (i = 0; i < len; i++) {
7385 expr_ty value;
7386 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7387 if (res != 0) goto failed;
7388 asdl_seq_SET(kw_defaults, i, value);
7389 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007390 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007391 } else {
7392 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7393 return 1;
7394 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007395 if (exists_not_none(obj, &PyId_kwarg)) {
7396 int res;
7397 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7398 if (tmp == NULL) goto failed;
7399 res = obj2ast_arg(tmp, &kwarg, arena);
7400 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007401 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007402 } else {
7403 kwarg = NULL;
7404 }
7405 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7406 int res;
7407 Py_ssize_t len;
7408 Py_ssize_t i;
7409 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7410 if (tmp == NULL) goto failed;
7411 if (!PyList_Check(tmp)) {
7412 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7413 goto failed;
7414 }
7415 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007416 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007417 if (defaults == NULL) goto failed;
7418 for (i = 0; i < len; i++) {
7419 expr_ty value;
7420 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7421 if (res != 0) goto failed;
7422 asdl_seq_SET(defaults, i, value);
7423 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007424 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007425 } else {
7426 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7427 return 1;
7428 }
7429 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7430 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007431 return 0;
7432failed:
7433 Py_XDECREF(tmp);
7434 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007435}
7436
7437int
7438obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7439{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007440 PyObject* tmp = NULL;
7441 identifier arg;
7442 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007443 int lineno;
7444 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007445
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007446 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7447 int res;
7448 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7449 if (tmp == NULL) goto failed;
7450 res = obj2ast_identifier(tmp, &arg, arena);
7451 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007452 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007453 } else {
7454 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007455 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007456 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007457 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007458 int res;
7459 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7460 if (tmp == NULL) goto failed;
7461 res = obj2ast_expr(tmp, &annotation, arena);
7462 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007463 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007464 } else {
7465 annotation = NULL;
7466 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007467 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7468 int res;
7469 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7470 if (tmp == NULL) goto failed;
7471 res = obj2ast_int(tmp, &lineno, arena);
7472 if (res != 0) goto failed;
7473 Py_CLEAR(tmp);
7474 } else {
7475 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7476 return 1;
7477 }
7478 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7479 int res;
7480 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7481 if (tmp == NULL) goto failed;
7482 res = obj2ast_int(tmp, &col_offset, arena);
7483 if (res != 0) goto failed;
7484 Py_CLEAR(tmp);
7485 } else {
7486 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7487 return 1;
7488 }
7489 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007490 return 0;
7491failed:
7492 Py_XDECREF(tmp);
7493 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007494}
7495
7496int
7497obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7498{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007499 PyObject* tmp = NULL;
7500 identifier arg;
7501 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007502
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007503 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007504 int res;
7505 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7506 if (tmp == NULL) goto failed;
7507 res = obj2ast_identifier(tmp, &arg, arena);
7508 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007509 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007510 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007511 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007512 }
7513 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7514 int res;
7515 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7516 if (tmp == NULL) goto failed;
7517 res = obj2ast_expr(tmp, &value, arena);
7518 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007519 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007520 } else {
7521 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7522 return 1;
7523 }
7524 *out = keyword(arg, value, arena);
7525 return 0;
7526failed:
7527 Py_XDECREF(tmp);
7528 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007529}
7530
7531int
7532obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7533{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007534 PyObject* tmp = NULL;
7535 identifier name;
7536 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007537
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007538 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7539 int res;
7540 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7541 if (tmp == NULL) goto failed;
7542 res = obj2ast_identifier(tmp, &name, arena);
7543 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007544 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007545 } else {
7546 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007547 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007548 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007549 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007550 int res;
7551 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7552 if (tmp == NULL) goto failed;
7553 res = obj2ast_identifier(tmp, &asname, arena);
7554 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007555 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007556 } else {
7557 asname = NULL;
7558 }
7559 *out = alias(name, asname, arena);
7560 return 0;
7561failed:
7562 Py_XDECREF(tmp);
7563 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007564}
7565
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007566int
7567obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7568{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007569 PyObject* tmp = NULL;
7570 expr_ty context_expr;
7571 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007572
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007573 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7574 int res;
7575 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7576 if (tmp == NULL) goto failed;
7577 res = obj2ast_expr(tmp, &context_expr, arena);
7578 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007579 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007580 } else {
7581 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007582 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007583 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007584 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007585 int res;
7586 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7587 if (tmp == NULL) goto failed;
7588 res = obj2ast_expr(tmp, &optional_vars, arena);
7589 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007590 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007591 } else {
7592 optional_vars = NULL;
7593 }
7594 *out = withitem(context_expr, optional_vars, arena);
7595 return 0;
7596failed:
7597 Py_XDECREF(tmp);
7598 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007599}
7600
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007601
Martin v. Löwis1a214512008-06-11 05:26:20 +00007602static struct PyModuleDef _astmodule = {
7603 PyModuleDef_HEAD_INIT, "_ast"
7604};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007605PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007606PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007607{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007608 PyObject *m, *d;
7609 if (!init_types()) return NULL;
7610 m = PyModule_Create(&_astmodule);
7611 if (!m) return NULL;
7612 d = PyModule_GetDict(m);
7613 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007614 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007615 return NULL;
7616 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7617 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7618 NULL;
7619 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7620 0) return NULL;
7621 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7622 return NULL;
7623 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7624 NULL;
7625 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7626 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7627 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007628 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7629 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007630 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7631 return NULL;
7632 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7633 NULL;
7634 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7635 NULL;
7636 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7637 NULL;
7638 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7639 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007640 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7641 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007642 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007643 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7644 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007645 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7646 NULL;
7647 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7648 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007649 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7650 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007651 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7652 NULL;
7653 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7654 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7655 NULL;
7656 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7657 NULL;
7658 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7659 return NULL;
7660 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7661 NULL;
7662 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7663 return NULL;
7664 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7665 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7666 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7667 NULL;
7668 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7669 return NULL;
7670 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7671 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7672 NULL;
7673 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7674 NULL;
7675 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7676 NULL;
7677 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7678 NULL;
7679 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7680 NULL;
7681 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7682 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7683 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7684 return NULL;
7685 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7686 NULL;
7687 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7688 return NULL;
7689 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7690 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007691 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7692 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007693 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7694 NULL;
7695 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7696 return NULL;
7697 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7698 NULL;
7699 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7700 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7701 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007702 if (PyDict_SetItemString(d, "FormattedValue",
7703 (PyObject*)FormattedValue_type) < 0) return NULL;
7704 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7705 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007706 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7707 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007708 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7709 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007710 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7711 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007712 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7713 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007714 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7715 return NULL;
7716 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7717 return NULL;
7718 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7719 NULL;
7720 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7721 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7722 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7723 NULL;
7724 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7725 0) return NULL;
7726 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7727 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7728 NULL;
7729 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7730 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7731 NULL;
7732 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7733 return NULL;
7734 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7735 NULL;
7736 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7737 NULL;
7738 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7739 NULL;
7740 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7741 return NULL;
7742 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7743 NULL;
7744 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7745 NULL;
7746 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7747 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7748 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7749 return NULL;
7750 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7751 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7752 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007753 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7754 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007755 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7756 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7757 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7758 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7759 NULL;
7760 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7761 NULL;
7762 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7763 NULL;
7764 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7765 NULL;
7766 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7767 NULL;
7768 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7769 return NULL;
7770 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7771 NULL;
7772 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7773 NULL;
7774 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7775 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7776 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7777 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7778 NULL;
7779 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7780 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7781 NULL;
7782 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7783 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7784 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7785 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7786 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7787 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7788 NULL;
7789 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7790 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7791 NULL;
7792 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7793 < 0) return NULL;
7794 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7795 < 0) return NULL;
7796 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7797 < 0) return NULL;
7798 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7799 return NULL;
7800 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7801 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7802 NULL;
7803 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7804 NULL;
7805 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7806 return NULL;
7807 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007808}
7809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007810
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007811PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007812{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007813 if (!init_types())
7814 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007815 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007816}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007817
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007818/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7819mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007820{
7821 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007822 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007823 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007824 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007825
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007826 req_type[0] = (PyObject*)Module_type;
7827 req_type[1] = (PyObject*)Expression_type;
7828 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007829
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007830 assert(0 <= mode && mode <= 2);
7831
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007832 if (!init_types())
7833 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007834
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007835 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7836 if (isinstance == -1)
7837 return NULL;
7838 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007839 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7840 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007841 return NULL;
7842 }
7843 if (obj2ast_mod(ast, &res, arena) != 0)
7844 return NULL;
7845 else
7846 return res;
7847}
7848
7849int PyAST_Check(PyObject* obj)
7850{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007851 if (!init_types())
7852 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007853 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007854}
7855
Martin v. Löwis5b222132007-06-10 09:51:05 +00007856