blob: f10e315707203b0a60d1b53661723fb92ea68d03 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07003#include <stddef.h>
4
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "Python.h"
6#include "Python-ast.h"
7
Neal Norwitz207c9f32008-03-31 04:42:11 +00008static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +00009static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000010static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000011static PyTypeObject *Module_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020012_Py_IDENTIFIER(body);
Neal Norwitz53d960c2006-02-28 22:47:29 +000013static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020014 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000015};
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *Interactive_type;
17static char *Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020018 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000019};
Neal Norwitz53d960c2006-02-28 22:47:29 +000020static PyTypeObject *Expression_type;
21static char *Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020022 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000023};
Neal Norwitz53d960c2006-02-28 22:47:29 +000024static PyTypeObject *Suite_type;
25static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020026 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000027};
Neal Norwitz53d960c2006-02-28 22:47:29 +000028static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020029_Py_IDENTIFIER(lineno);
30_Py_IDENTIFIER(col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000031static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020032 "lineno",
33 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000034};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000035static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000036static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020037_Py_IDENTIFIER(name);
38_Py_IDENTIFIER(args);
39_Py_IDENTIFIER(decorator_list);
40_Py_IDENTIFIER(returns);
Neal Norwitz53d960c2006-02-28 22:47:29 +000041static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020042 "name",
43 "args",
44 "body",
45 "decorator_list",
46 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Yury Selivanov75445082015-05-11 22:57:16 -040048static PyTypeObject *AsyncFunctionDef_type;
49static char *AsyncFunctionDef_fields[]={
50 "name",
51 "args",
52 "body",
53 "decorator_list",
54 "returns",
55};
Neal Norwitz53d960c2006-02-28 22:47:29 +000056static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020057_Py_IDENTIFIER(bases);
58_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000059static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020060 "name",
61 "bases",
62 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020063 "body",
64 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000065};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020067_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020069 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075};
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static PyTypeObject *Assign_type;
77static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020078 "targets",
79 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(target);
83_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020085 "target",
86 "op",
87 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088};
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070089static PyTypeObject *AnnAssign_type;
90_Py_IDENTIFIER(annotation);
91_Py_IDENTIFIER(simple);
92static char *AnnAssign_fields[]={
93 "target",
94 "annotation",
95 "value",
96 "simple",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020099_Py_IDENTIFIER(iter);
100_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000101static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200102 "target",
103 "iter",
104 "body",
105 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Yury Selivanov75445082015-05-11 22:57:16 -0400107static PyTypeObject *AsyncFor_type;
108static char *AsyncFor_fields[]={
109 "target",
110 "iter",
111 "body",
112 "orelse",
113};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000114static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200115_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200117 "test",
118 "body",
119 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *If_type;
122static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200123 "test",
124 "body",
125 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200128_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000129static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200130 "items",
131 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000132};
Yury Selivanov75445082015-05-11 22:57:16 -0400133static PyTypeObject *AsyncWith_type;
134static char *AsyncWith_fields[]={
135 "items",
136 "body",
137};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000138static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200139_Py_IDENTIFIER(exc);
140_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000141static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200142 "exc",
143 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500145static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146_Py_IDENTIFIER(handlers);
147_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500148static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200149 "body",
150 "handlers",
151 "orelse",
152 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200155_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200157 "test",
158 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000159};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000160static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200161_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000162static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200163 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200166_Py_IDENTIFIER(module);
167_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000168static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200169 "module",
170 "names",
171 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000173static PyTypeObject *Global_type;
174static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200175 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000176};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000177static PyTypeObject *Nonlocal_type;
178static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200179 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000180};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static PyTypeObject *Expr_type;
182static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200183 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *Pass_type;
186static PyTypeObject *Break_type;
187static PyTypeObject *Continue_type;
188static PyTypeObject *expr_type;
189static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200190 "lineno",
191 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000192};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000194static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200195_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200197 "op",
198 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000199};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000200static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200201_Py_IDENTIFIER(left);
202_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000203static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200204 "left",
205 "op",
206 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000208static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200209_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200211 "op",
212 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000214static PyTypeObject *Lambda_type;
215static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200216 "args",
217 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *IfExp_type;
220static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200221 "test",
222 "body",
223 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000224};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000225static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200226_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200228 "keys",
229 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000230};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000231static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200232_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000233static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200234 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000235};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000236static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200237_Py_IDENTIFIER(elt);
238_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000239static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200240 "elt",
241 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000242};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000243static PyTypeObject *SetComp_type;
244static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200245 "elt",
246 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000247};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000248static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200249_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000250static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200251 "key",
252 "value",
253 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000254};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000255static PyTypeObject *GeneratorExp_type;
256static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200257 "elt",
258 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259};
Yury Selivanov75445082015-05-11 22:57:16 -0400260static PyTypeObject *Await_type;
261static char *Await_fields[]={
262 "value",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *Yield_type;
265static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200266 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500267};
268static PyTypeObject *YieldFrom_type;
269static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200270 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000272static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200273_Py_IDENTIFIER(ops);
274_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000275static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200276 "left",
277 "ops",
278 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000279};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000280static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200281_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000282static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200283 "func",
284 "args",
285 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000287static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200288_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000289static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200293_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000294static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200295 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000296};
Eric V. Smith235a6f02015-09-19 14:51:32 -0400297static PyTypeObject *FormattedValue_type;
298_Py_IDENTIFIER(conversion);
299_Py_IDENTIFIER(format_spec);
300static char *FormattedValue_fields[]={
301 "value",
302 "conversion",
303 "format_spec",
304};
305static PyTypeObject *JoinedStr_type;
306static char *JoinedStr_fields[]={
307 "values",
308};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000309static PyTypeObject *Bytes_type;
310static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200311 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000312};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500313static PyTypeObject *NameConstant_type;
314static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200315 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500316};
Georg Brandl52318d62006-09-06 07:06:08 +0000317static PyTypeObject *Ellipsis_type;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100318static PyTypeObject *Constant_type;
319static char *Constant_fields[]={
320 "value",
321};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000322static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200323_Py_IDENTIFIER(attr);
324_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000325static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200326 "value",
327 "attr",
328 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000329};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000330static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200331_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000332static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200333 "value",
334 "slice",
335 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000336};
Guido van Rossum0368b722007-05-11 16:50:42 +0000337static PyTypeObject *Starred_type;
338static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200339 "value",
340 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000341};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000342static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200343_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000344static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200345 "id",
346 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static PyTypeObject *List_type;
349static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200350 "elts",
351 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *Tuple_type;
354static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200355 "elts",
356 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000359static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
360*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
361static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000362static PyTypeObject *Load_type;
363static PyTypeObject *Store_type;
364static PyTypeObject *Del_type;
365static PyTypeObject *AugLoad_type;
366static PyTypeObject *AugStore_type;
367static PyTypeObject *Param_type;
368static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000370static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200371_Py_IDENTIFIER(lower);
372_Py_IDENTIFIER(upper);
373_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000374static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200375 "lower",
376 "upper",
377 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000378};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000379static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200380_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000381static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200382 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000383};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static PyTypeObject *Index_type;
385static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200386 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000388static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000389static PyObject *And_singleton, *Or_singleton;
390static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static PyTypeObject *And_type;
392static PyTypeObject *Or_type;
393static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000394static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400395*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
396*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
397*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000398static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000399static PyTypeObject *Add_type;
400static PyTypeObject *Sub_type;
401static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400402static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000403static PyTypeObject *Div_type;
404static PyTypeObject *Mod_type;
405static PyTypeObject *Pow_type;
406static PyTypeObject *LShift_type;
407static PyTypeObject *RShift_type;
408static PyTypeObject *BitOr_type;
409static PyTypeObject *BitXor_type;
410static PyTypeObject *BitAnd_type;
411static PyTypeObject *FloorDiv_type;
412static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000413static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
414*USub_singleton;
415static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000416static PyTypeObject *Invert_type;
417static PyTypeObject *Not_type;
418static PyTypeObject *UAdd_type;
419static PyTypeObject *USub_type;
420static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000421static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
422*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
423*NotIn_singleton;
424static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000425static PyTypeObject *Eq_type;
426static PyTypeObject *NotEq_type;
427static PyTypeObject *Lt_type;
428static PyTypeObject *LtE_type;
429static PyTypeObject *Gt_type;
430static PyTypeObject *GtE_type;
431static PyTypeObject *Is_type;
432static PyTypeObject *IsNot_type;
433static PyTypeObject *In_type;
434static PyTypeObject *NotIn_type;
435static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000436static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200437_Py_IDENTIFIER(ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700438_Py_IDENTIFIER(is_async);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000439static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200440 "target",
441 "iter",
442 "ifs",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700443 "is_async",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000444};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000445static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000446static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200447 "lineno",
448 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000449};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000450static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000451static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200452_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000453static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200454 "type",
455 "name",
456 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000457};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000458static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000459static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200460_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200461_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200462_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700463_Py_IDENTIFIER(kwarg);
464_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000465static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200466 "args",
467 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200468 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200469 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200470 "kwarg",
471 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000472};
Neal Norwitzc1505362006-12-28 06:47:50 +0000473static PyTypeObject *arg_type;
474static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700475static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200476 "lineno",
477 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700478};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200479_Py_IDENTIFIER(arg);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000480static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200481 "arg",
482 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000483};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000484static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000485static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000486static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200487 "arg",
488 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000489};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000490static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000491static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200492_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000493static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200494 "name",
495 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000496};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500497static PyTypeObject *withitem_type;
498static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200499_Py_IDENTIFIER(context_expr);
500_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500501static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200502 "context_expr",
503 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500504};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000505
506
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700507typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100508 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700509 PyObject *dict;
510} AST_object;
511
Benjamin Peterson1767e022012-03-14 21:50:29 -0500512static void
513ast_dealloc(AST_object *self)
514{
515 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200516 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500517}
518
Neal Norwitz207c9f32008-03-31 04:42:11 +0000519static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700520ast_traverse(AST_object *self, visitproc visit, void *arg)
521{
522 Py_VISIT(self->dict);
523 return 0;
524}
525
526static void
527ast_clear(AST_object *self)
528{
529 Py_CLEAR(self->dict);
530}
531
532static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000533ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
534{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200535 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000536 Py_ssize_t i, numfields = 0;
537 int res = -1;
538 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200539 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000540 if (!fields)
541 PyErr_Clear();
542 if (fields) {
543 numfields = PySequence_Size(fields);
544 if (numfields == -1)
545 goto cleanup;
546 }
547 res = 0; /* if no error occurs, this stays 0 to the end */
548 if (PyTuple_GET_SIZE(args) > 0) {
549 if (numfields != PyTuple_GET_SIZE(args)) {
550 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000551 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000552 Py_TYPE(self)->tp_name,
553 numfields == 0 ? "" : "either 0 or ",
554 numfields, numfields == 1 ? "" : "s");
555 res = -1;
556 goto cleanup;
557 }
558 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
559 /* cannot be reached when fields is NULL */
560 PyObject *name = PySequence_GetItem(fields, i);
561 if (!name) {
562 res = -1;
563 goto cleanup;
564 }
565 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
566 Py_DECREF(name);
567 if (res < 0)
568 goto cleanup;
569 }
570 }
571 if (kw) {
572 i = 0; /* needed by PyDict_Next */
573 while (PyDict_Next(kw, &i, &key, &value)) {
574 res = PyObject_SetAttr(self, key, value);
575 if (res < 0)
576 goto cleanup;
577 }
578 }
579 cleanup:
580 Py_XDECREF(fields);
581 return res;
582}
583
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000584/* Pickling support */
585static PyObject *
586ast_type_reduce(PyObject *self, PyObject *unused)
587{
588 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200589 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200590 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000591 if (dict == NULL) {
592 if (PyErr_ExceptionMatches(PyExc_AttributeError))
593 PyErr_Clear();
594 else
595 return NULL;
596 }
597 if (dict) {
598 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
599 Py_DECREF(dict);
600 return res;
601 }
602 return Py_BuildValue("O()", Py_TYPE(self));
603}
604
605static PyMethodDef ast_type_methods[] = {
606 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
607 {NULL}
608};
609
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700610static PyGetSetDef ast_type_getsets[] = {
611 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
612 {NULL}
613};
614
Neal Norwitz207c9f32008-03-31 04:42:11 +0000615static PyTypeObject AST_type = {
616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000617 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700618 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000619 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500620 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000621 0, /* tp_print */
622 0, /* tp_getattr */
623 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000624 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000625 0, /* tp_repr */
626 0, /* tp_as_number */
627 0, /* tp_as_sequence */
628 0, /* tp_as_mapping */
629 0, /* tp_hash */
630 0, /* tp_call */
631 0, /* tp_str */
632 PyObject_GenericGetAttr, /* tp_getattro */
633 PyObject_GenericSetAttr, /* tp_setattro */
634 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000636 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700637 (traverseproc)ast_traverse, /* tp_traverse */
638 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000639 0, /* tp_richcompare */
640 0, /* tp_weaklistoffset */
641 0, /* tp_iter */
642 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000643 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000644 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700645 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000646 0, /* tp_base */
647 0, /* tp_dict */
648 0, /* tp_descr_get */
649 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700650 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000651 (initproc)ast_type_init, /* tp_init */
652 PyType_GenericAlloc, /* tp_alloc */
653 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700654 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000655};
656
657
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
659{
660 PyObject *fnames, *result;
661 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000662 fnames = PyTuple_New(num_fields);
663 if (!fnames) return NULL;
664 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000665 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 if (!field) {
667 Py_DECREF(fnames);
668 return NULL;
669 }
670 PyTuple_SET_ITEM(fnames, i, field);
671 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000672 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Py_DECREF(fnames);
675 return (PyTypeObject*)result;
676}
677
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
679{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200681 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000682 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000683 if (!l)
684 return 0;
685 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000686 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!s) {
688 Py_DECREF(l);
689 return 0;
690 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000691 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200693 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 Py_DECREF(l);
695 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696}
697
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000698/* Conversion AST -> Python */
699
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
701{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700702 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 PyObject *result = PyList_New(n);
704 PyObject *value;
705 if (!result)
706 return NULL;
707 for (i = 0; i < n; i++) {
708 value = func(asdl_seq_GET(seq, i));
709 if (!value) {
710 Py_DECREF(result);
711 return NULL;
712 }
713 PyList_SET_ITEM(result, i, value);
714 }
715 return result;
716}
717
718static PyObject* ast2obj_object(void *o)
719{
720 if (!o)
721 o = Py_None;
722 Py_INCREF((PyObject*)o);
723 return (PyObject*)o;
724}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500725#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100726#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727#define ast2obj_identifier ast2obj_object
728#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500729#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000731static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732{
Christian Heimes217cfd12007-12-02 14:31:20 +0000733 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734}
735
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000736/* Conversion Python -> AST */
737
Benjamin Peterson442f2092012-12-06 17:41:04 -0500738static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
739{
740 if (obj != Py_None && obj != Py_True && obj != Py_False) {
741 PyErr_SetString(PyExc_ValueError,
742 "AST singleton must be True, False, or None");
743 return 1;
744 }
745 *out = obj;
746 return 0;
747}
748
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000749static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
750{
751 if (obj == Py_None)
752 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200753 if (obj) {
754 if (PyArena_AddPyObject(arena, obj) < 0) {
755 *out = NULL;
756 return -1;
757 }
758 Py_INCREF(obj);
759 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000760 *out = obj;
761 return 0;
762}
763
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100764static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
765{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100766 if (obj) {
767 if (PyArena_AddPyObject(arena, obj) < 0) {
768 *out = NULL;
769 return -1;
770 }
771 Py_INCREF(obj);
772 }
773 *out = obj;
774 return 0;
775}
776
Benjamin Peterson180e6352011-07-22 11:09:07 -0500777static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500778{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500779 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
780 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500781 return 1;
782 }
783 return obj2ast_object(obj, out, arena);
784}
785
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500786static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
787{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400788 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500789 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
790 return 1;
791 }
792 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500793}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000794
Benjamin Petersone2498412011-08-09 16:08:39 -0500795static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
796{
797 if (!PyBytes_CheckExact(obj)) {
798 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
799 return 1;
800 }
801 return obj2ast_object(obj, out, arena);
802}
803
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000804static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
805{
806 int i;
807 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100808 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809 return 1;
810 }
811
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300812 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000813 if (i == -1 && PyErr_Occurred())
814 return 1;
815 *out = i;
816 return 0;
817}
818
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000819static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000820{
821 PyObject *empty_tuple, *d;
822 if (PyType_Ready(&AST_type) < 0)
823 return -1;
824 d = AST_type.tp_dict;
825 empty_tuple = PyTuple_New(0);
826 if (!empty_tuple ||
827 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
828 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
829 Py_XDECREF(empty_tuple);
830 return -1;
831 }
832 Py_DECREF(empty_tuple);
833 return 0;
834}
835
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700836static int exists_not_none(PyObject *obj, _Py_Identifier *id)
837{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700838 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700839 PyObject *attr = _PyObject_GetAttrId(obj, id);
840 if (!attr) {
841 PyErr_Clear();
842 return 0;
843 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700844 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700845 Py_DECREF(attr);
846 return !isnone;
847}
848
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000849
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000850static int init_types(void)
851{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200852 static int initialized;
853 if (initialized) return 1;
854 if (add_ast_fields() < 0) return 0;
855 mod_type = make_type("mod", &AST_type, NULL, 0);
856 if (!mod_type) return 0;
857 if (!add_attributes(mod_type, NULL, 0)) return 0;
858 Module_type = make_type("Module", mod_type, Module_fields, 1);
859 if (!Module_type) return 0;
860 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
861 1);
862 if (!Interactive_type) return 0;
863 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
864 if (!Expression_type) return 0;
865 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
866 if (!Suite_type) return 0;
867 stmt_type = make_type("stmt", &AST_type, NULL, 0);
868 if (!stmt_type) return 0;
869 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
870 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
871 5);
872 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400873 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
874 AsyncFunctionDef_fields, 5);
875 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400876 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200877 if (!ClassDef_type) return 0;
878 Return_type = make_type("Return", stmt_type, Return_fields, 1);
879 if (!Return_type) return 0;
880 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
881 if (!Delete_type) return 0;
882 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
883 if (!Assign_type) return 0;
884 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
885 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700886 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
887 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200888 For_type = make_type("For", stmt_type, For_fields, 4);
889 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400890 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
891 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200892 While_type = make_type("While", stmt_type, While_fields, 3);
893 if (!While_type) return 0;
894 If_type = make_type("If", stmt_type, If_fields, 3);
895 if (!If_type) return 0;
896 With_type = make_type("With", stmt_type, With_fields, 2);
897 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400898 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
899 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200900 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
901 if (!Raise_type) return 0;
902 Try_type = make_type("Try", stmt_type, Try_fields, 4);
903 if (!Try_type) return 0;
904 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
905 if (!Assert_type) return 0;
906 Import_type = make_type("Import", stmt_type, Import_fields, 1);
907 if (!Import_type) return 0;
908 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
909 if (!ImportFrom_type) return 0;
910 Global_type = make_type("Global", stmt_type, Global_fields, 1);
911 if (!Global_type) return 0;
912 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
913 if (!Nonlocal_type) return 0;
914 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
915 if (!Expr_type) return 0;
916 Pass_type = make_type("Pass", stmt_type, NULL, 0);
917 if (!Pass_type) return 0;
918 Break_type = make_type("Break", stmt_type, NULL, 0);
919 if (!Break_type) return 0;
920 Continue_type = make_type("Continue", stmt_type, NULL, 0);
921 if (!Continue_type) return 0;
922 expr_type = make_type("expr", &AST_type, NULL, 0);
923 if (!expr_type) return 0;
924 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
925 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
926 if (!BoolOp_type) return 0;
927 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
928 if (!BinOp_type) return 0;
929 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
930 if (!UnaryOp_type) return 0;
931 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
932 if (!Lambda_type) return 0;
933 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
934 if (!IfExp_type) return 0;
935 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
936 if (!Dict_type) return 0;
937 Set_type = make_type("Set", expr_type, Set_fields, 1);
938 if (!Set_type) return 0;
939 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
940 if (!ListComp_type) return 0;
941 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
942 if (!SetComp_type) return 0;
943 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
944 if (!DictComp_type) return 0;
945 GeneratorExp_type = make_type("GeneratorExp", expr_type,
946 GeneratorExp_fields, 2);
947 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400948 Await_type = make_type("Await", expr_type, Await_fields, 1);
949 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200950 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
951 if (!Yield_type) return 0;
952 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
953 if (!YieldFrom_type) return 0;
954 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
955 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400956 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200957 if (!Call_type) return 0;
958 Num_type = make_type("Num", expr_type, Num_fields, 1);
959 if (!Num_type) return 0;
960 Str_type = make_type("Str", expr_type, Str_fields, 1);
961 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400962 FormattedValue_type = make_type("FormattedValue", expr_type,
963 FormattedValue_fields, 3);
964 if (!FormattedValue_type) return 0;
965 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
966 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200967 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
968 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200969 NameConstant_type = make_type("NameConstant", expr_type,
970 NameConstant_fields, 1);
971 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200972 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
973 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100974 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
975 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200976 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
977 if (!Attribute_type) return 0;
978 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
979 if (!Subscript_type) return 0;
980 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
981 if (!Starred_type) return 0;
982 Name_type = make_type("Name", expr_type, Name_fields, 2);
983 if (!Name_type) return 0;
984 List_type = make_type("List", expr_type, List_fields, 2);
985 if (!List_type) return 0;
986 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
987 if (!Tuple_type) return 0;
988 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
989 if (!expr_context_type) return 0;
990 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
991 Load_type = make_type("Load", expr_context_type, NULL, 0);
992 if (!Load_type) return 0;
993 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
994 if (!Load_singleton) return 0;
995 Store_type = make_type("Store", expr_context_type, NULL, 0);
996 if (!Store_type) return 0;
997 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
998 if (!Store_singleton) return 0;
999 Del_type = make_type("Del", expr_context_type, NULL, 0);
1000 if (!Del_type) return 0;
1001 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1002 if (!Del_singleton) return 0;
1003 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1004 if (!AugLoad_type) return 0;
1005 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1006 if (!AugLoad_singleton) return 0;
1007 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1008 if (!AugStore_type) return 0;
1009 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1010 if (!AugStore_singleton) return 0;
1011 Param_type = make_type("Param", expr_context_type, NULL, 0);
1012 if (!Param_type) return 0;
1013 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1014 if (!Param_singleton) return 0;
1015 slice_type = make_type("slice", &AST_type, NULL, 0);
1016 if (!slice_type) return 0;
1017 if (!add_attributes(slice_type, NULL, 0)) return 0;
1018 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1019 if (!Slice_type) return 0;
1020 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1021 if (!ExtSlice_type) return 0;
1022 Index_type = make_type("Index", slice_type, Index_fields, 1);
1023 if (!Index_type) return 0;
1024 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1025 if (!boolop_type) return 0;
1026 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1027 And_type = make_type("And", boolop_type, NULL, 0);
1028 if (!And_type) return 0;
1029 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1030 if (!And_singleton) return 0;
1031 Or_type = make_type("Or", boolop_type, NULL, 0);
1032 if (!Or_type) return 0;
1033 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1034 if (!Or_singleton) return 0;
1035 operator_type = make_type("operator", &AST_type, NULL, 0);
1036 if (!operator_type) return 0;
1037 if (!add_attributes(operator_type, NULL, 0)) return 0;
1038 Add_type = make_type("Add", operator_type, NULL, 0);
1039 if (!Add_type) return 0;
1040 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1041 if (!Add_singleton) return 0;
1042 Sub_type = make_type("Sub", operator_type, NULL, 0);
1043 if (!Sub_type) return 0;
1044 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1045 if (!Sub_singleton) return 0;
1046 Mult_type = make_type("Mult", operator_type, NULL, 0);
1047 if (!Mult_type) return 0;
1048 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1049 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001050 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1051 if (!MatMult_type) return 0;
1052 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1053 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001054 Div_type = make_type("Div", operator_type, NULL, 0);
1055 if (!Div_type) return 0;
1056 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1057 if (!Div_singleton) return 0;
1058 Mod_type = make_type("Mod", operator_type, NULL, 0);
1059 if (!Mod_type) return 0;
1060 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1061 if (!Mod_singleton) return 0;
1062 Pow_type = make_type("Pow", operator_type, NULL, 0);
1063 if (!Pow_type) return 0;
1064 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1065 if (!Pow_singleton) return 0;
1066 LShift_type = make_type("LShift", operator_type, NULL, 0);
1067 if (!LShift_type) return 0;
1068 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1069 if (!LShift_singleton) return 0;
1070 RShift_type = make_type("RShift", operator_type, NULL, 0);
1071 if (!RShift_type) return 0;
1072 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1073 if (!RShift_singleton) return 0;
1074 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1075 if (!BitOr_type) return 0;
1076 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1077 if (!BitOr_singleton) return 0;
1078 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1079 if (!BitXor_type) return 0;
1080 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1081 if (!BitXor_singleton) return 0;
1082 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1083 if (!BitAnd_type) return 0;
1084 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1085 if (!BitAnd_singleton) return 0;
1086 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1087 if (!FloorDiv_type) return 0;
1088 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1089 if (!FloorDiv_singleton) return 0;
1090 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1091 if (!unaryop_type) return 0;
1092 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1093 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1094 if (!Invert_type) return 0;
1095 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1096 if (!Invert_singleton) return 0;
1097 Not_type = make_type("Not", unaryop_type, NULL, 0);
1098 if (!Not_type) return 0;
1099 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1100 if (!Not_singleton) return 0;
1101 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1102 if (!UAdd_type) return 0;
1103 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1104 if (!UAdd_singleton) return 0;
1105 USub_type = make_type("USub", unaryop_type, NULL, 0);
1106 if (!USub_type) return 0;
1107 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1108 if (!USub_singleton) return 0;
1109 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1110 if (!cmpop_type) return 0;
1111 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1112 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1113 if (!Eq_type) return 0;
1114 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1115 if (!Eq_singleton) return 0;
1116 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1117 if (!NotEq_type) return 0;
1118 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1119 if (!NotEq_singleton) return 0;
1120 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1121 if (!Lt_type) return 0;
1122 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1123 if (!Lt_singleton) return 0;
1124 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1125 if (!LtE_type) return 0;
1126 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1127 if (!LtE_singleton) return 0;
1128 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1129 if (!Gt_type) return 0;
1130 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1131 if (!Gt_singleton) return 0;
1132 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1133 if (!GtE_type) return 0;
1134 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1135 if (!GtE_singleton) return 0;
1136 Is_type = make_type("Is", cmpop_type, NULL, 0);
1137 if (!Is_type) return 0;
1138 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1139 if (!Is_singleton) return 0;
1140 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1141 if (!IsNot_type) return 0;
1142 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1143 if (!IsNot_singleton) return 0;
1144 In_type = make_type("In", cmpop_type, NULL, 0);
1145 if (!In_type) return 0;
1146 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1147 if (!In_singleton) return 0;
1148 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1149 if (!NotIn_type) return 0;
1150 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1151 if (!NotIn_singleton) return 0;
1152 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001153 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001155 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001156 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1157 if (!excepthandler_type) return 0;
1158 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1159 return 0;
1160 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1161 ExceptHandler_fields, 3);
1162 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001163 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001164 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001165 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001166 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1167 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001168 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001169 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1170 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001171 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001172 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1173 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001174 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1176 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001177 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001178 initialized = 1;
1179 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001180}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001181
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001182static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1183static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1184static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1185static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1186 arena);
1187static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1188static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1189static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1190static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1191static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1192static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1193 arena);
1194static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1195 arena);
1196static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1197static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1198static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1199static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001200static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001205 mod_ty p;
1206 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1207 if (!p)
1208 return NULL;
1209 p->kind = Module_kind;
1210 p->v.Module.body = body;
1211 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212}
1213
1214mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001217 mod_ty p;
1218 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1219 if (!p)
1220 return NULL;
1221 p->kind = Interactive_kind;
1222 p->v.Interactive.body = body;
1223 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224}
1225
1226mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001229 mod_ty p;
1230 if (!body) {
1231 PyErr_SetString(PyExc_ValueError,
1232 "field body is required for Expression");
1233 return NULL;
1234 }
1235 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1236 if (!p)
1237 return NULL;
1238 p->kind = Expression_kind;
1239 p->v.Expression.body = body;
1240 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
1243mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001246 mod_ty p;
1247 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1248 if (!p)
1249 return NULL;
1250 p->kind = Suite_kind;
1251 p->v.Suite.body = body;
1252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255stmt_ty
1256FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001257 decorator_list, expr_ty returns, int lineno, int col_offset,
1258 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001260 stmt_ty p;
1261 if (!name) {
1262 PyErr_SetString(PyExc_ValueError,
1263 "field name is required for FunctionDef");
1264 return NULL;
1265 }
1266 if (!args) {
1267 PyErr_SetString(PyExc_ValueError,
1268 "field args is required for FunctionDef");
1269 return NULL;
1270 }
1271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1272 if (!p)
1273 return NULL;
1274 p->kind = FunctionDef_kind;
1275 p->v.FunctionDef.name = name;
1276 p->v.FunctionDef.args = args;
1277 p->v.FunctionDef.body = body;
1278 p->v.FunctionDef.decorator_list = decorator_list;
1279 p->v.FunctionDef.returns = returns;
1280 p->lineno = lineno;
1281 p->col_offset = col_offset;
1282 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283}
1284
1285stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001286AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1287 * decorator_list, expr_ty returns, int lineno, int col_offset,
1288 PyArena *arena)
1289{
1290 stmt_ty p;
1291 if (!name) {
1292 PyErr_SetString(PyExc_ValueError,
1293 "field name is required for AsyncFunctionDef");
1294 return NULL;
1295 }
1296 if (!args) {
1297 PyErr_SetString(PyExc_ValueError,
1298 "field args is required for AsyncFunctionDef");
1299 return NULL;
1300 }
1301 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1302 if (!p)
1303 return NULL;
1304 p->kind = AsyncFunctionDef_kind;
1305 p->v.AsyncFunctionDef.name = name;
1306 p->v.AsyncFunctionDef.args = args;
1307 p->v.AsyncFunctionDef.body = body;
1308 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1309 p->v.AsyncFunctionDef.returns = returns;
1310 p->lineno = lineno;
1311 p->col_offset = col_offset;
1312 return p;
1313}
1314
1315stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001316ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1317 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1318 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001320 stmt_ty p;
1321 if (!name) {
1322 PyErr_SetString(PyExc_ValueError,
1323 "field name is required for ClassDef");
1324 return NULL;
1325 }
1326 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1327 if (!p)
1328 return NULL;
1329 p->kind = ClassDef_kind;
1330 p->v.ClassDef.name = name;
1331 p->v.ClassDef.bases = bases;
1332 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001333 p->v.ClassDef.body = body;
1334 p->v.ClassDef.decorator_list = decorator_list;
1335 p->lineno = lineno;
1336 p->col_offset = col_offset;
1337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001343 stmt_ty p;
1344 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1345 if (!p)
1346 return NULL;
1347 p->kind = Return_kind;
1348 p->v.Return.value = value;
1349 p->lineno = lineno;
1350 p->col_offset = col_offset;
1351 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352}
1353
1354stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001355Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001357 stmt_ty p;
1358 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1359 if (!p)
1360 return NULL;
1361 p->kind = Delete_kind;
1362 p->v.Delete.targets = targets;
1363 p->lineno = lineno;
1364 p->col_offset = col_offset;
1365 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366}
1367
1368stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001369Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1370 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001372 stmt_ty p;
1373 if (!value) {
1374 PyErr_SetString(PyExc_ValueError,
1375 "field value is required for Assign");
1376 return NULL;
1377 }
1378 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1379 if (!p)
1380 return NULL;
1381 p->kind = Assign_kind;
1382 p->v.Assign.targets = targets;
1383 p->v.Assign.value = value;
1384 p->lineno = lineno;
1385 p->col_offset = col_offset;
1386 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387}
1388
1389stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001390AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1391 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001393 stmt_ty p;
1394 if (!target) {
1395 PyErr_SetString(PyExc_ValueError,
1396 "field target is required for AugAssign");
1397 return NULL;
1398 }
1399 if (!op) {
1400 PyErr_SetString(PyExc_ValueError,
1401 "field op is required for AugAssign");
1402 return NULL;
1403 }
1404 if (!value) {
1405 PyErr_SetString(PyExc_ValueError,
1406 "field value is required for AugAssign");
1407 return NULL;
1408 }
1409 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1410 if (!p)
1411 return NULL;
1412 p->kind = AugAssign_kind;
1413 p->v.AugAssign.target = target;
1414 p->v.AugAssign.op = op;
1415 p->v.AugAssign.value = value;
1416 p->lineno = lineno;
1417 p->col_offset = col_offset;
1418 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419}
1420
1421stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001422AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1423 lineno, int col_offset, PyArena *arena)
1424{
1425 stmt_ty p;
1426 if (!target) {
1427 PyErr_SetString(PyExc_ValueError,
1428 "field target is required for AnnAssign");
1429 return NULL;
1430 }
1431 if (!annotation) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "field annotation is required for AnnAssign");
1434 return NULL;
1435 }
1436 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1437 if (!p)
1438 return NULL;
1439 p->kind = AnnAssign_kind;
1440 p->v.AnnAssign.target = target;
1441 p->v.AnnAssign.annotation = annotation;
1442 p->v.AnnAssign.value = value;
1443 p->v.AnnAssign.simple = simple;
1444 p->lineno = lineno;
1445 p->col_offset = col_offset;
1446 return p;
1447}
1448
1449stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001451 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001453 stmt_ty p;
1454 if (!target) {
1455 PyErr_SetString(PyExc_ValueError,
1456 "field target is required for For");
1457 return NULL;
1458 }
1459 if (!iter) {
1460 PyErr_SetString(PyExc_ValueError,
1461 "field iter is required for For");
1462 return NULL;
1463 }
1464 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1465 if (!p)
1466 return NULL;
1467 p->kind = For_kind;
1468 p->v.For.target = target;
1469 p->v.For.iter = iter;
1470 p->v.For.body = body;
1471 p->v.For.orelse = orelse;
1472 p->lineno = lineno;
1473 p->col_offset = col_offset;
1474 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
1477stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001478AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1479 lineno, int col_offset, PyArena *arena)
1480{
1481 stmt_ty p;
1482 if (!target) {
1483 PyErr_SetString(PyExc_ValueError,
1484 "field target is required for AsyncFor");
1485 return NULL;
1486 }
1487 if (!iter) {
1488 PyErr_SetString(PyExc_ValueError,
1489 "field iter is required for AsyncFor");
1490 return NULL;
1491 }
1492 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1493 if (!p)
1494 return NULL;
1495 p->kind = AsyncFor_kind;
1496 p->v.AsyncFor.target = target;
1497 p->v.AsyncFor.iter = iter;
1498 p->v.AsyncFor.body = body;
1499 p->v.AsyncFor.orelse = orelse;
1500 p->lineno = lineno;
1501 p->col_offset = col_offset;
1502 return p;
1503}
1504
1505stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1507 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001509 stmt_ty p;
1510 if (!test) {
1511 PyErr_SetString(PyExc_ValueError,
1512 "field test is required for While");
1513 return NULL;
1514 }
1515 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1516 if (!p)
1517 return NULL;
1518 p->kind = While_kind;
1519 p->v.While.test = test;
1520 p->v.While.body = body;
1521 p->v.While.orelse = orelse;
1522 p->lineno = lineno;
1523 p->col_offset = col_offset;
1524 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001528If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1529 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001531 stmt_ty p;
1532 if (!test) {
1533 PyErr_SetString(PyExc_ValueError,
1534 "field test is required for If");
1535 return NULL;
1536 }
1537 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1538 if (!p)
1539 return NULL;
1540 p->kind = If_kind;
1541 p->v.If.test = test;
1542 p->v.If.body = body;
1543 p->v.If.orelse = orelse;
1544 p->lineno = lineno;
1545 p->col_offset = col_offset;
1546 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547}
1548
1549stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001550With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1551 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001552{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001553 stmt_ty p;
1554 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1555 if (!p)
1556 return NULL;
1557 p->kind = With_kind;
1558 p->v.With.items = items;
1559 p->v.With.body = body;
1560 p->lineno = lineno;
1561 p->col_offset = col_offset;
1562 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001563}
1564
1565stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001566AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1567 PyArena *arena)
1568{
1569 stmt_ty p;
1570 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1571 if (!p)
1572 return NULL;
1573 p->kind = AsyncWith_kind;
1574 p->v.AsyncWith.items = items;
1575 p->v.AsyncWith.body = body;
1576 p->lineno = lineno;
1577 p->col_offset = col_offset;
1578 return p;
1579}
1580
1581stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001582Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001584 stmt_ty p;
1585 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1586 if (!p)
1587 return NULL;
1588 p->kind = Raise_kind;
1589 p->v.Raise.exc = exc;
1590 p->v.Raise.cause = cause;
1591 p->lineno = lineno;
1592 p->col_offset = col_offset;
1593 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001597Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1598 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001600 stmt_ty p;
1601 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1602 if (!p)
1603 return NULL;
1604 p->kind = Try_kind;
1605 p->v.Try.body = body;
1606 p->v.Try.handlers = handlers;
1607 p->v.Try.orelse = orelse;
1608 p->v.Try.finalbody = finalbody;
1609 p->lineno = lineno;
1610 p->col_offset = col_offset;
1611 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612}
1613
1614stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001615Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001617 stmt_ty p;
1618 if (!test) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "field test is required for Assert");
1621 return NULL;
1622 }
1623 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1624 if (!p)
1625 return NULL;
1626 p->kind = Assert_kind;
1627 p->v.Assert.test = test;
1628 p->v.Assert.msg = msg;
1629 p->lineno = lineno;
1630 p->col_offset = col_offset;
1631 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632}
1633
1634stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001637 stmt_ty p;
1638 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = Import_kind;
1642 p->v.Import.names = names;
1643 p->lineno = lineno;
1644 p->col_offset = col_offset;
1645 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1650 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001652 stmt_ty p;
1653 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1654 if (!p)
1655 return NULL;
1656 p->kind = ImportFrom_kind;
1657 p->v.ImportFrom.module = module;
1658 p->v.ImportFrom.names = names;
1659 p->v.ImportFrom.level = level;
1660 p->lineno = lineno;
1661 p->col_offset = col_offset;
1662 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
1665stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001668 stmt_ty p;
1669 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1670 if (!p)
1671 return NULL;
1672 p->kind = Global_kind;
1673 p->v.Global.names = names;
1674 p->lineno = lineno;
1675 p->col_offset = col_offset;
1676 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
1679stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001680Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1681{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001682 stmt_ty p;
1683 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1684 if (!p)
1685 return NULL;
1686 p->kind = Nonlocal_kind;
1687 p->v.Nonlocal.names = names;
1688 p->lineno = lineno;
1689 p->col_offset = col_offset;
1690 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001691}
1692
1693stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001696 stmt_ty p;
1697 if (!value) {
1698 PyErr_SetString(PyExc_ValueError,
1699 "field value is required for Expr");
1700 return NULL;
1701 }
1702 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1703 if (!p)
1704 return NULL;
1705 p->kind = Expr_kind;
1706 p->v.Expr.value = value;
1707 p->lineno = lineno;
1708 p->col_offset = col_offset;
1709 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710}
1711
1712stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001715 stmt_ty p;
1716 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1717 if (!p)
1718 return NULL;
1719 p->kind = Pass_kind;
1720 p->lineno = lineno;
1721 p->col_offset = col_offset;
1722 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723}
1724
1725stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001726Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001728 stmt_ty p;
1729 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1730 if (!p)
1731 return NULL;
1732 p->kind = Break_kind;
1733 p->lineno = lineno;
1734 p->col_offset = col_offset;
1735 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736}
1737
1738stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001739Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001741 stmt_ty p;
1742 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1743 if (!p)
1744 return NULL;
1745 p->kind = Continue_kind;
1746 p->lineno = lineno;
1747 p->col_offset = col_offset;
1748 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1753 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001755 expr_ty p;
1756 if (!op) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field op is required for BoolOp");
1759 return NULL;
1760 }
1761 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1762 if (!p)
1763 return NULL;
1764 p->kind = BoolOp_kind;
1765 p->v.BoolOp.op = op;
1766 p->v.BoolOp.values = values;
1767 p->lineno = lineno;
1768 p->col_offset = col_offset;
1769 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770}
1771
1772expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1774 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001776 expr_ty p;
1777 if (!left) {
1778 PyErr_SetString(PyExc_ValueError,
1779 "field left is required for BinOp");
1780 return NULL;
1781 }
1782 if (!op) {
1783 PyErr_SetString(PyExc_ValueError,
1784 "field op is required for BinOp");
1785 return NULL;
1786 }
1787 if (!right) {
1788 PyErr_SetString(PyExc_ValueError,
1789 "field right is required for BinOp");
1790 return NULL;
1791 }
1792 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1793 if (!p)
1794 return NULL;
1795 p->kind = BinOp_kind;
1796 p->v.BinOp.left = left;
1797 p->v.BinOp.op = op;
1798 p->v.BinOp.right = right;
1799 p->lineno = lineno;
1800 p->col_offset = col_offset;
1801 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
1804expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001805UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1806 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001808 expr_ty p;
1809 if (!op) {
1810 PyErr_SetString(PyExc_ValueError,
1811 "field op is required for UnaryOp");
1812 return NULL;
1813 }
1814 if (!operand) {
1815 PyErr_SetString(PyExc_ValueError,
1816 "field operand is required for UnaryOp");
1817 return NULL;
1818 }
1819 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1820 if (!p)
1821 return NULL;
1822 p->kind = UnaryOp_kind;
1823 p->v.UnaryOp.op = op;
1824 p->v.UnaryOp.operand = operand;
1825 p->lineno = lineno;
1826 p->col_offset = col_offset;
1827 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828}
1829
1830expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001831Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1832 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001834 expr_ty p;
1835 if (!args) {
1836 PyErr_SetString(PyExc_ValueError,
1837 "field args is required for Lambda");
1838 return NULL;
1839 }
1840 if (!body) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field body is required for Lambda");
1843 return NULL;
1844 }
1845 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1846 if (!p)
1847 return NULL;
1848 p->kind = Lambda_kind;
1849 p->v.Lambda.args = args;
1850 p->v.Lambda.body = body;
1851 p->lineno = lineno;
1852 p->col_offset = col_offset;
1853 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854}
1855
1856expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001857IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1858 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001859{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001860 expr_ty p;
1861 if (!test) {
1862 PyErr_SetString(PyExc_ValueError,
1863 "field test is required for IfExp");
1864 return NULL;
1865 }
1866 if (!body) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "field body is required for IfExp");
1869 return NULL;
1870 }
1871 if (!orelse) {
1872 PyErr_SetString(PyExc_ValueError,
1873 "field orelse is required for IfExp");
1874 return NULL;
1875 }
1876 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1877 if (!p)
1878 return NULL;
1879 p->kind = IfExp_kind;
1880 p->v.IfExp.test = test;
1881 p->v.IfExp.body = body;
1882 p->v.IfExp.orelse = orelse;
1883 p->lineno = lineno;
1884 p->col_offset = col_offset;
1885 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001886}
1887
1888expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001889Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1890 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001892 expr_ty p;
1893 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1894 if (!p)
1895 return NULL;
1896 p->kind = Dict_kind;
1897 p->v.Dict.keys = keys;
1898 p->v.Dict.values = values;
1899 p->lineno = lineno;
1900 p->col_offset = col_offset;
1901 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902}
1903
1904expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001905Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1906{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001907 expr_ty p;
1908 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1909 if (!p)
1910 return NULL;
1911 p->kind = Set_kind;
1912 p->v.Set.elts = elts;
1913 p->lineno = lineno;
1914 p->col_offset = col_offset;
1915 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001916}
1917
1918expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001919ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1920 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001922 expr_ty p;
1923 if (!elt) {
1924 PyErr_SetString(PyExc_ValueError,
1925 "field elt is required for ListComp");
1926 return NULL;
1927 }
1928 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1929 if (!p)
1930 return NULL;
1931 p->kind = ListComp_kind;
1932 p->v.ListComp.elt = elt;
1933 p->v.ListComp.generators = generators;
1934 p->lineno = lineno;
1935 p->col_offset = col_offset;
1936 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937}
1938
1939expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1941 *arena)
1942{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001943 expr_ty p;
1944 if (!elt) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field elt is required for SetComp");
1947 return NULL;
1948 }
1949 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1950 if (!p)
1951 return NULL;
1952 p->kind = SetComp_kind;
1953 p->v.SetComp.elt = elt;
1954 p->v.SetComp.generators = generators;
1955 p->lineno = lineno;
1956 p->col_offset = col_offset;
1957 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001958}
1959
1960expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1962 col_offset, PyArena *arena)
1963{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001964 expr_ty p;
1965 if (!key) {
1966 PyErr_SetString(PyExc_ValueError,
1967 "field key is required for DictComp");
1968 return NULL;
1969 }
1970 if (!value) {
1971 PyErr_SetString(PyExc_ValueError,
1972 "field value is required for DictComp");
1973 return NULL;
1974 }
1975 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1976 if (!p)
1977 return NULL;
1978 p->kind = DictComp_kind;
1979 p->v.DictComp.key = key;
1980 p->v.DictComp.value = value;
1981 p->v.DictComp.generators = generators;
1982 p->lineno = lineno;
1983 p->col_offset = col_offset;
1984 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001985}
1986
1987expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001988GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1989 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001991 expr_ty p;
1992 if (!elt) {
1993 PyErr_SetString(PyExc_ValueError,
1994 "field elt is required for GeneratorExp");
1995 return NULL;
1996 }
1997 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1998 if (!p)
1999 return NULL;
2000 p->kind = GeneratorExp_kind;
2001 p->v.GeneratorExp.elt = elt;
2002 p->v.GeneratorExp.generators = generators;
2003 p->lineno = lineno;
2004 p->col_offset = col_offset;
2005 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006}
2007
2008expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002009Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2010{
2011 expr_ty p;
2012 if (!value) {
2013 PyErr_SetString(PyExc_ValueError,
2014 "field value is required for Await");
2015 return NULL;
2016 }
2017 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2018 if (!p)
2019 return NULL;
2020 p->kind = Await_kind;
2021 p->v.Await.value = value;
2022 p->lineno = lineno;
2023 p->col_offset = col_offset;
2024 return p;
2025}
2026
2027expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002028Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002030 expr_ty p;
2031 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2032 if (!p)
2033 return NULL;
2034 p->kind = Yield_kind;
2035 p->v.Yield.value = value;
2036 p->lineno = lineno;
2037 p->col_offset = col_offset;
2038 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
2041expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002042YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2043{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002044 expr_ty p;
2045 if (!value) {
2046 PyErr_SetString(PyExc_ValueError,
2047 "field value is required for YieldFrom");
2048 return NULL;
2049 }
2050 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2051 if (!p)
2052 return NULL;
2053 p->kind = YieldFrom_kind;
2054 p->v.YieldFrom.value = value;
2055 p->lineno = lineno;
2056 p->col_offset = col_offset;
2057 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002058}
2059
2060expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002061Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2062 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002064 expr_ty p;
2065 if (!left) {
2066 PyErr_SetString(PyExc_ValueError,
2067 "field left is required for Compare");
2068 return NULL;
2069 }
2070 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2071 if (!p)
2072 return NULL;
2073 p->kind = Compare_kind;
2074 p->v.Compare.left = left;
2075 p->v.Compare.ops = ops;
2076 p->v.Compare.comparators = comparators;
2077 p->lineno = lineno;
2078 p->col_offset = col_offset;
2079 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002083Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2084 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002086 expr_ty p;
2087 if (!func) {
2088 PyErr_SetString(PyExc_ValueError,
2089 "field func is required for Call");
2090 return NULL;
2091 }
2092 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2093 if (!p)
2094 return NULL;
2095 p->kind = Call_kind;
2096 p->v.Call.func = func;
2097 p->v.Call.args = args;
2098 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002099 p->lineno = lineno;
2100 p->col_offset = col_offset;
2101 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102}
2103
2104expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002105Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002107 expr_ty p;
2108 if (!n) {
2109 PyErr_SetString(PyExc_ValueError,
2110 "field n is required for Num");
2111 return NULL;
2112 }
2113 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2114 if (!p)
2115 return NULL;
2116 p->kind = Num_kind;
2117 p->v.Num.n = n;
2118 p->lineno = lineno;
2119 p->col_offset = col_offset;
2120 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121}
2122
2123expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002124Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002126 expr_ty p;
2127 if (!s) {
2128 PyErr_SetString(PyExc_ValueError,
2129 "field s is required for Str");
2130 return NULL;
2131 }
2132 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2133 if (!p)
2134 return NULL;
2135 p->kind = Str_kind;
2136 p->v.Str.s = s;
2137 p->lineno = lineno;
2138 p->col_offset = col_offset;
2139 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140}
2141
2142expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002143FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2144 int col_offset, PyArena *arena)
2145{
2146 expr_ty p;
2147 if (!value) {
2148 PyErr_SetString(PyExc_ValueError,
2149 "field value is required for FormattedValue");
2150 return NULL;
2151 }
2152 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2153 if (!p)
2154 return NULL;
2155 p->kind = FormattedValue_kind;
2156 p->v.FormattedValue.value = value;
2157 p->v.FormattedValue.conversion = conversion;
2158 p->v.FormattedValue.format_spec = format_spec;
2159 p->lineno = lineno;
2160 p->col_offset = col_offset;
2161 return p;
2162}
2163
2164expr_ty
2165JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2166{
2167 expr_ty p;
2168 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2169 if (!p)
2170 return NULL;
2171 p->kind = JoinedStr_kind;
2172 p->v.JoinedStr.values = values;
2173 p->lineno = lineno;
2174 p->col_offset = col_offset;
2175 return p;
2176}
2177
2178expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002179Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002180{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002181 expr_ty p;
2182 if (!s) {
2183 PyErr_SetString(PyExc_ValueError,
2184 "field s is required for Bytes");
2185 return NULL;
2186 }
2187 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2188 if (!p)
2189 return NULL;
2190 p->kind = Bytes_kind;
2191 p->v.Bytes.s = s;
2192 p->lineno = lineno;
2193 p->col_offset = col_offset;
2194 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002195}
2196
2197expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002198NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2199{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002200 expr_ty p;
2201 if (!value) {
2202 PyErr_SetString(PyExc_ValueError,
2203 "field value is required for NameConstant");
2204 return NULL;
2205 }
2206 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2207 if (!p)
2208 return NULL;
2209 p->kind = NameConstant_kind;
2210 p->v.NameConstant.value = value;
2211 p->lineno = lineno;
2212 p->col_offset = col_offset;
2213 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002214}
2215
2216expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002217Ellipsis(int lineno, int col_offset, PyArena *arena)
2218{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002219 expr_ty p;
2220 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2221 if (!p)
2222 return NULL;
2223 p->kind = Ellipsis_kind;
2224 p->lineno = lineno;
2225 p->col_offset = col_offset;
2226 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002227}
2228
2229expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002230Constant(constant value, int lineno, int col_offset, PyArena *arena)
2231{
2232 expr_ty p;
2233 if (!value) {
2234 PyErr_SetString(PyExc_ValueError,
2235 "field value is required for Constant");
2236 return NULL;
2237 }
2238 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2239 if (!p)
2240 return NULL;
2241 p->kind = Constant_kind;
2242 p->v.Constant.value = value;
2243 p->lineno = lineno;
2244 p->col_offset = col_offset;
2245 return p;
2246}
2247
2248expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002249Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2250 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002252 expr_ty p;
2253 if (!value) {
2254 PyErr_SetString(PyExc_ValueError,
2255 "field value is required for Attribute");
2256 return NULL;
2257 }
2258 if (!attr) {
2259 PyErr_SetString(PyExc_ValueError,
2260 "field attr is required for Attribute");
2261 return NULL;
2262 }
2263 if (!ctx) {
2264 PyErr_SetString(PyExc_ValueError,
2265 "field ctx is required for Attribute");
2266 return NULL;
2267 }
2268 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2269 if (!p)
2270 return NULL;
2271 p->kind = Attribute_kind;
2272 p->v.Attribute.value = value;
2273 p->v.Attribute.attr = attr;
2274 p->v.Attribute.ctx = ctx;
2275 p->lineno = lineno;
2276 p->col_offset = col_offset;
2277 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278}
2279
2280expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002281Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2282 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002284 expr_ty p;
2285 if (!value) {
2286 PyErr_SetString(PyExc_ValueError,
2287 "field value is required for Subscript");
2288 return NULL;
2289 }
2290 if (!slice) {
2291 PyErr_SetString(PyExc_ValueError,
2292 "field slice is required for Subscript");
2293 return NULL;
2294 }
2295 if (!ctx) {
2296 PyErr_SetString(PyExc_ValueError,
2297 "field ctx is required for Subscript");
2298 return NULL;
2299 }
2300 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2301 if (!p)
2302 return NULL;
2303 p->kind = Subscript_kind;
2304 p->v.Subscript.value = value;
2305 p->v.Subscript.slice = slice;
2306 p->v.Subscript.ctx = ctx;
2307 p->lineno = lineno;
2308 p->col_offset = col_offset;
2309 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310}
2311
2312expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002313Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2314 *arena)
2315{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002316 expr_ty p;
2317 if (!value) {
2318 PyErr_SetString(PyExc_ValueError,
2319 "field value is required for Starred");
2320 return NULL;
2321 }
2322 if (!ctx) {
2323 PyErr_SetString(PyExc_ValueError,
2324 "field ctx is required for Starred");
2325 return NULL;
2326 }
2327 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2328 if (!p)
2329 return NULL;
2330 p->kind = Starred_kind;
2331 p->v.Starred.value = value;
2332 p->v.Starred.ctx = ctx;
2333 p->lineno = lineno;
2334 p->col_offset = col_offset;
2335 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002336}
2337
2338expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002339Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2340 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002342 expr_ty p;
2343 if (!id) {
2344 PyErr_SetString(PyExc_ValueError,
2345 "field id is required for Name");
2346 return NULL;
2347 }
2348 if (!ctx) {
2349 PyErr_SetString(PyExc_ValueError,
2350 "field ctx is required for Name");
2351 return NULL;
2352 }
2353 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2354 if (!p)
2355 return NULL;
2356 p->kind = Name_kind;
2357 p->v.Name.id = id;
2358 p->v.Name.ctx = ctx;
2359 p->lineno = lineno;
2360 p->col_offset = col_offset;
2361 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362}
2363
2364expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002365List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2366 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002368 expr_ty p;
2369 if (!ctx) {
2370 PyErr_SetString(PyExc_ValueError,
2371 "field ctx is required for List");
2372 return NULL;
2373 }
2374 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2375 if (!p)
2376 return NULL;
2377 p->kind = List_kind;
2378 p->v.List.elts = elts;
2379 p->v.List.ctx = ctx;
2380 p->lineno = lineno;
2381 p->col_offset = col_offset;
2382 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383}
2384
2385expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002386Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2387 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002389 expr_ty p;
2390 if (!ctx) {
2391 PyErr_SetString(PyExc_ValueError,
2392 "field ctx is required for Tuple");
2393 return NULL;
2394 }
2395 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2396 if (!p)
2397 return NULL;
2398 p->kind = Tuple_kind;
2399 p->v.Tuple.elts = elts;
2400 p->v.Tuple.ctx = ctx;
2401 p->lineno = lineno;
2402 p->col_offset = col_offset;
2403 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404}
2405
2406slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002409 slice_ty p;
2410 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2411 if (!p)
2412 return NULL;
2413 p->kind = Slice_kind;
2414 p->v.Slice.lower = lower;
2415 p->v.Slice.upper = upper;
2416 p->v.Slice.step = step;
2417 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418}
2419
2420slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002421ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002423 slice_ty p;
2424 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2425 if (!p)
2426 return NULL;
2427 p->kind = ExtSlice_kind;
2428 p->v.ExtSlice.dims = dims;
2429 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430}
2431
2432slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002433Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002435 slice_ty p;
2436 if (!value) {
2437 PyErr_SetString(PyExc_ValueError,
2438 "field value is required for Index");
2439 return NULL;
2440 }
2441 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2442 if (!p)
2443 return NULL;
2444 p->kind = Index_kind;
2445 p->v.Index.value = value;
2446 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447}
2448
2449comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002450comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2451 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002453 comprehension_ty p;
2454 if (!target) {
2455 PyErr_SetString(PyExc_ValueError,
2456 "field target is required for comprehension");
2457 return NULL;
2458 }
2459 if (!iter) {
2460 PyErr_SetString(PyExc_ValueError,
2461 "field iter is required for comprehension");
2462 return NULL;
2463 }
2464 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2465 if (!p)
2466 return NULL;
2467 p->target = target;
2468 p->iter = iter;
2469 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002470 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002471 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472}
2473
2474excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002475ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002476 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002478 excepthandler_ty p;
2479 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2480 if (!p)
2481 return NULL;
2482 p->kind = ExceptHandler_kind;
2483 p->v.ExceptHandler.type = type;
2484 p->v.ExceptHandler.name = name;
2485 p->v.ExceptHandler.body = body;
2486 p->lineno = lineno;
2487 p->col_offset = col_offset;
2488 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489}
2490
2491arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002492arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2493 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002495 arguments_ty p;
2496 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2497 if (!p)
2498 return NULL;
2499 p->args = args;
2500 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002502 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002503 p->kwarg = kwarg;
2504 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002505 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506}
2507
Neal Norwitzc1505362006-12-28 06:47:50 +00002508arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002509arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2510 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002511{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002512 arg_ty p;
2513 if (!arg) {
2514 PyErr_SetString(PyExc_ValueError,
2515 "field arg is required for arg");
2516 return NULL;
2517 }
2518 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2519 if (!p)
2520 return NULL;
2521 p->arg = arg;
2522 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002523 p->lineno = lineno;
2524 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002525 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002526}
2527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002529keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002531 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002532 if (!value) {
2533 PyErr_SetString(PyExc_ValueError,
2534 "field value is required for keyword");
2535 return NULL;
2536 }
2537 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2538 if (!p)
2539 return NULL;
2540 p->arg = arg;
2541 p->value = value;
2542 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543}
2544
2545alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002546alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002548 alias_ty p;
2549 if (!name) {
2550 PyErr_SetString(PyExc_ValueError,
2551 "field name is required for alias");
2552 return NULL;
2553 }
2554 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2555 if (!p)
2556 return NULL;
2557 p->name = name;
2558 p->asname = asname;
2559 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560}
2561
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002562withitem_ty
2563withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2564{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002565 withitem_ty p;
2566 if (!context_expr) {
2567 PyErr_SetString(PyExc_ValueError,
2568 "field context_expr is required for withitem");
2569 return NULL;
2570 }
2571 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2572 if (!p)
2573 return NULL;
2574 p->context_expr = context_expr;
2575 p->optional_vars = optional_vars;
2576 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002577}
2578
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002579
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002580PyObject*
2581ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002582{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002583 mod_ty o = (mod_ty)_o;
2584 PyObject *result = NULL, *value = NULL;
2585 if (!o) {
2586 Py_INCREF(Py_None);
2587 return Py_None;
2588 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002589
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002590 switch (o->kind) {
2591 case Module_kind:
2592 result = PyType_GenericNew(Module_type, NULL, NULL);
2593 if (!result) goto failed;
2594 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 break;
2600 case Interactive_kind:
2601 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2602 if (!result) goto failed;
2603 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2604 if (!value) goto failed;
2605 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 break;
2609 case Expression_kind:
2610 result = PyType_GenericNew(Expression_type, NULL, NULL);
2611 if (!result) goto failed;
2612 value = ast2obj_expr(o->v.Expression.body);
2613 if (!value) goto failed;
2614 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
2617 break;
2618 case Suite_kind:
2619 result = PyType_GenericNew(Suite_type, NULL, NULL);
2620 if (!result) goto failed;
2621 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2622 if (!value) goto failed;
2623 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
2626 break;
2627 }
2628 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002629failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002630 Py_XDECREF(value);
2631 Py_XDECREF(result);
2632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633}
2634
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002635PyObject*
2636ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002638 stmt_ty o = (stmt_ty)_o;
2639 PyObject *result = NULL, *value = NULL;
2640 if (!o) {
2641 Py_INCREF(Py_None);
2642 return Py_None;
2643 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002644
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002645 switch (o->kind) {
2646 case FunctionDef_kind:
2647 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2648 if (!result) goto failed;
2649 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002650 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002651 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2652 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002653 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002654 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002655 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002656 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2657 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002658 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002659 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2660 if (!value) goto failed;
2661 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2665 if (!value) goto failed;
2666 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_expr(o->v.FunctionDef.returns);
2670 if (!value) goto failed;
2671 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002675 case AsyncFunctionDef_kind:
2676 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2679 if (!value) goto failed;
2680 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2684 if (!value) goto failed;
2685 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
2688 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2694 ast2obj_expr);
2695 if (!value) goto failed;
2696 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2700 if (!value) goto failed;
2701 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002705 case ClassDef_kind:
2706 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2707 if (!result) goto failed;
2708 value = ast2obj_identifier(o->v.ClassDef.name);
2709 if (!value) goto failed;
2710 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
2713 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2714 if (!value) goto failed;
2715 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2716 goto failed;
2717 Py_DECREF(value);
2718 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002723 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2724 if (!value) goto failed;
2725 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 break;
2734 case Return_kind:
2735 result = PyType_GenericNew(Return_type, NULL, NULL);
2736 if (!result) goto failed;
2737 value = ast2obj_expr(o->v.Return.value);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 break;
2743 case Delete_kind:
2744 result = PyType_GenericNew(Delete_type, NULL, NULL);
2745 if (!result) goto failed;
2746 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 break;
2752 case Assign_kind:
2753 result = PyType_GenericNew(Assign_type, NULL, NULL);
2754 if (!result) goto failed;
2755 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2756 if (!value) goto failed;
2757 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
2760 value = ast2obj_expr(o->v.Assign.value);
2761 if (!value) goto failed;
2762 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
2765 break;
2766 case AugAssign_kind:
2767 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2768 if (!result) goto failed;
2769 value = ast2obj_expr(o->v.AugAssign.target);
2770 if (!value) goto failed;
2771 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
2774 value = ast2obj_operator(o->v.AugAssign.op);
2775 if (!value) goto failed;
2776 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_expr(o->v.AugAssign.value);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002785 case AnnAssign_kind:
2786 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2787 if (!result) goto failed;
2788 value = ast2obj_expr(o->v.AnnAssign.target);
2789 if (!value) goto failed;
2790 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2791 goto failed;
2792 Py_DECREF(value);
2793 value = ast2obj_expr(o->v.AnnAssign.annotation);
2794 if (!value) goto failed;
2795 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 value = ast2obj_expr(o->v.AnnAssign.value);
2799 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 value = ast2obj_int(o->v.AnnAssign.simple);
2804 if (!value) goto failed;
2805 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002809 case For_kind:
2810 result = PyType_GenericNew(For_type, NULL, NULL);
2811 if (!result) goto failed;
2812 value = ast2obj_expr(o->v.For.target);
2813 if (!value) goto failed;
2814 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2815 goto failed;
2816 Py_DECREF(value);
2817 value = ast2obj_expr(o->v.For.iter);
2818 if (!value) goto failed;
2819 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
2822 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2823 if (!value) goto failed;
2824 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2825 goto failed;
2826 Py_DECREF(value);
2827 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2828 if (!value) goto failed;
2829 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2830 goto failed;
2831 Py_DECREF(value);
2832 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002833 case AsyncFor_kind:
2834 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2835 if (!result) goto failed;
2836 value = ast2obj_expr(o->v.AsyncFor.target);
2837 if (!value) goto failed;
2838 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2839 goto failed;
2840 Py_DECREF(value);
2841 value = ast2obj_expr(o->v.AsyncFor.iter);
2842 if (!value) goto failed;
2843 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2844 goto failed;
2845 Py_DECREF(value);
2846 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2847 if (!value) goto failed;
2848 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2849 goto failed;
2850 Py_DECREF(value);
2851 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2852 if (!value) goto failed;
2853 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002857 case While_kind:
2858 result = PyType_GenericNew(While_type, NULL, NULL);
2859 if (!result) goto failed;
2860 value = ast2obj_expr(o->v.While.test);
2861 if (!value) goto failed;
2862 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2866 if (!value) goto failed;
2867 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2868 goto failed;
2869 Py_DECREF(value);
2870 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2871 if (!value) goto failed;
2872 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 break;
2876 case If_kind:
2877 result = PyType_GenericNew(If_type, NULL, NULL);
2878 if (!result) goto failed;
2879 value = ast2obj_expr(o->v.If.test);
2880 if (!value) goto failed;
2881 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
2884 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 break;
2895 case With_kind:
2896 result = PyType_GenericNew(With_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2904 if (!value) goto failed;
2905 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002909 case AsyncWith_kind:
2910 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2913 if (!value) goto failed;
2914 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2918 if (!value) goto failed;
2919 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
2922 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002923 case Raise_kind:
2924 result = PyType_GenericNew(Raise_type, NULL, NULL);
2925 if (!result) goto failed;
2926 value = ast2obj_expr(o->v.Raise.exc);
2927 if (!value) goto failed;
2928 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2929 goto failed;
2930 Py_DECREF(value);
2931 value = ast2obj_expr(o->v.Raise.cause);
2932 if (!value) goto failed;
2933 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
2936 break;
2937 case Try_kind:
2938 result = PyType_GenericNew(Try_type, NULL, NULL);
2939 if (!result) goto failed;
2940 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2946 if (!value) goto failed;
2947 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2948 goto failed;
2949 Py_DECREF(value);
2950 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2951 if (!value) goto failed;
2952 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2953 goto failed;
2954 Py_DECREF(value);
2955 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2956 if (!value) goto failed;
2957 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2958 goto failed;
2959 Py_DECREF(value);
2960 break;
2961 case Assert_kind:
2962 result = PyType_GenericNew(Assert_type, NULL, NULL);
2963 if (!result) goto failed;
2964 value = ast2obj_expr(o->v.Assert.test);
2965 if (!value) goto failed;
2966 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 value = ast2obj_expr(o->v.Assert.msg);
2970 if (!value) goto failed;
2971 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
2974 break;
2975 case Import_kind:
2976 result = PyType_GenericNew(Import_type, NULL, NULL);
2977 if (!result) goto failed;
2978 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2979 if (!value) goto failed;
2980 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
2983 break;
2984 case ImportFrom_kind:
2985 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2986 if (!result) goto failed;
2987 value = ast2obj_identifier(o->v.ImportFrom.module);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2993 if (!value) goto failed;
2994 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
2997 value = ast2obj_int(o->v.ImportFrom.level);
2998 if (!value) goto failed;
2999 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3000 goto failed;
3001 Py_DECREF(value);
3002 break;
3003 case Global_kind:
3004 result = PyType_GenericNew(Global_type, NULL, NULL);
3005 if (!result) goto failed;
3006 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3007 if (!value) goto failed;
3008 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
3011 break;
3012 case Nonlocal_kind:
3013 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3014 if (!result) goto failed;
3015 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3016 if (!value) goto failed;
3017 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3018 goto failed;
3019 Py_DECREF(value);
3020 break;
3021 case Expr_kind:
3022 result = PyType_GenericNew(Expr_type, NULL, NULL);
3023 if (!result) goto failed;
3024 value = ast2obj_expr(o->v.Expr.value);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 break;
3030 case Pass_kind:
3031 result = PyType_GenericNew(Pass_type, NULL, NULL);
3032 if (!result) goto failed;
3033 break;
3034 case Break_kind:
3035 result = PyType_GenericNew(Break_type, NULL, NULL);
3036 if (!result) goto failed;
3037 break;
3038 case Continue_kind:
3039 result = PyType_GenericNew(Continue_type, NULL, NULL);
3040 if (!result) goto failed;
3041 break;
3042 }
3043 value = ast2obj_int(o->lineno);
3044 if (!value) goto failed;
3045 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3046 goto failed;
3047 Py_DECREF(value);
3048 value = ast2obj_int(o->col_offset);
3049 if (!value) goto failed;
3050 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3051 goto failed;
3052 Py_DECREF(value);
3053 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003054failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003055 Py_XDECREF(value);
3056 Py_XDECREF(result);
3057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058}
3059
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003060PyObject*
3061ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003063 expr_ty o = (expr_ty)_o;
3064 PyObject *result = NULL, *value = NULL;
3065 if (!o) {
3066 Py_INCREF(Py_None);
3067 return Py_None;
3068 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003069
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003070 switch (o->kind) {
3071 case BoolOp_kind:
3072 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3073 if (!result) goto failed;
3074 value = ast2obj_boolop(o->v.BoolOp.op);
3075 if (!value) goto failed;
3076 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3077 goto failed;
3078 Py_DECREF(value);
3079 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3080 if (!value) goto failed;
3081 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3082 goto failed;
3083 Py_DECREF(value);
3084 break;
3085 case BinOp_kind:
3086 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3087 if (!result) goto failed;
3088 value = ast2obj_expr(o->v.BinOp.left);
3089 if (!value) goto failed;
3090 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_operator(o->v.BinOp.op);
3094 if (!value) goto failed;
3095 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3096 goto failed;
3097 Py_DECREF(value);
3098 value = ast2obj_expr(o->v.BinOp.right);
3099 if (!value) goto failed;
3100 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3101 goto failed;
3102 Py_DECREF(value);
3103 break;
3104 case UnaryOp_kind:
3105 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3106 if (!result) goto failed;
3107 value = ast2obj_unaryop(o->v.UnaryOp.op);
3108 if (!value) goto failed;
3109 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3110 goto failed;
3111 Py_DECREF(value);
3112 value = ast2obj_expr(o->v.UnaryOp.operand);
3113 if (!value) goto failed;
3114 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3115 goto failed;
3116 Py_DECREF(value);
3117 break;
3118 case Lambda_kind:
3119 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3120 if (!result) goto failed;
3121 value = ast2obj_arguments(o->v.Lambda.args);
3122 if (!value) goto failed;
3123 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3124 goto failed;
3125 Py_DECREF(value);
3126 value = ast2obj_expr(o->v.Lambda.body);
3127 if (!value) goto failed;
3128 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3129 goto failed;
3130 Py_DECREF(value);
3131 break;
3132 case IfExp_kind:
3133 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3134 if (!result) goto failed;
3135 value = ast2obj_expr(o->v.IfExp.test);
3136 if (!value) goto failed;
3137 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3138 goto failed;
3139 Py_DECREF(value);
3140 value = ast2obj_expr(o->v.IfExp.body);
3141 if (!value) goto failed;
3142 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3143 goto failed;
3144 Py_DECREF(value);
3145 value = ast2obj_expr(o->v.IfExp.orelse);
3146 if (!value) goto failed;
3147 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3148 goto failed;
3149 Py_DECREF(value);
3150 break;
3151 case Dict_kind:
3152 result = PyType_GenericNew(Dict_type, NULL, NULL);
3153 if (!result) goto failed;
3154 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3155 if (!value) goto failed;
3156 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3157 goto failed;
3158 Py_DECREF(value);
3159 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3160 if (!value) goto failed;
3161 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3162 goto failed;
3163 Py_DECREF(value);
3164 break;
3165 case Set_kind:
3166 result = PyType_GenericNew(Set_type, NULL, NULL);
3167 if (!result) goto failed;
3168 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3169 if (!value) goto failed;
3170 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3171 goto failed;
3172 Py_DECREF(value);
3173 break;
3174 case ListComp_kind:
3175 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3176 if (!result) goto failed;
3177 value = ast2obj_expr(o->v.ListComp.elt);
3178 if (!value) goto failed;
3179 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3180 goto failed;
3181 Py_DECREF(value);
3182 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3183 if (!value) goto failed;
3184 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3185 goto failed;
3186 Py_DECREF(value);
3187 break;
3188 case SetComp_kind:
3189 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3190 if (!result) goto failed;
3191 value = ast2obj_expr(o->v.SetComp.elt);
3192 if (!value) goto failed;
3193 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3194 goto failed;
3195 Py_DECREF(value);
3196 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3197 if (!value) goto failed;
3198 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3199 goto failed;
3200 Py_DECREF(value);
3201 break;
3202 case DictComp_kind:
3203 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3204 if (!result) goto failed;
3205 value = ast2obj_expr(o->v.DictComp.key);
3206 if (!value) goto failed;
3207 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3208 goto failed;
3209 Py_DECREF(value);
3210 value = ast2obj_expr(o->v.DictComp.value);
3211 if (!value) goto failed;
3212 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3213 goto failed;
3214 Py_DECREF(value);
3215 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3216 if (!value) goto failed;
3217 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3218 goto failed;
3219 Py_DECREF(value);
3220 break;
3221 case GeneratorExp_kind:
3222 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3223 if (!result) goto failed;
3224 value = ast2obj_expr(o->v.GeneratorExp.elt);
3225 if (!value) goto failed;
3226 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3227 goto failed;
3228 Py_DECREF(value);
3229 value = ast2obj_list(o->v.GeneratorExp.generators,
3230 ast2obj_comprehension);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003236 case Await_kind:
3237 result = PyType_GenericNew(Await_type, NULL, NULL);
3238 if (!result) goto failed;
3239 value = ast2obj_expr(o->v.Await.value);
3240 if (!value) goto failed;
3241 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3242 goto failed;
3243 Py_DECREF(value);
3244 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003245 case Yield_kind:
3246 result = PyType_GenericNew(Yield_type, NULL, NULL);
3247 if (!result) goto failed;
3248 value = ast2obj_expr(o->v.Yield.value);
3249 if (!value) goto failed;
3250 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
3253 break;
3254 case YieldFrom_kind:
3255 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3256 if (!result) goto failed;
3257 value = ast2obj_expr(o->v.YieldFrom.value);
3258 if (!value) goto failed;
3259 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3260 goto failed;
3261 Py_DECREF(value);
3262 break;
3263 case Compare_kind:
3264 result = PyType_GenericNew(Compare_type, NULL, NULL);
3265 if (!result) goto failed;
3266 value = ast2obj_expr(o->v.Compare.left);
3267 if (!value) goto failed;
3268 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3269 goto failed;
3270 Py_DECREF(value);
3271 {
3272 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3273 value = PyList_New(n);
3274 if (!value) goto failed;
3275 for(i = 0; i < n; i++)
3276 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003278 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003279 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3280 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003281 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003282 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003283 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003284 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3285 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003286 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003287 break;
3288 case Call_kind:
3289 result = PyType_GenericNew(Call_type, NULL, NULL);
3290 if (!result) goto failed;
3291 value = ast2obj_expr(o->v.Call.func);
3292 if (!value) goto failed;
3293 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3294 goto failed;
3295 Py_DECREF(value);
3296 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3297 if (!value) goto failed;
3298 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3299 goto failed;
3300 Py_DECREF(value);
3301 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3302 if (!value) goto failed;
3303 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3304 goto failed;
3305 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003306 break;
3307 case Num_kind:
3308 result = PyType_GenericNew(Num_type, NULL, NULL);
3309 if (!result) goto failed;
3310 value = ast2obj_object(o->v.Num.n);
3311 if (!value) goto failed;
3312 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3313 goto failed;
3314 Py_DECREF(value);
3315 break;
3316 case Str_kind:
3317 result = PyType_GenericNew(Str_type, NULL, NULL);
3318 if (!result) goto failed;
3319 value = ast2obj_string(o->v.Str.s);
3320 if (!value) goto failed;
3321 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3322 goto failed;
3323 Py_DECREF(value);
3324 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003325 case FormattedValue_kind:
3326 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3327 if (!result) goto failed;
3328 value = ast2obj_expr(o->v.FormattedValue.value);
3329 if (!value) goto failed;
3330 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3331 goto failed;
3332 Py_DECREF(value);
3333 value = ast2obj_int(o->v.FormattedValue.conversion);
3334 if (!value) goto failed;
3335 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3336 goto failed;
3337 Py_DECREF(value);
3338 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3339 if (!value) goto failed;
3340 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3341 goto failed;
3342 Py_DECREF(value);
3343 break;
3344 case JoinedStr_kind:
3345 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3346 if (!result) goto failed;
3347 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3348 if (!value) goto failed;
3349 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3350 goto failed;
3351 Py_DECREF(value);
3352 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003353 case Bytes_kind:
3354 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3355 if (!result) goto failed;
3356 value = ast2obj_bytes(o->v.Bytes.s);
3357 if (!value) goto failed;
3358 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3359 goto failed;
3360 Py_DECREF(value);
3361 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003362 case NameConstant_kind:
3363 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3364 if (!result) goto failed;
3365 value = ast2obj_singleton(o->v.NameConstant.value);
3366 if (!value) goto failed;
3367 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3368 goto failed;
3369 Py_DECREF(value);
3370 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003371 case Ellipsis_kind:
3372 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3373 if (!result) goto failed;
3374 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003375 case Constant_kind:
3376 result = PyType_GenericNew(Constant_type, NULL, NULL);
3377 if (!result) goto failed;
3378 value = ast2obj_constant(o->v.Constant.value);
3379 if (!value) goto failed;
3380 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3381 goto failed;
3382 Py_DECREF(value);
3383 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003384 case Attribute_kind:
3385 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3386 if (!result) goto failed;
3387 value = ast2obj_expr(o->v.Attribute.value);
3388 if (!value) goto failed;
3389 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3390 goto failed;
3391 Py_DECREF(value);
3392 value = ast2obj_identifier(o->v.Attribute.attr);
3393 if (!value) goto failed;
3394 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3395 goto failed;
3396 Py_DECREF(value);
3397 value = ast2obj_expr_context(o->v.Attribute.ctx);
3398 if (!value) goto failed;
3399 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3400 goto failed;
3401 Py_DECREF(value);
3402 break;
3403 case Subscript_kind:
3404 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3405 if (!result) goto failed;
3406 value = ast2obj_expr(o->v.Subscript.value);
3407 if (!value) goto failed;
3408 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3409 goto failed;
3410 Py_DECREF(value);
3411 value = ast2obj_slice(o->v.Subscript.slice);
3412 if (!value) goto failed;
3413 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3414 goto failed;
3415 Py_DECREF(value);
3416 value = ast2obj_expr_context(o->v.Subscript.ctx);
3417 if (!value) goto failed;
3418 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3419 goto failed;
3420 Py_DECREF(value);
3421 break;
3422 case Starred_kind:
3423 result = PyType_GenericNew(Starred_type, NULL, NULL);
3424 if (!result) goto failed;
3425 value = ast2obj_expr(o->v.Starred.value);
3426 if (!value) goto failed;
3427 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3428 goto failed;
3429 Py_DECREF(value);
3430 value = ast2obj_expr_context(o->v.Starred.ctx);
3431 if (!value) goto failed;
3432 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
3435 break;
3436 case Name_kind:
3437 result = PyType_GenericNew(Name_type, NULL, NULL);
3438 if (!result) goto failed;
3439 value = ast2obj_identifier(o->v.Name.id);
3440 if (!value) goto failed;
3441 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3442 goto failed;
3443 Py_DECREF(value);
3444 value = ast2obj_expr_context(o->v.Name.ctx);
3445 if (!value) goto failed;
3446 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3447 goto failed;
3448 Py_DECREF(value);
3449 break;
3450 case List_kind:
3451 result = PyType_GenericNew(List_type, NULL, NULL);
3452 if (!result) goto failed;
3453 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3454 if (!value) goto failed;
3455 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3456 goto failed;
3457 Py_DECREF(value);
3458 value = ast2obj_expr_context(o->v.List.ctx);
3459 if (!value) goto failed;
3460 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3461 goto failed;
3462 Py_DECREF(value);
3463 break;
3464 case Tuple_kind:
3465 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3466 if (!result) goto failed;
3467 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3468 if (!value) goto failed;
3469 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3470 goto failed;
3471 Py_DECREF(value);
3472 value = ast2obj_expr_context(o->v.Tuple.ctx);
3473 if (!value) goto failed;
3474 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3475 goto failed;
3476 Py_DECREF(value);
3477 break;
3478 }
3479 value = ast2obj_int(o->lineno);
3480 if (!value) goto failed;
3481 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3482 goto failed;
3483 Py_DECREF(value);
3484 value = ast2obj_int(o->col_offset);
3485 if (!value) goto failed;
3486 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3487 goto failed;
3488 Py_DECREF(value);
3489 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003490failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003491 Py_XDECREF(value);
3492 Py_XDECREF(result);
3493 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494}
3495
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003496PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003498 switch(o) {
3499 case Load:
3500 Py_INCREF(Load_singleton);
3501 return Load_singleton;
3502 case Store:
3503 Py_INCREF(Store_singleton);
3504 return Store_singleton;
3505 case Del:
3506 Py_INCREF(Del_singleton);
3507 return Del_singleton;
3508 case AugLoad:
3509 Py_INCREF(AugLoad_singleton);
3510 return AugLoad_singleton;
3511 case AugStore:
3512 Py_INCREF(AugStore_singleton);
3513 return AugStore_singleton;
3514 case Param:
3515 Py_INCREF(Param_singleton);
3516 return Param_singleton;
3517 default:
3518 /* should never happen, but just in case ... */
3519 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3520 return NULL;
3521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003523PyObject*
3524ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003526 slice_ty o = (slice_ty)_o;
3527 PyObject *result = NULL, *value = NULL;
3528 if (!o) {
3529 Py_INCREF(Py_None);
3530 return Py_None;
3531 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003532
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 switch (o->kind) {
3534 case Slice_kind:
3535 result = PyType_GenericNew(Slice_type, NULL, NULL);
3536 if (!result) goto failed;
3537 value = ast2obj_expr(o->v.Slice.lower);
3538 if (!value) goto failed;
3539 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3540 goto failed;
3541 Py_DECREF(value);
3542 value = ast2obj_expr(o->v.Slice.upper);
3543 if (!value) goto failed;
3544 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3545 goto failed;
3546 Py_DECREF(value);
3547 value = ast2obj_expr(o->v.Slice.step);
3548 if (!value) goto failed;
3549 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3550 goto failed;
3551 Py_DECREF(value);
3552 break;
3553 case ExtSlice_kind:
3554 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3555 if (!result) goto failed;
3556 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3557 if (!value) goto failed;
3558 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3559 goto failed;
3560 Py_DECREF(value);
3561 break;
3562 case Index_kind:
3563 result = PyType_GenericNew(Index_type, NULL, NULL);
3564 if (!result) goto failed;
3565 value = ast2obj_expr(o->v.Index.value);
3566 if (!value) goto failed;
3567 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3568 goto failed;
3569 Py_DECREF(value);
3570 break;
3571 }
3572 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003573failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003574 Py_XDECREF(value);
3575 Py_XDECREF(result);
3576 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577}
3578
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003579PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003581 switch(o) {
3582 case And:
3583 Py_INCREF(And_singleton);
3584 return And_singleton;
3585 case Or:
3586 Py_INCREF(Or_singleton);
3587 return Or_singleton;
3588 default:
3589 /* should never happen, but just in case ... */
3590 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3591 return NULL;
3592 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003593}
3594PyObject* ast2obj_operator(operator_ty o)
3595{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 switch(o) {
3597 case Add:
3598 Py_INCREF(Add_singleton);
3599 return Add_singleton;
3600 case Sub:
3601 Py_INCREF(Sub_singleton);
3602 return Sub_singleton;
3603 case Mult:
3604 Py_INCREF(Mult_singleton);
3605 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003606 case MatMult:
3607 Py_INCREF(MatMult_singleton);
3608 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003609 case Div:
3610 Py_INCREF(Div_singleton);
3611 return Div_singleton;
3612 case Mod:
3613 Py_INCREF(Mod_singleton);
3614 return Mod_singleton;
3615 case Pow:
3616 Py_INCREF(Pow_singleton);
3617 return Pow_singleton;
3618 case LShift:
3619 Py_INCREF(LShift_singleton);
3620 return LShift_singleton;
3621 case RShift:
3622 Py_INCREF(RShift_singleton);
3623 return RShift_singleton;
3624 case BitOr:
3625 Py_INCREF(BitOr_singleton);
3626 return BitOr_singleton;
3627 case BitXor:
3628 Py_INCREF(BitXor_singleton);
3629 return BitXor_singleton;
3630 case BitAnd:
3631 Py_INCREF(BitAnd_singleton);
3632 return BitAnd_singleton;
3633 case FloorDiv:
3634 Py_INCREF(FloorDiv_singleton);
3635 return FloorDiv_singleton;
3636 default:
3637 /* should never happen, but just in case ... */
3638 PyErr_Format(PyExc_SystemError, "unknown operator found");
3639 return NULL;
3640 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003641}
3642PyObject* ast2obj_unaryop(unaryop_ty o)
3643{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003644 switch(o) {
3645 case Invert:
3646 Py_INCREF(Invert_singleton);
3647 return Invert_singleton;
3648 case Not:
3649 Py_INCREF(Not_singleton);
3650 return Not_singleton;
3651 case UAdd:
3652 Py_INCREF(UAdd_singleton);
3653 return UAdd_singleton;
3654 case USub:
3655 Py_INCREF(USub_singleton);
3656 return USub_singleton;
3657 default:
3658 /* should never happen, but just in case ... */
3659 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3660 return NULL;
3661 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003662}
3663PyObject* ast2obj_cmpop(cmpop_ty o)
3664{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003665 switch(o) {
3666 case Eq:
3667 Py_INCREF(Eq_singleton);
3668 return Eq_singleton;
3669 case NotEq:
3670 Py_INCREF(NotEq_singleton);
3671 return NotEq_singleton;
3672 case Lt:
3673 Py_INCREF(Lt_singleton);
3674 return Lt_singleton;
3675 case LtE:
3676 Py_INCREF(LtE_singleton);
3677 return LtE_singleton;
3678 case Gt:
3679 Py_INCREF(Gt_singleton);
3680 return Gt_singleton;
3681 case GtE:
3682 Py_INCREF(GtE_singleton);
3683 return GtE_singleton;
3684 case Is:
3685 Py_INCREF(Is_singleton);
3686 return Is_singleton;
3687 case IsNot:
3688 Py_INCREF(IsNot_singleton);
3689 return IsNot_singleton;
3690 case In:
3691 Py_INCREF(In_singleton);
3692 return In_singleton;
3693 case NotIn:
3694 Py_INCREF(NotIn_singleton);
3695 return NotIn_singleton;
3696 default:
3697 /* should never happen, but just in case ... */
3698 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3699 return NULL;
3700 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003701}
3702PyObject*
3703ast2obj_comprehension(void* _o)
3704{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003705 comprehension_ty o = (comprehension_ty)_o;
3706 PyObject *result = NULL, *value = NULL;
3707 if (!o) {
3708 Py_INCREF(Py_None);
3709 return Py_None;
3710 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003711
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003712 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3713 if (!result) return NULL;
3714 value = ast2obj_expr(o->target);
3715 if (!value) goto failed;
3716 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3717 goto failed;
3718 Py_DECREF(value);
3719 value = ast2obj_expr(o->iter);
3720 if (!value) goto failed;
3721 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3722 goto failed;
3723 Py_DECREF(value);
3724 value = ast2obj_list(o->ifs, ast2obj_expr);
3725 if (!value) goto failed;
3726 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3727 goto failed;
3728 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003729 value = ast2obj_int(o->is_async);
3730 if (!value) goto failed;
3731 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3732 goto failed;
3733 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003734 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003735failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 Py_XDECREF(value);
3737 Py_XDECREF(result);
3738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739}
3740
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003741PyObject*
3742ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003744 excepthandler_ty o = (excepthandler_ty)_o;
3745 PyObject *result = NULL, *value = NULL;
3746 if (!o) {
3747 Py_INCREF(Py_None);
3748 return Py_None;
3749 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003750
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 switch (o->kind) {
3752 case ExceptHandler_kind:
3753 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3754 if (!result) goto failed;
3755 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003756 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003757 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3758 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003759 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003760 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3763 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003764 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003765 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3766 if (!value) goto failed;
3767 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3768 goto failed;
3769 Py_DECREF(value);
3770 break;
3771 }
3772 value = ast2obj_int(o->lineno);
3773 if (!value) goto failed;
3774 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3775 goto failed;
3776 Py_DECREF(value);
3777 value = ast2obj_int(o->col_offset);
3778 if (!value) goto failed;
3779 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3780 goto failed;
3781 Py_DECREF(value);
3782 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003783failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003784 Py_XDECREF(value);
3785 Py_XDECREF(result);
3786 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787}
3788
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003789PyObject*
3790ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 arguments_ty o = (arguments_ty)_o;
3793 PyObject *result = NULL, *value = NULL;
3794 if (!o) {
3795 Py_INCREF(Py_None);
3796 return Py_None;
3797 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003798
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003799 result = PyType_GenericNew(arguments_type, NULL, NULL);
3800 if (!result) return NULL;
3801 value = ast2obj_list(o->args, ast2obj_arg);
3802 if (!value) goto failed;
3803 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3804 goto failed;
3805 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003806 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003807 if (!value) goto failed;
3808 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3809 goto failed;
3810 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003811 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3812 if (!value) goto failed;
3813 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3814 goto failed;
3815 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003816 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003817 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003818 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 goto failed;
3820 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003821 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003822 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003823 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003824 goto failed;
3825 Py_DECREF(value);
3826 value = ast2obj_list(o->defaults, ast2obj_expr);
3827 if (!value) goto failed;
3828 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3829 goto failed;
3830 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003831 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003832failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003833 Py_XDECREF(value);
3834 Py_XDECREF(result);
3835 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836}
3837
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003838PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003839ast2obj_arg(void* _o)
3840{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003841 arg_ty o = (arg_ty)_o;
3842 PyObject *result = NULL, *value = NULL;
3843 if (!o) {
3844 Py_INCREF(Py_None);
3845 return Py_None;
3846 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003847
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003848 result = PyType_GenericNew(arg_type, NULL, NULL);
3849 if (!result) return NULL;
3850 value = ast2obj_identifier(o->arg);
3851 if (!value) goto failed;
3852 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3853 goto failed;
3854 Py_DECREF(value);
3855 value = ast2obj_expr(o->annotation);
3856 if (!value) goto failed;
3857 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3858 goto failed;
3859 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003860 value = ast2obj_int(o->lineno);
3861 if (!value) goto failed;
3862 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3863 goto failed;
3864 Py_DECREF(value);
3865 value = ast2obj_int(o->col_offset);
3866 if (!value) goto failed;
3867 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3868 goto failed;
3869 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003870 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003871failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003872 Py_XDECREF(value);
3873 Py_XDECREF(result);
3874 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003875}
3876
3877PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003878ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003880 keyword_ty o = (keyword_ty)_o;
3881 PyObject *result = NULL, *value = NULL;
3882 if (!o) {
3883 Py_INCREF(Py_None);
3884 return Py_None;
3885 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003886
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003887 result = PyType_GenericNew(keyword_type, NULL, NULL);
3888 if (!result) return NULL;
3889 value = ast2obj_identifier(o->arg);
3890 if (!value) goto failed;
3891 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3892 goto failed;
3893 Py_DECREF(value);
3894 value = ast2obj_expr(o->value);
3895 if (!value) goto failed;
3896 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3897 goto failed;
3898 Py_DECREF(value);
3899 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003900failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003901 Py_XDECREF(value);
3902 Py_XDECREF(result);
3903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904}
3905
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003906PyObject*
3907ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003909 alias_ty o = (alias_ty)_o;
3910 PyObject *result = NULL, *value = NULL;
3911 if (!o) {
3912 Py_INCREF(Py_None);
3913 return Py_None;
3914 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003915
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003916 result = PyType_GenericNew(alias_type, NULL, NULL);
3917 if (!result) return NULL;
3918 value = ast2obj_identifier(o->name);
3919 if (!value) goto failed;
3920 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3921 goto failed;
3922 Py_DECREF(value);
3923 value = ast2obj_identifier(o->asname);
3924 if (!value) goto failed;
3925 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3926 goto failed;
3927 Py_DECREF(value);
3928 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003929failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 Py_XDECREF(value);
3931 Py_XDECREF(result);
3932 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933}
3934
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003935PyObject*
3936ast2obj_withitem(void* _o)
3937{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 withitem_ty o = (withitem_ty)_o;
3939 PyObject *result = NULL, *value = NULL;
3940 if (!o) {
3941 Py_INCREF(Py_None);
3942 return Py_None;
3943 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003944
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 result = PyType_GenericNew(withitem_type, NULL, NULL);
3946 if (!result) return NULL;
3947 value = ast2obj_expr(o->context_expr);
3948 if (!value) goto failed;
3949 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3950 goto failed;
3951 Py_DECREF(value);
3952 value = ast2obj_expr(o->optional_vars);
3953 if (!value) goto failed;
3954 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3955 goto failed;
3956 Py_DECREF(value);
3957 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003958failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 Py_XDECREF(value);
3960 Py_XDECREF(result);
3961 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003962}
3963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003965int
3966obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3967{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003968 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003969
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003970 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003971
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003972 if (obj == Py_None) {
3973 *out = NULL;
3974 return 0;
3975 }
3976 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3977 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003978 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003979 }
3980 if (isinstance) {
3981 asdl_seq* body;
3982
3983 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3984 int res;
3985 Py_ssize_t len;
3986 Py_ssize_t i;
3987 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3988 if (tmp == NULL) goto failed;
3989 if (!PyList_Check(tmp)) {
3990 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3991 goto failed;
3992 }
3993 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003994 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003995 if (body == NULL) goto failed;
3996 for (i = 0; i < len; i++) {
3997 stmt_ty value;
3998 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3999 if (res != 0) goto failed;
4000 asdl_seq_SET(body, i, value);
4001 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004002 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004003 } else {
4004 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4005 return 1;
4006 }
4007 *out = Module(body, arena);
4008 if (*out == NULL) goto failed;
4009 return 0;
4010 }
4011 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4012 if (isinstance == -1) {
4013 return 1;
4014 }
4015 if (isinstance) {
4016 asdl_seq* body;
4017
4018 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4019 int res;
4020 Py_ssize_t len;
4021 Py_ssize_t i;
4022 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4023 if (tmp == NULL) goto failed;
4024 if (!PyList_Check(tmp)) {
4025 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4026 goto failed;
4027 }
4028 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004029 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004030 if (body == NULL) goto failed;
4031 for (i = 0; i < len; i++) {
4032 stmt_ty value;
4033 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4034 if (res != 0) goto failed;
4035 asdl_seq_SET(body, i, value);
4036 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004037 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004038 } else {
4039 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4040 return 1;
4041 }
4042 *out = Interactive(body, arena);
4043 if (*out == NULL) goto failed;
4044 return 0;
4045 }
4046 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4047 if (isinstance == -1) {
4048 return 1;
4049 }
4050 if (isinstance) {
4051 expr_ty body;
4052
4053 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4054 int res;
4055 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4056 if (tmp == NULL) goto failed;
4057 res = obj2ast_expr(tmp, &body, arena);
4058 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004059 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004060 } else {
4061 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4062 return 1;
4063 }
4064 *out = Expression(body, arena);
4065 if (*out == NULL) goto failed;
4066 return 0;
4067 }
4068 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4069 if (isinstance == -1) {
4070 return 1;
4071 }
4072 if (isinstance) {
4073 asdl_seq* body;
4074
4075 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4076 int res;
4077 Py_ssize_t len;
4078 Py_ssize_t i;
4079 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4080 if (tmp == NULL) goto failed;
4081 if (!PyList_Check(tmp)) {
4082 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4083 goto failed;
4084 }
4085 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004086 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004087 if (body == NULL) goto failed;
4088 for (i = 0; i < len; i++) {
4089 stmt_ty value;
4090 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4091 if (res != 0) goto failed;
4092 asdl_seq_SET(body, i, value);
4093 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004094 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004095 } else {
4096 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4097 return 1;
4098 }
4099 *out = Suite(body, arena);
4100 if (*out == NULL) goto failed;
4101 return 0;
4102 }
4103
4104 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4105 failed:
4106 Py_XDECREF(tmp);
4107 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004108}
4109
4110int
4111obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4112{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004113 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004114
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004115 PyObject *tmp = NULL;
4116 int lineno;
4117 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004118
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004119 if (obj == Py_None) {
4120 *out = NULL;
4121 return 0;
4122 }
4123 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4124 int res;
4125 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4126 if (tmp == NULL) goto failed;
4127 res = obj2ast_int(tmp, &lineno, arena);
4128 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004129 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004130 } else {
4131 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004132 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004133 }
4134 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4135 int res;
4136 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4137 if (tmp == NULL) goto failed;
4138 res = obj2ast_int(tmp, &col_offset, arena);
4139 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004140 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004141 } else {
4142 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4143 return 1;
4144 }
4145 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4146 if (isinstance == -1) {
4147 return 1;
4148 }
4149 if (isinstance) {
4150 identifier name;
4151 arguments_ty args;
4152 asdl_seq* body;
4153 asdl_seq* decorator_list;
4154 expr_ty returns;
4155
4156 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4157 int res;
4158 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4159 if (tmp == NULL) goto failed;
4160 res = obj2ast_identifier(tmp, &name, arena);
4161 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004162 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004163 } else {
4164 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4165 return 1;
4166 }
4167 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4168 int res;
4169 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4170 if (tmp == NULL) goto failed;
4171 res = obj2ast_arguments(tmp, &args, arena);
4172 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004173 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004174 } else {
4175 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4176 return 1;
4177 }
4178 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4179 int res;
4180 Py_ssize_t len;
4181 Py_ssize_t i;
4182 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4183 if (tmp == NULL) goto failed;
4184 if (!PyList_Check(tmp)) {
4185 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4186 goto failed;
4187 }
4188 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004189 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004190 if (body == NULL) goto failed;
4191 for (i = 0; i < len; i++) {
4192 stmt_ty value;
4193 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4194 if (res != 0) goto failed;
4195 asdl_seq_SET(body, i, value);
4196 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004197 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004198 } else {
4199 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4200 return 1;
4201 }
4202 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4203 int res;
4204 Py_ssize_t len;
4205 Py_ssize_t i;
4206 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4207 if (tmp == NULL) goto failed;
4208 if (!PyList_Check(tmp)) {
4209 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4210 goto failed;
4211 }
4212 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004213 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004214 if (decorator_list == NULL) goto failed;
4215 for (i = 0; i < len; i++) {
4216 expr_ty value;
4217 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4218 if (res != 0) goto failed;
4219 asdl_seq_SET(decorator_list, i, value);
4220 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004221 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004222 } else {
4223 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4224 return 1;
4225 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004226 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004227 int res;
4228 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4229 if (tmp == NULL) goto failed;
4230 res = obj2ast_expr(tmp, &returns, arena);
4231 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004232 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004233 } else {
4234 returns = NULL;
4235 }
4236 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4237 col_offset, arena);
4238 if (*out == NULL) goto failed;
4239 return 0;
4240 }
Yury Selivanov75445082015-05-11 22:57:16 -04004241 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4242 if (isinstance == -1) {
4243 return 1;
4244 }
4245 if (isinstance) {
4246 identifier name;
4247 arguments_ty args;
4248 asdl_seq* body;
4249 asdl_seq* decorator_list;
4250 expr_ty returns;
4251
4252 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4253 int res;
4254 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4255 if (tmp == NULL) goto failed;
4256 res = obj2ast_identifier(tmp, &name, arena);
4257 if (res != 0) goto failed;
4258 Py_CLEAR(tmp);
4259 } else {
4260 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4261 return 1;
4262 }
4263 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4264 int res;
4265 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4266 if (tmp == NULL) goto failed;
4267 res = obj2ast_arguments(tmp, &args, arena);
4268 if (res != 0) goto failed;
4269 Py_CLEAR(tmp);
4270 } else {
4271 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4272 return 1;
4273 }
4274 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4275 int res;
4276 Py_ssize_t len;
4277 Py_ssize_t i;
4278 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4279 if (tmp == NULL) goto failed;
4280 if (!PyList_Check(tmp)) {
4281 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4282 goto failed;
4283 }
4284 len = PyList_GET_SIZE(tmp);
4285 body = _Py_asdl_seq_new(len, arena);
4286 if (body == NULL) goto failed;
4287 for (i = 0; i < len; i++) {
4288 stmt_ty value;
4289 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4290 if (res != 0) goto failed;
4291 asdl_seq_SET(body, i, value);
4292 }
4293 Py_CLEAR(tmp);
4294 } else {
4295 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4296 return 1;
4297 }
4298 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4299 int res;
4300 Py_ssize_t len;
4301 Py_ssize_t i;
4302 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4303 if (tmp == NULL) goto failed;
4304 if (!PyList_Check(tmp)) {
4305 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4306 goto failed;
4307 }
4308 len = PyList_GET_SIZE(tmp);
4309 decorator_list = _Py_asdl_seq_new(len, arena);
4310 if (decorator_list == NULL) goto failed;
4311 for (i = 0; i < len; i++) {
4312 expr_ty value;
4313 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4314 if (res != 0) goto failed;
4315 asdl_seq_SET(decorator_list, i, value);
4316 }
4317 Py_CLEAR(tmp);
4318 } else {
4319 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4320 return 1;
4321 }
4322 if (exists_not_none(obj, &PyId_returns)) {
4323 int res;
4324 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4325 if (tmp == NULL) goto failed;
4326 res = obj2ast_expr(tmp, &returns, arena);
4327 if (res != 0) goto failed;
4328 Py_CLEAR(tmp);
4329 } else {
4330 returns = NULL;
4331 }
4332 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4333 lineno, col_offset, arena);
4334 if (*out == NULL) goto failed;
4335 return 0;
4336 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004337 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4338 if (isinstance == -1) {
4339 return 1;
4340 }
4341 if (isinstance) {
4342 identifier name;
4343 asdl_seq* bases;
4344 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004345 asdl_seq* body;
4346 asdl_seq* decorator_list;
4347
4348 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4349 int res;
4350 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4351 if (tmp == NULL) goto failed;
4352 res = obj2ast_identifier(tmp, &name, arena);
4353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004355 } else {
4356 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4357 return 1;
4358 }
4359 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4360 int res;
4361 Py_ssize_t len;
4362 Py_ssize_t i;
4363 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4364 if (tmp == NULL) goto failed;
4365 if (!PyList_Check(tmp)) {
4366 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4367 goto failed;
4368 }
4369 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004370 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004371 if (bases == NULL) goto failed;
4372 for (i = 0; i < len; i++) {
4373 expr_ty value;
4374 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4375 if (res != 0) goto failed;
4376 asdl_seq_SET(bases, i, value);
4377 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004378 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004379 } else {
4380 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4381 return 1;
4382 }
4383 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4384 int res;
4385 Py_ssize_t len;
4386 Py_ssize_t i;
4387 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4388 if (tmp == NULL) goto failed;
4389 if (!PyList_Check(tmp)) {
4390 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4391 goto failed;
4392 }
4393 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004394 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004395 if (keywords == NULL) goto failed;
4396 for (i = 0; i < len; i++) {
4397 keyword_ty value;
4398 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4399 if (res != 0) goto failed;
4400 asdl_seq_SET(keywords, i, value);
4401 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004402 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004403 } else {
4404 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4405 return 1;
4406 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004407 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4408 int res;
4409 Py_ssize_t len;
4410 Py_ssize_t i;
4411 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4412 if (tmp == NULL) goto failed;
4413 if (!PyList_Check(tmp)) {
4414 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4415 goto failed;
4416 }
4417 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004418 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004419 if (body == NULL) goto failed;
4420 for (i = 0; i < len; i++) {
4421 stmt_ty value;
4422 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4423 if (res != 0) goto failed;
4424 asdl_seq_SET(body, i, value);
4425 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004426 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004427 } else {
4428 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4429 return 1;
4430 }
4431 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4432 int res;
4433 Py_ssize_t len;
4434 Py_ssize_t i;
4435 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4436 if (tmp == NULL) goto failed;
4437 if (!PyList_Check(tmp)) {
4438 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4439 goto failed;
4440 }
4441 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004442 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004443 if (decorator_list == NULL) goto failed;
4444 for (i = 0; i < len; i++) {
4445 expr_ty value;
4446 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4447 if (res != 0) goto failed;
4448 asdl_seq_SET(decorator_list, i, value);
4449 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004450 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004451 } else {
4452 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4453 return 1;
4454 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004455 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4456 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004457 if (*out == NULL) goto failed;
4458 return 0;
4459 }
4460 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4461 if (isinstance == -1) {
4462 return 1;
4463 }
4464 if (isinstance) {
4465 expr_ty value;
4466
Victor Stinneree4b59c2013-07-27 00:01:35 +02004467 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004468 int res;
4469 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4470 if (tmp == NULL) goto failed;
4471 res = obj2ast_expr(tmp, &value, arena);
4472 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004473 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004474 } else {
4475 value = NULL;
4476 }
4477 *out = Return(value, lineno, col_offset, arena);
4478 if (*out == NULL) goto failed;
4479 return 0;
4480 }
4481 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4482 if (isinstance == -1) {
4483 return 1;
4484 }
4485 if (isinstance) {
4486 asdl_seq* targets;
4487
4488 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4489 int res;
4490 Py_ssize_t len;
4491 Py_ssize_t i;
4492 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4493 if (tmp == NULL) goto failed;
4494 if (!PyList_Check(tmp)) {
4495 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4496 goto failed;
4497 }
4498 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004499 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004500 if (targets == NULL) goto failed;
4501 for (i = 0; i < len; i++) {
4502 expr_ty value;
4503 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4504 if (res != 0) goto failed;
4505 asdl_seq_SET(targets, i, value);
4506 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004507 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004508 } else {
4509 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4510 return 1;
4511 }
4512 *out = Delete(targets, lineno, col_offset, arena);
4513 if (*out == NULL) goto failed;
4514 return 0;
4515 }
4516 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4517 if (isinstance == -1) {
4518 return 1;
4519 }
4520 if (isinstance) {
4521 asdl_seq* targets;
4522 expr_ty value;
4523
4524 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4525 int res;
4526 Py_ssize_t len;
4527 Py_ssize_t i;
4528 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4529 if (tmp == NULL) goto failed;
4530 if (!PyList_Check(tmp)) {
4531 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4532 goto failed;
4533 }
4534 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004535 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004536 if (targets == NULL) goto failed;
4537 for (i = 0; i < len; i++) {
4538 expr_ty value;
4539 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4540 if (res != 0) goto failed;
4541 asdl_seq_SET(targets, i, value);
4542 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004543 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004544 } else {
4545 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4546 return 1;
4547 }
4548 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4549 int res;
4550 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4551 if (tmp == NULL) goto failed;
4552 res = obj2ast_expr(tmp, &value, arena);
4553 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004554 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004555 } else {
4556 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4557 return 1;
4558 }
4559 *out = Assign(targets, value, lineno, col_offset, arena);
4560 if (*out == NULL) goto failed;
4561 return 0;
4562 }
4563 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4564 if (isinstance == -1) {
4565 return 1;
4566 }
4567 if (isinstance) {
4568 expr_ty target;
4569 operator_ty op;
4570 expr_ty value;
4571
4572 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4573 int res;
4574 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4575 if (tmp == NULL) goto failed;
4576 res = obj2ast_expr(tmp, &target, arena);
4577 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004578 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004579 } else {
4580 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4581 return 1;
4582 }
4583 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4584 int res;
4585 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4586 if (tmp == NULL) goto failed;
4587 res = obj2ast_operator(tmp, &op, arena);
4588 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004590 } else {
4591 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4592 return 1;
4593 }
4594 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4595 int res;
4596 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4597 if (tmp == NULL) goto failed;
4598 res = obj2ast_expr(tmp, &value, arena);
4599 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004600 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004601 } else {
4602 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4603 return 1;
4604 }
4605 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4606 if (*out == NULL) goto failed;
4607 return 0;
4608 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004609 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4610 if (isinstance == -1) {
4611 return 1;
4612 }
4613 if (isinstance) {
4614 expr_ty target;
4615 expr_ty annotation;
4616 expr_ty value;
4617 int simple;
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;
4625 Py_CLEAR(tmp);
4626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4628 return 1;
4629 }
4630 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4631 int res;
4632 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4633 if (tmp == NULL) goto failed;
4634 res = obj2ast_expr(tmp, &annotation, arena);
4635 if (res != 0) goto failed;
4636 Py_CLEAR(tmp);
4637 } else {
4638 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4639 return 1;
4640 }
4641 if (exists_not_none(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;
4647 Py_CLEAR(tmp);
4648 } else {
4649 value = NULL;
4650 }
4651 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4652 int res;
4653 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4654 if (tmp == NULL) goto failed;
4655 res = obj2ast_int(tmp, &simple, arena);
4656 if (res != 0) goto failed;
4657 Py_CLEAR(tmp);
4658 } else {
4659 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4660 return 1;
4661 }
4662 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4663 arena);
4664 if (*out == NULL) goto failed;
4665 return 0;
4666 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004667 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4668 if (isinstance == -1) {
4669 return 1;
4670 }
4671 if (isinstance) {
4672 expr_ty target;
4673 expr_ty iter;
4674 asdl_seq* body;
4675 asdl_seq* orelse;
4676
4677 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4678 int res;
4679 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4680 if (tmp == NULL) goto failed;
4681 res = obj2ast_expr(tmp, &target, arena);
4682 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004683 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004684 } else {
4685 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4686 return 1;
4687 }
4688 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4689 int res;
4690 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4691 if (tmp == NULL) goto failed;
4692 res = obj2ast_expr(tmp, &iter, arena);
4693 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004694 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004695 } else {
4696 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4697 return 1;
4698 }
4699 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4700 int res;
4701 Py_ssize_t len;
4702 Py_ssize_t i;
4703 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4704 if (tmp == NULL) goto failed;
4705 if (!PyList_Check(tmp)) {
4706 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4707 goto failed;
4708 }
4709 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004710 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004711 if (body == NULL) goto failed;
4712 for (i = 0; i < len; i++) {
4713 stmt_ty value;
4714 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4715 if (res != 0) goto failed;
4716 asdl_seq_SET(body, i, value);
4717 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004719 } else {
4720 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4721 return 1;
4722 }
4723 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4724 int res;
4725 Py_ssize_t len;
4726 Py_ssize_t i;
4727 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4728 if (tmp == NULL) goto failed;
4729 if (!PyList_Check(tmp)) {
4730 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4731 goto failed;
4732 }
4733 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004734 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004735 if (orelse == NULL) goto failed;
4736 for (i = 0; i < len; i++) {
4737 stmt_ty value;
4738 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4739 if (res != 0) goto failed;
4740 asdl_seq_SET(orelse, i, value);
4741 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004742 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004743 } else {
4744 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4745 return 1;
4746 }
4747 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4748 if (*out == NULL) goto failed;
4749 return 0;
4750 }
Yury Selivanov75445082015-05-11 22:57:16 -04004751 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4752 if (isinstance == -1) {
4753 return 1;
4754 }
4755 if (isinstance) {
4756 expr_ty target;
4757 expr_ty iter;
4758 asdl_seq* body;
4759 asdl_seq* orelse;
4760
4761 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4762 int res;
4763 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4764 if (tmp == NULL) goto failed;
4765 res = obj2ast_expr(tmp, &target, arena);
4766 if (res != 0) goto failed;
4767 Py_CLEAR(tmp);
4768 } else {
4769 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4770 return 1;
4771 }
4772 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4773 int res;
4774 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4775 if (tmp == NULL) goto failed;
4776 res = obj2ast_expr(tmp, &iter, arena);
4777 if (res != 0) goto failed;
4778 Py_CLEAR(tmp);
4779 } else {
4780 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4781 return 1;
4782 }
4783 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4784 int res;
4785 Py_ssize_t len;
4786 Py_ssize_t i;
4787 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4788 if (tmp == NULL) goto failed;
4789 if (!PyList_Check(tmp)) {
4790 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4791 goto failed;
4792 }
4793 len = PyList_GET_SIZE(tmp);
4794 body = _Py_asdl_seq_new(len, arena);
4795 if (body == NULL) goto failed;
4796 for (i = 0; i < len; i++) {
4797 stmt_ty value;
4798 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4799 if (res != 0) goto failed;
4800 asdl_seq_SET(body, i, value);
4801 }
4802 Py_CLEAR(tmp);
4803 } else {
4804 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4805 return 1;
4806 }
4807 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4808 int res;
4809 Py_ssize_t len;
4810 Py_ssize_t i;
4811 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4812 if (tmp == NULL) goto failed;
4813 if (!PyList_Check(tmp)) {
4814 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4815 goto failed;
4816 }
4817 len = PyList_GET_SIZE(tmp);
4818 orelse = _Py_asdl_seq_new(len, arena);
4819 if (orelse == NULL) goto failed;
4820 for (i = 0; i < len; i++) {
4821 stmt_ty value;
4822 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4823 if (res != 0) goto failed;
4824 asdl_seq_SET(orelse, i, value);
4825 }
4826 Py_CLEAR(tmp);
4827 } else {
4828 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4829 return 1;
4830 }
4831 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4832 if (*out == NULL) goto failed;
4833 return 0;
4834 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004835 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4836 if (isinstance == -1) {
4837 return 1;
4838 }
4839 if (isinstance) {
4840 expr_ty test;
4841 asdl_seq* body;
4842 asdl_seq* orelse;
4843
4844 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4845 int res;
4846 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4847 if (tmp == NULL) goto failed;
4848 res = obj2ast_expr(tmp, &test, arena);
4849 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004850 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004851 } else {
4852 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4853 return 1;
4854 }
4855 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4856 int res;
4857 Py_ssize_t len;
4858 Py_ssize_t i;
4859 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4860 if (tmp == NULL) goto failed;
4861 if (!PyList_Check(tmp)) {
4862 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4863 goto failed;
4864 }
4865 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004866 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004867 if (body == NULL) goto failed;
4868 for (i = 0; i < len; i++) {
4869 stmt_ty value;
4870 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4871 if (res != 0) goto failed;
4872 asdl_seq_SET(body, i, value);
4873 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004874 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004875 } else {
4876 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4877 return 1;
4878 }
4879 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4880 int res;
4881 Py_ssize_t len;
4882 Py_ssize_t i;
4883 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4884 if (tmp == NULL) goto failed;
4885 if (!PyList_Check(tmp)) {
4886 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4887 goto failed;
4888 }
4889 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004890 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004891 if (orelse == NULL) goto failed;
4892 for (i = 0; i < len; i++) {
4893 stmt_ty value;
4894 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4895 if (res != 0) goto failed;
4896 asdl_seq_SET(orelse, i, value);
4897 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004898 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004899 } else {
4900 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4901 return 1;
4902 }
4903 *out = While(test, body, orelse, lineno, col_offset, arena);
4904 if (*out == NULL) goto failed;
4905 return 0;
4906 }
4907 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4908 if (isinstance == -1) {
4909 return 1;
4910 }
4911 if (isinstance) {
4912 expr_ty test;
4913 asdl_seq* body;
4914 asdl_seq* orelse;
4915
4916 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4917 int res;
4918 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4919 if (tmp == NULL) goto failed;
4920 res = obj2ast_expr(tmp, &test, arena);
4921 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004922 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004923 } else {
4924 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4925 return 1;
4926 }
4927 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4928 int res;
4929 Py_ssize_t len;
4930 Py_ssize_t i;
4931 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4932 if (tmp == NULL) goto failed;
4933 if (!PyList_Check(tmp)) {
4934 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4935 goto failed;
4936 }
4937 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004938 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004939 if (body == NULL) goto failed;
4940 for (i = 0; i < len; i++) {
4941 stmt_ty value;
4942 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4943 if (res != 0) goto failed;
4944 asdl_seq_SET(body, i, value);
4945 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004947 } else {
4948 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4949 return 1;
4950 }
4951 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4952 int res;
4953 Py_ssize_t len;
4954 Py_ssize_t i;
4955 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4956 if (tmp == NULL) goto failed;
4957 if (!PyList_Check(tmp)) {
4958 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4959 goto failed;
4960 }
4961 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004962 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004963 if (orelse == NULL) goto failed;
4964 for (i = 0; i < len; i++) {
4965 stmt_ty value;
4966 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4967 if (res != 0) goto failed;
4968 asdl_seq_SET(orelse, i, value);
4969 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004970 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004971 } else {
4972 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4973 return 1;
4974 }
4975 *out = If(test, body, orelse, lineno, col_offset, arena);
4976 if (*out == NULL) goto failed;
4977 return 0;
4978 }
4979 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4980 if (isinstance == -1) {
4981 return 1;
4982 }
4983 if (isinstance) {
4984 asdl_seq* items;
4985 asdl_seq* body;
4986
4987 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4988 int res;
4989 Py_ssize_t len;
4990 Py_ssize_t i;
4991 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4992 if (tmp == NULL) goto failed;
4993 if (!PyList_Check(tmp)) {
4994 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4995 goto failed;
4996 }
4997 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004998 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004999 if (items == NULL) goto failed;
5000 for (i = 0; i < len; i++) {
5001 withitem_ty value;
5002 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5003 if (res != 0) goto failed;
5004 asdl_seq_SET(items, i, value);
5005 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005006 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005007 } else {
5008 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5009 return 1;
5010 }
5011 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5012 int res;
5013 Py_ssize_t len;
5014 Py_ssize_t i;
5015 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5016 if (tmp == NULL) goto failed;
5017 if (!PyList_Check(tmp)) {
5018 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5019 goto failed;
5020 }
5021 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005022 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005023 if (body == NULL) goto failed;
5024 for (i = 0; i < len; i++) {
5025 stmt_ty value;
5026 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5027 if (res != 0) goto failed;
5028 asdl_seq_SET(body, i, value);
5029 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005030 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005031 } else {
5032 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5033 return 1;
5034 }
5035 *out = With(items, body, lineno, col_offset, arena);
5036 if (*out == NULL) goto failed;
5037 return 0;
5038 }
Yury Selivanov75445082015-05-11 22:57:16 -04005039 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5040 if (isinstance == -1) {
5041 return 1;
5042 }
5043 if (isinstance) {
5044 asdl_seq* items;
5045 asdl_seq* body;
5046
5047 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5048 int res;
5049 Py_ssize_t len;
5050 Py_ssize_t i;
5051 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5052 if (tmp == NULL) goto failed;
5053 if (!PyList_Check(tmp)) {
5054 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5055 goto failed;
5056 }
5057 len = PyList_GET_SIZE(tmp);
5058 items = _Py_asdl_seq_new(len, arena);
5059 if (items == NULL) goto failed;
5060 for (i = 0; i < len; i++) {
5061 withitem_ty value;
5062 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5063 if (res != 0) goto failed;
5064 asdl_seq_SET(items, i, value);
5065 }
5066 Py_CLEAR(tmp);
5067 } else {
5068 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5069 return 1;
5070 }
5071 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5072 int res;
5073 Py_ssize_t len;
5074 Py_ssize_t i;
5075 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5076 if (tmp == NULL) goto failed;
5077 if (!PyList_Check(tmp)) {
5078 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5079 goto failed;
5080 }
5081 len = PyList_GET_SIZE(tmp);
5082 body = _Py_asdl_seq_new(len, arena);
5083 if (body == NULL) goto failed;
5084 for (i = 0; i < len; i++) {
5085 stmt_ty value;
5086 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5087 if (res != 0) goto failed;
5088 asdl_seq_SET(body, i, value);
5089 }
5090 Py_CLEAR(tmp);
5091 } else {
5092 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5093 return 1;
5094 }
5095 *out = AsyncWith(items, body, lineno, col_offset, arena);
5096 if (*out == NULL) goto failed;
5097 return 0;
5098 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005099 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5100 if (isinstance == -1) {
5101 return 1;
5102 }
5103 if (isinstance) {
5104 expr_ty exc;
5105 expr_ty cause;
5106
Victor Stinneree4b59c2013-07-27 00:01:35 +02005107 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005108 int res;
5109 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5110 if (tmp == NULL) goto failed;
5111 res = obj2ast_expr(tmp, &exc, arena);
5112 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005113 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005114 } else {
5115 exc = NULL;
5116 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005117 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005118 int res;
5119 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5120 if (tmp == NULL) goto failed;
5121 res = obj2ast_expr(tmp, &cause, arena);
5122 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005123 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005124 } else {
5125 cause = NULL;
5126 }
5127 *out = Raise(exc, cause, lineno, col_offset, arena);
5128 if (*out == NULL) goto failed;
5129 return 0;
5130 }
5131 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5132 if (isinstance == -1) {
5133 return 1;
5134 }
5135 if (isinstance) {
5136 asdl_seq* body;
5137 asdl_seq* handlers;
5138 asdl_seq* orelse;
5139 asdl_seq* finalbody;
5140
5141 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5142 int res;
5143 Py_ssize_t len;
5144 Py_ssize_t i;
5145 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5146 if (tmp == NULL) goto failed;
5147 if (!PyList_Check(tmp)) {
5148 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5149 goto failed;
5150 }
5151 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005152 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005153 if (body == NULL) goto failed;
5154 for (i = 0; i < len; i++) {
5155 stmt_ty value;
5156 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5157 if (res != 0) goto failed;
5158 asdl_seq_SET(body, i, value);
5159 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005160 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005161 } else {
5162 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5163 return 1;
5164 }
5165 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5166 int res;
5167 Py_ssize_t len;
5168 Py_ssize_t i;
5169 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5170 if (tmp == NULL) goto failed;
5171 if (!PyList_Check(tmp)) {
5172 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5173 goto failed;
5174 }
5175 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005176 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005177 if (handlers == NULL) goto failed;
5178 for (i = 0; i < len; i++) {
5179 excepthandler_ty value;
5180 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5181 if (res != 0) goto failed;
5182 asdl_seq_SET(handlers, i, value);
5183 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005184 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005185 } else {
5186 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5187 return 1;
5188 }
5189 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5190 int res;
5191 Py_ssize_t len;
5192 Py_ssize_t i;
5193 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5194 if (tmp == NULL) goto failed;
5195 if (!PyList_Check(tmp)) {
5196 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5197 goto failed;
5198 }
5199 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005200 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005201 if (orelse == NULL) goto failed;
5202 for (i = 0; i < len; i++) {
5203 stmt_ty value;
5204 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5205 if (res != 0) goto failed;
5206 asdl_seq_SET(orelse, i, value);
5207 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005208 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005209 } else {
5210 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5211 return 1;
5212 }
5213 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5214 int res;
5215 Py_ssize_t len;
5216 Py_ssize_t i;
5217 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5218 if (tmp == NULL) goto failed;
5219 if (!PyList_Check(tmp)) {
5220 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5221 goto failed;
5222 }
5223 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005224 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005225 if (finalbody == NULL) goto failed;
5226 for (i = 0; i < len; i++) {
5227 stmt_ty value;
5228 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5229 if (res != 0) goto failed;
5230 asdl_seq_SET(finalbody, i, value);
5231 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005232 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005233 } else {
5234 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5235 return 1;
5236 }
5237 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5238 arena);
5239 if (*out == NULL) goto failed;
5240 return 0;
5241 }
5242 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5243 if (isinstance == -1) {
5244 return 1;
5245 }
5246 if (isinstance) {
5247 expr_ty test;
5248 expr_ty msg;
5249
5250 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5251 int res;
5252 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5253 if (tmp == NULL) goto failed;
5254 res = obj2ast_expr(tmp, &test, arena);
5255 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005256 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005257 } else {
5258 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5259 return 1;
5260 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005261 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005262 int res;
5263 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5264 if (tmp == NULL) goto failed;
5265 res = obj2ast_expr(tmp, &msg, arena);
5266 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005267 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005268 } else {
5269 msg = NULL;
5270 }
5271 *out = Assert(test, msg, lineno, col_offset, arena);
5272 if (*out == NULL) goto failed;
5273 return 0;
5274 }
5275 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5276 if (isinstance == -1) {
5277 return 1;
5278 }
5279 if (isinstance) {
5280 asdl_seq* names;
5281
5282 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5283 int res;
5284 Py_ssize_t len;
5285 Py_ssize_t i;
5286 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5287 if (tmp == NULL) goto failed;
5288 if (!PyList_Check(tmp)) {
5289 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5290 goto failed;
5291 }
5292 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005293 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005294 if (names == NULL) goto failed;
5295 for (i = 0; i < len; i++) {
5296 alias_ty value;
5297 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5298 if (res != 0) goto failed;
5299 asdl_seq_SET(names, i, value);
5300 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005301 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005302 } else {
5303 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5304 return 1;
5305 }
5306 *out = Import(names, lineno, col_offset, arena);
5307 if (*out == NULL) goto failed;
5308 return 0;
5309 }
5310 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5311 if (isinstance == -1) {
5312 return 1;
5313 }
5314 if (isinstance) {
5315 identifier module;
5316 asdl_seq* names;
5317 int level;
5318
Victor Stinneree4b59c2013-07-27 00:01:35 +02005319 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005320 int res;
5321 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5322 if (tmp == NULL) goto failed;
5323 res = obj2ast_identifier(tmp, &module, arena);
5324 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005325 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005326 } else {
5327 module = NULL;
5328 }
5329 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5330 int res;
5331 Py_ssize_t len;
5332 Py_ssize_t i;
5333 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5334 if (tmp == NULL) goto failed;
5335 if (!PyList_Check(tmp)) {
5336 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5337 goto failed;
5338 }
5339 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005340 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005341 if (names == NULL) goto failed;
5342 for (i = 0; i < len; i++) {
5343 alias_ty value;
5344 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5345 if (res != 0) goto failed;
5346 asdl_seq_SET(names, i, value);
5347 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005348 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005349 } else {
5350 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5351 return 1;
5352 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005353 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005354 int res;
5355 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5356 if (tmp == NULL) goto failed;
5357 res = obj2ast_int(tmp, &level, arena);
5358 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005359 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005360 } else {
5361 level = 0;
5362 }
5363 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5364 if (*out == NULL) goto failed;
5365 return 0;
5366 }
5367 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5368 if (isinstance == -1) {
5369 return 1;
5370 }
5371 if (isinstance) {
5372 asdl_seq* names;
5373
5374 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5375 int res;
5376 Py_ssize_t len;
5377 Py_ssize_t i;
5378 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5379 if (tmp == NULL) goto failed;
5380 if (!PyList_Check(tmp)) {
5381 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5382 goto failed;
5383 }
5384 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005385 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005386 if (names == NULL) goto failed;
5387 for (i = 0; i < len; i++) {
5388 identifier value;
5389 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5390 if (res != 0) goto failed;
5391 asdl_seq_SET(names, i, value);
5392 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005393 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005394 } else {
5395 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5396 return 1;
5397 }
5398 *out = Global(names, lineno, col_offset, arena);
5399 if (*out == NULL) goto failed;
5400 return 0;
5401 }
5402 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5403 if (isinstance == -1) {
5404 return 1;
5405 }
5406 if (isinstance) {
5407 asdl_seq* names;
5408
5409 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5410 int res;
5411 Py_ssize_t len;
5412 Py_ssize_t i;
5413 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5414 if (tmp == NULL) goto failed;
5415 if (!PyList_Check(tmp)) {
5416 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5417 goto failed;
5418 }
5419 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005420 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005421 if (names == NULL) goto failed;
5422 for (i = 0; i < len; i++) {
5423 identifier value;
5424 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5425 if (res != 0) goto failed;
5426 asdl_seq_SET(names, i, value);
5427 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005428 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005429 } else {
5430 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5431 return 1;
5432 }
5433 *out = Nonlocal(names, lineno, col_offset, arena);
5434 if (*out == NULL) goto failed;
5435 return 0;
5436 }
5437 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5438 if (isinstance == -1) {
5439 return 1;
5440 }
5441 if (isinstance) {
5442 expr_ty value;
5443
5444 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5445 int res;
5446 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5447 if (tmp == NULL) goto failed;
5448 res = obj2ast_expr(tmp, &value, arena);
5449 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005450 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005451 } else {
5452 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5453 return 1;
5454 }
5455 *out = Expr(value, lineno, col_offset, arena);
5456 if (*out == NULL) goto failed;
5457 return 0;
5458 }
5459 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5460 if (isinstance == -1) {
5461 return 1;
5462 }
5463 if (isinstance) {
5464
5465 *out = Pass(lineno, col_offset, arena);
5466 if (*out == NULL) goto failed;
5467 return 0;
5468 }
5469 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5470 if (isinstance == -1) {
5471 return 1;
5472 }
5473 if (isinstance) {
5474
5475 *out = Break(lineno, col_offset, arena);
5476 if (*out == NULL) goto failed;
5477 return 0;
5478 }
5479 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5480 if (isinstance == -1) {
5481 return 1;
5482 }
5483 if (isinstance) {
5484
5485 *out = Continue(lineno, col_offset, arena);
5486 if (*out == NULL) goto failed;
5487 return 0;
5488 }
5489
5490 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5491 failed:
5492 Py_XDECREF(tmp);
5493 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005494}
5495
5496int
5497obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5498{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005499 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005500
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005501 PyObject *tmp = NULL;
5502 int lineno;
5503 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005504
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005505 if (obj == Py_None) {
5506 *out = NULL;
5507 return 0;
5508 }
5509 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5510 int res;
5511 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5512 if (tmp == NULL) goto failed;
5513 res = obj2ast_int(tmp, &lineno, arena);
5514 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005515 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005516 } else {
5517 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005518 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005519 }
5520 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5521 int res;
5522 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5523 if (tmp == NULL) goto failed;
5524 res = obj2ast_int(tmp, &col_offset, arena);
5525 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005526 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005527 } else {
5528 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5529 return 1;
5530 }
5531 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5532 if (isinstance == -1) {
5533 return 1;
5534 }
5535 if (isinstance) {
5536 boolop_ty op;
5537 asdl_seq* values;
5538
5539 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5540 int res;
5541 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5542 if (tmp == NULL) goto failed;
5543 res = obj2ast_boolop(tmp, &op, arena);
5544 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005545 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005546 } else {
5547 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5548 return 1;
5549 }
5550 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5551 int res;
5552 Py_ssize_t len;
5553 Py_ssize_t i;
5554 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5555 if (tmp == NULL) goto failed;
5556 if (!PyList_Check(tmp)) {
5557 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5558 goto failed;
5559 }
5560 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005561 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005562 if (values == NULL) goto failed;
5563 for (i = 0; i < len; i++) {
5564 expr_ty value;
5565 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5566 if (res != 0) goto failed;
5567 asdl_seq_SET(values, i, value);
5568 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005569 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005570 } else {
5571 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5572 return 1;
5573 }
5574 *out = BoolOp(op, values, lineno, col_offset, arena);
5575 if (*out == NULL) goto failed;
5576 return 0;
5577 }
5578 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5579 if (isinstance == -1) {
5580 return 1;
5581 }
5582 if (isinstance) {
5583 expr_ty left;
5584 operator_ty op;
5585 expr_ty right;
5586
5587 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5588 int res;
5589 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5590 if (tmp == NULL) goto failed;
5591 res = obj2ast_expr(tmp, &left, arena);
5592 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005593 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005594 } else {
5595 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5596 return 1;
5597 }
5598 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5599 int res;
5600 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5601 if (tmp == NULL) goto failed;
5602 res = obj2ast_operator(tmp, &op, arena);
5603 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005604 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005605 } else {
5606 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5607 return 1;
5608 }
5609 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5610 int res;
5611 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5612 if (tmp == NULL) goto failed;
5613 res = obj2ast_expr(tmp, &right, arena);
5614 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005615 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005616 } else {
5617 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5618 return 1;
5619 }
5620 *out = BinOp(left, op, right, lineno, col_offset, arena);
5621 if (*out == NULL) goto failed;
5622 return 0;
5623 }
5624 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5625 if (isinstance == -1) {
5626 return 1;
5627 }
5628 if (isinstance) {
5629 unaryop_ty op;
5630 expr_ty operand;
5631
5632 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5633 int res;
5634 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5635 if (tmp == NULL) goto failed;
5636 res = obj2ast_unaryop(tmp, &op, arena);
5637 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005638 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005639 } else {
5640 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5641 return 1;
5642 }
5643 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5644 int res;
5645 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5646 if (tmp == NULL) goto failed;
5647 res = obj2ast_expr(tmp, &operand, arena);
5648 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005649 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005650 } else {
5651 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5652 return 1;
5653 }
5654 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5655 if (*out == NULL) goto failed;
5656 return 0;
5657 }
5658 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5659 if (isinstance == -1) {
5660 return 1;
5661 }
5662 if (isinstance) {
5663 arguments_ty args;
5664 expr_ty body;
5665
5666 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5667 int res;
5668 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5669 if (tmp == NULL) goto failed;
5670 res = obj2ast_arguments(tmp, &args, 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 \"args\" missing from Lambda");
5675 return 1;
5676 }
5677 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5678 int res;
5679 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5680 if (tmp == NULL) goto failed;
5681 res = obj2ast_expr(tmp, &body, arena);
5682 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005683 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005684 } else {
5685 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5686 return 1;
5687 }
5688 *out = Lambda(args, body, lineno, col_offset, arena);
5689 if (*out == NULL) goto failed;
5690 return 0;
5691 }
5692 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5693 if (isinstance == -1) {
5694 return 1;
5695 }
5696 if (isinstance) {
5697 expr_ty test;
5698 expr_ty body;
5699 expr_ty orelse;
5700
5701 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5702 int res;
5703 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5704 if (tmp == NULL) goto failed;
5705 res = obj2ast_expr(tmp, &test, arena);
5706 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005707 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005708 } else {
5709 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5710 return 1;
5711 }
5712 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5713 int res;
5714 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5715 if (tmp == NULL) goto failed;
5716 res = obj2ast_expr(tmp, &body, arena);
5717 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005719 } else {
5720 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5721 return 1;
5722 }
5723 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5724 int res;
5725 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5726 if (tmp == NULL) goto failed;
5727 res = obj2ast_expr(tmp, &orelse, arena);
5728 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005729 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005730 } else {
5731 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5732 return 1;
5733 }
5734 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5735 if (*out == NULL) goto failed;
5736 return 0;
5737 }
5738 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5739 if (isinstance == -1) {
5740 return 1;
5741 }
5742 if (isinstance) {
5743 asdl_seq* keys;
5744 asdl_seq* values;
5745
5746 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5747 int res;
5748 Py_ssize_t len;
5749 Py_ssize_t i;
5750 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5751 if (tmp == NULL) goto failed;
5752 if (!PyList_Check(tmp)) {
5753 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5754 goto failed;
5755 }
5756 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005757 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005758 if (keys == NULL) goto failed;
5759 for (i = 0; i < len; i++) {
5760 expr_ty value;
5761 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5762 if (res != 0) goto failed;
5763 asdl_seq_SET(keys, i, value);
5764 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005765 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005766 } else {
5767 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5768 return 1;
5769 }
5770 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5771 int res;
5772 Py_ssize_t len;
5773 Py_ssize_t i;
5774 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5775 if (tmp == NULL) goto failed;
5776 if (!PyList_Check(tmp)) {
5777 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5778 goto failed;
5779 }
5780 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005781 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005782 if (values == NULL) goto failed;
5783 for (i = 0; i < len; i++) {
5784 expr_ty value;
5785 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5786 if (res != 0) goto failed;
5787 asdl_seq_SET(values, i, value);
5788 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005789 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005790 } else {
5791 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5792 return 1;
5793 }
5794 *out = Dict(keys, values, lineno, col_offset, arena);
5795 if (*out == NULL) goto failed;
5796 return 0;
5797 }
5798 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5799 if (isinstance == -1) {
5800 return 1;
5801 }
5802 if (isinstance) {
5803 asdl_seq* elts;
5804
5805 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5806 int res;
5807 Py_ssize_t len;
5808 Py_ssize_t i;
5809 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5810 if (tmp == NULL) goto failed;
5811 if (!PyList_Check(tmp)) {
5812 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5813 goto failed;
5814 }
5815 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005816 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005817 if (elts == NULL) goto failed;
5818 for (i = 0; i < len; i++) {
5819 expr_ty value;
5820 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5821 if (res != 0) goto failed;
5822 asdl_seq_SET(elts, i, value);
5823 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005824 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005825 } else {
5826 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5827 return 1;
5828 }
5829 *out = Set(elts, lineno, col_offset, arena);
5830 if (*out == NULL) goto failed;
5831 return 0;
5832 }
5833 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5834 if (isinstance == -1) {
5835 return 1;
5836 }
5837 if (isinstance) {
5838 expr_ty elt;
5839 asdl_seq* generators;
5840
5841 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5842 int res;
5843 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5844 if (tmp == NULL) goto failed;
5845 res = obj2ast_expr(tmp, &elt, arena);
5846 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005847 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005848 } else {
5849 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5850 return 1;
5851 }
5852 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5853 int res;
5854 Py_ssize_t len;
5855 Py_ssize_t i;
5856 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5857 if (tmp == NULL) goto failed;
5858 if (!PyList_Check(tmp)) {
5859 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5860 goto failed;
5861 }
5862 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005863 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005864 if (generators == NULL) goto failed;
5865 for (i = 0; i < len; i++) {
5866 comprehension_ty value;
5867 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5868 if (res != 0) goto failed;
5869 asdl_seq_SET(generators, i, value);
5870 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005871 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005872 } else {
5873 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5874 return 1;
5875 }
5876 *out = ListComp(elt, generators, lineno, col_offset, arena);
5877 if (*out == NULL) goto failed;
5878 return 0;
5879 }
5880 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5881 if (isinstance == -1) {
5882 return 1;
5883 }
5884 if (isinstance) {
5885 expr_ty elt;
5886 asdl_seq* generators;
5887
5888 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5889 int res;
5890 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5891 if (tmp == NULL) goto failed;
5892 res = obj2ast_expr(tmp, &elt, arena);
5893 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005894 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005895 } else {
5896 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5897 return 1;
5898 }
5899 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5900 int res;
5901 Py_ssize_t len;
5902 Py_ssize_t i;
5903 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5904 if (tmp == NULL) goto failed;
5905 if (!PyList_Check(tmp)) {
5906 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5907 goto failed;
5908 }
5909 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005910 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005911 if (generators == NULL) goto failed;
5912 for (i = 0; i < len; i++) {
5913 comprehension_ty value;
5914 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5915 if (res != 0) goto failed;
5916 asdl_seq_SET(generators, i, value);
5917 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005918 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005919 } else {
5920 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5921 return 1;
5922 }
5923 *out = SetComp(elt, generators, lineno, col_offset, arena);
5924 if (*out == NULL) goto failed;
5925 return 0;
5926 }
5927 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5928 if (isinstance == -1) {
5929 return 1;
5930 }
5931 if (isinstance) {
5932 expr_ty key;
5933 expr_ty value;
5934 asdl_seq* generators;
5935
5936 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5937 int res;
5938 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5939 if (tmp == NULL) goto failed;
5940 res = obj2ast_expr(tmp, &key, arena);
5941 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005942 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005943 } else {
5944 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5945 return 1;
5946 }
5947 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5948 int res;
5949 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5950 if (tmp == NULL) goto failed;
5951 res = obj2ast_expr(tmp, &value, arena);
5952 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005953 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005954 } else {
5955 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5956 return 1;
5957 }
5958 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5959 int res;
5960 Py_ssize_t len;
5961 Py_ssize_t i;
5962 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5963 if (tmp == NULL) goto failed;
5964 if (!PyList_Check(tmp)) {
5965 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5966 goto failed;
5967 }
5968 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005969 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005970 if (generators == NULL) goto failed;
5971 for (i = 0; i < len; i++) {
5972 comprehension_ty value;
5973 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5974 if (res != 0) goto failed;
5975 asdl_seq_SET(generators, i, value);
5976 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005977 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005978 } else {
5979 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5980 return 1;
5981 }
5982 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5983 if (*out == NULL) goto failed;
5984 return 0;
5985 }
5986 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5987 if (isinstance == -1) {
5988 return 1;
5989 }
5990 if (isinstance) {
5991 expr_ty elt;
5992 asdl_seq* generators;
5993
5994 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5995 int res;
5996 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5997 if (tmp == NULL) goto failed;
5998 res = obj2ast_expr(tmp, &elt, arena);
5999 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006000 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006001 } else {
6002 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6003 return 1;
6004 }
6005 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6006 int res;
6007 Py_ssize_t len;
6008 Py_ssize_t i;
6009 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6010 if (tmp == NULL) goto failed;
6011 if (!PyList_Check(tmp)) {
6012 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6013 goto failed;
6014 }
6015 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006016 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006017 if (generators == NULL) goto failed;
6018 for (i = 0; i < len; i++) {
6019 comprehension_ty value;
6020 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6021 if (res != 0) goto failed;
6022 asdl_seq_SET(generators, i, value);
6023 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006024 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006025 } else {
6026 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6027 return 1;
6028 }
6029 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6030 if (*out == NULL) goto failed;
6031 return 0;
6032 }
Yury Selivanov75445082015-05-11 22:57:16 -04006033 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6034 if (isinstance == -1) {
6035 return 1;
6036 }
6037 if (isinstance) {
6038 expr_ty value;
6039
6040 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6041 int res;
6042 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6043 if (tmp == NULL) goto failed;
6044 res = obj2ast_expr(tmp, &value, arena);
6045 if (res != 0) goto failed;
6046 Py_CLEAR(tmp);
6047 } else {
6048 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6049 return 1;
6050 }
6051 *out = Await(value, lineno, col_offset, arena);
6052 if (*out == NULL) goto failed;
6053 return 0;
6054 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006055 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6056 if (isinstance == -1) {
6057 return 1;
6058 }
6059 if (isinstance) {
6060 expr_ty value;
6061
Victor Stinneree4b59c2013-07-27 00:01:35 +02006062 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006063 int res;
6064 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6065 if (tmp == NULL) goto failed;
6066 res = obj2ast_expr(tmp, &value, arena);
6067 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006068 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006069 } else {
6070 value = NULL;
6071 }
6072 *out = Yield(value, lineno, col_offset, arena);
6073 if (*out == NULL) goto failed;
6074 return 0;
6075 }
6076 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6077 if (isinstance == -1) {
6078 return 1;
6079 }
6080 if (isinstance) {
6081 expr_ty value;
6082
6083 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6084 int res;
6085 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6086 if (tmp == NULL) goto failed;
6087 res = obj2ast_expr(tmp, &value, arena);
6088 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006089 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006090 } else {
6091 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6092 return 1;
6093 }
6094 *out = YieldFrom(value, lineno, col_offset, arena);
6095 if (*out == NULL) goto failed;
6096 return 0;
6097 }
6098 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6099 if (isinstance == -1) {
6100 return 1;
6101 }
6102 if (isinstance) {
6103 expr_ty left;
6104 asdl_int_seq* ops;
6105 asdl_seq* comparators;
6106
6107 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6108 int res;
6109 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6110 if (tmp == NULL) goto failed;
6111 res = obj2ast_expr(tmp, &left, arena);
6112 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006113 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006114 } else {
6115 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6116 return 1;
6117 }
6118 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6119 int res;
6120 Py_ssize_t len;
6121 Py_ssize_t i;
6122 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6123 if (tmp == NULL) goto failed;
6124 if (!PyList_Check(tmp)) {
6125 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6126 goto failed;
6127 }
6128 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006129 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006130 if (ops == NULL) goto failed;
6131 for (i = 0; i < len; i++) {
6132 cmpop_ty value;
6133 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6134 if (res != 0) goto failed;
6135 asdl_seq_SET(ops, i, value);
6136 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006137 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006138 } else {
6139 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6140 return 1;
6141 }
6142 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6143 int res;
6144 Py_ssize_t len;
6145 Py_ssize_t i;
6146 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6147 if (tmp == NULL) goto failed;
6148 if (!PyList_Check(tmp)) {
6149 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6150 goto failed;
6151 }
6152 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006153 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006154 if (comparators == NULL) goto failed;
6155 for (i = 0; i < len; i++) {
6156 expr_ty value;
6157 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6158 if (res != 0) goto failed;
6159 asdl_seq_SET(comparators, i, value);
6160 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006161 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006162 } else {
6163 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6164 return 1;
6165 }
6166 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6167 if (*out == NULL) goto failed;
6168 return 0;
6169 }
6170 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
6175 expr_ty func;
6176 asdl_seq* args;
6177 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006178
6179 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6180 int res;
6181 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6182 if (tmp == NULL) goto failed;
6183 res = obj2ast_expr(tmp, &func, arena);
6184 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006185 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006186 } else {
6187 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6188 return 1;
6189 }
6190 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6191 int res;
6192 Py_ssize_t len;
6193 Py_ssize_t i;
6194 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6195 if (tmp == NULL) goto failed;
6196 if (!PyList_Check(tmp)) {
6197 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6198 goto failed;
6199 }
6200 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006201 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006202 if (args == NULL) goto failed;
6203 for (i = 0; i < len; i++) {
6204 expr_ty value;
6205 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6206 if (res != 0) goto failed;
6207 asdl_seq_SET(args, i, value);
6208 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006209 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006210 } else {
6211 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6212 return 1;
6213 }
6214 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6215 int res;
6216 Py_ssize_t len;
6217 Py_ssize_t i;
6218 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6219 if (tmp == NULL) goto failed;
6220 if (!PyList_Check(tmp)) {
6221 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6222 goto failed;
6223 }
6224 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006225 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006226 if (keywords == NULL) goto failed;
6227 for (i = 0; i < len; i++) {
6228 keyword_ty value;
6229 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6230 if (res != 0) goto failed;
6231 asdl_seq_SET(keywords, i, value);
6232 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006233 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006234 } else {
6235 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6236 return 1;
6237 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006238 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006239 if (*out == NULL) goto failed;
6240 return 0;
6241 }
6242 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6243 if (isinstance == -1) {
6244 return 1;
6245 }
6246 if (isinstance) {
6247 object n;
6248
6249 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6250 int res;
6251 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6252 if (tmp == NULL) goto failed;
6253 res = obj2ast_object(tmp, &n, arena);
6254 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006255 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006256 } else {
6257 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6258 return 1;
6259 }
6260 *out = Num(n, lineno, col_offset, arena);
6261 if (*out == NULL) goto failed;
6262 return 0;
6263 }
6264 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6265 if (isinstance == -1) {
6266 return 1;
6267 }
6268 if (isinstance) {
6269 string s;
6270
6271 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6272 int res;
6273 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6274 if (tmp == NULL) goto failed;
6275 res = obj2ast_string(tmp, &s, arena);
6276 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006277 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006278 } else {
6279 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6280 return 1;
6281 }
6282 *out = Str(s, lineno, col_offset, arena);
6283 if (*out == NULL) goto failed;
6284 return 0;
6285 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006286 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6287 if (isinstance == -1) {
6288 return 1;
6289 }
6290 if (isinstance) {
6291 expr_ty value;
6292 int conversion;
6293 expr_ty format_spec;
6294
6295 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6296 int res;
6297 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6298 if (tmp == NULL) goto failed;
6299 res = obj2ast_expr(tmp, &value, arena);
6300 if (res != 0) goto failed;
6301 Py_CLEAR(tmp);
6302 } else {
6303 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6304 return 1;
6305 }
6306 if (exists_not_none(obj, &PyId_conversion)) {
6307 int res;
6308 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6309 if (tmp == NULL) goto failed;
6310 res = obj2ast_int(tmp, &conversion, arena);
6311 if (res != 0) goto failed;
6312 Py_CLEAR(tmp);
6313 } else {
6314 conversion = 0;
6315 }
6316 if (exists_not_none(obj, &PyId_format_spec)) {
6317 int res;
6318 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6319 if (tmp == NULL) goto failed;
6320 res = obj2ast_expr(tmp, &format_spec, arena);
6321 if (res != 0) goto failed;
6322 Py_CLEAR(tmp);
6323 } else {
6324 format_spec = NULL;
6325 }
6326 *out = FormattedValue(value, conversion, format_spec, lineno,
6327 col_offset, arena);
6328 if (*out == NULL) goto failed;
6329 return 0;
6330 }
6331 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6332 if (isinstance == -1) {
6333 return 1;
6334 }
6335 if (isinstance) {
6336 asdl_seq* values;
6337
6338 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6339 int res;
6340 Py_ssize_t len;
6341 Py_ssize_t i;
6342 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6343 if (tmp == NULL) goto failed;
6344 if (!PyList_Check(tmp)) {
6345 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6346 goto failed;
6347 }
6348 len = PyList_GET_SIZE(tmp);
6349 values = _Py_asdl_seq_new(len, arena);
6350 if (values == NULL) goto failed;
6351 for (i = 0; i < len; i++) {
6352 expr_ty value;
6353 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6354 if (res != 0) goto failed;
6355 asdl_seq_SET(values, i, value);
6356 }
6357 Py_CLEAR(tmp);
6358 } else {
6359 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6360 return 1;
6361 }
6362 *out = JoinedStr(values, lineno, col_offset, arena);
6363 if (*out == NULL) goto failed;
6364 return 0;
6365 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006366 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6367 if (isinstance == -1) {
6368 return 1;
6369 }
6370 if (isinstance) {
6371 bytes s;
6372
6373 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6374 int res;
6375 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6376 if (tmp == NULL) goto failed;
6377 res = obj2ast_bytes(tmp, &s, arena);
6378 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006379 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006380 } else {
6381 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6382 return 1;
6383 }
6384 *out = Bytes(s, lineno, col_offset, arena);
6385 if (*out == NULL) goto failed;
6386 return 0;
6387 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006388 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6389 if (isinstance == -1) {
6390 return 1;
6391 }
6392 if (isinstance) {
6393 singleton value;
6394
6395 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6396 int res;
6397 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6398 if (tmp == NULL) goto failed;
6399 res = obj2ast_singleton(tmp, &value, arena);
6400 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006401 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006402 } else {
6403 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6404 return 1;
6405 }
6406 *out = NameConstant(value, lineno, col_offset, arena);
6407 if (*out == NULL) goto failed;
6408 return 0;
6409 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006410 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6411 if (isinstance == -1) {
6412 return 1;
6413 }
6414 if (isinstance) {
6415
6416 *out = Ellipsis(lineno, col_offset, arena);
6417 if (*out == NULL) goto failed;
6418 return 0;
6419 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006420 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6421 if (isinstance == -1) {
6422 return 1;
6423 }
6424 if (isinstance) {
6425 constant value;
6426
6427 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6428 int res;
6429 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6430 if (tmp == NULL) goto failed;
6431 res = obj2ast_constant(tmp, &value, arena);
6432 if (res != 0) goto failed;
6433 Py_CLEAR(tmp);
6434 } else {
6435 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6436 return 1;
6437 }
6438 *out = Constant(value, lineno, col_offset, arena);
6439 if (*out == NULL) goto failed;
6440 return 0;
6441 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006442 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6443 if (isinstance == -1) {
6444 return 1;
6445 }
6446 if (isinstance) {
6447 expr_ty value;
6448 identifier attr;
6449 expr_context_ty ctx;
6450
6451 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6452 int res;
6453 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6454 if (tmp == NULL) goto failed;
6455 res = obj2ast_expr(tmp, &value, arena);
6456 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006457 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006458 } else {
6459 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6460 return 1;
6461 }
6462 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6463 int res;
6464 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6465 if (tmp == NULL) goto failed;
6466 res = obj2ast_identifier(tmp, &attr, arena);
6467 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006468 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006469 } else {
6470 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6471 return 1;
6472 }
6473 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6474 int res;
6475 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6476 if (tmp == NULL) goto failed;
6477 res = obj2ast_expr_context(tmp, &ctx, arena);
6478 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006479 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006480 } else {
6481 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6482 return 1;
6483 }
6484 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6485 if (*out == NULL) goto failed;
6486 return 0;
6487 }
6488 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6489 if (isinstance == -1) {
6490 return 1;
6491 }
6492 if (isinstance) {
6493 expr_ty value;
6494 slice_ty slice;
6495 expr_context_ty ctx;
6496
6497 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6498 int res;
6499 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6500 if (tmp == NULL) goto failed;
6501 res = obj2ast_expr(tmp, &value, arena);
6502 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006503 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 } else {
6505 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6506 return 1;
6507 }
6508 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6509 int res;
6510 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6511 if (tmp == NULL) goto failed;
6512 res = obj2ast_slice(tmp, &slice, arena);
6513 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006514 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006515 } else {
6516 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6517 return 1;
6518 }
6519 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6520 int res;
6521 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6522 if (tmp == NULL) goto failed;
6523 res = obj2ast_expr_context(tmp, &ctx, arena);
6524 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006525 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006526 } else {
6527 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6528 return 1;
6529 }
6530 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6531 if (*out == NULL) goto failed;
6532 return 0;
6533 }
6534 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6535 if (isinstance == -1) {
6536 return 1;
6537 }
6538 if (isinstance) {
6539 expr_ty value;
6540 expr_context_ty ctx;
6541
6542 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6543 int res;
6544 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6545 if (tmp == NULL) goto failed;
6546 res = obj2ast_expr(tmp, &value, arena);
6547 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006548 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006549 } else {
6550 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6551 return 1;
6552 }
6553 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6554 int res;
6555 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6556 if (tmp == NULL) goto failed;
6557 res = obj2ast_expr_context(tmp, &ctx, arena);
6558 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006559 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006560 } else {
6561 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6562 return 1;
6563 }
6564 *out = Starred(value, ctx, lineno, col_offset, arena);
6565 if (*out == NULL) goto failed;
6566 return 0;
6567 }
6568 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6569 if (isinstance == -1) {
6570 return 1;
6571 }
6572 if (isinstance) {
6573 identifier id;
6574 expr_context_ty ctx;
6575
6576 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6577 int res;
6578 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6579 if (tmp == NULL) goto failed;
6580 res = obj2ast_identifier(tmp, &id, arena);
6581 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006582 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 } else {
6584 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6585 return 1;
6586 }
6587 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6588 int res;
6589 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6590 if (tmp == NULL) goto failed;
6591 res = obj2ast_expr_context(tmp, &ctx, arena);
6592 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006593 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006594 } else {
6595 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6596 return 1;
6597 }
6598 *out = Name(id, ctx, lineno, col_offset, arena);
6599 if (*out == NULL) goto failed;
6600 return 0;
6601 }
6602 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6603 if (isinstance == -1) {
6604 return 1;
6605 }
6606 if (isinstance) {
6607 asdl_seq* elts;
6608 expr_context_ty ctx;
6609
6610 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6611 int res;
6612 Py_ssize_t len;
6613 Py_ssize_t i;
6614 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6615 if (tmp == NULL) goto failed;
6616 if (!PyList_Check(tmp)) {
6617 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6618 goto failed;
6619 }
6620 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006621 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006622 if (elts == NULL) goto failed;
6623 for (i = 0; i < len; i++) {
6624 expr_ty value;
6625 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6626 if (res != 0) goto failed;
6627 asdl_seq_SET(elts, i, value);
6628 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006629 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006630 } else {
6631 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6632 return 1;
6633 }
6634 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6635 int res;
6636 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6637 if (tmp == NULL) goto failed;
6638 res = obj2ast_expr_context(tmp, &ctx, arena);
6639 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006640 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006641 } else {
6642 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6643 return 1;
6644 }
6645 *out = List(elts, ctx, lineno, col_offset, arena);
6646 if (*out == NULL) goto failed;
6647 return 0;
6648 }
6649 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6650 if (isinstance == -1) {
6651 return 1;
6652 }
6653 if (isinstance) {
6654 asdl_seq* elts;
6655 expr_context_ty ctx;
6656
6657 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6658 int res;
6659 Py_ssize_t len;
6660 Py_ssize_t i;
6661 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6662 if (tmp == NULL) goto failed;
6663 if (!PyList_Check(tmp)) {
6664 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6665 goto failed;
6666 }
6667 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006668 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006669 if (elts == NULL) goto failed;
6670 for (i = 0; i < len; i++) {
6671 expr_ty value;
6672 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6673 if (res != 0) goto failed;
6674 asdl_seq_SET(elts, i, value);
6675 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006676 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006677 } else {
6678 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6679 return 1;
6680 }
6681 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6682 int res;
6683 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6684 if (tmp == NULL) goto failed;
6685 res = obj2ast_expr_context(tmp, &ctx, arena);
6686 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006687 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006688 } else {
6689 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6690 return 1;
6691 }
6692 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6693 if (*out == NULL) goto failed;
6694 return 0;
6695 }
6696
6697 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6698 failed:
6699 Py_XDECREF(tmp);
6700 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006701}
6702
6703int
6704obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6705{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006706 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006707
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006708 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6709 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006710 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006711 }
6712 if (isinstance) {
6713 *out = Load;
6714 return 0;
6715 }
6716 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6717 if (isinstance == -1) {
6718 return 1;
6719 }
6720 if (isinstance) {
6721 *out = Store;
6722 return 0;
6723 }
6724 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6725 if (isinstance == -1) {
6726 return 1;
6727 }
6728 if (isinstance) {
6729 *out = Del;
6730 return 0;
6731 }
6732 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6733 if (isinstance == -1) {
6734 return 1;
6735 }
6736 if (isinstance) {
6737 *out = AugLoad;
6738 return 0;
6739 }
6740 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6741 if (isinstance == -1) {
6742 return 1;
6743 }
6744 if (isinstance) {
6745 *out = AugStore;
6746 return 0;
6747 }
6748 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6749 if (isinstance == -1) {
6750 return 1;
6751 }
6752 if (isinstance) {
6753 *out = Param;
6754 return 0;
6755 }
6756
6757 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6758 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006759}
6760
6761int
6762obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6763{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006764 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006765
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006766 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006767
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006768 if (obj == Py_None) {
6769 *out = NULL;
6770 return 0;
6771 }
6772 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6773 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006774 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006775 }
6776 if (isinstance) {
6777 expr_ty lower;
6778 expr_ty upper;
6779 expr_ty step;
6780
Victor Stinneree4b59c2013-07-27 00:01:35 +02006781 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006782 int res;
6783 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6784 if (tmp == NULL) goto failed;
6785 res = obj2ast_expr(tmp, &lower, arena);
6786 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006787 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006788 } else {
6789 lower = NULL;
6790 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006791 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006792 int res;
6793 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6794 if (tmp == NULL) goto failed;
6795 res = obj2ast_expr(tmp, &upper, arena);
6796 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006798 } else {
6799 upper = NULL;
6800 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006801 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006802 int res;
6803 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6804 if (tmp == NULL) goto failed;
6805 res = obj2ast_expr(tmp, &step, arena);
6806 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006807 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006808 } else {
6809 step = NULL;
6810 }
6811 *out = Slice(lower, upper, step, arena);
6812 if (*out == NULL) goto failed;
6813 return 0;
6814 }
6815 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6816 if (isinstance == -1) {
6817 return 1;
6818 }
6819 if (isinstance) {
6820 asdl_seq* dims;
6821
6822 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6823 int res;
6824 Py_ssize_t len;
6825 Py_ssize_t i;
6826 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6827 if (tmp == NULL) goto failed;
6828 if (!PyList_Check(tmp)) {
6829 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6830 goto failed;
6831 }
6832 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006833 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006834 if (dims == NULL) goto failed;
6835 for (i = 0; i < len; i++) {
6836 slice_ty value;
6837 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6838 if (res != 0) goto failed;
6839 asdl_seq_SET(dims, i, value);
6840 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006841 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006842 } else {
6843 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6844 return 1;
6845 }
6846 *out = ExtSlice(dims, arena);
6847 if (*out == NULL) goto failed;
6848 return 0;
6849 }
6850 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6851 if (isinstance == -1) {
6852 return 1;
6853 }
6854 if (isinstance) {
6855 expr_ty value;
6856
6857 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6858 int res;
6859 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6860 if (tmp == NULL) goto failed;
6861 res = obj2ast_expr(tmp, &value, arena);
6862 if (res != 0) goto failed;
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 \"value\" missing from Index");
6866 return 1;
6867 }
6868 *out = Index(value, arena);
6869 if (*out == NULL) goto failed;
6870 return 0;
6871 }
6872
6873 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6874 failed:
6875 Py_XDECREF(tmp);
6876 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006877}
6878
6879int
6880obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6881{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006882 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006883
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006884 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6885 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006886 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006887 }
6888 if (isinstance) {
6889 *out = And;
6890 return 0;
6891 }
6892 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6893 if (isinstance == -1) {
6894 return 1;
6895 }
6896 if (isinstance) {
6897 *out = Or;
6898 return 0;
6899 }
6900
6901 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6902 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006903}
6904
6905int
6906obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6907{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006908 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006909
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006910 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6911 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006912 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006913 }
6914 if (isinstance) {
6915 *out = Add;
6916 return 0;
6917 }
6918 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6919 if (isinstance == -1) {
6920 return 1;
6921 }
6922 if (isinstance) {
6923 *out = Sub;
6924 return 0;
6925 }
6926 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6927 if (isinstance == -1) {
6928 return 1;
6929 }
6930 if (isinstance) {
6931 *out = Mult;
6932 return 0;
6933 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006934 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6935 if (isinstance == -1) {
6936 return 1;
6937 }
6938 if (isinstance) {
6939 *out = MatMult;
6940 return 0;
6941 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006942 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6943 if (isinstance == -1) {
6944 return 1;
6945 }
6946 if (isinstance) {
6947 *out = Div;
6948 return 0;
6949 }
6950 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6951 if (isinstance == -1) {
6952 return 1;
6953 }
6954 if (isinstance) {
6955 *out = Mod;
6956 return 0;
6957 }
6958 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6959 if (isinstance == -1) {
6960 return 1;
6961 }
6962 if (isinstance) {
6963 *out = Pow;
6964 return 0;
6965 }
6966 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6967 if (isinstance == -1) {
6968 return 1;
6969 }
6970 if (isinstance) {
6971 *out = LShift;
6972 return 0;
6973 }
6974 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6975 if (isinstance == -1) {
6976 return 1;
6977 }
6978 if (isinstance) {
6979 *out = RShift;
6980 return 0;
6981 }
6982 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6983 if (isinstance == -1) {
6984 return 1;
6985 }
6986 if (isinstance) {
6987 *out = BitOr;
6988 return 0;
6989 }
6990 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6991 if (isinstance == -1) {
6992 return 1;
6993 }
6994 if (isinstance) {
6995 *out = BitXor;
6996 return 0;
6997 }
6998 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6999 if (isinstance == -1) {
7000 return 1;
7001 }
7002 if (isinstance) {
7003 *out = BitAnd;
7004 return 0;
7005 }
7006 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7007 if (isinstance == -1) {
7008 return 1;
7009 }
7010 if (isinstance) {
7011 *out = FloorDiv;
7012 return 0;
7013 }
7014
7015 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7016 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007017}
7018
7019int
7020obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7021{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007022 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007023
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007024 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7025 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007026 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007027 }
7028 if (isinstance) {
7029 *out = Invert;
7030 return 0;
7031 }
7032 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7033 if (isinstance == -1) {
7034 return 1;
7035 }
7036 if (isinstance) {
7037 *out = Not;
7038 return 0;
7039 }
7040 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7041 if (isinstance == -1) {
7042 return 1;
7043 }
7044 if (isinstance) {
7045 *out = UAdd;
7046 return 0;
7047 }
7048 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7049 if (isinstance == -1) {
7050 return 1;
7051 }
7052 if (isinstance) {
7053 *out = USub;
7054 return 0;
7055 }
7056
7057 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7058 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007059}
7060
7061int
7062obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7063{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007064 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007065
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007066 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7067 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007068 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007069 }
7070 if (isinstance) {
7071 *out = Eq;
7072 return 0;
7073 }
7074 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7075 if (isinstance == -1) {
7076 return 1;
7077 }
7078 if (isinstance) {
7079 *out = NotEq;
7080 return 0;
7081 }
7082 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7083 if (isinstance == -1) {
7084 return 1;
7085 }
7086 if (isinstance) {
7087 *out = Lt;
7088 return 0;
7089 }
7090 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7091 if (isinstance == -1) {
7092 return 1;
7093 }
7094 if (isinstance) {
7095 *out = LtE;
7096 return 0;
7097 }
7098 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7099 if (isinstance == -1) {
7100 return 1;
7101 }
7102 if (isinstance) {
7103 *out = Gt;
7104 return 0;
7105 }
7106 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7107 if (isinstance == -1) {
7108 return 1;
7109 }
7110 if (isinstance) {
7111 *out = GtE;
7112 return 0;
7113 }
7114 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7115 if (isinstance == -1) {
7116 return 1;
7117 }
7118 if (isinstance) {
7119 *out = Is;
7120 return 0;
7121 }
7122 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7123 if (isinstance == -1) {
7124 return 1;
7125 }
7126 if (isinstance) {
7127 *out = IsNot;
7128 return 0;
7129 }
7130 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7131 if (isinstance == -1) {
7132 return 1;
7133 }
7134 if (isinstance) {
7135 *out = In;
7136 return 0;
7137 }
7138 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7139 if (isinstance == -1) {
7140 return 1;
7141 }
7142 if (isinstance) {
7143 *out = NotIn;
7144 return 0;
7145 }
7146
7147 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7148 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007149}
7150
7151int
7152obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7153{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007154 PyObject* tmp = NULL;
7155 expr_ty target;
7156 expr_ty iter;
7157 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007158 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007159
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007160 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7161 int res;
7162 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7163 if (tmp == NULL) goto failed;
7164 res = obj2ast_expr(tmp, &target, arena);
7165 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007166 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007167 } else {
7168 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007169 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007170 }
7171 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7172 int res;
7173 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7174 if (tmp == NULL) goto failed;
7175 res = obj2ast_expr(tmp, &iter, arena);
7176 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007177 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007178 } else {
7179 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7180 return 1;
7181 }
7182 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7183 int res;
7184 Py_ssize_t len;
7185 Py_ssize_t i;
7186 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7187 if (tmp == NULL) goto failed;
7188 if (!PyList_Check(tmp)) {
7189 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7190 goto failed;
7191 }
7192 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007193 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007194 if (ifs == NULL) goto failed;
7195 for (i = 0; i < len; i++) {
7196 expr_ty value;
7197 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7198 if (res != 0) goto failed;
7199 asdl_seq_SET(ifs, i, value);
7200 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007201 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007202 } else {
7203 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7204 return 1;
7205 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007206 if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7207 int res;
7208 tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7209 if (tmp == NULL) goto failed;
7210 res = obj2ast_int(tmp, &is_async, arena);
7211 if (res != 0) goto failed;
7212 Py_CLEAR(tmp);
7213 } else {
7214 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7215 return 1;
7216 }
7217 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007218 return 0;
7219failed:
7220 Py_XDECREF(tmp);
7221 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007222}
7223
7224int
7225obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7226{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007227 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007228
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007229 PyObject *tmp = NULL;
7230 int lineno;
7231 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007232
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007233 if (obj == Py_None) {
7234 *out = NULL;
7235 return 0;
7236 }
7237 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7238 int res;
7239 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7240 if (tmp == NULL) goto failed;
7241 res = obj2ast_int(tmp, &lineno, arena);
7242 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007243 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007244 } else {
7245 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007246 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007247 }
7248 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7249 int res;
7250 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7251 if (tmp == NULL) goto failed;
7252 res = obj2ast_int(tmp, &col_offset, arena);
7253 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007254 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007255 } else {
7256 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7257 return 1;
7258 }
7259 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7260 if (isinstance == -1) {
7261 return 1;
7262 }
7263 if (isinstance) {
7264 expr_ty type;
7265 identifier name;
7266 asdl_seq* body;
7267
Victor Stinneree4b59c2013-07-27 00:01:35 +02007268 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007269 int res;
7270 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7271 if (tmp == NULL) goto failed;
7272 res = obj2ast_expr(tmp, &type, arena);
7273 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007274 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007275 } else {
7276 type = NULL;
7277 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007278 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007279 int res;
7280 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7281 if (tmp == NULL) goto failed;
7282 res = obj2ast_identifier(tmp, &name, arena);
7283 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007284 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007285 } else {
7286 name = NULL;
7287 }
7288 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7289 int res;
7290 Py_ssize_t len;
7291 Py_ssize_t i;
7292 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7293 if (tmp == NULL) goto failed;
7294 if (!PyList_Check(tmp)) {
7295 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7296 goto failed;
7297 }
7298 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007299 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007300 if (body == NULL) goto failed;
7301 for (i = 0; i < len; i++) {
7302 stmt_ty value;
7303 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7304 if (res != 0) goto failed;
7305 asdl_seq_SET(body, i, value);
7306 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007307 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007308 } else {
7309 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7310 return 1;
7311 }
7312 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7313 if (*out == NULL) goto failed;
7314 return 0;
7315 }
7316
7317 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7318 failed:
7319 Py_XDECREF(tmp);
7320 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007321}
7322
7323int
7324obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7325{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 PyObject* tmp = NULL;
7327 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007328 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007329 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007330 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007331 arg_ty kwarg;
7332 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007333
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007334 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7335 int res;
7336 Py_ssize_t len;
7337 Py_ssize_t i;
7338 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7339 if (tmp == NULL) goto failed;
7340 if (!PyList_Check(tmp)) {
7341 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7342 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007343 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007344 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007345 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007346 if (args == NULL) goto failed;
7347 for (i = 0; i < len; i++) {
7348 arg_ty value;
7349 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7350 if (res != 0) goto failed;
7351 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007352 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007353 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007354 } else {
7355 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007356 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007357 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007358 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007359 int res;
7360 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7361 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007362 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007363 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007364 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007365 } else {
7366 vararg = NULL;
7367 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007368 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7369 int res;
7370 Py_ssize_t len;
7371 Py_ssize_t i;
7372 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7373 if (tmp == NULL) goto failed;
7374 if (!PyList_Check(tmp)) {
7375 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7376 goto failed;
7377 }
7378 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007379 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007380 if (kwonlyargs == NULL) goto failed;
7381 for (i = 0; i < len; i++) {
7382 arg_ty value;
7383 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7384 if (res != 0) goto failed;
7385 asdl_seq_SET(kwonlyargs, i, value);
7386 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007387 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007388 } else {
7389 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7390 return 1;
7391 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007392 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7393 int res;
7394 Py_ssize_t len;
7395 Py_ssize_t i;
7396 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7397 if (tmp == NULL) goto failed;
7398 if (!PyList_Check(tmp)) {
7399 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7400 goto failed;
7401 }
7402 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007403 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007404 if (kw_defaults == NULL) goto failed;
7405 for (i = 0; i < len; i++) {
7406 expr_ty value;
7407 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7408 if (res != 0) goto failed;
7409 asdl_seq_SET(kw_defaults, i, value);
7410 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007411 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007412 } else {
7413 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7414 return 1;
7415 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007416 if (exists_not_none(obj, &PyId_kwarg)) {
7417 int res;
7418 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7419 if (tmp == NULL) goto failed;
7420 res = obj2ast_arg(tmp, &kwarg, arena);
7421 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007422 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007423 } else {
7424 kwarg = NULL;
7425 }
7426 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7427 int res;
7428 Py_ssize_t len;
7429 Py_ssize_t i;
7430 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7431 if (tmp == NULL) goto failed;
7432 if (!PyList_Check(tmp)) {
7433 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7434 goto failed;
7435 }
7436 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007437 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007438 if (defaults == NULL) goto failed;
7439 for (i = 0; i < len; i++) {
7440 expr_ty value;
7441 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7442 if (res != 0) goto failed;
7443 asdl_seq_SET(defaults, i, value);
7444 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007445 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007446 } else {
7447 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7448 return 1;
7449 }
7450 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7451 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007452 return 0;
7453failed:
7454 Py_XDECREF(tmp);
7455 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007456}
7457
7458int
7459obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7460{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007461 PyObject* tmp = NULL;
7462 identifier arg;
7463 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007464 int lineno;
7465 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007466
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007467 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7468 int res;
7469 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7470 if (tmp == NULL) goto failed;
7471 res = obj2ast_identifier(tmp, &arg, arena);
7472 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007473 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007474 } else {
7475 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007476 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007477 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007478 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007479 int res;
7480 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7481 if (tmp == NULL) goto failed;
7482 res = obj2ast_expr(tmp, &annotation, arena);
7483 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007484 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007485 } else {
7486 annotation = NULL;
7487 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007488 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7489 int res;
7490 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7491 if (tmp == NULL) goto failed;
7492 res = obj2ast_int(tmp, &lineno, arena);
7493 if (res != 0) goto failed;
7494 Py_CLEAR(tmp);
7495 } else {
7496 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7497 return 1;
7498 }
7499 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7500 int res;
7501 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7502 if (tmp == NULL) goto failed;
7503 res = obj2ast_int(tmp, &col_offset, arena);
7504 if (res != 0) goto failed;
7505 Py_CLEAR(tmp);
7506 } else {
7507 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7508 return 1;
7509 }
7510 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007511 return 0;
7512failed:
7513 Py_XDECREF(tmp);
7514 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007515}
7516
7517int
7518obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7519{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007520 PyObject* tmp = NULL;
7521 identifier arg;
7522 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007523
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007524 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007525 int res;
7526 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7527 if (tmp == NULL) goto failed;
7528 res = obj2ast_identifier(tmp, &arg, arena);
7529 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007530 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007531 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007532 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007533 }
7534 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7535 int res;
7536 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7537 if (tmp == NULL) goto failed;
7538 res = obj2ast_expr(tmp, &value, arena);
7539 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007540 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007541 } else {
7542 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7543 return 1;
7544 }
7545 *out = keyword(arg, value, arena);
7546 return 0;
7547failed:
7548 Py_XDECREF(tmp);
7549 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007550}
7551
7552int
7553obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7554{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007555 PyObject* tmp = NULL;
7556 identifier name;
7557 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007558
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007559 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7560 int res;
7561 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7562 if (tmp == NULL) goto failed;
7563 res = obj2ast_identifier(tmp, &name, arena);
7564 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007565 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007566 } else {
7567 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007568 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007569 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007570 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007571 int res;
7572 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7573 if (tmp == NULL) goto failed;
7574 res = obj2ast_identifier(tmp, &asname, arena);
7575 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007576 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007577 } else {
7578 asname = NULL;
7579 }
7580 *out = alias(name, asname, arena);
7581 return 0;
7582failed:
7583 Py_XDECREF(tmp);
7584 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007585}
7586
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007587int
7588obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7589{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007590 PyObject* tmp = NULL;
7591 expr_ty context_expr;
7592 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007593
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007594 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7595 int res;
7596 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7597 if (tmp == NULL) goto failed;
7598 res = obj2ast_expr(tmp, &context_expr, arena);
7599 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007600 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007601 } else {
7602 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007603 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007604 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007605 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007606 int res;
7607 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7608 if (tmp == NULL) goto failed;
7609 res = obj2ast_expr(tmp, &optional_vars, arena);
7610 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007611 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007612 } else {
7613 optional_vars = NULL;
7614 }
7615 *out = withitem(context_expr, optional_vars, arena);
7616 return 0;
7617failed:
7618 Py_XDECREF(tmp);
7619 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007620}
7621
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007622
Martin v. Löwis1a214512008-06-11 05:26:20 +00007623static struct PyModuleDef _astmodule = {
7624 PyModuleDef_HEAD_INIT, "_ast"
7625};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007626PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007627PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007628{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007629 PyObject *m, *d;
7630 if (!init_types()) return NULL;
7631 m = PyModule_Create(&_astmodule);
7632 if (!m) return NULL;
7633 d = PyModule_GetDict(m);
7634 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007635 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007636 return NULL;
7637 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7638 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7639 NULL;
7640 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7641 0) return NULL;
7642 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7643 return NULL;
7644 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7645 NULL;
7646 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7647 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7648 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007649 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7650 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007651 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7652 return NULL;
7653 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7654 NULL;
7655 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7656 NULL;
7657 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7658 NULL;
7659 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7660 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007661 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7662 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007663 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007664 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7665 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007666 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7667 NULL;
7668 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7669 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007670 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7671 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007672 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7673 NULL;
7674 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7675 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7676 NULL;
7677 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7678 NULL;
7679 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7680 return NULL;
7681 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7682 NULL;
7683 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7684 return NULL;
7685 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7686 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7687 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7688 NULL;
7689 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7690 return NULL;
7691 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7692 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7693 NULL;
7694 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7695 NULL;
7696 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7697 NULL;
7698 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7699 NULL;
7700 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7701 NULL;
7702 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7703 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7704 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7705 return NULL;
7706 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7707 NULL;
7708 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7709 return NULL;
7710 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7711 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007712 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7713 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007714 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7715 NULL;
7716 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7717 return NULL;
7718 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7719 NULL;
7720 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7721 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7722 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007723 if (PyDict_SetItemString(d, "FormattedValue",
7724 (PyObject*)FormattedValue_type) < 0) return NULL;
7725 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7726 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007727 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7728 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007729 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7730 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007731 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7732 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007733 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7734 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007735 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7736 return NULL;
7737 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7738 return NULL;
7739 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7740 NULL;
7741 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7742 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7743 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7744 NULL;
7745 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7746 0) return NULL;
7747 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7748 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7749 NULL;
7750 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7751 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7752 NULL;
7753 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7754 return NULL;
7755 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7756 NULL;
7757 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7758 NULL;
7759 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7760 NULL;
7761 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7762 return NULL;
7763 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7764 NULL;
7765 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7766 NULL;
7767 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7768 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7769 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7770 return NULL;
7771 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7772 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7773 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007774 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7775 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007776 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7777 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7778 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7779 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7780 NULL;
7781 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7782 NULL;
7783 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7784 NULL;
7785 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7786 NULL;
7787 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7788 NULL;
7789 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7790 return NULL;
7791 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7792 NULL;
7793 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7794 NULL;
7795 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7796 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7797 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7798 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7799 NULL;
7800 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7801 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7802 NULL;
7803 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7804 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7805 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7806 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7807 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7808 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7809 NULL;
7810 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7811 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7812 NULL;
7813 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7814 < 0) return NULL;
7815 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7816 < 0) return NULL;
7817 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7818 < 0) return NULL;
7819 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7820 return NULL;
7821 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7822 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7823 NULL;
7824 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7825 NULL;
7826 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7827 return NULL;
7828 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007829}
7830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007831
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007832PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007833{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007834 if (!init_types())
7835 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007836 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007837}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007838
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007839/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7840mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007841{
7842 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007843 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007844 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007845 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007846
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007847 req_type[0] = (PyObject*)Module_type;
7848 req_type[1] = (PyObject*)Expression_type;
7849 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007850
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007851 assert(0 <= mode && mode <= 2);
7852
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007853 if (!init_types())
7854 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007855
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007856 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7857 if (isinstance == -1)
7858 return NULL;
7859 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007860 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7861 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007862 return NULL;
7863 }
7864 if (obj2ast_mod(ast, &res, arena) != 0)
7865 return NULL;
7866 else
7867 return res;
7868}
7869
7870int PyAST_Check(PyObject* obj)
7871{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007872 if (!init_types())
7873 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007874 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007875}
7876
Martin v. Löwis5b222132007-06-10 09:51:05 +00007877