blob: e0607ba9ae6d6869b8d355c787c46c6a96a865ea [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);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700438_Py_IDENTIFIER(is_async);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000439static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200440 "target",
441 "iter",
442 "ifs",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700443 "is_async",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000444};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000445static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000446static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200447 "lineno",
448 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000449};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000450static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000451static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200452_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000453static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200454 "type",
455 "name",
456 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000457};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000458static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000459static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200460_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200461_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200462_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700463_Py_IDENTIFIER(kwarg);
464_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000465static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200466 "args",
467 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200468 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200469 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200470 "kwarg",
471 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000472};
Neal Norwitzc1505362006-12-28 06:47:50 +0000473static PyTypeObject *arg_type;
474static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700475static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200476 "lineno",
477 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700478};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200479_Py_IDENTIFIER(arg);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000480static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200481 "arg",
482 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000483};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000484static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000485static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000486static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200487 "arg",
488 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000489};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000490static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000491static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200492_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000493static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200494 "name",
495 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000496};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500497static PyTypeObject *withitem_type;
498static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200499_Py_IDENTIFIER(context_expr);
500_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500501static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200502 "context_expr",
503 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500504};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000505
506
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700507typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100508 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700509 PyObject *dict;
510} AST_object;
511
Benjamin Peterson1767e022012-03-14 21:50:29 -0500512static void
513ast_dealloc(AST_object *self)
514{
515 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200516 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500517}
518
Neal Norwitz207c9f32008-03-31 04:42:11 +0000519static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700520ast_traverse(AST_object *self, visitproc visit, void *arg)
521{
522 Py_VISIT(self->dict);
523 return 0;
524}
525
526static void
527ast_clear(AST_object *self)
528{
529 Py_CLEAR(self->dict);
530}
531
532static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000533ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
534{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200535 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000536 Py_ssize_t i, numfields = 0;
537 int res = -1;
538 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200539 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000540 if (!fields)
541 PyErr_Clear();
542 if (fields) {
543 numfields = PySequence_Size(fields);
544 if (numfields == -1)
545 goto cleanup;
546 }
547 res = 0; /* if no error occurs, this stays 0 to the end */
548 if (PyTuple_GET_SIZE(args) > 0) {
549 if (numfields != PyTuple_GET_SIZE(args)) {
550 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000551 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000552 Py_TYPE(self)->tp_name,
553 numfields == 0 ? "" : "either 0 or ",
554 numfields, numfields == 1 ? "" : "s");
555 res = -1;
556 goto cleanup;
557 }
558 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
559 /* cannot be reached when fields is NULL */
560 PyObject *name = PySequence_GetItem(fields, i);
561 if (!name) {
562 res = -1;
563 goto cleanup;
564 }
565 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
566 Py_DECREF(name);
567 if (res < 0)
568 goto cleanup;
569 }
570 }
571 if (kw) {
572 i = 0; /* needed by PyDict_Next */
573 while (PyDict_Next(kw, &i, &key, &value)) {
574 res = PyObject_SetAttr(self, key, value);
575 if (res < 0)
576 goto cleanup;
577 }
578 }
579 cleanup:
580 Py_XDECREF(fields);
581 return res;
582}
583
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000584/* Pickling support */
585static PyObject *
586ast_type_reduce(PyObject *self, PyObject *unused)
587{
588 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200589 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200590 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000591 if (dict == NULL) {
592 if (PyErr_ExceptionMatches(PyExc_AttributeError))
593 PyErr_Clear();
594 else
595 return NULL;
596 }
597 if (dict) {
598 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
599 Py_DECREF(dict);
600 return res;
601 }
602 return Py_BuildValue("O()", Py_TYPE(self));
603}
604
605static PyMethodDef ast_type_methods[] = {
606 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
607 {NULL}
608};
609
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700610static PyGetSetDef ast_type_getsets[] = {
611 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
612 {NULL}
613};
614
Neal Norwitz207c9f32008-03-31 04:42:11 +0000615static PyTypeObject AST_type = {
616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000617 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700618 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000619 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500620 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000621 0, /* tp_print */
622 0, /* tp_getattr */
623 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000624 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000625 0, /* tp_repr */
626 0, /* tp_as_number */
627 0, /* tp_as_sequence */
628 0, /* tp_as_mapping */
629 0, /* tp_hash */
630 0, /* tp_call */
631 0, /* tp_str */
632 PyObject_GenericGetAttr, /* tp_getattro */
633 PyObject_GenericSetAttr, /* tp_setattro */
634 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000636 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700637 (traverseproc)ast_traverse, /* tp_traverse */
638 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000639 0, /* tp_richcompare */
640 0, /* tp_weaklistoffset */
641 0, /* tp_iter */
642 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000643 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000644 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700645 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000646 0, /* tp_base */
647 0, /* tp_dict */
648 0, /* tp_descr_get */
649 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700650 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000651 (initproc)ast_type_init, /* tp_init */
652 PyType_GenericAlloc, /* tp_alloc */
653 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700654 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000655};
656
657
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
659{
660 PyObject *fnames, *result;
661 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000662 fnames = PyTuple_New(num_fields);
663 if (!fnames) return NULL;
664 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000665 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 if (!field) {
667 Py_DECREF(fnames);
668 return NULL;
669 }
670 PyTuple_SET_ITEM(fnames, i, field);
671 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000672 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Py_DECREF(fnames);
675 return (PyTypeObject*)result;
676}
677
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
679{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200681 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000682 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000683 if (!l)
684 return 0;
685 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000686 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!s) {
688 Py_DECREF(l);
689 return 0;
690 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000691 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200693 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 Py_DECREF(l);
695 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696}
697
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000698/* Conversion AST -> Python */
699
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
701{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700702 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 PyObject *result = PyList_New(n);
704 PyObject *value;
705 if (!result)
706 return NULL;
707 for (i = 0; i < n; i++) {
708 value = func(asdl_seq_GET(seq, i));
709 if (!value) {
710 Py_DECREF(result);
711 return NULL;
712 }
713 PyList_SET_ITEM(result, i, value);
714 }
715 return result;
716}
717
718static PyObject* ast2obj_object(void *o)
719{
720 if (!o)
721 o = Py_None;
722 Py_INCREF((PyObject*)o);
723 return (PyObject*)o;
724}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500725#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100726#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727#define ast2obj_identifier ast2obj_object
728#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500729#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000731static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732{
Christian Heimes217cfd12007-12-02 14:31:20 +0000733 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734}
735
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000736/* Conversion Python -> AST */
737
Benjamin Peterson442f2092012-12-06 17:41:04 -0500738static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
739{
740 if (obj != Py_None && obj != Py_True && obj != Py_False) {
741 PyErr_SetString(PyExc_ValueError,
742 "AST singleton must be True, False, or None");
743 return 1;
744 }
745 *out = obj;
746 return 0;
747}
748
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000749static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
750{
751 if (obj == Py_None)
752 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200753 if (obj) {
754 if (PyArena_AddPyObject(arena, obj) < 0) {
755 *out = NULL;
756 return -1;
757 }
758 Py_INCREF(obj);
759 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000760 *out = obj;
761 return 0;
762}
763
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100764static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
765{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100766 if (obj) {
767 if (PyArena_AddPyObject(arena, obj) < 0) {
768 *out = NULL;
769 return -1;
770 }
771 Py_INCREF(obj);
772 }
773 *out = obj;
774 return 0;
775}
776
Benjamin Peterson180e6352011-07-22 11:09:07 -0500777static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500778{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500779 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
780 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500781 return 1;
782 }
783 return obj2ast_object(obj, out, arena);
784}
785
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500786static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
787{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400788 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500789 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
790 return 1;
791 }
792 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500793}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000794
Benjamin Petersone2498412011-08-09 16:08:39 -0500795static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
796{
797 if (!PyBytes_CheckExact(obj)) {
798 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
799 return 1;
800 }
801 return obj2ast_object(obj, out, arena);
802}
803
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000804static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
805{
806 int i;
807 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100808 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809 return 1;
810 }
811
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300812 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000813 if (i == -1 && PyErr_Occurred())
814 return 1;
815 *out = i;
816 return 0;
817}
818
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000819static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000820{
821 PyObject *empty_tuple, *d;
822 if (PyType_Ready(&AST_type) < 0)
823 return -1;
824 d = AST_type.tp_dict;
825 empty_tuple = PyTuple_New(0);
826 if (!empty_tuple ||
827 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
828 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
829 Py_XDECREF(empty_tuple);
830 return -1;
831 }
832 Py_DECREF(empty_tuple);
833 return 0;
834}
835
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700836static int exists_not_none(PyObject *obj, _Py_Identifier *id)
837{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700838 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700839 PyObject *attr = _PyObject_GetAttrId(obj, id);
840 if (!attr) {
841 PyErr_Clear();
842 return 0;
843 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700844 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700845 Py_DECREF(attr);
846 return !isnone;
847}
848
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000849
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000850static int init_types(void)
851{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200852 static int initialized;
853 if (initialized) return 1;
854 if (add_ast_fields() < 0) return 0;
855 mod_type = make_type("mod", &AST_type, NULL, 0);
856 if (!mod_type) return 0;
857 if (!add_attributes(mod_type, NULL, 0)) return 0;
858 Module_type = make_type("Module", mod_type, Module_fields, 1);
859 if (!Module_type) return 0;
860 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
861 1);
862 if (!Interactive_type) return 0;
863 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
864 if (!Expression_type) return 0;
865 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
866 if (!Suite_type) return 0;
867 stmt_type = make_type("stmt", &AST_type, NULL, 0);
868 if (!stmt_type) return 0;
869 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
870 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
871 5);
872 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400873 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
874 AsyncFunctionDef_fields, 5);
875 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400876 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200877 if (!ClassDef_type) return 0;
878 Return_type = make_type("Return", stmt_type, Return_fields, 1);
879 if (!Return_type) return 0;
880 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
881 if (!Delete_type) return 0;
882 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
883 if (!Assign_type) return 0;
884 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
885 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700886 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
887 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200888 For_type = make_type("For", stmt_type, For_fields, 4);
889 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400890 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
891 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200892 While_type = make_type("While", stmt_type, While_fields, 3);
893 if (!While_type) return 0;
894 If_type = make_type("If", stmt_type, If_fields, 3);
895 if (!If_type) return 0;
896 With_type = make_type("With", stmt_type, With_fields, 2);
897 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400898 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
899 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200900 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
901 if (!Raise_type) return 0;
902 Try_type = make_type("Try", stmt_type, Try_fields, 4);
903 if (!Try_type) return 0;
904 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
905 if (!Assert_type) return 0;
906 Import_type = make_type("Import", stmt_type, Import_fields, 1);
907 if (!Import_type) return 0;
908 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
909 if (!ImportFrom_type) return 0;
910 Global_type = make_type("Global", stmt_type, Global_fields, 1);
911 if (!Global_type) return 0;
912 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
913 if (!Nonlocal_type) return 0;
914 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
915 if (!Expr_type) return 0;
916 Pass_type = make_type("Pass", stmt_type, NULL, 0);
917 if (!Pass_type) return 0;
918 Break_type = make_type("Break", stmt_type, NULL, 0);
919 if (!Break_type) return 0;
920 Continue_type = make_type("Continue", stmt_type, NULL, 0);
921 if (!Continue_type) return 0;
922 expr_type = make_type("expr", &AST_type, NULL, 0);
923 if (!expr_type) return 0;
924 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
925 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
926 if (!BoolOp_type) return 0;
927 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
928 if (!BinOp_type) return 0;
929 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
930 if (!UnaryOp_type) return 0;
931 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
932 if (!Lambda_type) return 0;
933 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
934 if (!IfExp_type) return 0;
935 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
936 if (!Dict_type) return 0;
937 Set_type = make_type("Set", expr_type, Set_fields, 1);
938 if (!Set_type) return 0;
939 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
940 if (!ListComp_type) return 0;
941 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
942 if (!SetComp_type) return 0;
943 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
944 if (!DictComp_type) return 0;
945 GeneratorExp_type = make_type("GeneratorExp", expr_type,
946 GeneratorExp_fields, 2);
947 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400948 Await_type = make_type("Await", expr_type, Await_fields, 1);
949 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200950 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
951 if (!Yield_type) return 0;
952 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
953 if (!YieldFrom_type) return 0;
954 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
955 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400956 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200957 if (!Call_type) return 0;
958 Num_type = make_type("Num", expr_type, Num_fields, 1);
959 if (!Num_type) return 0;
960 Str_type = make_type("Str", expr_type, Str_fields, 1);
961 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400962 FormattedValue_type = make_type("FormattedValue", expr_type,
963 FormattedValue_fields, 3);
964 if (!FormattedValue_type) return 0;
965 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
966 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200967 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
968 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200969 NameConstant_type = make_type("NameConstant", expr_type,
970 NameConstant_fields, 1);
971 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200972 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
973 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100974 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
975 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200976 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
977 if (!Attribute_type) return 0;
978 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
979 if (!Subscript_type) return 0;
980 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
981 if (!Starred_type) return 0;
982 Name_type = make_type("Name", expr_type, Name_fields, 2);
983 if (!Name_type) return 0;
984 List_type = make_type("List", expr_type, List_fields, 2);
985 if (!List_type) return 0;
986 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
987 if (!Tuple_type) return 0;
988 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
989 if (!expr_context_type) return 0;
990 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
991 Load_type = make_type("Load", expr_context_type, NULL, 0);
992 if (!Load_type) return 0;
993 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
994 if (!Load_singleton) return 0;
995 Store_type = make_type("Store", expr_context_type, NULL, 0);
996 if (!Store_type) return 0;
997 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
998 if (!Store_singleton) return 0;
999 Del_type = make_type("Del", expr_context_type, NULL, 0);
1000 if (!Del_type) return 0;
1001 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1002 if (!Del_singleton) return 0;
1003 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1004 if (!AugLoad_type) return 0;
1005 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1006 if (!AugLoad_singleton) return 0;
1007 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1008 if (!AugStore_type) return 0;
1009 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1010 if (!AugStore_singleton) return 0;
1011 Param_type = make_type("Param", expr_context_type, NULL, 0);
1012 if (!Param_type) return 0;
1013 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1014 if (!Param_singleton) return 0;
1015 slice_type = make_type("slice", &AST_type, NULL, 0);
1016 if (!slice_type) return 0;
1017 if (!add_attributes(slice_type, NULL, 0)) return 0;
1018 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1019 if (!Slice_type) return 0;
1020 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1021 if (!ExtSlice_type) return 0;
1022 Index_type = make_type("Index", slice_type, Index_fields, 1);
1023 if (!Index_type) return 0;
1024 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1025 if (!boolop_type) return 0;
1026 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1027 And_type = make_type("And", boolop_type, NULL, 0);
1028 if (!And_type) return 0;
1029 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1030 if (!And_singleton) return 0;
1031 Or_type = make_type("Or", boolop_type, NULL, 0);
1032 if (!Or_type) return 0;
1033 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1034 if (!Or_singleton) return 0;
1035 operator_type = make_type("operator", &AST_type, NULL, 0);
1036 if (!operator_type) return 0;
1037 if (!add_attributes(operator_type, NULL, 0)) return 0;
1038 Add_type = make_type("Add", operator_type, NULL, 0);
1039 if (!Add_type) return 0;
1040 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1041 if (!Add_singleton) return 0;
1042 Sub_type = make_type("Sub", operator_type, NULL, 0);
1043 if (!Sub_type) return 0;
1044 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1045 if (!Sub_singleton) return 0;
1046 Mult_type = make_type("Mult", operator_type, NULL, 0);
1047 if (!Mult_type) return 0;
1048 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1049 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001050 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1051 if (!MatMult_type) return 0;
1052 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1053 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001054 Div_type = make_type("Div", operator_type, NULL, 0);
1055 if (!Div_type) return 0;
1056 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1057 if (!Div_singleton) return 0;
1058 Mod_type = make_type("Mod", operator_type, NULL, 0);
1059 if (!Mod_type) return 0;
1060 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1061 if (!Mod_singleton) return 0;
1062 Pow_type = make_type("Pow", operator_type, NULL, 0);
1063 if (!Pow_type) return 0;
1064 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1065 if (!Pow_singleton) return 0;
1066 LShift_type = make_type("LShift", operator_type, NULL, 0);
1067 if (!LShift_type) return 0;
1068 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1069 if (!LShift_singleton) return 0;
1070 RShift_type = make_type("RShift", operator_type, NULL, 0);
1071 if (!RShift_type) return 0;
1072 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1073 if (!RShift_singleton) return 0;
1074 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1075 if (!BitOr_type) return 0;
1076 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1077 if (!BitOr_singleton) return 0;
1078 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1079 if (!BitXor_type) return 0;
1080 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1081 if (!BitXor_singleton) return 0;
1082 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1083 if (!BitAnd_type) return 0;
1084 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1085 if (!BitAnd_singleton) return 0;
1086 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1087 if (!FloorDiv_type) return 0;
1088 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1089 if (!FloorDiv_singleton) return 0;
1090 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1091 if (!unaryop_type) return 0;
1092 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1093 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1094 if (!Invert_type) return 0;
1095 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1096 if (!Invert_singleton) return 0;
1097 Not_type = make_type("Not", unaryop_type, NULL, 0);
1098 if (!Not_type) return 0;
1099 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1100 if (!Not_singleton) return 0;
1101 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1102 if (!UAdd_type) return 0;
1103 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1104 if (!UAdd_singleton) return 0;
1105 USub_type = make_type("USub", unaryop_type, NULL, 0);
1106 if (!USub_type) return 0;
1107 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1108 if (!USub_singleton) return 0;
1109 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1110 if (!cmpop_type) return 0;
1111 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1112 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1113 if (!Eq_type) return 0;
1114 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1115 if (!Eq_singleton) return 0;
1116 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1117 if (!NotEq_type) return 0;
1118 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1119 if (!NotEq_singleton) return 0;
1120 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1121 if (!Lt_type) return 0;
1122 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1123 if (!Lt_singleton) return 0;
1124 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1125 if (!LtE_type) return 0;
1126 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1127 if (!LtE_singleton) return 0;
1128 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1129 if (!Gt_type) return 0;
1130 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1131 if (!Gt_singleton) return 0;
1132 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1133 if (!GtE_type) return 0;
1134 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1135 if (!GtE_singleton) return 0;
1136 Is_type = make_type("Is", cmpop_type, NULL, 0);
1137 if (!Is_type) return 0;
1138 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1139 if (!Is_singleton) return 0;
1140 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1141 if (!IsNot_type) return 0;
1142 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1143 if (!IsNot_singleton) return 0;
1144 In_type = make_type("In", cmpop_type, NULL, 0);
1145 if (!In_type) return 0;
1146 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1147 if (!In_singleton) return 0;
1148 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1149 if (!NotIn_type) return 0;
1150 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1151 if (!NotIn_singleton) return 0;
1152 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001153 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001155 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001156 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1157 if (!excepthandler_type) return 0;
1158 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1159 return 0;
1160 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1161 ExceptHandler_fields, 3);
1162 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001163 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001164 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001165 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001166 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1167 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001168 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001169 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1170 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001171 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001172 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1173 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001174 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1176 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001177 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001178 initialized = 1;
1179 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001180}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001181
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001182static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1183static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1184static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1185static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1186 arena);
1187static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1188static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1189static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1190static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1191static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1192static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1193 arena);
1194static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1195 arena);
1196static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1197static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1198static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1199static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001200static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001205 mod_ty p;
1206 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1207 if (!p)
1208 return NULL;
1209 p->kind = Module_kind;
1210 p->v.Module.body = body;
1211 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212}
1213
1214mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001217 mod_ty p;
1218 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1219 if (!p)
1220 return NULL;
1221 p->kind = Interactive_kind;
1222 p->v.Interactive.body = body;
1223 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224}
1225
1226mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001229 mod_ty p;
1230 if (!body) {
1231 PyErr_SetString(PyExc_ValueError,
1232 "field body is required for Expression");
1233 return NULL;
1234 }
1235 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1236 if (!p)
1237 return NULL;
1238 p->kind = Expression_kind;
1239 p->v.Expression.body = body;
1240 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
1243mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001246 mod_ty p;
1247 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1248 if (!p)
1249 return NULL;
1250 p->kind = Suite_kind;
1251 p->v.Suite.body = body;
1252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255stmt_ty
1256FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001257 decorator_list, expr_ty returns, int lineno, int col_offset,
1258 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001260 stmt_ty p;
1261 if (!name) {
1262 PyErr_SetString(PyExc_ValueError,
1263 "field name is required for FunctionDef");
1264 return NULL;
1265 }
1266 if (!args) {
1267 PyErr_SetString(PyExc_ValueError,
1268 "field args is required for FunctionDef");
1269 return NULL;
1270 }
1271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1272 if (!p)
1273 return NULL;
1274 p->kind = FunctionDef_kind;
1275 p->v.FunctionDef.name = name;
1276 p->v.FunctionDef.args = args;
1277 p->v.FunctionDef.body = body;
1278 p->v.FunctionDef.decorator_list = decorator_list;
1279 p->v.FunctionDef.returns = returns;
1280 p->lineno = lineno;
1281 p->col_offset = col_offset;
1282 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283}
1284
1285stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001286AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1287 * decorator_list, expr_ty returns, int lineno, int col_offset,
1288 PyArena *arena)
1289{
1290 stmt_ty p;
1291 if (!name) {
1292 PyErr_SetString(PyExc_ValueError,
1293 "field name is required for AsyncFunctionDef");
1294 return NULL;
1295 }
1296 if (!args) {
1297 PyErr_SetString(PyExc_ValueError,
1298 "field args is required for AsyncFunctionDef");
1299 return NULL;
1300 }
1301 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1302 if (!p)
1303 return NULL;
1304 p->kind = AsyncFunctionDef_kind;
1305 p->v.AsyncFunctionDef.name = name;
1306 p->v.AsyncFunctionDef.args = args;
1307 p->v.AsyncFunctionDef.body = body;
1308 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1309 p->v.AsyncFunctionDef.returns = returns;
1310 p->lineno = lineno;
1311 p->col_offset = col_offset;
1312 return p;
1313}
1314
1315stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001316ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1317 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1318 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001320 stmt_ty p;
1321 if (!name) {
1322 PyErr_SetString(PyExc_ValueError,
1323 "field name is required for ClassDef");
1324 return NULL;
1325 }
1326 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1327 if (!p)
1328 return NULL;
1329 p->kind = ClassDef_kind;
1330 p->v.ClassDef.name = name;
1331 p->v.ClassDef.bases = bases;
1332 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001333 p->v.ClassDef.body = body;
1334 p->v.ClassDef.decorator_list = decorator_list;
1335 p->lineno = lineno;
1336 p->col_offset = col_offset;
1337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001343 stmt_ty p;
1344 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1345 if (!p)
1346 return NULL;
1347 p->kind = Return_kind;
1348 p->v.Return.value = value;
1349 p->lineno = lineno;
1350 p->col_offset = col_offset;
1351 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352}
1353
1354stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001355Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001357 stmt_ty p;
1358 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1359 if (!p)
1360 return NULL;
1361 p->kind = Delete_kind;
1362 p->v.Delete.targets = targets;
1363 p->lineno = lineno;
1364 p->col_offset = col_offset;
1365 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366}
1367
1368stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001369Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1370 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001372 stmt_ty p;
1373 if (!value) {
1374 PyErr_SetString(PyExc_ValueError,
1375 "field value is required for Assign");
1376 return NULL;
1377 }
1378 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1379 if (!p)
1380 return NULL;
1381 p->kind = Assign_kind;
1382 p->v.Assign.targets = targets;
1383 p->v.Assign.value = value;
1384 p->lineno = lineno;
1385 p->col_offset = col_offset;
1386 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387}
1388
1389stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001390AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1391 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001393 stmt_ty p;
1394 if (!target) {
1395 PyErr_SetString(PyExc_ValueError,
1396 "field target is required for AugAssign");
1397 return NULL;
1398 }
1399 if (!op) {
1400 PyErr_SetString(PyExc_ValueError,
1401 "field op is required for AugAssign");
1402 return NULL;
1403 }
1404 if (!value) {
1405 PyErr_SetString(PyExc_ValueError,
1406 "field value is required for AugAssign");
1407 return NULL;
1408 }
1409 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1410 if (!p)
1411 return NULL;
1412 p->kind = AugAssign_kind;
1413 p->v.AugAssign.target = target;
1414 p->v.AugAssign.op = op;
1415 p->v.AugAssign.value = value;
1416 p->lineno = lineno;
1417 p->col_offset = col_offset;
1418 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419}
1420
1421stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001422AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1423 lineno, int col_offset, PyArena *arena)
1424{
1425 stmt_ty p;
1426 if (!target) {
1427 PyErr_SetString(PyExc_ValueError,
1428 "field target is required for AnnAssign");
1429 return NULL;
1430 }
1431 if (!annotation) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "field annotation is required for AnnAssign");
1434 return NULL;
1435 }
1436 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1437 if (!p)
1438 return NULL;
1439 p->kind = AnnAssign_kind;
1440 p->v.AnnAssign.target = target;
1441 p->v.AnnAssign.annotation = annotation;
1442 p->v.AnnAssign.value = value;
1443 p->v.AnnAssign.simple = simple;
1444 p->lineno = lineno;
1445 p->col_offset = col_offset;
1446 return p;
1447}
1448
1449stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001451 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001453 stmt_ty p;
1454 if (!target) {
1455 PyErr_SetString(PyExc_ValueError,
1456 "field target is required for For");
1457 return NULL;
1458 }
1459 if (!iter) {
1460 PyErr_SetString(PyExc_ValueError,
1461 "field iter is required for For");
1462 return NULL;
1463 }
1464 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1465 if (!p)
1466 return NULL;
1467 p->kind = For_kind;
1468 p->v.For.target = target;
1469 p->v.For.iter = iter;
1470 p->v.For.body = body;
1471 p->v.For.orelse = orelse;
1472 p->lineno = lineno;
1473 p->col_offset = col_offset;
1474 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
1477stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001478AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1479 lineno, int col_offset, PyArena *arena)
1480{
1481 stmt_ty p;
1482 if (!target) {
1483 PyErr_SetString(PyExc_ValueError,
1484 "field target is required for AsyncFor");
1485 return NULL;
1486 }
1487 if (!iter) {
1488 PyErr_SetString(PyExc_ValueError,
1489 "field iter is required for AsyncFor");
1490 return NULL;
1491 }
1492 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1493 if (!p)
1494 return NULL;
1495 p->kind = AsyncFor_kind;
1496 p->v.AsyncFor.target = target;
1497 p->v.AsyncFor.iter = iter;
1498 p->v.AsyncFor.body = body;
1499 p->v.AsyncFor.orelse = orelse;
1500 p->lineno = lineno;
1501 p->col_offset = col_offset;
1502 return p;
1503}
1504
1505stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1507 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001509 stmt_ty p;
1510 if (!test) {
1511 PyErr_SetString(PyExc_ValueError,
1512 "field test is required for While");
1513 return NULL;
1514 }
1515 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1516 if (!p)
1517 return NULL;
1518 p->kind = While_kind;
1519 p->v.While.test = test;
1520 p->v.While.body = body;
1521 p->v.While.orelse = orelse;
1522 p->lineno = lineno;
1523 p->col_offset = col_offset;
1524 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001528If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1529 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001531 stmt_ty p;
1532 if (!test) {
1533 PyErr_SetString(PyExc_ValueError,
1534 "field test is required for If");
1535 return NULL;
1536 }
1537 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1538 if (!p)
1539 return NULL;
1540 p->kind = If_kind;
1541 p->v.If.test = test;
1542 p->v.If.body = body;
1543 p->v.If.orelse = orelse;
1544 p->lineno = lineno;
1545 p->col_offset = col_offset;
1546 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001550With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1551 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001552{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001553 stmt_ty p;
1554 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1555 if (!p)
1556 return NULL;
1557 p->kind = With_kind;
1558 p->v.With.items = items;
1559 p->v.With.body = body;
1560 p->lineno = lineno;
1561 p->col_offset = col_offset;
1562 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001563}
1564
1565stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001566AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1567 PyArena *arena)
1568{
1569 stmt_ty p;
1570 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1571 if (!p)
1572 return NULL;
1573 p->kind = AsyncWith_kind;
1574 p->v.AsyncWith.items = items;
1575 p->v.AsyncWith.body = body;
1576 p->lineno = lineno;
1577 p->col_offset = col_offset;
1578 return p;
1579}
1580
1581stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001582Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001584 stmt_ty p;
1585 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1586 if (!p)
1587 return NULL;
1588 p->kind = Raise_kind;
1589 p->v.Raise.exc = exc;
1590 p->v.Raise.cause = cause;
1591 p->lineno = lineno;
1592 p->col_offset = col_offset;
1593 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001597Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1598 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001600 stmt_ty p;
1601 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1602 if (!p)
1603 return NULL;
1604 p->kind = Try_kind;
1605 p->v.Try.body = body;
1606 p->v.Try.handlers = handlers;
1607 p->v.Try.orelse = orelse;
1608 p->v.Try.finalbody = finalbody;
1609 p->lineno = lineno;
1610 p->col_offset = col_offset;
1611 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612}
1613
1614stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001615Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001617 stmt_ty p;
1618 if (!test) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "field test is required for Assert");
1621 return NULL;
1622 }
1623 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1624 if (!p)
1625 return NULL;
1626 p->kind = Assert_kind;
1627 p->v.Assert.test = test;
1628 p->v.Assert.msg = msg;
1629 p->lineno = lineno;
1630 p->col_offset = col_offset;
1631 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632}
1633
1634stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001637 stmt_ty p;
1638 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = Import_kind;
1642 p->v.Import.names = names;
1643 p->lineno = lineno;
1644 p->col_offset = col_offset;
1645 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1650 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001652 stmt_ty p;
1653 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1654 if (!p)
1655 return NULL;
1656 p->kind = ImportFrom_kind;
1657 p->v.ImportFrom.module = module;
1658 p->v.ImportFrom.names = names;
1659 p->v.ImportFrom.level = level;
1660 p->lineno = lineno;
1661 p->col_offset = col_offset;
1662 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
1665stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001668 stmt_ty p;
1669 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1670 if (!p)
1671 return NULL;
1672 p->kind = Global_kind;
1673 p->v.Global.names = names;
1674 p->lineno = lineno;
1675 p->col_offset = col_offset;
1676 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
1679stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001680Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1681{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001682 stmt_ty p;
1683 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1684 if (!p)
1685 return NULL;
1686 p->kind = Nonlocal_kind;
1687 p->v.Nonlocal.names = names;
1688 p->lineno = lineno;
1689 p->col_offset = col_offset;
1690 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001691}
1692
1693stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001696 stmt_ty p;
1697 if (!value) {
1698 PyErr_SetString(PyExc_ValueError,
1699 "field value is required for Expr");
1700 return NULL;
1701 }
1702 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1703 if (!p)
1704 return NULL;
1705 p->kind = Expr_kind;
1706 p->v.Expr.value = value;
1707 p->lineno = lineno;
1708 p->col_offset = col_offset;
1709 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710}
1711
1712stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001715 stmt_ty p;
1716 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1717 if (!p)
1718 return NULL;
1719 p->kind = Pass_kind;
1720 p->lineno = lineno;
1721 p->col_offset = col_offset;
1722 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723}
1724
1725stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001726Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001728 stmt_ty p;
1729 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1730 if (!p)
1731 return NULL;
1732 p->kind = Break_kind;
1733 p->lineno = lineno;
1734 p->col_offset = col_offset;
1735 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736}
1737
1738stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001739Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001741 stmt_ty p;
1742 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1743 if (!p)
1744 return NULL;
1745 p->kind = Continue_kind;
1746 p->lineno = lineno;
1747 p->col_offset = col_offset;
1748 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1753 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001755 expr_ty p;
1756 if (!op) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field op is required for BoolOp");
1759 return NULL;
1760 }
1761 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1762 if (!p)
1763 return NULL;
1764 p->kind = BoolOp_kind;
1765 p->v.BoolOp.op = op;
1766 p->v.BoolOp.values = values;
1767 p->lineno = lineno;
1768 p->col_offset = col_offset;
1769 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770}
1771
1772expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1774 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001776 expr_ty p;
1777 if (!left) {
1778 PyErr_SetString(PyExc_ValueError,
1779 "field left is required for BinOp");
1780 return NULL;
1781 }
1782 if (!op) {
1783 PyErr_SetString(PyExc_ValueError,
1784 "field op is required for BinOp");
1785 return NULL;
1786 }
1787 if (!right) {
1788 PyErr_SetString(PyExc_ValueError,
1789 "field right is required for BinOp");
1790 return NULL;
1791 }
1792 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1793 if (!p)
1794 return NULL;
1795 p->kind = BinOp_kind;
1796 p->v.BinOp.left = left;
1797 p->v.BinOp.op = op;
1798 p->v.BinOp.right = right;
1799 p->lineno = lineno;
1800 p->col_offset = col_offset;
1801 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
1804expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001805UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1806 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001808 expr_ty p;
1809 if (!op) {
1810 PyErr_SetString(PyExc_ValueError,
1811 "field op is required for UnaryOp");
1812 return NULL;
1813 }
1814 if (!operand) {
1815 PyErr_SetString(PyExc_ValueError,
1816 "field operand is required for UnaryOp");
1817 return NULL;
1818 }
1819 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1820 if (!p)
1821 return NULL;
1822 p->kind = UnaryOp_kind;
1823 p->v.UnaryOp.op = op;
1824 p->v.UnaryOp.operand = operand;
1825 p->lineno = lineno;
1826 p->col_offset = col_offset;
1827 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828}
1829
1830expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001831Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1832 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001834 expr_ty p;
1835 if (!args) {
1836 PyErr_SetString(PyExc_ValueError,
1837 "field args is required for Lambda");
1838 return NULL;
1839 }
1840 if (!body) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field body is required for Lambda");
1843 return NULL;
1844 }
1845 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1846 if (!p)
1847 return NULL;
1848 p->kind = Lambda_kind;
1849 p->v.Lambda.args = args;
1850 p->v.Lambda.body = body;
1851 p->lineno = lineno;
1852 p->col_offset = col_offset;
1853 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854}
1855
1856expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001857IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1858 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001859{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001860 expr_ty p;
1861 if (!test) {
1862 PyErr_SetString(PyExc_ValueError,
1863 "field test is required for IfExp");
1864 return NULL;
1865 }
1866 if (!body) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "field body is required for IfExp");
1869 return NULL;
1870 }
1871 if (!orelse) {
1872 PyErr_SetString(PyExc_ValueError,
1873 "field orelse is required for IfExp");
1874 return NULL;
1875 }
1876 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1877 if (!p)
1878 return NULL;
1879 p->kind = IfExp_kind;
1880 p->v.IfExp.test = test;
1881 p->v.IfExp.body = body;
1882 p->v.IfExp.orelse = orelse;
1883 p->lineno = lineno;
1884 p->col_offset = col_offset;
1885 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001886}
1887
1888expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001889Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1890 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001892 expr_ty p;
1893 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1894 if (!p)
1895 return NULL;
1896 p->kind = Dict_kind;
1897 p->v.Dict.keys = keys;
1898 p->v.Dict.values = values;
1899 p->lineno = lineno;
1900 p->col_offset = col_offset;
1901 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902}
1903
1904expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001905Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1906{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001907 expr_ty p;
1908 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1909 if (!p)
1910 return NULL;
1911 p->kind = Set_kind;
1912 p->v.Set.elts = elts;
1913 p->lineno = lineno;
1914 p->col_offset = col_offset;
1915 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001916}
1917
1918expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001919ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1920 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001922 expr_ty p;
1923 if (!elt) {
1924 PyErr_SetString(PyExc_ValueError,
1925 "field elt is required for ListComp");
1926 return NULL;
1927 }
1928 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1929 if (!p)
1930 return NULL;
1931 p->kind = ListComp_kind;
1932 p->v.ListComp.elt = elt;
1933 p->v.ListComp.generators = generators;
1934 p->lineno = lineno;
1935 p->col_offset = col_offset;
1936 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937}
1938
1939expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1941 *arena)
1942{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001943 expr_ty p;
1944 if (!elt) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field elt is required for SetComp");
1947 return NULL;
1948 }
1949 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1950 if (!p)
1951 return NULL;
1952 p->kind = SetComp_kind;
1953 p->v.SetComp.elt = elt;
1954 p->v.SetComp.generators = generators;
1955 p->lineno = lineno;
1956 p->col_offset = col_offset;
1957 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958}
1959
1960expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1962 col_offset, PyArena *arena)
1963{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001964 expr_ty p;
1965 if (!key) {
1966 PyErr_SetString(PyExc_ValueError,
1967 "field key is required for DictComp");
1968 return NULL;
1969 }
1970 if (!value) {
1971 PyErr_SetString(PyExc_ValueError,
1972 "field value is required for DictComp");
1973 return NULL;
1974 }
1975 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1976 if (!p)
1977 return NULL;
1978 p->kind = DictComp_kind;
1979 p->v.DictComp.key = key;
1980 p->v.DictComp.value = value;
1981 p->v.DictComp.generators = generators;
1982 p->lineno = lineno;
1983 p->col_offset = col_offset;
1984 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001985}
1986
1987expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001988GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1989 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001991 expr_ty p;
1992 if (!elt) {
1993 PyErr_SetString(PyExc_ValueError,
1994 "field elt is required for GeneratorExp");
1995 return NULL;
1996 }
1997 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1998 if (!p)
1999 return NULL;
2000 p->kind = GeneratorExp_kind;
2001 p->v.GeneratorExp.elt = elt;
2002 p->v.GeneratorExp.generators = generators;
2003 p->lineno = lineno;
2004 p->col_offset = col_offset;
2005 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006}
2007
2008expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002009Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2010{
2011 expr_ty p;
2012 if (!value) {
2013 PyErr_SetString(PyExc_ValueError,
2014 "field value is required for Await");
2015 return NULL;
2016 }
2017 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2018 if (!p)
2019 return NULL;
2020 p->kind = Await_kind;
2021 p->v.Await.value = value;
2022 p->lineno = lineno;
2023 p->col_offset = col_offset;
2024 return p;
2025}
2026
2027expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002028Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002030 expr_ty p;
2031 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2032 if (!p)
2033 return NULL;
2034 p->kind = Yield_kind;
2035 p->v.Yield.value = value;
2036 p->lineno = lineno;
2037 p->col_offset = col_offset;
2038 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
2041expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002042YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2043{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002044 expr_ty p;
2045 if (!value) {
2046 PyErr_SetString(PyExc_ValueError,
2047 "field value is required for YieldFrom");
2048 return NULL;
2049 }
2050 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2051 if (!p)
2052 return NULL;
2053 p->kind = YieldFrom_kind;
2054 p->v.YieldFrom.value = value;
2055 p->lineno = lineno;
2056 p->col_offset = col_offset;
2057 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002058}
2059
2060expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002061Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2062 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002064 expr_ty p;
2065 if (!left) {
2066 PyErr_SetString(PyExc_ValueError,
2067 "field left is required for Compare");
2068 return NULL;
2069 }
2070 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2071 if (!p)
2072 return NULL;
2073 p->kind = Compare_kind;
2074 p->v.Compare.left = left;
2075 p->v.Compare.ops = ops;
2076 p->v.Compare.comparators = comparators;
2077 p->lineno = lineno;
2078 p->col_offset = col_offset;
2079 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002083Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2084 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002086 expr_ty p;
2087 if (!func) {
2088 PyErr_SetString(PyExc_ValueError,
2089 "field func is required for Call");
2090 return NULL;
2091 }
2092 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2093 if (!p)
2094 return NULL;
2095 p->kind = Call_kind;
2096 p->v.Call.func = func;
2097 p->v.Call.args = args;
2098 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002099 p->lineno = lineno;
2100 p->col_offset = col_offset;
2101 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102}
2103
2104expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002105Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002107 expr_ty p;
2108 if (!n) {
2109 PyErr_SetString(PyExc_ValueError,
2110 "field n is required for Num");
2111 return NULL;
2112 }
2113 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2114 if (!p)
2115 return NULL;
2116 p->kind = Num_kind;
2117 p->v.Num.n = n;
2118 p->lineno = lineno;
2119 p->col_offset = col_offset;
2120 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121}
2122
2123expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002124Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002126 expr_ty p;
2127 if (!s) {
2128 PyErr_SetString(PyExc_ValueError,
2129 "field s is required for Str");
2130 return NULL;
2131 }
2132 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2133 if (!p)
2134 return NULL;
2135 p->kind = Str_kind;
2136 p->v.Str.s = s;
2137 p->lineno = lineno;
2138 p->col_offset = col_offset;
2139 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140}
2141
2142expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002143FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2144 int col_offset, PyArena *arena)
2145{
2146 expr_ty p;
2147 if (!value) {
2148 PyErr_SetString(PyExc_ValueError,
2149 "field value is required for FormattedValue");
2150 return NULL;
2151 }
2152 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2153 if (!p)
2154 return NULL;
2155 p->kind = FormattedValue_kind;
2156 p->v.FormattedValue.value = value;
2157 p->v.FormattedValue.conversion = conversion;
2158 p->v.FormattedValue.format_spec = format_spec;
2159 p->lineno = lineno;
2160 p->col_offset = col_offset;
2161 return p;
2162}
2163
2164expr_ty
2165JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2166{
2167 expr_ty p;
2168 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2169 if (!p)
2170 return NULL;
2171 p->kind = JoinedStr_kind;
2172 p->v.JoinedStr.values = values;
2173 p->lineno = lineno;
2174 p->col_offset = col_offset;
2175 return p;
2176}
2177
2178expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002179Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002180{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002181 expr_ty p;
2182 if (!s) {
2183 PyErr_SetString(PyExc_ValueError,
2184 "field s is required for Bytes");
2185 return NULL;
2186 }
2187 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2188 if (!p)
2189 return NULL;
2190 p->kind = Bytes_kind;
2191 p->v.Bytes.s = s;
2192 p->lineno = lineno;
2193 p->col_offset = col_offset;
2194 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002195}
2196
2197expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002198NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2199{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002200 expr_ty p;
2201 if (!value) {
2202 PyErr_SetString(PyExc_ValueError,
2203 "field value is required for NameConstant");
2204 return NULL;
2205 }
2206 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2207 if (!p)
2208 return NULL;
2209 p->kind = NameConstant_kind;
2210 p->v.NameConstant.value = value;
2211 p->lineno = lineno;
2212 p->col_offset = col_offset;
2213 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002214}
2215
2216expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002217Ellipsis(int lineno, int col_offset, PyArena *arena)
2218{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002219 expr_ty p;
2220 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2221 if (!p)
2222 return NULL;
2223 p->kind = Ellipsis_kind;
2224 p->lineno = lineno;
2225 p->col_offset = col_offset;
2226 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002227}
2228
2229expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002230Constant(constant value, int lineno, int col_offset, PyArena *arena)
2231{
2232 expr_ty p;
2233 if (!value) {
2234 PyErr_SetString(PyExc_ValueError,
2235 "field value is required for Constant");
2236 return NULL;
2237 }
2238 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2239 if (!p)
2240 return NULL;
2241 p->kind = Constant_kind;
2242 p->v.Constant.value = value;
2243 p->lineno = lineno;
2244 p->col_offset = col_offset;
2245 return p;
2246}
2247
2248expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002249Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2250 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002252 expr_ty p;
2253 if (!value) {
2254 PyErr_SetString(PyExc_ValueError,
2255 "field value is required for Attribute");
2256 return NULL;
2257 }
2258 if (!attr) {
2259 PyErr_SetString(PyExc_ValueError,
2260 "field attr is required for Attribute");
2261 return NULL;
2262 }
2263 if (!ctx) {
2264 PyErr_SetString(PyExc_ValueError,
2265 "field ctx is required for Attribute");
2266 return NULL;
2267 }
2268 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2269 if (!p)
2270 return NULL;
2271 p->kind = Attribute_kind;
2272 p->v.Attribute.value = value;
2273 p->v.Attribute.attr = attr;
2274 p->v.Attribute.ctx = ctx;
2275 p->lineno = lineno;
2276 p->col_offset = col_offset;
2277 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278}
2279
2280expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002281Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2282 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002284 expr_ty p;
2285 if (!value) {
2286 PyErr_SetString(PyExc_ValueError,
2287 "field value is required for Subscript");
2288 return NULL;
2289 }
2290 if (!slice) {
2291 PyErr_SetString(PyExc_ValueError,
2292 "field slice is required for Subscript");
2293 return NULL;
2294 }
2295 if (!ctx) {
2296 PyErr_SetString(PyExc_ValueError,
2297 "field ctx is required for Subscript");
2298 return NULL;
2299 }
2300 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2301 if (!p)
2302 return NULL;
2303 p->kind = Subscript_kind;
2304 p->v.Subscript.value = value;
2305 p->v.Subscript.slice = slice;
2306 p->v.Subscript.ctx = ctx;
2307 p->lineno = lineno;
2308 p->col_offset = col_offset;
2309 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310}
2311
2312expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002313Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2314 *arena)
2315{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002316 expr_ty p;
2317 if (!value) {
2318 PyErr_SetString(PyExc_ValueError,
2319 "field value is required for Starred");
2320 return NULL;
2321 }
2322 if (!ctx) {
2323 PyErr_SetString(PyExc_ValueError,
2324 "field ctx is required for Starred");
2325 return NULL;
2326 }
2327 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2328 if (!p)
2329 return NULL;
2330 p->kind = Starred_kind;
2331 p->v.Starred.value = value;
2332 p->v.Starred.ctx = ctx;
2333 p->lineno = lineno;
2334 p->col_offset = col_offset;
2335 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002336}
2337
2338expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002339Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2340 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002342 expr_ty p;
2343 if (!id) {
2344 PyErr_SetString(PyExc_ValueError,
2345 "field id is required for Name");
2346 return NULL;
2347 }
2348 if (!ctx) {
2349 PyErr_SetString(PyExc_ValueError,
2350 "field ctx is required for Name");
2351 return NULL;
2352 }
2353 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2354 if (!p)
2355 return NULL;
2356 p->kind = Name_kind;
2357 p->v.Name.id = id;
2358 p->v.Name.ctx = ctx;
2359 p->lineno = lineno;
2360 p->col_offset = col_offset;
2361 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362}
2363
2364expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002365List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2366 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002368 expr_ty p;
2369 if (!ctx) {
2370 PyErr_SetString(PyExc_ValueError,
2371 "field ctx is required for List");
2372 return NULL;
2373 }
2374 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2375 if (!p)
2376 return NULL;
2377 p->kind = List_kind;
2378 p->v.List.elts = elts;
2379 p->v.List.ctx = ctx;
2380 p->lineno = lineno;
2381 p->col_offset = col_offset;
2382 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383}
2384
2385expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002386Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2387 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002389 expr_ty p;
2390 if (!ctx) {
2391 PyErr_SetString(PyExc_ValueError,
2392 "field ctx is required for Tuple");
2393 return NULL;
2394 }
2395 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2396 if (!p)
2397 return NULL;
2398 p->kind = Tuple_kind;
2399 p->v.Tuple.elts = elts;
2400 p->v.Tuple.ctx = ctx;
2401 p->lineno = lineno;
2402 p->col_offset = col_offset;
2403 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404}
2405
2406slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002409 slice_ty p;
2410 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2411 if (!p)
2412 return NULL;
2413 p->kind = Slice_kind;
2414 p->v.Slice.lower = lower;
2415 p->v.Slice.upper = upper;
2416 p->v.Slice.step = step;
2417 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002421ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002423 slice_ty p;
2424 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2425 if (!p)
2426 return NULL;
2427 p->kind = ExtSlice_kind;
2428 p->v.ExtSlice.dims = dims;
2429 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430}
2431
2432slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002433Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002435 slice_ty p;
2436 if (!value) {
2437 PyErr_SetString(PyExc_ValueError,
2438 "field value is required for Index");
2439 return NULL;
2440 }
2441 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2442 if (!p)
2443 return NULL;
2444 p->kind = Index_kind;
2445 p->v.Index.value = value;
2446 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447}
2448
2449comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002450comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2451 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002453 comprehension_ty p;
2454 if (!target) {
2455 PyErr_SetString(PyExc_ValueError,
2456 "field target is required for comprehension");
2457 return NULL;
2458 }
2459 if (!iter) {
2460 PyErr_SetString(PyExc_ValueError,
2461 "field iter is required for comprehension");
2462 return NULL;
2463 }
2464 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2465 if (!p)
2466 return NULL;
2467 p->target = target;
2468 p->iter = iter;
2469 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002470 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002471 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472}
2473
2474excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002475ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002476 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002478 excepthandler_ty p;
2479 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2480 if (!p)
2481 return NULL;
2482 p->kind = ExceptHandler_kind;
2483 p->v.ExceptHandler.type = type;
2484 p->v.ExceptHandler.name = name;
2485 p->v.ExceptHandler.body = body;
2486 p->lineno = lineno;
2487 p->col_offset = col_offset;
2488 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489}
2490
2491arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002492arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2493 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002495 arguments_ty p;
2496 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2497 if (!p)
2498 return NULL;
2499 p->args = args;
2500 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002502 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002503 p->kwarg = kwarg;
2504 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002505 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506}
2507
Neal Norwitzc1505362006-12-28 06:47:50 +00002508arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002509arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2510 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002511{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002512 arg_ty p;
2513 if (!arg) {
2514 PyErr_SetString(PyExc_ValueError,
2515 "field arg is required for arg");
2516 return NULL;
2517 }
2518 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2519 if (!p)
2520 return NULL;
2521 p->arg = arg;
2522 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002523 p->lineno = lineno;
2524 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002525 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002526}
2527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002529keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002531 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002532 if (!value) {
2533 PyErr_SetString(PyExc_ValueError,
2534 "field value is required for keyword");
2535 return NULL;
2536 }
2537 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2538 if (!p)
2539 return NULL;
2540 p->arg = arg;
2541 p->value = value;
2542 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543}
2544
2545alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002546alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002548 alias_ty p;
2549 if (!name) {
2550 PyErr_SetString(PyExc_ValueError,
2551 "field name is required for alias");
2552 return NULL;
2553 }
2554 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2555 if (!p)
2556 return NULL;
2557 p->name = name;
2558 p->asname = asname;
2559 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560}
2561
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002562withitem_ty
2563withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2564{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002565 withitem_ty p;
2566 if (!context_expr) {
2567 PyErr_SetString(PyExc_ValueError,
2568 "field context_expr is required for withitem");
2569 return NULL;
2570 }
2571 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2572 if (!p)
2573 return NULL;
2574 p->context_expr = context_expr;
2575 p->optional_vars = optional_vars;
2576 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002577}
2578
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002579
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002580PyObject*
2581ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002582{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002583 mod_ty o = (mod_ty)_o;
2584 PyObject *result = NULL, *value = NULL;
2585 if (!o) {
2586 Py_INCREF(Py_None);
2587 return Py_None;
2588 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002589
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002590 switch (o->kind) {
2591 case Module_kind:
2592 result = PyType_GenericNew(Module_type, NULL, NULL);
2593 if (!result) goto failed;
2594 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 break;
2600 case Interactive_kind:
2601 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2602 if (!result) goto failed;
2603 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2604 if (!value) goto failed;
2605 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 break;
2609 case Expression_kind:
2610 result = PyType_GenericNew(Expression_type, NULL, NULL);
2611 if (!result) goto failed;
2612 value = ast2obj_expr(o->v.Expression.body);
2613 if (!value) goto failed;
2614 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
2617 break;
2618 case Suite_kind:
2619 result = PyType_GenericNew(Suite_type, NULL, NULL);
2620 if (!result) goto failed;
2621 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2622 if (!value) goto failed;
2623 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
2626 break;
2627 }
2628 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002629failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002630 Py_XDECREF(value);
2631 Py_XDECREF(result);
2632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633}
2634
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002635PyObject*
2636ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002638 stmt_ty o = (stmt_ty)_o;
2639 PyObject *result = NULL, *value = NULL;
2640 if (!o) {
2641 Py_INCREF(Py_None);
2642 return Py_None;
2643 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002644
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002645 switch (o->kind) {
2646 case FunctionDef_kind:
2647 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2648 if (!result) goto failed;
2649 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002650 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002651 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2652 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002653 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002654 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002655 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002656 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2657 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002658 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002659 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2660 if (!value) goto failed;
2661 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2665 if (!value) goto failed;
2666 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_expr(o->v.FunctionDef.returns);
2670 if (!value) goto failed;
2671 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002675 case AsyncFunctionDef_kind:
2676 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2679 if (!value) goto failed;
2680 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2684 if (!value) goto failed;
2685 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
2688 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2694 ast2obj_expr);
2695 if (!value) goto failed;
2696 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2700 if (!value) goto failed;
2701 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002705 case ClassDef_kind:
2706 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2707 if (!result) goto failed;
2708 value = ast2obj_identifier(o->v.ClassDef.name);
2709 if (!value) goto failed;
2710 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
2713 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2714 if (!value) goto failed;
2715 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2716 goto failed;
2717 Py_DECREF(value);
2718 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002723 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2724 if (!value) goto failed;
2725 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 break;
2734 case Return_kind:
2735 result = PyType_GenericNew(Return_type, NULL, NULL);
2736 if (!result) goto failed;
2737 value = ast2obj_expr(o->v.Return.value);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 break;
2743 case Delete_kind:
2744 result = PyType_GenericNew(Delete_type, NULL, NULL);
2745 if (!result) goto failed;
2746 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 break;
2752 case Assign_kind:
2753 result = PyType_GenericNew(Assign_type, NULL, NULL);
2754 if (!result) goto failed;
2755 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2756 if (!value) goto failed;
2757 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
2760 value = ast2obj_expr(o->v.Assign.value);
2761 if (!value) goto failed;
2762 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
2765 break;
2766 case AugAssign_kind:
2767 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2768 if (!result) goto failed;
2769 value = ast2obj_expr(o->v.AugAssign.target);
2770 if (!value) goto failed;
2771 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
2774 value = ast2obj_operator(o->v.AugAssign.op);
2775 if (!value) goto failed;
2776 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_expr(o->v.AugAssign.value);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002785 case AnnAssign_kind:
2786 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2787 if (!result) goto failed;
2788 value = ast2obj_expr(o->v.AnnAssign.target);
2789 if (!value) goto failed;
2790 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2791 goto failed;
2792 Py_DECREF(value);
2793 value = ast2obj_expr(o->v.AnnAssign.annotation);
2794 if (!value) goto failed;
2795 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 value = ast2obj_expr(o->v.AnnAssign.value);
2799 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 value = ast2obj_int(o->v.AnnAssign.simple);
2804 if (!value) goto failed;
2805 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002809 case For_kind:
2810 result = PyType_GenericNew(For_type, NULL, NULL);
2811 if (!result) goto failed;
2812 value = ast2obj_expr(o->v.For.target);
2813 if (!value) goto failed;
2814 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2815 goto failed;
2816 Py_DECREF(value);
2817 value = ast2obj_expr(o->v.For.iter);
2818 if (!value) goto failed;
2819 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
2822 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2823 if (!value) goto failed;
2824 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2825 goto failed;
2826 Py_DECREF(value);
2827 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2828 if (!value) goto failed;
2829 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2830 goto failed;
2831 Py_DECREF(value);
2832 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002833 case AsyncFor_kind:
2834 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2835 if (!result) goto failed;
2836 value = ast2obj_expr(o->v.AsyncFor.target);
2837 if (!value) goto failed;
2838 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2839 goto failed;
2840 Py_DECREF(value);
2841 value = ast2obj_expr(o->v.AsyncFor.iter);
2842 if (!value) goto failed;
2843 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2844 goto failed;
2845 Py_DECREF(value);
2846 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2847 if (!value) goto failed;
2848 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2849 goto failed;
2850 Py_DECREF(value);
2851 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2852 if (!value) goto failed;
2853 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002857 case While_kind:
2858 result = PyType_GenericNew(While_type, NULL, NULL);
2859 if (!result) goto failed;
2860 value = ast2obj_expr(o->v.While.test);
2861 if (!value) goto failed;
2862 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2866 if (!value) goto failed;
2867 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2868 goto failed;
2869 Py_DECREF(value);
2870 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2871 if (!value) goto failed;
2872 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 break;
2876 case If_kind:
2877 result = PyType_GenericNew(If_type, NULL, NULL);
2878 if (!result) goto failed;
2879 value = ast2obj_expr(o->v.If.test);
2880 if (!value) goto failed;
2881 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
2884 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 break;
2895 case With_kind:
2896 result = PyType_GenericNew(With_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2904 if (!value) goto failed;
2905 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002909 case AsyncWith_kind:
2910 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2913 if (!value) goto failed;
2914 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2918 if (!value) goto failed;
2919 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
2922 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002923 case Raise_kind:
2924 result = PyType_GenericNew(Raise_type, NULL, NULL);
2925 if (!result) goto failed;
2926 value = ast2obj_expr(o->v.Raise.exc);
2927 if (!value) goto failed;
2928 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2929 goto failed;
2930 Py_DECREF(value);
2931 value = ast2obj_expr(o->v.Raise.cause);
2932 if (!value) goto failed;
2933 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
2936 break;
2937 case Try_kind:
2938 result = PyType_GenericNew(Try_type, NULL, NULL);
2939 if (!result) goto failed;
2940 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2946 if (!value) goto failed;
2947 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2948 goto failed;
2949 Py_DECREF(value);
2950 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2951 if (!value) goto failed;
2952 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2953 goto failed;
2954 Py_DECREF(value);
2955 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2956 if (!value) goto failed;
2957 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2958 goto failed;
2959 Py_DECREF(value);
2960 break;
2961 case Assert_kind:
2962 result = PyType_GenericNew(Assert_type, NULL, NULL);
2963 if (!result) goto failed;
2964 value = ast2obj_expr(o->v.Assert.test);
2965 if (!value) goto failed;
2966 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 value = ast2obj_expr(o->v.Assert.msg);
2970 if (!value) goto failed;
2971 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
2974 break;
2975 case Import_kind:
2976 result = PyType_GenericNew(Import_type, NULL, NULL);
2977 if (!result) goto failed;
2978 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2979 if (!value) goto failed;
2980 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
2983 break;
2984 case ImportFrom_kind:
2985 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2986 if (!result) goto failed;
2987 value = ast2obj_identifier(o->v.ImportFrom.module);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2993 if (!value) goto failed;
2994 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
2997 value = ast2obj_int(o->v.ImportFrom.level);
2998 if (!value) goto failed;
2999 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3000 goto failed;
3001 Py_DECREF(value);
3002 break;
3003 case Global_kind:
3004 result = PyType_GenericNew(Global_type, NULL, NULL);
3005 if (!result) goto failed;
3006 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3007 if (!value) goto failed;
3008 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
3011 break;
3012 case Nonlocal_kind:
3013 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3014 if (!result) goto failed;
3015 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3016 if (!value) goto failed;
3017 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
3020 break;
3021 case Expr_kind:
3022 result = PyType_GenericNew(Expr_type, NULL, NULL);
3023 if (!result) goto failed;
3024 value = ast2obj_expr(o->v.Expr.value);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 break;
3030 case Pass_kind:
3031 result = PyType_GenericNew(Pass_type, NULL, NULL);
3032 if (!result) goto failed;
3033 break;
3034 case Break_kind:
3035 result = PyType_GenericNew(Break_type, NULL, NULL);
3036 if (!result) goto failed;
3037 break;
3038 case Continue_kind:
3039 result = PyType_GenericNew(Continue_type, NULL, NULL);
3040 if (!result) goto failed;
3041 break;
3042 }
3043 value = ast2obj_int(o->lineno);
3044 if (!value) goto failed;
3045 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3046 goto failed;
3047 Py_DECREF(value);
3048 value = ast2obj_int(o->col_offset);
3049 if (!value) goto failed;
3050 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3051 goto failed;
3052 Py_DECREF(value);
3053 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003054failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003055 Py_XDECREF(value);
3056 Py_XDECREF(result);
3057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003060PyObject*
3061ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003063 expr_ty o = (expr_ty)_o;
3064 PyObject *result = NULL, *value = NULL;
3065 if (!o) {
3066 Py_INCREF(Py_None);
3067 return Py_None;
3068 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003069
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003070 switch (o->kind) {
3071 case BoolOp_kind:
3072 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3073 if (!result) goto failed;
3074 value = ast2obj_boolop(o->v.BoolOp.op);
3075 if (!value) goto failed;
3076 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3077 goto failed;
3078 Py_DECREF(value);
3079 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3080 if (!value) goto failed;
3081 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3082 goto failed;
3083 Py_DECREF(value);
3084 break;
3085 case BinOp_kind:
3086 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3087 if (!result) goto failed;
3088 value = ast2obj_expr(o->v.BinOp.left);
3089 if (!value) goto failed;
3090 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_operator(o->v.BinOp.op);
3094 if (!value) goto failed;
3095 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3096 goto failed;
3097 Py_DECREF(value);
3098 value = ast2obj_expr(o->v.BinOp.right);
3099 if (!value) goto failed;
3100 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3101 goto failed;
3102 Py_DECREF(value);
3103 break;
3104 case UnaryOp_kind:
3105 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3106 if (!result) goto failed;
3107 value = ast2obj_unaryop(o->v.UnaryOp.op);
3108 if (!value) goto failed;
3109 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3110 goto failed;
3111 Py_DECREF(value);
3112 value = ast2obj_expr(o->v.UnaryOp.operand);
3113 if (!value) goto failed;
3114 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3115 goto failed;
3116 Py_DECREF(value);
3117 break;
3118 case Lambda_kind:
3119 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3120 if (!result) goto failed;
3121 value = ast2obj_arguments(o->v.Lambda.args);
3122 if (!value) goto failed;
3123 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3124 goto failed;
3125 Py_DECREF(value);
3126 value = ast2obj_expr(o->v.Lambda.body);
3127 if (!value) goto failed;
3128 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3129 goto failed;
3130 Py_DECREF(value);
3131 break;
3132 case IfExp_kind:
3133 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3134 if (!result) goto failed;
3135 value = ast2obj_expr(o->v.IfExp.test);
3136 if (!value) goto failed;
3137 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3138 goto failed;
3139 Py_DECREF(value);
3140 value = ast2obj_expr(o->v.IfExp.body);
3141 if (!value) goto failed;
3142 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3143 goto failed;
3144 Py_DECREF(value);
3145 value = ast2obj_expr(o->v.IfExp.orelse);
3146 if (!value) goto failed;
3147 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3148 goto failed;
3149 Py_DECREF(value);
3150 break;
3151 case Dict_kind:
3152 result = PyType_GenericNew(Dict_type, NULL, NULL);
3153 if (!result) goto failed;
3154 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3155 if (!value) goto failed;
3156 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3157 goto failed;
3158 Py_DECREF(value);
3159 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3160 if (!value) goto failed;
3161 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3162 goto failed;
3163 Py_DECREF(value);
3164 break;
3165 case Set_kind:
3166 result = PyType_GenericNew(Set_type, NULL, NULL);
3167 if (!result) goto failed;
3168 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3169 if (!value) goto failed;
3170 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3171 goto failed;
3172 Py_DECREF(value);
3173 break;
3174 case ListComp_kind:
3175 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3176 if (!result) goto failed;
3177 value = ast2obj_expr(o->v.ListComp.elt);
3178 if (!value) goto failed;
3179 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3180 goto failed;
3181 Py_DECREF(value);
3182 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3183 if (!value) goto failed;
3184 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3185 goto failed;
3186 Py_DECREF(value);
3187 break;
3188 case SetComp_kind:
3189 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3190 if (!result) goto failed;
3191 value = ast2obj_expr(o->v.SetComp.elt);
3192 if (!value) goto failed;
3193 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3194 goto failed;
3195 Py_DECREF(value);
3196 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3197 if (!value) goto failed;
3198 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3199 goto failed;
3200 Py_DECREF(value);
3201 break;
3202 case DictComp_kind:
3203 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3204 if (!result) goto failed;
3205 value = ast2obj_expr(o->v.DictComp.key);
3206 if (!value) goto failed;
3207 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3208 goto failed;
3209 Py_DECREF(value);
3210 value = ast2obj_expr(o->v.DictComp.value);
3211 if (!value) goto failed;
3212 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3213 goto failed;
3214 Py_DECREF(value);
3215 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3216 if (!value) goto failed;
3217 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3218 goto failed;
3219 Py_DECREF(value);
3220 break;
3221 case GeneratorExp_kind:
3222 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3223 if (!result) goto failed;
3224 value = ast2obj_expr(o->v.GeneratorExp.elt);
3225 if (!value) goto failed;
3226 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3227 goto failed;
3228 Py_DECREF(value);
3229 value = ast2obj_list(o->v.GeneratorExp.generators,
3230 ast2obj_comprehension);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003236 case Await_kind:
3237 result = PyType_GenericNew(Await_type, NULL, NULL);
3238 if (!result) goto failed;
3239 value = ast2obj_expr(o->v.Await.value);
3240 if (!value) goto failed;
3241 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3242 goto failed;
3243 Py_DECREF(value);
3244 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003245 case Yield_kind:
3246 result = PyType_GenericNew(Yield_type, NULL, NULL);
3247 if (!result) goto failed;
3248 value = ast2obj_expr(o->v.Yield.value);
3249 if (!value) goto failed;
3250 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
3253 break;
3254 case YieldFrom_kind:
3255 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3256 if (!result) goto failed;
3257 value = ast2obj_expr(o->v.YieldFrom.value);
3258 if (!value) goto failed;
3259 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3260 goto failed;
3261 Py_DECREF(value);
3262 break;
3263 case Compare_kind:
3264 result = PyType_GenericNew(Compare_type, NULL, NULL);
3265 if (!result) goto failed;
3266 value = ast2obj_expr(o->v.Compare.left);
3267 if (!value) goto failed;
3268 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3269 goto failed;
3270 Py_DECREF(value);
3271 {
3272 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3273 value = PyList_New(n);
3274 if (!value) goto failed;
3275 for(i = 0; i < n; i++)
3276 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003278 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003279 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3280 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003281 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003282 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003283 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003284 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3285 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003286 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003287 break;
3288 case Call_kind:
3289 result = PyType_GenericNew(Call_type, NULL, NULL);
3290 if (!result) goto failed;
3291 value = ast2obj_expr(o->v.Call.func);
3292 if (!value) goto failed;
3293 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3294 goto failed;
3295 Py_DECREF(value);
3296 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3297 if (!value) goto failed;
3298 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3299 goto failed;
3300 Py_DECREF(value);
3301 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3302 if (!value) goto failed;
3303 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3304 goto failed;
3305 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003306 break;
3307 case Num_kind:
3308 result = PyType_GenericNew(Num_type, NULL, NULL);
3309 if (!result) goto failed;
3310 value = ast2obj_object(o->v.Num.n);
3311 if (!value) goto failed;
3312 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3313 goto failed;
3314 Py_DECREF(value);
3315 break;
3316 case Str_kind:
3317 result = PyType_GenericNew(Str_type, NULL, NULL);
3318 if (!result) goto failed;
3319 value = ast2obj_string(o->v.Str.s);
3320 if (!value) goto failed;
3321 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3322 goto failed;
3323 Py_DECREF(value);
3324 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003325 case FormattedValue_kind:
3326 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3327 if (!result) goto failed;
3328 value = ast2obj_expr(o->v.FormattedValue.value);
3329 if (!value) goto failed;
3330 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3331 goto failed;
3332 Py_DECREF(value);
3333 value = ast2obj_int(o->v.FormattedValue.conversion);
3334 if (!value) goto failed;
3335 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3336 goto failed;
3337 Py_DECREF(value);
3338 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3339 if (!value) goto failed;
3340 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3341 goto failed;
3342 Py_DECREF(value);
3343 break;
3344 case JoinedStr_kind:
3345 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3346 if (!result) goto failed;
3347 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3348 if (!value) goto failed;
3349 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3350 goto failed;
3351 Py_DECREF(value);
3352 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003353 case Bytes_kind:
3354 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3355 if (!result) goto failed;
3356 value = ast2obj_bytes(o->v.Bytes.s);
3357 if (!value) goto failed;
3358 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3359 goto failed;
3360 Py_DECREF(value);
3361 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003362 case NameConstant_kind:
3363 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3364 if (!result) goto failed;
3365 value = ast2obj_singleton(o->v.NameConstant.value);
3366 if (!value) goto failed;
3367 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3368 goto failed;
3369 Py_DECREF(value);
3370 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003371 case Ellipsis_kind:
3372 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3373 if (!result) goto failed;
3374 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003375 case Constant_kind:
3376 result = PyType_GenericNew(Constant_type, NULL, NULL);
3377 if (!result) goto failed;
3378 value = ast2obj_constant(o->v.Constant.value);
3379 if (!value) goto failed;
3380 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3381 goto failed;
3382 Py_DECREF(value);
3383 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003384 case Attribute_kind:
3385 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3386 if (!result) goto failed;
3387 value = ast2obj_expr(o->v.Attribute.value);
3388 if (!value) goto failed;
3389 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3390 goto failed;
3391 Py_DECREF(value);
3392 value = ast2obj_identifier(o->v.Attribute.attr);
3393 if (!value) goto failed;
3394 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3395 goto failed;
3396 Py_DECREF(value);
3397 value = ast2obj_expr_context(o->v.Attribute.ctx);
3398 if (!value) goto failed;
3399 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3400 goto failed;
3401 Py_DECREF(value);
3402 break;
3403 case Subscript_kind:
3404 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3405 if (!result) goto failed;
3406 value = ast2obj_expr(o->v.Subscript.value);
3407 if (!value) goto failed;
3408 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3409 goto failed;
3410 Py_DECREF(value);
3411 value = ast2obj_slice(o->v.Subscript.slice);
3412 if (!value) goto failed;
3413 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3414 goto failed;
3415 Py_DECREF(value);
3416 value = ast2obj_expr_context(o->v.Subscript.ctx);
3417 if (!value) goto failed;
3418 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3419 goto failed;
3420 Py_DECREF(value);
3421 break;
3422 case Starred_kind:
3423 result = PyType_GenericNew(Starred_type, NULL, NULL);
3424 if (!result) goto failed;
3425 value = ast2obj_expr(o->v.Starred.value);
3426 if (!value) goto failed;
3427 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3428 goto failed;
3429 Py_DECREF(value);
3430 value = ast2obj_expr_context(o->v.Starred.ctx);
3431 if (!value) goto failed;
3432 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
3435 break;
3436 case Name_kind:
3437 result = PyType_GenericNew(Name_type, NULL, NULL);
3438 if (!result) goto failed;
3439 value = ast2obj_identifier(o->v.Name.id);
3440 if (!value) goto failed;
3441 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3442 goto failed;
3443 Py_DECREF(value);
3444 value = ast2obj_expr_context(o->v.Name.ctx);
3445 if (!value) goto failed;
3446 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3447 goto failed;
3448 Py_DECREF(value);
3449 break;
3450 case List_kind:
3451 result = PyType_GenericNew(List_type, NULL, NULL);
3452 if (!result) goto failed;
3453 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3454 if (!value) goto failed;
3455 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3456 goto failed;
3457 Py_DECREF(value);
3458 value = ast2obj_expr_context(o->v.List.ctx);
3459 if (!value) goto failed;
3460 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3461 goto failed;
3462 Py_DECREF(value);
3463 break;
3464 case Tuple_kind:
3465 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3466 if (!result) goto failed;
3467 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3468 if (!value) goto failed;
3469 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3470 goto failed;
3471 Py_DECREF(value);
3472 value = ast2obj_expr_context(o->v.Tuple.ctx);
3473 if (!value) goto failed;
3474 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3475 goto failed;
3476 Py_DECREF(value);
3477 break;
3478 }
3479 value = ast2obj_int(o->lineno);
3480 if (!value) goto failed;
3481 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3482 goto failed;
3483 Py_DECREF(value);
3484 value = ast2obj_int(o->col_offset);
3485 if (!value) goto failed;
3486 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3487 goto failed;
3488 Py_DECREF(value);
3489 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003490failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003491 Py_XDECREF(value);
3492 Py_XDECREF(result);
3493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494}
3495
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003496PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003498 switch(o) {
3499 case Load:
3500 Py_INCREF(Load_singleton);
3501 return Load_singleton;
3502 case Store:
3503 Py_INCREF(Store_singleton);
3504 return Store_singleton;
3505 case Del:
3506 Py_INCREF(Del_singleton);
3507 return Del_singleton;
3508 case AugLoad:
3509 Py_INCREF(AugLoad_singleton);
3510 return AugLoad_singleton;
3511 case AugStore:
3512 Py_INCREF(AugStore_singleton);
3513 return AugStore_singleton;
3514 case Param:
3515 Py_INCREF(Param_singleton);
3516 return Param_singleton;
3517 default:
3518 /* should never happen, but just in case ... */
3519 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3520 return NULL;
3521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003523PyObject*
3524ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 slice_ty o = (slice_ty)_o;
3527 PyObject *result = NULL, *value = NULL;
3528 if (!o) {
3529 Py_INCREF(Py_None);
3530 return Py_None;
3531 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003532
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 switch (o->kind) {
3534 case Slice_kind:
3535 result = PyType_GenericNew(Slice_type, NULL, NULL);
3536 if (!result) goto failed;
3537 value = ast2obj_expr(o->v.Slice.lower);
3538 if (!value) goto failed;
3539 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3540 goto failed;
3541 Py_DECREF(value);
3542 value = ast2obj_expr(o->v.Slice.upper);
3543 if (!value) goto failed;
3544 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3545 goto failed;
3546 Py_DECREF(value);
3547 value = ast2obj_expr(o->v.Slice.step);
3548 if (!value) goto failed;
3549 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3550 goto failed;
3551 Py_DECREF(value);
3552 break;
3553 case ExtSlice_kind:
3554 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3555 if (!result) goto failed;
3556 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3557 if (!value) goto failed;
3558 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3559 goto failed;
3560 Py_DECREF(value);
3561 break;
3562 case Index_kind:
3563 result = PyType_GenericNew(Index_type, NULL, NULL);
3564 if (!result) goto failed;
3565 value = ast2obj_expr(o->v.Index.value);
3566 if (!value) goto failed;
3567 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3568 goto failed;
3569 Py_DECREF(value);
3570 break;
3571 }
3572 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003573failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003574 Py_XDECREF(value);
3575 Py_XDECREF(result);
3576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577}
3578
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003579PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003581 switch(o) {
3582 case And:
3583 Py_INCREF(And_singleton);
3584 return And_singleton;
3585 case Or:
3586 Py_INCREF(Or_singleton);
3587 return Or_singleton;
3588 default:
3589 /* should never happen, but just in case ... */
3590 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3591 return NULL;
3592 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003593}
3594PyObject* ast2obj_operator(operator_ty o)
3595{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 switch(o) {
3597 case Add:
3598 Py_INCREF(Add_singleton);
3599 return Add_singleton;
3600 case Sub:
3601 Py_INCREF(Sub_singleton);
3602 return Sub_singleton;
3603 case Mult:
3604 Py_INCREF(Mult_singleton);
3605 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003606 case MatMult:
3607 Py_INCREF(MatMult_singleton);
3608 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003609 case Div:
3610 Py_INCREF(Div_singleton);
3611 return Div_singleton;
3612 case Mod:
3613 Py_INCREF(Mod_singleton);
3614 return Mod_singleton;
3615 case Pow:
3616 Py_INCREF(Pow_singleton);
3617 return Pow_singleton;
3618 case LShift:
3619 Py_INCREF(LShift_singleton);
3620 return LShift_singleton;
3621 case RShift:
3622 Py_INCREF(RShift_singleton);
3623 return RShift_singleton;
3624 case BitOr:
3625 Py_INCREF(BitOr_singleton);
3626 return BitOr_singleton;
3627 case BitXor:
3628 Py_INCREF(BitXor_singleton);
3629 return BitXor_singleton;
3630 case BitAnd:
3631 Py_INCREF(BitAnd_singleton);
3632 return BitAnd_singleton;
3633 case FloorDiv:
3634 Py_INCREF(FloorDiv_singleton);
3635 return FloorDiv_singleton;
3636 default:
3637 /* should never happen, but just in case ... */
3638 PyErr_Format(PyExc_SystemError, "unknown operator found");
3639 return NULL;
3640 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003641}
3642PyObject* ast2obj_unaryop(unaryop_ty o)
3643{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003644 switch(o) {
3645 case Invert:
3646 Py_INCREF(Invert_singleton);
3647 return Invert_singleton;
3648 case Not:
3649 Py_INCREF(Not_singleton);
3650 return Not_singleton;
3651 case UAdd:
3652 Py_INCREF(UAdd_singleton);
3653 return UAdd_singleton;
3654 case USub:
3655 Py_INCREF(USub_singleton);
3656 return USub_singleton;
3657 default:
3658 /* should never happen, but just in case ... */
3659 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3660 return NULL;
3661 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003662}
3663PyObject* ast2obj_cmpop(cmpop_ty o)
3664{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003665 switch(o) {
3666 case Eq:
3667 Py_INCREF(Eq_singleton);
3668 return Eq_singleton;
3669 case NotEq:
3670 Py_INCREF(NotEq_singleton);
3671 return NotEq_singleton;
3672 case Lt:
3673 Py_INCREF(Lt_singleton);
3674 return Lt_singleton;
3675 case LtE:
3676 Py_INCREF(LtE_singleton);
3677 return LtE_singleton;
3678 case Gt:
3679 Py_INCREF(Gt_singleton);
3680 return Gt_singleton;
3681 case GtE:
3682 Py_INCREF(GtE_singleton);
3683 return GtE_singleton;
3684 case Is:
3685 Py_INCREF(Is_singleton);
3686 return Is_singleton;
3687 case IsNot:
3688 Py_INCREF(IsNot_singleton);
3689 return IsNot_singleton;
3690 case In:
3691 Py_INCREF(In_singleton);
3692 return In_singleton;
3693 case NotIn:
3694 Py_INCREF(NotIn_singleton);
3695 return NotIn_singleton;
3696 default:
3697 /* should never happen, but just in case ... */
3698 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3699 return NULL;
3700 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003701}
3702PyObject*
3703ast2obj_comprehension(void* _o)
3704{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003705 comprehension_ty o = (comprehension_ty)_o;
3706 PyObject *result = NULL, *value = NULL;
3707 if (!o) {
3708 Py_INCREF(Py_None);
3709 return Py_None;
3710 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003711
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003712 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3713 if (!result) return NULL;
3714 value = ast2obj_expr(o->target);
3715 if (!value) goto failed;
3716 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3717 goto failed;
3718 Py_DECREF(value);
3719 value = ast2obj_expr(o->iter);
3720 if (!value) goto failed;
3721 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3722 goto failed;
3723 Py_DECREF(value);
3724 value = ast2obj_list(o->ifs, ast2obj_expr);
3725 if (!value) goto failed;
3726 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3727 goto failed;
3728 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003729 value = ast2obj_int(o->is_async);
3730 if (!value) goto failed;
3731 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3732 goto failed;
3733 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003734 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003735failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 Py_XDECREF(value);
3737 Py_XDECREF(result);
3738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739}
3740
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003741PyObject*
3742ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003744 excepthandler_ty o = (excepthandler_ty)_o;
3745 PyObject *result = NULL, *value = NULL;
3746 if (!o) {
3747 Py_INCREF(Py_None);
3748 return Py_None;
3749 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003750
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 switch (o->kind) {
3752 case ExceptHandler_kind:
3753 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3754 if (!result) goto failed;
3755 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003756 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003757 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3758 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003759 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003760 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3763 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003764 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003765 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3766 if (!value) goto failed;
3767 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3768 goto failed;
3769 Py_DECREF(value);
3770 break;
3771 }
3772 value = ast2obj_int(o->lineno);
3773 if (!value) goto failed;
3774 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3775 goto failed;
3776 Py_DECREF(value);
3777 value = ast2obj_int(o->col_offset);
3778 if (!value) goto failed;
3779 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3780 goto failed;
3781 Py_DECREF(value);
3782 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003783failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003784 Py_XDECREF(value);
3785 Py_XDECREF(result);
3786 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787}
3788
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003789PyObject*
3790ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 arguments_ty o = (arguments_ty)_o;
3793 PyObject *result = NULL, *value = NULL;
3794 if (!o) {
3795 Py_INCREF(Py_None);
3796 return Py_None;
3797 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003798
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003799 result = PyType_GenericNew(arguments_type, NULL, NULL);
3800 if (!result) return NULL;
3801 value = ast2obj_list(o->args, ast2obj_arg);
3802 if (!value) goto failed;
3803 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3804 goto failed;
3805 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003806 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003807 if (!value) goto failed;
3808 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3809 goto failed;
3810 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003811 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3812 if (!value) goto failed;
3813 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3814 goto failed;
3815 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003816 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003817 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003818 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 goto failed;
3820 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003821 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003822 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003823 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003824 goto failed;
3825 Py_DECREF(value);
3826 value = ast2obj_list(o->defaults, ast2obj_expr);
3827 if (!value) goto failed;
3828 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3829 goto failed;
3830 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003831 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003832failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003833 Py_XDECREF(value);
3834 Py_XDECREF(result);
3835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836}
3837
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003838PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003839ast2obj_arg(void* _o)
3840{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003841 arg_ty o = (arg_ty)_o;
3842 PyObject *result = NULL, *value = NULL;
3843 if (!o) {
3844 Py_INCREF(Py_None);
3845 return Py_None;
3846 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003847
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003848 result = PyType_GenericNew(arg_type, NULL, NULL);
3849 if (!result) return NULL;
3850 value = ast2obj_identifier(o->arg);
3851 if (!value) goto failed;
3852 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3853 goto failed;
3854 Py_DECREF(value);
3855 value = ast2obj_expr(o->annotation);
3856 if (!value) goto failed;
3857 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3858 goto failed;
3859 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003860 value = ast2obj_int(o->lineno);
3861 if (!value) goto failed;
3862 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3863 goto failed;
3864 Py_DECREF(value);
3865 value = ast2obj_int(o->col_offset);
3866 if (!value) goto failed;
3867 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3868 goto failed;
3869 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003870 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003871failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003872 Py_XDECREF(value);
3873 Py_XDECREF(result);
3874 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003875}
3876
3877PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003878ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003880 keyword_ty o = (keyword_ty)_o;
3881 PyObject *result = NULL, *value = NULL;
3882 if (!o) {
3883 Py_INCREF(Py_None);
3884 return Py_None;
3885 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003886
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003887 result = PyType_GenericNew(keyword_type, NULL, NULL);
3888 if (!result) return NULL;
3889 value = ast2obj_identifier(o->arg);
3890 if (!value) goto failed;
3891 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3892 goto failed;
3893 Py_DECREF(value);
3894 value = ast2obj_expr(o->value);
3895 if (!value) goto failed;
3896 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3897 goto failed;
3898 Py_DECREF(value);
3899 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003900failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003901 Py_XDECREF(value);
3902 Py_XDECREF(result);
3903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904}
3905
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003906PyObject*
3907ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003909 alias_ty o = (alias_ty)_o;
3910 PyObject *result = NULL, *value = NULL;
3911 if (!o) {
3912 Py_INCREF(Py_None);
3913 return Py_None;
3914 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003915
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003916 result = PyType_GenericNew(alias_type, NULL, NULL);
3917 if (!result) return NULL;
3918 value = ast2obj_identifier(o->name);
3919 if (!value) goto failed;
3920 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3921 goto failed;
3922 Py_DECREF(value);
3923 value = ast2obj_identifier(o->asname);
3924 if (!value) goto failed;
3925 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3926 goto failed;
3927 Py_DECREF(value);
3928 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003929failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 Py_XDECREF(value);
3931 Py_XDECREF(result);
3932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933}
3934
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003935PyObject*
3936ast2obj_withitem(void* _o)
3937{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 withitem_ty o = (withitem_ty)_o;
3939 PyObject *result = NULL, *value = NULL;
3940 if (!o) {
3941 Py_INCREF(Py_None);
3942 return Py_None;
3943 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003944
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 result = PyType_GenericNew(withitem_type, NULL, NULL);
3946 if (!result) return NULL;
3947 value = ast2obj_expr(o->context_expr);
3948 if (!value) goto failed;
3949 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3950 goto failed;
3951 Py_DECREF(value);
3952 value = ast2obj_expr(o->optional_vars);
3953 if (!value) goto failed;
3954 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3955 goto failed;
3956 Py_DECREF(value);
3957 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003958failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 Py_XDECREF(value);
3960 Py_XDECREF(result);
3961 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003962}
3963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003965int
3966obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3967{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003968 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003969
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003970 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003971
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003972 if (obj == Py_None) {
3973 *out = NULL;
3974 return 0;
3975 }
3976 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3977 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003978 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003979 }
3980 if (isinstance) {
3981 asdl_seq* body;
3982
3983 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3984 int res;
3985 Py_ssize_t len;
3986 Py_ssize_t i;
3987 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3988 if (tmp == NULL) goto failed;
3989 if (!PyList_Check(tmp)) {
3990 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3991 goto failed;
3992 }
3993 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003994 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003995 if (body == NULL) goto failed;
3996 for (i = 0; i < len; i++) {
3997 stmt_ty value;
3998 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3999 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004000 if (len != PyList_GET_SIZE(tmp)) {
4001 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4002 goto failed;
4003 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004004 asdl_seq_SET(body, i, value);
4005 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004006 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004007 } else {
4008 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4009 return 1;
4010 }
4011 *out = Module(body, arena);
4012 if (*out == NULL) goto failed;
4013 return 0;
4014 }
4015 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4016 if (isinstance == -1) {
4017 return 1;
4018 }
4019 if (isinstance) {
4020 asdl_seq* body;
4021
4022 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4023 int res;
4024 Py_ssize_t len;
4025 Py_ssize_t i;
4026 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4027 if (tmp == NULL) goto failed;
4028 if (!PyList_Check(tmp)) {
4029 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4030 goto failed;
4031 }
4032 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004033 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004034 if (body == NULL) goto failed;
4035 for (i = 0; i < len; i++) {
4036 stmt_ty value;
4037 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4038 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004039 if (len != PyList_GET_SIZE(tmp)) {
4040 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4041 goto failed;
4042 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004043 asdl_seq_SET(body, i, value);
4044 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004045 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004046 } else {
4047 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4048 return 1;
4049 }
4050 *out = Interactive(body, arena);
4051 if (*out == NULL) goto failed;
4052 return 0;
4053 }
4054 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4055 if (isinstance == -1) {
4056 return 1;
4057 }
4058 if (isinstance) {
4059 expr_ty body;
4060
4061 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4062 int res;
4063 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4064 if (tmp == NULL) goto failed;
4065 res = obj2ast_expr(tmp, &body, arena);
4066 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004067 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004068 } else {
4069 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4070 return 1;
4071 }
4072 *out = Expression(body, arena);
4073 if (*out == NULL) goto failed;
4074 return 0;
4075 }
4076 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4077 if (isinstance == -1) {
4078 return 1;
4079 }
4080 if (isinstance) {
4081 asdl_seq* body;
4082
4083 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4084 int res;
4085 Py_ssize_t len;
4086 Py_ssize_t i;
4087 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4088 if (tmp == NULL) goto failed;
4089 if (!PyList_Check(tmp)) {
4090 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4091 goto failed;
4092 }
4093 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004094 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004095 if (body == NULL) goto failed;
4096 for (i = 0; i < len; i++) {
4097 stmt_ty value;
4098 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4099 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004100 if (len != PyList_GET_SIZE(tmp)) {
4101 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4102 goto failed;
4103 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004104 asdl_seq_SET(body, i, value);
4105 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004106 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004107 } else {
4108 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4109 return 1;
4110 }
4111 *out = Suite(body, arena);
4112 if (*out == NULL) goto failed;
4113 return 0;
4114 }
4115
4116 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4117 failed:
4118 Py_XDECREF(tmp);
4119 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004120}
4121
4122int
4123obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4124{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004125 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004126
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004127 PyObject *tmp = NULL;
4128 int lineno;
4129 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004130
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004131 if (obj == Py_None) {
4132 *out = NULL;
4133 return 0;
4134 }
4135 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4136 int res;
4137 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4138 if (tmp == NULL) goto failed;
4139 res = obj2ast_int(tmp, &lineno, arena);
4140 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004141 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004142 } else {
4143 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004144 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004145 }
4146 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4147 int res;
4148 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4149 if (tmp == NULL) goto failed;
4150 res = obj2ast_int(tmp, &col_offset, arena);
4151 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004152 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004153 } else {
4154 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4155 return 1;
4156 }
4157 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4158 if (isinstance == -1) {
4159 return 1;
4160 }
4161 if (isinstance) {
4162 identifier name;
4163 arguments_ty args;
4164 asdl_seq* body;
4165 asdl_seq* decorator_list;
4166 expr_ty returns;
4167
4168 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4169 int res;
4170 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4171 if (tmp == NULL) goto failed;
4172 res = obj2ast_identifier(tmp, &name, arena);
4173 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004174 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004175 } else {
4176 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4177 return 1;
4178 }
4179 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4180 int res;
4181 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4182 if (tmp == NULL) goto failed;
4183 res = obj2ast_arguments(tmp, &args, arena);
4184 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004185 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004186 } else {
4187 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4188 return 1;
4189 }
4190 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4191 int res;
4192 Py_ssize_t len;
4193 Py_ssize_t i;
4194 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4195 if (tmp == NULL) goto failed;
4196 if (!PyList_Check(tmp)) {
4197 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4198 goto failed;
4199 }
4200 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004201 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004202 if (body == NULL) goto failed;
4203 for (i = 0; i < len; i++) {
4204 stmt_ty value;
4205 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4206 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004207 if (len != PyList_GET_SIZE(tmp)) {
4208 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4209 goto failed;
4210 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004211 asdl_seq_SET(body, i, value);
4212 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004213 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004214 } else {
4215 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4216 return 1;
4217 }
4218 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4219 int res;
4220 Py_ssize_t len;
4221 Py_ssize_t i;
4222 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4223 if (tmp == NULL) goto failed;
4224 if (!PyList_Check(tmp)) {
4225 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4226 goto failed;
4227 }
4228 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004229 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004230 if (decorator_list == NULL) goto failed;
4231 for (i = 0; i < len; i++) {
4232 expr_ty value;
4233 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4234 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004235 if (len != PyList_GET_SIZE(tmp)) {
4236 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4237 goto failed;
4238 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004239 asdl_seq_SET(decorator_list, i, value);
4240 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004241 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004242 } else {
4243 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4244 return 1;
4245 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004246 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004247 int res;
4248 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4249 if (tmp == NULL) goto failed;
4250 res = obj2ast_expr(tmp, &returns, arena);
4251 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004252 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004253 } else {
4254 returns = NULL;
4255 }
4256 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4257 col_offset, arena);
4258 if (*out == NULL) goto failed;
4259 return 0;
4260 }
Yury Selivanov75445082015-05-11 22:57:16 -04004261 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4262 if (isinstance == -1) {
4263 return 1;
4264 }
4265 if (isinstance) {
4266 identifier name;
4267 arguments_ty args;
4268 asdl_seq* body;
4269 asdl_seq* decorator_list;
4270 expr_ty returns;
4271
4272 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4273 int res;
4274 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4275 if (tmp == NULL) goto failed;
4276 res = obj2ast_identifier(tmp, &name, arena);
4277 if (res != 0) goto failed;
4278 Py_CLEAR(tmp);
4279 } else {
4280 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4281 return 1;
4282 }
4283 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4284 int res;
4285 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4286 if (tmp == NULL) goto failed;
4287 res = obj2ast_arguments(tmp, &args, arena);
4288 if (res != 0) goto failed;
4289 Py_CLEAR(tmp);
4290 } else {
4291 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4292 return 1;
4293 }
4294 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4295 int res;
4296 Py_ssize_t len;
4297 Py_ssize_t i;
4298 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4299 if (tmp == NULL) goto failed;
4300 if (!PyList_Check(tmp)) {
4301 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4302 goto failed;
4303 }
4304 len = PyList_GET_SIZE(tmp);
4305 body = _Py_asdl_seq_new(len, arena);
4306 if (body == NULL) goto failed;
4307 for (i = 0; i < len; i++) {
4308 stmt_ty value;
4309 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4310 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004311 if (len != PyList_GET_SIZE(tmp)) {
4312 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4313 goto failed;
4314 }
Yury Selivanov75445082015-05-11 22:57:16 -04004315 asdl_seq_SET(body, i, value);
4316 }
4317 Py_CLEAR(tmp);
4318 } else {
4319 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4320 return 1;
4321 }
4322 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4323 int res;
4324 Py_ssize_t len;
4325 Py_ssize_t i;
4326 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4327 if (tmp == NULL) goto failed;
4328 if (!PyList_Check(tmp)) {
4329 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4330 goto failed;
4331 }
4332 len = PyList_GET_SIZE(tmp);
4333 decorator_list = _Py_asdl_seq_new(len, arena);
4334 if (decorator_list == NULL) goto failed;
4335 for (i = 0; i < len; i++) {
4336 expr_ty value;
4337 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4338 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004339 if (len != PyList_GET_SIZE(tmp)) {
4340 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4341 goto failed;
4342 }
Yury Selivanov75445082015-05-11 22:57:16 -04004343 asdl_seq_SET(decorator_list, i, value);
4344 }
4345 Py_CLEAR(tmp);
4346 } else {
4347 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4348 return 1;
4349 }
4350 if (exists_not_none(obj, &PyId_returns)) {
4351 int res;
4352 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4353 if (tmp == NULL) goto failed;
4354 res = obj2ast_expr(tmp, &returns, arena);
4355 if (res != 0) goto failed;
4356 Py_CLEAR(tmp);
4357 } else {
4358 returns = NULL;
4359 }
4360 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4361 lineno, col_offset, arena);
4362 if (*out == NULL) goto failed;
4363 return 0;
4364 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004365 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4366 if (isinstance == -1) {
4367 return 1;
4368 }
4369 if (isinstance) {
4370 identifier name;
4371 asdl_seq* bases;
4372 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004373 asdl_seq* body;
4374 asdl_seq* decorator_list;
4375
4376 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4377 int res;
4378 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4379 if (tmp == NULL) goto failed;
4380 res = obj2ast_identifier(tmp, &name, arena);
4381 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004382 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004383 } else {
4384 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4385 return 1;
4386 }
4387 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4388 int res;
4389 Py_ssize_t len;
4390 Py_ssize_t i;
4391 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4392 if (tmp == NULL) goto failed;
4393 if (!PyList_Check(tmp)) {
4394 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4395 goto failed;
4396 }
4397 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004398 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004399 if (bases == NULL) goto failed;
4400 for (i = 0; i < len; i++) {
4401 expr_ty value;
4402 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4403 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004404 if (len != PyList_GET_SIZE(tmp)) {
4405 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4406 goto failed;
4407 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004408 asdl_seq_SET(bases, i, value);
4409 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004410 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004411 } else {
4412 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4413 return 1;
4414 }
4415 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4416 int res;
4417 Py_ssize_t len;
4418 Py_ssize_t i;
4419 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4420 if (tmp == NULL) goto failed;
4421 if (!PyList_Check(tmp)) {
4422 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4423 goto failed;
4424 }
4425 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004426 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004427 if (keywords == NULL) goto failed;
4428 for (i = 0; i < len; i++) {
4429 keyword_ty value;
4430 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4431 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004432 if (len != PyList_GET_SIZE(tmp)) {
4433 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4434 goto failed;
4435 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004436 asdl_seq_SET(keywords, i, value);
4437 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004439 } else {
4440 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4441 return 1;
4442 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004443 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4444 int res;
4445 Py_ssize_t len;
4446 Py_ssize_t i;
4447 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4448 if (tmp == NULL) goto failed;
4449 if (!PyList_Check(tmp)) {
4450 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451 goto failed;
4452 }
4453 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004454 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004455 if (body == NULL) goto failed;
4456 for (i = 0; i < len; i++) {
4457 stmt_ty value;
4458 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4459 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004460 if (len != PyList_GET_SIZE(tmp)) {
4461 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4462 goto failed;
4463 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004464 asdl_seq_SET(body, i, value);
4465 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004466 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 } else {
4468 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4469 return 1;
4470 }
4471 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4472 int res;
4473 Py_ssize_t len;
4474 Py_ssize_t i;
4475 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4476 if (tmp == NULL) goto failed;
4477 if (!PyList_Check(tmp)) {
4478 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479 goto failed;
4480 }
4481 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004482 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004483 if (decorator_list == NULL) goto failed;
4484 for (i = 0; i < len; i++) {
4485 expr_ty value;
4486 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4487 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004488 if (len != PyList_GET_SIZE(tmp)) {
4489 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4490 goto failed;
4491 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004492 asdl_seq_SET(decorator_list, i, value);
4493 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004494 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004495 } else {
4496 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4497 return 1;
4498 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004499 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4500 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004501 if (*out == NULL) goto failed;
4502 return 0;
4503 }
4504 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4505 if (isinstance == -1) {
4506 return 1;
4507 }
4508 if (isinstance) {
4509 expr_ty value;
4510
Victor Stinneree4b59c2013-07-27 00:01:35 +02004511 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004512 int res;
4513 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4514 if (tmp == NULL) goto failed;
4515 res = obj2ast_expr(tmp, &value, arena);
4516 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004517 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004518 } else {
4519 value = NULL;
4520 }
4521 *out = Return(value, lineno, col_offset, arena);
4522 if (*out == NULL) goto failed;
4523 return 0;
4524 }
4525 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4526 if (isinstance == -1) {
4527 return 1;
4528 }
4529 if (isinstance) {
4530 asdl_seq* targets;
4531
4532 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4533 int res;
4534 Py_ssize_t len;
4535 Py_ssize_t i;
4536 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4537 if (tmp == NULL) goto failed;
4538 if (!PyList_Check(tmp)) {
4539 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4540 goto failed;
4541 }
4542 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004543 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004544 if (targets == NULL) goto failed;
4545 for (i = 0; i < len; i++) {
4546 expr_ty value;
4547 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4548 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004549 if (len != PyList_GET_SIZE(tmp)) {
4550 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4551 goto failed;
4552 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004553 asdl_seq_SET(targets, i, value);
4554 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004555 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004556 } else {
4557 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4558 return 1;
4559 }
4560 *out = Delete(targets, lineno, col_offset, arena);
4561 if (*out == NULL) goto failed;
4562 return 0;
4563 }
4564 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4565 if (isinstance == -1) {
4566 return 1;
4567 }
4568 if (isinstance) {
4569 asdl_seq* targets;
4570 expr_ty value;
4571
4572 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4573 int res;
4574 Py_ssize_t len;
4575 Py_ssize_t i;
4576 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4577 if (tmp == NULL) goto failed;
4578 if (!PyList_Check(tmp)) {
4579 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4580 goto failed;
4581 }
4582 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004583 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004584 if (targets == NULL) goto failed;
4585 for (i = 0; i < len; i++) {
4586 expr_ty value;
4587 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4588 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004589 if (len != PyList_GET_SIZE(tmp)) {
4590 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4591 goto failed;
4592 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004593 asdl_seq_SET(targets, i, value);
4594 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004595 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004596 } else {
4597 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4598 return 1;
4599 }
4600 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4601 int res;
4602 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4603 if (tmp == NULL) goto failed;
4604 res = obj2ast_expr(tmp, &value, arena);
4605 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004606 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004607 } else {
4608 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4609 return 1;
4610 }
4611 *out = Assign(targets, value, lineno, col_offset, arena);
4612 if (*out == NULL) goto failed;
4613 return 0;
4614 }
4615 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4616 if (isinstance == -1) {
4617 return 1;
4618 }
4619 if (isinstance) {
4620 expr_ty target;
4621 operator_ty op;
4622 expr_ty value;
4623
4624 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4625 int res;
4626 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4627 if (tmp == NULL) goto failed;
4628 res = obj2ast_expr(tmp, &target, arena);
4629 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004630 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004631 } else {
4632 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4633 return 1;
4634 }
4635 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4636 int res;
4637 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4638 if (tmp == NULL) goto failed;
4639 res = obj2ast_operator(tmp, &op, arena);
4640 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004641 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004642 } else {
4643 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4644 return 1;
4645 }
4646 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4647 int res;
4648 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4649 if (tmp == NULL) goto failed;
4650 res = obj2ast_expr(tmp, &value, arena);
4651 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004652 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004653 } else {
4654 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4655 return 1;
4656 }
4657 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4658 if (*out == NULL) goto failed;
4659 return 0;
4660 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004661 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4662 if (isinstance == -1) {
4663 return 1;
4664 }
4665 if (isinstance) {
4666 expr_ty target;
4667 expr_ty annotation;
4668 expr_ty value;
4669 int simple;
4670
4671 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4672 int res;
4673 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4674 if (tmp == NULL) goto failed;
4675 res = obj2ast_expr(tmp, &target, arena);
4676 if (res != 0) goto failed;
4677 Py_CLEAR(tmp);
4678 } else {
4679 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4680 return 1;
4681 }
4682 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4683 int res;
4684 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4685 if (tmp == NULL) goto failed;
4686 res = obj2ast_expr(tmp, &annotation, arena);
4687 if (res != 0) goto failed;
4688 Py_CLEAR(tmp);
4689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4691 return 1;
4692 }
4693 if (exists_not_none(obj, &PyId_value)) {
4694 int res;
4695 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4696 if (tmp == NULL) goto failed;
4697 res = obj2ast_expr(tmp, &value, arena);
4698 if (res != 0) goto failed;
4699 Py_CLEAR(tmp);
4700 } else {
4701 value = NULL;
4702 }
4703 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4704 int res;
4705 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4706 if (tmp == NULL) goto failed;
4707 res = obj2ast_int(tmp, &simple, arena);
4708 if (res != 0) goto failed;
4709 Py_CLEAR(tmp);
4710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4712 return 1;
4713 }
4714 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4715 arena);
4716 if (*out == NULL) goto failed;
4717 return 0;
4718 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004719 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4720 if (isinstance == -1) {
4721 return 1;
4722 }
4723 if (isinstance) {
4724 expr_ty target;
4725 expr_ty iter;
4726 asdl_seq* body;
4727 asdl_seq* orelse;
4728
4729 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4730 int res;
4731 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4732 if (tmp == NULL) goto failed;
4733 res = obj2ast_expr(tmp, &target, arena);
4734 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004735 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004736 } else {
4737 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4738 return 1;
4739 }
4740 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4741 int res;
4742 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4743 if (tmp == NULL) goto failed;
4744 res = obj2ast_expr(tmp, &iter, arena);
4745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004747 } else {
4748 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4749 return 1;
4750 }
4751 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4752 int res;
4753 Py_ssize_t len;
4754 Py_ssize_t i;
4755 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4756 if (tmp == NULL) goto failed;
4757 if (!PyList_Check(tmp)) {
4758 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4759 goto failed;
4760 }
4761 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004762 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004763 if (body == NULL) goto failed;
4764 for (i = 0; i < len; i++) {
4765 stmt_ty value;
4766 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4767 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004768 if (len != PyList_GET_SIZE(tmp)) {
4769 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4770 goto failed;
4771 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004772 asdl_seq_SET(body, i, value);
4773 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004774 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004775 } else {
4776 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4777 return 1;
4778 }
4779 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4780 int res;
4781 Py_ssize_t len;
4782 Py_ssize_t i;
4783 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4784 if (tmp == NULL) goto failed;
4785 if (!PyList_Check(tmp)) {
4786 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4787 goto failed;
4788 }
4789 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004790 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004791 if (orelse == NULL) goto failed;
4792 for (i = 0; i < len; i++) {
4793 stmt_ty value;
4794 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4795 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004796 if (len != PyList_GET_SIZE(tmp)) {
4797 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4798 goto failed;
4799 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004800 asdl_seq_SET(orelse, i, value);
4801 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004802 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004803 } else {
4804 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4805 return 1;
4806 }
4807 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4808 if (*out == NULL) goto failed;
4809 return 0;
4810 }
Yury Selivanov75445082015-05-11 22:57:16 -04004811 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4812 if (isinstance == -1) {
4813 return 1;
4814 }
4815 if (isinstance) {
4816 expr_ty target;
4817 expr_ty iter;
4818 asdl_seq* body;
4819 asdl_seq* orelse;
4820
4821 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4822 int res;
4823 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4824 if (tmp == NULL) goto failed;
4825 res = obj2ast_expr(tmp, &target, arena);
4826 if (res != 0) goto failed;
4827 Py_CLEAR(tmp);
4828 } else {
4829 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4830 return 1;
4831 }
4832 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4833 int res;
4834 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4835 if (tmp == NULL) goto failed;
4836 res = obj2ast_expr(tmp, &iter, arena);
4837 if (res != 0) goto failed;
4838 Py_CLEAR(tmp);
4839 } else {
4840 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4841 return 1;
4842 }
4843 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4844 int res;
4845 Py_ssize_t len;
4846 Py_ssize_t i;
4847 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4848 if (tmp == NULL) goto failed;
4849 if (!PyList_Check(tmp)) {
4850 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4851 goto failed;
4852 }
4853 len = PyList_GET_SIZE(tmp);
4854 body = _Py_asdl_seq_new(len, arena);
4855 if (body == NULL) goto failed;
4856 for (i = 0; i < len; i++) {
4857 stmt_ty value;
4858 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4859 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004860 if (len != PyList_GET_SIZE(tmp)) {
4861 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4862 goto failed;
4863 }
Yury Selivanov75445082015-05-11 22:57:16 -04004864 asdl_seq_SET(body, i, value);
4865 }
4866 Py_CLEAR(tmp);
4867 } else {
4868 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4869 return 1;
4870 }
4871 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4872 int res;
4873 Py_ssize_t len;
4874 Py_ssize_t i;
4875 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4876 if (tmp == NULL) goto failed;
4877 if (!PyList_Check(tmp)) {
4878 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4879 goto failed;
4880 }
4881 len = PyList_GET_SIZE(tmp);
4882 orelse = _Py_asdl_seq_new(len, arena);
4883 if (orelse == NULL) goto failed;
4884 for (i = 0; i < len; i++) {
4885 stmt_ty value;
4886 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4887 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004888 if (len != PyList_GET_SIZE(tmp)) {
4889 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4890 goto failed;
4891 }
Yury Selivanov75445082015-05-11 22:57:16 -04004892 asdl_seq_SET(orelse, i, value);
4893 }
4894 Py_CLEAR(tmp);
4895 } else {
4896 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4897 return 1;
4898 }
4899 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4900 if (*out == NULL) goto failed;
4901 return 0;
4902 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004903 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4904 if (isinstance == -1) {
4905 return 1;
4906 }
4907 if (isinstance) {
4908 expr_ty test;
4909 asdl_seq* body;
4910 asdl_seq* orelse;
4911
4912 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4913 int res;
4914 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4915 if (tmp == NULL) goto failed;
4916 res = obj2ast_expr(tmp, &test, arena);
4917 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004918 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004919 } else {
4920 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4921 return 1;
4922 }
4923 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4924 int res;
4925 Py_ssize_t len;
4926 Py_ssize_t i;
4927 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4928 if (tmp == NULL) goto failed;
4929 if (!PyList_Check(tmp)) {
4930 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4931 goto failed;
4932 }
4933 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004934 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004935 if (body == NULL) goto failed;
4936 for (i = 0; i < len; i++) {
4937 stmt_ty value;
4938 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4939 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004940 if (len != PyList_GET_SIZE(tmp)) {
4941 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
4942 goto failed;
4943 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004944 asdl_seq_SET(body, i, value);
4945 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004947 } else {
4948 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4949 return 1;
4950 }
4951 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4952 int res;
4953 Py_ssize_t len;
4954 Py_ssize_t i;
4955 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4956 if (tmp == NULL) goto failed;
4957 if (!PyList_Check(tmp)) {
4958 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4959 goto failed;
4960 }
4961 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004962 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004963 if (orelse == NULL) goto failed;
4964 for (i = 0; i < len; i++) {
4965 stmt_ty value;
4966 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4967 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004968 if (len != PyList_GET_SIZE(tmp)) {
4969 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
4970 goto failed;
4971 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004972 asdl_seq_SET(orelse, i, value);
4973 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004975 } else {
4976 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4977 return 1;
4978 }
4979 *out = While(test, body, orelse, lineno, col_offset, arena);
4980 if (*out == NULL) goto failed;
4981 return 0;
4982 }
4983 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4984 if (isinstance == -1) {
4985 return 1;
4986 }
4987 if (isinstance) {
4988 expr_ty test;
4989 asdl_seq* body;
4990 asdl_seq* orelse;
4991
4992 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4993 int res;
4994 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4995 if (tmp == NULL) goto failed;
4996 res = obj2ast_expr(tmp, &test, arena);
4997 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004998 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004999 } else {
5000 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5001 return 1;
5002 }
5003 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5004 int res;
5005 Py_ssize_t len;
5006 Py_ssize_t i;
5007 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5008 if (tmp == NULL) goto failed;
5009 if (!PyList_Check(tmp)) {
5010 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5011 goto failed;
5012 }
5013 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005014 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005015 if (body == NULL) goto failed;
5016 for (i = 0; i < len; i++) {
5017 stmt_ty value;
5018 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5019 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005020 if (len != PyList_GET_SIZE(tmp)) {
5021 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5022 goto failed;
5023 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005024 asdl_seq_SET(body, i, value);
5025 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005026 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005027 } else {
5028 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5029 return 1;
5030 }
5031 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5032 int res;
5033 Py_ssize_t len;
5034 Py_ssize_t i;
5035 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5036 if (tmp == NULL) goto failed;
5037 if (!PyList_Check(tmp)) {
5038 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5039 goto failed;
5040 }
5041 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005042 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005043 if (orelse == NULL) goto failed;
5044 for (i = 0; i < len; i++) {
5045 stmt_ty value;
5046 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5047 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005048 if (len != PyList_GET_SIZE(tmp)) {
5049 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5050 goto failed;
5051 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005052 asdl_seq_SET(orelse, i, value);
5053 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005054 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005055 } else {
5056 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5057 return 1;
5058 }
5059 *out = If(test, body, orelse, lineno, col_offset, arena);
5060 if (*out == NULL) goto failed;
5061 return 0;
5062 }
5063 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5064 if (isinstance == -1) {
5065 return 1;
5066 }
5067 if (isinstance) {
5068 asdl_seq* items;
5069 asdl_seq* body;
5070
5071 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5072 int res;
5073 Py_ssize_t len;
5074 Py_ssize_t i;
5075 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5076 if (tmp == NULL) goto failed;
5077 if (!PyList_Check(tmp)) {
5078 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5079 goto failed;
5080 }
5081 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005082 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005083 if (items == NULL) goto failed;
5084 for (i = 0; i < len; i++) {
5085 withitem_ty value;
5086 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5087 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005088 if (len != PyList_GET_SIZE(tmp)) {
5089 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5090 goto failed;
5091 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005092 asdl_seq_SET(items, i, value);
5093 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005094 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005095 } else {
5096 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5097 return 1;
5098 }
5099 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5100 int res;
5101 Py_ssize_t len;
5102 Py_ssize_t i;
5103 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5104 if (tmp == NULL) goto failed;
5105 if (!PyList_Check(tmp)) {
5106 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5107 goto failed;
5108 }
5109 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005110 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005111 if (body == NULL) goto failed;
5112 for (i = 0; i < len; i++) {
5113 stmt_ty value;
5114 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5115 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005116 if (len != PyList_GET_SIZE(tmp)) {
5117 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5118 goto failed;
5119 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005120 asdl_seq_SET(body, i, value);
5121 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005122 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005123 } else {
5124 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5125 return 1;
5126 }
5127 *out = With(items, body, lineno, col_offset, arena);
5128 if (*out == NULL) goto failed;
5129 return 0;
5130 }
Yury Selivanov75445082015-05-11 22:57:16 -04005131 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5132 if (isinstance == -1) {
5133 return 1;
5134 }
5135 if (isinstance) {
5136 asdl_seq* items;
5137 asdl_seq* body;
5138
5139 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5140 int res;
5141 Py_ssize_t len;
5142 Py_ssize_t i;
5143 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5144 if (tmp == NULL) goto failed;
5145 if (!PyList_Check(tmp)) {
5146 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5147 goto failed;
5148 }
5149 len = PyList_GET_SIZE(tmp);
5150 items = _Py_asdl_seq_new(len, arena);
5151 if (items == NULL) goto failed;
5152 for (i = 0; i < len; i++) {
5153 withitem_ty value;
5154 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5155 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005156 if (len != PyList_GET_SIZE(tmp)) {
5157 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5158 goto failed;
5159 }
Yury Selivanov75445082015-05-11 22:57:16 -04005160 asdl_seq_SET(items, i, value);
5161 }
5162 Py_CLEAR(tmp);
5163 } else {
5164 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5165 return 1;
5166 }
5167 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5168 int res;
5169 Py_ssize_t len;
5170 Py_ssize_t i;
5171 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5172 if (tmp == NULL) goto failed;
5173 if (!PyList_Check(tmp)) {
5174 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5175 goto failed;
5176 }
5177 len = PyList_GET_SIZE(tmp);
5178 body = _Py_asdl_seq_new(len, arena);
5179 if (body == NULL) goto failed;
5180 for (i = 0; i < len; i++) {
5181 stmt_ty value;
5182 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5183 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005184 if (len != PyList_GET_SIZE(tmp)) {
5185 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5186 goto failed;
5187 }
Yury Selivanov75445082015-05-11 22:57:16 -04005188 asdl_seq_SET(body, i, value);
5189 }
5190 Py_CLEAR(tmp);
5191 } else {
5192 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5193 return 1;
5194 }
5195 *out = AsyncWith(items, body, lineno, col_offset, arena);
5196 if (*out == NULL) goto failed;
5197 return 0;
5198 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005199 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5200 if (isinstance == -1) {
5201 return 1;
5202 }
5203 if (isinstance) {
5204 expr_ty exc;
5205 expr_ty cause;
5206
Victor Stinneree4b59c2013-07-27 00:01:35 +02005207 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005208 int res;
5209 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5210 if (tmp == NULL) goto failed;
5211 res = obj2ast_expr(tmp, &exc, arena);
5212 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005213 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005214 } else {
5215 exc = NULL;
5216 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005217 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005218 int res;
5219 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5220 if (tmp == NULL) goto failed;
5221 res = obj2ast_expr(tmp, &cause, arena);
5222 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005224 } else {
5225 cause = NULL;
5226 }
5227 *out = Raise(exc, cause, lineno, col_offset, arena);
5228 if (*out == NULL) goto failed;
5229 return 0;
5230 }
5231 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5232 if (isinstance == -1) {
5233 return 1;
5234 }
5235 if (isinstance) {
5236 asdl_seq* body;
5237 asdl_seq* handlers;
5238 asdl_seq* orelse;
5239 asdl_seq* finalbody;
5240
5241 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5242 int res;
5243 Py_ssize_t len;
5244 Py_ssize_t i;
5245 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5246 if (tmp == NULL) goto failed;
5247 if (!PyList_Check(tmp)) {
5248 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5249 goto failed;
5250 }
5251 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005252 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005253 if (body == NULL) goto failed;
5254 for (i = 0; i < len; i++) {
5255 stmt_ty value;
5256 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5257 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005258 if (len != PyList_GET_SIZE(tmp)) {
5259 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5260 goto failed;
5261 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005262 asdl_seq_SET(body, i, value);
5263 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005264 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5267 return 1;
5268 }
5269 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5270 int res;
5271 Py_ssize_t len;
5272 Py_ssize_t i;
5273 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5274 if (tmp == NULL) goto failed;
5275 if (!PyList_Check(tmp)) {
5276 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5277 goto failed;
5278 }
5279 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005280 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005281 if (handlers == NULL) goto failed;
5282 for (i = 0; i < len; i++) {
5283 excepthandler_ty value;
5284 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5285 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005286 if (len != PyList_GET_SIZE(tmp)) {
5287 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5288 goto failed;
5289 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005290 asdl_seq_SET(handlers, 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 \"handlers\" missing from Try");
5295 return 1;
5296 }
5297 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5298 int res;
5299 Py_ssize_t len;
5300 Py_ssize_t i;
5301 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5302 if (tmp == NULL) goto failed;
5303 if (!PyList_Check(tmp)) {
5304 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5305 goto failed;
5306 }
5307 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005308 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005309 if (orelse == NULL) goto failed;
5310 for (i = 0; i < len; i++) {
5311 stmt_ty value;
5312 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5313 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005314 if (len != PyList_GET_SIZE(tmp)) {
5315 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5316 goto failed;
5317 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005318 asdl_seq_SET(orelse, i, value);
5319 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005320 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005321 } else {
5322 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5323 return 1;
5324 }
5325 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5326 int res;
5327 Py_ssize_t len;
5328 Py_ssize_t i;
5329 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5330 if (tmp == NULL) goto failed;
5331 if (!PyList_Check(tmp)) {
5332 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5333 goto failed;
5334 }
5335 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005336 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005337 if (finalbody == NULL) goto failed;
5338 for (i = 0; i < len; i++) {
5339 stmt_ty value;
5340 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5341 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005342 if (len != PyList_GET_SIZE(tmp)) {
5343 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5344 goto failed;
5345 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005346 asdl_seq_SET(finalbody, i, value);
5347 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005348 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005349 } else {
5350 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5351 return 1;
5352 }
5353 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5354 arena);
5355 if (*out == NULL) goto failed;
5356 return 0;
5357 }
5358 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5359 if (isinstance == -1) {
5360 return 1;
5361 }
5362 if (isinstance) {
5363 expr_ty test;
5364 expr_ty msg;
5365
5366 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5367 int res;
5368 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5369 if (tmp == NULL) goto failed;
5370 res = obj2ast_expr(tmp, &test, arena);
5371 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005372 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005373 } else {
5374 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5375 return 1;
5376 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005377 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005378 int res;
5379 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5380 if (tmp == NULL) goto failed;
5381 res = obj2ast_expr(tmp, &msg, arena);
5382 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005383 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005384 } else {
5385 msg = NULL;
5386 }
5387 *out = Assert(test, msg, lineno, col_offset, arena);
5388 if (*out == NULL) goto failed;
5389 return 0;
5390 }
5391 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5392 if (isinstance == -1) {
5393 return 1;
5394 }
5395 if (isinstance) {
5396 asdl_seq* names;
5397
5398 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5399 int res;
5400 Py_ssize_t len;
5401 Py_ssize_t i;
5402 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5403 if (tmp == NULL) goto failed;
5404 if (!PyList_Check(tmp)) {
5405 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5406 goto failed;
5407 }
5408 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005409 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005410 if (names == NULL) goto failed;
5411 for (i = 0; i < len; i++) {
5412 alias_ty value;
5413 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5414 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005415 if (len != PyList_GET_SIZE(tmp)) {
5416 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5417 goto failed;
5418 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005419 asdl_seq_SET(names, i, value);
5420 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005421 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005422 } else {
5423 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5424 return 1;
5425 }
5426 *out = Import(names, lineno, col_offset, arena);
5427 if (*out == NULL) goto failed;
5428 return 0;
5429 }
5430 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5431 if (isinstance == -1) {
5432 return 1;
5433 }
5434 if (isinstance) {
5435 identifier module;
5436 asdl_seq* names;
5437 int level;
5438
Victor Stinneree4b59c2013-07-27 00:01:35 +02005439 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005440 int res;
5441 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5442 if (tmp == NULL) goto failed;
5443 res = obj2ast_identifier(tmp, &module, arena);
5444 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005445 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005446 } else {
5447 module = NULL;
5448 }
5449 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5450 int res;
5451 Py_ssize_t len;
5452 Py_ssize_t i;
5453 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5454 if (tmp == NULL) goto failed;
5455 if (!PyList_Check(tmp)) {
5456 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5457 goto failed;
5458 }
5459 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005460 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005461 if (names == NULL) goto failed;
5462 for (i = 0; i < len; i++) {
5463 alias_ty value;
5464 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5465 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005466 if (len != PyList_GET_SIZE(tmp)) {
5467 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5468 goto failed;
5469 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005470 asdl_seq_SET(names, i, value);
5471 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005472 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005473 } else {
5474 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5475 return 1;
5476 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005477 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005478 int res;
5479 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5480 if (tmp == NULL) goto failed;
5481 res = obj2ast_int(tmp, &level, arena);
5482 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005483 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005484 } else {
5485 level = 0;
5486 }
5487 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5488 if (*out == NULL) goto failed;
5489 return 0;
5490 }
5491 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5492 if (isinstance == -1) {
5493 return 1;
5494 }
5495 if (isinstance) {
5496 asdl_seq* names;
5497
5498 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5499 int res;
5500 Py_ssize_t len;
5501 Py_ssize_t i;
5502 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5503 if (tmp == NULL) goto failed;
5504 if (!PyList_Check(tmp)) {
5505 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5506 goto failed;
5507 }
5508 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005509 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005510 if (names == NULL) goto failed;
5511 for (i = 0; i < len; i++) {
5512 identifier value;
5513 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5514 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005515 if (len != PyList_GET_SIZE(tmp)) {
5516 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5517 goto failed;
5518 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005519 asdl_seq_SET(names, i, value);
5520 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005521 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005522 } else {
5523 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5524 return 1;
5525 }
5526 *out = Global(names, lineno, col_offset, arena);
5527 if (*out == NULL) goto failed;
5528 return 0;
5529 }
5530 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5531 if (isinstance == -1) {
5532 return 1;
5533 }
5534 if (isinstance) {
5535 asdl_seq* names;
5536
5537 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5538 int res;
5539 Py_ssize_t len;
5540 Py_ssize_t i;
5541 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5542 if (tmp == NULL) goto failed;
5543 if (!PyList_Check(tmp)) {
5544 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5545 goto failed;
5546 }
5547 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005548 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005549 if (names == NULL) goto failed;
5550 for (i = 0; i < len; i++) {
5551 identifier value;
5552 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5553 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005554 if (len != PyList_GET_SIZE(tmp)) {
5555 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5556 goto failed;
5557 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005558 asdl_seq_SET(names, 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 \"names\" missing from Nonlocal");
5563 return 1;
5564 }
5565 *out = Nonlocal(names, lineno, col_offset, arena);
5566 if (*out == NULL) goto failed;
5567 return 0;
5568 }
5569 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5570 if (isinstance == -1) {
5571 return 1;
5572 }
5573 if (isinstance) {
5574 expr_ty value;
5575
5576 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5577 int res;
5578 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5579 if (tmp == NULL) goto failed;
5580 res = obj2ast_expr(tmp, &value, arena);
5581 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005582 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005583 } else {
5584 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5585 return 1;
5586 }
5587 *out = Expr(value, lineno, col_offset, arena);
5588 if (*out == NULL) goto failed;
5589 return 0;
5590 }
5591 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5592 if (isinstance == -1) {
5593 return 1;
5594 }
5595 if (isinstance) {
5596
5597 *out = Pass(lineno, col_offset, arena);
5598 if (*out == NULL) goto failed;
5599 return 0;
5600 }
5601 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5602 if (isinstance == -1) {
5603 return 1;
5604 }
5605 if (isinstance) {
5606
5607 *out = Break(lineno, col_offset, arena);
5608 if (*out == NULL) goto failed;
5609 return 0;
5610 }
5611 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5612 if (isinstance == -1) {
5613 return 1;
5614 }
5615 if (isinstance) {
5616
5617 *out = Continue(lineno, col_offset, arena);
5618 if (*out == NULL) goto failed;
5619 return 0;
5620 }
5621
5622 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5623 failed:
5624 Py_XDECREF(tmp);
5625 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005626}
5627
5628int
5629obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5630{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005631 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005632
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005633 PyObject *tmp = NULL;
5634 int lineno;
5635 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005636
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005637 if (obj == Py_None) {
5638 *out = NULL;
5639 return 0;
5640 }
5641 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5642 int res;
5643 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5644 if (tmp == NULL) goto failed;
5645 res = obj2ast_int(tmp, &lineno, arena);
5646 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005648 } else {
5649 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005650 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005651 }
5652 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5653 int res;
5654 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5655 if (tmp == NULL) goto failed;
5656 res = obj2ast_int(tmp, &col_offset, arena);
5657 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005658 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005659 } else {
5660 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5661 return 1;
5662 }
5663 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5664 if (isinstance == -1) {
5665 return 1;
5666 }
5667 if (isinstance) {
5668 boolop_ty op;
5669 asdl_seq* values;
5670
5671 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5672 int res;
5673 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5674 if (tmp == NULL) goto failed;
5675 res = obj2ast_boolop(tmp, &op, arena);
5676 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005677 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005678 } else {
5679 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5680 return 1;
5681 }
5682 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5683 int res;
5684 Py_ssize_t len;
5685 Py_ssize_t i;
5686 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5687 if (tmp == NULL) goto failed;
5688 if (!PyList_Check(tmp)) {
5689 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5690 goto failed;
5691 }
5692 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005693 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005694 if (values == NULL) goto failed;
5695 for (i = 0; i < len; i++) {
5696 expr_ty value;
5697 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5698 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005699 if (len != PyList_GET_SIZE(tmp)) {
5700 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5701 goto failed;
5702 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005703 asdl_seq_SET(values, i, value);
5704 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005705 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005706 } else {
5707 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5708 return 1;
5709 }
5710 *out = BoolOp(op, values, lineno, col_offset, arena);
5711 if (*out == NULL) goto failed;
5712 return 0;
5713 }
5714 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5715 if (isinstance == -1) {
5716 return 1;
5717 }
5718 if (isinstance) {
5719 expr_ty left;
5720 operator_ty op;
5721 expr_ty right;
5722
5723 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5724 int res;
5725 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5726 if (tmp == NULL) goto failed;
5727 res = obj2ast_expr(tmp, &left, arena);
5728 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005729 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005730 } else {
5731 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5732 return 1;
5733 }
5734 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5735 int res;
5736 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5737 if (tmp == NULL) goto failed;
5738 res = obj2ast_operator(tmp, &op, arena);
5739 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005740 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005741 } else {
5742 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5743 return 1;
5744 }
5745 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5746 int res;
5747 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5748 if (tmp == NULL) goto failed;
5749 res = obj2ast_expr(tmp, &right, arena);
5750 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005751 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005752 } else {
5753 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5754 return 1;
5755 }
5756 *out = BinOp(left, op, right, lineno, col_offset, arena);
5757 if (*out == NULL) goto failed;
5758 return 0;
5759 }
5760 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5761 if (isinstance == -1) {
5762 return 1;
5763 }
5764 if (isinstance) {
5765 unaryop_ty op;
5766 expr_ty operand;
5767
5768 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5769 int res;
5770 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5771 if (tmp == NULL) goto failed;
5772 res = obj2ast_unaryop(tmp, &op, arena);
5773 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005774 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005775 } else {
5776 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5777 return 1;
5778 }
5779 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5780 int res;
5781 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5782 if (tmp == NULL) goto failed;
5783 res = obj2ast_expr(tmp, &operand, arena);
5784 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005785 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005786 } else {
5787 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5788 return 1;
5789 }
5790 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5791 if (*out == NULL) goto failed;
5792 return 0;
5793 }
5794 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5795 if (isinstance == -1) {
5796 return 1;
5797 }
5798 if (isinstance) {
5799 arguments_ty args;
5800 expr_ty body;
5801
5802 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5803 int res;
5804 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5805 if (tmp == NULL) goto failed;
5806 res = obj2ast_arguments(tmp, &args, arena);
5807 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005808 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005809 } else {
5810 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5811 return 1;
5812 }
5813 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5814 int res;
5815 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5816 if (tmp == NULL) goto failed;
5817 res = obj2ast_expr(tmp, &body, arena);
5818 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005819 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005820 } else {
5821 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5822 return 1;
5823 }
5824 *out = Lambda(args, body, lineno, col_offset, arena);
5825 if (*out == NULL) goto failed;
5826 return 0;
5827 }
5828 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5829 if (isinstance == -1) {
5830 return 1;
5831 }
5832 if (isinstance) {
5833 expr_ty test;
5834 expr_ty body;
5835 expr_ty orelse;
5836
5837 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5838 int res;
5839 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5840 if (tmp == NULL) goto failed;
5841 res = obj2ast_expr(tmp, &test, arena);
5842 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005843 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005844 } else {
5845 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5846 return 1;
5847 }
5848 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5849 int res;
5850 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5851 if (tmp == NULL) goto failed;
5852 res = obj2ast_expr(tmp, &body, arena);
5853 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005854 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005855 } else {
5856 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5857 return 1;
5858 }
5859 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5860 int res;
5861 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5862 if (tmp == NULL) goto failed;
5863 res = obj2ast_expr(tmp, &orelse, arena);
5864 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005865 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005866 } else {
5867 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5868 return 1;
5869 }
5870 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5871 if (*out == NULL) goto failed;
5872 return 0;
5873 }
5874 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5875 if (isinstance == -1) {
5876 return 1;
5877 }
5878 if (isinstance) {
5879 asdl_seq* keys;
5880 asdl_seq* values;
5881
5882 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5883 int res;
5884 Py_ssize_t len;
5885 Py_ssize_t i;
5886 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5887 if (tmp == NULL) goto failed;
5888 if (!PyList_Check(tmp)) {
5889 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5890 goto failed;
5891 }
5892 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005893 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005894 if (keys == NULL) goto failed;
5895 for (i = 0; i < len; i++) {
5896 expr_ty value;
5897 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5898 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005899 if (len != PyList_GET_SIZE(tmp)) {
5900 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5901 goto failed;
5902 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005903 asdl_seq_SET(keys, i, value);
5904 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005905 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005906 } else {
5907 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5908 return 1;
5909 }
5910 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5911 int res;
5912 Py_ssize_t len;
5913 Py_ssize_t i;
5914 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5915 if (tmp == NULL) goto failed;
5916 if (!PyList_Check(tmp)) {
5917 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5918 goto failed;
5919 }
5920 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005921 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005922 if (values == NULL) goto failed;
5923 for (i = 0; i < len; i++) {
5924 expr_ty value;
5925 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5926 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005927 if (len != PyList_GET_SIZE(tmp)) {
5928 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5929 goto failed;
5930 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005931 asdl_seq_SET(values, i, value);
5932 }
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 \"values\" missing from Dict");
5936 return 1;
5937 }
5938 *out = Dict(keys, values, lineno, col_offset, arena);
5939 if (*out == NULL) goto failed;
5940 return 0;
5941 }
5942 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5943 if (isinstance == -1) {
5944 return 1;
5945 }
5946 if (isinstance) {
5947 asdl_seq* elts;
5948
5949 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5950 int res;
5951 Py_ssize_t len;
5952 Py_ssize_t i;
5953 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5954 if (tmp == NULL) goto failed;
5955 if (!PyList_Check(tmp)) {
5956 PyErr_Format(PyExc_TypeError, "Set field \"elts\" 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 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005961 if (elts == NULL) goto failed;
5962 for (i = 0; i < len; i++) {
5963 expr_ty value;
5964 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5965 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005966 if (len != PyList_GET_SIZE(tmp)) {
5967 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
5968 goto failed;
5969 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005970 asdl_seq_SET(elts, i, value);
5971 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005972 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005973 } else {
5974 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5975 return 1;
5976 }
5977 *out = Set(elts, lineno, col_offset, arena);
5978 if (*out == NULL) goto failed;
5979 return 0;
5980 }
5981 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5982 if (isinstance == -1) {
5983 return 1;
5984 }
5985 if (isinstance) {
5986 expr_ty elt;
5987 asdl_seq* generators;
5988
5989 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5990 int res;
5991 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5992 if (tmp == NULL) goto failed;
5993 res = obj2ast_expr(tmp, &elt, arena);
5994 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005995 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005996 } else {
5997 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5998 return 1;
5999 }
6000 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6001 int res;
6002 Py_ssize_t len;
6003 Py_ssize_t i;
6004 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6005 if (tmp == NULL) goto failed;
6006 if (!PyList_Check(tmp)) {
6007 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6008 goto failed;
6009 }
6010 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006011 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006012 if (generators == NULL) goto failed;
6013 for (i = 0; i < len; i++) {
6014 comprehension_ty value;
6015 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6016 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006017 if (len != PyList_GET_SIZE(tmp)) {
6018 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6019 goto failed;
6020 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006021 asdl_seq_SET(generators, i, value);
6022 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006023 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006024 } else {
6025 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6026 return 1;
6027 }
6028 *out = ListComp(elt, generators, lineno, col_offset, arena);
6029 if (*out == NULL) goto failed;
6030 return 0;
6031 }
6032 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6033 if (isinstance == -1) {
6034 return 1;
6035 }
6036 if (isinstance) {
6037 expr_ty elt;
6038 asdl_seq* generators;
6039
6040 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6041 int res;
6042 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6043 if (tmp == NULL) goto failed;
6044 res = obj2ast_expr(tmp, &elt, arena);
6045 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006046 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006047 } else {
6048 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6049 return 1;
6050 }
6051 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6052 int res;
6053 Py_ssize_t len;
6054 Py_ssize_t i;
6055 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6056 if (tmp == NULL) goto failed;
6057 if (!PyList_Check(tmp)) {
6058 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6059 goto failed;
6060 }
6061 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006062 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006063 if (generators == NULL) goto failed;
6064 for (i = 0; i < len; i++) {
6065 comprehension_ty value;
6066 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6067 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006068 if (len != PyList_GET_SIZE(tmp)) {
6069 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6070 goto failed;
6071 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006072 asdl_seq_SET(generators, i, value);
6073 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006074 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006075 } else {
6076 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6077 return 1;
6078 }
6079 *out = SetComp(elt, generators, lineno, col_offset, arena);
6080 if (*out == NULL) goto failed;
6081 return 0;
6082 }
6083 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6084 if (isinstance == -1) {
6085 return 1;
6086 }
6087 if (isinstance) {
6088 expr_ty key;
6089 expr_ty value;
6090 asdl_seq* generators;
6091
6092 if (_PyObject_HasAttrId(obj, &PyId_key)) {
6093 int res;
6094 tmp = _PyObject_GetAttrId(obj, &PyId_key);
6095 if (tmp == NULL) goto failed;
6096 res = obj2ast_expr(tmp, &key, arena);
6097 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006098 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006099 } else {
6100 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6101 return 1;
6102 }
6103 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6104 int res;
6105 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6106 if (tmp == NULL) goto failed;
6107 res = obj2ast_expr(tmp, &value, arena);
6108 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006109 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006110 } else {
6111 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6112 return 1;
6113 }
6114 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6115 int res;
6116 Py_ssize_t len;
6117 Py_ssize_t i;
6118 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6119 if (tmp == NULL) goto failed;
6120 if (!PyList_Check(tmp)) {
6121 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6122 goto failed;
6123 }
6124 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006125 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006126 if (generators == NULL) goto failed;
6127 for (i = 0; i < len; i++) {
6128 comprehension_ty value;
6129 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6130 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006131 if (len != PyList_GET_SIZE(tmp)) {
6132 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6133 goto failed;
6134 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006135 asdl_seq_SET(generators, i, value);
6136 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006137 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006138 } else {
6139 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6140 return 1;
6141 }
6142 *out = DictComp(key, value, generators, lineno, col_offset, arena);
6143 if (*out == NULL) goto failed;
6144 return 0;
6145 }
6146 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6147 if (isinstance == -1) {
6148 return 1;
6149 }
6150 if (isinstance) {
6151 expr_ty elt;
6152 asdl_seq* generators;
6153
6154 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6155 int res;
6156 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6157 if (tmp == NULL) goto failed;
6158 res = obj2ast_expr(tmp, &elt, arena);
6159 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006160 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006161 } else {
6162 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6163 return 1;
6164 }
6165 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6166 int res;
6167 Py_ssize_t len;
6168 Py_ssize_t i;
6169 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6170 if (tmp == NULL) goto failed;
6171 if (!PyList_Check(tmp)) {
6172 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6173 goto failed;
6174 }
6175 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006176 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006177 if (generators == NULL) goto failed;
6178 for (i = 0; i < len; i++) {
6179 comprehension_ty value;
6180 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6181 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006182 if (len != PyList_GET_SIZE(tmp)) {
6183 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6184 goto failed;
6185 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006186 asdl_seq_SET(generators, i, value);
6187 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006188 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006189 } else {
6190 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6191 return 1;
6192 }
6193 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6194 if (*out == NULL) goto failed;
6195 return 0;
6196 }
Yury Selivanov75445082015-05-11 22:57:16 -04006197 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6198 if (isinstance == -1) {
6199 return 1;
6200 }
6201 if (isinstance) {
6202 expr_ty value;
6203
6204 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6205 int res;
6206 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6207 if (tmp == NULL) goto failed;
6208 res = obj2ast_expr(tmp, &value, arena);
6209 if (res != 0) goto failed;
6210 Py_CLEAR(tmp);
6211 } else {
6212 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6213 return 1;
6214 }
6215 *out = Await(value, lineno, col_offset, arena);
6216 if (*out == NULL) goto failed;
6217 return 0;
6218 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006219 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6220 if (isinstance == -1) {
6221 return 1;
6222 }
6223 if (isinstance) {
6224 expr_ty value;
6225
Victor Stinneree4b59c2013-07-27 00:01:35 +02006226 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006227 int res;
6228 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6229 if (tmp == NULL) goto failed;
6230 res = obj2ast_expr(tmp, &value, arena);
6231 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006232 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006233 } else {
6234 value = NULL;
6235 }
6236 *out = Yield(value, lineno, col_offset, arena);
6237 if (*out == NULL) goto failed;
6238 return 0;
6239 }
6240 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6241 if (isinstance == -1) {
6242 return 1;
6243 }
6244 if (isinstance) {
6245 expr_ty value;
6246
6247 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6248 int res;
6249 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6250 if (tmp == NULL) goto failed;
6251 res = obj2ast_expr(tmp, &value, arena);
6252 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006253 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006254 } else {
6255 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6256 return 1;
6257 }
6258 *out = YieldFrom(value, lineno, col_offset, arena);
6259 if (*out == NULL) goto failed;
6260 return 0;
6261 }
6262 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6263 if (isinstance == -1) {
6264 return 1;
6265 }
6266 if (isinstance) {
6267 expr_ty left;
6268 asdl_int_seq* ops;
6269 asdl_seq* comparators;
6270
6271 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6272 int res;
6273 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6274 if (tmp == NULL) goto failed;
6275 res = obj2ast_expr(tmp, &left, arena);
6276 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006277 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006278 } else {
6279 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6280 return 1;
6281 }
6282 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6283 int res;
6284 Py_ssize_t len;
6285 Py_ssize_t i;
6286 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6287 if (tmp == NULL) goto failed;
6288 if (!PyList_Check(tmp)) {
6289 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6290 goto failed;
6291 }
6292 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006293 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006294 if (ops == NULL) goto failed;
6295 for (i = 0; i < len; i++) {
6296 cmpop_ty value;
6297 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6298 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006299 if (len != PyList_GET_SIZE(tmp)) {
6300 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6301 goto failed;
6302 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006303 asdl_seq_SET(ops, i, value);
6304 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006305 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006306 } else {
6307 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6308 return 1;
6309 }
6310 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6311 int res;
6312 Py_ssize_t len;
6313 Py_ssize_t i;
6314 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6315 if (tmp == NULL) goto failed;
6316 if (!PyList_Check(tmp)) {
6317 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6318 goto failed;
6319 }
6320 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006321 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006322 if (comparators == NULL) goto failed;
6323 for (i = 0; i < len; i++) {
6324 expr_ty value;
6325 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6326 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006327 if (len != PyList_GET_SIZE(tmp)) {
6328 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6329 goto failed;
6330 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006331 asdl_seq_SET(comparators, i, value);
6332 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006333 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006334 } else {
6335 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6336 return 1;
6337 }
6338 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6339 if (*out == NULL) goto failed;
6340 return 0;
6341 }
6342 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6343 if (isinstance == -1) {
6344 return 1;
6345 }
6346 if (isinstance) {
6347 expr_ty func;
6348 asdl_seq* args;
6349 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006350
6351 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6352 int res;
6353 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6354 if (tmp == NULL) goto failed;
6355 res = obj2ast_expr(tmp, &func, arena);
6356 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006357 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006358 } else {
6359 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6360 return 1;
6361 }
6362 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6363 int res;
6364 Py_ssize_t len;
6365 Py_ssize_t i;
6366 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6367 if (tmp == NULL) goto failed;
6368 if (!PyList_Check(tmp)) {
6369 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6370 goto failed;
6371 }
6372 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006373 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006374 if (args == NULL) goto failed;
6375 for (i = 0; i < len; i++) {
6376 expr_ty value;
6377 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6378 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006379 if (len != PyList_GET_SIZE(tmp)) {
6380 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6381 goto failed;
6382 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006383 asdl_seq_SET(args, i, value);
6384 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006385 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386 } else {
6387 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6388 return 1;
6389 }
6390 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6391 int res;
6392 Py_ssize_t len;
6393 Py_ssize_t i;
6394 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6395 if (tmp == NULL) goto failed;
6396 if (!PyList_Check(tmp)) {
6397 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6398 goto failed;
6399 }
6400 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006401 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006402 if (keywords == NULL) goto failed;
6403 for (i = 0; i < len; i++) {
6404 keyword_ty value;
6405 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6406 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006407 if (len != PyList_GET_SIZE(tmp)) {
6408 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6409 goto failed;
6410 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006411 asdl_seq_SET(keywords, i, value);
6412 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006413 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006414 } else {
6415 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6416 return 1;
6417 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006418 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006419 if (*out == NULL) goto failed;
6420 return 0;
6421 }
6422 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6423 if (isinstance == -1) {
6424 return 1;
6425 }
6426 if (isinstance) {
6427 object n;
6428
6429 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6430 int res;
6431 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6432 if (tmp == NULL) goto failed;
6433 res = obj2ast_object(tmp, &n, arena);
6434 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006436 } else {
6437 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6438 return 1;
6439 }
6440 *out = Num(n, lineno, col_offset, arena);
6441 if (*out == NULL) goto failed;
6442 return 0;
6443 }
6444 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6445 if (isinstance == -1) {
6446 return 1;
6447 }
6448 if (isinstance) {
6449 string s;
6450
6451 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6452 int res;
6453 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6454 if (tmp == NULL) goto failed;
6455 res = obj2ast_string(tmp, &s, arena);
6456 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006457 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006458 } else {
6459 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6460 return 1;
6461 }
6462 *out = Str(s, lineno, col_offset, arena);
6463 if (*out == NULL) goto failed;
6464 return 0;
6465 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006466 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6467 if (isinstance == -1) {
6468 return 1;
6469 }
6470 if (isinstance) {
6471 expr_ty value;
6472 int conversion;
6473 expr_ty format_spec;
6474
6475 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6476 int res;
6477 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6478 if (tmp == NULL) goto failed;
6479 res = obj2ast_expr(tmp, &value, arena);
6480 if (res != 0) goto failed;
6481 Py_CLEAR(tmp);
6482 } else {
6483 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6484 return 1;
6485 }
6486 if (exists_not_none(obj, &PyId_conversion)) {
6487 int res;
6488 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6489 if (tmp == NULL) goto failed;
6490 res = obj2ast_int(tmp, &conversion, arena);
6491 if (res != 0) goto failed;
6492 Py_CLEAR(tmp);
6493 } else {
6494 conversion = 0;
6495 }
6496 if (exists_not_none(obj, &PyId_format_spec)) {
6497 int res;
6498 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6499 if (tmp == NULL) goto failed;
6500 res = obj2ast_expr(tmp, &format_spec, arena);
6501 if (res != 0) goto failed;
6502 Py_CLEAR(tmp);
6503 } else {
6504 format_spec = NULL;
6505 }
6506 *out = FormattedValue(value, conversion, format_spec, lineno,
6507 col_offset, arena);
6508 if (*out == NULL) goto failed;
6509 return 0;
6510 }
6511 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6512 if (isinstance == -1) {
6513 return 1;
6514 }
6515 if (isinstance) {
6516 asdl_seq* values;
6517
6518 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6519 int res;
6520 Py_ssize_t len;
6521 Py_ssize_t i;
6522 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6523 if (tmp == NULL) goto failed;
6524 if (!PyList_Check(tmp)) {
6525 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6526 goto failed;
6527 }
6528 len = PyList_GET_SIZE(tmp);
6529 values = _Py_asdl_seq_new(len, arena);
6530 if (values == NULL) goto failed;
6531 for (i = 0; i < len; i++) {
6532 expr_ty value;
6533 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6534 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03006535 if (len != PyList_GET_SIZE(tmp)) {
6536 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
6537 goto failed;
6538 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006539 asdl_seq_SET(values, i, value);
6540 }
6541 Py_CLEAR(tmp);
6542 } else {
6543 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6544 return 1;
6545 }
6546 *out = JoinedStr(values, lineno, col_offset, arena);
6547 if (*out == NULL) goto failed;
6548 return 0;
6549 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6551 if (isinstance == -1) {
6552 return 1;
6553 }
6554 if (isinstance) {
6555 bytes s;
6556
6557 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6558 int res;
6559 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6560 if (tmp == NULL) goto failed;
6561 res = obj2ast_bytes(tmp, &s, arena);
6562 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006563 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006564 } else {
6565 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6566 return 1;
6567 }
6568 *out = Bytes(s, lineno, col_offset, arena);
6569 if (*out == NULL) goto failed;
6570 return 0;
6571 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006572 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6573 if (isinstance == -1) {
6574 return 1;
6575 }
6576 if (isinstance) {
6577 singleton value;
6578
6579 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6580 int res;
6581 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6582 if (tmp == NULL) goto failed;
6583 res = obj2ast_singleton(tmp, &value, arena);
6584 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006585 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006586 } else {
6587 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6588 return 1;
6589 }
6590 *out = NameConstant(value, lineno, col_offset, arena);
6591 if (*out == NULL) goto failed;
6592 return 0;
6593 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006594 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6595 if (isinstance == -1) {
6596 return 1;
6597 }
6598 if (isinstance) {
6599
6600 *out = Ellipsis(lineno, col_offset, arena);
6601 if (*out == NULL) goto failed;
6602 return 0;
6603 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006604 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6605 if (isinstance == -1) {
6606 return 1;
6607 }
6608 if (isinstance) {
6609 constant value;
6610
6611 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6612 int res;
6613 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6614 if (tmp == NULL) goto failed;
6615 res = obj2ast_constant(tmp, &value, arena);
6616 if (res != 0) goto failed;
6617 Py_CLEAR(tmp);
6618 } else {
6619 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6620 return 1;
6621 }
6622 *out = Constant(value, lineno, col_offset, arena);
6623 if (*out == NULL) goto failed;
6624 return 0;
6625 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006626 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6627 if (isinstance == -1) {
6628 return 1;
6629 }
6630 if (isinstance) {
6631 expr_ty value;
6632 identifier attr;
6633 expr_context_ty ctx;
6634
6635 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6636 int res;
6637 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6638 if (tmp == NULL) goto failed;
6639 res = obj2ast_expr(tmp, &value, arena);
6640 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006641 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006642 } else {
6643 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6644 return 1;
6645 }
6646 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6647 int res;
6648 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6649 if (tmp == NULL) goto failed;
6650 res = obj2ast_identifier(tmp, &attr, arena);
6651 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006652 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006653 } else {
6654 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6655 return 1;
6656 }
6657 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6658 int res;
6659 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6660 if (tmp == NULL) goto failed;
6661 res = obj2ast_expr_context(tmp, &ctx, arena);
6662 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006663 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006664 } else {
6665 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6666 return 1;
6667 }
6668 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6669 if (*out == NULL) goto failed;
6670 return 0;
6671 }
6672 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6673 if (isinstance == -1) {
6674 return 1;
6675 }
6676 if (isinstance) {
6677 expr_ty value;
6678 slice_ty slice;
6679 expr_context_ty ctx;
6680
6681 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6682 int res;
6683 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6684 if (tmp == NULL) goto failed;
6685 res = obj2ast_expr(tmp, &value, arena);
6686 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006687 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006688 } else {
6689 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6690 return 1;
6691 }
6692 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6693 int res;
6694 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6695 if (tmp == NULL) goto failed;
6696 res = obj2ast_slice(tmp, &slice, arena);
6697 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006698 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006699 } else {
6700 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6701 return 1;
6702 }
6703 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6704 int res;
6705 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6706 if (tmp == NULL) goto failed;
6707 res = obj2ast_expr_context(tmp, &ctx, arena);
6708 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006710 } else {
6711 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6712 return 1;
6713 }
6714 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6715 if (*out == NULL) goto failed;
6716 return 0;
6717 }
6718 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6719 if (isinstance == -1) {
6720 return 1;
6721 }
6722 if (isinstance) {
6723 expr_ty value;
6724 expr_context_ty ctx;
6725
6726 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6727 int res;
6728 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6729 if (tmp == NULL) goto failed;
6730 res = obj2ast_expr(tmp, &value, arena);
6731 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006732 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006733 } else {
6734 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6735 return 1;
6736 }
6737 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6738 int res;
6739 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6740 if (tmp == NULL) goto failed;
6741 res = obj2ast_expr_context(tmp, &ctx, arena);
6742 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006743 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006744 } else {
6745 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6746 return 1;
6747 }
6748 *out = Starred(value, ctx, lineno, col_offset, arena);
6749 if (*out == NULL) goto failed;
6750 return 0;
6751 }
6752 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6753 if (isinstance == -1) {
6754 return 1;
6755 }
6756 if (isinstance) {
6757 identifier id;
6758 expr_context_ty ctx;
6759
6760 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6761 int res;
6762 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6763 if (tmp == NULL) goto failed;
6764 res = obj2ast_identifier(tmp, &id, arena);
6765 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006767 } else {
6768 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6769 return 1;
6770 }
6771 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6772 int res;
6773 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6774 if (tmp == NULL) goto failed;
6775 res = obj2ast_expr_context(tmp, &ctx, arena);
6776 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006777 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006778 } else {
6779 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6780 return 1;
6781 }
6782 *out = Name(id, ctx, lineno, col_offset, arena);
6783 if (*out == NULL) goto failed;
6784 return 0;
6785 }
6786 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6787 if (isinstance == -1) {
6788 return 1;
6789 }
6790 if (isinstance) {
6791 asdl_seq* elts;
6792 expr_context_ty ctx;
6793
6794 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6795 int res;
6796 Py_ssize_t len;
6797 Py_ssize_t i;
6798 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6799 if (tmp == NULL) goto failed;
6800 if (!PyList_Check(tmp)) {
6801 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6802 goto failed;
6803 }
6804 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006805 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006806 if (elts == NULL) goto failed;
6807 for (i = 0; i < len; i++) {
6808 expr_ty value;
6809 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6810 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006811 if (len != PyList_GET_SIZE(tmp)) {
6812 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6813 goto failed;
6814 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006815 asdl_seq_SET(elts, i, value);
6816 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006817 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006818 } else {
6819 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6820 return 1;
6821 }
6822 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6823 int res;
6824 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6825 if (tmp == NULL) goto failed;
6826 res = obj2ast_expr_context(tmp, &ctx, arena);
6827 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006828 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006829 } else {
6830 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6831 return 1;
6832 }
6833 *out = List(elts, ctx, lineno, col_offset, arena);
6834 if (*out == NULL) goto failed;
6835 return 0;
6836 }
6837 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6838 if (isinstance == -1) {
6839 return 1;
6840 }
6841 if (isinstance) {
6842 asdl_seq* elts;
6843 expr_context_ty ctx;
6844
6845 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6846 int res;
6847 Py_ssize_t len;
6848 Py_ssize_t i;
6849 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6850 if (tmp == NULL) goto failed;
6851 if (!PyList_Check(tmp)) {
6852 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6853 goto failed;
6854 }
6855 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006856 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006857 if (elts == NULL) goto failed;
6858 for (i = 0; i < len; i++) {
6859 expr_ty value;
6860 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6861 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006862 if (len != PyList_GET_SIZE(tmp)) {
6863 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6864 goto failed;
6865 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006866 asdl_seq_SET(elts, i, value);
6867 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006868 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006869 } else {
6870 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6871 return 1;
6872 }
6873 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6874 int res;
6875 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6876 if (tmp == NULL) goto failed;
6877 res = obj2ast_expr_context(tmp, &ctx, arena);
6878 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006880 } else {
6881 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6882 return 1;
6883 }
6884 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6885 if (*out == NULL) goto failed;
6886 return 0;
6887 }
6888
6889 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6890 failed:
6891 Py_XDECREF(tmp);
6892 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006893}
6894
6895int
6896obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6897{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006898 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006899
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006900 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6901 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006902 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006903 }
6904 if (isinstance) {
6905 *out = Load;
6906 return 0;
6907 }
6908 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6909 if (isinstance == -1) {
6910 return 1;
6911 }
6912 if (isinstance) {
6913 *out = Store;
6914 return 0;
6915 }
6916 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6917 if (isinstance == -1) {
6918 return 1;
6919 }
6920 if (isinstance) {
6921 *out = Del;
6922 return 0;
6923 }
6924 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6925 if (isinstance == -1) {
6926 return 1;
6927 }
6928 if (isinstance) {
6929 *out = AugLoad;
6930 return 0;
6931 }
6932 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6933 if (isinstance == -1) {
6934 return 1;
6935 }
6936 if (isinstance) {
6937 *out = AugStore;
6938 return 0;
6939 }
6940 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6941 if (isinstance == -1) {
6942 return 1;
6943 }
6944 if (isinstance) {
6945 *out = Param;
6946 return 0;
6947 }
6948
6949 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6950 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006951}
6952
6953int
6954obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6955{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006956 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006957
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006958 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006959
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006960 if (obj == Py_None) {
6961 *out = NULL;
6962 return 0;
6963 }
6964 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6965 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006966 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006967 }
6968 if (isinstance) {
6969 expr_ty lower;
6970 expr_ty upper;
6971 expr_ty step;
6972
Victor Stinneree4b59c2013-07-27 00:01:35 +02006973 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006974 int res;
6975 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6976 if (tmp == NULL) goto failed;
6977 res = obj2ast_expr(tmp, &lower, arena);
6978 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006979 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006980 } else {
6981 lower = NULL;
6982 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006983 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006984 int res;
6985 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6986 if (tmp == NULL) goto failed;
6987 res = obj2ast_expr(tmp, &upper, arena);
6988 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006989 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006990 } else {
6991 upper = NULL;
6992 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006993 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006994 int res;
6995 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6996 if (tmp == NULL) goto failed;
6997 res = obj2ast_expr(tmp, &step, arena);
6998 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006999 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007000 } else {
7001 step = NULL;
7002 }
7003 *out = Slice(lower, upper, step, arena);
7004 if (*out == NULL) goto failed;
7005 return 0;
7006 }
7007 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7008 if (isinstance == -1) {
7009 return 1;
7010 }
7011 if (isinstance) {
7012 asdl_seq* dims;
7013
7014 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
7015 int res;
7016 Py_ssize_t len;
7017 Py_ssize_t i;
7018 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
7019 if (tmp == NULL) goto failed;
7020 if (!PyList_Check(tmp)) {
7021 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7022 goto failed;
7023 }
7024 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007025 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007026 if (dims == NULL) goto failed;
7027 for (i = 0; i < len; i++) {
7028 slice_ty value;
7029 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
7030 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007031 if (len != PyList_GET_SIZE(tmp)) {
7032 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7033 goto failed;
7034 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007035 asdl_seq_SET(dims, i, value);
7036 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007037 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007038 } else {
7039 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7040 return 1;
7041 }
7042 *out = ExtSlice(dims, arena);
7043 if (*out == NULL) goto failed;
7044 return 0;
7045 }
7046 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7047 if (isinstance == -1) {
7048 return 1;
7049 }
7050 if (isinstance) {
7051 expr_ty value;
7052
7053 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7054 int res;
7055 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7056 if (tmp == NULL) goto failed;
7057 res = obj2ast_expr(tmp, &value, arena);
7058 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007059 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007060 } else {
7061 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7062 return 1;
7063 }
7064 *out = Index(value, arena);
7065 if (*out == NULL) goto failed;
7066 return 0;
7067 }
7068
7069 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7070 failed:
7071 Py_XDECREF(tmp);
7072 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007073}
7074
7075int
7076obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7077{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007078 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007079
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007080 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7081 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007082 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007083 }
7084 if (isinstance) {
7085 *out = And;
7086 return 0;
7087 }
7088 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7089 if (isinstance == -1) {
7090 return 1;
7091 }
7092 if (isinstance) {
7093 *out = Or;
7094 return 0;
7095 }
7096
7097 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7098 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007099}
7100
7101int
7102obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7103{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007104 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007105
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007106 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7107 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007108 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007109 }
7110 if (isinstance) {
7111 *out = Add;
7112 return 0;
7113 }
7114 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7115 if (isinstance == -1) {
7116 return 1;
7117 }
7118 if (isinstance) {
7119 *out = Sub;
7120 return 0;
7121 }
7122 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7123 if (isinstance == -1) {
7124 return 1;
7125 }
7126 if (isinstance) {
7127 *out = Mult;
7128 return 0;
7129 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007130 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7131 if (isinstance == -1) {
7132 return 1;
7133 }
7134 if (isinstance) {
7135 *out = MatMult;
7136 return 0;
7137 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007138 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7139 if (isinstance == -1) {
7140 return 1;
7141 }
7142 if (isinstance) {
7143 *out = Div;
7144 return 0;
7145 }
7146 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7147 if (isinstance == -1) {
7148 return 1;
7149 }
7150 if (isinstance) {
7151 *out = Mod;
7152 return 0;
7153 }
7154 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7155 if (isinstance == -1) {
7156 return 1;
7157 }
7158 if (isinstance) {
7159 *out = Pow;
7160 return 0;
7161 }
7162 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7163 if (isinstance == -1) {
7164 return 1;
7165 }
7166 if (isinstance) {
7167 *out = LShift;
7168 return 0;
7169 }
7170 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7171 if (isinstance == -1) {
7172 return 1;
7173 }
7174 if (isinstance) {
7175 *out = RShift;
7176 return 0;
7177 }
7178 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7179 if (isinstance == -1) {
7180 return 1;
7181 }
7182 if (isinstance) {
7183 *out = BitOr;
7184 return 0;
7185 }
7186 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7187 if (isinstance == -1) {
7188 return 1;
7189 }
7190 if (isinstance) {
7191 *out = BitXor;
7192 return 0;
7193 }
7194 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7195 if (isinstance == -1) {
7196 return 1;
7197 }
7198 if (isinstance) {
7199 *out = BitAnd;
7200 return 0;
7201 }
7202 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7203 if (isinstance == -1) {
7204 return 1;
7205 }
7206 if (isinstance) {
7207 *out = FloorDiv;
7208 return 0;
7209 }
7210
7211 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7212 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007213}
7214
7215int
7216obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7217{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007218 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007219
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007220 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7221 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007222 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007223 }
7224 if (isinstance) {
7225 *out = Invert;
7226 return 0;
7227 }
7228 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7229 if (isinstance == -1) {
7230 return 1;
7231 }
7232 if (isinstance) {
7233 *out = Not;
7234 return 0;
7235 }
7236 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7237 if (isinstance == -1) {
7238 return 1;
7239 }
7240 if (isinstance) {
7241 *out = UAdd;
7242 return 0;
7243 }
7244 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7245 if (isinstance == -1) {
7246 return 1;
7247 }
7248 if (isinstance) {
7249 *out = USub;
7250 return 0;
7251 }
7252
7253 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7254 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007255}
7256
7257int
7258obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7259{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007260 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007261
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007262 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7263 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007264 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007265 }
7266 if (isinstance) {
7267 *out = Eq;
7268 return 0;
7269 }
7270 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7271 if (isinstance == -1) {
7272 return 1;
7273 }
7274 if (isinstance) {
7275 *out = NotEq;
7276 return 0;
7277 }
7278 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7279 if (isinstance == -1) {
7280 return 1;
7281 }
7282 if (isinstance) {
7283 *out = Lt;
7284 return 0;
7285 }
7286 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7287 if (isinstance == -1) {
7288 return 1;
7289 }
7290 if (isinstance) {
7291 *out = LtE;
7292 return 0;
7293 }
7294 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7295 if (isinstance == -1) {
7296 return 1;
7297 }
7298 if (isinstance) {
7299 *out = Gt;
7300 return 0;
7301 }
7302 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7303 if (isinstance == -1) {
7304 return 1;
7305 }
7306 if (isinstance) {
7307 *out = GtE;
7308 return 0;
7309 }
7310 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7311 if (isinstance == -1) {
7312 return 1;
7313 }
7314 if (isinstance) {
7315 *out = Is;
7316 return 0;
7317 }
7318 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7319 if (isinstance == -1) {
7320 return 1;
7321 }
7322 if (isinstance) {
7323 *out = IsNot;
7324 return 0;
7325 }
7326 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7327 if (isinstance == -1) {
7328 return 1;
7329 }
7330 if (isinstance) {
7331 *out = In;
7332 return 0;
7333 }
7334 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7335 if (isinstance == -1) {
7336 return 1;
7337 }
7338 if (isinstance) {
7339 *out = NotIn;
7340 return 0;
7341 }
7342
7343 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7344 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007345}
7346
7347int
7348obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7349{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007350 PyObject* tmp = NULL;
7351 expr_ty target;
7352 expr_ty iter;
7353 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007354 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007355
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007356 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7357 int res;
7358 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7359 if (tmp == NULL) goto failed;
7360 res = obj2ast_expr(tmp, &target, arena);
7361 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007362 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007363 } else {
7364 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007365 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007366 }
7367 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7368 int res;
7369 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7370 if (tmp == NULL) goto failed;
7371 res = obj2ast_expr(tmp, &iter, arena);
7372 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007373 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007374 } else {
7375 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7376 return 1;
7377 }
7378 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7379 int res;
7380 Py_ssize_t len;
7381 Py_ssize_t i;
7382 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7383 if (tmp == NULL) goto failed;
7384 if (!PyList_Check(tmp)) {
7385 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7386 goto failed;
7387 }
7388 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007389 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007390 if (ifs == NULL) goto failed;
7391 for (i = 0; i < len; i++) {
7392 expr_ty value;
7393 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7394 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007395 if (len != PyList_GET_SIZE(tmp)) {
7396 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7397 goto failed;
7398 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007399 asdl_seq_SET(ifs, i, value);
7400 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007401 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007402 } else {
7403 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7404 return 1;
7405 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007406 if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7407 int res;
7408 tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7409 if (tmp == NULL) goto failed;
7410 res = obj2ast_int(tmp, &is_async, arena);
7411 if (res != 0) goto failed;
7412 Py_CLEAR(tmp);
7413 } else {
7414 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7415 return 1;
7416 }
7417 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007418 return 0;
7419failed:
7420 Py_XDECREF(tmp);
7421 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007422}
7423
7424int
7425obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7426{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007427 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007428
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007429 PyObject *tmp = NULL;
7430 int lineno;
7431 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007432
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007433 if (obj == Py_None) {
7434 *out = NULL;
7435 return 0;
7436 }
7437 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7438 int res;
7439 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7440 if (tmp == NULL) goto failed;
7441 res = obj2ast_int(tmp, &lineno, arena);
7442 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007443 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007444 } else {
7445 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007446 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007447 }
7448 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7449 int res;
7450 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7451 if (tmp == NULL) goto failed;
7452 res = obj2ast_int(tmp, &col_offset, arena);
7453 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007454 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007455 } else {
7456 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7457 return 1;
7458 }
7459 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7460 if (isinstance == -1) {
7461 return 1;
7462 }
7463 if (isinstance) {
7464 expr_ty type;
7465 identifier name;
7466 asdl_seq* body;
7467
Victor Stinneree4b59c2013-07-27 00:01:35 +02007468 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007469 int res;
7470 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7471 if (tmp == NULL) goto failed;
7472 res = obj2ast_expr(tmp, &type, arena);
7473 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007474 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007475 } else {
7476 type = NULL;
7477 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007478 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007479 int res;
7480 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7481 if (tmp == NULL) goto failed;
7482 res = obj2ast_identifier(tmp, &name, arena);
7483 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007484 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007485 } else {
7486 name = NULL;
7487 }
7488 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7489 int res;
7490 Py_ssize_t len;
7491 Py_ssize_t i;
7492 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7493 if (tmp == NULL) goto failed;
7494 if (!PyList_Check(tmp)) {
7495 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7496 goto failed;
7497 }
7498 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007499 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007500 if (body == NULL) goto failed;
7501 for (i = 0; i < len; i++) {
7502 stmt_ty value;
7503 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7504 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007505 if (len != PyList_GET_SIZE(tmp)) {
7506 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7507 goto failed;
7508 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007509 asdl_seq_SET(body, i, value);
7510 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007511 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007512 } else {
7513 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7514 return 1;
7515 }
7516 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7517 if (*out == NULL) goto failed;
7518 return 0;
7519 }
7520
7521 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7522 failed:
7523 Py_XDECREF(tmp);
7524 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007525}
7526
7527int
7528obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007530 PyObject* tmp = NULL;
7531 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007532 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007533 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007534 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007535 arg_ty kwarg;
7536 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007537
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007538 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7539 int res;
7540 Py_ssize_t len;
7541 Py_ssize_t i;
7542 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7543 if (tmp == NULL) goto failed;
7544 if (!PyList_Check(tmp)) {
7545 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7546 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007547 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007548 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007549 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007550 if (args == NULL) goto failed;
7551 for (i = 0; i < len; i++) {
7552 arg_ty value;
7553 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7554 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007555 if (len != PyList_GET_SIZE(tmp)) {
7556 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7557 goto failed;
7558 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007559 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007560 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007561 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007562 } else {
7563 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007564 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007565 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007566 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007567 int res;
7568 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7569 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007570 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007571 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007572 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007573 } else {
7574 vararg = NULL;
7575 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007576 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7577 int res;
7578 Py_ssize_t len;
7579 Py_ssize_t i;
7580 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7581 if (tmp == NULL) goto failed;
7582 if (!PyList_Check(tmp)) {
7583 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7584 goto failed;
7585 }
7586 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007587 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007588 if (kwonlyargs == NULL) goto failed;
7589 for (i = 0; i < len; i++) {
7590 arg_ty value;
7591 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7592 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007593 if (len != PyList_GET_SIZE(tmp)) {
7594 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7595 goto failed;
7596 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007597 asdl_seq_SET(kwonlyargs, i, value);
7598 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007599 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007600 } else {
7601 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7602 return 1;
7603 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007604 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7605 int res;
7606 Py_ssize_t len;
7607 Py_ssize_t i;
7608 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7609 if (tmp == NULL) goto failed;
7610 if (!PyList_Check(tmp)) {
7611 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7612 goto failed;
7613 }
7614 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007615 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007616 if (kw_defaults == NULL) goto failed;
7617 for (i = 0; i < len; i++) {
7618 expr_ty value;
7619 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7620 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007621 if (len != PyList_GET_SIZE(tmp)) {
7622 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7623 goto failed;
7624 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007625 asdl_seq_SET(kw_defaults, i, value);
7626 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007627 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007628 } else {
7629 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7630 return 1;
7631 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007632 if (exists_not_none(obj, &PyId_kwarg)) {
7633 int res;
7634 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7635 if (tmp == NULL) goto failed;
7636 res = obj2ast_arg(tmp, &kwarg, arena);
7637 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007638 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007639 } else {
7640 kwarg = NULL;
7641 }
7642 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7643 int res;
7644 Py_ssize_t len;
7645 Py_ssize_t i;
7646 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7647 if (tmp == NULL) goto failed;
7648 if (!PyList_Check(tmp)) {
7649 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7650 goto failed;
7651 }
7652 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007653 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007654 if (defaults == NULL) goto failed;
7655 for (i = 0; i < len; i++) {
7656 expr_ty value;
7657 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7658 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007659 if (len != PyList_GET_SIZE(tmp)) {
7660 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7661 goto failed;
7662 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007663 asdl_seq_SET(defaults, i, value);
7664 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007665 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007666 } else {
7667 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7668 return 1;
7669 }
7670 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7671 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007672 return 0;
7673failed:
7674 Py_XDECREF(tmp);
7675 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007676}
7677
7678int
7679obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7680{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007681 PyObject* tmp = NULL;
7682 identifier arg;
7683 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007684 int lineno;
7685 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007686
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007687 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7688 int res;
7689 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7690 if (tmp == NULL) goto failed;
7691 res = obj2ast_identifier(tmp, &arg, arena);
7692 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007693 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007694 } else {
7695 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007696 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007697 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007698 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007699 int res;
7700 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7701 if (tmp == NULL) goto failed;
7702 res = obj2ast_expr(tmp, &annotation, arena);
7703 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007704 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007705 } else {
7706 annotation = NULL;
7707 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007708 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7709 int res;
7710 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7711 if (tmp == NULL) goto failed;
7712 res = obj2ast_int(tmp, &lineno, arena);
7713 if (res != 0) goto failed;
7714 Py_CLEAR(tmp);
7715 } else {
7716 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7717 return 1;
7718 }
7719 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7720 int res;
7721 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7722 if (tmp == NULL) goto failed;
7723 res = obj2ast_int(tmp, &col_offset, arena);
7724 if (res != 0) goto failed;
7725 Py_CLEAR(tmp);
7726 } else {
7727 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7728 return 1;
7729 }
7730 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007731 return 0;
7732failed:
7733 Py_XDECREF(tmp);
7734 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007735}
7736
7737int
7738obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7739{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007740 PyObject* tmp = NULL;
7741 identifier arg;
7742 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007743
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007744 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007745 int res;
7746 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7747 if (tmp == NULL) goto failed;
7748 res = obj2ast_identifier(tmp, &arg, arena);
7749 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007750 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007751 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007752 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007753 }
7754 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7755 int res;
7756 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7757 if (tmp == NULL) goto failed;
7758 res = obj2ast_expr(tmp, &value, arena);
7759 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007760 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007761 } else {
7762 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7763 return 1;
7764 }
7765 *out = keyword(arg, value, arena);
7766 return 0;
7767failed:
7768 Py_XDECREF(tmp);
7769 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007770}
7771
7772int
7773obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7774{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007775 PyObject* tmp = NULL;
7776 identifier name;
7777 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007778
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007779 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7780 int res;
7781 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7782 if (tmp == NULL) goto failed;
7783 res = obj2ast_identifier(tmp, &name, arena);
7784 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007785 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007786 } else {
7787 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007788 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007789 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007790 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007791 int res;
7792 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7793 if (tmp == NULL) goto failed;
7794 res = obj2ast_identifier(tmp, &asname, arena);
7795 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007796 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007797 } else {
7798 asname = NULL;
7799 }
7800 *out = alias(name, asname, arena);
7801 return 0;
7802failed:
7803 Py_XDECREF(tmp);
7804 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007805}
7806
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007807int
7808obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7809{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007810 PyObject* tmp = NULL;
7811 expr_ty context_expr;
7812 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007813
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007814 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7815 int res;
7816 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7817 if (tmp == NULL) goto failed;
7818 res = obj2ast_expr(tmp, &context_expr, arena);
7819 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007820 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007821 } else {
7822 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007823 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007824 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007825 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007826 int res;
7827 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7828 if (tmp == NULL) goto failed;
7829 res = obj2ast_expr(tmp, &optional_vars, arena);
7830 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007831 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007832 } else {
7833 optional_vars = NULL;
7834 }
7835 *out = withitem(context_expr, optional_vars, arena);
7836 return 0;
7837failed:
7838 Py_XDECREF(tmp);
7839 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007840}
7841
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007842
Martin v. Löwis1a214512008-06-11 05:26:20 +00007843static struct PyModuleDef _astmodule = {
7844 PyModuleDef_HEAD_INIT, "_ast"
7845};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007846PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007847PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007848{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007849 PyObject *m, *d;
7850 if (!init_types()) return NULL;
7851 m = PyModule_Create(&_astmodule);
7852 if (!m) return NULL;
7853 d = PyModule_GetDict(m);
7854 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007855 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007856 return NULL;
7857 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7858 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7859 NULL;
7860 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7861 0) return NULL;
7862 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7863 return NULL;
7864 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7865 NULL;
7866 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7867 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7868 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007869 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7870 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007871 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7872 return NULL;
7873 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7874 NULL;
7875 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7876 NULL;
7877 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7878 NULL;
7879 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7880 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007881 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7882 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007883 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007884 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7885 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007886 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7887 NULL;
7888 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7889 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007890 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7891 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007892 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7893 NULL;
7894 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7895 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7896 NULL;
7897 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7898 NULL;
7899 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7900 return NULL;
7901 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7902 NULL;
7903 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7904 return NULL;
7905 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7906 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7907 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7908 NULL;
7909 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7910 return NULL;
7911 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7912 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7913 NULL;
7914 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7915 NULL;
7916 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7917 NULL;
7918 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7919 NULL;
7920 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7921 NULL;
7922 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7923 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7924 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7925 return NULL;
7926 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7927 NULL;
7928 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7929 return NULL;
7930 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7931 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007932 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7933 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007934 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7935 NULL;
7936 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7937 return NULL;
7938 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7939 NULL;
7940 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7941 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7942 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007943 if (PyDict_SetItemString(d, "FormattedValue",
7944 (PyObject*)FormattedValue_type) < 0) return NULL;
7945 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7946 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007947 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7948 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007949 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7950 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007951 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7952 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007953 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7954 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007955 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7956 return NULL;
7957 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7958 return NULL;
7959 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7960 NULL;
7961 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7962 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7963 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7964 NULL;
7965 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7966 0) return NULL;
7967 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7968 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7969 NULL;
7970 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7971 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7972 NULL;
7973 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7974 return NULL;
7975 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7976 NULL;
7977 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7978 NULL;
7979 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7980 NULL;
7981 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7982 return NULL;
7983 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7984 NULL;
7985 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7986 NULL;
7987 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7988 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7989 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7990 return NULL;
7991 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7992 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7993 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007994 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7995 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007996 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7997 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7998 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7999 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
8000 NULL;
8001 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
8002 NULL;
8003 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
8004 NULL;
8005 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8006 NULL;
8007 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8008 NULL;
8009 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8010 return NULL;
8011 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8012 NULL;
8013 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8014 NULL;
8015 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8016 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8017 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8018 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8019 NULL;
8020 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8021 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8022 NULL;
8023 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8024 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8025 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8026 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8027 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8028 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8029 NULL;
8030 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8031 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8032 NULL;
8033 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8034 < 0) return NULL;
8035 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8036 < 0) return NULL;
8037 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8038 < 0) return NULL;
8039 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8040 return NULL;
8041 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8042 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8043 NULL;
8044 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8045 NULL;
8046 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8047 return NULL;
8048 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008049}
8050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008051
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008052PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008053{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008054 if (!init_types())
8055 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008056 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008057}
Martin v. Löwis5b222132007-06-10 09:51:05 +00008058
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008059/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
8060mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008061{
8062 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008063 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008064 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008065 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008066
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008067 req_type[0] = (PyObject*)Module_type;
8068 req_type[1] = (PyObject*)Expression_type;
8069 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008070
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008071 assert(0 <= mode && mode <= 2);
8072
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008073 if (!init_types())
8074 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008075
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008076 isinstance = PyObject_IsInstance(ast, req_type[mode]);
8077 if (isinstance == -1)
8078 return NULL;
8079 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008080 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8081 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008082 return NULL;
8083 }
8084 if (obj2ast_mod(ast, &res, arena) != 0)
8085 return NULL;
8086 else
8087 return res;
8088}
8089
8090int PyAST_Check(PyObject* obj)
8091{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008092 if (!init_types())
8093 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00008094 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008095}
8096
Martin v. Löwis5b222132007-06-10 09:51:05 +00008097