blob: 2759b2fe9c4b33fa2470e6391f695257984edf24 [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);
INADA Naokicb41b272017-02-23 00:31:59 +090013_Py_IDENTIFIER(docstring);
Neal Norwitz53d960c2006-02-28 22:47:29 +000014static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020015 "body",
INADA Naokicb41b272017-02-23 00:31:59 +090016 "docstring",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017};
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Interactive_type;
19static char *Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020020 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000021};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Expression_type;
23static char *Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020024 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000025};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Suite_type;
27static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020028 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000029};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020031_Py_IDENTIFIER(lineno);
32_Py_IDENTIFIER(col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000033static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020034 "lineno",
35 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000037static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000038static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020039_Py_IDENTIFIER(name);
40_Py_IDENTIFIER(args);
41_Py_IDENTIFIER(decorator_list);
42_Py_IDENTIFIER(returns);
Neal Norwitz53d960c2006-02-28 22:47:29 +000043static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020044 "name",
45 "args",
46 "body",
47 "decorator_list",
48 "returns",
INADA Naokicb41b272017-02-23 00:31:59 +090049 "docstring",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000050};
Yury Selivanov75445082015-05-11 22:57:16 -040051static PyTypeObject *AsyncFunctionDef_type;
52static char *AsyncFunctionDef_fields[]={
53 "name",
54 "args",
55 "body",
56 "decorator_list",
57 "returns",
INADA Naokicb41b272017-02-23 00:31:59 +090058 "docstring",
Yury Selivanov75445082015-05-11 22:57:16 -040059};
Neal Norwitz53d960c2006-02-28 22:47:29 +000060static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020061_Py_IDENTIFIER(bases);
62_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000063static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020064 "name",
65 "bases",
66 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020067 "body",
68 "decorator_list",
INADA Naokicb41b272017-02-23 00:31:59 +090069 "docstring",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075};
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020077_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000078static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020079 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *Assign_type;
82static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020083 "targets",
84 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000085};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020087_Py_IDENTIFIER(target);
88_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000089static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020090 "target",
91 "op",
92 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000093};
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070094static PyTypeObject *AnnAssign_type;
95_Py_IDENTIFIER(annotation);
96_Py_IDENTIFIER(simple);
97static char *AnnAssign_fields[]={
98 "target",
99 "annotation",
100 "value",
101 "simple",
102};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000103static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200104_Py_IDENTIFIER(iter);
105_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000106static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200107 "target",
108 "iter",
109 "body",
110 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000111};
Yury Selivanov75445082015-05-11 22:57:16 -0400112static PyTypeObject *AsyncFor_type;
113static char *AsyncFor_fields[]={
114 "target",
115 "iter",
116 "body",
117 "orelse",
118};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000119static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200120_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200122 "test",
123 "body",
124 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *If_type;
127static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200128 "test",
129 "body",
130 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000131};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000132static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200133_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000134static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200135 "items",
136 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000137};
Yury Selivanov75445082015-05-11 22:57:16 -0400138static PyTypeObject *AsyncWith_type;
139static char *AsyncWith_fields[]={
140 "items",
141 "body",
142};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000143static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200144_Py_IDENTIFIER(exc);
145_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000146static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200147 "exc",
148 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000149};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500150static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200151_Py_IDENTIFIER(handlers);
152_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500153static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200154 "body",
155 "handlers",
156 "orelse",
157 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200160_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000161static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200162 "test",
163 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200166_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000167static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200168 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200171_Py_IDENTIFIER(module);
172_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000173static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200174 "module",
175 "names",
176 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000177};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000178static PyTypeObject *Global_type;
179static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200180 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000181};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000182static PyTypeObject *Nonlocal_type;
183static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200184 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000185};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000186static PyTypeObject *Expr_type;
187static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200188 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000189};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000190static PyTypeObject *Pass_type;
191static PyTypeObject *Break_type;
192static PyTypeObject *Continue_type;
193static PyTypeObject *expr_type;
194static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200195 "lineno",
196 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000197};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000199static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200200_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200202 "op",
203 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200206_Py_IDENTIFIER(left);
207_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000208static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200209 "left",
210 "op",
211 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000212};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000213static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200214_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200216 "op",
217 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *Lambda_type;
220static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200221 "args",
222 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000223};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000224static PyTypeObject *IfExp_type;
225static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200226 "test",
227 "body",
228 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000229};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000230static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200231_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000232static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200233 "keys",
234 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000235};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000236static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200237_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000238static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200239 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000240};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000241static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200242_Py_IDENTIFIER(elt);
243_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000244static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200245 "elt",
246 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000247};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000248static PyTypeObject *SetComp_type;
249static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200250 "elt",
251 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000252};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000253static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200254_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000255static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200256 "key",
257 "value",
258 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000259};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000260static PyTypeObject *GeneratorExp_type;
261static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200262 "elt",
263 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000264};
Yury Selivanov75445082015-05-11 22:57:16 -0400265static PyTypeObject *Await_type;
266static char *Await_fields[]={
267 "value",
268};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000269static PyTypeObject *Yield_type;
270static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200271 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500272};
273static PyTypeObject *YieldFrom_type;
274static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200275 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000276};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200278_Py_IDENTIFIER(ops);
279_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000280static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200281 "left",
282 "ops",
283 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000284};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200286_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000287static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200288 "func",
289 "args",
290 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200293_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000294static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200295 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000296};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000297static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200298_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000299static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200300 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301};
Eric V. Smith235a6f02015-09-19 14:51:32 -0400302static PyTypeObject *FormattedValue_type;
303_Py_IDENTIFIER(conversion);
304_Py_IDENTIFIER(format_spec);
305static char *FormattedValue_fields[]={
306 "value",
307 "conversion",
308 "format_spec",
309};
310static PyTypeObject *JoinedStr_type;
311static char *JoinedStr_fields[]={
312 "values",
313};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000314static PyTypeObject *Bytes_type;
315static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200316 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000317};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500318static PyTypeObject *NameConstant_type;
319static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200320 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500321};
Georg Brandl52318d62006-09-06 07:06:08 +0000322static PyTypeObject *Ellipsis_type;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100323static PyTypeObject *Constant_type;
324static char *Constant_fields[]={
325 "value",
326};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200328_Py_IDENTIFIER(attr);
329_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000330static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200331 "value",
332 "attr",
333 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000335static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200336_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000337static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200338 "value",
339 "slice",
340 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000341};
Guido van Rossum0368b722007-05-11 16:50:42 +0000342static PyTypeObject *Starred_type;
343static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200344 "value",
345 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000346};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000347static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200348_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200350 "id",
351 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *List_type;
354static char *List_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 *Tuple_type;
359static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200360 "elts",
361 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000363static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000364static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
365*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
366static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000367static PyTypeObject *Load_type;
368static PyTypeObject *Store_type;
369static PyTypeObject *Del_type;
370static PyTypeObject *AugLoad_type;
371static PyTypeObject *AugStore_type;
372static PyTypeObject *Param_type;
373static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000375static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200376_Py_IDENTIFIER(lower);
377_Py_IDENTIFIER(upper);
378_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000379static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200380 "lower",
381 "upper",
382 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000383};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200385_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000386static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200387 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000388};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000389static PyTypeObject *Index_type;
390static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200391 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000392};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000393static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000394static PyObject *And_singleton, *Or_singleton;
395static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000396static PyTypeObject *And_type;
397static PyTypeObject *Or_type;
398static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000399static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400400*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
401*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
402*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000403static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000404static PyTypeObject *Add_type;
405static PyTypeObject *Sub_type;
406static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400407static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000408static PyTypeObject *Div_type;
409static PyTypeObject *Mod_type;
410static PyTypeObject *Pow_type;
411static PyTypeObject *LShift_type;
412static PyTypeObject *RShift_type;
413static PyTypeObject *BitOr_type;
414static PyTypeObject *BitXor_type;
415static PyTypeObject *BitAnd_type;
416static PyTypeObject *FloorDiv_type;
417static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000418static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
419*USub_singleton;
420static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000421static PyTypeObject *Invert_type;
422static PyTypeObject *Not_type;
423static PyTypeObject *UAdd_type;
424static PyTypeObject *USub_type;
425static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000426static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
427*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
428*NotIn_singleton;
429static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000430static PyTypeObject *Eq_type;
431static PyTypeObject *NotEq_type;
432static PyTypeObject *Lt_type;
433static PyTypeObject *LtE_type;
434static PyTypeObject *Gt_type;
435static PyTypeObject *GtE_type;
436static PyTypeObject *Is_type;
437static PyTypeObject *IsNot_type;
438static PyTypeObject *In_type;
439static PyTypeObject *NotIn_type;
440static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000441static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200442_Py_IDENTIFIER(ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700443_Py_IDENTIFIER(is_async);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000444static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200445 "target",
446 "iter",
447 "ifs",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700448 "is_async",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000449};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000450static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000451static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200452 "lineno",
453 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000454};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000455static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000456static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200457_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000458static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200459 "type",
460 "name",
461 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000462};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000463static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000464static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200465_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200466_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200467_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700468_Py_IDENTIFIER(kwarg);
469_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000470static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200471 "args",
472 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200473 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200474 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200475 "kwarg",
476 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000477};
Neal Norwitzc1505362006-12-28 06:47:50 +0000478static PyTypeObject *arg_type;
479static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700480static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200481 "lineno",
482 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700483};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200484_Py_IDENTIFIER(arg);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000485static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200486 "arg",
487 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000488};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000489static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000490static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000491static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200492 "arg",
493 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000494};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000495static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000496static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200497_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000498static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200499 "name",
500 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000501};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500502static PyTypeObject *withitem_type;
503static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200504_Py_IDENTIFIER(context_expr);
505_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500506static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200507 "context_expr",
508 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500509};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000510
511
INADA Naokifc489082017-01-25 22:33:43 +0900512_Py_IDENTIFIER(_fields);
513_Py_IDENTIFIER(_attributes);
514
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700515typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100516 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700517 PyObject *dict;
518} AST_object;
519
Benjamin Peterson1767e022012-03-14 21:50:29 -0500520static void
521ast_dealloc(AST_object *self)
522{
523 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200524 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500525}
526
Neal Norwitz207c9f32008-03-31 04:42:11 +0000527static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700528ast_traverse(AST_object *self, visitproc visit, void *arg)
529{
530 Py_VISIT(self->dict);
531 return 0;
532}
533
534static void
535ast_clear(AST_object *self)
536{
537 Py_CLEAR(self->dict);
538}
539
540static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000541ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
542{
543 Py_ssize_t i, numfields = 0;
544 int res = -1;
545 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200546 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000547 if (!fields)
548 PyErr_Clear();
549 if (fields) {
550 numfields = PySequence_Size(fields);
551 if (numfields == -1)
552 goto cleanup;
553 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900554
Neal Norwitz207c9f32008-03-31 04:42:11 +0000555 res = 0; /* if no error occurs, this stays 0 to the end */
INADA Naoki4c78c522017-02-24 02:48:17 +0900556 if (numfields < PyTuple_GET_SIZE(args)) {
557 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
558 "%zd positional argument%s",
559 Py_TYPE(self)->tp_name,
560 numfields, numfields == 1 ? "" : "s");
561 res = -1;
562 goto cleanup;
563 }
564 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
565 /* cannot be reached when fields is NULL */
566 PyObject *name = PySequence_GetItem(fields, i);
567 if (!name) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000568 res = -1;
569 goto cleanup;
570 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900571 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
572 Py_DECREF(name);
573 if (res < 0)
574 goto cleanup;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000575 }
576 if (kw) {
577 i = 0; /* needed by PyDict_Next */
578 while (PyDict_Next(kw, &i, &key, &value)) {
579 res = PyObject_SetAttr(self, key, value);
580 if (res < 0)
581 goto cleanup;
582 }
583 }
584 cleanup:
585 Py_XDECREF(fields);
586 return res;
587}
588
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000589/* Pickling support */
590static PyObject *
591ast_type_reduce(PyObject *self, PyObject *unused)
592{
593 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200594 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200595 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000596 if (dict == NULL) {
597 if (PyErr_ExceptionMatches(PyExc_AttributeError))
598 PyErr_Clear();
599 else
600 return NULL;
601 }
602 if (dict) {
603 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
604 Py_DECREF(dict);
605 return res;
606 }
607 return Py_BuildValue("O()", Py_TYPE(self));
608}
609
610static PyMethodDef ast_type_methods[] = {
611 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
612 {NULL}
613};
614
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700615static PyGetSetDef ast_type_getsets[] = {
616 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
617 {NULL}
618};
619
Neal Norwitz207c9f32008-03-31 04:42:11 +0000620static PyTypeObject AST_type = {
621 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000622 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700623 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000624 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500625 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000626 0, /* tp_print */
627 0, /* tp_getattr */
628 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000629 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000630 0, /* tp_repr */
631 0, /* tp_as_number */
632 0, /* tp_as_sequence */
633 0, /* tp_as_mapping */
634 0, /* tp_hash */
635 0, /* tp_call */
636 0, /* tp_str */
637 PyObject_GenericGetAttr, /* tp_getattro */
638 PyObject_GenericSetAttr, /* tp_setattro */
639 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700640 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700642 (traverseproc)ast_traverse, /* tp_traverse */
643 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000644 0, /* tp_richcompare */
645 0, /* tp_weaklistoffset */
646 0, /* tp_iter */
647 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000648 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000649 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700650 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000651 0, /* tp_base */
652 0, /* tp_dict */
653 0, /* tp_descr_get */
654 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700655 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000656 (initproc)ast_type_init, /* tp_init */
657 PyType_GenericAlloc, /* tp_alloc */
658 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700659 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000660};
661
662
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000663static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
664{
INADA Naokifc489082017-01-25 22:33:43 +0900665 _Py_IDENTIFIER(__module__);
666 _Py_IDENTIFIER(_ast);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000667 PyObject *fnames, *result;
668 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000669 fnames = PyTuple_New(num_fields);
670 if (!fnames) return NULL;
671 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000672 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 if (!field) {
674 Py_DECREF(fnames);
675 return NULL;
676 }
677 PyTuple_SET_ITEM(fnames, i, field);
678 }
INADA Naokifc489082017-01-25 22:33:43 +0900679 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
680 type, base,
681 _PyUnicode_FromId(&PyId__fields), fnames,
682 _PyUnicode_FromId(&PyId___module__),
683 _PyUnicode_FromId(&PyId__ast));
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 Py_DECREF(fnames);
685 return (PyTypeObject*)result;
686}
687
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
689{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000691 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000692 if (!l)
693 return 0;
694 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000695 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!s) {
697 Py_DECREF(l);
698 return 0;
699 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000700 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200702 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 Py_DECREF(l);
704 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705}
706
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000707/* Conversion AST -> Python */
708
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
710{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700711 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 PyObject *result = PyList_New(n);
713 PyObject *value;
714 if (!result)
715 return NULL;
716 for (i = 0; i < n; i++) {
717 value = func(asdl_seq_GET(seq, i));
718 if (!value) {
719 Py_DECREF(result);
720 return NULL;
721 }
722 PyList_SET_ITEM(result, i, value);
723 }
724 return result;
725}
726
727static PyObject* ast2obj_object(void *o)
728{
729 if (!o)
730 o = Py_None;
731 Py_INCREF((PyObject*)o);
732 return (PyObject*)o;
733}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500734#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100735#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000736#define ast2obj_identifier ast2obj_object
737#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500738#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000740static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741{
Christian Heimes217cfd12007-12-02 14:31:20 +0000742 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743}
744
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000745/* Conversion Python -> AST */
746
Benjamin Peterson442f2092012-12-06 17:41:04 -0500747static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
748{
749 if (obj != Py_None && obj != Py_True && obj != Py_False) {
750 PyErr_SetString(PyExc_ValueError,
751 "AST singleton must be True, False, or None");
752 return 1;
753 }
754 *out = obj;
755 return 0;
756}
757
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000758static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
759{
760 if (obj == Py_None)
761 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200762 if (obj) {
763 if (PyArena_AddPyObject(arena, obj) < 0) {
764 *out = NULL;
765 return -1;
766 }
767 Py_INCREF(obj);
768 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000769 *out = obj;
770 return 0;
771}
772
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100773static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
774{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100775 if (obj) {
776 if (PyArena_AddPyObject(arena, obj) < 0) {
777 *out = NULL;
778 return -1;
779 }
780 Py_INCREF(obj);
781 }
782 *out = obj;
783 return 0;
784}
785
Benjamin Peterson180e6352011-07-22 11:09:07 -0500786static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500787{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500788 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
789 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500790 return 1;
791 }
792 return obj2ast_object(obj, out, arena);
793}
794
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500795static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
796{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400797 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500798 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
799 return 1;
800 }
801 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500802}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000803
Benjamin Petersone2498412011-08-09 16:08:39 -0500804static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
805{
806 if (!PyBytes_CheckExact(obj)) {
807 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
808 return 1;
809 }
810 return obj2ast_object(obj, out, arena);
811}
812
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000813static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
814{
815 int i;
816 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100817 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000818 return 1;
819 }
820
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300821 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000822 if (i == -1 && PyErr_Occurred())
823 return 1;
824 *out = i;
825 return 0;
826}
827
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000828static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000829{
830 PyObject *empty_tuple, *d;
831 if (PyType_Ready(&AST_type) < 0)
832 return -1;
833 d = AST_type.tp_dict;
834 empty_tuple = PyTuple_New(0);
835 if (!empty_tuple ||
INADA Naokifc489082017-01-25 22:33:43 +0900836 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
837 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +0000838 Py_XDECREF(empty_tuple);
839 return -1;
840 }
841 Py_DECREF(empty_tuple);
842 return 0;
843}
844
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700845static int exists_not_none(PyObject *obj, _Py_Identifier *id)
846{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700847 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700848 PyObject *attr = _PyObject_GetAttrId(obj, id);
849 if (!attr) {
850 PyErr_Clear();
851 return 0;
852 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700853 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700854 Py_DECREF(attr);
855 return !isnone;
856}
857
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000858
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859static int init_types(void)
860{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200861 static int initialized;
862 if (initialized) return 1;
863 if (add_ast_fields() < 0) return 0;
864 mod_type = make_type("mod", &AST_type, NULL, 0);
865 if (!mod_type) return 0;
866 if (!add_attributes(mod_type, NULL, 0)) return 0;
INADA Naokicb41b272017-02-23 00:31:59 +0900867 Module_type = make_type("Module", mod_type, Module_fields, 2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200868 if (!Module_type) return 0;
869 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
870 1);
871 if (!Interactive_type) return 0;
872 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
873 if (!Expression_type) return 0;
874 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
875 if (!Suite_type) return 0;
876 stmt_type = make_type("stmt", &AST_type, NULL, 0);
877 if (!stmt_type) return 0;
878 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
879 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
INADA Naokicb41b272017-02-23 00:31:59 +0900880 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200881 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400882 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
INADA Naokicb41b272017-02-23 00:31:59 +0900883 AsyncFunctionDef_fields, 6);
Yury Selivanov75445082015-05-11 22:57:16 -0400884 if (!AsyncFunctionDef_type) return 0;
INADA Naokicb41b272017-02-23 00:31:59 +0900885 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200886 if (!ClassDef_type) return 0;
887 Return_type = make_type("Return", stmt_type, Return_fields, 1);
888 if (!Return_type) return 0;
889 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
890 if (!Delete_type) return 0;
891 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
892 if (!Assign_type) return 0;
893 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
894 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700895 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
896 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200897 For_type = make_type("For", stmt_type, For_fields, 4);
898 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400899 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
900 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200901 While_type = make_type("While", stmt_type, While_fields, 3);
902 if (!While_type) return 0;
903 If_type = make_type("If", stmt_type, If_fields, 3);
904 if (!If_type) return 0;
905 With_type = make_type("With", stmt_type, With_fields, 2);
906 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400907 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
908 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200909 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
910 if (!Raise_type) return 0;
911 Try_type = make_type("Try", stmt_type, Try_fields, 4);
912 if (!Try_type) return 0;
913 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
914 if (!Assert_type) return 0;
915 Import_type = make_type("Import", stmt_type, Import_fields, 1);
916 if (!Import_type) return 0;
917 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
918 if (!ImportFrom_type) return 0;
919 Global_type = make_type("Global", stmt_type, Global_fields, 1);
920 if (!Global_type) return 0;
921 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
922 if (!Nonlocal_type) return 0;
923 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
924 if (!Expr_type) return 0;
925 Pass_type = make_type("Pass", stmt_type, NULL, 0);
926 if (!Pass_type) return 0;
927 Break_type = make_type("Break", stmt_type, NULL, 0);
928 if (!Break_type) return 0;
929 Continue_type = make_type("Continue", stmt_type, NULL, 0);
930 if (!Continue_type) return 0;
931 expr_type = make_type("expr", &AST_type, NULL, 0);
932 if (!expr_type) return 0;
933 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
934 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
935 if (!BoolOp_type) return 0;
936 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
937 if (!BinOp_type) return 0;
938 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
939 if (!UnaryOp_type) return 0;
940 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
941 if (!Lambda_type) return 0;
942 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
943 if (!IfExp_type) return 0;
944 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
945 if (!Dict_type) return 0;
946 Set_type = make_type("Set", expr_type, Set_fields, 1);
947 if (!Set_type) return 0;
948 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
949 if (!ListComp_type) return 0;
950 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
951 if (!SetComp_type) return 0;
952 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
953 if (!DictComp_type) return 0;
954 GeneratorExp_type = make_type("GeneratorExp", expr_type,
955 GeneratorExp_fields, 2);
956 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400957 Await_type = make_type("Await", expr_type, Await_fields, 1);
958 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200959 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
960 if (!Yield_type) return 0;
961 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
962 if (!YieldFrom_type) return 0;
963 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
964 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400965 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200966 if (!Call_type) return 0;
967 Num_type = make_type("Num", expr_type, Num_fields, 1);
968 if (!Num_type) return 0;
969 Str_type = make_type("Str", expr_type, Str_fields, 1);
970 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400971 FormattedValue_type = make_type("FormattedValue", expr_type,
972 FormattedValue_fields, 3);
973 if (!FormattedValue_type) return 0;
974 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
975 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200976 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
977 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200978 NameConstant_type = make_type("NameConstant", expr_type,
979 NameConstant_fields, 1);
980 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200981 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
982 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100983 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
984 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200985 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
986 if (!Attribute_type) return 0;
987 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
988 if (!Subscript_type) return 0;
989 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
990 if (!Starred_type) return 0;
991 Name_type = make_type("Name", expr_type, Name_fields, 2);
992 if (!Name_type) return 0;
993 List_type = make_type("List", expr_type, List_fields, 2);
994 if (!List_type) return 0;
995 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
996 if (!Tuple_type) return 0;
997 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
998 if (!expr_context_type) return 0;
999 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
1000 Load_type = make_type("Load", expr_context_type, NULL, 0);
1001 if (!Load_type) return 0;
1002 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
1003 if (!Load_singleton) return 0;
1004 Store_type = make_type("Store", expr_context_type, NULL, 0);
1005 if (!Store_type) return 0;
1006 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
1007 if (!Store_singleton) return 0;
1008 Del_type = make_type("Del", expr_context_type, NULL, 0);
1009 if (!Del_type) return 0;
1010 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1011 if (!Del_singleton) return 0;
1012 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1013 if (!AugLoad_type) return 0;
1014 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1015 if (!AugLoad_singleton) return 0;
1016 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1017 if (!AugStore_type) return 0;
1018 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1019 if (!AugStore_singleton) return 0;
1020 Param_type = make_type("Param", expr_context_type, NULL, 0);
1021 if (!Param_type) return 0;
1022 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1023 if (!Param_singleton) return 0;
1024 slice_type = make_type("slice", &AST_type, NULL, 0);
1025 if (!slice_type) return 0;
1026 if (!add_attributes(slice_type, NULL, 0)) return 0;
1027 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1028 if (!Slice_type) return 0;
1029 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1030 if (!ExtSlice_type) return 0;
1031 Index_type = make_type("Index", slice_type, Index_fields, 1);
1032 if (!Index_type) return 0;
1033 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1034 if (!boolop_type) return 0;
1035 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1036 And_type = make_type("And", boolop_type, NULL, 0);
1037 if (!And_type) return 0;
1038 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1039 if (!And_singleton) return 0;
1040 Or_type = make_type("Or", boolop_type, NULL, 0);
1041 if (!Or_type) return 0;
1042 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1043 if (!Or_singleton) return 0;
1044 operator_type = make_type("operator", &AST_type, NULL, 0);
1045 if (!operator_type) return 0;
1046 if (!add_attributes(operator_type, NULL, 0)) return 0;
1047 Add_type = make_type("Add", operator_type, NULL, 0);
1048 if (!Add_type) return 0;
1049 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1050 if (!Add_singleton) return 0;
1051 Sub_type = make_type("Sub", operator_type, NULL, 0);
1052 if (!Sub_type) return 0;
1053 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1054 if (!Sub_singleton) return 0;
1055 Mult_type = make_type("Mult", operator_type, NULL, 0);
1056 if (!Mult_type) return 0;
1057 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1058 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001059 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1060 if (!MatMult_type) return 0;
1061 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1062 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001063 Div_type = make_type("Div", operator_type, NULL, 0);
1064 if (!Div_type) return 0;
1065 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1066 if (!Div_singleton) return 0;
1067 Mod_type = make_type("Mod", operator_type, NULL, 0);
1068 if (!Mod_type) return 0;
1069 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1070 if (!Mod_singleton) return 0;
1071 Pow_type = make_type("Pow", operator_type, NULL, 0);
1072 if (!Pow_type) return 0;
1073 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1074 if (!Pow_singleton) return 0;
1075 LShift_type = make_type("LShift", operator_type, NULL, 0);
1076 if (!LShift_type) return 0;
1077 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1078 if (!LShift_singleton) return 0;
1079 RShift_type = make_type("RShift", operator_type, NULL, 0);
1080 if (!RShift_type) return 0;
1081 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1082 if (!RShift_singleton) return 0;
1083 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1084 if (!BitOr_type) return 0;
1085 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1086 if (!BitOr_singleton) return 0;
1087 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1088 if (!BitXor_type) return 0;
1089 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1090 if (!BitXor_singleton) return 0;
1091 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1092 if (!BitAnd_type) return 0;
1093 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1094 if (!BitAnd_singleton) return 0;
1095 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1096 if (!FloorDiv_type) return 0;
1097 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1098 if (!FloorDiv_singleton) return 0;
1099 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1100 if (!unaryop_type) return 0;
1101 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1102 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1103 if (!Invert_type) return 0;
1104 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1105 if (!Invert_singleton) return 0;
1106 Not_type = make_type("Not", unaryop_type, NULL, 0);
1107 if (!Not_type) return 0;
1108 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1109 if (!Not_singleton) return 0;
1110 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1111 if (!UAdd_type) return 0;
1112 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1113 if (!UAdd_singleton) return 0;
1114 USub_type = make_type("USub", unaryop_type, NULL, 0);
1115 if (!USub_type) return 0;
1116 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1117 if (!USub_singleton) return 0;
1118 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1119 if (!cmpop_type) return 0;
1120 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1121 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1122 if (!Eq_type) return 0;
1123 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1124 if (!Eq_singleton) return 0;
1125 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1126 if (!NotEq_type) return 0;
1127 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1128 if (!NotEq_singleton) return 0;
1129 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1130 if (!Lt_type) return 0;
1131 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1132 if (!Lt_singleton) return 0;
1133 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1134 if (!LtE_type) return 0;
1135 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1136 if (!LtE_singleton) return 0;
1137 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1138 if (!Gt_type) return 0;
1139 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1140 if (!Gt_singleton) return 0;
1141 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1142 if (!GtE_type) return 0;
1143 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1144 if (!GtE_singleton) return 0;
1145 Is_type = make_type("Is", cmpop_type, NULL, 0);
1146 if (!Is_type) return 0;
1147 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1148 if (!Is_singleton) return 0;
1149 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1150 if (!IsNot_type) return 0;
1151 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1152 if (!IsNot_singleton) return 0;
1153 In_type = make_type("In", cmpop_type, NULL, 0);
1154 if (!In_type) return 0;
1155 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1156 if (!In_singleton) return 0;
1157 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1158 if (!NotIn_type) return 0;
1159 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1160 if (!NotIn_singleton) return 0;
1161 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001162 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001163 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001164 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001165 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1166 if (!excepthandler_type) return 0;
1167 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1168 return 0;
1169 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1170 ExceptHandler_fields, 3);
1171 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001172 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001173 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001174 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1176 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001177 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001178 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1179 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001180 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001181 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1182 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001183 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001184 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1185 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001186 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001187 initialized = 1;
1188 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001189}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001190
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001191static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1192static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1193static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1194static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1195 arena);
1196static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1197static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1198static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1199static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1200static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1201static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1202 arena);
1203static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1204 arena);
1205static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1206static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1207static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1208static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001209static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001210
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211mod_ty
INADA Naokicb41b272017-02-23 00:31:59 +09001212Module(asdl_seq * body, string docstring, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001214 mod_ty p;
1215 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1216 if (!p)
1217 return NULL;
1218 p->kind = Module_kind;
1219 p->v.Module.body = body;
INADA Naokicb41b272017-02-23 00:31:59 +09001220 p->v.Module.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001221 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
1224mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001225Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001227 mod_ty p;
1228 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1229 if (!p)
1230 return NULL;
1231 p->kind = Interactive_kind;
1232 p->v.Interactive.body = body;
1233 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234}
1235
1236mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001237Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001239 mod_ty p;
1240 if (!body) {
1241 PyErr_SetString(PyExc_ValueError,
1242 "field body is required for Expression");
1243 return NULL;
1244 }
1245 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1246 if (!p)
1247 return NULL;
1248 p->kind = Expression_kind;
1249 p->v.Expression.body = body;
1250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001254Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001256 mod_ty p;
1257 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1258 if (!p)
1259 return NULL;
1260 p->kind = Suite_kind;
1261 p->v.Suite.body = body;
1262 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263}
1264
1265stmt_ty
1266FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
INADA Naokicb41b272017-02-23 00:31:59 +09001267 decorator_list, expr_ty returns, string docstring, int lineno, int
1268 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001270 stmt_ty p;
1271 if (!name) {
1272 PyErr_SetString(PyExc_ValueError,
1273 "field name is required for FunctionDef");
1274 return NULL;
1275 }
1276 if (!args) {
1277 PyErr_SetString(PyExc_ValueError,
1278 "field args is required for FunctionDef");
1279 return NULL;
1280 }
1281 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1282 if (!p)
1283 return NULL;
1284 p->kind = FunctionDef_kind;
1285 p->v.FunctionDef.name = name;
1286 p->v.FunctionDef.args = args;
1287 p->v.FunctionDef.body = body;
1288 p->v.FunctionDef.decorator_list = decorator_list;
1289 p->v.FunctionDef.returns = returns;
INADA Naokicb41b272017-02-23 00:31:59 +09001290 p->v.FunctionDef.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001291 p->lineno = lineno;
1292 p->col_offset = col_offset;
1293 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294}
1295
1296stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001297AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
INADA Naokicb41b272017-02-23 00:31:59 +09001298 * decorator_list, expr_ty returns, string docstring, int
1299 lineno, int col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001300{
1301 stmt_ty p;
1302 if (!name) {
1303 PyErr_SetString(PyExc_ValueError,
1304 "field name is required for AsyncFunctionDef");
1305 return NULL;
1306 }
1307 if (!args) {
1308 PyErr_SetString(PyExc_ValueError,
1309 "field args is required for AsyncFunctionDef");
1310 return NULL;
1311 }
1312 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1313 if (!p)
1314 return NULL;
1315 p->kind = AsyncFunctionDef_kind;
1316 p->v.AsyncFunctionDef.name = name;
1317 p->v.AsyncFunctionDef.args = args;
1318 p->v.AsyncFunctionDef.body = body;
1319 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1320 p->v.AsyncFunctionDef.returns = returns;
INADA Naokicb41b272017-02-23 00:31:59 +09001321 p->v.AsyncFunctionDef.docstring = docstring;
Yury Selivanov75445082015-05-11 22:57:16 -04001322 p->lineno = lineno;
1323 p->col_offset = col_offset;
1324 return p;
1325}
1326
1327stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001328ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
INADA Naokicb41b272017-02-23 00:31:59 +09001329 body, asdl_seq * decorator_list, string docstring, int lineno, int
1330 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001332 stmt_ty p;
1333 if (!name) {
1334 PyErr_SetString(PyExc_ValueError,
1335 "field name is required for ClassDef");
1336 return NULL;
1337 }
1338 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1339 if (!p)
1340 return NULL;
1341 p->kind = ClassDef_kind;
1342 p->v.ClassDef.name = name;
1343 p->v.ClassDef.bases = bases;
1344 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001345 p->v.ClassDef.body = body;
1346 p->v.ClassDef.decorator_list = decorator_list;
INADA Naokicb41b272017-02-23 00:31:59 +09001347 p->v.ClassDef.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001348 p->lineno = lineno;
1349 p->col_offset = col_offset;
1350 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351}
1352
1353stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001354Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001356 stmt_ty p;
1357 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1358 if (!p)
1359 return NULL;
1360 p->kind = Return_kind;
1361 p->v.Return.value = value;
1362 p->lineno = lineno;
1363 p->col_offset = col_offset;
1364 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001370 stmt_ty p;
1371 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1372 if (!p)
1373 return NULL;
1374 p->kind = Delete_kind;
1375 p->v.Delete.targets = targets;
1376 p->lineno = lineno;
1377 p->col_offset = col_offset;
1378 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379}
1380
1381stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001382Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1383 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001385 stmt_ty p;
1386 if (!value) {
1387 PyErr_SetString(PyExc_ValueError,
1388 "field value is required for Assign");
1389 return NULL;
1390 }
1391 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1392 if (!p)
1393 return NULL;
1394 p->kind = Assign_kind;
1395 p->v.Assign.targets = targets;
1396 p->v.Assign.value = value;
1397 p->lineno = lineno;
1398 p->col_offset = col_offset;
1399 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400}
1401
1402stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001403AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1404 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001406 stmt_ty p;
1407 if (!target) {
1408 PyErr_SetString(PyExc_ValueError,
1409 "field target is required for AugAssign");
1410 return NULL;
1411 }
1412 if (!op) {
1413 PyErr_SetString(PyExc_ValueError,
1414 "field op is required for AugAssign");
1415 return NULL;
1416 }
1417 if (!value) {
1418 PyErr_SetString(PyExc_ValueError,
1419 "field value is required for AugAssign");
1420 return NULL;
1421 }
1422 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1423 if (!p)
1424 return NULL;
1425 p->kind = AugAssign_kind;
1426 p->v.AugAssign.target = target;
1427 p->v.AugAssign.op = op;
1428 p->v.AugAssign.value = value;
1429 p->lineno = lineno;
1430 p->col_offset = col_offset;
1431 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432}
1433
1434stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001435AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1436 lineno, int col_offset, PyArena *arena)
1437{
1438 stmt_ty p;
1439 if (!target) {
1440 PyErr_SetString(PyExc_ValueError,
1441 "field target is required for AnnAssign");
1442 return NULL;
1443 }
1444 if (!annotation) {
1445 PyErr_SetString(PyExc_ValueError,
1446 "field annotation is required for AnnAssign");
1447 return NULL;
1448 }
1449 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1450 if (!p)
1451 return NULL;
1452 p->kind = AnnAssign_kind;
1453 p->v.AnnAssign.target = target;
1454 p->v.AnnAssign.annotation = annotation;
1455 p->v.AnnAssign.value = value;
1456 p->v.AnnAssign.simple = simple;
1457 p->lineno = lineno;
1458 p->col_offset = col_offset;
1459 return p;
1460}
1461
1462stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001464 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001466 stmt_ty p;
1467 if (!target) {
1468 PyErr_SetString(PyExc_ValueError,
1469 "field target is required for For");
1470 return NULL;
1471 }
1472 if (!iter) {
1473 PyErr_SetString(PyExc_ValueError,
1474 "field iter is required for For");
1475 return NULL;
1476 }
1477 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1478 if (!p)
1479 return NULL;
1480 p->kind = For_kind;
1481 p->v.For.target = target;
1482 p->v.For.iter = iter;
1483 p->v.For.body = body;
1484 p->v.For.orelse = orelse;
1485 p->lineno = lineno;
1486 p->col_offset = col_offset;
1487 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001491AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1492 lineno, int col_offset, PyArena *arena)
1493{
1494 stmt_ty p;
1495 if (!target) {
1496 PyErr_SetString(PyExc_ValueError,
1497 "field target is required for AsyncFor");
1498 return NULL;
1499 }
1500 if (!iter) {
1501 PyErr_SetString(PyExc_ValueError,
1502 "field iter is required for AsyncFor");
1503 return NULL;
1504 }
1505 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1506 if (!p)
1507 return NULL;
1508 p->kind = AsyncFor_kind;
1509 p->v.AsyncFor.target = target;
1510 p->v.AsyncFor.iter = iter;
1511 p->v.AsyncFor.body = body;
1512 p->v.AsyncFor.orelse = orelse;
1513 p->lineno = lineno;
1514 p->col_offset = col_offset;
1515 return p;
1516}
1517
1518stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001519While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1520 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001522 stmt_ty p;
1523 if (!test) {
1524 PyErr_SetString(PyExc_ValueError,
1525 "field test is required for While");
1526 return NULL;
1527 }
1528 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1529 if (!p)
1530 return NULL;
1531 p->kind = While_kind;
1532 p->v.While.test = test;
1533 p->v.While.body = body;
1534 p->v.While.orelse = orelse;
1535 p->lineno = lineno;
1536 p->col_offset = col_offset;
1537 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
1540stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001541If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1542 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001544 stmt_ty p;
1545 if (!test) {
1546 PyErr_SetString(PyExc_ValueError,
1547 "field test is required for If");
1548 return NULL;
1549 }
1550 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1551 if (!p)
1552 return NULL;
1553 p->kind = If_kind;
1554 p->v.If.test = test;
1555 p->v.If.body = body;
1556 p->v.If.orelse = orelse;
1557 p->lineno = lineno;
1558 p->col_offset = col_offset;
1559 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560}
1561
1562stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001563With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1564 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001565{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001566 stmt_ty p;
1567 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1568 if (!p)
1569 return NULL;
1570 p->kind = With_kind;
1571 p->v.With.items = items;
1572 p->v.With.body = body;
1573 p->lineno = lineno;
1574 p->col_offset = col_offset;
1575 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001576}
1577
1578stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001579AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1580 PyArena *arena)
1581{
1582 stmt_ty p;
1583 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1584 if (!p)
1585 return NULL;
1586 p->kind = AsyncWith_kind;
1587 p->v.AsyncWith.items = items;
1588 p->v.AsyncWith.body = body;
1589 p->lineno = lineno;
1590 p->col_offset = col_offset;
1591 return p;
1592}
1593
1594stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001595Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001597 stmt_ty p;
1598 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1599 if (!p)
1600 return NULL;
1601 p->kind = Raise_kind;
1602 p->v.Raise.exc = exc;
1603 p->v.Raise.cause = cause;
1604 p->lineno = lineno;
1605 p->col_offset = col_offset;
1606 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607}
1608
1609stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001610Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1611 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001613 stmt_ty p;
1614 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1615 if (!p)
1616 return NULL;
1617 p->kind = Try_kind;
1618 p->v.Try.body = body;
1619 p->v.Try.handlers = handlers;
1620 p->v.Try.orelse = orelse;
1621 p->v.Try.finalbody = finalbody;
1622 p->lineno = lineno;
1623 p->col_offset = col_offset;
1624 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625}
1626
1627stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001628Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001630 stmt_ty p;
1631 if (!test) {
1632 PyErr_SetString(PyExc_ValueError,
1633 "field test is required for Assert");
1634 return NULL;
1635 }
1636 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1637 if (!p)
1638 return NULL;
1639 p->kind = Assert_kind;
1640 p->v.Assert.test = test;
1641 p->v.Assert.msg = msg;
1642 p->lineno = lineno;
1643 p->col_offset = col_offset;
1644 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645}
1646
1647stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001648Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001650 stmt_ty p;
1651 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1652 if (!p)
1653 return NULL;
1654 p->kind = Import_kind;
1655 p->v.Import.names = names;
1656 p->lineno = lineno;
1657 p->col_offset = col_offset;
1658 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
1661stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001662ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1663 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001665 stmt_ty p;
1666 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1667 if (!p)
1668 return NULL;
1669 p->kind = ImportFrom_kind;
1670 p->v.ImportFrom.module = module;
1671 p->v.ImportFrom.names = names;
1672 p->v.ImportFrom.level = level;
1673 p->lineno = lineno;
1674 p->col_offset = col_offset;
1675 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
1677
1678stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001679Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001681 stmt_ty p;
1682 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1683 if (!p)
1684 return NULL;
1685 p->kind = Global_kind;
1686 p->v.Global.names = names;
1687 p->lineno = lineno;
1688 p->col_offset = col_offset;
1689 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690}
1691
1692stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001693Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1694{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001695 stmt_ty p;
1696 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1697 if (!p)
1698 return NULL;
1699 p->kind = Nonlocal_kind;
1700 p->v.Nonlocal.names = names;
1701 p->lineno = lineno;
1702 p->col_offset = col_offset;
1703 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001704}
1705
1706stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001707Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001709 stmt_ty p;
1710 if (!value) {
1711 PyErr_SetString(PyExc_ValueError,
1712 "field value is required for Expr");
1713 return NULL;
1714 }
1715 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1716 if (!p)
1717 return NULL;
1718 p->kind = Expr_kind;
1719 p->v.Expr.value = value;
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 +00001726Pass(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 = Pass_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 +00001739Break(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 = Break_kind;
1746 p->lineno = lineno;
1747 p->col_offset = col_offset;
1748 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001754 stmt_ty p;
1755 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1756 if (!p)
1757 return NULL;
1758 p->kind = Continue_kind;
1759 p->lineno = lineno;
1760 p->col_offset = col_offset;
1761 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762}
1763
1764expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001765BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1766 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001768 expr_ty p;
1769 if (!op) {
1770 PyErr_SetString(PyExc_ValueError,
1771 "field op is required for BoolOp");
1772 return NULL;
1773 }
1774 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1775 if (!p)
1776 return NULL;
1777 p->kind = BoolOp_kind;
1778 p->v.BoolOp.op = op;
1779 p->v.BoolOp.values = values;
1780 p->lineno = lineno;
1781 p->col_offset = col_offset;
1782 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783}
1784
1785expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001786BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1787 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001789 expr_ty p;
1790 if (!left) {
1791 PyErr_SetString(PyExc_ValueError,
1792 "field left is required for BinOp");
1793 return NULL;
1794 }
1795 if (!op) {
1796 PyErr_SetString(PyExc_ValueError,
1797 "field op is required for BinOp");
1798 return NULL;
1799 }
1800 if (!right) {
1801 PyErr_SetString(PyExc_ValueError,
1802 "field right is required for BinOp");
1803 return NULL;
1804 }
1805 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1806 if (!p)
1807 return NULL;
1808 p->kind = BinOp_kind;
1809 p->v.BinOp.left = left;
1810 p->v.BinOp.op = op;
1811 p->v.BinOp.right = right;
1812 p->lineno = lineno;
1813 p->col_offset = col_offset;
1814 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815}
1816
1817expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001818UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1819 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001821 expr_ty p;
1822 if (!op) {
1823 PyErr_SetString(PyExc_ValueError,
1824 "field op is required for UnaryOp");
1825 return NULL;
1826 }
1827 if (!operand) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "field operand is required for UnaryOp");
1830 return NULL;
1831 }
1832 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1833 if (!p)
1834 return NULL;
1835 p->kind = UnaryOp_kind;
1836 p->v.UnaryOp.op = op;
1837 p->v.UnaryOp.operand = operand;
1838 p->lineno = lineno;
1839 p->col_offset = col_offset;
1840 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841}
1842
1843expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001844Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1845 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001847 expr_ty p;
1848 if (!args) {
1849 PyErr_SetString(PyExc_ValueError,
1850 "field args is required for Lambda");
1851 return NULL;
1852 }
1853 if (!body) {
1854 PyErr_SetString(PyExc_ValueError,
1855 "field body is required for Lambda");
1856 return NULL;
1857 }
1858 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1859 if (!p)
1860 return NULL;
1861 p->kind = Lambda_kind;
1862 p->v.Lambda.args = args;
1863 p->v.Lambda.body = body;
1864 p->lineno = lineno;
1865 p->col_offset = col_offset;
1866 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867}
1868
1869expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001870IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1871 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001872{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001873 expr_ty p;
1874 if (!test) {
1875 PyErr_SetString(PyExc_ValueError,
1876 "field test is required for IfExp");
1877 return NULL;
1878 }
1879 if (!body) {
1880 PyErr_SetString(PyExc_ValueError,
1881 "field body is required for IfExp");
1882 return NULL;
1883 }
1884 if (!orelse) {
1885 PyErr_SetString(PyExc_ValueError,
1886 "field orelse is required for IfExp");
1887 return NULL;
1888 }
1889 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1890 if (!p)
1891 return NULL;
1892 p->kind = IfExp_kind;
1893 p->v.IfExp.test = test;
1894 p->v.IfExp.body = body;
1895 p->v.IfExp.orelse = orelse;
1896 p->lineno = lineno;
1897 p->col_offset = col_offset;
1898 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001899}
1900
1901expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001902Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1903 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001905 expr_ty p;
1906 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1907 if (!p)
1908 return NULL;
1909 p->kind = Dict_kind;
1910 p->v.Dict.keys = keys;
1911 p->v.Dict.values = values;
1912 p->lineno = lineno;
1913 p->col_offset = col_offset;
1914 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915}
1916
1917expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001918Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1919{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001920 expr_ty p;
1921 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1922 if (!p)
1923 return NULL;
1924 p->kind = Set_kind;
1925 p->v.Set.elts = elts;
1926 p->lineno = lineno;
1927 p->col_offset = col_offset;
1928 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001929}
1930
1931expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001932ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1933 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001935 expr_ty p;
1936 if (!elt) {
1937 PyErr_SetString(PyExc_ValueError,
1938 "field elt is required for ListComp");
1939 return NULL;
1940 }
1941 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1942 if (!p)
1943 return NULL;
1944 p->kind = ListComp_kind;
1945 p->v.ListComp.elt = elt;
1946 p->v.ListComp.generators = generators;
1947 p->lineno = lineno;
1948 p->col_offset = col_offset;
1949 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
1952expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001953SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1954 *arena)
1955{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001956 expr_ty p;
1957 if (!elt) {
1958 PyErr_SetString(PyExc_ValueError,
1959 "field elt is required for SetComp");
1960 return NULL;
1961 }
1962 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1963 if (!p)
1964 return NULL;
1965 p->kind = SetComp_kind;
1966 p->v.SetComp.elt = elt;
1967 p->v.SetComp.generators = generators;
1968 p->lineno = lineno;
1969 p->col_offset = col_offset;
1970 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001971}
1972
1973expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001974DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1975 col_offset, PyArena *arena)
1976{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001977 expr_ty p;
1978 if (!key) {
1979 PyErr_SetString(PyExc_ValueError,
1980 "field key is required for DictComp");
1981 return NULL;
1982 }
1983 if (!value) {
1984 PyErr_SetString(PyExc_ValueError,
1985 "field value is required for DictComp");
1986 return NULL;
1987 }
1988 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1989 if (!p)
1990 return NULL;
1991 p->kind = DictComp_kind;
1992 p->v.DictComp.key = key;
1993 p->v.DictComp.value = value;
1994 p->v.DictComp.generators = generators;
1995 p->lineno = lineno;
1996 p->col_offset = col_offset;
1997 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001998}
1999
2000expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002001GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
2002 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002004 expr_ty p;
2005 if (!elt) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field elt is required for GeneratorExp");
2008 return NULL;
2009 }
2010 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2011 if (!p)
2012 return NULL;
2013 p->kind = GeneratorExp_kind;
2014 p->v.GeneratorExp.elt = elt;
2015 p->v.GeneratorExp.generators = generators;
2016 p->lineno = lineno;
2017 p->col_offset = col_offset;
2018 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019}
2020
2021expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002022Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2023{
2024 expr_ty p;
2025 if (!value) {
2026 PyErr_SetString(PyExc_ValueError,
2027 "field value is required for Await");
2028 return NULL;
2029 }
2030 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2031 if (!p)
2032 return NULL;
2033 p->kind = Await_kind;
2034 p->v.Await.value = value;
2035 p->lineno = lineno;
2036 p->col_offset = col_offset;
2037 return p;
2038}
2039
2040expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002041Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002043 expr_ty p;
2044 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2045 if (!p)
2046 return NULL;
2047 p->kind = Yield_kind;
2048 p->v.Yield.value = value;
2049 p->lineno = lineno;
2050 p->col_offset = col_offset;
2051 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052}
2053
2054expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002055YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2056{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002057 expr_ty p;
2058 if (!value) {
2059 PyErr_SetString(PyExc_ValueError,
2060 "field value is required for YieldFrom");
2061 return NULL;
2062 }
2063 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2064 if (!p)
2065 return NULL;
2066 p->kind = YieldFrom_kind;
2067 p->v.YieldFrom.value = value;
2068 p->lineno = lineno;
2069 p->col_offset = col_offset;
2070 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002071}
2072
2073expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002074Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2075 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002077 expr_ty p;
2078 if (!left) {
2079 PyErr_SetString(PyExc_ValueError,
2080 "field left is required for Compare");
2081 return NULL;
2082 }
2083 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2084 if (!p)
2085 return NULL;
2086 p->kind = Compare_kind;
2087 p->v.Compare.left = left;
2088 p->v.Compare.ops = ops;
2089 p->v.Compare.comparators = comparators;
2090 p->lineno = lineno;
2091 p->col_offset = col_offset;
2092 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093}
2094
2095expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002096Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2097 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002099 expr_ty p;
2100 if (!func) {
2101 PyErr_SetString(PyExc_ValueError,
2102 "field func is required for Call");
2103 return NULL;
2104 }
2105 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2106 if (!p)
2107 return NULL;
2108 p->kind = Call_kind;
2109 p->v.Call.func = func;
2110 p->v.Call.args = args;
2111 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002112 p->lineno = lineno;
2113 p->col_offset = col_offset;
2114 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115}
2116
2117expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002118Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002120 expr_ty p;
2121 if (!n) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "field n is required for Num");
2124 return NULL;
2125 }
2126 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2127 if (!p)
2128 return NULL;
2129 p->kind = Num_kind;
2130 p->v.Num.n = n;
2131 p->lineno = lineno;
2132 p->col_offset = col_offset;
2133 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134}
2135
2136expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002137Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002139 expr_ty p;
2140 if (!s) {
2141 PyErr_SetString(PyExc_ValueError,
2142 "field s is required for Str");
2143 return NULL;
2144 }
2145 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2146 if (!p)
2147 return NULL;
2148 p->kind = Str_kind;
2149 p->v.Str.s = s;
2150 p->lineno = lineno;
2151 p->col_offset = col_offset;
2152 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153}
2154
2155expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002156FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2157 int col_offset, PyArena *arena)
2158{
2159 expr_ty p;
2160 if (!value) {
2161 PyErr_SetString(PyExc_ValueError,
2162 "field value is required for FormattedValue");
2163 return NULL;
2164 }
2165 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2166 if (!p)
2167 return NULL;
2168 p->kind = FormattedValue_kind;
2169 p->v.FormattedValue.value = value;
2170 p->v.FormattedValue.conversion = conversion;
2171 p->v.FormattedValue.format_spec = format_spec;
2172 p->lineno = lineno;
2173 p->col_offset = col_offset;
2174 return p;
2175}
2176
2177expr_ty
2178JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2179{
2180 expr_ty p;
2181 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2182 if (!p)
2183 return NULL;
2184 p->kind = JoinedStr_kind;
2185 p->v.JoinedStr.values = values;
2186 p->lineno = lineno;
2187 p->col_offset = col_offset;
2188 return p;
2189}
2190
2191expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002192Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002193{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002194 expr_ty p;
2195 if (!s) {
2196 PyErr_SetString(PyExc_ValueError,
2197 "field s is required for Bytes");
2198 return NULL;
2199 }
2200 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2201 if (!p)
2202 return NULL;
2203 p->kind = Bytes_kind;
2204 p->v.Bytes.s = s;
2205 p->lineno = lineno;
2206 p->col_offset = col_offset;
2207 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002208}
2209
2210expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002211NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2212{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002213 expr_ty p;
2214 if (!value) {
2215 PyErr_SetString(PyExc_ValueError,
2216 "field value is required for NameConstant");
2217 return NULL;
2218 }
2219 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2220 if (!p)
2221 return NULL;
2222 p->kind = NameConstant_kind;
2223 p->v.NameConstant.value = value;
2224 p->lineno = lineno;
2225 p->col_offset = col_offset;
2226 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002227}
2228
2229expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002230Ellipsis(int lineno, int col_offset, PyArena *arena)
2231{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002232 expr_ty p;
2233 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2234 if (!p)
2235 return NULL;
2236 p->kind = Ellipsis_kind;
2237 p->lineno = lineno;
2238 p->col_offset = col_offset;
2239 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002240}
2241
2242expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002243Constant(constant value, int lineno, int col_offset, PyArena *arena)
2244{
2245 expr_ty p;
2246 if (!value) {
2247 PyErr_SetString(PyExc_ValueError,
2248 "field value is required for Constant");
2249 return NULL;
2250 }
2251 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2252 if (!p)
2253 return NULL;
2254 p->kind = Constant_kind;
2255 p->v.Constant.value = value;
2256 p->lineno = lineno;
2257 p->col_offset = col_offset;
2258 return p;
2259}
2260
2261expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002262Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2263 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002265 expr_ty p;
2266 if (!value) {
2267 PyErr_SetString(PyExc_ValueError,
2268 "field value is required for Attribute");
2269 return NULL;
2270 }
2271 if (!attr) {
2272 PyErr_SetString(PyExc_ValueError,
2273 "field attr is required for Attribute");
2274 return NULL;
2275 }
2276 if (!ctx) {
2277 PyErr_SetString(PyExc_ValueError,
2278 "field ctx is required for Attribute");
2279 return NULL;
2280 }
2281 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2282 if (!p)
2283 return NULL;
2284 p->kind = Attribute_kind;
2285 p->v.Attribute.value = value;
2286 p->v.Attribute.attr = attr;
2287 p->v.Attribute.ctx = ctx;
2288 p->lineno = lineno;
2289 p->col_offset = col_offset;
2290 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291}
2292
2293expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002294Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2295 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002297 expr_ty p;
2298 if (!value) {
2299 PyErr_SetString(PyExc_ValueError,
2300 "field value is required for Subscript");
2301 return NULL;
2302 }
2303 if (!slice) {
2304 PyErr_SetString(PyExc_ValueError,
2305 "field slice is required for Subscript");
2306 return NULL;
2307 }
2308 if (!ctx) {
2309 PyErr_SetString(PyExc_ValueError,
2310 "field ctx is required for Subscript");
2311 return NULL;
2312 }
2313 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2314 if (!p)
2315 return NULL;
2316 p->kind = Subscript_kind;
2317 p->v.Subscript.value = value;
2318 p->v.Subscript.slice = slice;
2319 p->v.Subscript.ctx = ctx;
2320 p->lineno = lineno;
2321 p->col_offset = col_offset;
2322 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323}
2324
2325expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002326Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2327 *arena)
2328{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002329 expr_ty p;
2330 if (!value) {
2331 PyErr_SetString(PyExc_ValueError,
2332 "field value is required for Starred");
2333 return NULL;
2334 }
2335 if (!ctx) {
2336 PyErr_SetString(PyExc_ValueError,
2337 "field ctx is required for Starred");
2338 return NULL;
2339 }
2340 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2341 if (!p)
2342 return NULL;
2343 p->kind = Starred_kind;
2344 p->v.Starred.value = value;
2345 p->v.Starred.ctx = ctx;
2346 p->lineno = lineno;
2347 p->col_offset = col_offset;
2348 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002349}
2350
2351expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002352Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2353 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002355 expr_ty p;
2356 if (!id) {
2357 PyErr_SetString(PyExc_ValueError,
2358 "field id is required for Name");
2359 return NULL;
2360 }
2361 if (!ctx) {
2362 PyErr_SetString(PyExc_ValueError,
2363 "field ctx is required for Name");
2364 return NULL;
2365 }
2366 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2367 if (!p)
2368 return NULL;
2369 p->kind = Name_kind;
2370 p->v.Name.id = id;
2371 p->v.Name.ctx = ctx;
2372 p->lineno = lineno;
2373 p->col_offset = col_offset;
2374 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375}
2376
2377expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002378List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2379 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002381 expr_ty p;
2382 if (!ctx) {
2383 PyErr_SetString(PyExc_ValueError,
2384 "field ctx is required for List");
2385 return NULL;
2386 }
2387 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2388 if (!p)
2389 return NULL;
2390 p->kind = List_kind;
2391 p->v.List.elts = elts;
2392 p->v.List.ctx = ctx;
2393 p->lineno = lineno;
2394 p->col_offset = col_offset;
2395 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396}
2397
2398expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002399Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2400 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002402 expr_ty p;
2403 if (!ctx) {
2404 PyErr_SetString(PyExc_ValueError,
2405 "field ctx is required for Tuple");
2406 return NULL;
2407 }
2408 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2409 if (!p)
2410 return NULL;
2411 p->kind = Tuple_kind;
2412 p->v.Tuple.elts = elts;
2413 p->v.Tuple.ctx = ctx;
2414 p->lineno = lineno;
2415 p->col_offset = col_offset;
2416 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417}
2418
2419slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002420Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002422 slice_ty p;
2423 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2424 if (!p)
2425 return NULL;
2426 p->kind = Slice_kind;
2427 p->v.Slice.lower = lower;
2428 p->v.Slice.upper = upper;
2429 p->v.Slice.step = step;
2430 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
2433slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002434ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002436 slice_ty p;
2437 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2438 if (!p)
2439 return NULL;
2440 p->kind = ExtSlice_kind;
2441 p->v.ExtSlice.dims = dims;
2442 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443}
2444
2445slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002446Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002448 slice_ty p;
2449 if (!value) {
2450 PyErr_SetString(PyExc_ValueError,
2451 "field value is required for Index");
2452 return NULL;
2453 }
2454 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2455 if (!p)
2456 return NULL;
2457 p->kind = Index_kind;
2458 p->v.Index.value = value;
2459 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460}
2461
2462comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002463comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2464 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002466 comprehension_ty p;
2467 if (!target) {
2468 PyErr_SetString(PyExc_ValueError,
2469 "field target is required for comprehension");
2470 return NULL;
2471 }
2472 if (!iter) {
2473 PyErr_SetString(PyExc_ValueError,
2474 "field iter is required for comprehension");
2475 return NULL;
2476 }
2477 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2478 if (!p)
2479 return NULL;
2480 p->target = target;
2481 p->iter = iter;
2482 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002483 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002484 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485}
2486
2487excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002488ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002489 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002491 excepthandler_ty p;
2492 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2493 if (!p)
2494 return NULL;
2495 p->kind = ExceptHandler_kind;
2496 p->v.ExceptHandler.type = type;
2497 p->v.ExceptHandler.name = name;
2498 p->v.ExceptHandler.body = body;
2499 p->lineno = lineno;
2500 p->col_offset = col_offset;
2501 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502}
2503
2504arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002505arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2506 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002508 arguments_ty p;
2509 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2510 if (!p)
2511 return NULL;
2512 p->args = args;
2513 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002514 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002515 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002516 p->kwarg = kwarg;
2517 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002518 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519}
2520
Neal Norwitzc1505362006-12-28 06:47:50 +00002521arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002522arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2523 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002524{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002525 arg_ty p;
2526 if (!arg) {
2527 PyErr_SetString(PyExc_ValueError,
2528 "field arg is required for arg");
2529 return NULL;
2530 }
2531 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2532 if (!p)
2533 return NULL;
2534 p->arg = arg;
2535 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002536 p->lineno = lineno;
2537 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002538 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002539}
2540
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002542keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002544 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002545 if (!value) {
2546 PyErr_SetString(PyExc_ValueError,
2547 "field value is required for keyword");
2548 return NULL;
2549 }
2550 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2551 if (!p)
2552 return NULL;
2553 p->arg = arg;
2554 p->value = value;
2555 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002559alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002561 alias_ty p;
2562 if (!name) {
2563 PyErr_SetString(PyExc_ValueError,
2564 "field name is required for alias");
2565 return NULL;
2566 }
2567 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2568 if (!p)
2569 return NULL;
2570 p->name = name;
2571 p->asname = asname;
2572 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573}
2574
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002575withitem_ty
2576withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2577{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002578 withitem_ty p;
2579 if (!context_expr) {
2580 PyErr_SetString(PyExc_ValueError,
2581 "field context_expr is required for withitem");
2582 return NULL;
2583 }
2584 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2585 if (!p)
2586 return NULL;
2587 p->context_expr = context_expr;
2588 p->optional_vars = optional_vars;
2589 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002590}
2591
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002592
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002593PyObject*
2594ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002595{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002596 mod_ty o = (mod_ty)_o;
2597 PyObject *result = NULL, *value = NULL;
2598 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002599 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002600 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002601
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002602 switch (o->kind) {
2603 case Module_kind:
2604 result = PyType_GenericNew(Module_type, NULL, NULL);
2605 if (!result) goto failed;
2606 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2607 if (!value) goto failed;
2608 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2609 goto failed;
2610 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002611 value = ast2obj_string(o->v.Module.docstring);
2612 if (!value) goto failed;
2613 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002616 break;
2617 case Interactive_kind:
2618 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2619 if (!result) goto failed;
2620 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2621 if (!value) goto failed;
2622 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2623 goto failed;
2624 Py_DECREF(value);
2625 break;
2626 case Expression_kind:
2627 result = PyType_GenericNew(Expression_type, NULL, NULL);
2628 if (!result) goto failed;
2629 value = ast2obj_expr(o->v.Expression.body);
2630 if (!value) goto failed;
2631 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 break;
2635 case Suite_kind:
2636 result = PyType_GenericNew(Suite_type, NULL, NULL);
2637 if (!result) goto failed;
2638 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2639 if (!value) goto failed;
2640 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
2643 break;
2644 }
2645 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002646failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002647 Py_XDECREF(value);
2648 Py_XDECREF(result);
2649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650}
2651
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002652PyObject*
2653ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002655 stmt_ty o = (stmt_ty)_o;
2656 PyObject *result = NULL, *value = NULL;
2657 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002658 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002659 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002660
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002661 switch (o->kind) {
2662 case FunctionDef_kind:
2663 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002666 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002667 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2668 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002669 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002670 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002671 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002672 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2673 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002674 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002675 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2676 if (!value) goto failed;
2677 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2678 goto failed;
2679 Py_DECREF(value);
2680 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2681 if (!value) goto failed;
2682 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 value = ast2obj_expr(o->v.FunctionDef.returns);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002690 value = ast2obj_string(o->v.FunctionDef.docstring);
2691 if (!value) goto failed;
2692 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002695 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002696 case AsyncFunctionDef_kind:
2697 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2698 if (!result) goto failed;
2699 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2700 if (!value) goto failed;
2701 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2715 ast2obj_expr);
2716 if (!value) goto failed;
2717 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2718 goto failed;
2719 Py_DECREF(value);
2720 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2721 if (!value) goto failed;
2722 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2723 goto failed;
2724 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002725 value = ast2obj_string(o->v.AsyncFunctionDef.docstring);
2726 if (!value) goto failed;
2727 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2728 goto failed;
2729 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04002730 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002731 case ClassDef_kind:
2732 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2733 if (!result) goto failed;
2734 value = ast2obj_identifier(o->v.ClassDef.name);
2735 if (!value) goto failed;
2736 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2737 goto failed;
2738 Py_DECREF(value);
2739 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2740 if (!value) goto failed;
2741 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2742 goto failed;
2743 Py_DECREF(value);
2744 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2745 if (!value) goto failed;
2746 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2747 goto failed;
2748 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002749 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2750 if (!value) goto failed;
2751 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
2754 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2755 if (!value) goto failed;
2756 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002759 value = ast2obj_string(o->v.ClassDef.docstring);
2760 if (!value) goto failed;
2761 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002764 break;
2765 case Return_kind:
2766 result = PyType_GenericNew(Return_type, NULL, NULL);
2767 if (!result) goto failed;
2768 value = ast2obj_expr(o->v.Return.value);
2769 if (!value) goto failed;
2770 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 break;
2774 case Delete_kind:
2775 result = PyType_GenericNew(Delete_type, NULL, NULL);
2776 if (!result) goto failed;
2777 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2778 if (!value) goto failed;
2779 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2780 goto failed;
2781 Py_DECREF(value);
2782 break;
2783 case Assign_kind:
2784 result = PyType_GenericNew(Assign_type, NULL, NULL);
2785 if (!result) goto failed;
2786 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2787 if (!value) goto failed;
2788 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2789 goto failed;
2790 Py_DECREF(value);
2791 value = ast2obj_expr(o->v.Assign.value);
2792 if (!value) goto failed;
2793 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 break;
2797 case AugAssign_kind:
2798 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2799 if (!result) goto failed;
2800 value = ast2obj_expr(o->v.AugAssign.target);
2801 if (!value) goto failed;
2802 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2803 goto failed;
2804 Py_DECREF(value);
2805 value = ast2obj_operator(o->v.AugAssign.op);
2806 if (!value) goto failed;
2807 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2808 goto failed;
2809 Py_DECREF(value);
2810 value = ast2obj_expr(o->v.AugAssign.value);
2811 if (!value) goto failed;
2812 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
2815 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002816 case AnnAssign_kind:
2817 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_expr(o->v.AnnAssign.target);
2820 if (!value) goto failed;
2821 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_expr(o->v.AnnAssign.annotation);
2825 if (!value) goto failed;
2826 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 value = ast2obj_expr(o->v.AnnAssign.value);
2830 if (!value) goto failed;
2831 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 value = ast2obj_int(o->v.AnnAssign.simple);
2835 if (!value) goto failed;
2836 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2837 goto failed;
2838 Py_DECREF(value);
2839 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002840 case For_kind:
2841 result = PyType_GenericNew(For_type, NULL, NULL);
2842 if (!result) goto failed;
2843 value = ast2obj_expr(o->v.For.target);
2844 if (!value) goto failed;
2845 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
2848 value = ast2obj_expr(o->v.For.iter);
2849 if (!value) goto failed;
2850 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2854 if (!value) goto failed;
2855 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2856 goto failed;
2857 Py_DECREF(value);
2858 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2859 if (!value) goto failed;
2860 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2861 goto failed;
2862 Py_DECREF(value);
2863 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002864 case AsyncFor_kind:
2865 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2866 if (!result) goto failed;
2867 value = ast2obj_expr(o->v.AsyncFor.target);
2868 if (!value) goto failed;
2869 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2870 goto failed;
2871 Py_DECREF(value);
2872 value = ast2obj_expr(o->v.AsyncFor.iter);
2873 if (!value) goto failed;
2874 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2875 goto failed;
2876 Py_DECREF(value);
2877 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2878 if (!value) goto failed;
2879 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2880 goto failed;
2881 Py_DECREF(value);
2882 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2883 if (!value) goto failed;
2884 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2885 goto failed;
2886 Py_DECREF(value);
2887 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002888 case While_kind:
2889 result = PyType_GenericNew(While_type, NULL, NULL);
2890 if (!result) goto failed;
2891 value = ast2obj_expr(o->v.While.test);
2892 if (!value) goto failed;
2893 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
2896 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2897 if (!value) goto failed;
2898 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2899 goto failed;
2900 Py_DECREF(value);
2901 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2902 if (!value) goto failed;
2903 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2904 goto failed;
2905 Py_DECREF(value);
2906 break;
2907 case If_kind:
2908 result = PyType_GenericNew(If_type, NULL, NULL);
2909 if (!result) goto failed;
2910 value = ast2obj_expr(o->v.If.test);
2911 if (!value) goto failed;
2912 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2913 goto failed;
2914 Py_DECREF(value);
2915 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2916 if (!value) goto failed;
2917 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2918 goto failed;
2919 Py_DECREF(value);
2920 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2921 if (!value) goto failed;
2922 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2923 goto failed;
2924 Py_DECREF(value);
2925 break;
2926 case With_kind:
2927 result = PyType_GenericNew(With_type, NULL, NULL);
2928 if (!result) goto failed;
2929 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2930 if (!value) goto failed;
2931 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2932 goto failed;
2933 Py_DECREF(value);
2934 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2935 if (!value) goto failed;
2936 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2937 goto failed;
2938 Py_DECREF(value);
2939 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002940 case AsyncWith_kind:
2941 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2942 if (!result) goto failed;
2943 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2944 if (!value) goto failed;
2945 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2946 goto failed;
2947 Py_DECREF(value);
2948 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2949 if (!value) goto failed;
2950 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2951 goto failed;
2952 Py_DECREF(value);
2953 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002954 case Raise_kind:
2955 result = PyType_GenericNew(Raise_type, NULL, NULL);
2956 if (!result) goto failed;
2957 value = ast2obj_expr(o->v.Raise.exc);
2958 if (!value) goto failed;
2959 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2960 goto failed;
2961 Py_DECREF(value);
2962 value = ast2obj_expr(o->v.Raise.cause);
2963 if (!value) goto failed;
2964 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2965 goto failed;
2966 Py_DECREF(value);
2967 break;
2968 case Try_kind:
2969 result = PyType_GenericNew(Try_type, NULL, NULL);
2970 if (!result) goto failed;
2971 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2972 if (!value) goto failed;
2973 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2974 goto failed;
2975 Py_DECREF(value);
2976 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2977 if (!value) goto failed;
2978 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2979 goto failed;
2980 Py_DECREF(value);
2981 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2982 if (!value) goto failed;
2983 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2984 goto failed;
2985 Py_DECREF(value);
2986 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2987 if (!value) goto failed;
2988 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2989 goto failed;
2990 Py_DECREF(value);
2991 break;
2992 case Assert_kind:
2993 result = PyType_GenericNew(Assert_type, NULL, NULL);
2994 if (!result) goto failed;
2995 value = ast2obj_expr(o->v.Assert.test);
2996 if (!value) goto failed;
2997 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2998 goto failed;
2999 Py_DECREF(value);
3000 value = ast2obj_expr(o->v.Assert.msg);
3001 if (!value) goto failed;
3002 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
3003 goto failed;
3004 Py_DECREF(value);
3005 break;
3006 case Import_kind:
3007 result = PyType_GenericNew(Import_type, NULL, NULL);
3008 if (!result) goto failed;
3009 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
3010 if (!value) goto failed;
3011 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3012 goto failed;
3013 Py_DECREF(value);
3014 break;
3015 case ImportFrom_kind:
3016 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
3017 if (!result) goto failed;
3018 value = ast2obj_identifier(o->v.ImportFrom.module);
3019 if (!value) goto failed;
3020 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
3021 goto failed;
3022 Py_DECREF(value);
3023 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
3024 if (!value) goto failed;
3025 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3026 goto failed;
3027 Py_DECREF(value);
3028 value = ast2obj_int(o->v.ImportFrom.level);
3029 if (!value) goto failed;
3030 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3031 goto failed;
3032 Py_DECREF(value);
3033 break;
3034 case Global_kind:
3035 result = PyType_GenericNew(Global_type, NULL, NULL);
3036 if (!result) goto failed;
3037 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3038 if (!value) goto failed;
3039 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3040 goto failed;
3041 Py_DECREF(value);
3042 break;
3043 case Nonlocal_kind:
3044 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3045 if (!result) goto failed;
3046 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3047 if (!value) goto failed;
3048 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3049 goto failed;
3050 Py_DECREF(value);
3051 break;
3052 case Expr_kind:
3053 result = PyType_GenericNew(Expr_type, NULL, NULL);
3054 if (!result) goto failed;
3055 value = ast2obj_expr(o->v.Expr.value);
3056 if (!value) goto failed;
3057 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3058 goto failed;
3059 Py_DECREF(value);
3060 break;
3061 case Pass_kind:
3062 result = PyType_GenericNew(Pass_type, NULL, NULL);
3063 if (!result) goto failed;
3064 break;
3065 case Break_kind:
3066 result = PyType_GenericNew(Break_type, NULL, NULL);
3067 if (!result) goto failed;
3068 break;
3069 case Continue_kind:
3070 result = PyType_GenericNew(Continue_type, NULL, NULL);
3071 if (!result) goto failed;
3072 break;
3073 }
3074 value = ast2obj_int(o->lineno);
3075 if (!value) goto failed;
3076 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3077 goto failed;
3078 Py_DECREF(value);
3079 value = ast2obj_int(o->col_offset);
3080 if (!value) goto failed;
3081 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3082 goto failed;
3083 Py_DECREF(value);
3084 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003085failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003086 Py_XDECREF(value);
3087 Py_XDECREF(result);
3088 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089}
3090
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003091PyObject*
3092ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003094 expr_ty o = (expr_ty)_o;
3095 PyObject *result = NULL, *value = NULL;
3096 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003097 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003098 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003099
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003100 switch (o->kind) {
3101 case BoolOp_kind:
3102 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3103 if (!result) goto failed;
3104 value = ast2obj_boolop(o->v.BoolOp.op);
3105 if (!value) goto failed;
3106 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3107 goto failed;
3108 Py_DECREF(value);
3109 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3110 if (!value) goto failed;
3111 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3112 goto failed;
3113 Py_DECREF(value);
3114 break;
3115 case BinOp_kind:
3116 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3117 if (!result) goto failed;
3118 value = ast2obj_expr(o->v.BinOp.left);
3119 if (!value) goto failed;
3120 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3121 goto failed;
3122 Py_DECREF(value);
3123 value = ast2obj_operator(o->v.BinOp.op);
3124 if (!value) goto failed;
3125 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3126 goto failed;
3127 Py_DECREF(value);
3128 value = ast2obj_expr(o->v.BinOp.right);
3129 if (!value) goto failed;
3130 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3131 goto failed;
3132 Py_DECREF(value);
3133 break;
3134 case UnaryOp_kind:
3135 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3136 if (!result) goto failed;
3137 value = ast2obj_unaryop(o->v.UnaryOp.op);
3138 if (!value) goto failed;
3139 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3140 goto failed;
3141 Py_DECREF(value);
3142 value = ast2obj_expr(o->v.UnaryOp.operand);
3143 if (!value) goto failed;
3144 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3145 goto failed;
3146 Py_DECREF(value);
3147 break;
3148 case Lambda_kind:
3149 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3150 if (!result) goto failed;
3151 value = ast2obj_arguments(o->v.Lambda.args);
3152 if (!value) goto failed;
3153 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3154 goto failed;
3155 Py_DECREF(value);
3156 value = ast2obj_expr(o->v.Lambda.body);
3157 if (!value) goto failed;
3158 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 break;
3162 case IfExp_kind:
3163 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3164 if (!result) goto failed;
3165 value = ast2obj_expr(o->v.IfExp.test);
3166 if (!value) goto failed;
3167 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3168 goto failed;
3169 Py_DECREF(value);
3170 value = ast2obj_expr(o->v.IfExp.body);
3171 if (!value) goto failed;
3172 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 value = ast2obj_expr(o->v.IfExp.orelse);
3176 if (!value) goto failed;
3177 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3178 goto failed;
3179 Py_DECREF(value);
3180 break;
3181 case Dict_kind:
3182 result = PyType_GenericNew(Dict_type, NULL, NULL);
3183 if (!result) goto failed;
3184 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3185 if (!value) goto failed;
3186 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3187 goto failed;
3188 Py_DECREF(value);
3189 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3190 if (!value) goto failed;
3191 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3192 goto failed;
3193 Py_DECREF(value);
3194 break;
3195 case Set_kind:
3196 result = PyType_GenericNew(Set_type, NULL, NULL);
3197 if (!result) goto failed;
3198 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3199 if (!value) goto failed;
3200 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3201 goto failed;
3202 Py_DECREF(value);
3203 break;
3204 case ListComp_kind:
3205 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3206 if (!result) goto failed;
3207 value = ast2obj_expr(o->v.ListComp.elt);
3208 if (!value) goto failed;
3209 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3210 goto failed;
3211 Py_DECREF(value);
3212 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3213 if (!value) goto failed;
3214 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3215 goto failed;
3216 Py_DECREF(value);
3217 break;
3218 case SetComp_kind:
3219 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3220 if (!result) goto failed;
3221 value = ast2obj_expr(o->v.SetComp.elt);
3222 if (!value) goto failed;
3223 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3224 goto failed;
3225 Py_DECREF(value);
3226 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3227 if (!value) goto failed;
3228 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3229 goto failed;
3230 Py_DECREF(value);
3231 break;
3232 case DictComp_kind:
3233 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3234 if (!result) goto failed;
3235 value = ast2obj_expr(o->v.DictComp.key);
3236 if (!value) goto failed;
3237 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
3240 value = ast2obj_expr(o->v.DictComp.value);
3241 if (!value) goto failed;
3242 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3243 goto failed;
3244 Py_DECREF(value);
3245 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3246 if (!value) goto failed;
3247 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3248 goto failed;
3249 Py_DECREF(value);
3250 break;
3251 case GeneratorExp_kind:
3252 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3253 if (!result) goto failed;
3254 value = ast2obj_expr(o->v.GeneratorExp.elt);
3255 if (!value) goto failed;
3256 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3257 goto failed;
3258 Py_DECREF(value);
3259 value = ast2obj_list(o->v.GeneratorExp.generators,
3260 ast2obj_comprehension);
3261 if (!value) goto failed;
3262 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3263 goto failed;
3264 Py_DECREF(value);
3265 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003266 case Await_kind:
3267 result = PyType_GenericNew(Await_type, NULL, NULL);
3268 if (!result) goto failed;
3269 value = ast2obj_expr(o->v.Await.value);
3270 if (!value) goto failed;
3271 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3272 goto failed;
3273 Py_DECREF(value);
3274 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003275 case Yield_kind:
3276 result = PyType_GenericNew(Yield_type, NULL, NULL);
3277 if (!result) goto failed;
3278 value = ast2obj_expr(o->v.Yield.value);
3279 if (!value) goto failed;
3280 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3281 goto failed;
3282 Py_DECREF(value);
3283 break;
3284 case YieldFrom_kind:
3285 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3286 if (!result) goto failed;
3287 value = ast2obj_expr(o->v.YieldFrom.value);
3288 if (!value) goto failed;
3289 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3290 goto failed;
3291 Py_DECREF(value);
3292 break;
3293 case Compare_kind:
3294 result = PyType_GenericNew(Compare_type, NULL, NULL);
3295 if (!result) goto failed;
3296 value = ast2obj_expr(o->v.Compare.left);
3297 if (!value) goto failed;
3298 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3299 goto failed;
3300 Py_DECREF(value);
3301 {
3302 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3303 value = PyList_New(n);
3304 if (!value) goto failed;
3305 for(i = 0; i < n; i++)
3306 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003308 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003309 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3310 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003311 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003312 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003313 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003314 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3315 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003316 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003317 break;
3318 case Call_kind:
3319 result = PyType_GenericNew(Call_type, NULL, NULL);
3320 if (!result) goto failed;
3321 value = ast2obj_expr(o->v.Call.func);
3322 if (!value) goto failed;
3323 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3324 goto failed;
3325 Py_DECREF(value);
3326 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3327 if (!value) goto failed;
3328 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3329 goto failed;
3330 Py_DECREF(value);
3331 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3332 if (!value) goto failed;
3333 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3334 goto failed;
3335 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003336 break;
3337 case Num_kind:
3338 result = PyType_GenericNew(Num_type, NULL, NULL);
3339 if (!result) goto failed;
3340 value = ast2obj_object(o->v.Num.n);
3341 if (!value) goto failed;
3342 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3343 goto failed;
3344 Py_DECREF(value);
3345 break;
3346 case Str_kind:
3347 result = PyType_GenericNew(Str_type, NULL, NULL);
3348 if (!result) goto failed;
3349 value = ast2obj_string(o->v.Str.s);
3350 if (!value) goto failed;
3351 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3352 goto failed;
3353 Py_DECREF(value);
3354 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003355 case FormattedValue_kind:
3356 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3357 if (!result) goto failed;
3358 value = ast2obj_expr(o->v.FormattedValue.value);
3359 if (!value) goto failed;
3360 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3361 goto failed;
3362 Py_DECREF(value);
3363 value = ast2obj_int(o->v.FormattedValue.conversion);
3364 if (!value) goto failed;
3365 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3366 goto failed;
3367 Py_DECREF(value);
3368 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3369 if (!value) goto failed;
3370 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3371 goto failed;
3372 Py_DECREF(value);
3373 break;
3374 case JoinedStr_kind:
3375 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3376 if (!result) goto failed;
3377 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3378 if (!value) goto failed;
3379 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3380 goto failed;
3381 Py_DECREF(value);
3382 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003383 case Bytes_kind:
3384 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3385 if (!result) goto failed;
3386 value = ast2obj_bytes(o->v.Bytes.s);
3387 if (!value) goto failed;
3388 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3389 goto failed;
3390 Py_DECREF(value);
3391 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003392 case NameConstant_kind:
3393 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3394 if (!result) goto failed;
3395 value = ast2obj_singleton(o->v.NameConstant.value);
3396 if (!value) goto failed;
3397 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3398 goto failed;
3399 Py_DECREF(value);
3400 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 case Ellipsis_kind:
3402 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3403 if (!result) goto failed;
3404 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003405 case Constant_kind:
3406 result = PyType_GenericNew(Constant_type, NULL, NULL);
3407 if (!result) goto failed;
3408 value = ast2obj_constant(o->v.Constant.value);
3409 if (!value) goto failed;
3410 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3411 goto failed;
3412 Py_DECREF(value);
3413 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003414 case Attribute_kind:
3415 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3416 if (!result) goto failed;
3417 value = ast2obj_expr(o->v.Attribute.value);
3418 if (!value) goto failed;
3419 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3420 goto failed;
3421 Py_DECREF(value);
3422 value = ast2obj_identifier(o->v.Attribute.attr);
3423 if (!value) goto failed;
3424 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3425 goto failed;
3426 Py_DECREF(value);
3427 value = ast2obj_expr_context(o->v.Attribute.ctx);
3428 if (!value) goto failed;
3429 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3430 goto failed;
3431 Py_DECREF(value);
3432 break;
3433 case Subscript_kind:
3434 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3435 if (!result) goto failed;
3436 value = ast2obj_expr(o->v.Subscript.value);
3437 if (!value) goto failed;
3438 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3439 goto failed;
3440 Py_DECREF(value);
3441 value = ast2obj_slice(o->v.Subscript.slice);
3442 if (!value) goto failed;
3443 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3444 goto failed;
3445 Py_DECREF(value);
3446 value = ast2obj_expr_context(o->v.Subscript.ctx);
3447 if (!value) goto failed;
3448 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3449 goto failed;
3450 Py_DECREF(value);
3451 break;
3452 case Starred_kind:
3453 result = PyType_GenericNew(Starred_type, NULL, NULL);
3454 if (!result) goto failed;
3455 value = ast2obj_expr(o->v.Starred.value);
3456 if (!value) goto failed;
3457 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3458 goto failed;
3459 Py_DECREF(value);
3460 value = ast2obj_expr_context(o->v.Starred.ctx);
3461 if (!value) goto failed;
3462 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3463 goto failed;
3464 Py_DECREF(value);
3465 break;
3466 case Name_kind:
3467 result = PyType_GenericNew(Name_type, NULL, NULL);
3468 if (!result) goto failed;
3469 value = ast2obj_identifier(o->v.Name.id);
3470 if (!value) goto failed;
3471 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3472 goto failed;
3473 Py_DECREF(value);
3474 value = ast2obj_expr_context(o->v.Name.ctx);
3475 if (!value) goto failed;
3476 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3477 goto failed;
3478 Py_DECREF(value);
3479 break;
3480 case List_kind:
3481 result = PyType_GenericNew(List_type, NULL, NULL);
3482 if (!result) goto failed;
3483 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3484 if (!value) goto failed;
3485 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3486 goto failed;
3487 Py_DECREF(value);
3488 value = ast2obj_expr_context(o->v.List.ctx);
3489 if (!value) goto failed;
3490 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3491 goto failed;
3492 Py_DECREF(value);
3493 break;
3494 case Tuple_kind:
3495 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3496 if (!result) goto failed;
3497 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3498 if (!value) goto failed;
3499 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3500 goto failed;
3501 Py_DECREF(value);
3502 value = ast2obj_expr_context(o->v.Tuple.ctx);
3503 if (!value) goto failed;
3504 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3505 goto failed;
3506 Py_DECREF(value);
3507 break;
3508 }
3509 value = ast2obj_int(o->lineno);
3510 if (!value) goto failed;
3511 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3512 goto failed;
3513 Py_DECREF(value);
3514 value = ast2obj_int(o->col_offset);
3515 if (!value) goto failed;
3516 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3517 goto failed;
3518 Py_DECREF(value);
3519 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003520failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003521 Py_XDECREF(value);
3522 Py_XDECREF(result);
3523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003526PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003528 switch(o) {
3529 case Load:
3530 Py_INCREF(Load_singleton);
3531 return Load_singleton;
3532 case Store:
3533 Py_INCREF(Store_singleton);
3534 return Store_singleton;
3535 case Del:
3536 Py_INCREF(Del_singleton);
3537 return Del_singleton;
3538 case AugLoad:
3539 Py_INCREF(AugLoad_singleton);
3540 return AugLoad_singleton;
3541 case AugStore:
3542 Py_INCREF(AugStore_singleton);
3543 return AugStore_singleton;
3544 case Param:
3545 Py_INCREF(Param_singleton);
3546 return Param_singleton;
3547 default:
3548 /* should never happen, but just in case ... */
3549 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3550 return NULL;
3551 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003553PyObject*
3554ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003556 slice_ty o = (slice_ty)_o;
3557 PyObject *result = NULL, *value = NULL;
3558 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003559 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003560 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003561
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 switch (o->kind) {
3563 case Slice_kind:
3564 result = PyType_GenericNew(Slice_type, NULL, NULL);
3565 if (!result) goto failed;
3566 value = ast2obj_expr(o->v.Slice.lower);
3567 if (!value) goto failed;
3568 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3569 goto failed;
3570 Py_DECREF(value);
3571 value = ast2obj_expr(o->v.Slice.upper);
3572 if (!value) goto failed;
3573 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3574 goto failed;
3575 Py_DECREF(value);
3576 value = ast2obj_expr(o->v.Slice.step);
3577 if (!value) goto failed;
3578 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3579 goto failed;
3580 Py_DECREF(value);
3581 break;
3582 case ExtSlice_kind:
3583 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3584 if (!result) goto failed;
3585 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3586 if (!value) goto failed;
3587 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3588 goto failed;
3589 Py_DECREF(value);
3590 break;
3591 case Index_kind:
3592 result = PyType_GenericNew(Index_type, NULL, NULL);
3593 if (!result) goto failed;
3594 value = ast2obj_expr(o->v.Index.value);
3595 if (!value) goto failed;
3596 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3597 goto failed;
3598 Py_DECREF(value);
3599 break;
3600 }
3601 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003602failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003603 Py_XDECREF(value);
3604 Py_XDECREF(result);
3605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606}
3607
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003608PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003610 switch(o) {
3611 case And:
3612 Py_INCREF(And_singleton);
3613 return And_singleton;
3614 case Or:
3615 Py_INCREF(Or_singleton);
3616 return Or_singleton;
3617 default:
3618 /* should never happen, but just in case ... */
3619 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3620 return NULL;
3621 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003622}
3623PyObject* ast2obj_operator(operator_ty o)
3624{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003625 switch(o) {
3626 case Add:
3627 Py_INCREF(Add_singleton);
3628 return Add_singleton;
3629 case Sub:
3630 Py_INCREF(Sub_singleton);
3631 return Sub_singleton;
3632 case Mult:
3633 Py_INCREF(Mult_singleton);
3634 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003635 case MatMult:
3636 Py_INCREF(MatMult_singleton);
3637 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003638 case Div:
3639 Py_INCREF(Div_singleton);
3640 return Div_singleton;
3641 case Mod:
3642 Py_INCREF(Mod_singleton);
3643 return Mod_singleton;
3644 case Pow:
3645 Py_INCREF(Pow_singleton);
3646 return Pow_singleton;
3647 case LShift:
3648 Py_INCREF(LShift_singleton);
3649 return LShift_singleton;
3650 case RShift:
3651 Py_INCREF(RShift_singleton);
3652 return RShift_singleton;
3653 case BitOr:
3654 Py_INCREF(BitOr_singleton);
3655 return BitOr_singleton;
3656 case BitXor:
3657 Py_INCREF(BitXor_singleton);
3658 return BitXor_singleton;
3659 case BitAnd:
3660 Py_INCREF(BitAnd_singleton);
3661 return BitAnd_singleton;
3662 case FloorDiv:
3663 Py_INCREF(FloorDiv_singleton);
3664 return FloorDiv_singleton;
3665 default:
3666 /* should never happen, but just in case ... */
3667 PyErr_Format(PyExc_SystemError, "unknown operator found");
3668 return NULL;
3669 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003670}
3671PyObject* ast2obj_unaryop(unaryop_ty o)
3672{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003673 switch(o) {
3674 case Invert:
3675 Py_INCREF(Invert_singleton);
3676 return Invert_singleton;
3677 case Not:
3678 Py_INCREF(Not_singleton);
3679 return Not_singleton;
3680 case UAdd:
3681 Py_INCREF(UAdd_singleton);
3682 return UAdd_singleton;
3683 case USub:
3684 Py_INCREF(USub_singleton);
3685 return USub_singleton;
3686 default:
3687 /* should never happen, but just in case ... */
3688 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3689 return NULL;
3690 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003691}
3692PyObject* ast2obj_cmpop(cmpop_ty o)
3693{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003694 switch(o) {
3695 case Eq:
3696 Py_INCREF(Eq_singleton);
3697 return Eq_singleton;
3698 case NotEq:
3699 Py_INCREF(NotEq_singleton);
3700 return NotEq_singleton;
3701 case Lt:
3702 Py_INCREF(Lt_singleton);
3703 return Lt_singleton;
3704 case LtE:
3705 Py_INCREF(LtE_singleton);
3706 return LtE_singleton;
3707 case Gt:
3708 Py_INCREF(Gt_singleton);
3709 return Gt_singleton;
3710 case GtE:
3711 Py_INCREF(GtE_singleton);
3712 return GtE_singleton;
3713 case Is:
3714 Py_INCREF(Is_singleton);
3715 return Is_singleton;
3716 case IsNot:
3717 Py_INCREF(IsNot_singleton);
3718 return IsNot_singleton;
3719 case In:
3720 Py_INCREF(In_singleton);
3721 return In_singleton;
3722 case NotIn:
3723 Py_INCREF(NotIn_singleton);
3724 return NotIn_singleton;
3725 default:
3726 /* should never happen, but just in case ... */
3727 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3728 return NULL;
3729 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003730}
3731PyObject*
3732ast2obj_comprehension(void* _o)
3733{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003734 comprehension_ty o = (comprehension_ty)_o;
3735 PyObject *result = NULL, *value = NULL;
3736 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003737 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003738 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003739
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003740 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3741 if (!result) return NULL;
3742 value = ast2obj_expr(o->target);
3743 if (!value) goto failed;
3744 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3745 goto failed;
3746 Py_DECREF(value);
3747 value = ast2obj_expr(o->iter);
3748 if (!value) goto failed;
3749 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3750 goto failed;
3751 Py_DECREF(value);
3752 value = ast2obj_list(o->ifs, ast2obj_expr);
3753 if (!value) goto failed;
3754 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3755 goto failed;
3756 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003757 value = ast2obj_int(o->is_async);
3758 if (!value) goto failed;
3759 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3760 goto failed;
3761 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003763failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003764 Py_XDECREF(value);
3765 Py_XDECREF(result);
3766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767}
3768
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003769PyObject*
3770ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003772 excepthandler_ty o = (excepthandler_ty)_o;
3773 PyObject *result = NULL, *value = NULL;
3774 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003775 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003776 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003777
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003778 switch (o->kind) {
3779 case ExceptHandler_kind:
3780 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3781 if (!result) goto failed;
3782 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003783 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003784 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3785 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003786 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003787 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003788 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003789 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3790 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003791 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3793 if (!value) goto failed;
3794 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3795 goto failed;
3796 Py_DECREF(value);
3797 break;
3798 }
3799 value = ast2obj_int(o->lineno);
3800 if (!value) goto failed;
3801 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3802 goto failed;
3803 Py_DECREF(value);
3804 value = ast2obj_int(o->col_offset);
3805 if (!value) goto failed;
3806 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3807 goto failed;
3808 Py_DECREF(value);
3809 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003810failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003811 Py_XDECREF(value);
3812 Py_XDECREF(result);
3813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814}
3815
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003816PyObject*
3817ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 arguments_ty o = (arguments_ty)_o;
3820 PyObject *result = NULL, *value = NULL;
3821 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003822 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003823 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003824
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003825 result = PyType_GenericNew(arguments_type, NULL, NULL);
3826 if (!result) return NULL;
3827 value = ast2obj_list(o->args, ast2obj_arg);
3828 if (!value) goto failed;
3829 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3830 goto failed;
3831 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003832 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003833 if (!value) goto failed;
3834 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3835 goto failed;
3836 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003837 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3838 if (!value) goto failed;
3839 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3840 goto failed;
3841 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003842 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003844 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003845 goto failed;
3846 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003847 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003848 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003849 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003850 goto failed;
3851 Py_DECREF(value);
3852 value = ast2obj_list(o->defaults, ast2obj_expr);
3853 if (!value) goto failed;
3854 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3855 goto failed;
3856 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003857 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003858failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003859 Py_XDECREF(value);
3860 Py_XDECREF(result);
3861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862}
3863
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003864PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003865ast2obj_arg(void* _o)
3866{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003867 arg_ty o = (arg_ty)_o;
3868 PyObject *result = NULL, *value = NULL;
3869 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003870 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003871 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003872
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003873 result = PyType_GenericNew(arg_type, NULL, NULL);
3874 if (!result) return NULL;
3875 value = ast2obj_identifier(o->arg);
3876 if (!value) goto failed;
3877 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3878 goto failed;
3879 Py_DECREF(value);
3880 value = ast2obj_expr(o->annotation);
3881 if (!value) goto failed;
3882 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3883 goto failed;
3884 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003885 value = ast2obj_int(o->lineno);
3886 if (!value) goto failed;
3887 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3888 goto failed;
3889 Py_DECREF(value);
3890 value = ast2obj_int(o->col_offset);
3891 if (!value) goto failed;
3892 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3893 goto failed;
3894 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003895 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003896failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003897 Py_XDECREF(value);
3898 Py_XDECREF(result);
3899 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003900}
3901
3902PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003903ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003905 keyword_ty o = (keyword_ty)_o;
3906 PyObject *result = NULL, *value = NULL;
3907 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003908 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003909 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003910
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003911 result = PyType_GenericNew(keyword_type, NULL, NULL);
3912 if (!result) return NULL;
3913 value = ast2obj_identifier(o->arg);
3914 if (!value) goto failed;
3915 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3916 goto failed;
3917 Py_DECREF(value);
3918 value = ast2obj_expr(o->value);
3919 if (!value) goto failed;
3920 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3921 goto failed;
3922 Py_DECREF(value);
3923 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003924failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003925 Py_XDECREF(value);
3926 Py_XDECREF(result);
3927 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928}
3929
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003930PyObject*
3931ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003933 alias_ty o = (alias_ty)_o;
3934 PyObject *result = NULL, *value = NULL;
3935 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003936 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003937 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003938
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003939 result = PyType_GenericNew(alias_type, NULL, NULL);
3940 if (!result) return NULL;
3941 value = ast2obj_identifier(o->name);
3942 if (!value) goto failed;
3943 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3944 goto failed;
3945 Py_DECREF(value);
3946 value = ast2obj_identifier(o->asname);
3947 if (!value) goto failed;
3948 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3949 goto failed;
3950 Py_DECREF(value);
3951 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003952failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003953 Py_XDECREF(value);
3954 Py_XDECREF(result);
3955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956}
3957
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003958PyObject*
3959ast2obj_withitem(void* _o)
3960{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003961 withitem_ty o = (withitem_ty)_o;
3962 PyObject *result = NULL, *value = NULL;
3963 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003964 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003965 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003966
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003967 result = PyType_GenericNew(withitem_type, NULL, NULL);
3968 if (!result) return NULL;
3969 value = ast2obj_expr(o->context_expr);
3970 if (!value) goto failed;
3971 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3972 goto failed;
3973 Py_DECREF(value);
3974 value = ast2obj_expr(o->optional_vars);
3975 if (!value) goto failed;
3976 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3977 goto failed;
3978 Py_DECREF(value);
3979 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003980failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003981 Py_XDECREF(value);
3982 Py_XDECREF(result);
3983 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003984}
3985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003987int
3988obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3989{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003990 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003991
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003992 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003993
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 if (obj == Py_None) {
3995 *out = NULL;
3996 return 0;
3997 }
3998 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3999 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004000 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004001 }
4002 if (isinstance) {
4003 asdl_seq* body;
INADA Naokicb41b272017-02-23 00:31:59 +09004004 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004005
4006 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4007 int res;
4008 Py_ssize_t len;
4009 Py_ssize_t i;
4010 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4011 if (tmp == NULL) goto failed;
4012 if (!PyList_Check(tmp)) {
4013 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4014 goto failed;
4015 }
4016 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004017 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004018 if (body == NULL) goto failed;
4019 for (i = 0; i < len; i++) {
4020 stmt_ty value;
4021 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4022 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004023 if (len != PyList_GET_SIZE(tmp)) {
4024 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4025 goto failed;
4026 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004027 asdl_seq_SET(body, i, value);
4028 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004029 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004030 } else {
4031 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4032 return 1;
4033 }
INADA Naokicb41b272017-02-23 00:31:59 +09004034 if (exists_not_none(obj, &PyId_docstring)) {
4035 int res;
4036 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4037 if (tmp == NULL) goto failed;
4038 res = obj2ast_string(tmp, &docstring, arena);
4039 if (res != 0) goto failed;
4040 Py_CLEAR(tmp);
4041 } else {
4042 docstring = NULL;
4043 }
4044 *out = Module(body, docstring, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004045 if (*out == NULL) goto failed;
4046 return 0;
4047 }
4048 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4049 if (isinstance == -1) {
4050 return 1;
4051 }
4052 if (isinstance) {
4053 asdl_seq* body;
4054
4055 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4056 int res;
4057 Py_ssize_t len;
4058 Py_ssize_t i;
4059 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4060 if (tmp == NULL) goto failed;
4061 if (!PyList_Check(tmp)) {
4062 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4063 goto failed;
4064 }
4065 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004066 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004067 if (body == NULL) goto failed;
4068 for (i = 0; i < len; i++) {
4069 stmt_ty value;
4070 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4071 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004072 if (len != PyList_GET_SIZE(tmp)) {
4073 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4074 goto failed;
4075 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004076 asdl_seq_SET(body, i, value);
4077 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004078 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004079 } else {
4080 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4081 return 1;
4082 }
4083 *out = Interactive(body, arena);
4084 if (*out == NULL) goto failed;
4085 return 0;
4086 }
4087 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4088 if (isinstance == -1) {
4089 return 1;
4090 }
4091 if (isinstance) {
4092 expr_ty body;
4093
4094 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4095 int res;
4096 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4097 if (tmp == NULL) goto failed;
4098 res = obj2ast_expr(tmp, &body, arena);
4099 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004100 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004101 } else {
4102 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4103 return 1;
4104 }
4105 *out = Expression(body, arena);
4106 if (*out == NULL) goto failed;
4107 return 0;
4108 }
4109 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4110 if (isinstance == -1) {
4111 return 1;
4112 }
4113 if (isinstance) {
4114 asdl_seq* body;
4115
4116 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4117 int res;
4118 Py_ssize_t len;
4119 Py_ssize_t i;
4120 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4121 if (tmp == NULL) goto failed;
4122 if (!PyList_Check(tmp)) {
4123 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4124 goto failed;
4125 }
4126 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004127 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004128 if (body == NULL) goto failed;
4129 for (i = 0; i < len; i++) {
4130 stmt_ty value;
4131 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4132 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004133 if (len != PyList_GET_SIZE(tmp)) {
4134 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4135 goto failed;
4136 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004137 asdl_seq_SET(body, i, value);
4138 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004139 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004140 } else {
4141 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4142 return 1;
4143 }
4144 *out = Suite(body, arena);
4145 if (*out == NULL) goto failed;
4146 return 0;
4147 }
4148
4149 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4150 failed:
4151 Py_XDECREF(tmp);
4152 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004153}
4154
4155int
4156obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4157{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004158 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004159
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004160 PyObject *tmp = NULL;
4161 int lineno;
4162 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004163
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004164 if (obj == Py_None) {
4165 *out = NULL;
4166 return 0;
4167 }
4168 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4169 int res;
4170 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4171 if (tmp == NULL) goto failed;
4172 res = obj2ast_int(tmp, &lineno, arena);
4173 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004174 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004175 } else {
4176 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004177 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004178 }
4179 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4180 int res;
4181 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4182 if (tmp == NULL) goto failed;
4183 res = obj2ast_int(tmp, &col_offset, arena);
4184 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004185 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004186 } else {
4187 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4188 return 1;
4189 }
4190 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4191 if (isinstance == -1) {
4192 return 1;
4193 }
4194 if (isinstance) {
4195 identifier name;
4196 arguments_ty args;
4197 asdl_seq* body;
4198 asdl_seq* decorator_list;
4199 expr_ty returns;
INADA Naokicb41b272017-02-23 00:31:59 +09004200 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004201
4202 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4203 int res;
4204 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4205 if (tmp == NULL) goto failed;
4206 res = obj2ast_identifier(tmp, &name, arena);
4207 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004208 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004209 } else {
4210 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4211 return 1;
4212 }
4213 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4214 int res;
4215 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4216 if (tmp == NULL) goto failed;
4217 res = obj2ast_arguments(tmp, &args, arena);
4218 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004219 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004220 } else {
4221 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4222 return 1;
4223 }
4224 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4225 int res;
4226 Py_ssize_t len;
4227 Py_ssize_t i;
4228 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4229 if (tmp == NULL) goto failed;
4230 if (!PyList_Check(tmp)) {
4231 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4232 goto failed;
4233 }
4234 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004235 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004236 if (body == NULL) goto failed;
4237 for (i = 0; i < len; i++) {
4238 stmt_ty value;
4239 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4240 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004241 if (len != PyList_GET_SIZE(tmp)) {
4242 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4243 goto failed;
4244 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004245 asdl_seq_SET(body, i, value);
4246 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004248 } else {
4249 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4250 return 1;
4251 }
4252 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4253 int res;
4254 Py_ssize_t len;
4255 Py_ssize_t i;
4256 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4257 if (tmp == NULL) goto failed;
4258 if (!PyList_Check(tmp)) {
4259 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4260 goto failed;
4261 }
4262 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004263 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004264 if (decorator_list == NULL) goto failed;
4265 for (i = 0; i < len; i++) {
4266 expr_ty value;
4267 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4268 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004269 if (len != PyList_GET_SIZE(tmp)) {
4270 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4271 goto failed;
4272 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004273 asdl_seq_SET(decorator_list, i, value);
4274 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004275 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004276 } else {
4277 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4278 return 1;
4279 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004280 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004281 int res;
4282 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4283 if (tmp == NULL) goto failed;
4284 res = obj2ast_expr(tmp, &returns, arena);
4285 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004286 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004287 } else {
4288 returns = NULL;
4289 }
INADA Naokicb41b272017-02-23 00:31:59 +09004290 if (exists_not_none(obj, &PyId_docstring)) {
4291 int res;
4292 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4293 if (tmp == NULL) goto failed;
4294 res = obj2ast_string(tmp, &docstring, arena);
4295 if (res != 0) goto failed;
4296 Py_CLEAR(tmp);
4297 } else {
4298 docstring = NULL;
4299 }
4300 *out = FunctionDef(name, args, body, decorator_list, returns,
4301 docstring, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004302 if (*out == NULL) goto failed;
4303 return 0;
4304 }
Yury Selivanov75445082015-05-11 22:57:16 -04004305 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4306 if (isinstance == -1) {
4307 return 1;
4308 }
4309 if (isinstance) {
4310 identifier name;
4311 arguments_ty args;
4312 asdl_seq* body;
4313 asdl_seq* decorator_list;
4314 expr_ty returns;
INADA Naokicb41b272017-02-23 00:31:59 +09004315 string docstring;
Yury Selivanov75445082015-05-11 22:57:16 -04004316
4317 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4318 int res;
4319 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4320 if (tmp == NULL) goto failed;
4321 res = obj2ast_identifier(tmp, &name, arena);
4322 if (res != 0) goto failed;
4323 Py_CLEAR(tmp);
4324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4326 return 1;
4327 }
4328 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4329 int res;
4330 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4331 if (tmp == NULL) goto failed;
4332 res = obj2ast_arguments(tmp, &args, arena);
4333 if (res != 0) goto failed;
4334 Py_CLEAR(tmp);
4335 } else {
4336 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4337 return 1;
4338 }
4339 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4340 int res;
4341 Py_ssize_t len;
4342 Py_ssize_t i;
4343 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4344 if (tmp == NULL) goto failed;
4345 if (!PyList_Check(tmp)) {
4346 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4347 goto failed;
4348 }
4349 len = PyList_GET_SIZE(tmp);
4350 body = _Py_asdl_seq_new(len, arena);
4351 if (body == NULL) goto failed;
4352 for (i = 0; i < len; i++) {
4353 stmt_ty value;
4354 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4355 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004356 if (len != PyList_GET_SIZE(tmp)) {
4357 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4358 goto failed;
4359 }
Yury Selivanov75445082015-05-11 22:57:16 -04004360 asdl_seq_SET(body, i, value);
4361 }
4362 Py_CLEAR(tmp);
4363 } else {
4364 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4365 return 1;
4366 }
4367 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4368 int res;
4369 Py_ssize_t len;
4370 Py_ssize_t i;
4371 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4372 if (tmp == NULL) goto failed;
4373 if (!PyList_Check(tmp)) {
4374 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4375 goto failed;
4376 }
4377 len = PyList_GET_SIZE(tmp);
4378 decorator_list = _Py_asdl_seq_new(len, arena);
4379 if (decorator_list == NULL) goto failed;
4380 for (i = 0; i < len; i++) {
4381 expr_ty value;
4382 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4383 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004384 if (len != PyList_GET_SIZE(tmp)) {
4385 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4386 goto failed;
4387 }
Yury Selivanov75445082015-05-11 22:57:16 -04004388 asdl_seq_SET(decorator_list, i, value);
4389 }
4390 Py_CLEAR(tmp);
4391 } else {
4392 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4393 return 1;
4394 }
4395 if (exists_not_none(obj, &PyId_returns)) {
4396 int res;
4397 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4398 if (tmp == NULL) goto failed;
4399 res = obj2ast_expr(tmp, &returns, arena);
4400 if (res != 0) goto failed;
4401 Py_CLEAR(tmp);
4402 } else {
4403 returns = NULL;
4404 }
INADA Naokicb41b272017-02-23 00:31:59 +09004405 if (exists_not_none(obj, &PyId_docstring)) {
4406 int res;
4407 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4408 if (tmp == NULL) goto failed;
4409 res = obj2ast_string(tmp, &docstring, arena);
4410 if (res != 0) goto failed;
4411 Py_CLEAR(tmp);
4412 } else {
4413 docstring = NULL;
4414 }
Yury Selivanov75445082015-05-11 22:57:16 -04004415 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09004416 docstring, lineno, col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004417 if (*out == NULL) goto failed;
4418 return 0;
4419 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004420 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4421 if (isinstance == -1) {
4422 return 1;
4423 }
4424 if (isinstance) {
4425 identifier name;
4426 asdl_seq* bases;
4427 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004428 asdl_seq* body;
4429 asdl_seq* decorator_list;
INADA Naokicb41b272017-02-23 00:31:59 +09004430 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004431
4432 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4433 int res;
4434 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4435 if (tmp == NULL) goto failed;
4436 res = obj2ast_identifier(tmp, &name, arena);
4437 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004439 } else {
4440 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4441 return 1;
4442 }
4443 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4444 int res;
4445 Py_ssize_t len;
4446 Py_ssize_t i;
4447 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4448 if (tmp == NULL) goto failed;
4449 if (!PyList_Check(tmp)) {
4450 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4451 goto failed;
4452 }
4453 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004454 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004455 if (bases == NULL) goto failed;
4456 for (i = 0; i < len; i++) {
4457 expr_ty value;
4458 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4459 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004460 if (len != PyList_GET_SIZE(tmp)) {
4461 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4462 goto failed;
4463 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004464 asdl_seq_SET(bases, i, value);
4465 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004466 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 } else {
4468 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4469 return 1;
4470 }
4471 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4472 int res;
4473 Py_ssize_t len;
4474 Py_ssize_t i;
4475 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4476 if (tmp == NULL) goto failed;
4477 if (!PyList_Check(tmp)) {
4478 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4479 goto failed;
4480 }
4481 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004482 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004483 if (keywords == NULL) goto failed;
4484 for (i = 0; i < len; i++) {
4485 keyword_ty value;
4486 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4487 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004488 if (len != PyList_GET_SIZE(tmp)) {
4489 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4490 goto failed;
4491 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004492 asdl_seq_SET(keywords, i, value);
4493 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004494 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004495 } else {
4496 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4497 return 1;
4498 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004499 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4500 int res;
4501 Py_ssize_t len;
4502 Py_ssize_t i;
4503 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4504 if (tmp == NULL) goto failed;
4505 if (!PyList_Check(tmp)) {
4506 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4507 goto failed;
4508 }
4509 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004510 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004511 if (body == NULL) goto failed;
4512 for (i = 0; i < len; i++) {
4513 stmt_ty value;
4514 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4515 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004516 if (len != PyList_GET_SIZE(tmp)) {
4517 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4518 goto failed;
4519 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004520 asdl_seq_SET(body, i, value);
4521 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004522 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004523 } else {
4524 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4525 return 1;
4526 }
4527 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4528 int res;
4529 Py_ssize_t len;
4530 Py_ssize_t i;
4531 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4532 if (tmp == NULL) goto failed;
4533 if (!PyList_Check(tmp)) {
4534 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4535 goto failed;
4536 }
4537 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004538 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004539 if (decorator_list == NULL) goto failed;
4540 for (i = 0; i < len; i++) {
4541 expr_ty value;
4542 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4543 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004544 if (len != PyList_GET_SIZE(tmp)) {
4545 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4546 goto failed;
4547 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004548 asdl_seq_SET(decorator_list, i, value);
4549 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004550 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004551 } else {
4552 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4553 return 1;
4554 }
INADA Naokicb41b272017-02-23 00:31:59 +09004555 if (exists_not_none(obj, &PyId_docstring)) {
4556 int res;
4557 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4558 if (tmp == NULL) goto failed;
4559 res = obj2ast_string(tmp, &docstring, arena);
4560 if (res != 0) goto failed;
4561 Py_CLEAR(tmp);
4562 } else {
4563 docstring = NULL;
4564 }
4565 *out = ClassDef(name, bases, keywords, body, decorator_list, docstring,
4566 lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004567 if (*out == NULL) goto failed;
4568 return 0;
4569 }
4570 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4571 if (isinstance == -1) {
4572 return 1;
4573 }
4574 if (isinstance) {
4575 expr_ty value;
4576
Victor Stinneree4b59c2013-07-27 00:01:35 +02004577 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004578 int res;
4579 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_expr(tmp, &value, arena);
4582 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004583 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004584 } else {
4585 value = NULL;
4586 }
4587 *out = Return(value, lineno, col_offset, arena);
4588 if (*out == NULL) goto failed;
4589 return 0;
4590 }
4591 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4592 if (isinstance == -1) {
4593 return 1;
4594 }
4595 if (isinstance) {
4596 asdl_seq* targets;
4597
4598 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4599 int res;
4600 Py_ssize_t len;
4601 Py_ssize_t i;
4602 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4603 if (tmp == NULL) goto failed;
4604 if (!PyList_Check(tmp)) {
4605 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4606 goto failed;
4607 }
4608 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004609 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004610 if (targets == NULL) goto failed;
4611 for (i = 0; i < len; i++) {
4612 expr_ty value;
4613 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4614 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004615 if (len != PyList_GET_SIZE(tmp)) {
4616 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4617 goto failed;
4618 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004619 asdl_seq_SET(targets, i, value);
4620 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004621 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004622 } else {
4623 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4624 return 1;
4625 }
4626 *out = Delete(targets, lineno, col_offset, arena);
4627 if (*out == NULL) goto failed;
4628 return 0;
4629 }
4630 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4631 if (isinstance == -1) {
4632 return 1;
4633 }
4634 if (isinstance) {
4635 asdl_seq* targets;
4636 expr_ty value;
4637
4638 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4639 int res;
4640 Py_ssize_t len;
4641 Py_ssize_t i;
4642 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4643 if (tmp == NULL) goto failed;
4644 if (!PyList_Check(tmp)) {
4645 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4646 goto failed;
4647 }
4648 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004649 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004650 if (targets == NULL) goto failed;
4651 for (i = 0; i < len; i++) {
4652 expr_ty value;
4653 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4654 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004655 if (len != PyList_GET_SIZE(tmp)) {
4656 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4657 goto failed;
4658 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004659 asdl_seq_SET(targets, i, value);
4660 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004661 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004662 } else {
4663 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4664 return 1;
4665 }
4666 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4667 int res;
4668 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4669 if (tmp == NULL) goto failed;
4670 res = obj2ast_expr(tmp, &value, arena);
4671 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004672 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004673 } else {
4674 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4675 return 1;
4676 }
4677 *out = Assign(targets, value, lineno, col_offset, arena);
4678 if (*out == NULL) goto failed;
4679 return 0;
4680 }
4681 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4682 if (isinstance == -1) {
4683 return 1;
4684 }
4685 if (isinstance) {
4686 expr_ty target;
4687 operator_ty op;
4688 expr_ty value;
4689
4690 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4691 int res;
4692 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4693 if (tmp == NULL) goto failed;
4694 res = obj2ast_expr(tmp, &target, arena);
4695 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004696 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004697 } else {
4698 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4699 return 1;
4700 }
4701 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4702 int res;
4703 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4704 if (tmp == NULL) goto failed;
4705 res = obj2ast_operator(tmp, &op, arena);
4706 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004707 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004708 } else {
4709 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4710 return 1;
4711 }
4712 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4713 int res;
4714 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4715 if (tmp == NULL) goto failed;
4716 res = obj2ast_expr(tmp, &value, arena);
4717 if (res != 0) goto failed;
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 \"value\" missing from AugAssign");
4721 return 1;
4722 }
4723 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4724 if (*out == NULL) goto failed;
4725 return 0;
4726 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004727 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4728 if (isinstance == -1) {
4729 return 1;
4730 }
4731 if (isinstance) {
4732 expr_ty target;
4733 expr_ty annotation;
4734 expr_ty value;
4735 int simple;
4736
4737 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4738 int res;
4739 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4740 if (tmp == NULL) goto failed;
4741 res = obj2ast_expr(tmp, &target, arena);
4742 if (res != 0) goto failed;
4743 Py_CLEAR(tmp);
4744 } else {
4745 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4746 return 1;
4747 }
4748 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4749 int res;
4750 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4751 if (tmp == NULL) goto failed;
4752 res = obj2ast_expr(tmp, &annotation, arena);
4753 if (res != 0) goto failed;
4754 Py_CLEAR(tmp);
4755 } else {
4756 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4757 return 1;
4758 }
4759 if (exists_not_none(obj, &PyId_value)) {
4760 int res;
4761 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4762 if (tmp == NULL) goto failed;
4763 res = obj2ast_expr(tmp, &value, arena);
4764 if (res != 0) goto failed;
4765 Py_CLEAR(tmp);
4766 } else {
4767 value = NULL;
4768 }
4769 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4770 int res;
4771 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4772 if (tmp == NULL) goto failed;
4773 res = obj2ast_int(tmp, &simple, arena);
4774 if (res != 0) goto failed;
4775 Py_CLEAR(tmp);
4776 } else {
4777 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4778 return 1;
4779 }
4780 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4781 arena);
4782 if (*out == NULL) goto failed;
4783 return 0;
4784 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004785 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4786 if (isinstance == -1) {
4787 return 1;
4788 }
4789 if (isinstance) {
4790 expr_ty target;
4791 expr_ty iter;
4792 asdl_seq* body;
4793 asdl_seq* orelse;
4794
4795 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4796 int res;
4797 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4798 if (tmp == NULL) goto failed;
4799 res = obj2ast_expr(tmp, &target, arena);
4800 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004801 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004802 } else {
4803 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4804 return 1;
4805 }
4806 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4807 int res;
4808 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4809 if (tmp == NULL) goto failed;
4810 res = obj2ast_expr(tmp, &iter, arena);
4811 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004812 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004813 } else {
4814 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4815 return 1;
4816 }
4817 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4818 int res;
4819 Py_ssize_t len;
4820 Py_ssize_t i;
4821 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4822 if (tmp == NULL) goto failed;
4823 if (!PyList_Check(tmp)) {
4824 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4825 goto failed;
4826 }
4827 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004828 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004829 if (body == NULL) goto failed;
4830 for (i = 0; i < len; i++) {
4831 stmt_ty value;
4832 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4833 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004834 if (len != PyList_GET_SIZE(tmp)) {
4835 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4836 goto failed;
4837 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004838 asdl_seq_SET(body, i, value);
4839 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004841 } else {
4842 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4843 return 1;
4844 }
4845 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4846 int res;
4847 Py_ssize_t len;
4848 Py_ssize_t i;
4849 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4850 if (tmp == NULL) goto failed;
4851 if (!PyList_Check(tmp)) {
4852 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4853 goto failed;
4854 }
4855 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004856 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004857 if (orelse == NULL) goto failed;
4858 for (i = 0; i < len; i++) {
4859 stmt_ty value;
4860 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4861 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004862 if (len != PyList_GET_SIZE(tmp)) {
4863 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4864 goto failed;
4865 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004866 asdl_seq_SET(orelse, i, value);
4867 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004868 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004869 } else {
4870 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4871 return 1;
4872 }
4873 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4874 if (*out == NULL) goto failed;
4875 return 0;
4876 }
Yury Selivanov75445082015-05-11 22:57:16 -04004877 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4878 if (isinstance == -1) {
4879 return 1;
4880 }
4881 if (isinstance) {
4882 expr_ty target;
4883 expr_ty iter;
4884 asdl_seq* body;
4885 asdl_seq* orelse;
4886
4887 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4888 int res;
4889 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4890 if (tmp == NULL) goto failed;
4891 res = obj2ast_expr(tmp, &target, arena);
4892 if (res != 0) goto failed;
4893 Py_CLEAR(tmp);
4894 } else {
4895 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4896 return 1;
4897 }
4898 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4899 int res;
4900 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4901 if (tmp == NULL) goto failed;
4902 res = obj2ast_expr(tmp, &iter, arena);
4903 if (res != 0) goto failed;
4904 Py_CLEAR(tmp);
4905 } else {
4906 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4907 return 1;
4908 }
4909 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4910 int res;
4911 Py_ssize_t len;
4912 Py_ssize_t i;
4913 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4914 if (tmp == NULL) goto failed;
4915 if (!PyList_Check(tmp)) {
4916 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4917 goto failed;
4918 }
4919 len = PyList_GET_SIZE(tmp);
4920 body = _Py_asdl_seq_new(len, arena);
4921 if (body == NULL) goto failed;
4922 for (i = 0; i < len; i++) {
4923 stmt_ty value;
4924 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4925 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004926 if (len != PyList_GET_SIZE(tmp)) {
4927 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4928 goto failed;
4929 }
Yury Selivanov75445082015-05-11 22:57:16 -04004930 asdl_seq_SET(body, i, value);
4931 }
4932 Py_CLEAR(tmp);
4933 } else {
4934 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4935 return 1;
4936 }
4937 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4938 int res;
4939 Py_ssize_t len;
4940 Py_ssize_t i;
4941 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4942 if (tmp == NULL) goto failed;
4943 if (!PyList_Check(tmp)) {
4944 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4945 goto failed;
4946 }
4947 len = PyList_GET_SIZE(tmp);
4948 orelse = _Py_asdl_seq_new(len, arena);
4949 if (orelse == NULL) goto failed;
4950 for (i = 0; i < len; i++) {
4951 stmt_ty value;
4952 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4953 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004954 if (len != PyList_GET_SIZE(tmp)) {
4955 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4956 goto failed;
4957 }
Yury Selivanov75445082015-05-11 22:57:16 -04004958 asdl_seq_SET(orelse, i, value);
4959 }
4960 Py_CLEAR(tmp);
4961 } else {
4962 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4963 return 1;
4964 }
4965 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4966 if (*out == NULL) goto failed;
4967 return 0;
4968 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004969 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4970 if (isinstance == -1) {
4971 return 1;
4972 }
4973 if (isinstance) {
4974 expr_ty test;
4975 asdl_seq* body;
4976 asdl_seq* orelse;
4977
4978 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4979 int res;
4980 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4981 if (tmp == NULL) goto failed;
4982 res = obj2ast_expr(tmp, &test, arena);
4983 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004984 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004985 } else {
4986 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4987 return 1;
4988 }
4989 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4990 int res;
4991 Py_ssize_t len;
4992 Py_ssize_t i;
4993 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4994 if (tmp == NULL) goto failed;
4995 if (!PyList_Check(tmp)) {
4996 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4997 goto failed;
4998 }
4999 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005000 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005001 if (body == NULL) goto failed;
5002 for (i = 0; i < len; i++) {
5003 stmt_ty value;
5004 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5005 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005006 if (len != PyList_GET_SIZE(tmp)) {
5007 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
5008 goto failed;
5009 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005010 asdl_seq_SET(body, i, value);
5011 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005012 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005013 } else {
5014 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
5015 return 1;
5016 }
5017 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5018 int res;
5019 Py_ssize_t len;
5020 Py_ssize_t i;
5021 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5022 if (tmp == NULL) goto failed;
5023 if (!PyList_Check(tmp)) {
5024 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5025 goto failed;
5026 }
5027 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005028 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005029 if (orelse == NULL) goto failed;
5030 for (i = 0; i < len; i++) {
5031 stmt_ty value;
5032 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5033 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005034 if (len != PyList_GET_SIZE(tmp)) {
5035 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
5036 goto failed;
5037 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005038 asdl_seq_SET(orelse, i, value);
5039 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005041 } else {
5042 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
5043 return 1;
5044 }
5045 *out = While(test, body, orelse, lineno, col_offset, arena);
5046 if (*out == NULL) goto failed;
5047 return 0;
5048 }
5049 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
5050 if (isinstance == -1) {
5051 return 1;
5052 }
5053 if (isinstance) {
5054 expr_ty test;
5055 asdl_seq* body;
5056 asdl_seq* orelse;
5057
5058 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5059 int res;
5060 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5061 if (tmp == NULL) goto failed;
5062 res = obj2ast_expr(tmp, &test, arena);
5063 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005064 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005065 } else {
5066 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5067 return 1;
5068 }
5069 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5070 int res;
5071 Py_ssize_t len;
5072 Py_ssize_t i;
5073 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5074 if (tmp == NULL) goto failed;
5075 if (!PyList_Check(tmp)) {
5076 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5077 goto failed;
5078 }
5079 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005080 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005081 if (body == NULL) goto failed;
5082 for (i = 0; i < len; i++) {
5083 stmt_ty value;
5084 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5085 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005086 if (len != PyList_GET_SIZE(tmp)) {
5087 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5088 goto failed;
5089 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005090 asdl_seq_SET(body, i, value);
5091 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005092 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005093 } else {
5094 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5095 return 1;
5096 }
5097 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5098 int res;
5099 Py_ssize_t len;
5100 Py_ssize_t i;
5101 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5102 if (tmp == NULL) goto failed;
5103 if (!PyList_Check(tmp)) {
5104 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5105 goto failed;
5106 }
5107 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005108 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005109 if (orelse == NULL) goto failed;
5110 for (i = 0; i < len; i++) {
5111 stmt_ty value;
5112 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5113 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005114 if (len != PyList_GET_SIZE(tmp)) {
5115 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5116 goto failed;
5117 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005118 asdl_seq_SET(orelse, i, value);
5119 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005120 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005121 } else {
5122 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5123 return 1;
5124 }
5125 *out = If(test, body, orelse, lineno, col_offset, arena);
5126 if (*out == NULL) goto failed;
5127 return 0;
5128 }
5129 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5130 if (isinstance == -1) {
5131 return 1;
5132 }
5133 if (isinstance) {
5134 asdl_seq* items;
5135 asdl_seq* body;
5136
5137 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5138 int res;
5139 Py_ssize_t len;
5140 Py_ssize_t i;
5141 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5142 if (tmp == NULL) goto failed;
5143 if (!PyList_Check(tmp)) {
5144 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5145 goto failed;
5146 }
5147 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005148 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005149 if (items == NULL) goto failed;
5150 for (i = 0; i < len; i++) {
5151 withitem_ty value;
5152 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5153 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005154 if (len != PyList_GET_SIZE(tmp)) {
5155 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5156 goto failed;
5157 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005158 asdl_seq_SET(items, 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 \"items\" missing from With");
5163 return 1;
5164 }
5165 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5166 int res;
5167 Py_ssize_t len;
5168 Py_ssize_t i;
5169 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5170 if (tmp == NULL) goto failed;
5171 if (!PyList_Check(tmp)) {
5172 PyErr_Format(PyExc_TypeError, "With field \"body\" 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 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005177 if (body == NULL) goto failed;
5178 for (i = 0; i < len; i++) {
5179 stmt_ty value;
5180 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5181 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005182 if (len != PyList_GET_SIZE(tmp)) {
5183 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5184 goto failed;
5185 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005186 asdl_seq_SET(body, i, value);
5187 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005188 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005189 } else {
5190 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5191 return 1;
5192 }
5193 *out = With(items, body, lineno, col_offset, arena);
5194 if (*out == NULL) goto failed;
5195 return 0;
5196 }
Yury Selivanov75445082015-05-11 22:57:16 -04005197 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5198 if (isinstance == -1) {
5199 return 1;
5200 }
5201 if (isinstance) {
5202 asdl_seq* items;
5203 asdl_seq* body;
5204
5205 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5206 int res;
5207 Py_ssize_t len;
5208 Py_ssize_t i;
5209 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5210 if (tmp == NULL) goto failed;
5211 if (!PyList_Check(tmp)) {
5212 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5213 goto failed;
5214 }
5215 len = PyList_GET_SIZE(tmp);
5216 items = _Py_asdl_seq_new(len, arena);
5217 if (items == NULL) goto failed;
5218 for (i = 0; i < len; i++) {
5219 withitem_ty value;
5220 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
5221 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005222 if (len != PyList_GET_SIZE(tmp)) {
5223 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5224 goto failed;
5225 }
Yury Selivanov75445082015-05-11 22:57:16 -04005226 asdl_seq_SET(items, i, value);
5227 }
5228 Py_CLEAR(tmp);
5229 } else {
5230 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5231 return 1;
5232 }
5233 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5234 int res;
5235 Py_ssize_t len;
5236 Py_ssize_t i;
5237 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5238 if (tmp == NULL) goto failed;
5239 if (!PyList_Check(tmp)) {
5240 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5241 goto failed;
5242 }
5243 len = PyList_GET_SIZE(tmp);
5244 body = _Py_asdl_seq_new(len, arena);
5245 if (body == NULL) goto failed;
5246 for (i = 0; i < len; i++) {
5247 stmt_ty value;
5248 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5249 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005250 if (len != PyList_GET_SIZE(tmp)) {
5251 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5252 goto failed;
5253 }
Yury Selivanov75445082015-05-11 22:57:16 -04005254 asdl_seq_SET(body, i, value);
5255 }
5256 Py_CLEAR(tmp);
5257 } else {
5258 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5259 return 1;
5260 }
5261 *out = AsyncWith(items, body, lineno, col_offset, arena);
5262 if (*out == NULL) goto failed;
5263 return 0;
5264 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005265 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5266 if (isinstance == -1) {
5267 return 1;
5268 }
5269 if (isinstance) {
5270 expr_ty exc;
5271 expr_ty cause;
5272
Victor Stinneree4b59c2013-07-27 00:01:35 +02005273 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005274 int res;
5275 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5276 if (tmp == NULL) goto failed;
5277 res = obj2ast_expr(tmp, &exc, arena);
5278 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005279 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005280 } else {
5281 exc = NULL;
5282 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005283 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005284 int res;
5285 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5286 if (tmp == NULL) goto failed;
5287 res = obj2ast_expr(tmp, &cause, arena);
5288 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005289 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005290 } else {
5291 cause = NULL;
5292 }
5293 *out = Raise(exc, cause, lineno, col_offset, arena);
5294 if (*out == NULL) goto failed;
5295 return 0;
5296 }
5297 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5298 if (isinstance == -1) {
5299 return 1;
5300 }
5301 if (isinstance) {
5302 asdl_seq* body;
5303 asdl_seq* handlers;
5304 asdl_seq* orelse;
5305 asdl_seq* finalbody;
5306
5307 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5308 int res;
5309 Py_ssize_t len;
5310 Py_ssize_t i;
5311 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5312 if (tmp == NULL) goto failed;
5313 if (!PyList_Check(tmp)) {
5314 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5315 goto failed;
5316 }
5317 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005318 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005319 if (body == NULL) goto failed;
5320 for (i = 0; i < len; i++) {
5321 stmt_ty value;
5322 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5323 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005324 if (len != PyList_GET_SIZE(tmp)) {
5325 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5326 goto failed;
5327 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005328 asdl_seq_SET(body, i, value);
5329 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005330 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005331 } else {
5332 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5333 return 1;
5334 }
5335 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5336 int res;
5337 Py_ssize_t len;
5338 Py_ssize_t i;
5339 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5340 if (tmp == NULL) goto failed;
5341 if (!PyList_Check(tmp)) {
5342 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5343 goto failed;
5344 }
5345 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005346 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005347 if (handlers == NULL) goto failed;
5348 for (i = 0; i < len; i++) {
5349 excepthandler_ty value;
5350 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5351 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005352 if (len != PyList_GET_SIZE(tmp)) {
5353 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5354 goto failed;
5355 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005356 asdl_seq_SET(handlers, i, value);
5357 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005358 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005359 } else {
5360 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5361 return 1;
5362 }
5363 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5364 int res;
5365 Py_ssize_t len;
5366 Py_ssize_t i;
5367 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5368 if (tmp == NULL) goto failed;
5369 if (!PyList_Check(tmp)) {
5370 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5371 goto failed;
5372 }
5373 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005374 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005375 if (orelse == NULL) goto failed;
5376 for (i = 0; i < len; i++) {
5377 stmt_ty value;
5378 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5379 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005380 if (len != PyList_GET_SIZE(tmp)) {
5381 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5382 goto failed;
5383 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005384 asdl_seq_SET(orelse, i, value);
5385 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005386 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005387 } else {
5388 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5389 return 1;
5390 }
5391 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5392 int res;
5393 Py_ssize_t len;
5394 Py_ssize_t i;
5395 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5396 if (tmp == NULL) goto failed;
5397 if (!PyList_Check(tmp)) {
5398 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5399 goto failed;
5400 }
5401 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005402 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005403 if (finalbody == NULL) goto failed;
5404 for (i = 0; i < len; i++) {
5405 stmt_ty value;
5406 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5407 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005408 if (len != PyList_GET_SIZE(tmp)) {
5409 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5410 goto failed;
5411 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005412 asdl_seq_SET(finalbody, i, value);
5413 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005414 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5417 return 1;
5418 }
5419 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5420 arena);
5421 if (*out == NULL) goto failed;
5422 return 0;
5423 }
5424 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5425 if (isinstance == -1) {
5426 return 1;
5427 }
5428 if (isinstance) {
5429 expr_ty test;
5430 expr_ty msg;
5431
5432 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5433 int res;
5434 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5435 if (tmp == NULL) goto failed;
5436 res = obj2ast_expr(tmp, &test, arena);
5437 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005439 } else {
5440 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5441 return 1;
5442 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005443 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005444 int res;
5445 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5446 if (tmp == NULL) goto failed;
5447 res = obj2ast_expr(tmp, &msg, arena);
5448 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005449 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005450 } else {
5451 msg = NULL;
5452 }
5453 *out = Assert(test, msg, lineno, col_offset, arena);
5454 if (*out == NULL) goto failed;
5455 return 0;
5456 }
5457 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5458 if (isinstance == -1) {
5459 return 1;
5460 }
5461 if (isinstance) {
5462 asdl_seq* names;
5463
5464 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5465 int res;
5466 Py_ssize_t len;
5467 Py_ssize_t i;
5468 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5469 if (tmp == NULL) goto failed;
5470 if (!PyList_Check(tmp)) {
5471 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5472 goto failed;
5473 }
5474 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005475 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005476 if (names == NULL) goto failed;
5477 for (i = 0; i < len; i++) {
5478 alias_ty value;
5479 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5480 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005481 if (len != PyList_GET_SIZE(tmp)) {
5482 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5483 goto failed;
5484 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005485 asdl_seq_SET(names, i, value);
5486 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005487 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005488 } else {
5489 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5490 return 1;
5491 }
5492 *out = Import(names, lineno, col_offset, arena);
5493 if (*out == NULL) goto failed;
5494 return 0;
5495 }
5496 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5497 if (isinstance == -1) {
5498 return 1;
5499 }
5500 if (isinstance) {
5501 identifier module;
5502 asdl_seq* names;
5503 int level;
5504
Victor Stinneree4b59c2013-07-27 00:01:35 +02005505 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005506 int res;
5507 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5508 if (tmp == NULL) goto failed;
5509 res = obj2ast_identifier(tmp, &module, arena);
5510 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005511 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005512 } else {
5513 module = NULL;
5514 }
5515 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5516 int res;
5517 Py_ssize_t len;
5518 Py_ssize_t i;
5519 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5520 if (tmp == NULL) goto failed;
5521 if (!PyList_Check(tmp)) {
5522 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5523 goto failed;
5524 }
5525 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005526 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005527 if (names == NULL) goto failed;
5528 for (i = 0; i < len; i++) {
5529 alias_ty value;
5530 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5531 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005532 if (len != PyList_GET_SIZE(tmp)) {
5533 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5534 goto failed;
5535 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005536 asdl_seq_SET(names, i, value);
5537 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005539 } else {
5540 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5541 return 1;
5542 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005543 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005544 int res;
5545 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5546 if (tmp == NULL) goto failed;
5547 res = obj2ast_int(tmp, &level, arena);
5548 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005549 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005550 } else {
5551 level = 0;
5552 }
5553 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5554 if (*out == NULL) goto failed;
5555 return 0;
5556 }
5557 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5558 if (isinstance == -1) {
5559 return 1;
5560 }
5561 if (isinstance) {
5562 asdl_seq* names;
5563
5564 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5565 int res;
5566 Py_ssize_t len;
5567 Py_ssize_t i;
5568 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5569 if (tmp == NULL) goto failed;
5570 if (!PyList_Check(tmp)) {
5571 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5572 goto failed;
5573 }
5574 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005575 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005576 if (names == NULL) goto failed;
5577 for (i = 0; i < len; i++) {
5578 identifier value;
5579 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5580 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005581 if (len != PyList_GET_SIZE(tmp)) {
5582 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5583 goto failed;
5584 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005585 asdl_seq_SET(names, i, value);
5586 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005587 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005588 } else {
5589 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5590 return 1;
5591 }
5592 *out = Global(names, lineno, col_offset, arena);
5593 if (*out == NULL) goto failed;
5594 return 0;
5595 }
5596 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5597 if (isinstance == -1) {
5598 return 1;
5599 }
5600 if (isinstance) {
5601 asdl_seq* names;
5602
5603 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5604 int res;
5605 Py_ssize_t len;
5606 Py_ssize_t i;
5607 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5608 if (tmp == NULL) goto failed;
5609 if (!PyList_Check(tmp)) {
5610 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5611 goto failed;
5612 }
5613 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005614 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005615 if (names == NULL) goto failed;
5616 for (i = 0; i < len; i++) {
5617 identifier value;
5618 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5619 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005620 if (len != PyList_GET_SIZE(tmp)) {
5621 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5622 goto failed;
5623 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005624 asdl_seq_SET(names, i, value);
5625 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005626 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005627 } else {
5628 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5629 return 1;
5630 }
5631 *out = Nonlocal(names, lineno, col_offset, arena);
5632 if (*out == NULL) goto failed;
5633 return 0;
5634 }
5635 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5636 if (isinstance == -1) {
5637 return 1;
5638 }
5639 if (isinstance) {
5640 expr_ty value;
5641
5642 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5643 int res;
5644 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5645 if (tmp == NULL) goto failed;
5646 res = obj2ast_expr(tmp, &value, arena);
5647 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005648 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005649 } else {
5650 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5651 return 1;
5652 }
5653 *out = Expr(value, lineno, col_offset, arena);
5654 if (*out == NULL) goto failed;
5655 return 0;
5656 }
5657 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5658 if (isinstance == -1) {
5659 return 1;
5660 }
5661 if (isinstance) {
5662
5663 *out = Pass(lineno, col_offset, arena);
5664 if (*out == NULL) goto failed;
5665 return 0;
5666 }
5667 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5668 if (isinstance == -1) {
5669 return 1;
5670 }
5671 if (isinstance) {
5672
5673 *out = Break(lineno, col_offset, arena);
5674 if (*out == NULL) goto failed;
5675 return 0;
5676 }
5677 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5678 if (isinstance == -1) {
5679 return 1;
5680 }
5681 if (isinstance) {
5682
5683 *out = Continue(lineno, col_offset, arena);
5684 if (*out == NULL) goto failed;
5685 return 0;
5686 }
5687
5688 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5689 failed:
5690 Py_XDECREF(tmp);
5691 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005692}
5693
5694int
5695obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5696{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005697 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005698
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005699 PyObject *tmp = NULL;
5700 int lineno;
5701 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005702
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005703 if (obj == Py_None) {
5704 *out = NULL;
5705 return 0;
5706 }
5707 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5708 int res;
5709 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5710 if (tmp == NULL) goto failed;
5711 res = obj2ast_int(tmp, &lineno, arena);
5712 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005713 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005714 } else {
5715 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005716 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005717 }
5718 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5719 int res;
5720 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5721 if (tmp == NULL) goto failed;
5722 res = obj2ast_int(tmp, &col_offset, arena);
5723 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005724 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005725 } else {
5726 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5727 return 1;
5728 }
5729 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5730 if (isinstance == -1) {
5731 return 1;
5732 }
5733 if (isinstance) {
5734 boolop_ty op;
5735 asdl_seq* values;
5736
5737 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5738 int res;
5739 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5740 if (tmp == NULL) goto failed;
5741 res = obj2ast_boolop(tmp, &op, arena);
5742 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005743 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005744 } else {
5745 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5746 return 1;
5747 }
5748 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5749 int res;
5750 Py_ssize_t len;
5751 Py_ssize_t i;
5752 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5753 if (tmp == NULL) goto failed;
5754 if (!PyList_Check(tmp)) {
5755 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5756 goto failed;
5757 }
5758 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005759 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005760 if (values == NULL) goto failed;
5761 for (i = 0; i < len; i++) {
5762 expr_ty value;
5763 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5764 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005765 if (len != PyList_GET_SIZE(tmp)) {
5766 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5767 goto failed;
5768 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005769 asdl_seq_SET(values, i, value);
5770 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005771 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005772 } else {
5773 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5774 return 1;
5775 }
5776 *out = BoolOp(op, values, lineno, col_offset, arena);
5777 if (*out == NULL) goto failed;
5778 return 0;
5779 }
5780 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5781 if (isinstance == -1) {
5782 return 1;
5783 }
5784 if (isinstance) {
5785 expr_ty left;
5786 operator_ty op;
5787 expr_ty right;
5788
5789 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5790 int res;
5791 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5792 if (tmp == NULL) goto failed;
5793 res = obj2ast_expr(tmp, &left, arena);
5794 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005795 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005796 } else {
5797 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5798 return 1;
5799 }
5800 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5801 int res;
5802 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5803 if (tmp == NULL) goto failed;
5804 res = obj2ast_operator(tmp, &op, arena);
5805 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005806 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005807 } else {
5808 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5809 return 1;
5810 }
5811 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5812 int res;
5813 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5814 if (tmp == NULL) goto failed;
5815 res = obj2ast_expr(tmp, &right, arena);
5816 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005817 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005818 } else {
5819 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5820 return 1;
5821 }
5822 *out = BinOp(left, op, right, lineno, col_offset, arena);
5823 if (*out == NULL) goto failed;
5824 return 0;
5825 }
5826 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5827 if (isinstance == -1) {
5828 return 1;
5829 }
5830 if (isinstance) {
5831 unaryop_ty op;
5832 expr_ty operand;
5833
5834 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5835 int res;
5836 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5837 if (tmp == NULL) goto failed;
5838 res = obj2ast_unaryop(tmp, &op, arena);
5839 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 } else {
5842 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5843 return 1;
5844 }
5845 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5846 int res;
5847 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5848 if (tmp == NULL) goto failed;
5849 res = obj2ast_expr(tmp, &operand, arena);
5850 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005851 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005852 } else {
5853 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5854 return 1;
5855 }
5856 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5857 if (*out == NULL) goto failed;
5858 return 0;
5859 }
5860 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5861 if (isinstance == -1) {
5862 return 1;
5863 }
5864 if (isinstance) {
5865 arguments_ty args;
5866 expr_ty body;
5867
5868 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5869 int res;
5870 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5871 if (tmp == NULL) goto failed;
5872 res = obj2ast_arguments(tmp, &args, arena);
5873 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005874 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005875 } else {
5876 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5877 return 1;
5878 }
5879 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5880 int res;
5881 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5882 if (tmp == NULL) goto failed;
5883 res = obj2ast_expr(tmp, &body, arena);
5884 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005885 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005886 } else {
5887 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5888 return 1;
5889 }
5890 *out = Lambda(args, body, lineno, col_offset, arena);
5891 if (*out == NULL) goto failed;
5892 return 0;
5893 }
5894 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5895 if (isinstance == -1) {
5896 return 1;
5897 }
5898 if (isinstance) {
5899 expr_ty test;
5900 expr_ty body;
5901 expr_ty orelse;
5902
5903 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5904 int res;
5905 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5906 if (tmp == NULL) goto failed;
5907 res = obj2ast_expr(tmp, &test, arena);
5908 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005909 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005910 } else {
5911 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5912 return 1;
5913 }
5914 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5915 int res;
5916 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5917 if (tmp == NULL) goto failed;
5918 res = obj2ast_expr(tmp, &body, arena);
5919 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005920 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005921 } else {
5922 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5923 return 1;
5924 }
5925 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5926 int res;
5927 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5928 if (tmp == NULL) goto failed;
5929 res = obj2ast_expr(tmp, &orelse, arena);
5930 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005931 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005932 } else {
5933 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5934 return 1;
5935 }
5936 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5937 if (*out == NULL) goto failed;
5938 return 0;
5939 }
5940 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5941 if (isinstance == -1) {
5942 return 1;
5943 }
5944 if (isinstance) {
5945 asdl_seq* keys;
5946 asdl_seq* values;
5947
5948 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5949 int res;
5950 Py_ssize_t len;
5951 Py_ssize_t i;
5952 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5953 if (tmp == NULL) goto failed;
5954 if (!PyList_Check(tmp)) {
5955 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5956 goto failed;
5957 }
5958 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005959 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005960 if (keys == NULL) goto failed;
5961 for (i = 0; i < len; i++) {
5962 expr_ty value;
5963 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5964 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005965 if (len != PyList_GET_SIZE(tmp)) {
5966 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5967 goto failed;
5968 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005969 asdl_seq_SET(keys, i, value);
5970 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005971 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005972 } else {
5973 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5974 return 1;
5975 }
5976 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5977 int res;
5978 Py_ssize_t len;
5979 Py_ssize_t i;
5980 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5981 if (tmp == NULL) goto failed;
5982 if (!PyList_Check(tmp)) {
5983 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5984 goto failed;
5985 }
5986 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005987 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005988 if (values == NULL) goto failed;
5989 for (i = 0; i < len; i++) {
5990 expr_ty value;
5991 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5992 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005993 if (len != PyList_GET_SIZE(tmp)) {
5994 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5995 goto failed;
5996 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005997 asdl_seq_SET(values, i, value);
5998 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005999 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006000 } else {
6001 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
6002 return 1;
6003 }
6004 *out = Dict(keys, values, lineno, col_offset, arena);
6005 if (*out == NULL) goto failed;
6006 return 0;
6007 }
6008 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
6009 if (isinstance == -1) {
6010 return 1;
6011 }
6012 if (isinstance) {
6013 asdl_seq* elts;
6014
6015 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6016 int res;
6017 Py_ssize_t len;
6018 Py_ssize_t i;
6019 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6020 if (tmp == NULL) goto failed;
6021 if (!PyList_Check(tmp)) {
6022 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6023 goto failed;
6024 }
6025 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006026 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006027 if (elts == NULL) goto failed;
6028 for (i = 0; i < len; i++) {
6029 expr_ty value;
6030 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6031 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006032 if (len != PyList_GET_SIZE(tmp)) {
6033 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
6034 goto failed;
6035 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 asdl_seq_SET(elts, i, value);
6037 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006038 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006039 } else {
6040 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
6041 return 1;
6042 }
6043 *out = Set(elts, lineno, col_offset, arena);
6044 if (*out == NULL) goto failed;
6045 return 0;
6046 }
6047 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
6048 if (isinstance == -1) {
6049 return 1;
6050 }
6051 if (isinstance) {
6052 expr_ty elt;
6053 asdl_seq* generators;
6054
6055 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6056 int res;
6057 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6058 if (tmp == NULL) goto failed;
6059 res = obj2ast_expr(tmp, &elt, arena);
6060 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006061 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006062 } else {
6063 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
6064 return 1;
6065 }
6066 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6067 int res;
6068 Py_ssize_t len;
6069 Py_ssize_t i;
6070 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6071 if (tmp == NULL) goto failed;
6072 if (!PyList_Check(tmp)) {
6073 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6074 goto failed;
6075 }
6076 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006077 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006078 if (generators == NULL) goto failed;
6079 for (i = 0; i < len; i++) {
6080 comprehension_ty value;
6081 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6082 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006083 if (len != PyList_GET_SIZE(tmp)) {
6084 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6085 goto failed;
6086 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006087 asdl_seq_SET(generators, i, value);
6088 }
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 \"generators\" missing from ListComp");
6092 return 1;
6093 }
6094 *out = ListComp(elt, generators, lineno, col_offset, arena);
6095 if (*out == NULL) goto failed;
6096 return 0;
6097 }
6098 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6099 if (isinstance == -1) {
6100 return 1;
6101 }
6102 if (isinstance) {
6103 expr_ty elt;
6104 asdl_seq* generators;
6105
6106 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6107 int res;
6108 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6109 if (tmp == NULL) goto failed;
6110 res = obj2ast_expr(tmp, &elt, arena);
6111 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006112 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006113 } else {
6114 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6115 return 1;
6116 }
6117 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6118 int res;
6119 Py_ssize_t len;
6120 Py_ssize_t i;
6121 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6122 if (tmp == NULL) goto failed;
6123 if (!PyList_Check(tmp)) {
6124 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6125 goto failed;
6126 }
6127 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006128 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006129 if (generators == NULL) goto failed;
6130 for (i = 0; i < len; i++) {
6131 comprehension_ty value;
6132 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6133 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006134 if (len != PyList_GET_SIZE(tmp)) {
6135 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6136 goto failed;
6137 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006138 asdl_seq_SET(generators, i, value);
6139 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006140 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006141 } else {
6142 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6143 return 1;
6144 }
6145 *out = SetComp(elt, generators, lineno, col_offset, arena);
6146 if (*out == NULL) goto failed;
6147 return 0;
6148 }
6149 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6150 if (isinstance == -1) {
6151 return 1;
6152 }
6153 if (isinstance) {
6154 expr_ty key;
6155 expr_ty value;
6156 asdl_seq* generators;
6157
6158 if (_PyObject_HasAttrId(obj, &PyId_key)) {
6159 int res;
6160 tmp = _PyObject_GetAttrId(obj, &PyId_key);
6161 if (tmp == NULL) goto failed;
6162 res = obj2ast_expr(tmp, &key, arena);
6163 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006164 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006165 } else {
6166 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6167 return 1;
6168 }
6169 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6170 int res;
6171 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6172 if (tmp == NULL) goto failed;
6173 res = obj2ast_expr(tmp, &value, arena);
6174 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006175 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006176 } else {
6177 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6178 return 1;
6179 }
6180 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6181 int res;
6182 Py_ssize_t len;
6183 Py_ssize_t i;
6184 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6185 if (tmp == NULL) goto failed;
6186 if (!PyList_Check(tmp)) {
6187 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6188 goto failed;
6189 }
6190 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006191 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006192 if (generators == NULL) goto failed;
6193 for (i = 0; i < len; i++) {
6194 comprehension_ty value;
6195 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6196 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006197 if (len != PyList_GET_SIZE(tmp)) {
6198 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6199 goto failed;
6200 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006201 asdl_seq_SET(generators, i, value);
6202 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006203 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006204 } else {
6205 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6206 return 1;
6207 }
6208 *out = DictComp(key, value, generators, lineno, col_offset, arena);
6209 if (*out == NULL) goto failed;
6210 return 0;
6211 }
6212 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6213 if (isinstance == -1) {
6214 return 1;
6215 }
6216 if (isinstance) {
6217 expr_ty elt;
6218 asdl_seq* generators;
6219
6220 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6221 int res;
6222 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6223 if (tmp == NULL) goto failed;
6224 res = obj2ast_expr(tmp, &elt, arena);
6225 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006226 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006227 } else {
6228 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6229 return 1;
6230 }
6231 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6232 int res;
6233 Py_ssize_t len;
6234 Py_ssize_t i;
6235 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6236 if (tmp == NULL) goto failed;
6237 if (!PyList_Check(tmp)) {
6238 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6239 goto failed;
6240 }
6241 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006242 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006243 if (generators == NULL) goto failed;
6244 for (i = 0; i < len; i++) {
6245 comprehension_ty value;
6246 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
6247 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006248 if (len != PyList_GET_SIZE(tmp)) {
6249 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6250 goto failed;
6251 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006252 asdl_seq_SET(generators, i, value);
6253 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006254 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006255 } else {
6256 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6257 return 1;
6258 }
6259 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6260 if (*out == NULL) goto failed;
6261 return 0;
6262 }
Yury Selivanov75445082015-05-11 22:57:16 -04006263 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6264 if (isinstance == -1) {
6265 return 1;
6266 }
6267 if (isinstance) {
6268 expr_ty value;
6269
6270 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6271 int res;
6272 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6273 if (tmp == NULL) goto failed;
6274 res = obj2ast_expr(tmp, &value, arena);
6275 if (res != 0) goto failed;
6276 Py_CLEAR(tmp);
6277 } else {
6278 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6279 return 1;
6280 }
6281 *out = Await(value, lineno, col_offset, arena);
6282 if (*out == NULL) goto failed;
6283 return 0;
6284 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006285 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6286 if (isinstance == -1) {
6287 return 1;
6288 }
6289 if (isinstance) {
6290 expr_ty value;
6291
Victor Stinneree4b59c2013-07-27 00:01:35 +02006292 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006293 int res;
6294 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6295 if (tmp == NULL) goto failed;
6296 res = obj2ast_expr(tmp, &value, arena);
6297 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006298 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006299 } else {
6300 value = NULL;
6301 }
6302 *out = Yield(value, lineno, col_offset, arena);
6303 if (*out == NULL) goto failed;
6304 return 0;
6305 }
6306 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6307 if (isinstance == -1) {
6308 return 1;
6309 }
6310 if (isinstance) {
6311 expr_ty value;
6312
6313 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6314 int res;
6315 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6316 if (tmp == NULL) goto failed;
6317 res = obj2ast_expr(tmp, &value, arena);
6318 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006319 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006320 } else {
6321 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6322 return 1;
6323 }
6324 *out = YieldFrom(value, lineno, col_offset, arena);
6325 if (*out == NULL) goto failed;
6326 return 0;
6327 }
6328 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6329 if (isinstance == -1) {
6330 return 1;
6331 }
6332 if (isinstance) {
6333 expr_ty left;
6334 asdl_int_seq* ops;
6335 asdl_seq* comparators;
6336
6337 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6338 int res;
6339 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6340 if (tmp == NULL) goto failed;
6341 res = obj2ast_expr(tmp, &left, arena);
6342 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006344 } else {
6345 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6346 return 1;
6347 }
6348 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6349 int res;
6350 Py_ssize_t len;
6351 Py_ssize_t i;
6352 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6353 if (tmp == NULL) goto failed;
6354 if (!PyList_Check(tmp)) {
6355 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6356 goto failed;
6357 }
6358 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006359 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006360 if (ops == NULL) goto failed;
6361 for (i = 0; i < len; i++) {
6362 cmpop_ty value;
6363 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6364 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006365 if (len != PyList_GET_SIZE(tmp)) {
6366 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6367 goto failed;
6368 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006369 asdl_seq_SET(ops, i, value);
6370 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006371 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006372 } else {
6373 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6374 return 1;
6375 }
6376 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6377 int res;
6378 Py_ssize_t len;
6379 Py_ssize_t i;
6380 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6381 if (tmp == NULL) goto failed;
6382 if (!PyList_Check(tmp)) {
6383 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6384 goto failed;
6385 }
6386 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006387 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006388 if (comparators == NULL) goto failed;
6389 for (i = 0; i < len; i++) {
6390 expr_ty value;
6391 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6392 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006393 if (len != PyList_GET_SIZE(tmp)) {
6394 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6395 goto failed;
6396 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006397 asdl_seq_SET(comparators, i, value);
6398 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006399 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006400 } else {
6401 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6402 return 1;
6403 }
6404 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6405 if (*out == NULL) goto failed;
6406 return 0;
6407 }
6408 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6409 if (isinstance == -1) {
6410 return 1;
6411 }
6412 if (isinstance) {
6413 expr_ty func;
6414 asdl_seq* args;
6415 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006416
6417 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6418 int res;
6419 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6420 if (tmp == NULL) goto failed;
6421 res = obj2ast_expr(tmp, &func, arena);
6422 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006423 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006424 } else {
6425 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6426 return 1;
6427 }
6428 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6429 int res;
6430 Py_ssize_t len;
6431 Py_ssize_t i;
6432 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6433 if (tmp == NULL) goto failed;
6434 if (!PyList_Check(tmp)) {
6435 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6436 goto failed;
6437 }
6438 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006439 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006440 if (args == NULL) goto failed;
6441 for (i = 0; i < len; i++) {
6442 expr_ty value;
6443 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6444 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006445 if (len != PyList_GET_SIZE(tmp)) {
6446 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6447 goto failed;
6448 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006449 asdl_seq_SET(args, i, value);
6450 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006451 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006452 } else {
6453 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6454 return 1;
6455 }
6456 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6457 int res;
6458 Py_ssize_t len;
6459 Py_ssize_t i;
6460 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6461 if (tmp == NULL) goto failed;
6462 if (!PyList_Check(tmp)) {
6463 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6464 goto failed;
6465 }
6466 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006467 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006468 if (keywords == NULL) goto failed;
6469 for (i = 0; i < len; i++) {
6470 keyword_ty value;
6471 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6472 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006473 if (len != PyList_GET_SIZE(tmp)) {
6474 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6475 goto failed;
6476 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006477 asdl_seq_SET(keywords, i, value);
6478 }
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 \"keywords\" missing from Call");
6482 return 1;
6483 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006484 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006485 if (*out == NULL) goto failed;
6486 return 0;
6487 }
6488 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6489 if (isinstance == -1) {
6490 return 1;
6491 }
6492 if (isinstance) {
6493 object n;
6494
6495 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6496 int res;
6497 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6498 if (tmp == NULL) goto failed;
6499 res = obj2ast_object(tmp, &n, arena);
6500 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006501 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006502 } else {
6503 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6504 return 1;
6505 }
6506 *out = Num(n, lineno, col_offset, arena);
6507 if (*out == NULL) goto failed;
6508 return 0;
6509 }
6510 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6511 if (isinstance == -1) {
6512 return 1;
6513 }
6514 if (isinstance) {
6515 string s;
6516
6517 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6518 int res;
6519 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6520 if (tmp == NULL) goto failed;
6521 res = obj2ast_string(tmp, &s, arena);
6522 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006523 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006524 } else {
6525 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6526 return 1;
6527 }
6528 *out = Str(s, lineno, col_offset, arena);
6529 if (*out == NULL) goto failed;
6530 return 0;
6531 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006532 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6533 if (isinstance == -1) {
6534 return 1;
6535 }
6536 if (isinstance) {
6537 expr_ty value;
6538 int conversion;
6539 expr_ty format_spec;
6540
6541 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6542 int res;
6543 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6544 if (tmp == NULL) goto failed;
6545 res = obj2ast_expr(tmp, &value, arena);
6546 if (res != 0) goto failed;
6547 Py_CLEAR(tmp);
6548 } else {
6549 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6550 return 1;
6551 }
6552 if (exists_not_none(obj, &PyId_conversion)) {
6553 int res;
6554 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6555 if (tmp == NULL) goto failed;
6556 res = obj2ast_int(tmp, &conversion, arena);
6557 if (res != 0) goto failed;
6558 Py_CLEAR(tmp);
6559 } else {
6560 conversion = 0;
6561 }
6562 if (exists_not_none(obj, &PyId_format_spec)) {
6563 int res;
6564 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6565 if (tmp == NULL) goto failed;
6566 res = obj2ast_expr(tmp, &format_spec, arena);
6567 if (res != 0) goto failed;
6568 Py_CLEAR(tmp);
6569 } else {
6570 format_spec = NULL;
6571 }
6572 *out = FormattedValue(value, conversion, format_spec, lineno,
6573 col_offset, arena);
6574 if (*out == NULL) goto failed;
6575 return 0;
6576 }
6577 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6578 if (isinstance == -1) {
6579 return 1;
6580 }
6581 if (isinstance) {
6582 asdl_seq* values;
6583
6584 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6585 int res;
6586 Py_ssize_t len;
6587 Py_ssize_t i;
6588 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6589 if (tmp == NULL) goto failed;
6590 if (!PyList_Check(tmp)) {
6591 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6592 goto failed;
6593 }
6594 len = PyList_GET_SIZE(tmp);
6595 values = _Py_asdl_seq_new(len, arena);
6596 if (values == NULL) goto failed;
6597 for (i = 0; i < len; i++) {
6598 expr_ty value;
6599 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6600 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03006601 if (len != PyList_GET_SIZE(tmp)) {
6602 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
6603 goto failed;
6604 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006605 asdl_seq_SET(values, i, value);
6606 }
6607 Py_CLEAR(tmp);
6608 } else {
6609 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6610 return 1;
6611 }
6612 *out = JoinedStr(values, lineno, col_offset, arena);
6613 if (*out == NULL) goto failed;
6614 return 0;
6615 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006616 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6617 if (isinstance == -1) {
6618 return 1;
6619 }
6620 if (isinstance) {
6621 bytes s;
6622
6623 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6624 int res;
6625 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6626 if (tmp == NULL) goto failed;
6627 res = obj2ast_bytes(tmp, &s, arena);
6628 if (res != 0) goto failed;
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 \"s\" missing from Bytes");
6632 return 1;
6633 }
6634 *out = Bytes(s, lineno, col_offset, arena);
6635 if (*out == NULL) goto failed;
6636 return 0;
6637 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006638 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6639 if (isinstance == -1) {
6640 return 1;
6641 }
6642 if (isinstance) {
6643 singleton value;
6644
6645 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6646 int res;
6647 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6648 if (tmp == NULL) goto failed;
6649 res = obj2ast_singleton(tmp, &value, arena);
6650 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006651 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006652 } else {
6653 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6654 return 1;
6655 }
6656 *out = NameConstant(value, lineno, col_offset, arena);
6657 if (*out == NULL) goto failed;
6658 return 0;
6659 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006660 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6661 if (isinstance == -1) {
6662 return 1;
6663 }
6664 if (isinstance) {
6665
6666 *out = Ellipsis(lineno, col_offset, arena);
6667 if (*out == NULL) goto failed;
6668 return 0;
6669 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006670 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6671 if (isinstance == -1) {
6672 return 1;
6673 }
6674 if (isinstance) {
6675 constant value;
6676
6677 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6678 int res;
6679 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6680 if (tmp == NULL) goto failed;
6681 res = obj2ast_constant(tmp, &value, arena);
6682 if (res != 0) goto failed;
6683 Py_CLEAR(tmp);
6684 } else {
6685 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6686 return 1;
6687 }
6688 *out = Constant(value, lineno, col_offset, arena);
6689 if (*out == NULL) goto failed;
6690 return 0;
6691 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006692 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6693 if (isinstance == -1) {
6694 return 1;
6695 }
6696 if (isinstance) {
6697 expr_ty value;
6698 identifier attr;
6699 expr_context_ty ctx;
6700
6701 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6702 int res;
6703 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6704 if (tmp == NULL) goto failed;
6705 res = obj2ast_expr(tmp, &value, arena);
6706 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006707 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006708 } else {
6709 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6710 return 1;
6711 }
6712 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6713 int res;
6714 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6715 if (tmp == NULL) goto failed;
6716 res = obj2ast_identifier(tmp, &attr, arena);
6717 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006719 } else {
6720 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6721 return 1;
6722 }
6723 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6724 int res;
6725 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6726 if (tmp == NULL) goto failed;
6727 res = obj2ast_expr_context(tmp, &ctx, arena);
6728 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006729 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006730 } else {
6731 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6732 return 1;
6733 }
6734 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6735 if (*out == NULL) goto failed;
6736 return 0;
6737 }
6738 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6739 if (isinstance == -1) {
6740 return 1;
6741 }
6742 if (isinstance) {
6743 expr_ty value;
6744 slice_ty slice;
6745 expr_context_ty ctx;
6746
6747 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6748 int res;
6749 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6750 if (tmp == NULL) goto failed;
6751 res = obj2ast_expr(tmp, &value, arena);
6752 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006753 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006754 } else {
6755 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6756 return 1;
6757 }
6758 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6759 int res;
6760 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6761 if (tmp == NULL) goto failed;
6762 res = obj2ast_slice(tmp, &slice, arena);
6763 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006764 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006765 } else {
6766 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6767 return 1;
6768 }
6769 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6770 int res;
6771 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6772 if (tmp == NULL) goto failed;
6773 res = obj2ast_expr_context(tmp, &ctx, arena);
6774 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006775 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006776 } else {
6777 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6778 return 1;
6779 }
6780 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6781 if (*out == NULL) goto failed;
6782 return 0;
6783 }
6784 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6785 if (isinstance == -1) {
6786 return 1;
6787 }
6788 if (isinstance) {
6789 expr_ty value;
6790 expr_context_ty ctx;
6791
6792 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6793 int res;
6794 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6795 if (tmp == NULL) goto failed;
6796 res = obj2ast_expr(tmp, &value, arena);
6797 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006798 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006799 } else {
6800 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6801 return 1;
6802 }
6803 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6804 int res;
6805 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6806 if (tmp == NULL) goto failed;
6807 res = obj2ast_expr_context(tmp, &ctx, arena);
6808 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006809 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006810 } else {
6811 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6812 return 1;
6813 }
6814 *out = Starred(value, ctx, lineno, col_offset, arena);
6815 if (*out == NULL) goto failed;
6816 return 0;
6817 }
6818 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6819 if (isinstance == -1) {
6820 return 1;
6821 }
6822 if (isinstance) {
6823 identifier id;
6824 expr_context_ty ctx;
6825
6826 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6827 int res;
6828 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6829 if (tmp == NULL) goto failed;
6830 res = obj2ast_identifier(tmp, &id, arena);
6831 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006832 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 } else {
6834 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6835 return 1;
6836 }
6837 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6838 int res;
6839 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6840 if (tmp == NULL) goto failed;
6841 res = obj2ast_expr_context(tmp, &ctx, arena);
6842 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006843 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006844 } else {
6845 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6846 return 1;
6847 }
6848 *out = Name(id, ctx, lineno, col_offset, arena);
6849 if (*out == NULL) goto failed;
6850 return 0;
6851 }
6852 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6853 if (isinstance == -1) {
6854 return 1;
6855 }
6856 if (isinstance) {
6857 asdl_seq* elts;
6858 expr_context_ty ctx;
6859
6860 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6861 int res;
6862 Py_ssize_t len;
6863 Py_ssize_t i;
6864 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6865 if (tmp == NULL) goto failed;
6866 if (!PyList_Check(tmp)) {
6867 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6868 goto failed;
6869 }
6870 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006871 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006872 if (elts == NULL) goto failed;
6873 for (i = 0; i < len; i++) {
6874 expr_ty value;
6875 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6876 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006877 if (len != PyList_GET_SIZE(tmp)) {
6878 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6879 goto failed;
6880 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006881 asdl_seq_SET(elts, i, value);
6882 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006883 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006884 } else {
6885 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6886 return 1;
6887 }
6888 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6889 int res;
6890 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6891 if (tmp == NULL) goto failed;
6892 res = obj2ast_expr_context(tmp, &ctx, arena);
6893 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006894 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006895 } else {
6896 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6897 return 1;
6898 }
6899 *out = List(elts, ctx, lineno, col_offset, arena);
6900 if (*out == NULL) goto failed;
6901 return 0;
6902 }
6903 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6904 if (isinstance == -1) {
6905 return 1;
6906 }
6907 if (isinstance) {
6908 asdl_seq* elts;
6909 expr_context_ty ctx;
6910
6911 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6912 int res;
6913 Py_ssize_t len;
6914 Py_ssize_t i;
6915 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6916 if (tmp == NULL) goto failed;
6917 if (!PyList_Check(tmp)) {
6918 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6919 goto failed;
6920 }
6921 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006922 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006923 if (elts == NULL) goto failed;
6924 for (i = 0; i < len; i++) {
6925 expr_ty value;
6926 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6927 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006928 if (len != PyList_GET_SIZE(tmp)) {
6929 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6930 goto failed;
6931 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006932 asdl_seq_SET(elts, i, value);
6933 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006934 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006935 } else {
6936 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6937 return 1;
6938 }
6939 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6940 int res;
6941 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6942 if (tmp == NULL) goto failed;
6943 res = obj2ast_expr_context(tmp, &ctx, arena);
6944 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006945 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006946 } else {
6947 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6948 return 1;
6949 }
6950 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6951 if (*out == NULL) goto failed;
6952 return 0;
6953 }
6954
6955 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6956 failed:
6957 Py_XDECREF(tmp);
6958 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006959}
6960
6961int
6962obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6963{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006964 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006965
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006966 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6967 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006968 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006969 }
6970 if (isinstance) {
6971 *out = Load;
6972 return 0;
6973 }
6974 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6975 if (isinstance == -1) {
6976 return 1;
6977 }
6978 if (isinstance) {
6979 *out = Store;
6980 return 0;
6981 }
6982 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6983 if (isinstance == -1) {
6984 return 1;
6985 }
6986 if (isinstance) {
6987 *out = Del;
6988 return 0;
6989 }
6990 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6991 if (isinstance == -1) {
6992 return 1;
6993 }
6994 if (isinstance) {
6995 *out = AugLoad;
6996 return 0;
6997 }
6998 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6999 if (isinstance == -1) {
7000 return 1;
7001 }
7002 if (isinstance) {
7003 *out = AugStore;
7004 return 0;
7005 }
7006 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
7007 if (isinstance == -1) {
7008 return 1;
7009 }
7010 if (isinstance) {
7011 *out = Param;
7012 return 0;
7013 }
7014
7015 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
7016 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007017}
7018
7019int
7020obj2ast_slice(PyObject* obj, slice_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 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007025
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007026 if (obj == Py_None) {
7027 *out = NULL;
7028 return 0;
7029 }
7030 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
7031 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007032 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007033 }
7034 if (isinstance) {
7035 expr_ty lower;
7036 expr_ty upper;
7037 expr_ty step;
7038
Victor Stinneree4b59c2013-07-27 00:01:35 +02007039 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007040 int res;
7041 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
7042 if (tmp == NULL) goto failed;
7043 res = obj2ast_expr(tmp, &lower, arena);
7044 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007045 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007046 } else {
7047 lower = NULL;
7048 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007049 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007050 int res;
7051 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
7052 if (tmp == NULL) goto failed;
7053 res = obj2ast_expr(tmp, &upper, arena);
7054 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007055 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007056 } else {
7057 upper = NULL;
7058 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007059 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007060 int res;
7061 tmp = _PyObject_GetAttrId(obj, &PyId_step);
7062 if (tmp == NULL) goto failed;
7063 res = obj2ast_expr(tmp, &step, arena);
7064 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007065 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007066 } else {
7067 step = NULL;
7068 }
7069 *out = Slice(lower, upper, step, arena);
7070 if (*out == NULL) goto failed;
7071 return 0;
7072 }
7073 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7074 if (isinstance == -1) {
7075 return 1;
7076 }
7077 if (isinstance) {
7078 asdl_seq* dims;
7079
7080 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
7081 int res;
7082 Py_ssize_t len;
7083 Py_ssize_t i;
7084 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
7085 if (tmp == NULL) goto failed;
7086 if (!PyList_Check(tmp)) {
7087 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7088 goto failed;
7089 }
7090 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007091 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007092 if (dims == NULL) goto failed;
7093 for (i = 0; i < len; i++) {
7094 slice_ty value;
7095 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
7096 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007097 if (len != PyList_GET_SIZE(tmp)) {
7098 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7099 goto failed;
7100 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007101 asdl_seq_SET(dims, i, value);
7102 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007103 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007104 } else {
7105 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7106 return 1;
7107 }
7108 *out = ExtSlice(dims, arena);
7109 if (*out == NULL) goto failed;
7110 return 0;
7111 }
7112 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7113 if (isinstance == -1) {
7114 return 1;
7115 }
7116 if (isinstance) {
7117 expr_ty value;
7118
7119 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7120 int res;
7121 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7122 if (tmp == NULL) goto failed;
7123 res = obj2ast_expr(tmp, &value, arena);
7124 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007125 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007126 } else {
7127 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7128 return 1;
7129 }
7130 *out = Index(value, arena);
7131 if (*out == NULL) goto failed;
7132 return 0;
7133 }
7134
7135 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7136 failed:
7137 Py_XDECREF(tmp);
7138 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007139}
7140
7141int
7142obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7143{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007144 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007145
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007146 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7147 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007148 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007149 }
7150 if (isinstance) {
7151 *out = And;
7152 return 0;
7153 }
7154 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7155 if (isinstance == -1) {
7156 return 1;
7157 }
7158 if (isinstance) {
7159 *out = Or;
7160 return 0;
7161 }
7162
7163 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7164 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007165}
7166
7167int
7168obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7169{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007170 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007171
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007172 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7173 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007174 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007175 }
7176 if (isinstance) {
7177 *out = Add;
7178 return 0;
7179 }
7180 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7181 if (isinstance == -1) {
7182 return 1;
7183 }
7184 if (isinstance) {
7185 *out = Sub;
7186 return 0;
7187 }
7188 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7189 if (isinstance == -1) {
7190 return 1;
7191 }
7192 if (isinstance) {
7193 *out = Mult;
7194 return 0;
7195 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007196 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7197 if (isinstance == -1) {
7198 return 1;
7199 }
7200 if (isinstance) {
7201 *out = MatMult;
7202 return 0;
7203 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007204 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7205 if (isinstance == -1) {
7206 return 1;
7207 }
7208 if (isinstance) {
7209 *out = Div;
7210 return 0;
7211 }
7212 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7213 if (isinstance == -1) {
7214 return 1;
7215 }
7216 if (isinstance) {
7217 *out = Mod;
7218 return 0;
7219 }
7220 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7221 if (isinstance == -1) {
7222 return 1;
7223 }
7224 if (isinstance) {
7225 *out = Pow;
7226 return 0;
7227 }
7228 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7229 if (isinstance == -1) {
7230 return 1;
7231 }
7232 if (isinstance) {
7233 *out = LShift;
7234 return 0;
7235 }
7236 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7237 if (isinstance == -1) {
7238 return 1;
7239 }
7240 if (isinstance) {
7241 *out = RShift;
7242 return 0;
7243 }
7244 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7245 if (isinstance == -1) {
7246 return 1;
7247 }
7248 if (isinstance) {
7249 *out = BitOr;
7250 return 0;
7251 }
7252 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7253 if (isinstance == -1) {
7254 return 1;
7255 }
7256 if (isinstance) {
7257 *out = BitXor;
7258 return 0;
7259 }
7260 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7261 if (isinstance == -1) {
7262 return 1;
7263 }
7264 if (isinstance) {
7265 *out = BitAnd;
7266 return 0;
7267 }
7268 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7269 if (isinstance == -1) {
7270 return 1;
7271 }
7272 if (isinstance) {
7273 *out = FloorDiv;
7274 return 0;
7275 }
7276
7277 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7278 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007279}
7280
7281int
7282obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7283{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007284 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007285
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007286 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7287 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007288 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007289 }
7290 if (isinstance) {
7291 *out = Invert;
7292 return 0;
7293 }
7294 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7295 if (isinstance == -1) {
7296 return 1;
7297 }
7298 if (isinstance) {
7299 *out = Not;
7300 return 0;
7301 }
7302 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7303 if (isinstance == -1) {
7304 return 1;
7305 }
7306 if (isinstance) {
7307 *out = UAdd;
7308 return 0;
7309 }
7310 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7311 if (isinstance == -1) {
7312 return 1;
7313 }
7314 if (isinstance) {
7315 *out = USub;
7316 return 0;
7317 }
7318
7319 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7320 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007321}
7322
7323int
7324obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7325{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007327
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007328 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7329 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007330 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007331 }
7332 if (isinstance) {
7333 *out = Eq;
7334 return 0;
7335 }
7336 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7337 if (isinstance == -1) {
7338 return 1;
7339 }
7340 if (isinstance) {
7341 *out = NotEq;
7342 return 0;
7343 }
7344 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7345 if (isinstance == -1) {
7346 return 1;
7347 }
7348 if (isinstance) {
7349 *out = Lt;
7350 return 0;
7351 }
7352 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7353 if (isinstance == -1) {
7354 return 1;
7355 }
7356 if (isinstance) {
7357 *out = LtE;
7358 return 0;
7359 }
7360 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7361 if (isinstance == -1) {
7362 return 1;
7363 }
7364 if (isinstance) {
7365 *out = Gt;
7366 return 0;
7367 }
7368 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7369 if (isinstance == -1) {
7370 return 1;
7371 }
7372 if (isinstance) {
7373 *out = GtE;
7374 return 0;
7375 }
7376 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7377 if (isinstance == -1) {
7378 return 1;
7379 }
7380 if (isinstance) {
7381 *out = Is;
7382 return 0;
7383 }
7384 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7385 if (isinstance == -1) {
7386 return 1;
7387 }
7388 if (isinstance) {
7389 *out = IsNot;
7390 return 0;
7391 }
7392 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7393 if (isinstance == -1) {
7394 return 1;
7395 }
7396 if (isinstance) {
7397 *out = In;
7398 return 0;
7399 }
7400 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7401 if (isinstance == -1) {
7402 return 1;
7403 }
7404 if (isinstance) {
7405 *out = NotIn;
7406 return 0;
7407 }
7408
7409 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7410 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007411}
7412
7413int
7414obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7415{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007416 PyObject* tmp = NULL;
7417 expr_ty target;
7418 expr_ty iter;
7419 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007420 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007421
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007422 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7423 int res;
7424 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7425 if (tmp == NULL) goto failed;
7426 res = obj2ast_expr(tmp, &target, arena);
7427 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007428 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007429 } else {
7430 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007431 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007432 }
7433 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7434 int res;
7435 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7436 if (tmp == NULL) goto failed;
7437 res = obj2ast_expr(tmp, &iter, arena);
7438 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007439 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007440 } else {
7441 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7442 return 1;
7443 }
7444 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7445 int res;
7446 Py_ssize_t len;
7447 Py_ssize_t i;
7448 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7449 if (tmp == NULL) goto failed;
7450 if (!PyList_Check(tmp)) {
7451 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7452 goto failed;
7453 }
7454 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007455 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007456 if (ifs == NULL) goto failed;
7457 for (i = 0; i < len; i++) {
7458 expr_ty value;
7459 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7460 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007461 if (len != PyList_GET_SIZE(tmp)) {
7462 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7463 goto failed;
7464 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007465 asdl_seq_SET(ifs, i, value);
7466 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007467 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007468 } else {
7469 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7470 return 1;
7471 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007472 if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7473 int res;
7474 tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7475 if (tmp == NULL) goto failed;
7476 res = obj2ast_int(tmp, &is_async, arena);
7477 if (res != 0) goto failed;
7478 Py_CLEAR(tmp);
7479 } else {
7480 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7481 return 1;
7482 }
7483 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007484 return 0;
7485failed:
7486 Py_XDECREF(tmp);
7487 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007488}
7489
7490int
7491obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7492{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007493 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007494
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007495 PyObject *tmp = NULL;
7496 int lineno;
7497 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007498
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007499 if (obj == Py_None) {
7500 *out = NULL;
7501 return 0;
7502 }
7503 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7504 int res;
7505 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7506 if (tmp == NULL) goto failed;
7507 res = obj2ast_int(tmp, &lineno, arena);
7508 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007509 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007510 } else {
7511 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007512 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007513 }
7514 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7515 int res;
7516 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7517 if (tmp == NULL) goto failed;
7518 res = obj2ast_int(tmp, &col_offset, arena);
7519 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007520 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007521 } else {
7522 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7523 return 1;
7524 }
7525 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7526 if (isinstance == -1) {
7527 return 1;
7528 }
7529 if (isinstance) {
7530 expr_ty type;
7531 identifier name;
7532 asdl_seq* body;
7533
Victor Stinneree4b59c2013-07-27 00:01:35 +02007534 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007535 int res;
7536 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7537 if (tmp == NULL) goto failed;
7538 res = obj2ast_expr(tmp, &type, 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 type = NULL;
7543 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007544 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007545 int res;
7546 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7547 if (tmp == NULL) goto failed;
7548 res = obj2ast_identifier(tmp, &name, arena);
7549 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007550 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007551 } else {
7552 name = NULL;
7553 }
7554 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7555 int res;
7556 Py_ssize_t len;
7557 Py_ssize_t i;
7558 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7559 if (tmp == NULL) goto failed;
7560 if (!PyList_Check(tmp)) {
7561 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7562 goto failed;
7563 }
7564 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007565 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007566 if (body == NULL) goto failed;
7567 for (i = 0; i < len; i++) {
7568 stmt_ty value;
7569 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7570 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007571 if (len != PyList_GET_SIZE(tmp)) {
7572 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7573 goto failed;
7574 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007575 asdl_seq_SET(body, i, value);
7576 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007577 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007578 } else {
7579 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7580 return 1;
7581 }
7582 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7583 if (*out == NULL) goto failed;
7584 return 0;
7585 }
7586
7587 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7588 failed:
7589 Py_XDECREF(tmp);
7590 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007591}
7592
7593int
7594obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7595{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007596 PyObject* tmp = NULL;
7597 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007598 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007599 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007600 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007601 arg_ty kwarg;
7602 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007603
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007604 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7605 int res;
7606 Py_ssize_t len;
7607 Py_ssize_t i;
7608 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7609 if (tmp == NULL) goto failed;
7610 if (!PyList_Check(tmp)) {
7611 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7612 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007613 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007614 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007615 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007616 if (args == NULL) goto failed;
7617 for (i = 0; i < len; i++) {
7618 arg_ty value;
7619 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7620 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007621 if (len != PyList_GET_SIZE(tmp)) {
7622 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7623 goto failed;
7624 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007625 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007626 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007627 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007628 } else {
7629 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007630 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007631 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007632 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007633 int res;
7634 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7635 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007636 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007637 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007638 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007639 } else {
7640 vararg = NULL;
7641 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007642 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7643 int res;
7644 Py_ssize_t len;
7645 Py_ssize_t i;
7646 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7647 if (tmp == NULL) goto failed;
7648 if (!PyList_Check(tmp)) {
7649 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7650 goto failed;
7651 }
7652 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007653 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007654 if (kwonlyargs == NULL) goto failed;
7655 for (i = 0; i < len; i++) {
7656 arg_ty value;
7657 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7658 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007659 if (len != PyList_GET_SIZE(tmp)) {
7660 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7661 goto failed;
7662 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007663 asdl_seq_SET(kwonlyargs, i, value);
7664 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007665 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007666 } else {
7667 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7668 return 1;
7669 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007670 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7671 int res;
7672 Py_ssize_t len;
7673 Py_ssize_t i;
7674 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7675 if (tmp == NULL) goto failed;
7676 if (!PyList_Check(tmp)) {
7677 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7678 goto failed;
7679 }
7680 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007681 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007682 if (kw_defaults == NULL) goto failed;
7683 for (i = 0; i < len; i++) {
7684 expr_ty value;
7685 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7686 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007687 if (len != PyList_GET_SIZE(tmp)) {
7688 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7689 goto failed;
7690 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007691 asdl_seq_SET(kw_defaults, i, value);
7692 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007693 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007694 } else {
7695 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7696 return 1;
7697 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007698 if (exists_not_none(obj, &PyId_kwarg)) {
7699 int res;
7700 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7701 if (tmp == NULL) goto failed;
7702 res = obj2ast_arg(tmp, &kwarg, arena);
7703 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007704 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007705 } else {
7706 kwarg = NULL;
7707 }
7708 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7709 int res;
7710 Py_ssize_t len;
7711 Py_ssize_t i;
7712 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7713 if (tmp == NULL) goto failed;
7714 if (!PyList_Check(tmp)) {
7715 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7716 goto failed;
7717 }
7718 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007719 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007720 if (defaults == NULL) goto failed;
7721 for (i = 0; i < len; i++) {
7722 expr_ty value;
7723 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7724 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007725 if (len != PyList_GET_SIZE(tmp)) {
7726 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7727 goto failed;
7728 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007729 asdl_seq_SET(defaults, i, value);
7730 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007731 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007732 } else {
7733 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7734 return 1;
7735 }
7736 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7737 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007738 return 0;
7739failed:
7740 Py_XDECREF(tmp);
7741 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007742}
7743
7744int
7745obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7746{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007747 PyObject* tmp = NULL;
7748 identifier arg;
7749 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007750 int lineno;
7751 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007752
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007753 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7754 int res;
7755 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7756 if (tmp == NULL) goto failed;
7757 res = obj2ast_identifier(tmp, &arg, arena);
7758 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007759 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007760 } else {
7761 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007762 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007763 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007764 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007765 int res;
7766 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7767 if (tmp == NULL) goto failed;
7768 res = obj2ast_expr(tmp, &annotation, arena);
7769 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007770 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007771 } else {
7772 annotation = NULL;
7773 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007774 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7775 int res;
7776 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7777 if (tmp == NULL) goto failed;
7778 res = obj2ast_int(tmp, &lineno, arena);
7779 if (res != 0) goto failed;
7780 Py_CLEAR(tmp);
7781 } else {
7782 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7783 return 1;
7784 }
7785 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7786 int res;
7787 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7788 if (tmp == NULL) goto failed;
7789 res = obj2ast_int(tmp, &col_offset, arena);
7790 if (res != 0) goto failed;
7791 Py_CLEAR(tmp);
7792 } else {
7793 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7794 return 1;
7795 }
7796 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007797 return 0;
7798failed:
7799 Py_XDECREF(tmp);
7800 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007801}
7802
7803int
7804obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007806 PyObject* tmp = NULL;
7807 identifier arg;
7808 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007809
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007810 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007811 int res;
7812 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7813 if (tmp == NULL) goto failed;
7814 res = obj2ast_identifier(tmp, &arg, arena);
7815 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007816 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007817 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007818 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007819 }
7820 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7821 int res;
7822 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7823 if (tmp == NULL) goto failed;
7824 res = obj2ast_expr(tmp, &value, arena);
7825 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007827 } else {
7828 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7829 return 1;
7830 }
7831 *out = keyword(arg, value, arena);
7832 return 0;
7833failed:
7834 Py_XDECREF(tmp);
7835 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007836}
7837
7838int
7839obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7840{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007841 PyObject* tmp = NULL;
7842 identifier name;
7843 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007844
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007845 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7846 int res;
7847 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7848 if (tmp == NULL) goto failed;
7849 res = obj2ast_identifier(tmp, &name, arena);
7850 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007851 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007852 } else {
7853 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007854 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007855 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007856 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007857 int res;
7858 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7859 if (tmp == NULL) goto failed;
7860 res = obj2ast_identifier(tmp, &asname, arena);
7861 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007862 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007863 } else {
7864 asname = NULL;
7865 }
7866 *out = alias(name, asname, arena);
7867 return 0;
7868failed:
7869 Py_XDECREF(tmp);
7870 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007871}
7872
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007873int
7874obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7875{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007876 PyObject* tmp = NULL;
7877 expr_ty context_expr;
7878 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007879
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007880 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7881 int res;
7882 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7883 if (tmp == NULL) goto failed;
7884 res = obj2ast_expr(tmp, &context_expr, arena);
7885 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007886 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007887 } else {
7888 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007889 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007890 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007891 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007892 int res;
7893 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7894 if (tmp == NULL) goto failed;
7895 res = obj2ast_expr(tmp, &optional_vars, arena);
7896 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007897 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007898 } else {
7899 optional_vars = NULL;
7900 }
7901 *out = withitem(context_expr, optional_vars, arena);
7902 return 0;
7903failed:
7904 Py_XDECREF(tmp);
7905 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007906}
7907
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007908
Martin v. Löwis1a214512008-06-11 05:26:20 +00007909static struct PyModuleDef _astmodule = {
7910 PyModuleDef_HEAD_INIT, "_ast"
7911};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007912PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007913PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007914{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007915 PyObject *m, *d;
7916 if (!init_types()) return NULL;
7917 m = PyModule_Create(&_astmodule);
7918 if (!m) return NULL;
7919 d = PyModule_GetDict(m);
7920 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007921 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007922 return NULL;
7923 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7924 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7925 NULL;
7926 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7927 0) return NULL;
7928 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7929 return NULL;
7930 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7931 NULL;
7932 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7933 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7934 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007935 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7936 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007937 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7938 return NULL;
7939 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7940 NULL;
7941 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7942 NULL;
7943 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7944 NULL;
7945 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7946 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007947 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7948 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007949 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007950 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7951 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007952 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7953 NULL;
7954 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7955 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007956 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7957 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007958 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7959 NULL;
7960 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7961 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7962 NULL;
7963 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7964 NULL;
7965 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7966 return NULL;
7967 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7968 NULL;
7969 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7970 return NULL;
7971 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7972 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7973 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7974 NULL;
7975 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7976 return NULL;
7977 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7978 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7979 NULL;
7980 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7981 NULL;
7982 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7983 NULL;
7984 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7985 NULL;
7986 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7987 NULL;
7988 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7989 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7990 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7991 return NULL;
7992 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7993 NULL;
7994 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7995 return NULL;
7996 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7997 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007998 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7999 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008000 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
8001 NULL;
8002 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
8003 return NULL;
8004 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
8005 NULL;
8006 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
8007 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
8008 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04008009 if (PyDict_SetItemString(d, "FormattedValue",
8010 (PyObject*)FormattedValue_type) < 0) return NULL;
8011 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
8012 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008013 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
8014 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008015 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
8016 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008017 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
8018 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008019 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
8020 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008021 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
8022 return NULL;
8023 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
8024 return NULL;
8025 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
8026 NULL;
8027 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
8028 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
8029 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
8030 NULL;
8031 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
8032 0) return NULL;
8033 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
8034 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
8035 NULL;
8036 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
8037 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
8038 NULL;
8039 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
8040 return NULL;
8041 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
8042 NULL;
8043 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
8044 NULL;
8045 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
8046 NULL;
8047 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
8048 return NULL;
8049 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
8050 NULL;
8051 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
8052 NULL;
8053 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
8054 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
8055 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
8056 return NULL;
8057 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
8058 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
8059 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04008060 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
8061 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008062 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
8063 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
8064 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
8065 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
8066 NULL;
8067 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
8068 NULL;
8069 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
8070 NULL;
8071 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8072 NULL;
8073 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8074 NULL;
8075 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8076 return NULL;
8077 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8078 NULL;
8079 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8080 NULL;
8081 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8082 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8083 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8084 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8085 NULL;
8086 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8087 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8088 NULL;
8089 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8090 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8091 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8092 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8093 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8094 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8095 NULL;
8096 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8097 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8098 NULL;
8099 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8100 < 0) return NULL;
8101 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8102 < 0) return NULL;
8103 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8104 < 0) return NULL;
8105 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8106 return NULL;
8107 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8108 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8109 NULL;
8110 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8111 NULL;
8112 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8113 return NULL;
8114 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008115}
8116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008117
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008118PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008119{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008120 if (!init_types())
8121 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008122 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008123}
Martin v. Löwis5b222132007-06-10 09:51:05 +00008124
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008125/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
8126mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008127{
8128 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008129 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008130 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008131 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008132
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008133 req_type[0] = (PyObject*)Module_type;
8134 req_type[1] = (PyObject*)Expression_type;
8135 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008136
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008137 assert(0 <= mode && mode <= 2);
8138
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008139 if (!init_types())
8140 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008141
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008142 isinstance = PyObject_IsInstance(ast, req_type[mode]);
8143 if (isinstance == -1)
8144 return NULL;
8145 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008146 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8147 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008148 return NULL;
8149 }
8150 if (obj2ast_mod(ast, &res, arena) != 0)
8151 return NULL;
8152 else
8153 return res;
8154}
8155
8156int PyAST_Check(PyObject* obj)
8157{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008158 if (!init_types())
8159 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00008160 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008161}
8162
Martin v. Löwis5b222132007-06-10 09:51:05 +00008163