blob: dcf24f0df34a5b2da4cc64b313cecbd7f8a8fa4a [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
INADA Naokifc489082017-01-25 22:33:43 +0900507_Py_IDENTIFIER(_fields);
508_Py_IDENTIFIER(_attributes);
509
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700510typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100511 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700512 PyObject *dict;
513} AST_object;
514
Benjamin Peterson1767e022012-03-14 21:50:29 -0500515static void
516ast_dealloc(AST_object *self)
517{
518 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200519 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500520}
521
Neal Norwitz207c9f32008-03-31 04:42:11 +0000522static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700523ast_traverse(AST_object *self, visitproc visit, void *arg)
524{
525 Py_VISIT(self->dict);
526 return 0;
527}
528
529static void
530ast_clear(AST_object *self)
531{
532 Py_CLEAR(self->dict);
533}
534
535static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000536ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
537{
538 Py_ssize_t i, numfields = 0;
539 int res = -1;
540 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200541 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000542 if (!fields)
543 PyErr_Clear();
544 if (fields) {
545 numfields = PySequence_Size(fields);
546 if (numfields == -1)
547 goto cleanup;
548 }
549 res = 0; /* if no error occurs, this stays 0 to the end */
550 if (PyTuple_GET_SIZE(args) > 0) {
551 if (numfields != PyTuple_GET_SIZE(args)) {
552 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000553 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000554 Py_TYPE(self)->tp_name,
555 numfields == 0 ? "" : "either 0 or ",
556 numfields, numfields == 1 ? "" : "s");
557 res = -1;
558 goto cleanup;
559 }
560 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
561 /* cannot be reached when fields is NULL */
562 PyObject *name = PySequence_GetItem(fields, i);
563 if (!name) {
564 res = -1;
565 goto cleanup;
566 }
567 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
568 Py_DECREF(name);
569 if (res < 0)
570 goto cleanup;
571 }
572 }
573 if (kw) {
574 i = 0; /* needed by PyDict_Next */
575 while (PyDict_Next(kw, &i, &key, &value)) {
576 res = PyObject_SetAttr(self, key, value);
577 if (res < 0)
578 goto cleanup;
579 }
580 }
581 cleanup:
582 Py_XDECREF(fields);
583 return res;
584}
585
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000586/* Pickling support */
587static PyObject *
588ast_type_reduce(PyObject *self, PyObject *unused)
589{
590 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200591 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200592 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000593 if (dict == NULL) {
594 if (PyErr_ExceptionMatches(PyExc_AttributeError))
595 PyErr_Clear();
596 else
597 return NULL;
598 }
599 if (dict) {
600 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
601 Py_DECREF(dict);
602 return res;
603 }
604 return Py_BuildValue("O()", Py_TYPE(self));
605}
606
607static PyMethodDef ast_type_methods[] = {
608 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
609 {NULL}
610};
611
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700612static PyGetSetDef ast_type_getsets[] = {
613 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
614 {NULL}
615};
616
Neal Norwitz207c9f32008-03-31 04:42:11 +0000617static PyTypeObject AST_type = {
618 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000619 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700620 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000621 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500622 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000623 0, /* tp_print */
624 0, /* tp_getattr */
625 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000626 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000627 0, /* tp_repr */
628 0, /* tp_as_number */
629 0, /* tp_as_sequence */
630 0, /* tp_as_mapping */
631 0, /* tp_hash */
632 0, /* tp_call */
633 0, /* tp_str */
634 PyObject_GenericGetAttr, /* tp_getattro */
635 PyObject_GenericSetAttr, /* tp_setattro */
636 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700637 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000638 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700639 (traverseproc)ast_traverse, /* tp_traverse */
640 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641 0, /* tp_richcompare */
642 0, /* tp_weaklistoffset */
643 0, /* tp_iter */
644 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000645 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000646 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700647 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000648 0, /* tp_base */
649 0, /* tp_dict */
650 0, /* tp_descr_get */
651 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700652 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000653 (initproc)ast_type_init, /* tp_init */
654 PyType_GenericAlloc, /* tp_alloc */
655 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700656 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000657};
658
659
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
661{
INADA Naokifc489082017-01-25 22:33:43 +0900662 _Py_IDENTIFIER(__module__);
663 _Py_IDENTIFIER(_ast);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 PyObject *fnames, *result;
665 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000666 fnames = PyTuple_New(num_fields);
667 if (!fnames) return NULL;
668 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000669 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 if (!field) {
671 Py_DECREF(fnames);
672 return NULL;
673 }
674 PyTuple_SET_ITEM(fnames, i, field);
675 }
INADA Naokifc489082017-01-25 22:33:43 +0900676 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
677 type, base,
678 _PyUnicode_FromId(&PyId__fields), fnames,
679 _PyUnicode_FromId(&PyId___module__),
680 _PyUnicode_FromId(&PyId__ast));
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 Py_DECREF(fnames);
682 return (PyTypeObject*)result;
683}
684
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
686{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000687 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000688 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000689 if (!l)
690 return 0;
691 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000692 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!s) {
694 Py_DECREF(l);
695 return 0;
696 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000697 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200699 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700 Py_DECREF(l);
701 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702}
703
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000704/* Conversion AST -> Python */
705
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000706static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
707{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700708 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 PyObject *result = PyList_New(n);
710 PyObject *value;
711 if (!result)
712 return NULL;
713 for (i = 0; i < n; i++) {
714 value = func(asdl_seq_GET(seq, i));
715 if (!value) {
716 Py_DECREF(result);
717 return NULL;
718 }
719 PyList_SET_ITEM(result, i, value);
720 }
721 return result;
722}
723
724static PyObject* ast2obj_object(void *o)
725{
726 if (!o)
727 o = Py_None;
728 Py_INCREF((PyObject*)o);
729 return (PyObject*)o;
730}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500731#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100732#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733#define ast2obj_identifier ast2obj_object
734#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500735#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000736
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000737static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738{
Christian Heimes217cfd12007-12-02 14:31:20 +0000739 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740}
741
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000742/* Conversion Python -> AST */
743
Benjamin Peterson442f2092012-12-06 17:41:04 -0500744static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
745{
746 if (obj != Py_None && obj != Py_True && obj != Py_False) {
747 PyErr_SetString(PyExc_ValueError,
748 "AST singleton must be True, False, or None");
749 return 1;
750 }
751 *out = obj;
752 return 0;
753}
754
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000755static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
756{
757 if (obj == Py_None)
758 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200759 if (obj) {
760 if (PyArena_AddPyObject(arena, obj) < 0) {
761 *out = NULL;
762 return -1;
763 }
764 Py_INCREF(obj);
765 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000766 *out = obj;
767 return 0;
768}
769
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100770static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
771{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100772 if (obj) {
773 if (PyArena_AddPyObject(arena, obj) < 0) {
774 *out = NULL;
775 return -1;
776 }
777 Py_INCREF(obj);
778 }
779 *out = obj;
780 return 0;
781}
782
Benjamin Peterson180e6352011-07-22 11:09:07 -0500783static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500784{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500785 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
786 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500787 return 1;
788 }
789 return obj2ast_object(obj, out, arena);
790}
791
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500792static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
793{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400794 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500795 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
796 return 1;
797 }
798 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500799}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000800
Benjamin Petersone2498412011-08-09 16:08:39 -0500801static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
802{
803 if (!PyBytes_CheckExact(obj)) {
804 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
805 return 1;
806 }
807 return obj2ast_object(obj, out, arena);
808}
809
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000810static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
811{
812 int i;
813 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100814 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000815 return 1;
816 }
817
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300818 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000819 if (i == -1 && PyErr_Occurred())
820 return 1;
821 *out = i;
822 return 0;
823}
824
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000825static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000826{
827 PyObject *empty_tuple, *d;
828 if (PyType_Ready(&AST_type) < 0)
829 return -1;
830 d = AST_type.tp_dict;
831 empty_tuple = PyTuple_New(0);
832 if (!empty_tuple ||
INADA Naokifc489082017-01-25 22:33:43 +0900833 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
834 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +0000835 Py_XDECREF(empty_tuple);
836 return -1;
837 }
838 Py_DECREF(empty_tuple);
839 return 0;
840}
841
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700842static int exists_not_none(PyObject *obj, _Py_Identifier *id)
843{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700844 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700845 PyObject *attr = _PyObject_GetAttrId(obj, id);
846 if (!attr) {
847 PyErr_Clear();
848 return 0;
849 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700850 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700851 Py_DECREF(attr);
852 return !isnone;
853}
854
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000855
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000856static int init_types(void)
857{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200858 static int initialized;
859 if (initialized) return 1;
860 if (add_ast_fields() < 0) return 0;
861 mod_type = make_type("mod", &AST_type, NULL, 0);
862 if (!mod_type) return 0;
863 if (!add_attributes(mod_type, NULL, 0)) return 0;
864 Module_type = make_type("Module", mod_type, Module_fields, 1);
865 if (!Module_type) return 0;
866 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
867 1);
868 if (!Interactive_type) return 0;
869 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
870 if (!Expression_type) return 0;
871 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
872 if (!Suite_type) return 0;
873 stmt_type = make_type("stmt", &AST_type, NULL, 0);
874 if (!stmt_type) return 0;
875 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
876 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
877 5);
878 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400879 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
880 AsyncFunctionDef_fields, 5);
881 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400882 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200883 if (!ClassDef_type) return 0;
884 Return_type = make_type("Return", stmt_type, Return_fields, 1);
885 if (!Return_type) return 0;
886 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
887 if (!Delete_type) return 0;
888 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
889 if (!Assign_type) return 0;
890 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
891 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700892 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
893 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200894 For_type = make_type("For", stmt_type, For_fields, 4);
895 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400896 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
897 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200898 While_type = make_type("While", stmt_type, While_fields, 3);
899 if (!While_type) return 0;
900 If_type = make_type("If", stmt_type, If_fields, 3);
901 if (!If_type) return 0;
902 With_type = make_type("With", stmt_type, With_fields, 2);
903 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400904 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
905 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200906 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
907 if (!Raise_type) return 0;
908 Try_type = make_type("Try", stmt_type, Try_fields, 4);
909 if (!Try_type) return 0;
910 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
911 if (!Assert_type) return 0;
912 Import_type = make_type("Import", stmt_type, Import_fields, 1);
913 if (!Import_type) return 0;
914 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
915 if (!ImportFrom_type) return 0;
916 Global_type = make_type("Global", stmt_type, Global_fields, 1);
917 if (!Global_type) return 0;
918 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
919 if (!Nonlocal_type) return 0;
920 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
921 if (!Expr_type) return 0;
922 Pass_type = make_type("Pass", stmt_type, NULL, 0);
923 if (!Pass_type) return 0;
924 Break_type = make_type("Break", stmt_type, NULL, 0);
925 if (!Break_type) return 0;
926 Continue_type = make_type("Continue", stmt_type, NULL, 0);
927 if (!Continue_type) return 0;
928 expr_type = make_type("expr", &AST_type, NULL, 0);
929 if (!expr_type) return 0;
930 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
931 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
932 if (!BoolOp_type) return 0;
933 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
934 if (!BinOp_type) return 0;
935 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
936 if (!UnaryOp_type) return 0;
937 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
938 if (!Lambda_type) return 0;
939 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
940 if (!IfExp_type) return 0;
941 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
942 if (!Dict_type) return 0;
943 Set_type = make_type("Set", expr_type, Set_fields, 1);
944 if (!Set_type) return 0;
945 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
946 if (!ListComp_type) return 0;
947 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
948 if (!SetComp_type) return 0;
949 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
950 if (!DictComp_type) return 0;
951 GeneratorExp_type = make_type("GeneratorExp", expr_type,
952 GeneratorExp_fields, 2);
953 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400954 Await_type = make_type("Await", expr_type, Await_fields, 1);
955 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200956 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
957 if (!Yield_type) return 0;
958 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
959 if (!YieldFrom_type) return 0;
960 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
961 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400962 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200963 if (!Call_type) return 0;
964 Num_type = make_type("Num", expr_type, Num_fields, 1);
965 if (!Num_type) return 0;
966 Str_type = make_type("Str", expr_type, Str_fields, 1);
967 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400968 FormattedValue_type = make_type("FormattedValue", expr_type,
969 FormattedValue_fields, 3);
970 if (!FormattedValue_type) return 0;
971 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
972 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200973 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
974 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200975 NameConstant_type = make_type("NameConstant", expr_type,
976 NameConstant_fields, 1);
977 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200978 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
979 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100980 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
981 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200982 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
983 if (!Attribute_type) return 0;
984 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
985 if (!Subscript_type) return 0;
986 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
987 if (!Starred_type) return 0;
988 Name_type = make_type("Name", expr_type, Name_fields, 2);
989 if (!Name_type) return 0;
990 List_type = make_type("List", expr_type, List_fields, 2);
991 if (!List_type) return 0;
992 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
993 if (!Tuple_type) return 0;
994 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
995 if (!expr_context_type) return 0;
996 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
997 Load_type = make_type("Load", expr_context_type, NULL, 0);
998 if (!Load_type) return 0;
999 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
1000 if (!Load_singleton) return 0;
1001 Store_type = make_type("Store", expr_context_type, NULL, 0);
1002 if (!Store_type) return 0;
1003 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
1004 if (!Store_singleton) return 0;
1005 Del_type = make_type("Del", expr_context_type, NULL, 0);
1006 if (!Del_type) return 0;
1007 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1008 if (!Del_singleton) return 0;
1009 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1010 if (!AugLoad_type) return 0;
1011 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1012 if (!AugLoad_singleton) return 0;
1013 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1014 if (!AugStore_type) return 0;
1015 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1016 if (!AugStore_singleton) return 0;
1017 Param_type = make_type("Param", expr_context_type, NULL, 0);
1018 if (!Param_type) return 0;
1019 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1020 if (!Param_singleton) return 0;
1021 slice_type = make_type("slice", &AST_type, NULL, 0);
1022 if (!slice_type) return 0;
1023 if (!add_attributes(slice_type, NULL, 0)) return 0;
1024 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1025 if (!Slice_type) return 0;
1026 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1027 if (!ExtSlice_type) return 0;
1028 Index_type = make_type("Index", slice_type, Index_fields, 1);
1029 if (!Index_type) return 0;
1030 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1031 if (!boolop_type) return 0;
1032 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1033 And_type = make_type("And", boolop_type, NULL, 0);
1034 if (!And_type) return 0;
1035 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1036 if (!And_singleton) return 0;
1037 Or_type = make_type("Or", boolop_type, NULL, 0);
1038 if (!Or_type) return 0;
1039 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1040 if (!Or_singleton) return 0;
1041 operator_type = make_type("operator", &AST_type, NULL, 0);
1042 if (!operator_type) return 0;
1043 if (!add_attributes(operator_type, NULL, 0)) return 0;
1044 Add_type = make_type("Add", operator_type, NULL, 0);
1045 if (!Add_type) return 0;
1046 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1047 if (!Add_singleton) return 0;
1048 Sub_type = make_type("Sub", operator_type, NULL, 0);
1049 if (!Sub_type) return 0;
1050 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1051 if (!Sub_singleton) return 0;
1052 Mult_type = make_type("Mult", operator_type, NULL, 0);
1053 if (!Mult_type) return 0;
1054 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1055 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001056 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1057 if (!MatMult_type) return 0;
1058 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1059 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001060 Div_type = make_type("Div", operator_type, NULL, 0);
1061 if (!Div_type) return 0;
1062 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1063 if (!Div_singleton) return 0;
1064 Mod_type = make_type("Mod", operator_type, NULL, 0);
1065 if (!Mod_type) return 0;
1066 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1067 if (!Mod_singleton) return 0;
1068 Pow_type = make_type("Pow", operator_type, NULL, 0);
1069 if (!Pow_type) return 0;
1070 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1071 if (!Pow_singleton) return 0;
1072 LShift_type = make_type("LShift", operator_type, NULL, 0);
1073 if (!LShift_type) return 0;
1074 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1075 if (!LShift_singleton) return 0;
1076 RShift_type = make_type("RShift", operator_type, NULL, 0);
1077 if (!RShift_type) return 0;
1078 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1079 if (!RShift_singleton) return 0;
1080 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1081 if (!BitOr_type) return 0;
1082 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1083 if (!BitOr_singleton) return 0;
1084 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1085 if (!BitXor_type) return 0;
1086 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1087 if (!BitXor_singleton) return 0;
1088 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1089 if (!BitAnd_type) return 0;
1090 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1091 if (!BitAnd_singleton) return 0;
1092 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1093 if (!FloorDiv_type) return 0;
1094 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1095 if (!FloorDiv_singleton) return 0;
1096 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1097 if (!unaryop_type) return 0;
1098 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1099 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1100 if (!Invert_type) return 0;
1101 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1102 if (!Invert_singleton) return 0;
1103 Not_type = make_type("Not", unaryop_type, NULL, 0);
1104 if (!Not_type) return 0;
1105 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1106 if (!Not_singleton) return 0;
1107 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1108 if (!UAdd_type) return 0;
1109 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1110 if (!UAdd_singleton) return 0;
1111 USub_type = make_type("USub", unaryop_type, NULL, 0);
1112 if (!USub_type) return 0;
1113 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1114 if (!USub_singleton) return 0;
1115 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1116 if (!cmpop_type) return 0;
1117 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1118 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1119 if (!Eq_type) return 0;
1120 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1121 if (!Eq_singleton) return 0;
1122 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1123 if (!NotEq_type) return 0;
1124 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1125 if (!NotEq_singleton) return 0;
1126 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1127 if (!Lt_type) return 0;
1128 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1129 if (!Lt_singleton) return 0;
1130 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1131 if (!LtE_type) return 0;
1132 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1133 if (!LtE_singleton) return 0;
1134 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1135 if (!Gt_type) return 0;
1136 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1137 if (!Gt_singleton) return 0;
1138 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1139 if (!GtE_type) return 0;
1140 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1141 if (!GtE_singleton) return 0;
1142 Is_type = make_type("Is", cmpop_type, NULL, 0);
1143 if (!Is_type) return 0;
1144 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1145 if (!Is_singleton) return 0;
1146 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1147 if (!IsNot_type) return 0;
1148 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1149 if (!IsNot_singleton) return 0;
1150 In_type = make_type("In", cmpop_type, NULL, 0);
1151 if (!In_type) return 0;
1152 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1153 if (!In_singleton) return 0;
1154 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1155 if (!NotIn_type) return 0;
1156 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1157 if (!NotIn_singleton) return 0;
1158 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001159 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001160 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001161 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001162 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1163 if (!excepthandler_type) return 0;
1164 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1165 return 0;
1166 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1167 ExceptHandler_fields, 3);
1168 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001169 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001170 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001171 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001172 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1173 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001174 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1176 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001177 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001178 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1179 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001180 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001181 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1182 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001183 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001184 initialized = 1;
1185 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001186}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001187
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001188static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1189static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1190static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1191static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1192 arena);
1193static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1194static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1195static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1196static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1197static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1198static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1199 arena);
1200static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1201 arena);
1202static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1203static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1204static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1205static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001206static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001207
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001209Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001211 mod_ty p;
1212 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1213 if (!p)
1214 return NULL;
1215 p->kind = Module_kind;
1216 p->v.Module.body = body;
1217 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218}
1219
1220mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001223 mod_ty p;
1224 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1225 if (!p)
1226 return NULL;
1227 p->kind = Interactive_kind;
1228 p->v.Interactive.body = body;
1229 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230}
1231
1232mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001233Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001235 mod_ty p;
1236 if (!body) {
1237 PyErr_SetString(PyExc_ValueError,
1238 "field body is required for Expression");
1239 return NULL;
1240 }
1241 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1242 if (!p)
1243 return NULL;
1244 p->kind = Expression_kind;
1245 p->v.Expression.body = body;
1246 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247}
1248
1249mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001250Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001252 mod_ty p;
1253 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1254 if (!p)
1255 return NULL;
1256 p->kind = Suite_kind;
1257 p->v.Suite.body = body;
1258 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259}
1260
1261stmt_ty
1262FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001263 decorator_list, expr_ty returns, int lineno, int col_offset,
1264 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001266 stmt_ty p;
1267 if (!name) {
1268 PyErr_SetString(PyExc_ValueError,
1269 "field name is required for FunctionDef");
1270 return NULL;
1271 }
1272 if (!args) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "field args is required for FunctionDef");
1275 return NULL;
1276 }
1277 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1278 if (!p)
1279 return NULL;
1280 p->kind = FunctionDef_kind;
1281 p->v.FunctionDef.name = name;
1282 p->v.FunctionDef.args = args;
1283 p->v.FunctionDef.body = body;
1284 p->v.FunctionDef.decorator_list = decorator_list;
1285 p->v.FunctionDef.returns = returns;
1286 p->lineno = lineno;
1287 p->col_offset = col_offset;
1288 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001292AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1293 * decorator_list, expr_ty returns, int lineno, int col_offset,
1294 PyArena *arena)
1295{
1296 stmt_ty p;
1297 if (!name) {
1298 PyErr_SetString(PyExc_ValueError,
1299 "field name is required for AsyncFunctionDef");
1300 return NULL;
1301 }
1302 if (!args) {
1303 PyErr_SetString(PyExc_ValueError,
1304 "field args is required for AsyncFunctionDef");
1305 return NULL;
1306 }
1307 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1308 if (!p)
1309 return NULL;
1310 p->kind = AsyncFunctionDef_kind;
1311 p->v.AsyncFunctionDef.name = name;
1312 p->v.AsyncFunctionDef.args = args;
1313 p->v.AsyncFunctionDef.body = body;
1314 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1315 p->v.AsyncFunctionDef.returns = returns;
1316 p->lineno = lineno;
1317 p->col_offset = col_offset;
1318 return p;
1319}
1320
1321stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001322ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1323 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1324 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001326 stmt_ty p;
1327 if (!name) {
1328 PyErr_SetString(PyExc_ValueError,
1329 "field name is required for ClassDef");
1330 return NULL;
1331 }
1332 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1333 if (!p)
1334 return NULL;
1335 p->kind = ClassDef_kind;
1336 p->v.ClassDef.name = name;
1337 p->v.ClassDef.bases = bases;
1338 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001339 p->v.ClassDef.body = body;
1340 p->v.ClassDef.decorator_list = decorator_list;
1341 p->lineno = lineno;
1342 p->col_offset = col_offset;
1343 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344}
1345
1346stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001347Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001349 stmt_ty p;
1350 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1351 if (!p)
1352 return NULL;
1353 p->kind = Return_kind;
1354 p->v.Return.value = value;
1355 p->lineno = lineno;
1356 p->col_offset = col_offset;
1357 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358}
1359
1360stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001361Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001363 stmt_ty p;
1364 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1365 if (!p)
1366 return NULL;
1367 p->kind = Delete_kind;
1368 p->v.Delete.targets = targets;
1369 p->lineno = lineno;
1370 p->col_offset = col_offset;
1371 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372}
1373
1374stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001375Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1376 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001378 stmt_ty p;
1379 if (!value) {
1380 PyErr_SetString(PyExc_ValueError,
1381 "field value is required for Assign");
1382 return NULL;
1383 }
1384 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1385 if (!p)
1386 return NULL;
1387 p->kind = Assign_kind;
1388 p->v.Assign.targets = targets;
1389 p->v.Assign.value = value;
1390 p->lineno = lineno;
1391 p->col_offset = col_offset;
1392 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393}
1394
1395stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001396AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1397 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001399 stmt_ty p;
1400 if (!target) {
1401 PyErr_SetString(PyExc_ValueError,
1402 "field target is required for AugAssign");
1403 return NULL;
1404 }
1405 if (!op) {
1406 PyErr_SetString(PyExc_ValueError,
1407 "field op is required for AugAssign");
1408 return NULL;
1409 }
1410 if (!value) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field value is required for AugAssign");
1413 return NULL;
1414 }
1415 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1416 if (!p)
1417 return NULL;
1418 p->kind = AugAssign_kind;
1419 p->v.AugAssign.target = target;
1420 p->v.AugAssign.op = op;
1421 p->v.AugAssign.value = value;
1422 p->lineno = lineno;
1423 p->col_offset = col_offset;
1424 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425}
1426
1427stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001428AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1429 lineno, int col_offset, PyArena *arena)
1430{
1431 stmt_ty p;
1432 if (!target) {
1433 PyErr_SetString(PyExc_ValueError,
1434 "field target is required for AnnAssign");
1435 return NULL;
1436 }
1437 if (!annotation) {
1438 PyErr_SetString(PyExc_ValueError,
1439 "field annotation is required for AnnAssign");
1440 return NULL;
1441 }
1442 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1443 if (!p)
1444 return NULL;
1445 p->kind = AnnAssign_kind;
1446 p->v.AnnAssign.target = target;
1447 p->v.AnnAssign.annotation = annotation;
1448 p->v.AnnAssign.value = value;
1449 p->v.AnnAssign.simple = simple;
1450 p->lineno = lineno;
1451 p->col_offset = col_offset;
1452 return p;
1453}
1454
1455stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001457 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001459 stmt_ty p;
1460 if (!target) {
1461 PyErr_SetString(PyExc_ValueError,
1462 "field target is required for For");
1463 return NULL;
1464 }
1465 if (!iter) {
1466 PyErr_SetString(PyExc_ValueError,
1467 "field iter is required for For");
1468 return NULL;
1469 }
1470 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1471 if (!p)
1472 return NULL;
1473 p->kind = For_kind;
1474 p->v.For.target = target;
1475 p->v.For.iter = iter;
1476 p->v.For.body = body;
1477 p->v.For.orelse = orelse;
1478 p->lineno = lineno;
1479 p->col_offset = col_offset;
1480 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481}
1482
1483stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001484AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1485 lineno, int col_offset, PyArena *arena)
1486{
1487 stmt_ty p;
1488 if (!target) {
1489 PyErr_SetString(PyExc_ValueError,
1490 "field target is required for AsyncFor");
1491 return NULL;
1492 }
1493 if (!iter) {
1494 PyErr_SetString(PyExc_ValueError,
1495 "field iter is required for AsyncFor");
1496 return NULL;
1497 }
1498 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1499 if (!p)
1500 return NULL;
1501 p->kind = AsyncFor_kind;
1502 p->v.AsyncFor.target = target;
1503 p->v.AsyncFor.iter = iter;
1504 p->v.AsyncFor.body = body;
1505 p->v.AsyncFor.orelse = orelse;
1506 p->lineno = lineno;
1507 p->col_offset = col_offset;
1508 return p;
1509}
1510
1511stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001512While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1513 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001515 stmt_ty p;
1516 if (!test) {
1517 PyErr_SetString(PyExc_ValueError,
1518 "field test is required for While");
1519 return NULL;
1520 }
1521 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1522 if (!p)
1523 return NULL;
1524 p->kind = While_kind;
1525 p->v.While.test = test;
1526 p->v.While.body = body;
1527 p->v.While.orelse = orelse;
1528 p->lineno = lineno;
1529 p->col_offset = col_offset;
1530 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
1532
1533stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001534If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1535 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001537 stmt_ty p;
1538 if (!test) {
1539 PyErr_SetString(PyExc_ValueError,
1540 "field test is required for If");
1541 return NULL;
1542 }
1543 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1544 if (!p)
1545 return NULL;
1546 p->kind = If_kind;
1547 p->v.If.test = test;
1548 p->v.If.body = body;
1549 p->v.If.orelse = orelse;
1550 p->lineno = lineno;
1551 p->col_offset = col_offset;
1552 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553}
1554
1555stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001556With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1557 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001558{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001559 stmt_ty p;
1560 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1561 if (!p)
1562 return NULL;
1563 p->kind = With_kind;
1564 p->v.With.items = items;
1565 p->v.With.body = body;
1566 p->lineno = lineno;
1567 p->col_offset = col_offset;
1568 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001569}
1570
1571stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001572AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1573 PyArena *arena)
1574{
1575 stmt_ty p;
1576 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1577 if (!p)
1578 return NULL;
1579 p->kind = AsyncWith_kind;
1580 p->v.AsyncWith.items = items;
1581 p->v.AsyncWith.body = body;
1582 p->lineno = lineno;
1583 p->col_offset = col_offset;
1584 return p;
1585}
1586
1587stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001588Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001590 stmt_ty p;
1591 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1592 if (!p)
1593 return NULL;
1594 p->kind = Raise_kind;
1595 p->v.Raise.exc = exc;
1596 p->v.Raise.cause = cause;
1597 p->lineno = lineno;
1598 p->col_offset = col_offset;
1599 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001603Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1604 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001606 stmt_ty p;
1607 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1608 if (!p)
1609 return NULL;
1610 p->kind = Try_kind;
1611 p->v.Try.body = body;
1612 p->v.Try.handlers = handlers;
1613 p->v.Try.orelse = orelse;
1614 p->v.Try.finalbody = finalbody;
1615 p->lineno = lineno;
1616 p->col_offset = col_offset;
1617 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618}
1619
1620stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001621Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001623 stmt_ty p;
1624 if (!test) {
1625 PyErr_SetString(PyExc_ValueError,
1626 "field test is required for Assert");
1627 return NULL;
1628 }
1629 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1630 if (!p)
1631 return NULL;
1632 p->kind = Assert_kind;
1633 p->v.Assert.test = test;
1634 p->v.Assert.msg = msg;
1635 p->lineno = lineno;
1636 p->col_offset = col_offset;
1637 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638}
1639
1640stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001641Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001643 stmt_ty p;
1644 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1645 if (!p)
1646 return NULL;
1647 p->kind = Import_kind;
1648 p->v.Import.names = names;
1649 p->lineno = lineno;
1650 p->col_offset = col_offset;
1651 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652}
1653
1654stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1656 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001658 stmt_ty p;
1659 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1660 if (!p)
1661 return NULL;
1662 p->kind = ImportFrom_kind;
1663 p->v.ImportFrom.module = module;
1664 p->v.ImportFrom.names = names;
1665 p->v.ImportFrom.level = level;
1666 p->lineno = lineno;
1667 p->col_offset = col_offset;
1668 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
1671stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001672Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001674 stmt_ty p;
1675 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1676 if (!p)
1677 return NULL;
1678 p->kind = Global_kind;
1679 p->v.Global.names = names;
1680 p->lineno = lineno;
1681 p->col_offset = col_offset;
1682 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683}
1684
1685stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001686Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1687{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001688 stmt_ty p;
1689 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1690 if (!p)
1691 return NULL;
1692 p->kind = Nonlocal_kind;
1693 p->v.Nonlocal.names = names;
1694 p->lineno = lineno;
1695 p->col_offset = col_offset;
1696 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001697}
1698
1699stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001700Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001702 stmt_ty p;
1703 if (!value) {
1704 PyErr_SetString(PyExc_ValueError,
1705 "field value is required for Expr");
1706 return NULL;
1707 }
1708 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1709 if (!p)
1710 return NULL;
1711 p->kind = Expr_kind;
1712 p->v.Expr.value = value;
1713 p->lineno = lineno;
1714 p->col_offset = col_offset;
1715 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716}
1717
1718stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001719Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001721 stmt_ty p;
1722 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1723 if (!p)
1724 return NULL;
1725 p->kind = Pass_kind;
1726 p->lineno = lineno;
1727 p->col_offset = col_offset;
1728 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729}
1730
1731stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001732Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001734 stmt_ty p;
1735 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1736 if (!p)
1737 return NULL;
1738 p->kind = Break_kind;
1739 p->lineno = lineno;
1740 p->col_offset = col_offset;
1741 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742}
1743
1744stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001745Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001747 stmt_ty p;
1748 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1749 if (!p)
1750 return NULL;
1751 p->kind = Continue_kind;
1752 p->lineno = lineno;
1753 p->col_offset = col_offset;
1754 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755}
1756
1757expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001758BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1759 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001761 expr_ty p;
1762 if (!op) {
1763 PyErr_SetString(PyExc_ValueError,
1764 "field op is required for BoolOp");
1765 return NULL;
1766 }
1767 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1768 if (!p)
1769 return NULL;
1770 p->kind = BoolOp_kind;
1771 p->v.BoolOp.op = op;
1772 p->v.BoolOp.values = values;
1773 p->lineno = lineno;
1774 p->col_offset = col_offset;
1775 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776}
1777
1778expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001779BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1780 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001782 expr_ty p;
1783 if (!left) {
1784 PyErr_SetString(PyExc_ValueError,
1785 "field left is required for BinOp");
1786 return NULL;
1787 }
1788 if (!op) {
1789 PyErr_SetString(PyExc_ValueError,
1790 "field op is required for BinOp");
1791 return NULL;
1792 }
1793 if (!right) {
1794 PyErr_SetString(PyExc_ValueError,
1795 "field right is required for BinOp");
1796 return NULL;
1797 }
1798 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1799 if (!p)
1800 return NULL;
1801 p->kind = BinOp_kind;
1802 p->v.BinOp.left = left;
1803 p->v.BinOp.op = op;
1804 p->v.BinOp.right = right;
1805 p->lineno = lineno;
1806 p->col_offset = col_offset;
1807 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808}
1809
1810expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001811UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1812 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001814 expr_ty p;
1815 if (!op) {
1816 PyErr_SetString(PyExc_ValueError,
1817 "field op is required for UnaryOp");
1818 return NULL;
1819 }
1820 if (!operand) {
1821 PyErr_SetString(PyExc_ValueError,
1822 "field operand is required for UnaryOp");
1823 return NULL;
1824 }
1825 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1826 if (!p)
1827 return NULL;
1828 p->kind = UnaryOp_kind;
1829 p->v.UnaryOp.op = op;
1830 p->v.UnaryOp.operand = operand;
1831 p->lineno = lineno;
1832 p->col_offset = col_offset;
1833 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834}
1835
1836expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001837Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1838 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001840 expr_ty p;
1841 if (!args) {
1842 PyErr_SetString(PyExc_ValueError,
1843 "field args is required for Lambda");
1844 return NULL;
1845 }
1846 if (!body) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field body is required for Lambda");
1849 return NULL;
1850 }
1851 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1852 if (!p)
1853 return NULL;
1854 p->kind = Lambda_kind;
1855 p->v.Lambda.args = args;
1856 p->v.Lambda.body = body;
1857 p->lineno = lineno;
1858 p->col_offset = col_offset;
1859 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860}
1861
1862expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001863IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1864 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001865{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001866 expr_ty p;
1867 if (!test) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field test is required for IfExp");
1870 return NULL;
1871 }
1872 if (!body) {
1873 PyErr_SetString(PyExc_ValueError,
1874 "field body is required for IfExp");
1875 return NULL;
1876 }
1877 if (!orelse) {
1878 PyErr_SetString(PyExc_ValueError,
1879 "field orelse is required for IfExp");
1880 return NULL;
1881 }
1882 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1883 if (!p)
1884 return NULL;
1885 p->kind = IfExp_kind;
1886 p->v.IfExp.test = test;
1887 p->v.IfExp.body = body;
1888 p->v.IfExp.orelse = orelse;
1889 p->lineno = lineno;
1890 p->col_offset = col_offset;
1891 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001892}
1893
1894expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001895Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1896 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001898 expr_ty p;
1899 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1900 if (!p)
1901 return NULL;
1902 p->kind = Dict_kind;
1903 p->v.Dict.keys = keys;
1904 p->v.Dict.values = values;
1905 p->lineno = lineno;
1906 p->col_offset = col_offset;
1907 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908}
1909
1910expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001911Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1912{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001913 expr_ty p;
1914 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1915 if (!p)
1916 return NULL;
1917 p->kind = Set_kind;
1918 p->v.Set.elts = elts;
1919 p->lineno = lineno;
1920 p->col_offset = col_offset;
1921 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001922}
1923
1924expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001925ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1926 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001928 expr_ty p;
1929 if (!elt) {
1930 PyErr_SetString(PyExc_ValueError,
1931 "field elt is required for ListComp");
1932 return NULL;
1933 }
1934 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1935 if (!p)
1936 return NULL;
1937 p->kind = ListComp_kind;
1938 p->v.ListComp.elt = elt;
1939 p->v.ListComp.generators = generators;
1940 p->lineno = lineno;
1941 p->col_offset = col_offset;
1942 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943}
1944
1945expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001946SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1947 *arena)
1948{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001949 expr_ty p;
1950 if (!elt) {
1951 PyErr_SetString(PyExc_ValueError,
1952 "field elt is required for SetComp");
1953 return NULL;
1954 }
1955 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1956 if (!p)
1957 return NULL;
1958 p->kind = SetComp_kind;
1959 p->v.SetComp.elt = elt;
1960 p->v.SetComp.generators = generators;
1961 p->lineno = lineno;
1962 p->col_offset = col_offset;
1963 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001964}
1965
1966expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001967DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1968 col_offset, PyArena *arena)
1969{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001970 expr_ty p;
1971 if (!key) {
1972 PyErr_SetString(PyExc_ValueError,
1973 "field key is required for DictComp");
1974 return NULL;
1975 }
1976 if (!value) {
1977 PyErr_SetString(PyExc_ValueError,
1978 "field value is required for DictComp");
1979 return NULL;
1980 }
1981 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1982 if (!p)
1983 return NULL;
1984 p->kind = DictComp_kind;
1985 p->v.DictComp.key = key;
1986 p->v.DictComp.value = value;
1987 p->v.DictComp.generators = generators;
1988 p->lineno = lineno;
1989 p->col_offset = col_offset;
1990 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001991}
1992
1993expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001994GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1995 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001997 expr_ty p;
1998 if (!elt) {
1999 PyErr_SetString(PyExc_ValueError,
2000 "field elt is required for GeneratorExp");
2001 return NULL;
2002 }
2003 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2004 if (!p)
2005 return NULL;
2006 p->kind = GeneratorExp_kind;
2007 p->v.GeneratorExp.elt = elt;
2008 p->v.GeneratorExp.generators = generators;
2009 p->lineno = lineno;
2010 p->col_offset = col_offset;
2011 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012}
2013
2014expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002015Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2016{
2017 expr_ty p;
2018 if (!value) {
2019 PyErr_SetString(PyExc_ValueError,
2020 "field value is required for Await");
2021 return NULL;
2022 }
2023 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2024 if (!p)
2025 return NULL;
2026 p->kind = Await_kind;
2027 p->v.Await.value = value;
2028 p->lineno = lineno;
2029 p->col_offset = col_offset;
2030 return p;
2031}
2032
2033expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002034Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002036 expr_ty p;
2037 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2038 if (!p)
2039 return NULL;
2040 p->kind = Yield_kind;
2041 p->v.Yield.value = value;
2042 p->lineno = lineno;
2043 p->col_offset = col_offset;
2044 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045}
2046
2047expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002048YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2049{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002050 expr_ty p;
2051 if (!value) {
2052 PyErr_SetString(PyExc_ValueError,
2053 "field value is required for YieldFrom");
2054 return NULL;
2055 }
2056 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2057 if (!p)
2058 return NULL;
2059 p->kind = YieldFrom_kind;
2060 p->v.YieldFrom.value = value;
2061 p->lineno = lineno;
2062 p->col_offset = col_offset;
2063 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002064}
2065
2066expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002067Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2068 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002070 expr_ty p;
2071 if (!left) {
2072 PyErr_SetString(PyExc_ValueError,
2073 "field left is required for Compare");
2074 return NULL;
2075 }
2076 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2077 if (!p)
2078 return NULL;
2079 p->kind = Compare_kind;
2080 p->v.Compare.left = left;
2081 p->v.Compare.ops = ops;
2082 p->v.Compare.comparators = comparators;
2083 p->lineno = lineno;
2084 p->col_offset = col_offset;
2085 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086}
2087
2088expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002089Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2090 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002092 expr_ty p;
2093 if (!func) {
2094 PyErr_SetString(PyExc_ValueError,
2095 "field func is required for Call");
2096 return NULL;
2097 }
2098 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2099 if (!p)
2100 return NULL;
2101 p->kind = Call_kind;
2102 p->v.Call.func = func;
2103 p->v.Call.args = args;
2104 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002105 p->lineno = lineno;
2106 p->col_offset = col_offset;
2107 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108}
2109
2110expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002111Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002113 expr_ty p;
2114 if (!n) {
2115 PyErr_SetString(PyExc_ValueError,
2116 "field n is required for Num");
2117 return NULL;
2118 }
2119 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2120 if (!p)
2121 return NULL;
2122 p->kind = Num_kind;
2123 p->v.Num.n = n;
2124 p->lineno = lineno;
2125 p->col_offset = col_offset;
2126 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127}
2128
2129expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002130Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002132 expr_ty p;
2133 if (!s) {
2134 PyErr_SetString(PyExc_ValueError,
2135 "field s is required for Str");
2136 return NULL;
2137 }
2138 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2139 if (!p)
2140 return NULL;
2141 p->kind = Str_kind;
2142 p->v.Str.s = s;
2143 p->lineno = lineno;
2144 p->col_offset = col_offset;
2145 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146}
2147
2148expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002149FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2150 int col_offset, PyArena *arena)
2151{
2152 expr_ty p;
2153 if (!value) {
2154 PyErr_SetString(PyExc_ValueError,
2155 "field value is required for FormattedValue");
2156 return NULL;
2157 }
2158 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2159 if (!p)
2160 return NULL;
2161 p->kind = FormattedValue_kind;
2162 p->v.FormattedValue.value = value;
2163 p->v.FormattedValue.conversion = conversion;
2164 p->v.FormattedValue.format_spec = format_spec;
2165 p->lineno = lineno;
2166 p->col_offset = col_offset;
2167 return p;
2168}
2169
2170expr_ty
2171JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2172{
2173 expr_ty p;
2174 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2175 if (!p)
2176 return NULL;
2177 p->kind = JoinedStr_kind;
2178 p->v.JoinedStr.values = values;
2179 p->lineno = lineno;
2180 p->col_offset = col_offset;
2181 return p;
2182}
2183
2184expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002185Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002186{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002187 expr_ty p;
2188 if (!s) {
2189 PyErr_SetString(PyExc_ValueError,
2190 "field s is required for Bytes");
2191 return NULL;
2192 }
2193 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2194 if (!p)
2195 return NULL;
2196 p->kind = Bytes_kind;
2197 p->v.Bytes.s = s;
2198 p->lineno = lineno;
2199 p->col_offset = col_offset;
2200 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002201}
2202
2203expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002204NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2205{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002206 expr_ty p;
2207 if (!value) {
2208 PyErr_SetString(PyExc_ValueError,
2209 "field value is required for NameConstant");
2210 return NULL;
2211 }
2212 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2213 if (!p)
2214 return NULL;
2215 p->kind = NameConstant_kind;
2216 p->v.NameConstant.value = value;
2217 p->lineno = lineno;
2218 p->col_offset = col_offset;
2219 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002220}
2221
2222expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002223Ellipsis(int lineno, int col_offset, PyArena *arena)
2224{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002225 expr_ty p;
2226 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2227 if (!p)
2228 return NULL;
2229 p->kind = Ellipsis_kind;
2230 p->lineno = lineno;
2231 p->col_offset = col_offset;
2232 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002233}
2234
2235expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002236Constant(constant value, int lineno, int col_offset, PyArena *arena)
2237{
2238 expr_ty p;
2239 if (!value) {
2240 PyErr_SetString(PyExc_ValueError,
2241 "field value is required for Constant");
2242 return NULL;
2243 }
2244 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2245 if (!p)
2246 return NULL;
2247 p->kind = Constant_kind;
2248 p->v.Constant.value = value;
2249 p->lineno = lineno;
2250 p->col_offset = col_offset;
2251 return p;
2252}
2253
2254expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002255Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2256 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002258 expr_ty p;
2259 if (!value) {
2260 PyErr_SetString(PyExc_ValueError,
2261 "field value is required for Attribute");
2262 return NULL;
2263 }
2264 if (!attr) {
2265 PyErr_SetString(PyExc_ValueError,
2266 "field attr is required for Attribute");
2267 return NULL;
2268 }
2269 if (!ctx) {
2270 PyErr_SetString(PyExc_ValueError,
2271 "field ctx is required for Attribute");
2272 return NULL;
2273 }
2274 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2275 if (!p)
2276 return NULL;
2277 p->kind = Attribute_kind;
2278 p->v.Attribute.value = value;
2279 p->v.Attribute.attr = attr;
2280 p->v.Attribute.ctx = ctx;
2281 p->lineno = lineno;
2282 p->col_offset = col_offset;
2283 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284}
2285
2286expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002287Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2288 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002290 expr_ty p;
2291 if (!value) {
2292 PyErr_SetString(PyExc_ValueError,
2293 "field value is required for Subscript");
2294 return NULL;
2295 }
2296 if (!slice) {
2297 PyErr_SetString(PyExc_ValueError,
2298 "field slice is required for Subscript");
2299 return NULL;
2300 }
2301 if (!ctx) {
2302 PyErr_SetString(PyExc_ValueError,
2303 "field ctx is required for Subscript");
2304 return NULL;
2305 }
2306 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2307 if (!p)
2308 return NULL;
2309 p->kind = Subscript_kind;
2310 p->v.Subscript.value = value;
2311 p->v.Subscript.slice = slice;
2312 p->v.Subscript.ctx = ctx;
2313 p->lineno = lineno;
2314 p->col_offset = col_offset;
2315 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316}
2317
2318expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002319Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2320 *arena)
2321{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002322 expr_ty p;
2323 if (!value) {
2324 PyErr_SetString(PyExc_ValueError,
2325 "field value is required for Starred");
2326 return NULL;
2327 }
2328 if (!ctx) {
2329 PyErr_SetString(PyExc_ValueError,
2330 "field ctx is required for Starred");
2331 return NULL;
2332 }
2333 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2334 if (!p)
2335 return NULL;
2336 p->kind = Starred_kind;
2337 p->v.Starred.value = value;
2338 p->v.Starred.ctx = ctx;
2339 p->lineno = lineno;
2340 p->col_offset = col_offset;
2341 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002342}
2343
2344expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002345Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2346 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002348 expr_ty p;
2349 if (!id) {
2350 PyErr_SetString(PyExc_ValueError,
2351 "field id is required for Name");
2352 return NULL;
2353 }
2354 if (!ctx) {
2355 PyErr_SetString(PyExc_ValueError,
2356 "field ctx is required for Name");
2357 return NULL;
2358 }
2359 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2360 if (!p)
2361 return NULL;
2362 p->kind = Name_kind;
2363 p->v.Name.id = id;
2364 p->v.Name.ctx = ctx;
2365 p->lineno = lineno;
2366 p->col_offset = col_offset;
2367 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368}
2369
2370expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002371List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2372 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002374 expr_ty p;
2375 if (!ctx) {
2376 PyErr_SetString(PyExc_ValueError,
2377 "field ctx is required for List");
2378 return NULL;
2379 }
2380 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2381 if (!p)
2382 return NULL;
2383 p->kind = List_kind;
2384 p->v.List.elts = elts;
2385 p->v.List.ctx = ctx;
2386 p->lineno = lineno;
2387 p->col_offset = col_offset;
2388 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389}
2390
2391expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002392Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2393 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002395 expr_ty p;
2396 if (!ctx) {
2397 PyErr_SetString(PyExc_ValueError,
2398 "field ctx is required for Tuple");
2399 return NULL;
2400 }
2401 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2402 if (!p)
2403 return NULL;
2404 p->kind = Tuple_kind;
2405 p->v.Tuple.elts = elts;
2406 p->v.Tuple.ctx = ctx;
2407 p->lineno = lineno;
2408 p->col_offset = col_offset;
2409 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410}
2411
2412slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002413Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002415 slice_ty p;
2416 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2417 if (!p)
2418 return NULL;
2419 p->kind = Slice_kind;
2420 p->v.Slice.lower = lower;
2421 p->v.Slice.upper = upper;
2422 p->v.Slice.step = step;
2423 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424}
2425
2426slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002427ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002429 slice_ty p;
2430 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2431 if (!p)
2432 return NULL;
2433 p->kind = ExtSlice_kind;
2434 p->v.ExtSlice.dims = dims;
2435 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436}
2437
2438slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002439Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002441 slice_ty p;
2442 if (!value) {
2443 PyErr_SetString(PyExc_ValueError,
2444 "field value is required for Index");
2445 return NULL;
2446 }
2447 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2448 if (!p)
2449 return NULL;
2450 p->kind = Index_kind;
2451 p->v.Index.value = value;
2452 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453}
2454
2455comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002456comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2457 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002459 comprehension_ty p;
2460 if (!target) {
2461 PyErr_SetString(PyExc_ValueError,
2462 "field target is required for comprehension");
2463 return NULL;
2464 }
2465 if (!iter) {
2466 PyErr_SetString(PyExc_ValueError,
2467 "field iter is required for comprehension");
2468 return NULL;
2469 }
2470 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2471 if (!p)
2472 return NULL;
2473 p->target = target;
2474 p->iter = iter;
2475 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002476 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002477 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478}
2479
2480excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002481ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002482 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002484 excepthandler_ty p;
2485 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2486 if (!p)
2487 return NULL;
2488 p->kind = ExceptHandler_kind;
2489 p->v.ExceptHandler.type = type;
2490 p->v.ExceptHandler.name = name;
2491 p->v.ExceptHandler.body = body;
2492 p->lineno = lineno;
2493 p->col_offset = col_offset;
2494 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495}
2496
2497arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002498arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2499 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 arguments_ty p;
2502 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2503 if (!p)
2504 return NULL;
2505 p->args = args;
2506 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002507 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002508 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002509 p->kwarg = kwarg;
2510 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002511 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512}
2513
Neal Norwitzc1505362006-12-28 06:47:50 +00002514arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002515arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2516 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002517{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002518 arg_ty p;
2519 if (!arg) {
2520 PyErr_SetString(PyExc_ValueError,
2521 "field arg is required for arg");
2522 return NULL;
2523 }
2524 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2525 if (!p)
2526 return NULL;
2527 p->arg = arg;
2528 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002529 p->lineno = lineno;
2530 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002531 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002532}
2533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002535keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002537 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002538 if (!value) {
2539 PyErr_SetString(PyExc_ValueError,
2540 "field value is required for keyword");
2541 return NULL;
2542 }
2543 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2544 if (!p)
2545 return NULL;
2546 p->arg = arg;
2547 p->value = value;
2548 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549}
2550
2551alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002552alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002554 alias_ty p;
2555 if (!name) {
2556 PyErr_SetString(PyExc_ValueError,
2557 "field name is required for alias");
2558 return NULL;
2559 }
2560 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2561 if (!p)
2562 return NULL;
2563 p->name = name;
2564 p->asname = asname;
2565 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566}
2567
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002568withitem_ty
2569withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2570{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002571 withitem_ty p;
2572 if (!context_expr) {
2573 PyErr_SetString(PyExc_ValueError,
2574 "field context_expr is required for withitem");
2575 return NULL;
2576 }
2577 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2578 if (!p)
2579 return NULL;
2580 p->context_expr = context_expr;
2581 p->optional_vars = optional_vars;
2582 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002583}
2584
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002585
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002586PyObject*
2587ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002588{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002589 mod_ty o = (mod_ty)_o;
2590 PyObject *result = NULL, *value = NULL;
2591 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002592 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002593 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002594
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002595 switch (o->kind) {
2596 case Module_kind:
2597 result = PyType_GenericNew(Module_type, NULL, NULL);
2598 if (!result) goto failed;
2599 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2600 if (!value) goto failed;
2601 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
2604 break;
2605 case Interactive_kind:
2606 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2607 if (!result) goto failed;
2608 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2609 if (!value) goto failed;
2610 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 break;
2614 case Expression_kind:
2615 result = PyType_GenericNew(Expression_type, NULL, NULL);
2616 if (!result) goto failed;
2617 value = ast2obj_expr(o->v.Expression.body);
2618 if (!value) goto failed;
2619 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 break;
2623 case Suite_kind:
2624 result = PyType_GenericNew(Suite_type, NULL, NULL);
2625 if (!result) goto failed;
2626 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2627 if (!value) goto failed;
2628 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
2631 break;
2632 }
2633 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002634failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002635 Py_XDECREF(value);
2636 Py_XDECREF(result);
2637 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638}
2639
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002640PyObject*
2641ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002643 stmt_ty o = (stmt_ty)_o;
2644 PyObject *result = NULL, *value = NULL;
2645 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002646 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002647 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002648
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002649 switch (o->kind) {
2650 case FunctionDef_kind:
2651 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2652 if (!result) goto failed;
2653 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002654 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002655 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2656 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002657 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002658 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002659 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002660 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2661 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002662 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002663 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2664 if (!value) goto failed;
2665 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2666 goto failed;
2667 Py_DECREF(value);
2668 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2669 if (!value) goto failed;
2670 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2671 goto failed;
2672 Py_DECREF(value);
2673 value = ast2obj_expr(o->v.FunctionDef.returns);
2674 if (!value) goto failed;
2675 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
2678 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002679 case AsyncFunctionDef_kind:
2680 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2681 if (!result) goto failed;
2682 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2683 if (!value) goto failed;
2684 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2688 if (!value) goto failed;
2689 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2690 goto failed;
2691 Py_DECREF(value);
2692 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2693 if (!value) goto failed;
2694 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
2697 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2698 ast2obj_expr);
2699 if (!value) goto failed;
2700 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2701 goto failed;
2702 Py_DECREF(value);
2703 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2704 if (!value) goto failed;
2705 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002709 case ClassDef_kind:
2710 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2711 if (!result) goto failed;
2712 value = ast2obj_identifier(o->v.ClassDef.name);
2713 if (!value) goto failed;
2714 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2718 if (!value) goto failed;
2719 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2723 if (!value) goto failed;
2724 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002727 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2728 if (!value) goto failed;
2729 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2730 goto failed;
2731 Py_DECREF(value);
2732 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2733 if (!value) goto failed;
2734 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2735 goto failed;
2736 Py_DECREF(value);
2737 break;
2738 case Return_kind:
2739 result = PyType_GenericNew(Return_type, NULL, NULL);
2740 if (!result) goto failed;
2741 value = ast2obj_expr(o->v.Return.value);
2742 if (!value) goto failed;
2743 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 break;
2747 case Delete_kind:
2748 result = PyType_GenericNew(Delete_type, NULL, NULL);
2749 if (!result) goto failed;
2750 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2751 if (!value) goto failed;
2752 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
2755 break;
2756 case Assign_kind:
2757 result = PyType_GenericNew(Assign_type, NULL, NULL);
2758 if (!result) goto failed;
2759 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2760 if (!value) goto failed;
2761 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 value = ast2obj_expr(o->v.Assign.value);
2765 if (!value) goto failed;
2766 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2767 goto failed;
2768 Py_DECREF(value);
2769 break;
2770 case AugAssign_kind:
2771 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2772 if (!result) goto failed;
2773 value = ast2obj_expr(o->v.AugAssign.target);
2774 if (!value) goto failed;
2775 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 value = ast2obj_operator(o->v.AugAssign.op);
2779 if (!value) goto failed;
2780 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2781 goto failed;
2782 Py_DECREF(value);
2783 value = ast2obj_expr(o->v.AugAssign.value);
2784 if (!value) goto failed;
2785 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2786 goto failed;
2787 Py_DECREF(value);
2788 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002789 case AnnAssign_kind:
2790 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2791 if (!result) goto failed;
2792 value = ast2obj_expr(o->v.AnnAssign.target);
2793 if (!value) goto failed;
2794 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 value = ast2obj_expr(o->v.AnnAssign.annotation);
2798 if (!value) goto failed;
2799 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
2802 value = ast2obj_expr(o->v.AnnAssign.value);
2803 if (!value) goto failed;
2804 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
2807 value = ast2obj_int(o->v.AnnAssign.simple);
2808 if (!value) goto failed;
2809 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
2812 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002813 case For_kind:
2814 result = PyType_GenericNew(For_type, NULL, NULL);
2815 if (!result) goto failed;
2816 value = ast2obj_expr(o->v.For.target);
2817 if (!value) goto failed;
2818 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2819 goto failed;
2820 Py_DECREF(value);
2821 value = ast2obj_expr(o->v.For.iter);
2822 if (!value) goto failed;
2823 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2824 goto failed;
2825 Py_DECREF(value);
2826 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2827 if (!value) goto failed;
2828 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
2831 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2832 if (!value) goto failed;
2833 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2834 goto failed;
2835 Py_DECREF(value);
2836 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002837 case AsyncFor_kind:
2838 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2839 if (!result) goto failed;
2840 value = ast2obj_expr(o->v.AsyncFor.target);
2841 if (!value) goto failed;
2842 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2843 goto failed;
2844 Py_DECREF(value);
2845 value = ast2obj_expr(o->v.AsyncFor.iter);
2846 if (!value) goto failed;
2847 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2848 goto failed;
2849 Py_DECREF(value);
2850 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2851 if (!value) goto failed;
2852 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2856 if (!value) goto failed;
2857 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2858 goto failed;
2859 Py_DECREF(value);
2860 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002861 case While_kind:
2862 result = PyType_GenericNew(While_type, NULL, NULL);
2863 if (!result) goto failed;
2864 value = ast2obj_expr(o->v.While.test);
2865 if (!value) goto failed;
2866 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2867 goto failed;
2868 Py_DECREF(value);
2869 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2870 if (!value) goto failed;
2871 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2872 goto failed;
2873 Py_DECREF(value);
2874 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2875 if (!value) goto failed;
2876 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2877 goto failed;
2878 Py_DECREF(value);
2879 break;
2880 case If_kind:
2881 result = PyType_GenericNew(If_type, NULL, NULL);
2882 if (!result) goto failed;
2883 value = ast2obj_expr(o->v.If.test);
2884 if (!value) goto failed;
2885 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2886 goto failed;
2887 Py_DECREF(value);
2888 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2889 if (!value) goto failed;
2890 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2891 goto failed;
2892 Py_DECREF(value);
2893 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2894 if (!value) goto failed;
2895 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2896 goto failed;
2897 Py_DECREF(value);
2898 break;
2899 case With_kind:
2900 result = PyType_GenericNew(With_type, NULL, NULL);
2901 if (!result) goto failed;
2902 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2903 if (!value) goto failed;
2904 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2905 goto failed;
2906 Py_DECREF(value);
2907 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2908 if (!value) goto failed;
2909 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2910 goto failed;
2911 Py_DECREF(value);
2912 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002913 case AsyncWith_kind:
2914 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2915 if (!result) goto failed;
2916 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2917 if (!value) goto failed;
2918 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
2921 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2922 if (!value) goto failed;
2923 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
2926 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002927 case Raise_kind:
2928 result = PyType_GenericNew(Raise_type, NULL, NULL);
2929 if (!result) goto failed;
2930 value = ast2obj_expr(o->v.Raise.exc);
2931 if (!value) goto failed;
2932 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2933 goto failed;
2934 Py_DECREF(value);
2935 value = ast2obj_expr(o->v.Raise.cause);
2936 if (!value) goto failed;
2937 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2938 goto failed;
2939 Py_DECREF(value);
2940 break;
2941 case Try_kind:
2942 result = PyType_GenericNew(Try_type, NULL, NULL);
2943 if (!result) goto failed;
2944 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2945 if (!value) goto failed;
2946 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2947 goto failed;
2948 Py_DECREF(value);
2949 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2950 if (!value) goto failed;
2951 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2952 goto failed;
2953 Py_DECREF(value);
2954 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2955 if (!value) goto failed;
2956 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2957 goto failed;
2958 Py_DECREF(value);
2959 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2960 if (!value) goto failed;
2961 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2962 goto failed;
2963 Py_DECREF(value);
2964 break;
2965 case Assert_kind:
2966 result = PyType_GenericNew(Assert_type, NULL, NULL);
2967 if (!result) goto failed;
2968 value = ast2obj_expr(o->v.Assert.test);
2969 if (!value) goto failed;
2970 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 value = ast2obj_expr(o->v.Assert.msg);
2974 if (!value) goto failed;
2975 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 break;
2979 case Import_kind:
2980 result = PyType_GenericNew(Import_type, NULL, NULL);
2981 if (!result) goto failed;
2982 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 break;
2988 case ImportFrom_kind:
2989 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2990 if (!result) goto failed;
2991 value = ast2obj_identifier(o->v.ImportFrom.module);
2992 if (!value) goto failed;
2993 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2994 goto failed;
2995 Py_DECREF(value);
2996 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2997 if (!value) goto failed;
2998 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 value = ast2obj_int(o->v.ImportFrom.level);
3002 if (!value) goto failed;
3003 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
3006 break;
3007 case Global_kind:
3008 result = PyType_GenericNew(Global_type, NULL, NULL);
3009 if (!result) goto failed;
3010 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 break;
3016 case Nonlocal_kind:
3017 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3020 if (!value) goto failed;
3021 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
3024 break;
3025 case Expr_kind:
3026 result = PyType_GenericNew(Expr_type, NULL, NULL);
3027 if (!result) goto failed;
3028 value = ast2obj_expr(o->v.Expr.value);
3029 if (!value) goto failed;
3030 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3031 goto failed;
3032 Py_DECREF(value);
3033 break;
3034 case Pass_kind:
3035 result = PyType_GenericNew(Pass_type, NULL, NULL);
3036 if (!result) goto failed;
3037 break;
3038 case Break_kind:
3039 result = PyType_GenericNew(Break_type, NULL, NULL);
3040 if (!result) goto failed;
3041 break;
3042 case Continue_kind:
3043 result = PyType_GenericNew(Continue_type, NULL, NULL);
3044 if (!result) goto failed;
3045 break;
3046 }
3047 value = ast2obj_int(o->lineno);
3048 if (!value) goto failed;
3049 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3050 goto failed;
3051 Py_DECREF(value);
3052 value = ast2obj_int(o->col_offset);
3053 if (!value) goto failed;
3054 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3055 goto failed;
3056 Py_DECREF(value);
3057 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003058failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003059 Py_XDECREF(value);
3060 Py_XDECREF(result);
3061 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062}
3063
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003064PyObject*
3065ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003067 expr_ty o = (expr_ty)_o;
3068 PyObject *result = NULL, *value = NULL;
3069 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003070 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003071 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003072
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003073 switch (o->kind) {
3074 case BoolOp_kind:
3075 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3076 if (!result) goto failed;
3077 value = ast2obj_boolop(o->v.BoolOp.op);
3078 if (!value) goto failed;
3079 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3080 goto failed;
3081 Py_DECREF(value);
3082 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3083 if (!value) goto failed;
3084 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3085 goto failed;
3086 Py_DECREF(value);
3087 break;
3088 case BinOp_kind:
3089 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3090 if (!result) goto failed;
3091 value = ast2obj_expr(o->v.BinOp.left);
3092 if (!value) goto failed;
3093 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3094 goto failed;
3095 Py_DECREF(value);
3096 value = ast2obj_operator(o->v.BinOp.op);
3097 if (!value) goto failed;
3098 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3099 goto failed;
3100 Py_DECREF(value);
3101 value = ast2obj_expr(o->v.BinOp.right);
3102 if (!value) goto failed;
3103 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3104 goto failed;
3105 Py_DECREF(value);
3106 break;
3107 case UnaryOp_kind:
3108 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3109 if (!result) goto failed;
3110 value = ast2obj_unaryop(o->v.UnaryOp.op);
3111 if (!value) goto failed;
3112 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3113 goto failed;
3114 Py_DECREF(value);
3115 value = ast2obj_expr(o->v.UnaryOp.operand);
3116 if (!value) goto failed;
3117 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3118 goto failed;
3119 Py_DECREF(value);
3120 break;
3121 case Lambda_kind:
3122 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3123 if (!result) goto failed;
3124 value = ast2obj_arguments(o->v.Lambda.args);
3125 if (!value) goto failed;
3126 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3127 goto failed;
3128 Py_DECREF(value);
3129 value = ast2obj_expr(o->v.Lambda.body);
3130 if (!value) goto failed;
3131 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3132 goto failed;
3133 Py_DECREF(value);
3134 break;
3135 case IfExp_kind:
3136 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3137 if (!result) goto failed;
3138 value = ast2obj_expr(o->v.IfExp.test);
3139 if (!value) goto failed;
3140 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3141 goto failed;
3142 Py_DECREF(value);
3143 value = ast2obj_expr(o->v.IfExp.body);
3144 if (!value) goto failed;
3145 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
3148 value = ast2obj_expr(o->v.IfExp.orelse);
3149 if (!value) goto failed;
3150 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3151 goto failed;
3152 Py_DECREF(value);
3153 break;
3154 case Dict_kind:
3155 result = PyType_GenericNew(Dict_type, NULL, NULL);
3156 if (!result) goto failed;
3157 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3158 if (!value) goto failed;
3159 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3160 goto failed;
3161 Py_DECREF(value);
3162 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3163 if (!value) goto failed;
3164 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3165 goto failed;
3166 Py_DECREF(value);
3167 break;
3168 case Set_kind:
3169 result = PyType_GenericNew(Set_type, NULL, NULL);
3170 if (!result) goto failed;
3171 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3172 if (!value) goto failed;
3173 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3174 goto failed;
3175 Py_DECREF(value);
3176 break;
3177 case ListComp_kind:
3178 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3179 if (!result) goto failed;
3180 value = ast2obj_expr(o->v.ListComp.elt);
3181 if (!value) goto failed;
3182 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3183 goto failed;
3184 Py_DECREF(value);
3185 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3186 if (!value) goto failed;
3187 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3188 goto failed;
3189 Py_DECREF(value);
3190 break;
3191 case SetComp_kind:
3192 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3193 if (!result) goto failed;
3194 value = ast2obj_expr(o->v.SetComp.elt);
3195 if (!value) goto failed;
3196 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3197 goto failed;
3198 Py_DECREF(value);
3199 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3200 if (!value) goto failed;
3201 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3202 goto failed;
3203 Py_DECREF(value);
3204 break;
3205 case DictComp_kind:
3206 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3207 if (!result) goto failed;
3208 value = ast2obj_expr(o->v.DictComp.key);
3209 if (!value) goto failed;
3210 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3211 goto failed;
3212 Py_DECREF(value);
3213 value = ast2obj_expr(o->v.DictComp.value);
3214 if (!value) goto failed;
3215 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3216 goto failed;
3217 Py_DECREF(value);
3218 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3219 if (!value) goto failed;
3220 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3221 goto failed;
3222 Py_DECREF(value);
3223 break;
3224 case GeneratorExp_kind:
3225 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3226 if (!result) goto failed;
3227 value = ast2obj_expr(o->v.GeneratorExp.elt);
3228 if (!value) goto failed;
3229 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3230 goto failed;
3231 Py_DECREF(value);
3232 value = ast2obj_list(o->v.GeneratorExp.generators,
3233 ast2obj_comprehension);
3234 if (!value) goto failed;
3235 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3236 goto failed;
3237 Py_DECREF(value);
3238 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003239 case Await_kind:
3240 result = PyType_GenericNew(Await_type, NULL, NULL);
3241 if (!result) goto failed;
3242 value = ast2obj_expr(o->v.Await.value);
3243 if (!value) goto failed;
3244 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3245 goto failed;
3246 Py_DECREF(value);
3247 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003248 case Yield_kind:
3249 result = PyType_GenericNew(Yield_type, NULL, NULL);
3250 if (!result) goto failed;
3251 value = ast2obj_expr(o->v.Yield.value);
3252 if (!value) goto failed;
3253 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3254 goto failed;
3255 Py_DECREF(value);
3256 break;
3257 case YieldFrom_kind:
3258 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3259 if (!result) goto failed;
3260 value = ast2obj_expr(o->v.YieldFrom.value);
3261 if (!value) goto failed;
3262 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3263 goto failed;
3264 Py_DECREF(value);
3265 break;
3266 case Compare_kind:
3267 result = PyType_GenericNew(Compare_type, NULL, NULL);
3268 if (!result) goto failed;
3269 value = ast2obj_expr(o->v.Compare.left);
3270 if (!value) goto failed;
3271 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3272 goto failed;
3273 Py_DECREF(value);
3274 {
3275 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3276 value = PyList_New(n);
3277 if (!value) goto failed;
3278 for(i = 0; i < n; i++)
3279 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003281 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003282 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3283 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003284 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003285 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003286 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003287 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3288 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003289 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003290 break;
3291 case Call_kind:
3292 result = PyType_GenericNew(Call_type, NULL, NULL);
3293 if (!result) goto failed;
3294 value = ast2obj_expr(o->v.Call.func);
3295 if (!value) goto failed;
3296 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3297 goto failed;
3298 Py_DECREF(value);
3299 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3300 if (!value) goto failed;
3301 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3302 goto failed;
3303 Py_DECREF(value);
3304 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3305 if (!value) goto failed;
3306 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3307 goto failed;
3308 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003309 break;
3310 case Num_kind:
3311 result = PyType_GenericNew(Num_type, NULL, NULL);
3312 if (!result) goto failed;
3313 value = ast2obj_object(o->v.Num.n);
3314 if (!value) goto failed;
3315 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
3318 break;
3319 case Str_kind:
3320 result = PyType_GenericNew(Str_type, NULL, NULL);
3321 if (!result) goto failed;
3322 value = ast2obj_string(o->v.Str.s);
3323 if (!value) goto failed;
3324 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3325 goto failed;
3326 Py_DECREF(value);
3327 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003328 case FormattedValue_kind:
3329 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3330 if (!result) goto failed;
3331 value = ast2obj_expr(o->v.FormattedValue.value);
3332 if (!value) goto failed;
3333 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3334 goto failed;
3335 Py_DECREF(value);
3336 value = ast2obj_int(o->v.FormattedValue.conversion);
3337 if (!value) goto failed;
3338 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3339 goto failed;
3340 Py_DECREF(value);
3341 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3342 if (!value) goto failed;
3343 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3344 goto failed;
3345 Py_DECREF(value);
3346 break;
3347 case JoinedStr_kind:
3348 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3349 if (!result) goto failed;
3350 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3351 if (!value) goto failed;
3352 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3353 goto failed;
3354 Py_DECREF(value);
3355 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003356 case Bytes_kind:
3357 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3358 if (!result) goto failed;
3359 value = ast2obj_bytes(o->v.Bytes.s);
3360 if (!value) goto failed;
3361 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3362 goto failed;
3363 Py_DECREF(value);
3364 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003365 case NameConstant_kind:
3366 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3367 if (!result) goto failed;
3368 value = ast2obj_singleton(o->v.NameConstant.value);
3369 if (!value) goto failed;
3370 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3371 goto failed;
3372 Py_DECREF(value);
3373 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003374 case Ellipsis_kind:
3375 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3376 if (!result) goto failed;
3377 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003378 case Constant_kind:
3379 result = PyType_GenericNew(Constant_type, NULL, NULL);
3380 if (!result) goto failed;
3381 value = ast2obj_constant(o->v.Constant.value);
3382 if (!value) goto failed;
3383 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3384 goto failed;
3385 Py_DECREF(value);
3386 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003387 case Attribute_kind:
3388 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3389 if (!result) goto failed;
3390 value = ast2obj_expr(o->v.Attribute.value);
3391 if (!value) goto failed;
3392 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3393 goto failed;
3394 Py_DECREF(value);
3395 value = ast2obj_identifier(o->v.Attribute.attr);
3396 if (!value) goto failed;
3397 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3398 goto failed;
3399 Py_DECREF(value);
3400 value = ast2obj_expr_context(o->v.Attribute.ctx);
3401 if (!value) goto failed;
3402 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3403 goto failed;
3404 Py_DECREF(value);
3405 break;
3406 case Subscript_kind:
3407 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3408 if (!result) goto failed;
3409 value = ast2obj_expr(o->v.Subscript.value);
3410 if (!value) goto failed;
3411 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3412 goto failed;
3413 Py_DECREF(value);
3414 value = ast2obj_slice(o->v.Subscript.slice);
3415 if (!value) goto failed;
3416 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3417 goto failed;
3418 Py_DECREF(value);
3419 value = ast2obj_expr_context(o->v.Subscript.ctx);
3420 if (!value) goto failed;
3421 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3422 goto failed;
3423 Py_DECREF(value);
3424 break;
3425 case Starred_kind:
3426 result = PyType_GenericNew(Starred_type, NULL, NULL);
3427 if (!result) goto failed;
3428 value = ast2obj_expr(o->v.Starred.value);
3429 if (!value) goto failed;
3430 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3431 goto failed;
3432 Py_DECREF(value);
3433 value = ast2obj_expr_context(o->v.Starred.ctx);
3434 if (!value) goto failed;
3435 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3436 goto failed;
3437 Py_DECREF(value);
3438 break;
3439 case Name_kind:
3440 result = PyType_GenericNew(Name_type, NULL, NULL);
3441 if (!result) goto failed;
3442 value = ast2obj_identifier(o->v.Name.id);
3443 if (!value) goto failed;
3444 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3445 goto failed;
3446 Py_DECREF(value);
3447 value = ast2obj_expr_context(o->v.Name.ctx);
3448 if (!value) goto failed;
3449 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3450 goto failed;
3451 Py_DECREF(value);
3452 break;
3453 case List_kind:
3454 result = PyType_GenericNew(List_type, NULL, NULL);
3455 if (!result) goto failed;
3456 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3457 if (!value) goto failed;
3458 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3459 goto failed;
3460 Py_DECREF(value);
3461 value = ast2obj_expr_context(o->v.List.ctx);
3462 if (!value) goto failed;
3463 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3464 goto failed;
3465 Py_DECREF(value);
3466 break;
3467 case Tuple_kind:
3468 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3469 if (!result) goto failed;
3470 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3471 if (!value) goto failed;
3472 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3473 goto failed;
3474 Py_DECREF(value);
3475 value = ast2obj_expr_context(o->v.Tuple.ctx);
3476 if (!value) goto failed;
3477 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3478 goto failed;
3479 Py_DECREF(value);
3480 break;
3481 }
3482 value = ast2obj_int(o->lineno);
3483 if (!value) goto failed;
3484 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3485 goto failed;
3486 Py_DECREF(value);
3487 value = ast2obj_int(o->col_offset);
3488 if (!value) goto failed;
3489 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3490 goto failed;
3491 Py_DECREF(value);
3492 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003493failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003494 Py_XDECREF(value);
3495 Py_XDECREF(result);
3496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497}
3498
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003499PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003501 switch(o) {
3502 case Load:
3503 Py_INCREF(Load_singleton);
3504 return Load_singleton;
3505 case Store:
3506 Py_INCREF(Store_singleton);
3507 return Store_singleton;
3508 case Del:
3509 Py_INCREF(Del_singleton);
3510 return Del_singleton;
3511 case AugLoad:
3512 Py_INCREF(AugLoad_singleton);
3513 return AugLoad_singleton;
3514 case AugStore:
3515 Py_INCREF(AugStore_singleton);
3516 return AugStore_singleton;
3517 case Param:
3518 Py_INCREF(Param_singleton);
3519 return Param_singleton;
3520 default:
3521 /* should never happen, but just in case ... */
3522 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3523 return NULL;
3524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003526PyObject*
3527ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003529 slice_ty o = (slice_ty)_o;
3530 PyObject *result = NULL, *value = NULL;
3531 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003532 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003534
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003535 switch (o->kind) {
3536 case Slice_kind:
3537 result = PyType_GenericNew(Slice_type, NULL, NULL);
3538 if (!result) goto failed;
3539 value = ast2obj_expr(o->v.Slice.lower);
3540 if (!value) goto failed;
3541 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3542 goto failed;
3543 Py_DECREF(value);
3544 value = ast2obj_expr(o->v.Slice.upper);
3545 if (!value) goto failed;
3546 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3547 goto failed;
3548 Py_DECREF(value);
3549 value = ast2obj_expr(o->v.Slice.step);
3550 if (!value) goto failed;
3551 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3552 goto failed;
3553 Py_DECREF(value);
3554 break;
3555 case ExtSlice_kind:
3556 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3557 if (!result) goto failed;
3558 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3559 if (!value) goto failed;
3560 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3561 goto failed;
3562 Py_DECREF(value);
3563 break;
3564 case Index_kind:
3565 result = PyType_GenericNew(Index_type, NULL, NULL);
3566 if (!result) goto failed;
3567 value = ast2obj_expr(o->v.Index.value);
3568 if (!value) goto failed;
3569 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3570 goto failed;
3571 Py_DECREF(value);
3572 break;
3573 }
3574 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003575failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003576 Py_XDECREF(value);
3577 Py_XDECREF(result);
3578 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579}
3580
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003581PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003583 switch(o) {
3584 case And:
3585 Py_INCREF(And_singleton);
3586 return And_singleton;
3587 case Or:
3588 Py_INCREF(Or_singleton);
3589 return Or_singleton;
3590 default:
3591 /* should never happen, but just in case ... */
3592 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3593 return NULL;
3594 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003595}
3596PyObject* ast2obj_operator(operator_ty o)
3597{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003598 switch(o) {
3599 case Add:
3600 Py_INCREF(Add_singleton);
3601 return Add_singleton;
3602 case Sub:
3603 Py_INCREF(Sub_singleton);
3604 return Sub_singleton;
3605 case Mult:
3606 Py_INCREF(Mult_singleton);
3607 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003608 case MatMult:
3609 Py_INCREF(MatMult_singleton);
3610 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003611 case Div:
3612 Py_INCREF(Div_singleton);
3613 return Div_singleton;
3614 case Mod:
3615 Py_INCREF(Mod_singleton);
3616 return Mod_singleton;
3617 case Pow:
3618 Py_INCREF(Pow_singleton);
3619 return Pow_singleton;
3620 case LShift:
3621 Py_INCREF(LShift_singleton);
3622 return LShift_singleton;
3623 case RShift:
3624 Py_INCREF(RShift_singleton);
3625 return RShift_singleton;
3626 case BitOr:
3627 Py_INCREF(BitOr_singleton);
3628 return BitOr_singleton;
3629 case BitXor:
3630 Py_INCREF(BitXor_singleton);
3631 return BitXor_singleton;
3632 case BitAnd:
3633 Py_INCREF(BitAnd_singleton);
3634 return BitAnd_singleton;
3635 case FloorDiv:
3636 Py_INCREF(FloorDiv_singleton);
3637 return FloorDiv_singleton;
3638 default:
3639 /* should never happen, but just in case ... */
3640 PyErr_Format(PyExc_SystemError, "unknown operator found");
3641 return NULL;
3642 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003643}
3644PyObject* ast2obj_unaryop(unaryop_ty o)
3645{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003646 switch(o) {
3647 case Invert:
3648 Py_INCREF(Invert_singleton);
3649 return Invert_singleton;
3650 case Not:
3651 Py_INCREF(Not_singleton);
3652 return Not_singleton;
3653 case UAdd:
3654 Py_INCREF(UAdd_singleton);
3655 return UAdd_singleton;
3656 case USub:
3657 Py_INCREF(USub_singleton);
3658 return USub_singleton;
3659 default:
3660 /* should never happen, but just in case ... */
3661 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3662 return NULL;
3663 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003664}
3665PyObject* ast2obj_cmpop(cmpop_ty o)
3666{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003667 switch(o) {
3668 case Eq:
3669 Py_INCREF(Eq_singleton);
3670 return Eq_singleton;
3671 case NotEq:
3672 Py_INCREF(NotEq_singleton);
3673 return NotEq_singleton;
3674 case Lt:
3675 Py_INCREF(Lt_singleton);
3676 return Lt_singleton;
3677 case LtE:
3678 Py_INCREF(LtE_singleton);
3679 return LtE_singleton;
3680 case Gt:
3681 Py_INCREF(Gt_singleton);
3682 return Gt_singleton;
3683 case GtE:
3684 Py_INCREF(GtE_singleton);
3685 return GtE_singleton;
3686 case Is:
3687 Py_INCREF(Is_singleton);
3688 return Is_singleton;
3689 case IsNot:
3690 Py_INCREF(IsNot_singleton);
3691 return IsNot_singleton;
3692 case In:
3693 Py_INCREF(In_singleton);
3694 return In_singleton;
3695 case NotIn:
3696 Py_INCREF(NotIn_singleton);
3697 return NotIn_singleton;
3698 default:
3699 /* should never happen, but just in case ... */
3700 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3701 return NULL;
3702 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003703}
3704PyObject*
3705ast2obj_comprehension(void* _o)
3706{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003707 comprehension_ty o = (comprehension_ty)_o;
3708 PyObject *result = NULL, *value = NULL;
3709 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003710 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003711 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003712
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003713 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3714 if (!result) return NULL;
3715 value = ast2obj_expr(o->target);
3716 if (!value) goto failed;
3717 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3718 goto failed;
3719 Py_DECREF(value);
3720 value = ast2obj_expr(o->iter);
3721 if (!value) goto failed;
3722 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3723 goto failed;
3724 Py_DECREF(value);
3725 value = ast2obj_list(o->ifs, ast2obj_expr);
3726 if (!value) goto failed;
3727 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3728 goto failed;
3729 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003730 value = ast2obj_int(o->is_async);
3731 if (!value) goto failed;
3732 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3733 goto failed;
3734 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003735 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003736failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003737 Py_XDECREF(value);
3738 Py_XDECREF(result);
3739 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740}
3741
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003742PyObject*
3743ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003745 excepthandler_ty o = (excepthandler_ty)_o;
3746 PyObject *result = NULL, *value = NULL;
3747 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003748 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003749 }
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) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003795 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003796 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003797
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003798 result = PyType_GenericNew(arguments_type, NULL, NULL);
3799 if (!result) return NULL;
3800 value = ast2obj_list(o->args, ast2obj_arg);
3801 if (!value) goto failed;
3802 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3803 goto failed;
3804 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003805 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003806 if (!value) goto failed;
3807 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3808 goto failed;
3809 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003810 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3811 if (!value) goto failed;
3812 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3813 goto failed;
3814 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003815 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003816 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003817 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003818 goto failed;
3819 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003820 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003821 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003822 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003823 goto failed;
3824 Py_DECREF(value);
3825 value = ast2obj_list(o->defaults, ast2obj_expr);
3826 if (!value) goto failed;
3827 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3828 goto failed;
3829 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003830 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003831failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003832 Py_XDECREF(value);
3833 Py_XDECREF(result);
3834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835}
3836
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003837PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003838ast2obj_arg(void* _o)
3839{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003840 arg_ty o = (arg_ty)_o;
3841 PyObject *result = NULL, *value = NULL;
3842 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003843 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003844 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003845
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003846 result = PyType_GenericNew(arg_type, NULL, NULL);
3847 if (!result) return NULL;
3848 value = ast2obj_identifier(o->arg);
3849 if (!value) goto failed;
3850 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3851 goto failed;
3852 Py_DECREF(value);
3853 value = ast2obj_expr(o->annotation);
3854 if (!value) goto failed;
3855 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3856 goto failed;
3857 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003858 value = ast2obj_int(o->lineno);
3859 if (!value) goto failed;
3860 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3861 goto failed;
3862 Py_DECREF(value);
3863 value = ast2obj_int(o->col_offset);
3864 if (!value) goto failed;
3865 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3866 goto failed;
3867 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003868 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003869failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003870 Py_XDECREF(value);
3871 Py_XDECREF(result);
3872 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003873}
3874
3875PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003876ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 keyword_ty o = (keyword_ty)_o;
3879 PyObject *result = NULL, *value = NULL;
3880 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003881 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003882 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003883
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003884 result = PyType_GenericNew(keyword_type, NULL, NULL);
3885 if (!result) return NULL;
3886 value = ast2obj_identifier(o->arg);
3887 if (!value) goto failed;
3888 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3889 goto failed;
3890 Py_DECREF(value);
3891 value = ast2obj_expr(o->value);
3892 if (!value) goto failed;
3893 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3894 goto failed;
3895 Py_DECREF(value);
3896 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003897failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003898 Py_XDECREF(value);
3899 Py_XDECREF(result);
3900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901}
3902
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003903PyObject*
3904ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003906 alias_ty o = (alias_ty)_o;
3907 PyObject *result = NULL, *value = NULL;
3908 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003909 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003910 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003911
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003912 result = PyType_GenericNew(alias_type, NULL, NULL);
3913 if (!result) return NULL;
3914 value = ast2obj_identifier(o->name);
3915 if (!value) goto failed;
3916 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3917 goto failed;
3918 Py_DECREF(value);
3919 value = ast2obj_identifier(o->asname);
3920 if (!value) goto failed;
3921 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3922 goto failed;
3923 Py_DECREF(value);
3924 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003925failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003926 Py_XDECREF(value);
3927 Py_XDECREF(result);
3928 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929}
3930
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003931PyObject*
3932ast2obj_withitem(void* _o)
3933{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003934 withitem_ty o = (withitem_ty)_o;
3935 PyObject *result = NULL, *value = NULL;
3936 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003937 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003939
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003940 result = PyType_GenericNew(withitem_type, NULL, NULL);
3941 if (!result) return NULL;
3942 value = ast2obj_expr(o->context_expr);
3943 if (!value) goto failed;
3944 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3945 goto failed;
3946 Py_DECREF(value);
3947 value = ast2obj_expr(o->optional_vars);
3948 if (!value) goto failed;
3949 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3950 goto failed;
3951 Py_DECREF(value);
3952 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003953failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003954 Py_XDECREF(value);
3955 Py_XDECREF(result);
3956 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003957}
3958
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003960int
3961obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3962{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003963 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003964
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003965 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003966
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003967 if (obj == Py_None) {
3968 *out = NULL;
3969 return 0;
3970 }
3971 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3972 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003973 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 }
3975 if (isinstance) {
3976 asdl_seq* body;
3977
3978 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3979 int res;
3980 Py_ssize_t len;
3981 Py_ssize_t i;
3982 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3983 if (tmp == NULL) goto failed;
3984 if (!PyList_Check(tmp)) {
3985 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3986 goto failed;
3987 }
3988 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003989 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003990 if (body == NULL) goto failed;
3991 for (i = 0; i < len; i++) {
3992 stmt_ty value;
3993 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3994 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03003995 if (len != PyList_GET_SIZE(tmp)) {
3996 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
3997 goto failed;
3998 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003999 asdl_seq_SET(body, i, value);
4000 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004001 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004002 } else {
4003 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4004 return 1;
4005 }
4006 *out = Module(body, arena);
4007 if (*out == NULL) goto failed;
4008 return 0;
4009 }
4010 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4011 if (isinstance == -1) {
4012 return 1;
4013 }
4014 if (isinstance) {
4015 asdl_seq* body;
4016
4017 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4018 int res;
4019 Py_ssize_t len;
4020 Py_ssize_t i;
4021 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4022 if (tmp == NULL) goto failed;
4023 if (!PyList_Check(tmp)) {
4024 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4025 goto failed;
4026 }
4027 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004028 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004029 if (body == NULL) goto failed;
4030 for (i = 0; i < len; i++) {
4031 stmt_ty value;
4032 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4033 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004034 if (len != PyList_GET_SIZE(tmp)) {
4035 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4036 goto failed;
4037 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004038 asdl_seq_SET(body, i, value);
4039 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004041 } else {
4042 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4043 return 1;
4044 }
4045 *out = Interactive(body, arena);
4046 if (*out == NULL) goto failed;
4047 return 0;
4048 }
4049 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4050 if (isinstance == -1) {
4051 return 1;
4052 }
4053 if (isinstance) {
4054 expr_ty body;
4055
4056 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4057 int res;
4058 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4059 if (tmp == NULL) goto failed;
4060 res = obj2ast_expr(tmp, &body, arena);
4061 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004062 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004063 } else {
4064 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4065 return 1;
4066 }
4067 *out = Expression(body, arena);
4068 if (*out == NULL) goto failed;
4069 return 0;
4070 }
4071 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4072 if (isinstance == -1) {
4073 return 1;
4074 }
4075 if (isinstance) {
4076 asdl_seq* body;
4077
4078 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4079 int res;
4080 Py_ssize_t len;
4081 Py_ssize_t i;
4082 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4083 if (tmp == NULL) goto failed;
4084 if (!PyList_Check(tmp)) {
4085 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4086 goto failed;
4087 }
4088 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004089 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004090 if (body == NULL) goto failed;
4091 for (i = 0; i < len; i++) {
4092 stmt_ty value;
4093 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4094 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004095 if (len != PyList_GET_SIZE(tmp)) {
4096 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4097 goto failed;
4098 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004099 asdl_seq_SET(body, i, value);
4100 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004101 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004102 } else {
4103 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4104 return 1;
4105 }
4106 *out = Suite(body, arena);
4107 if (*out == NULL) goto failed;
4108 return 0;
4109 }
4110
4111 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4112 failed:
4113 Py_XDECREF(tmp);
4114 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004115}
4116
4117int
4118obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004120 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004121
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004122 PyObject *tmp = NULL;
4123 int lineno;
4124 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004125
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004126 if (obj == Py_None) {
4127 *out = NULL;
4128 return 0;
4129 }
4130 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4131 int res;
4132 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4133 if (tmp == NULL) goto failed;
4134 res = obj2ast_int(tmp, &lineno, arena);
4135 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004136 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004137 } else {
4138 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004139 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004140 }
4141 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4142 int res;
4143 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4144 if (tmp == NULL) goto failed;
4145 res = obj2ast_int(tmp, &col_offset, arena);
4146 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004147 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004148 } else {
4149 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4150 return 1;
4151 }
4152 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4153 if (isinstance == -1) {
4154 return 1;
4155 }
4156 if (isinstance) {
4157 identifier name;
4158 arguments_ty args;
4159 asdl_seq* body;
4160 asdl_seq* decorator_list;
4161 expr_ty returns;
4162
4163 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4164 int res;
4165 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4166 if (tmp == NULL) goto failed;
4167 res = obj2ast_identifier(tmp, &name, arena);
4168 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004169 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004170 } else {
4171 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4172 return 1;
4173 }
4174 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4175 int res;
4176 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4177 if (tmp == NULL) goto failed;
4178 res = obj2ast_arguments(tmp, &args, arena);
4179 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004180 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004181 } else {
4182 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4183 return 1;
4184 }
4185 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4186 int res;
4187 Py_ssize_t len;
4188 Py_ssize_t i;
4189 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4190 if (tmp == NULL) goto failed;
4191 if (!PyList_Check(tmp)) {
4192 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4193 goto failed;
4194 }
4195 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004196 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004197 if (body == NULL) goto failed;
4198 for (i = 0; i < len; i++) {
4199 stmt_ty value;
4200 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4201 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004202 if (len != PyList_GET_SIZE(tmp)) {
4203 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4204 goto failed;
4205 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004206 asdl_seq_SET(body, i, value);
4207 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004208 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004209 } else {
4210 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4211 return 1;
4212 }
4213 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4214 int res;
4215 Py_ssize_t len;
4216 Py_ssize_t i;
4217 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4218 if (tmp == NULL) goto failed;
4219 if (!PyList_Check(tmp)) {
4220 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4221 goto failed;
4222 }
4223 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004224 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004225 if (decorator_list == NULL) goto failed;
4226 for (i = 0; i < len; i++) {
4227 expr_ty value;
4228 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4229 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004230 if (len != PyList_GET_SIZE(tmp)) {
4231 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4232 goto failed;
4233 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004234 asdl_seq_SET(decorator_list, i, value);
4235 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004236 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004237 } else {
4238 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4239 return 1;
4240 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004241 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004242 int res;
4243 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4244 if (tmp == NULL) goto failed;
4245 res = obj2ast_expr(tmp, &returns, arena);
4246 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004248 } else {
4249 returns = NULL;
4250 }
4251 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4252 col_offset, arena);
4253 if (*out == NULL) goto failed;
4254 return 0;
4255 }
Yury Selivanov75445082015-05-11 22:57:16 -04004256 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4257 if (isinstance == -1) {
4258 return 1;
4259 }
4260 if (isinstance) {
4261 identifier name;
4262 arguments_ty args;
4263 asdl_seq* body;
4264 asdl_seq* decorator_list;
4265 expr_ty returns;
4266
4267 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4268 int res;
4269 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4270 if (tmp == NULL) goto failed;
4271 res = obj2ast_identifier(tmp, &name, arena);
4272 if (res != 0) goto failed;
4273 Py_CLEAR(tmp);
4274 } else {
4275 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4276 return 1;
4277 }
4278 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4279 int res;
4280 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4281 if (tmp == NULL) goto failed;
4282 res = obj2ast_arguments(tmp, &args, arena);
4283 if (res != 0) goto failed;
4284 Py_CLEAR(tmp);
4285 } else {
4286 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4287 return 1;
4288 }
4289 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4290 int res;
4291 Py_ssize_t len;
4292 Py_ssize_t i;
4293 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4294 if (tmp == NULL) goto failed;
4295 if (!PyList_Check(tmp)) {
4296 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4297 goto failed;
4298 }
4299 len = PyList_GET_SIZE(tmp);
4300 body = _Py_asdl_seq_new(len, arena);
4301 if (body == NULL) goto failed;
4302 for (i = 0; i < len; i++) {
4303 stmt_ty value;
4304 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4305 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004306 if (len != PyList_GET_SIZE(tmp)) {
4307 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4308 goto failed;
4309 }
Yury Selivanov75445082015-05-11 22:57:16 -04004310 asdl_seq_SET(body, i, value);
4311 }
4312 Py_CLEAR(tmp);
4313 } else {
4314 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4315 return 1;
4316 }
4317 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4318 int res;
4319 Py_ssize_t len;
4320 Py_ssize_t i;
4321 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4322 if (tmp == NULL) goto failed;
4323 if (!PyList_Check(tmp)) {
4324 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4325 goto failed;
4326 }
4327 len = PyList_GET_SIZE(tmp);
4328 decorator_list = _Py_asdl_seq_new(len, arena);
4329 if (decorator_list == NULL) goto failed;
4330 for (i = 0; i < len; i++) {
4331 expr_ty value;
4332 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4333 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004334 if (len != PyList_GET_SIZE(tmp)) {
4335 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4336 goto failed;
4337 }
Yury Selivanov75445082015-05-11 22:57:16 -04004338 asdl_seq_SET(decorator_list, i, value);
4339 }
4340 Py_CLEAR(tmp);
4341 } else {
4342 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4343 return 1;
4344 }
4345 if (exists_not_none(obj, &PyId_returns)) {
4346 int res;
4347 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4348 if (tmp == NULL) goto failed;
4349 res = obj2ast_expr(tmp, &returns, arena);
4350 if (res != 0) goto failed;
4351 Py_CLEAR(tmp);
4352 } else {
4353 returns = NULL;
4354 }
4355 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4356 lineno, col_offset, arena);
4357 if (*out == NULL) goto failed;
4358 return 0;
4359 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004360 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4361 if (isinstance == -1) {
4362 return 1;
4363 }
4364 if (isinstance) {
4365 identifier name;
4366 asdl_seq* bases;
4367 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004368 asdl_seq* body;
4369 asdl_seq* decorator_list;
4370
4371 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4372 int res;
4373 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4374 if (tmp == NULL) goto failed;
4375 res = obj2ast_identifier(tmp, &name, arena);
4376 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004377 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004378 } else {
4379 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4380 return 1;
4381 }
4382 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4383 int res;
4384 Py_ssize_t len;
4385 Py_ssize_t i;
4386 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4387 if (tmp == NULL) goto failed;
4388 if (!PyList_Check(tmp)) {
4389 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4390 goto failed;
4391 }
4392 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004393 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004394 if (bases == NULL) goto failed;
4395 for (i = 0; i < len; i++) {
4396 expr_ty value;
4397 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4398 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004399 if (len != PyList_GET_SIZE(tmp)) {
4400 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4401 goto failed;
4402 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004403 asdl_seq_SET(bases, i, value);
4404 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004405 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004406 } else {
4407 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4408 return 1;
4409 }
4410 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4411 int res;
4412 Py_ssize_t len;
4413 Py_ssize_t i;
4414 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4415 if (tmp == NULL) goto failed;
4416 if (!PyList_Check(tmp)) {
4417 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4418 goto failed;
4419 }
4420 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004421 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004422 if (keywords == NULL) goto failed;
4423 for (i = 0; i < len; i++) {
4424 keyword_ty value;
4425 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4426 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004427 if (len != PyList_GET_SIZE(tmp)) {
4428 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4429 goto failed;
4430 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004431 asdl_seq_SET(keywords, i, value);
4432 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004433 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004434 } else {
4435 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4436 return 1;
4437 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004438 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4439 int res;
4440 Py_ssize_t len;
4441 Py_ssize_t i;
4442 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4443 if (tmp == NULL) goto failed;
4444 if (!PyList_Check(tmp)) {
4445 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4446 goto failed;
4447 }
4448 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004449 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004450 if (body == NULL) goto failed;
4451 for (i = 0; i < len; i++) {
4452 stmt_ty value;
4453 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4454 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004455 if (len != PyList_GET_SIZE(tmp)) {
4456 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4457 goto failed;
4458 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004459 asdl_seq_SET(body, i, value);
4460 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004461 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004462 } else {
4463 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4464 return 1;
4465 }
4466 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4467 int res;
4468 Py_ssize_t len;
4469 Py_ssize_t i;
4470 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4471 if (tmp == NULL) goto failed;
4472 if (!PyList_Check(tmp)) {
4473 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4474 goto failed;
4475 }
4476 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004477 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004478 if (decorator_list == NULL) goto failed;
4479 for (i = 0; i < len; i++) {
4480 expr_ty value;
4481 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4482 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004483 if (len != PyList_GET_SIZE(tmp)) {
4484 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4485 goto failed;
4486 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004487 asdl_seq_SET(decorator_list, i, value);
4488 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004489 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004490 } else {
4491 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4492 return 1;
4493 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004494 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4495 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004496 if (*out == NULL) goto failed;
4497 return 0;
4498 }
4499 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4500 if (isinstance == -1) {
4501 return 1;
4502 }
4503 if (isinstance) {
4504 expr_ty value;
4505
Victor Stinneree4b59c2013-07-27 00:01:35 +02004506 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004507 int res;
4508 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4509 if (tmp == NULL) goto failed;
4510 res = obj2ast_expr(tmp, &value, arena);
4511 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004512 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004513 } else {
4514 value = NULL;
4515 }
4516 *out = Return(value, lineno, col_offset, arena);
4517 if (*out == NULL) goto failed;
4518 return 0;
4519 }
4520 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4521 if (isinstance == -1) {
4522 return 1;
4523 }
4524 if (isinstance) {
4525 asdl_seq* targets;
4526
4527 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4528 int res;
4529 Py_ssize_t len;
4530 Py_ssize_t i;
4531 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4532 if (tmp == NULL) goto failed;
4533 if (!PyList_Check(tmp)) {
4534 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4535 goto failed;
4536 }
4537 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004538 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004539 if (targets == NULL) goto failed;
4540 for (i = 0; i < len; i++) {
4541 expr_ty value;
4542 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4543 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004544 if (len != PyList_GET_SIZE(tmp)) {
4545 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4546 goto failed;
4547 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004548 asdl_seq_SET(targets, i, value);
4549 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004550 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004551 } else {
4552 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4553 return 1;
4554 }
4555 *out = Delete(targets, lineno, col_offset, arena);
4556 if (*out == NULL) goto failed;
4557 return 0;
4558 }
4559 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4560 if (isinstance == -1) {
4561 return 1;
4562 }
4563 if (isinstance) {
4564 asdl_seq* targets;
4565 expr_ty value;
4566
4567 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4568 int res;
4569 Py_ssize_t len;
4570 Py_ssize_t i;
4571 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4572 if (tmp == NULL) goto failed;
4573 if (!PyList_Check(tmp)) {
4574 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4575 goto failed;
4576 }
4577 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004578 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004579 if (targets == NULL) goto failed;
4580 for (i = 0; i < len; i++) {
4581 expr_ty value;
4582 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4583 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004584 if (len != PyList_GET_SIZE(tmp)) {
4585 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4586 goto failed;
4587 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004588 asdl_seq_SET(targets, i, value);
4589 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004590 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004591 } else {
4592 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4593 return 1;
4594 }
4595 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4596 int res;
4597 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4598 if (tmp == NULL) goto failed;
4599 res = obj2ast_expr(tmp, &value, arena);
4600 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004601 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004602 } else {
4603 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4604 return 1;
4605 }
4606 *out = Assign(targets, value, lineno, col_offset, arena);
4607 if (*out == NULL) goto failed;
4608 return 0;
4609 }
4610 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4611 if (isinstance == -1) {
4612 return 1;
4613 }
4614 if (isinstance) {
4615 expr_ty target;
4616 operator_ty op;
4617 expr_ty value;
4618
4619 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4620 int res;
4621 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4622 if (tmp == NULL) goto failed;
4623 res = obj2ast_expr(tmp, &target, arena);
4624 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004625 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4628 return 1;
4629 }
4630 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4631 int res;
4632 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4633 if (tmp == NULL) goto failed;
4634 res = obj2ast_operator(tmp, &op, arena);
4635 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004636 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004637 } else {
4638 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4639 return 1;
4640 }
4641 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4642 int res;
4643 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4644 if (tmp == NULL) goto failed;
4645 res = obj2ast_expr(tmp, &value, arena);
4646 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4650 return 1;
4651 }
4652 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4653 if (*out == NULL) goto failed;
4654 return 0;
4655 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004656 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4657 if (isinstance == -1) {
4658 return 1;
4659 }
4660 if (isinstance) {
4661 expr_ty target;
4662 expr_ty annotation;
4663 expr_ty value;
4664 int simple;
4665
4666 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4667 int res;
4668 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4669 if (tmp == NULL) goto failed;
4670 res = obj2ast_expr(tmp, &target, arena);
4671 if (res != 0) goto failed;
4672 Py_CLEAR(tmp);
4673 } else {
4674 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4675 return 1;
4676 }
4677 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4678 int res;
4679 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4680 if (tmp == NULL) goto failed;
4681 res = obj2ast_expr(tmp, &annotation, arena);
4682 if (res != 0) goto failed;
4683 Py_CLEAR(tmp);
4684 } else {
4685 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4686 return 1;
4687 }
4688 if (exists_not_none(obj, &PyId_value)) {
4689 int res;
4690 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4691 if (tmp == NULL) goto failed;
4692 res = obj2ast_expr(tmp, &value, arena);
4693 if (res != 0) goto failed;
4694 Py_CLEAR(tmp);
4695 } else {
4696 value = NULL;
4697 }
4698 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4699 int res;
4700 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_int(tmp, &simple, arena);
4703 if (res != 0) goto failed;
4704 Py_CLEAR(tmp);
4705 } else {
4706 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4707 return 1;
4708 }
4709 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4710 arena);
4711 if (*out == NULL) goto failed;
4712 return 0;
4713 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004714 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4715 if (isinstance == -1) {
4716 return 1;
4717 }
4718 if (isinstance) {
4719 expr_ty target;
4720 expr_ty iter;
4721 asdl_seq* body;
4722 asdl_seq* orelse;
4723
4724 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4725 int res;
4726 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4727 if (tmp == NULL) goto failed;
4728 res = obj2ast_expr(tmp, &target, arena);
4729 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004730 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004731 } else {
4732 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4733 return 1;
4734 }
4735 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4736 int res;
4737 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4738 if (tmp == NULL) goto failed;
4739 res = obj2ast_expr(tmp, &iter, arena);
4740 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004741 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004742 } else {
4743 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4744 return 1;
4745 }
4746 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4747 int res;
4748 Py_ssize_t len;
4749 Py_ssize_t i;
4750 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4751 if (tmp == NULL) goto failed;
4752 if (!PyList_Check(tmp)) {
4753 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4754 goto failed;
4755 }
4756 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004757 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004758 if (body == NULL) goto failed;
4759 for (i = 0; i < len; i++) {
4760 stmt_ty value;
4761 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4762 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004763 if (len != PyList_GET_SIZE(tmp)) {
4764 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4765 goto failed;
4766 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004767 asdl_seq_SET(body, i, value);
4768 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004770 } else {
4771 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4772 return 1;
4773 }
4774 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4775 int res;
4776 Py_ssize_t len;
4777 Py_ssize_t i;
4778 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4779 if (tmp == NULL) goto failed;
4780 if (!PyList_Check(tmp)) {
4781 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4782 goto failed;
4783 }
4784 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004785 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004786 if (orelse == NULL) goto failed;
4787 for (i = 0; i < len; i++) {
4788 stmt_ty value;
4789 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4790 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004791 if (len != PyList_GET_SIZE(tmp)) {
4792 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4793 goto failed;
4794 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004795 asdl_seq_SET(orelse, i, value);
4796 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004798 } else {
4799 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4800 return 1;
4801 }
4802 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4803 if (*out == NULL) goto failed;
4804 return 0;
4805 }
Yury Selivanov75445082015-05-11 22:57:16 -04004806 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4807 if (isinstance == -1) {
4808 return 1;
4809 }
4810 if (isinstance) {
4811 expr_ty target;
4812 expr_ty iter;
4813 asdl_seq* body;
4814 asdl_seq* orelse;
4815
4816 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4817 int res;
4818 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4819 if (tmp == NULL) goto failed;
4820 res = obj2ast_expr(tmp, &target, arena);
4821 if (res != 0) goto failed;
4822 Py_CLEAR(tmp);
4823 } else {
4824 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4825 return 1;
4826 }
4827 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4828 int res;
4829 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4830 if (tmp == NULL) goto failed;
4831 res = obj2ast_expr(tmp, &iter, arena);
4832 if (res != 0) goto failed;
4833 Py_CLEAR(tmp);
4834 } else {
4835 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4836 return 1;
4837 }
4838 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4839 int res;
4840 Py_ssize_t len;
4841 Py_ssize_t i;
4842 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4843 if (tmp == NULL) goto failed;
4844 if (!PyList_Check(tmp)) {
4845 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4846 goto failed;
4847 }
4848 len = PyList_GET_SIZE(tmp);
4849 body = _Py_asdl_seq_new(len, arena);
4850 if (body == NULL) goto failed;
4851 for (i = 0; i < len; i++) {
4852 stmt_ty value;
4853 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4854 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004855 if (len != PyList_GET_SIZE(tmp)) {
4856 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4857 goto failed;
4858 }
Yury Selivanov75445082015-05-11 22:57:16 -04004859 asdl_seq_SET(body, i, value);
4860 }
4861 Py_CLEAR(tmp);
4862 } else {
4863 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4864 return 1;
4865 }
4866 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4867 int res;
4868 Py_ssize_t len;
4869 Py_ssize_t i;
4870 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4871 if (tmp == NULL) goto failed;
4872 if (!PyList_Check(tmp)) {
4873 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4874 goto failed;
4875 }
4876 len = PyList_GET_SIZE(tmp);
4877 orelse = _Py_asdl_seq_new(len, arena);
4878 if (orelse == NULL) goto failed;
4879 for (i = 0; i < len; i++) {
4880 stmt_ty value;
4881 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4882 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004883 if (len != PyList_GET_SIZE(tmp)) {
4884 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4885 goto failed;
4886 }
Yury Selivanov75445082015-05-11 22:57:16 -04004887 asdl_seq_SET(orelse, i, value);
4888 }
4889 Py_CLEAR(tmp);
4890 } else {
4891 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4892 return 1;
4893 }
4894 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4895 if (*out == NULL) goto failed;
4896 return 0;
4897 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004898 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4899 if (isinstance == -1) {
4900 return 1;
4901 }
4902 if (isinstance) {
4903 expr_ty test;
4904 asdl_seq* body;
4905 asdl_seq* orelse;
4906
4907 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4908 int res;
4909 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4910 if (tmp == NULL) goto failed;
4911 res = obj2ast_expr(tmp, &test, arena);
4912 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004913 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004914 } else {
4915 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4916 return 1;
4917 }
4918 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4919 int res;
4920 Py_ssize_t len;
4921 Py_ssize_t i;
4922 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4923 if (tmp == NULL) goto failed;
4924 if (!PyList_Check(tmp)) {
4925 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4926 goto failed;
4927 }
4928 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004929 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004930 if (body == NULL) goto failed;
4931 for (i = 0; i < len; i++) {
4932 stmt_ty value;
4933 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4934 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004935 if (len != PyList_GET_SIZE(tmp)) {
4936 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
4937 goto failed;
4938 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004939 asdl_seq_SET(body, i, value);
4940 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004941 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004942 } else {
4943 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4944 return 1;
4945 }
4946 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4947 int res;
4948 Py_ssize_t len;
4949 Py_ssize_t i;
4950 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4951 if (tmp == NULL) goto failed;
4952 if (!PyList_Check(tmp)) {
4953 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4954 goto failed;
4955 }
4956 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004957 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004958 if (orelse == NULL) goto failed;
4959 for (i = 0; i < len; i++) {
4960 stmt_ty value;
4961 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4962 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004963 if (len != PyList_GET_SIZE(tmp)) {
4964 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
4965 goto failed;
4966 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004967 asdl_seq_SET(orelse, i, value);
4968 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004969 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004970 } else {
4971 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4972 return 1;
4973 }
4974 *out = While(test, body, orelse, lineno, col_offset, arena);
4975 if (*out == NULL) goto failed;
4976 return 0;
4977 }
4978 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4979 if (isinstance == -1) {
4980 return 1;
4981 }
4982 if (isinstance) {
4983 expr_ty test;
4984 asdl_seq* body;
4985 asdl_seq* orelse;
4986
4987 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4988 int res;
4989 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4990 if (tmp == NULL) goto failed;
4991 res = obj2ast_expr(tmp, &test, arena);
4992 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004993 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004994 } else {
4995 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4996 return 1;
4997 }
4998 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4999 int res;
5000 Py_ssize_t len;
5001 Py_ssize_t i;
5002 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5003 if (tmp == NULL) goto failed;
5004 if (!PyList_Check(tmp)) {
5005 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5006 goto failed;
5007 }
5008 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005009 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005010 if (body == NULL) goto failed;
5011 for (i = 0; i < len; i++) {
5012 stmt_ty value;
5013 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5014 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005015 if (len != PyList_GET_SIZE(tmp)) {
5016 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5017 goto failed;
5018 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005019 asdl_seq_SET(body, i, value);
5020 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005021 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005022 } else {
5023 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5024 return 1;
5025 }
5026 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5027 int res;
5028 Py_ssize_t len;
5029 Py_ssize_t i;
5030 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5031 if (tmp == NULL) goto failed;
5032 if (!PyList_Check(tmp)) {
5033 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5034 goto failed;
5035 }
5036 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005037 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005038 if (orelse == NULL) goto failed;
5039 for (i = 0; i < len; i++) {
5040 stmt_ty value;
5041 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5042 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005043 if (len != PyList_GET_SIZE(tmp)) {
5044 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5045 goto failed;
5046 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005047 asdl_seq_SET(orelse, i, value);
5048 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005049 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005050 } else {
5051 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5052 return 1;
5053 }
5054 *out = If(test, body, orelse, lineno, col_offset, arena);
5055 if (*out == NULL) goto failed;
5056 return 0;
5057 }
5058 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5059 if (isinstance == -1) {
5060 return 1;
5061 }
5062 if (isinstance) {
5063 asdl_seq* items;
5064 asdl_seq* body;
5065
5066 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5067 int res;
5068 Py_ssize_t len;
5069 Py_ssize_t i;
5070 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5071 if (tmp == NULL) goto failed;
5072 if (!PyList_Check(tmp)) {
5073 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5074 goto failed;
5075 }
5076 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005077 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005078 if (items == NULL) goto failed;
5079 for (i = 0; i < len; i++) {
5080 withitem_ty value;
5081 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5082 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005083 if (len != PyList_GET_SIZE(tmp)) {
5084 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5085 goto failed;
5086 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005087 asdl_seq_SET(items, i, value);
5088 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005089 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005090 } else {
5091 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5092 return 1;
5093 }
5094 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5095 int res;
5096 Py_ssize_t len;
5097 Py_ssize_t i;
5098 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5099 if (tmp == NULL) goto failed;
5100 if (!PyList_Check(tmp)) {
5101 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5102 goto failed;
5103 }
5104 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005105 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005106 if (body == NULL) goto failed;
5107 for (i = 0; i < len; i++) {
5108 stmt_ty value;
5109 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5110 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005111 if (len != PyList_GET_SIZE(tmp)) {
5112 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5113 goto failed;
5114 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005115 asdl_seq_SET(body, i, value);
5116 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005117 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005118 } else {
5119 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5120 return 1;
5121 }
5122 *out = With(items, body, lineno, col_offset, arena);
5123 if (*out == NULL) goto failed;
5124 return 0;
5125 }
Yury Selivanov75445082015-05-11 22:57:16 -04005126 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5127 if (isinstance == -1) {
5128 return 1;
5129 }
5130 if (isinstance) {
5131 asdl_seq* items;
5132 asdl_seq* body;
5133
5134 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5135 int res;
5136 Py_ssize_t len;
5137 Py_ssize_t i;
5138 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5139 if (tmp == NULL) goto failed;
5140 if (!PyList_Check(tmp)) {
5141 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5142 goto failed;
5143 }
5144 len = PyList_GET_SIZE(tmp);
5145 items = _Py_asdl_seq_new(len, arena);
5146 if (items == NULL) goto failed;
5147 for (i = 0; i < len; i++) {
5148 withitem_ty value;
5149 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5150 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005151 if (len != PyList_GET_SIZE(tmp)) {
5152 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5153 goto failed;
5154 }
Yury Selivanov75445082015-05-11 22:57:16 -04005155 asdl_seq_SET(items, i, value);
5156 }
5157 Py_CLEAR(tmp);
5158 } else {
5159 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5160 return 1;
5161 }
5162 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5163 int res;
5164 Py_ssize_t len;
5165 Py_ssize_t i;
5166 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5167 if (tmp == NULL) goto failed;
5168 if (!PyList_Check(tmp)) {
5169 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5170 goto failed;
5171 }
5172 len = PyList_GET_SIZE(tmp);
5173 body = _Py_asdl_seq_new(len, arena);
5174 if (body == NULL) goto failed;
5175 for (i = 0; i < len; i++) {
5176 stmt_ty value;
5177 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5178 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005179 if (len != PyList_GET_SIZE(tmp)) {
5180 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5181 goto failed;
5182 }
Yury Selivanov75445082015-05-11 22:57:16 -04005183 asdl_seq_SET(body, i, value);
5184 }
5185 Py_CLEAR(tmp);
5186 } else {
5187 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5188 return 1;
5189 }
5190 *out = AsyncWith(items, body, lineno, col_offset, arena);
5191 if (*out == NULL) goto failed;
5192 return 0;
5193 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005194 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5195 if (isinstance == -1) {
5196 return 1;
5197 }
5198 if (isinstance) {
5199 expr_ty exc;
5200 expr_ty cause;
5201
Victor Stinneree4b59c2013-07-27 00:01:35 +02005202 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005203 int res;
5204 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5205 if (tmp == NULL) goto failed;
5206 res = obj2ast_expr(tmp, &exc, arena);
5207 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005208 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005209 } else {
5210 exc = NULL;
5211 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005212 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005213 int res;
5214 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5215 if (tmp == NULL) goto failed;
5216 res = obj2ast_expr(tmp, &cause, arena);
5217 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005218 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005219 } else {
5220 cause = NULL;
5221 }
5222 *out = Raise(exc, cause, lineno, col_offset, arena);
5223 if (*out == NULL) goto failed;
5224 return 0;
5225 }
5226 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5227 if (isinstance == -1) {
5228 return 1;
5229 }
5230 if (isinstance) {
5231 asdl_seq* body;
5232 asdl_seq* handlers;
5233 asdl_seq* orelse;
5234 asdl_seq* finalbody;
5235
5236 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5237 int res;
5238 Py_ssize_t len;
5239 Py_ssize_t i;
5240 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5241 if (tmp == NULL) goto failed;
5242 if (!PyList_Check(tmp)) {
5243 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5244 goto failed;
5245 }
5246 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005247 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005248 if (body == NULL) goto failed;
5249 for (i = 0; i < len; i++) {
5250 stmt_ty value;
5251 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5252 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005253 if (len != PyList_GET_SIZE(tmp)) {
5254 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5255 goto failed;
5256 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005257 asdl_seq_SET(body, i, value);
5258 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005259 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005260 } else {
5261 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5262 return 1;
5263 }
5264 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5265 int res;
5266 Py_ssize_t len;
5267 Py_ssize_t i;
5268 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5269 if (tmp == NULL) goto failed;
5270 if (!PyList_Check(tmp)) {
5271 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5272 goto failed;
5273 }
5274 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005275 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005276 if (handlers == NULL) goto failed;
5277 for (i = 0; i < len; i++) {
5278 excepthandler_ty value;
5279 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5280 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005281 if (len != PyList_GET_SIZE(tmp)) {
5282 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5283 goto failed;
5284 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005285 asdl_seq_SET(handlers, i, value);
5286 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005287 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005288 } else {
5289 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5290 return 1;
5291 }
5292 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5293 int res;
5294 Py_ssize_t len;
5295 Py_ssize_t i;
5296 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5297 if (tmp == NULL) goto failed;
5298 if (!PyList_Check(tmp)) {
5299 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5300 goto failed;
5301 }
5302 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005303 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005304 if (orelse == NULL) goto failed;
5305 for (i = 0; i < len; i++) {
5306 stmt_ty value;
5307 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5308 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005309 if (len != PyList_GET_SIZE(tmp)) {
5310 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5311 goto failed;
5312 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005313 asdl_seq_SET(orelse, i, value);
5314 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005315 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005316 } else {
5317 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5318 return 1;
5319 }
5320 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5321 int res;
5322 Py_ssize_t len;
5323 Py_ssize_t i;
5324 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5325 if (tmp == NULL) goto failed;
5326 if (!PyList_Check(tmp)) {
5327 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5328 goto failed;
5329 }
5330 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005331 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005332 if (finalbody == NULL) goto failed;
5333 for (i = 0; i < len; i++) {
5334 stmt_ty value;
5335 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5336 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005337 if (len != PyList_GET_SIZE(tmp)) {
5338 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5339 goto failed;
5340 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005341 asdl_seq_SET(finalbody, i, value);
5342 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005344 } else {
5345 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5346 return 1;
5347 }
5348 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5349 arena);
5350 if (*out == NULL) goto failed;
5351 return 0;
5352 }
5353 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5354 if (isinstance == -1) {
5355 return 1;
5356 }
5357 if (isinstance) {
5358 expr_ty test;
5359 expr_ty msg;
5360
5361 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5362 int res;
5363 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5364 if (tmp == NULL) goto failed;
5365 res = obj2ast_expr(tmp, &test, arena);
5366 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005367 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005368 } else {
5369 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5370 return 1;
5371 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005372 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005373 int res;
5374 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5375 if (tmp == NULL) goto failed;
5376 res = obj2ast_expr(tmp, &msg, arena);
5377 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005378 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005379 } else {
5380 msg = NULL;
5381 }
5382 *out = Assert(test, msg, lineno, col_offset, arena);
5383 if (*out == NULL) goto failed;
5384 return 0;
5385 }
5386 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5387 if (isinstance == -1) {
5388 return 1;
5389 }
5390 if (isinstance) {
5391 asdl_seq* names;
5392
5393 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5394 int res;
5395 Py_ssize_t len;
5396 Py_ssize_t i;
5397 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5398 if (tmp == NULL) goto failed;
5399 if (!PyList_Check(tmp)) {
5400 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5401 goto failed;
5402 }
5403 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005404 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005405 if (names == NULL) goto failed;
5406 for (i = 0; i < len; i++) {
5407 alias_ty value;
5408 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5409 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005410 if (len != PyList_GET_SIZE(tmp)) {
5411 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5412 goto failed;
5413 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005414 asdl_seq_SET(names, i, value);
5415 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005416 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005417 } else {
5418 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5419 return 1;
5420 }
5421 *out = Import(names, lineno, col_offset, arena);
5422 if (*out == NULL) goto failed;
5423 return 0;
5424 }
5425 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5426 if (isinstance == -1) {
5427 return 1;
5428 }
5429 if (isinstance) {
5430 identifier module;
5431 asdl_seq* names;
5432 int level;
5433
Victor Stinneree4b59c2013-07-27 00:01:35 +02005434 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005435 int res;
5436 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_identifier(tmp, &module, arena);
5439 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005440 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005441 } else {
5442 module = NULL;
5443 }
5444 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5445 int res;
5446 Py_ssize_t len;
5447 Py_ssize_t i;
5448 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5449 if (tmp == NULL) goto failed;
5450 if (!PyList_Check(tmp)) {
5451 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5452 goto failed;
5453 }
5454 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005455 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005456 if (names == NULL) goto failed;
5457 for (i = 0; i < len; i++) {
5458 alias_ty value;
5459 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5460 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005461 if (len != PyList_GET_SIZE(tmp)) {
5462 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5463 goto failed;
5464 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005465 asdl_seq_SET(names, i, value);
5466 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005467 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005468 } else {
5469 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5470 return 1;
5471 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005472 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005473 int res;
5474 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5475 if (tmp == NULL) goto failed;
5476 res = obj2ast_int(tmp, &level, arena);
5477 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005478 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005479 } else {
5480 level = 0;
5481 }
5482 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5483 if (*out == NULL) goto failed;
5484 return 0;
5485 }
5486 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5487 if (isinstance == -1) {
5488 return 1;
5489 }
5490 if (isinstance) {
5491 asdl_seq* names;
5492
5493 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5494 int res;
5495 Py_ssize_t len;
5496 Py_ssize_t i;
5497 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5498 if (tmp == NULL) goto failed;
5499 if (!PyList_Check(tmp)) {
5500 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5501 goto failed;
5502 }
5503 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005504 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005505 if (names == NULL) goto failed;
5506 for (i = 0; i < len; i++) {
5507 identifier value;
5508 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5509 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005510 if (len != PyList_GET_SIZE(tmp)) {
5511 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5512 goto failed;
5513 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005514 asdl_seq_SET(names, i, value);
5515 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005516 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005517 } else {
5518 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5519 return 1;
5520 }
5521 *out = Global(names, lineno, col_offset, arena);
5522 if (*out == NULL) goto failed;
5523 return 0;
5524 }
5525 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5526 if (isinstance == -1) {
5527 return 1;
5528 }
5529 if (isinstance) {
5530 asdl_seq* names;
5531
5532 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5533 int res;
5534 Py_ssize_t len;
5535 Py_ssize_t i;
5536 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5537 if (tmp == NULL) goto failed;
5538 if (!PyList_Check(tmp)) {
5539 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5540 goto failed;
5541 }
5542 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005543 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005544 if (names == NULL) goto failed;
5545 for (i = 0; i < len; i++) {
5546 identifier value;
5547 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5548 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005549 if (len != PyList_GET_SIZE(tmp)) {
5550 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5551 goto failed;
5552 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005553 asdl_seq_SET(names, i, value);
5554 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005555 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005556 } else {
5557 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5558 return 1;
5559 }
5560 *out = Nonlocal(names, lineno, col_offset, arena);
5561 if (*out == NULL) goto failed;
5562 return 0;
5563 }
5564 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5565 if (isinstance == -1) {
5566 return 1;
5567 }
5568 if (isinstance) {
5569 expr_ty value;
5570
5571 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5572 int res;
5573 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5574 if (tmp == NULL) goto failed;
5575 res = obj2ast_expr(tmp, &value, arena);
5576 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005577 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005578 } else {
5579 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5580 return 1;
5581 }
5582 *out = Expr(value, lineno, col_offset, arena);
5583 if (*out == NULL) goto failed;
5584 return 0;
5585 }
5586 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5587 if (isinstance == -1) {
5588 return 1;
5589 }
5590 if (isinstance) {
5591
5592 *out = Pass(lineno, col_offset, arena);
5593 if (*out == NULL) goto failed;
5594 return 0;
5595 }
5596 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5597 if (isinstance == -1) {
5598 return 1;
5599 }
5600 if (isinstance) {
5601
5602 *out = Break(lineno, col_offset, arena);
5603 if (*out == NULL) goto failed;
5604 return 0;
5605 }
5606 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5607 if (isinstance == -1) {
5608 return 1;
5609 }
5610 if (isinstance) {
5611
5612 *out = Continue(lineno, col_offset, arena);
5613 if (*out == NULL) goto failed;
5614 return 0;
5615 }
5616
5617 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5618 failed:
5619 Py_XDECREF(tmp);
5620 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005621}
5622
5623int
5624obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5625{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005626 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005627
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005628 PyObject *tmp = NULL;
5629 int lineno;
5630 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005631
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005632 if (obj == Py_None) {
5633 *out = NULL;
5634 return 0;
5635 }
5636 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5637 int res;
5638 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5639 if (tmp == NULL) goto failed;
5640 res = obj2ast_int(tmp, &lineno, arena);
5641 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005642 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005643 } else {
5644 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005645 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005646 }
5647 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5648 int res;
5649 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5650 if (tmp == NULL) goto failed;
5651 res = obj2ast_int(tmp, &col_offset, arena);
5652 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005653 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005654 } else {
5655 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5656 return 1;
5657 }
5658 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5659 if (isinstance == -1) {
5660 return 1;
5661 }
5662 if (isinstance) {
5663 boolop_ty op;
5664 asdl_seq* values;
5665
5666 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5667 int res;
5668 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5669 if (tmp == NULL) goto failed;
5670 res = obj2ast_boolop(tmp, &op, arena);
5671 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005672 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005673 } else {
5674 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5675 return 1;
5676 }
5677 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5678 int res;
5679 Py_ssize_t len;
5680 Py_ssize_t i;
5681 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5682 if (tmp == NULL) goto failed;
5683 if (!PyList_Check(tmp)) {
5684 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5685 goto failed;
5686 }
5687 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005688 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005689 if (values == NULL) goto failed;
5690 for (i = 0; i < len; i++) {
5691 expr_ty value;
5692 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5693 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005694 if (len != PyList_GET_SIZE(tmp)) {
5695 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5696 goto failed;
5697 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005698 asdl_seq_SET(values, i, value);
5699 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005700 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005701 } else {
5702 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5703 return 1;
5704 }
5705 *out = BoolOp(op, values, lineno, col_offset, arena);
5706 if (*out == NULL) goto failed;
5707 return 0;
5708 }
5709 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5710 if (isinstance == -1) {
5711 return 1;
5712 }
5713 if (isinstance) {
5714 expr_ty left;
5715 operator_ty op;
5716 expr_ty right;
5717
5718 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5719 int res;
5720 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5721 if (tmp == NULL) goto failed;
5722 res = obj2ast_expr(tmp, &left, arena);
5723 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005724 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005725 } else {
5726 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5727 return 1;
5728 }
5729 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5730 int res;
5731 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5732 if (tmp == NULL) goto failed;
5733 res = obj2ast_operator(tmp, &op, arena);
5734 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005735 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005736 } else {
5737 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5738 return 1;
5739 }
5740 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5741 int res;
5742 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5743 if (tmp == NULL) goto failed;
5744 res = obj2ast_expr(tmp, &right, arena);
5745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005747 } else {
5748 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5749 return 1;
5750 }
5751 *out = BinOp(left, op, right, lineno, col_offset, arena);
5752 if (*out == NULL) goto failed;
5753 return 0;
5754 }
5755 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5756 if (isinstance == -1) {
5757 return 1;
5758 }
5759 if (isinstance) {
5760 unaryop_ty op;
5761 expr_ty operand;
5762
5763 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5764 int res;
5765 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5766 if (tmp == NULL) goto failed;
5767 res = obj2ast_unaryop(tmp, &op, arena);
5768 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005770 } else {
5771 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5772 return 1;
5773 }
5774 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5775 int res;
5776 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5777 if (tmp == NULL) goto failed;
5778 res = obj2ast_expr(tmp, &operand, arena);
5779 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005780 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005781 } else {
5782 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5783 return 1;
5784 }
5785 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5786 if (*out == NULL) goto failed;
5787 return 0;
5788 }
5789 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5790 if (isinstance == -1) {
5791 return 1;
5792 }
5793 if (isinstance) {
5794 arguments_ty args;
5795 expr_ty body;
5796
5797 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5798 int res;
5799 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5800 if (tmp == NULL) goto failed;
5801 res = obj2ast_arguments(tmp, &args, arena);
5802 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005803 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005804 } else {
5805 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5806 return 1;
5807 }
5808 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5809 int res;
5810 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5811 if (tmp == NULL) goto failed;
5812 res = obj2ast_expr(tmp, &body, arena);
5813 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005814 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005815 } else {
5816 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5817 return 1;
5818 }
5819 *out = Lambda(args, body, lineno, col_offset, arena);
5820 if (*out == NULL) goto failed;
5821 return 0;
5822 }
5823 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5824 if (isinstance == -1) {
5825 return 1;
5826 }
5827 if (isinstance) {
5828 expr_ty test;
5829 expr_ty body;
5830 expr_ty orelse;
5831
5832 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5833 int res;
5834 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5835 if (tmp == NULL) goto failed;
5836 res = obj2ast_expr(tmp, &test, arena);
5837 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005838 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005839 } else {
5840 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5841 return 1;
5842 }
5843 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5844 int res;
5845 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5846 if (tmp == NULL) goto failed;
5847 res = obj2ast_expr(tmp, &body, arena);
5848 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005849 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005850 } else {
5851 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5852 return 1;
5853 }
5854 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5855 int res;
5856 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5857 if (tmp == NULL) goto failed;
5858 res = obj2ast_expr(tmp, &orelse, arena);
5859 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005860 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005861 } else {
5862 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5863 return 1;
5864 }
5865 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5866 if (*out == NULL) goto failed;
5867 return 0;
5868 }
5869 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5870 if (isinstance == -1) {
5871 return 1;
5872 }
5873 if (isinstance) {
5874 asdl_seq* keys;
5875 asdl_seq* values;
5876
5877 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5878 int res;
5879 Py_ssize_t len;
5880 Py_ssize_t i;
5881 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5882 if (tmp == NULL) goto failed;
5883 if (!PyList_Check(tmp)) {
5884 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5885 goto failed;
5886 }
5887 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005888 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005889 if (keys == NULL) goto failed;
5890 for (i = 0; i < len; i++) {
5891 expr_ty value;
5892 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5893 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005894 if (len != PyList_GET_SIZE(tmp)) {
5895 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5896 goto failed;
5897 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005898 asdl_seq_SET(keys, i, value);
5899 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005900 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005901 } else {
5902 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5903 return 1;
5904 }
5905 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5906 int res;
5907 Py_ssize_t len;
5908 Py_ssize_t i;
5909 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5910 if (tmp == NULL) goto failed;
5911 if (!PyList_Check(tmp)) {
5912 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5913 goto failed;
5914 }
5915 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005916 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005917 if (values == NULL) goto failed;
5918 for (i = 0; i < len; i++) {
5919 expr_ty value;
5920 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5921 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005922 if (len != PyList_GET_SIZE(tmp)) {
5923 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5924 goto failed;
5925 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005926 asdl_seq_SET(values, i, value);
5927 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005928 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005929 } else {
5930 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5931 return 1;
5932 }
5933 *out = Dict(keys, values, lineno, col_offset, arena);
5934 if (*out == NULL) goto failed;
5935 return 0;
5936 }
5937 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5938 if (isinstance == -1) {
5939 return 1;
5940 }
5941 if (isinstance) {
5942 asdl_seq* elts;
5943
5944 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5945 int res;
5946 Py_ssize_t len;
5947 Py_ssize_t i;
5948 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5949 if (tmp == NULL) goto failed;
5950 if (!PyList_Check(tmp)) {
5951 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5952 goto failed;
5953 }
5954 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005955 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005956 if (elts == NULL) goto failed;
5957 for (i = 0; i < len; i++) {
5958 expr_ty value;
5959 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5960 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005961 if (len != PyList_GET_SIZE(tmp)) {
5962 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
5963 goto failed;
5964 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005965 asdl_seq_SET(elts, i, value);
5966 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005967 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005968 } else {
5969 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5970 return 1;
5971 }
5972 *out = Set(elts, lineno, col_offset, arena);
5973 if (*out == NULL) goto failed;
5974 return 0;
5975 }
5976 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5977 if (isinstance == -1) {
5978 return 1;
5979 }
5980 if (isinstance) {
5981 expr_ty elt;
5982 asdl_seq* generators;
5983
5984 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5985 int res;
5986 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5987 if (tmp == NULL) goto failed;
5988 res = obj2ast_expr(tmp, &elt, arena);
5989 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005990 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005991 } else {
5992 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5993 return 1;
5994 }
5995 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5996 int res;
5997 Py_ssize_t len;
5998 Py_ssize_t i;
5999 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6000 if (tmp == NULL) goto failed;
6001 if (!PyList_Check(tmp)) {
6002 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6003 goto failed;
6004 }
6005 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006006 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006007 if (generators == NULL) goto failed;
6008 for (i = 0; i < len; i++) {
6009 comprehension_ty value;
6010 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6011 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006012 if (len != PyList_GET_SIZE(tmp)) {
6013 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6014 goto failed;
6015 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006016 asdl_seq_SET(generators, i, value);
6017 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006018 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006019 } else {
6020 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6021 return 1;
6022 }
6023 *out = ListComp(elt, generators, lineno, col_offset, arena);
6024 if (*out == NULL) goto failed;
6025 return 0;
6026 }
6027 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6028 if (isinstance == -1) {
6029 return 1;
6030 }
6031 if (isinstance) {
6032 expr_ty elt;
6033 asdl_seq* generators;
6034
6035 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6036 int res;
6037 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6038 if (tmp == NULL) goto failed;
6039 res = obj2ast_expr(tmp, &elt, arena);
6040 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006042 } else {
6043 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6044 return 1;
6045 }
6046 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6047 int res;
6048 Py_ssize_t len;
6049 Py_ssize_t i;
6050 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6051 if (tmp == NULL) goto failed;
6052 if (!PyList_Check(tmp)) {
6053 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6054 goto failed;
6055 }
6056 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006057 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006058 if (generators == NULL) goto failed;
6059 for (i = 0; i < len; i++) {
6060 comprehension_ty value;
6061 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6062 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006063 if (len != PyList_GET_SIZE(tmp)) {
6064 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6065 goto failed;
6066 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006067 asdl_seq_SET(generators, i, value);
6068 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006069 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006070 } else {
6071 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6072 return 1;
6073 }
6074 *out = SetComp(elt, generators, lineno, col_offset, arena);
6075 if (*out == NULL) goto failed;
6076 return 0;
6077 }
6078 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6079 if (isinstance == -1) {
6080 return 1;
6081 }
6082 if (isinstance) {
6083 expr_ty key;
6084 expr_ty value;
6085 asdl_seq* generators;
6086
6087 if (_PyObject_HasAttrId(obj, &PyId_key)) {
6088 int res;
6089 tmp = _PyObject_GetAttrId(obj, &PyId_key);
6090 if (tmp == NULL) goto failed;
6091 res = obj2ast_expr(tmp, &key, arena);
6092 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006093 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006094 } else {
6095 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6096 return 1;
6097 }
6098 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6099 int res;
6100 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6101 if (tmp == NULL) goto failed;
6102 res = obj2ast_expr(tmp, &value, arena);
6103 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006104 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006105 } else {
6106 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6107 return 1;
6108 }
6109 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6110 int res;
6111 Py_ssize_t len;
6112 Py_ssize_t i;
6113 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6114 if (tmp == NULL) goto failed;
6115 if (!PyList_Check(tmp)) {
6116 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6117 goto failed;
6118 }
6119 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006120 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006121 if (generators == NULL) goto failed;
6122 for (i = 0; i < len; i++) {
6123 comprehension_ty value;
6124 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6125 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006126 if (len != PyList_GET_SIZE(tmp)) {
6127 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6128 goto failed;
6129 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006130 asdl_seq_SET(generators, i, value);
6131 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006132 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006133 } else {
6134 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6135 return 1;
6136 }
6137 *out = DictComp(key, value, generators, lineno, col_offset, arena);
6138 if (*out == NULL) goto failed;
6139 return 0;
6140 }
6141 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6142 if (isinstance == -1) {
6143 return 1;
6144 }
6145 if (isinstance) {
6146 expr_ty elt;
6147 asdl_seq* generators;
6148
6149 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6150 int res;
6151 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6152 if (tmp == NULL) goto failed;
6153 res = obj2ast_expr(tmp, &elt, arena);
6154 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006155 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006156 } else {
6157 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6158 return 1;
6159 }
6160 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6161 int res;
6162 Py_ssize_t len;
6163 Py_ssize_t i;
6164 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6165 if (tmp == NULL) goto failed;
6166 if (!PyList_Check(tmp)) {
6167 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6168 goto failed;
6169 }
6170 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006171 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006172 if (generators == NULL) goto failed;
6173 for (i = 0; i < len; i++) {
6174 comprehension_ty value;
6175 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6176 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006177 if (len != PyList_GET_SIZE(tmp)) {
6178 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6179 goto failed;
6180 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006181 asdl_seq_SET(generators, i, value);
6182 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006183 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006184 } else {
6185 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6186 return 1;
6187 }
6188 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6189 if (*out == NULL) goto failed;
6190 return 0;
6191 }
Yury Selivanov75445082015-05-11 22:57:16 -04006192 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6193 if (isinstance == -1) {
6194 return 1;
6195 }
6196 if (isinstance) {
6197 expr_ty value;
6198
6199 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6200 int res;
6201 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6202 if (tmp == NULL) goto failed;
6203 res = obj2ast_expr(tmp, &value, arena);
6204 if (res != 0) goto failed;
6205 Py_CLEAR(tmp);
6206 } else {
6207 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6208 return 1;
6209 }
6210 *out = Await(value, lineno, col_offset, arena);
6211 if (*out == NULL) goto failed;
6212 return 0;
6213 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006214 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6215 if (isinstance == -1) {
6216 return 1;
6217 }
6218 if (isinstance) {
6219 expr_ty value;
6220
Victor Stinneree4b59c2013-07-27 00:01:35 +02006221 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006222 int res;
6223 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6224 if (tmp == NULL) goto failed;
6225 res = obj2ast_expr(tmp, &value, arena);
6226 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006227 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006228 } else {
6229 value = NULL;
6230 }
6231 *out = Yield(value, lineno, col_offset, arena);
6232 if (*out == NULL) goto failed;
6233 return 0;
6234 }
6235 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6236 if (isinstance == -1) {
6237 return 1;
6238 }
6239 if (isinstance) {
6240 expr_ty value;
6241
6242 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6243 int res;
6244 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6245 if (tmp == NULL) goto failed;
6246 res = obj2ast_expr(tmp, &value, arena);
6247 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006248 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006249 } else {
6250 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6251 return 1;
6252 }
6253 *out = YieldFrom(value, lineno, col_offset, arena);
6254 if (*out == NULL) goto failed;
6255 return 0;
6256 }
6257 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6258 if (isinstance == -1) {
6259 return 1;
6260 }
6261 if (isinstance) {
6262 expr_ty left;
6263 asdl_int_seq* ops;
6264 asdl_seq* comparators;
6265
6266 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6267 int res;
6268 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6269 if (tmp == NULL) goto failed;
6270 res = obj2ast_expr(tmp, &left, arena);
6271 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006272 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006273 } else {
6274 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6275 return 1;
6276 }
6277 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6278 int res;
6279 Py_ssize_t len;
6280 Py_ssize_t i;
6281 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6282 if (tmp == NULL) goto failed;
6283 if (!PyList_Check(tmp)) {
6284 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6285 goto failed;
6286 }
6287 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006288 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006289 if (ops == NULL) goto failed;
6290 for (i = 0; i < len; i++) {
6291 cmpop_ty value;
6292 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6293 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006294 if (len != PyList_GET_SIZE(tmp)) {
6295 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6296 goto failed;
6297 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006298 asdl_seq_SET(ops, i, value);
6299 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006300 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006301 } else {
6302 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6303 return 1;
6304 }
6305 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6306 int res;
6307 Py_ssize_t len;
6308 Py_ssize_t i;
6309 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6310 if (tmp == NULL) goto failed;
6311 if (!PyList_Check(tmp)) {
6312 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6313 goto failed;
6314 }
6315 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006316 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006317 if (comparators == NULL) goto failed;
6318 for (i = 0; i < len; i++) {
6319 expr_ty value;
6320 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6321 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006322 if (len != PyList_GET_SIZE(tmp)) {
6323 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6324 goto failed;
6325 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006326 asdl_seq_SET(comparators, i, value);
6327 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006328 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006329 } else {
6330 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6331 return 1;
6332 }
6333 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6334 if (*out == NULL) goto failed;
6335 return 0;
6336 }
6337 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6338 if (isinstance == -1) {
6339 return 1;
6340 }
6341 if (isinstance) {
6342 expr_ty func;
6343 asdl_seq* args;
6344 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006345
6346 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6347 int res;
6348 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6349 if (tmp == NULL) goto failed;
6350 res = obj2ast_expr(tmp, &func, arena);
6351 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006352 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006353 } else {
6354 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6355 return 1;
6356 }
6357 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6358 int res;
6359 Py_ssize_t len;
6360 Py_ssize_t i;
6361 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6362 if (tmp == NULL) goto failed;
6363 if (!PyList_Check(tmp)) {
6364 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6365 goto failed;
6366 }
6367 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006368 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006369 if (args == NULL) goto failed;
6370 for (i = 0; i < len; i++) {
6371 expr_ty value;
6372 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6373 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006374 if (len != PyList_GET_SIZE(tmp)) {
6375 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6376 goto failed;
6377 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006378 asdl_seq_SET(args, i, value);
6379 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006380 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006381 } else {
6382 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6383 return 1;
6384 }
6385 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6386 int res;
6387 Py_ssize_t len;
6388 Py_ssize_t i;
6389 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6390 if (tmp == NULL) goto failed;
6391 if (!PyList_Check(tmp)) {
6392 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6393 goto failed;
6394 }
6395 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006396 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006397 if (keywords == NULL) goto failed;
6398 for (i = 0; i < len; i++) {
6399 keyword_ty value;
6400 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6401 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006402 if (len != PyList_GET_SIZE(tmp)) {
6403 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6404 goto failed;
6405 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006406 asdl_seq_SET(keywords, i, value);
6407 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006408 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006409 } else {
6410 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6411 return 1;
6412 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006413 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006414 if (*out == NULL) goto failed;
6415 return 0;
6416 }
6417 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6418 if (isinstance == -1) {
6419 return 1;
6420 }
6421 if (isinstance) {
6422 object n;
6423
6424 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6425 int res;
6426 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6427 if (tmp == NULL) goto failed;
6428 res = obj2ast_object(tmp, &n, arena);
6429 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006430 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006431 } else {
6432 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6433 return 1;
6434 }
6435 *out = Num(n, lineno, col_offset, arena);
6436 if (*out == NULL) goto failed;
6437 return 0;
6438 }
6439 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6440 if (isinstance == -1) {
6441 return 1;
6442 }
6443 if (isinstance) {
6444 string s;
6445
6446 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6447 int res;
6448 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6449 if (tmp == NULL) goto failed;
6450 res = obj2ast_string(tmp, &s, arena);
6451 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006452 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006453 } else {
6454 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6455 return 1;
6456 }
6457 *out = Str(s, lineno, col_offset, arena);
6458 if (*out == NULL) goto failed;
6459 return 0;
6460 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006461 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6462 if (isinstance == -1) {
6463 return 1;
6464 }
6465 if (isinstance) {
6466 expr_ty value;
6467 int conversion;
6468 expr_ty format_spec;
6469
6470 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6471 int res;
6472 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6473 if (tmp == NULL) goto failed;
6474 res = obj2ast_expr(tmp, &value, arena);
6475 if (res != 0) goto failed;
6476 Py_CLEAR(tmp);
6477 } else {
6478 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6479 return 1;
6480 }
6481 if (exists_not_none(obj, &PyId_conversion)) {
6482 int res;
6483 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6484 if (tmp == NULL) goto failed;
6485 res = obj2ast_int(tmp, &conversion, arena);
6486 if (res != 0) goto failed;
6487 Py_CLEAR(tmp);
6488 } else {
6489 conversion = 0;
6490 }
6491 if (exists_not_none(obj, &PyId_format_spec)) {
6492 int res;
6493 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6494 if (tmp == NULL) goto failed;
6495 res = obj2ast_expr(tmp, &format_spec, arena);
6496 if (res != 0) goto failed;
6497 Py_CLEAR(tmp);
6498 } else {
6499 format_spec = NULL;
6500 }
6501 *out = FormattedValue(value, conversion, format_spec, lineno,
6502 col_offset, arena);
6503 if (*out == NULL) goto failed;
6504 return 0;
6505 }
6506 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6507 if (isinstance == -1) {
6508 return 1;
6509 }
6510 if (isinstance) {
6511 asdl_seq* values;
6512
6513 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6514 int res;
6515 Py_ssize_t len;
6516 Py_ssize_t i;
6517 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6518 if (tmp == NULL) goto failed;
6519 if (!PyList_Check(tmp)) {
6520 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6521 goto failed;
6522 }
6523 len = PyList_GET_SIZE(tmp);
6524 values = _Py_asdl_seq_new(len, arena);
6525 if (values == NULL) goto failed;
6526 for (i = 0; i < len; i++) {
6527 expr_ty value;
6528 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6529 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03006530 if (len != PyList_GET_SIZE(tmp)) {
6531 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
6532 goto failed;
6533 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006534 asdl_seq_SET(values, i, value);
6535 }
6536 Py_CLEAR(tmp);
6537 } else {
6538 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6539 return 1;
6540 }
6541 *out = JoinedStr(values, lineno, col_offset, arena);
6542 if (*out == NULL) goto failed;
6543 return 0;
6544 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006545 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6546 if (isinstance == -1) {
6547 return 1;
6548 }
6549 if (isinstance) {
6550 bytes s;
6551
6552 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6553 int res;
6554 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6555 if (tmp == NULL) goto failed;
6556 res = obj2ast_bytes(tmp, &s, arena);
6557 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006558 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006559 } else {
6560 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6561 return 1;
6562 }
6563 *out = Bytes(s, lineno, col_offset, arena);
6564 if (*out == NULL) goto failed;
6565 return 0;
6566 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006567 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6568 if (isinstance == -1) {
6569 return 1;
6570 }
6571 if (isinstance) {
6572 singleton value;
6573
6574 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6575 int res;
6576 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6577 if (tmp == NULL) goto failed;
6578 res = obj2ast_singleton(tmp, &value, arena);
6579 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006580 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006581 } else {
6582 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6583 return 1;
6584 }
6585 *out = NameConstant(value, lineno, col_offset, arena);
6586 if (*out == NULL) goto failed;
6587 return 0;
6588 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6590 if (isinstance == -1) {
6591 return 1;
6592 }
6593 if (isinstance) {
6594
6595 *out = Ellipsis(lineno, col_offset, arena);
6596 if (*out == NULL) goto failed;
6597 return 0;
6598 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006599 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6600 if (isinstance == -1) {
6601 return 1;
6602 }
6603 if (isinstance) {
6604 constant value;
6605
6606 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6607 int res;
6608 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6609 if (tmp == NULL) goto failed;
6610 res = obj2ast_constant(tmp, &value, arena);
6611 if (res != 0) goto failed;
6612 Py_CLEAR(tmp);
6613 } else {
6614 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6615 return 1;
6616 }
6617 *out = Constant(value, lineno, col_offset, arena);
6618 if (*out == NULL) goto failed;
6619 return 0;
6620 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006621 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6622 if (isinstance == -1) {
6623 return 1;
6624 }
6625 if (isinstance) {
6626 expr_ty value;
6627 identifier attr;
6628 expr_context_ty ctx;
6629
6630 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6631 int res;
6632 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6633 if (tmp == NULL) goto failed;
6634 res = obj2ast_expr(tmp, &value, arena);
6635 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006636 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006637 } else {
6638 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6639 return 1;
6640 }
6641 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6642 int res;
6643 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6644 if (tmp == NULL) goto failed;
6645 res = obj2ast_identifier(tmp, &attr, arena);
6646 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006648 } else {
6649 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6650 return 1;
6651 }
6652 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6653 int res;
6654 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6655 if (tmp == NULL) goto failed;
6656 res = obj2ast_expr_context(tmp, &ctx, arena);
6657 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006658 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006659 } else {
6660 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6661 return 1;
6662 }
6663 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6664 if (*out == NULL) goto failed;
6665 return 0;
6666 }
6667 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6668 if (isinstance == -1) {
6669 return 1;
6670 }
6671 if (isinstance) {
6672 expr_ty value;
6673 slice_ty slice;
6674 expr_context_ty ctx;
6675
6676 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6677 int res;
6678 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6679 if (tmp == NULL) goto failed;
6680 res = obj2ast_expr(tmp, &value, arena);
6681 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006682 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006683 } else {
6684 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6685 return 1;
6686 }
6687 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6688 int res;
6689 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6690 if (tmp == NULL) goto failed;
6691 res = obj2ast_slice(tmp, &slice, arena);
6692 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006693 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006694 } else {
6695 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6696 return 1;
6697 }
6698 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6699 int res;
6700 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6701 if (tmp == NULL) goto failed;
6702 res = obj2ast_expr_context(tmp, &ctx, arena);
6703 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006704 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006705 } else {
6706 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6707 return 1;
6708 }
6709 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6710 if (*out == NULL) goto failed;
6711 return 0;
6712 }
6713 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6714 if (isinstance == -1) {
6715 return 1;
6716 }
6717 if (isinstance) {
6718 expr_ty value;
6719 expr_context_ty ctx;
6720
6721 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6722 int res;
6723 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6724 if (tmp == NULL) goto failed;
6725 res = obj2ast_expr(tmp, &value, arena);
6726 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006727 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006728 } else {
6729 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6730 return 1;
6731 }
6732 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6733 int res;
6734 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6735 if (tmp == NULL) goto failed;
6736 res = obj2ast_expr_context(tmp, &ctx, arena);
6737 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006738 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006739 } else {
6740 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6741 return 1;
6742 }
6743 *out = Starred(value, ctx, lineno, col_offset, arena);
6744 if (*out == NULL) goto failed;
6745 return 0;
6746 }
6747 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6748 if (isinstance == -1) {
6749 return 1;
6750 }
6751 if (isinstance) {
6752 identifier id;
6753 expr_context_ty ctx;
6754
6755 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6756 int res;
6757 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6758 if (tmp == NULL) goto failed;
6759 res = obj2ast_identifier(tmp, &id, arena);
6760 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006761 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006762 } else {
6763 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6764 return 1;
6765 }
6766 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6767 int res;
6768 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6769 if (tmp == NULL) goto failed;
6770 res = obj2ast_expr_context(tmp, &ctx, arena);
6771 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006772 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006773 } else {
6774 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6775 return 1;
6776 }
6777 *out = Name(id, ctx, lineno, col_offset, arena);
6778 if (*out == NULL) goto failed;
6779 return 0;
6780 }
6781 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6782 if (isinstance == -1) {
6783 return 1;
6784 }
6785 if (isinstance) {
6786 asdl_seq* elts;
6787 expr_context_ty ctx;
6788
6789 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6790 int res;
6791 Py_ssize_t len;
6792 Py_ssize_t i;
6793 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6794 if (tmp == NULL) goto failed;
6795 if (!PyList_Check(tmp)) {
6796 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6797 goto failed;
6798 }
6799 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006800 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006801 if (elts == NULL) goto failed;
6802 for (i = 0; i < len; i++) {
6803 expr_ty value;
6804 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6805 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006806 if (len != PyList_GET_SIZE(tmp)) {
6807 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6808 goto failed;
6809 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006810 asdl_seq_SET(elts, i, value);
6811 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006812 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006813 } else {
6814 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6815 return 1;
6816 }
6817 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6818 int res;
6819 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6820 if (tmp == NULL) goto failed;
6821 res = obj2ast_expr_context(tmp, &ctx, arena);
6822 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006823 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006824 } else {
6825 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6826 return 1;
6827 }
6828 *out = List(elts, ctx, lineno, col_offset, arena);
6829 if (*out == NULL) goto failed;
6830 return 0;
6831 }
6832 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6833 if (isinstance == -1) {
6834 return 1;
6835 }
6836 if (isinstance) {
6837 asdl_seq* elts;
6838 expr_context_ty ctx;
6839
6840 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6841 int res;
6842 Py_ssize_t len;
6843 Py_ssize_t i;
6844 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6845 if (tmp == NULL) goto failed;
6846 if (!PyList_Check(tmp)) {
6847 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6848 goto failed;
6849 }
6850 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006851 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006852 if (elts == NULL) goto failed;
6853 for (i = 0; i < len; i++) {
6854 expr_ty value;
6855 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6856 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006857 if (len != PyList_GET_SIZE(tmp)) {
6858 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6859 goto failed;
6860 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006861 asdl_seq_SET(elts, i, value);
6862 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006863 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006864 } else {
6865 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6866 return 1;
6867 }
6868 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6869 int res;
6870 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6871 if (tmp == NULL) goto failed;
6872 res = obj2ast_expr_context(tmp, &ctx, arena);
6873 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006874 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006875 } else {
6876 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6877 return 1;
6878 }
6879 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6880 if (*out == NULL) goto failed;
6881 return 0;
6882 }
6883
6884 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6885 failed:
6886 Py_XDECREF(tmp);
6887 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006888}
6889
6890int
6891obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6892{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006893 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006894
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006895 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6896 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006897 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006898 }
6899 if (isinstance) {
6900 *out = Load;
6901 return 0;
6902 }
6903 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6904 if (isinstance == -1) {
6905 return 1;
6906 }
6907 if (isinstance) {
6908 *out = Store;
6909 return 0;
6910 }
6911 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6912 if (isinstance == -1) {
6913 return 1;
6914 }
6915 if (isinstance) {
6916 *out = Del;
6917 return 0;
6918 }
6919 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6920 if (isinstance == -1) {
6921 return 1;
6922 }
6923 if (isinstance) {
6924 *out = AugLoad;
6925 return 0;
6926 }
6927 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6928 if (isinstance == -1) {
6929 return 1;
6930 }
6931 if (isinstance) {
6932 *out = AugStore;
6933 return 0;
6934 }
6935 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6936 if (isinstance == -1) {
6937 return 1;
6938 }
6939 if (isinstance) {
6940 *out = Param;
6941 return 0;
6942 }
6943
6944 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6945 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006946}
6947
6948int
6949obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6950{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006951 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006952
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006953 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006954
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006955 if (obj == Py_None) {
6956 *out = NULL;
6957 return 0;
6958 }
6959 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6960 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006961 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006962 }
6963 if (isinstance) {
6964 expr_ty lower;
6965 expr_ty upper;
6966 expr_ty step;
6967
Victor Stinneree4b59c2013-07-27 00:01:35 +02006968 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006969 int res;
6970 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6971 if (tmp == NULL) goto failed;
6972 res = obj2ast_expr(tmp, &lower, arena);
6973 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006975 } else {
6976 lower = NULL;
6977 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006978 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006979 int res;
6980 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6981 if (tmp == NULL) goto failed;
6982 res = obj2ast_expr(tmp, &upper, arena);
6983 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006984 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006985 } else {
6986 upper = NULL;
6987 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006988 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006989 int res;
6990 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6991 if (tmp == NULL) goto failed;
6992 res = obj2ast_expr(tmp, &step, arena);
6993 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006994 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006995 } else {
6996 step = NULL;
6997 }
6998 *out = Slice(lower, upper, step, arena);
6999 if (*out == NULL) goto failed;
7000 return 0;
7001 }
7002 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7003 if (isinstance == -1) {
7004 return 1;
7005 }
7006 if (isinstance) {
7007 asdl_seq* dims;
7008
7009 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
7010 int res;
7011 Py_ssize_t len;
7012 Py_ssize_t i;
7013 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
7014 if (tmp == NULL) goto failed;
7015 if (!PyList_Check(tmp)) {
7016 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7017 goto failed;
7018 }
7019 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007020 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007021 if (dims == NULL) goto failed;
7022 for (i = 0; i < len; i++) {
7023 slice_ty value;
7024 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
7025 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007026 if (len != PyList_GET_SIZE(tmp)) {
7027 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7028 goto failed;
7029 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007030 asdl_seq_SET(dims, i, value);
7031 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007032 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007033 } else {
7034 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7035 return 1;
7036 }
7037 *out = ExtSlice(dims, arena);
7038 if (*out == NULL) goto failed;
7039 return 0;
7040 }
7041 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7042 if (isinstance == -1) {
7043 return 1;
7044 }
7045 if (isinstance) {
7046 expr_ty value;
7047
7048 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7049 int res;
7050 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7051 if (tmp == NULL) goto failed;
7052 res = obj2ast_expr(tmp, &value, arena);
7053 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007054 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007055 } else {
7056 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7057 return 1;
7058 }
7059 *out = Index(value, arena);
7060 if (*out == NULL) goto failed;
7061 return 0;
7062 }
7063
7064 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7065 failed:
7066 Py_XDECREF(tmp);
7067 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007068}
7069
7070int
7071obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7072{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007073 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007074
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007075 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7076 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007077 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007078 }
7079 if (isinstance) {
7080 *out = And;
7081 return 0;
7082 }
7083 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7084 if (isinstance == -1) {
7085 return 1;
7086 }
7087 if (isinstance) {
7088 *out = Or;
7089 return 0;
7090 }
7091
7092 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7093 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007094}
7095
7096int
7097obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7098{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007099 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007100
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007101 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7102 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007103 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007104 }
7105 if (isinstance) {
7106 *out = Add;
7107 return 0;
7108 }
7109 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7110 if (isinstance == -1) {
7111 return 1;
7112 }
7113 if (isinstance) {
7114 *out = Sub;
7115 return 0;
7116 }
7117 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7118 if (isinstance == -1) {
7119 return 1;
7120 }
7121 if (isinstance) {
7122 *out = Mult;
7123 return 0;
7124 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007125 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7126 if (isinstance == -1) {
7127 return 1;
7128 }
7129 if (isinstance) {
7130 *out = MatMult;
7131 return 0;
7132 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007133 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7134 if (isinstance == -1) {
7135 return 1;
7136 }
7137 if (isinstance) {
7138 *out = Div;
7139 return 0;
7140 }
7141 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7142 if (isinstance == -1) {
7143 return 1;
7144 }
7145 if (isinstance) {
7146 *out = Mod;
7147 return 0;
7148 }
7149 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7150 if (isinstance == -1) {
7151 return 1;
7152 }
7153 if (isinstance) {
7154 *out = Pow;
7155 return 0;
7156 }
7157 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7158 if (isinstance == -1) {
7159 return 1;
7160 }
7161 if (isinstance) {
7162 *out = LShift;
7163 return 0;
7164 }
7165 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7166 if (isinstance == -1) {
7167 return 1;
7168 }
7169 if (isinstance) {
7170 *out = RShift;
7171 return 0;
7172 }
7173 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7174 if (isinstance == -1) {
7175 return 1;
7176 }
7177 if (isinstance) {
7178 *out = BitOr;
7179 return 0;
7180 }
7181 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7182 if (isinstance == -1) {
7183 return 1;
7184 }
7185 if (isinstance) {
7186 *out = BitXor;
7187 return 0;
7188 }
7189 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7190 if (isinstance == -1) {
7191 return 1;
7192 }
7193 if (isinstance) {
7194 *out = BitAnd;
7195 return 0;
7196 }
7197 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7198 if (isinstance == -1) {
7199 return 1;
7200 }
7201 if (isinstance) {
7202 *out = FloorDiv;
7203 return 0;
7204 }
7205
7206 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7207 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007208}
7209
7210int
7211obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7212{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007213 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007214
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007215 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7216 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007217 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007218 }
7219 if (isinstance) {
7220 *out = Invert;
7221 return 0;
7222 }
7223 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7224 if (isinstance == -1) {
7225 return 1;
7226 }
7227 if (isinstance) {
7228 *out = Not;
7229 return 0;
7230 }
7231 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7232 if (isinstance == -1) {
7233 return 1;
7234 }
7235 if (isinstance) {
7236 *out = UAdd;
7237 return 0;
7238 }
7239 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7240 if (isinstance == -1) {
7241 return 1;
7242 }
7243 if (isinstance) {
7244 *out = USub;
7245 return 0;
7246 }
7247
7248 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7249 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007250}
7251
7252int
7253obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7254{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007255 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007256
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007257 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7258 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007259 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007260 }
7261 if (isinstance) {
7262 *out = Eq;
7263 return 0;
7264 }
7265 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7266 if (isinstance == -1) {
7267 return 1;
7268 }
7269 if (isinstance) {
7270 *out = NotEq;
7271 return 0;
7272 }
7273 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7274 if (isinstance == -1) {
7275 return 1;
7276 }
7277 if (isinstance) {
7278 *out = Lt;
7279 return 0;
7280 }
7281 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7282 if (isinstance == -1) {
7283 return 1;
7284 }
7285 if (isinstance) {
7286 *out = LtE;
7287 return 0;
7288 }
7289 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7290 if (isinstance == -1) {
7291 return 1;
7292 }
7293 if (isinstance) {
7294 *out = Gt;
7295 return 0;
7296 }
7297 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7298 if (isinstance == -1) {
7299 return 1;
7300 }
7301 if (isinstance) {
7302 *out = GtE;
7303 return 0;
7304 }
7305 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7306 if (isinstance == -1) {
7307 return 1;
7308 }
7309 if (isinstance) {
7310 *out = Is;
7311 return 0;
7312 }
7313 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7314 if (isinstance == -1) {
7315 return 1;
7316 }
7317 if (isinstance) {
7318 *out = IsNot;
7319 return 0;
7320 }
7321 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7322 if (isinstance == -1) {
7323 return 1;
7324 }
7325 if (isinstance) {
7326 *out = In;
7327 return 0;
7328 }
7329 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7330 if (isinstance == -1) {
7331 return 1;
7332 }
7333 if (isinstance) {
7334 *out = NotIn;
7335 return 0;
7336 }
7337
7338 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7339 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007340}
7341
7342int
7343obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7344{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007345 PyObject* tmp = NULL;
7346 expr_ty target;
7347 expr_ty iter;
7348 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007349 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007350
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007351 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7352 int res;
7353 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7354 if (tmp == NULL) goto failed;
7355 res = obj2ast_expr(tmp, &target, arena);
7356 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007357 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007358 } else {
7359 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007360 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007361 }
7362 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7363 int res;
7364 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7365 if (tmp == NULL) goto failed;
7366 res = obj2ast_expr(tmp, &iter, arena);
7367 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007368 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007369 } else {
7370 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7371 return 1;
7372 }
7373 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7374 int res;
7375 Py_ssize_t len;
7376 Py_ssize_t i;
7377 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7378 if (tmp == NULL) goto failed;
7379 if (!PyList_Check(tmp)) {
7380 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7381 goto failed;
7382 }
7383 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007384 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007385 if (ifs == NULL) goto failed;
7386 for (i = 0; i < len; i++) {
7387 expr_ty value;
7388 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7389 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007390 if (len != PyList_GET_SIZE(tmp)) {
7391 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7392 goto failed;
7393 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007394 asdl_seq_SET(ifs, i, value);
7395 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007396 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007397 } else {
7398 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7399 return 1;
7400 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007401 if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7402 int res;
7403 tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7404 if (tmp == NULL) goto failed;
7405 res = obj2ast_int(tmp, &is_async, arena);
7406 if (res != 0) goto failed;
7407 Py_CLEAR(tmp);
7408 } else {
7409 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7410 return 1;
7411 }
7412 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007413 return 0;
7414failed:
7415 Py_XDECREF(tmp);
7416 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007417}
7418
7419int
7420obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7421{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007422 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007423
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007424 PyObject *tmp = NULL;
7425 int lineno;
7426 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007427
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007428 if (obj == Py_None) {
7429 *out = NULL;
7430 return 0;
7431 }
7432 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7433 int res;
7434 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7435 if (tmp == NULL) goto failed;
7436 res = obj2ast_int(tmp, &lineno, arena);
7437 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007439 } else {
7440 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007441 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007442 }
7443 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7444 int res;
7445 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7446 if (tmp == NULL) goto failed;
7447 res = obj2ast_int(tmp, &col_offset, arena);
7448 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007449 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007450 } else {
7451 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7452 return 1;
7453 }
7454 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7455 if (isinstance == -1) {
7456 return 1;
7457 }
7458 if (isinstance) {
7459 expr_ty type;
7460 identifier name;
7461 asdl_seq* body;
7462
Victor Stinneree4b59c2013-07-27 00:01:35 +02007463 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007464 int res;
7465 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7466 if (tmp == NULL) goto failed;
7467 res = obj2ast_expr(tmp, &type, arena);
7468 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007469 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007470 } else {
7471 type = NULL;
7472 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007473 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007474 int res;
7475 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7476 if (tmp == NULL) goto failed;
7477 res = obj2ast_identifier(tmp, &name, arena);
7478 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007479 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007480 } else {
7481 name = NULL;
7482 }
7483 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7484 int res;
7485 Py_ssize_t len;
7486 Py_ssize_t i;
7487 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7488 if (tmp == NULL) goto failed;
7489 if (!PyList_Check(tmp)) {
7490 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7491 goto failed;
7492 }
7493 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007494 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007495 if (body == NULL) goto failed;
7496 for (i = 0; i < len; i++) {
7497 stmt_ty value;
7498 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7499 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007500 if (len != PyList_GET_SIZE(tmp)) {
7501 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7502 goto failed;
7503 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007504 asdl_seq_SET(body, i, value);
7505 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007507 } else {
7508 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7509 return 1;
7510 }
7511 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7512 if (*out == NULL) goto failed;
7513 return 0;
7514 }
7515
7516 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7517 failed:
7518 Py_XDECREF(tmp);
7519 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007520}
7521
7522int
7523obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7524{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007525 PyObject* tmp = NULL;
7526 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007527 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007528 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007529 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007530 arg_ty kwarg;
7531 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007532
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007533 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7534 int res;
7535 Py_ssize_t len;
7536 Py_ssize_t i;
7537 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7538 if (tmp == NULL) goto failed;
7539 if (!PyList_Check(tmp)) {
7540 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7541 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007542 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007543 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007544 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007545 if (args == NULL) goto failed;
7546 for (i = 0; i < len; i++) {
7547 arg_ty value;
7548 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7549 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007550 if (len != PyList_GET_SIZE(tmp)) {
7551 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7552 goto failed;
7553 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007554 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007555 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007556 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007557 } else {
7558 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007559 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007560 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007561 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007562 int res;
7563 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7564 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007565 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007566 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007567 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007568 } else {
7569 vararg = NULL;
7570 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007571 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7572 int res;
7573 Py_ssize_t len;
7574 Py_ssize_t i;
7575 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7576 if (tmp == NULL) goto failed;
7577 if (!PyList_Check(tmp)) {
7578 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7579 goto failed;
7580 }
7581 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007582 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007583 if (kwonlyargs == NULL) goto failed;
7584 for (i = 0; i < len; i++) {
7585 arg_ty value;
7586 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7587 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007588 if (len != PyList_GET_SIZE(tmp)) {
7589 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7590 goto failed;
7591 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007592 asdl_seq_SET(kwonlyargs, i, value);
7593 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007594 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007595 } else {
7596 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7597 return 1;
7598 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007599 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7600 int res;
7601 Py_ssize_t len;
7602 Py_ssize_t i;
7603 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7604 if (tmp == NULL) goto failed;
7605 if (!PyList_Check(tmp)) {
7606 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7607 goto failed;
7608 }
7609 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007610 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007611 if (kw_defaults == NULL) goto failed;
7612 for (i = 0; i < len; i++) {
7613 expr_ty value;
7614 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7615 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007616 if (len != PyList_GET_SIZE(tmp)) {
7617 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7618 goto failed;
7619 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007620 asdl_seq_SET(kw_defaults, i, value);
7621 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007622 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007623 } else {
7624 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7625 return 1;
7626 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007627 if (exists_not_none(obj, &PyId_kwarg)) {
7628 int res;
7629 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7630 if (tmp == NULL) goto failed;
7631 res = obj2ast_arg(tmp, &kwarg, arena);
7632 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007633 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007634 } else {
7635 kwarg = NULL;
7636 }
7637 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7638 int res;
7639 Py_ssize_t len;
7640 Py_ssize_t i;
7641 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7642 if (tmp == NULL) goto failed;
7643 if (!PyList_Check(tmp)) {
7644 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7645 goto failed;
7646 }
7647 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007648 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007649 if (defaults == NULL) goto failed;
7650 for (i = 0; i < len; i++) {
7651 expr_ty value;
7652 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7653 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007654 if (len != PyList_GET_SIZE(tmp)) {
7655 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7656 goto failed;
7657 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007658 asdl_seq_SET(defaults, i, value);
7659 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007660 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007661 } else {
7662 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7663 return 1;
7664 }
7665 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7666 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007667 return 0;
7668failed:
7669 Py_XDECREF(tmp);
7670 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007671}
7672
7673int
7674obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7675{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007676 PyObject* tmp = NULL;
7677 identifier arg;
7678 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007679 int lineno;
7680 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007681
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007682 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7683 int res;
7684 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7685 if (tmp == NULL) goto failed;
7686 res = obj2ast_identifier(tmp, &arg, arena);
7687 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007688 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007689 } else {
7690 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007691 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007692 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007693 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007694 int res;
7695 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7696 if (tmp == NULL) goto failed;
7697 res = obj2ast_expr(tmp, &annotation, arena);
7698 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007699 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007700 } else {
7701 annotation = NULL;
7702 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007703 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7704 int res;
7705 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7706 if (tmp == NULL) goto failed;
7707 res = obj2ast_int(tmp, &lineno, arena);
7708 if (res != 0) goto failed;
7709 Py_CLEAR(tmp);
7710 } else {
7711 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7712 return 1;
7713 }
7714 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7715 int res;
7716 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7717 if (tmp == NULL) goto failed;
7718 res = obj2ast_int(tmp, &col_offset, arena);
7719 if (res != 0) goto failed;
7720 Py_CLEAR(tmp);
7721 } else {
7722 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7723 return 1;
7724 }
7725 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007726 return 0;
7727failed:
7728 Py_XDECREF(tmp);
7729 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007730}
7731
7732int
7733obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7734{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007735 PyObject* tmp = NULL;
7736 identifier arg;
7737 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007738
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007739 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007740 int res;
7741 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7742 if (tmp == NULL) goto failed;
7743 res = obj2ast_identifier(tmp, &arg, arena);
7744 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007745 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007746 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007747 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007748 }
7749 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7750 int res;
7751 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7752 if (tmp == NULL) goto failed;
7753 res = obj2ast_expr(tmp, &value, arena);
7754 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007755 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007756 } else {
7757 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7758 return 1;
7759 }
7760 *out = keyword(arg, value, arena);
7761 return 0;
7762failed:
7763 Py_XDECREF(tmp);
7764 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007765}
7766
7767int
7768obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7769{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007770 PyObject* tmp = NULL;
7771 identifier name;
7772 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007773
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007774 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7775 int res;
7776 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7777 if (tmp == NULL) goto failed;
7778 res = obj2ast_identifier(tmp, &name, arena);
7779 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007780 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007781 } else {
7782 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007783 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007784 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007785 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007786 int res;
7787 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7788 if (tmp == NULL) goto failed;
7789 res = obj2ast_identifier(tmp, &asname, arena);
7790 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007791 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007792 } else {
7793 asname = NULL;
7794 }
7795 *out = alias(name, asname, arena);
7796 return 0;
7797failed:
7798 Py_XDECREF(tmp);
7799 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007800}
7801
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007802int
7803obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7804{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007805 PyObject* tmp = NULL;
7806 expr_ty context_expr;
7807 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007808
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007809 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7810 int res;
7811 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7812 if (tmp == NULL) goto failed;
7813 res = obj2ast_expr(tmp, &context_expr, arena);
7814 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007815 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007816 } else {
7817 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007818 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007819 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007820 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007821 int res;
7822 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7823 if (tmp == NULL) goto failed;
7824 res = obj2ast_expr(tmp, &optional_vars, arena);
7825 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007827 } else {
7828 optional_vars = NULL;
7829 }
7830 *out = withitem(context_expr, optional_vars, arena);
7831 return 0;
7832failed:
7833 Py_XDECREF(tmp);
7834 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007835}
7836
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007837
Martin v. Löwis1a214512008-06-11 05:26:20 +00007838static struct PyModuleDef _astmodule = {
7839 PyModuleDef_HEAD_INIT, "_ast"
7840};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007841PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007842PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007843{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007844 PyObject *m, *d;
7845 if (!init_types()) return NULL;
7846 m = PyModule_Create(&_astmodule);
7847 if (!m) return NULL;
7848 d = PyModule_GetDict(m);
7849 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007850 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007851 return NULL;
7852 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7853 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7854 NULL;
7855 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7856 0) return NULL;
7857 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7858 return NULL;
7859 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7860 NULL;
7861 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7862 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7863 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007864 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7865 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007866 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7867 return NULL;
7868 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7869 NULL;
7870 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7871 NULL;
7872 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7873 NULL;
7874 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7875 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007876 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7877 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007878 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007879 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7880 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007881 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7882 NULL;
7883 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7884 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007885 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7886 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007887 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7888 NULL;
7889 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7890 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7891 NULL;
7892 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7893 NULL;
7894 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7895 return NULL;
7896 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7897 NULL;
7898 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7899 return NULL;
7900 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7901 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7902 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7903 NULL;
7904 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7905 return NULL;
7906 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7907 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7908 NULL;
7909 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7910 NULL;
7911 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7912 NULL;
7913 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7914 NULL;
7915 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7916 NULL;
7917 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7918 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7919 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7920 return NULL;
7921 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7922 NULL;
7923 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7924 return NULL;
7925 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7926 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007927 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7928 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007929 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7930 NULL;
7931 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7932 return NULL;
7933 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7934 NULL;
7935 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7936 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7937 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007938 if (PyDict_SetItemString(d, "FormattedValue",
7939 (PyObject*)FormattedValue_type) < 0) return NULL;
7940 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7941 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007942 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7943 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007944 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7945 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007946 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7947 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007948 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7949 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007950 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7951 return NULL;
7952 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7953 return NULL;
7954 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7955 NULL;
7956 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7957 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7958 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7959 NULL;
7960 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7961 0) return NULL;
7962 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7963 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7964 NULL;
7965 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7966 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7967 NULL;
7968 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7969 return NULL;
7970 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7971 NULL;
7972 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7973 NULL;
7974 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7975 NULL;
7976 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7977 return NULL;
7978 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7979 NULL;
7980 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7981 NULL;
7982 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7983 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7984 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7985 return NULL;
7986 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7987 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7988 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007989 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7990 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007991 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7992 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7993 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7994 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7995 NULL;
7996 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7997 NULL;
7998 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7999 NULL;
8000 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8001 NULL;
8002 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8003 NULL;
8004 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8005 return NULL;
8006 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8007 NULL;
8008 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8009 NULL;
8010 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8011 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8012 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8013 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8014 NULL;
8015 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8016 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8017 NULL;
8018 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8019 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8020 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8021 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8022 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8023 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8024 NULL;
8025 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8026 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8027 NULL;
8028 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8029 < 0) return NULL;
8030 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8031 < 0) return NULL;
8032 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8033 < 0) return NULL;
8034 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8035 return NULL;
8036 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8037 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8038 NULL;
8039 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8040 NULL;
8041 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8042 return NULL;
8043 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008044}
8045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008046
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008047PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008048{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008049 if (!init_types())
8050 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008051 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008052}
Martin v. Löwis5b222132007-06-10 09:51:05 +00008053
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008054/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
8055mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008056{
8057 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008058 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008059 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008060 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008061
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008062 req_type[0] = (PyObject*)Module_type;
8063 req_type[1] = (PyObject*)Expression_type;
8064 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008065
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008066 assert(0 <= mode && mode <= 2);
8067
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008068 if (!init_types())
8069 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008070
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008071 isinstance = PyObject_IsInstance(ast, req_type[mode]);
8072 if (isinstance == -1)
8073 return NULL;
8074 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008075 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8076 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008077 return NULL;
8078 }
8079 if (obj2ast_mod(ast, &res, arena) != 0)
8080 return NULL;
8081 else
8082 return res;
8083}
8084
8085int PyAST_Check(PyObject* obj)
8086{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008087 if (!init_types())
8088 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00008089 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008090}
8091
Martin v. Löwis5b222132007-06-10 09:51:05 +00008092