blob: 2f1e737ea656f6eb35c097345552bd07ef686383 [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{
INADA Naokia6296d32017-08-24 14:55:17 +0900523 /* bpo-31095: UnTrack is needed before calling any callbacks */
524 PyObject_GC_UnTrack(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500525 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200526 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500527}
528
Neal Norwitz207c9f32008-03-31 04:42:11 +0000529static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700530ast_traverse(AST_object *self, visitproc visit, void *arg)
531{
532 Py_VISIT(self->dict);
533 return 0;
534}
535
536static void
537ast_clear(AST_object *self)
538{
539 Py_CLEAR(self->dict);
540}
541
542static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000543ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
544{
545 Py_ssize_t i, numfields = 0;
546 int res = -1;
547 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200548 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000549 if (!fields)
550 PyErr_Clear();
551 if (fields) {
552 numfields = PySequence_Size(fields);
553 if (numfields == -1)
554 goto cleanup;
555 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900556
Neal Norwitz207c9f32008-03-31 04:42:11 +0000557 res = 0; /* if no error occurs, this stays 0 to the end */
INADA Naoki4c78c522017-02-24 02:48:17 +0900558 if (numfields < PyTuple_GET_SIZE(args)) {
559 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
560 "%zd positional argument%s",
561 Py_TYPE(self)->tp_name,
562 numfields, numfields == 1 ? "" : "s");
563 res = -1;
564 goto cleanup;
565 }
566 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
567 /* cannot be reached when fields is NULL */
568 PyObject *name = PySequence_GetItem(fields, i);
569 if (!name) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000570 res = -1;
571 goto cleanup;
572 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900573 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
574 Py_DECREF(name);
575 if (res < 0)
576 goto cleanup;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000577 }
578 if (kw) {
579 i = 0; /* needed by PyDict_Next */
580 while (PyDict_Next(kw, &i, &key, &value)) {
581 res = PyObject_SetAttr(self, key, value);
582 if (res < 0)
583 goto cleanup;
584 }
585 }
586 cleanup:
587 Py_XDECREF(fields);
588 return res;
589}
590
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000591/* Pickling support */
592static PyObject *
593ast_type_reduce(PyObject *self, PyObject *unused)
594{
595 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200596 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200597 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000598 if (dict == NULL) {
599 if (PyErr_ExceptionMatches(PyExc_AttributeError))
600 PyErr_Clear();
601 else
602 return NULL;
603 }
604 if (dict) {
605 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
606 Py_DECREF(dict);
607 return res;
608 }
609 return Py_BuildValue("O()", Py_TYPE(self));
610}
611
612static PyMethodDef ast_type_methods[] = {
613 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
614 {NULL}
615};
616
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700617static PyGetSetDef ast_type_getsets[] = {
618 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
619 {NULL}
620};
621
Neal Norwitz207c9f32008-03-31 04:42:11 +0000622static PyTypeObject AST_type = {
623 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000624 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700625 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000626 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500627 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000628 0, /* tp_print */
629 0, /* tp_getattr */
630 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000631 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000632 0, /* tp_repr */
633 0, /* tp_as_number */
634 0, /* tp_as_sequence */
635 0, /* tp_as_mapping */
636 0, /* tp_hash */
637 0, /* tp_call */
638 0, /* tp_str */
639 PyObject_GenericGetAttr, /* tp_getattro */
640 PyObject_GenericSetAttr, /* tp_setattro */
641 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700642 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000643 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700644 (traverseproc)ast_traverse, /* tp_traverse */
645 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000646 0, /* tp_richcompare */
647 0, /* tp_weaklistoffset */
648 0, /* tp_iter */
649 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000650 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000651 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700652 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000653 0, /* tp_base */
654 0, /* tp_dict */
655 0, /* tp_descr_get */
656 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700657 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000658 (initproc)ast_type_init, /* tp_init */
659 PyType_GenericAlloc, /* tp_alloc */
660 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700661 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000662};
663
664
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000665static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
666{
INADA Naokifc489082017-01-25 22:33:43 +0900667 _Py_IDENTIFIER(__module__);
668 _Py_IDENTIFIER(_ast);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 PyObject *fnames, *result;
670 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000671 fnames = PyTuple_New(num_fields);
672 if (!fnames) return NULL;
673 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000674 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000675 if (!field) {
676 Py_DECREF(fnames);
677 return NULL;
678 }
679 PyTuple_SET_ITEM(fnames, i, field);
680 }
INADA Naokifc489082017-01-25 22:33:43 +0900681 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
682 type, base,
683 _PyUnicode_FromId(&PyId__fields), fnames,
684 _PyUnicode_FromId(&PyId___module__),
685 _PyUnicode_FromId(&PyId__ast));
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Py_DECREF(fnames);
687 return (PyTypeObject*)result;
688}
689
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
691{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000692 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000693 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000694 if (!l)
695 return 0;
696 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000697 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!s) {
699 Py_DECREF(l);
700 return 0;
701 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000702 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200704 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000705 Py_DECREF(l);
706 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707}
708
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000709/* Conversion AST -> Python */
710
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
712{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700713 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 PyObject *result = PyList_New(n);
715 PyObject *value;
716 if (!result)
717 return NULL;
718 for (i = 0; i < n; i++) {
719 value = func(asdl_seq_GET(seq, i));
720 if (!value) {
721 Py_DECREF(result);
722 return NULL;
723 }
724 PyList_SET_ITEM(result, i, value);
725 }
726 return result;
727}
728
729static PyObject* ast2obj_object(void *o)
730{
731 if (!o)
732 o = Py_None;
733 Py_INCREF((PyObject*)o);
734 return (PyObject*)o;
735}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500736#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100737#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738#define ast2obj_identifier ast2obj_object
739#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500740#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000741
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000742static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743{
Christian Heimes217cfd12007-12-02 14:31:20 +0000744 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745}
746
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000747/* Conversion Python -> AST */
748
Benjamin Peterson442f2092012-12-06 17:41:04 -0500749static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
750{
751 if (obj != Py_None && obj != Py_True && obj != Py_False) {
752 PyErr_SetString(PyExc_ValueError,
753 "AST singleton must be True, False, or None");
754 return 1;
755 }
756 *out = obj;
757 return 0;
758}
759
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000760static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
761{
762 if (obj == Py_None)
763 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200764 if (obj) {
765 if (PyArena_AddPyObject(arena, obj) < 0) {
766 *out = NULL;
767 return -1;
768 }
769 Py_INCREF(obj);
770 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000771 *out = obj;
772 return 0;
773}
774
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100775static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
776{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100777 if (obj) {
778 if (PyArena_AddPyObject(arena, obj) < 0) {
779 *out = NULL;
780 return -1;
781 }
782 Py_INCREF(obj);
783 }
784 *out = obj;
785 return 0;
786}
787
Benjamin Peterson180e6352011-07-22 11:09:07 -0500788static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500789{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500790 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
791 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500792 return 1;
793 }
794 return obj2ast_object(obj, out, arena);
795}
796
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500797static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
798{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400799 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500800 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
801 return 1;
802 }
803 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500804}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000805
Benjamin Petersone2498412011-08-09 16:08:39 -0500806static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
807{
808 if (!PyBytes_CheckExact(obj)) {
809 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
810 return 1;
811 }
812 return obj2ast_object(obj, out, arena);
813}
814
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000815static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
816{
817 int i;
818 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100819 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000820 return 1;
821 }
822
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300823 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000824 if (i == -1 && PyErr_Occurred())
825 return 1;
826 *out = i;
827 return 0;
828}
829
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000830static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000831{
832 PyObject *empty_tuple, *d;
833 if (PyType_Ready(&AST_type) < 0)
834 return -1;
835 d = AST_type.tp_dict;
836 empty_tuple = PyTuple_New(0);
837 if (!empty_tuple ||
INADA Naokifc489082017-01-25 22:33:43 +0900838 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
839 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +0000840 Py_XDECREF(empty_tuple);
841 return -1;
842 }
843 Py_DECREF(empty_tuple);
844 return 0;
845}
846
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700847static int exists_not_none(PyObject *obj, _Py_Identifier *id)
848{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700849 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700850 PyObject *attr = _PyObject_GetAttrId(obj, id);
851 if (!attr) {
852 PyErr_Clear();
853 return 0;
854 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700855 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700856 Py_DECREF(attr);
857 return !isnone;
858}
859
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000860
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000861static int init_types(void)
862{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200863 static int initialized;
864 if (initialized) return 1;
865 if (add_ast_fields() < 0) return 0;
866 mod_type = make_type("mod", &AST_type, NULL, 0);
867 if (!mod_type) return 0;
868 if (!add_attributes(mod_type, NULL, 0)) return 0;
INADA Naokicb41b272017-02-23 00:31:59 +0900869 Module_type = make_type("Module", mod_type, Module_fields, 2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200870 if (!Module_type) return 0;
871 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
872 1);
873 if (!Interactive_type) return 0;
874 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
875 if (!Expression_type) return 0;
876 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
877 if (!Suite_type) return 0;
878 stmt_type = make_type("stmt", &AST_type, NULL, 0);
879 if (!stmt_type) return 0;
880 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
881 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
INADA Naokicb41b272017-02-23 00:31:59 +0900882 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200883 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400884 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
INADA Naokicb41b272017-02-23 00:31:59 +0900885 AsyncFunctionDef_fields, 6);
Yury Selivanov75445082015-05-11 22:57:16 -0400886 if (!AsyncFunctionDef_type) return 0;
INADA Naokicb41b272017-02-23 00:31:59 +0900887 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200888 if (!ClassDef_type) return 0;
889 Return_type = make_type("Return", stmt_type, Return_fields, 1);
890 if (!Return_type) return 0;
891 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
892 if (!Delete_type) return 0;
893 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
894 if (!Assign_type) return 0;
895 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
896 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700897 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
898 if (!AnnAssign_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200899 For_type = make_type("For", stmt_type, For_fields, 4);
900 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400901 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
902 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200903 While_type = make_type("While", stmt_type, While_fields, 3);
904 if (!While_type) return 0;
905 If_type = make_type("If", stmt_type, If_fields, 3);
906 if (!If_type) return 0;
907 With_type = make_type("With", stmt_type, With_fields, 2);
908 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400909 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
910 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200911 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
912 if (!Raise_type) return 0;
913 Try_type = make_type("Try", stmt_type, Try_fields, 4);
914 if (!Try_type) return 0;
915 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
916 if (!Assert_type) return 0;
917 Import_type = make_type("Import", stmt_type, Import_fields, 1);
918 if (!Import_type) return 0;
919 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
920 if (!ImportFrom_type) return 0;
921 Global_type = make_type("Global", stmt_type, Global_fields, 1);
922 if (!Global_type) return 0;
923 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
924 if (!Nonlocal_type) return 0;
925 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
926 if (!Expr_type) return 0;
927 Pass_type = make_type("Pass", stmt_type, NULL, 0);
928 if (!Pass_type) return 0;
929 Break_type = make_type("Break", stmt_type, NULL, 0);
930 if (!Break_type) return 0;
931 Continue_type = make_type("Continue", stmt_type, NULL, 0);
932 if (!Continue_type) return 0;
933 expr_type = make_type("expr", &AST_type, NULL, 0);
934 if (!expr_type) return 0;
935 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
936 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
937 if (!BoolOp_type) return 0;
938 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
939 if (!BinOp_type) return 0;
940 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
941 if (!UnaryOp_type) return 0;
942 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
943 if (!Lambda_type) return 0;
944 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
945 if (!IfExp_type) return 0;
946 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
947 if (!Dict_type) return 0;
948 Set_type = make_type("Set", expr_type, Set_fields, 1);
949 if (!Set_type) return 0;
950 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
951 if (!ListComp_type) return 0;
952 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
953 if (!SetComp_type) return 0;
954 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
955 if (!DictComp_type) return 0;
956 GeneratorExp_type = make_type("GeneratorExp", expr_type,
957 GeneratorExp_fields, 2);
958 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400959 Await_type = make_type("Await", expr_type, Await_fields, 1);
960 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200961 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
962 if (!Yield_type) return 0;
963 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
964 if (!YieldFrom_type) return 0;
965 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
966 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400967 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200968 if (!Call_type) return 0;
969 Num_type = make_type("Num", expr_type, Num_fields, 1);
970 if (!Num_type) return 0;
971 Str_type = make_type("Str", expr_type, Str_fields, 1);
972 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400973 FormattedValue_type = make_type("FormattedValue", expr_type,
974 FormattedValue_fields, 3);
975 if (!FormattedValue_type) return 0;
976 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
977 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200978 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
979 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200980 NameConstant_type = make_type("NameConstant", expr_type,
981 NameConstant_fields, 1);
982 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200983 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
984 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100985 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
986 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200987 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
988 if (!Attribute_type) return 0;
989 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
990 if (!Subscript_type) return 0;
991 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
992 if (!Starred_type) return 0;
993 Name_type = make_type("Name", expr_type, Name_fields, 2);
994 if (!Name_type) return 0;
995 List_type = make_type("List", expr_type, List_fields, 2);
996 if (!List_type) return 0;
997 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
998 if (!Tuple_type) return 0;
999 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
1000 if (!expr_context_type) return 0;
1001 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
1002 Load_type = make_type("Load", expr_context_type, NULL, 0);
1003 if (!Load_type) return 0;
1004 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
1005 if (!Load_singleton) return 0;
1006 Store_type = make_type("Store", expr_context_type, NULL, 0);
1007 if (!Store_type) return 0;
1008 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
1009 if (!Store_singleton) return 0;
1010 Del_type = make_type("Del", expr_context_type, NULL, 0);
1011 if (!Del_type) return 0;
1012 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
1013 if (!Del_singleton) return 0;
1014 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
1015 if (!AugLoad_type) return 0;
1016 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
1017 if (!AugLoad_singleton) return 0;
1018 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
1019 if (!AugStore_type) return 0;
1020 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
1021 if (!AugStore_singleton) return 0;
1022 Param_type = make_type("Param", expr_context_type, NULL, 0);
1023 if (!Param_type) return 0;
1024 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1025 if (!Param_singleton) return 0;
1026 slice_type = make_type("slice", &AST_type, NULL, 0);
1027 if (!slice_type) return 0;
1028 if (!add_attributes(slice_type, NULL, 0)) return 0;
1029 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1030 if (!Slice_type) return 0;
1031 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1032 if (!ExtSlice_type) return 0;
1033 Index_type = make_type("Index", slice_type, Index_fields, 1);
1034 if (!Index_type) return 0;
1035 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1036 if (!boolop_type) return 0;
1037 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1038 And_type = make_type("And", boolop_type, NULL, 0);
1039 if (!And_type) return 0;
1040 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1041 if (!And_singleton) return 0;
1042 Or_type = make_type("Or", boolop_type, NULL, 0);
1043 if (!Or_type) return 0;
1044 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1045 if (!Or_singleton) return 0;
1046 operator_type = make_type("operator", &AST_type, NULL, 0);
1047 if (!operator_type) return 0;
1048 if (!add_attributes(operator_type, NULL, 0)) return 0;
1049 Add_type = make_type("Add", operator_type, NULL, 0);
1050 if (!Add_type) return 0;
1051 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1052 if (!Add_singleton) return 0;
1053 Sub_type = make_type("Sub", operator_type, NULL, 0);
1054 if (!Sub_type) return 0;
1055 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1056 if (!Sub_singleton) return 0;
1057 Mult_type = make_type("Mult", operator_type, NULL, 0);
1058 if (!Mult_type) return 0;
1059 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1060 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001061 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1062 if (!MatMult_type) return 0;
1063 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1064 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001065 Div_type = make_type("Div", operator_type, NULL, 0);
1066 if (!Div_type) return 0;
1067 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1068 if (!Div_singleton) return 0;
1069 Mod_type = make_type("Mod", operator_type, NULL, 0);
1070 if (!Mod_type) return 0;
1071 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1072 if (!Mod_singleton) return 0;
1073 Pow_type = make_type("Pow", operator_type, NULL, 0);
1074 if (!Pow_type) return 0;
1075 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1076 if (!Pow_singleton) return 0;
1077 LShift_type = make_type("LShift", operator_type, NULL, 0);
1078 if (!LShift_type) return 0;
1079 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1080 if (!LShift_singleton) return 0;
1081 RShift_type = make_type("RShift", operator_type, NULL, 0);
1082 if (!RShift_type) return 0;
1083 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1084 if (!RShift_singleton) return 0;
1085 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1086 if (!BitOr_type) return 0;
1087 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1088 if (!BitOr_singleton) return 0;
1089 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1090 if (!BitXor_type) return 0;
1091 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1092 if (!BitXor_singleton) return 0;
1093 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1094 if (!BitAnd_type) return 0;
1095 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1096 if (!BitAnd_singleton) return 0;
1097 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1098 if (!FloorDiv_type) return 0;
1099 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1100 if (!FloorDiv_singleton) return 0;
1101 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1102 if (!unaryop_type) return 0;
1103 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1104 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1105 if (!Invert_type) return 0;
1106 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1107 if (!Invert_singleton) return 0;
1108 Not_type = make_type("Not", unaryop_type, NULL, 0);
1109 if (!Not_type) return 0;
1110 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1111 if (!Not_singleton) return 0;
1112 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1113 if (!UAdd_type) return 0;
1114 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1115 if (!UAdd_singleton) return 0;
1116 USub_type = make_type("USub", unaryop_type, NULL, 0);
1117 if (!USub_type) return 0;
1118 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1119 if (!USub_singleton) return 0;
1120 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1121 if (!cmpop_type) return 0;
1122 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1123 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1124 if (!Eq_type) return 0;
1125 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1126 if (!Eq_singleton) return 0;
1127 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1128 if (!NotEq_type) return 0;
1129 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1130 if (!NotEq_singleton) return 0;
1131 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1132 if (!Lt_type) return 0;
1133 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1134 if (!Lt_singleton) return 0;
1135 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1136 if (!LtE_type) return 0;
1137 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1138 if (!LtE_singleton) return 0;
1139 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1140 if (!Gt_type) return 0;
1141 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1142 if (!Gt_singleton) return 0;
1143 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1144 if (!GtE_type) return 0;
1145 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1146 if (!GtE_singleton) return 0;
1147 Is_type = make_type("Is", cmpop_type, NULL, 0);
1148 if (!Is_type) return 0;
1149 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1150 if (!Is_singleton) return 0;
1151 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1152 if (!IsNot_type) return 0;
1153 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1154 if (!IsNot_singleton) return 0;
1155 In_type = make_type("In", cmpop_type, NULL, 0);
1156 if (!In_type) return 0;
1157 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1158 if (!In_singleton) return 0;
1159 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1160 if (!NotIn_type) return 0;
1161 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1162 if (!NotIn_singleton) return 0;
1163 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001164 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001165 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001166 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001167 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1168 if (!excepthandler_type) return 0;
1169 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1170 return 0;
1171 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1172 ExceptHandler_fields, 3);
1173 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001174 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001175 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001176 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001177 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1178 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001179 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001180 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1181 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001182 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001183 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1184 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001185 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001186 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1187 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001188 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001189 initialized = 1;
1190 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001191}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001192
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001193static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1194static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1195static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1196static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1197 arena);
1198static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1199static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1200static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1201static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1202static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1203static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1204 arena);
1205static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1206 arena);
1207static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1208static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1209static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1210static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001211static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001212
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213mod_ty
INADA Naokicb41b272017-02-23 00:31:59 +09001214Module(asdl_seq * body, string docstring, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001216 mod_ty p;
1217 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1218 if (!p)
1219 return NULL;
1220 p->kind = Module_kind;
1221 p->v.Module.body = body;
INADA Naokicb41b272017-02-23 00:31:59 +09001222 p->v.Module.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001223 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224}
1225
1226mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001229 mod_ty p;
1230 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1231 if (!p)
1232 return NULL;
1233 p->kind = Interactive_kind;
1234 p->v.Interactive.body = body;
1235 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236}
1237
1238mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001239Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001241 mod_ty p;
1242 if (!body) {
1243 PyErr_SetString(PyExc_ValueError,
1244 "field body is required for Expression");
1245 return NULL;
1246 }
1247 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1248 if (!p)
1249 return NULL;
1250 p->kind = Expression_kind;
1251 p->v.Expression.body = body;
1252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001256Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001258 mod_ty p;
1259 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1260 if (!p)
1261 return NULL;
1262 p->kind = Suite_kind;
1263 p->v.Suite.body = body;
1264 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265}
1266
1267stmt_ty
1268FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
INADA Naokicb41b272017-02-23 00:31:59 +09001269 decorator_list, expr_ty returns, string docstring, int lineno, int
1270 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001272 stmt_ty p;
1273 if (!name) {
1274 PyErr_SetString(PyExc_ValueError,
1275 "field name is required for FunctionDef");
1276 return NULL;
1277 }
1278 if (!args) {
1279 PyErr_SetString(PyExc_ValueError,
1280 "field args is required for FunctionDef");
1281 return NULL;
1282 }
1283 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1284 if (!p)
1285 return NULL;
1286 p->kind = FunctionDef_kind;
1287 p->v.FunctionDef.name = name;
1288 p->v.FunctionDef.args = args;
1289 p->v.FunctionDef.body = body;
1290 p->v.FunctionDef.decorator_list = decorator_list;
1291 p->v.FunctionDef.returns = returns;
INADA Naokicb41b272017-02-23 00:31:59 +09001292 p->v.FunctionDef.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001293 p->lineno = lineno;
1294 p->col_offset = col_offset;
1295 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296}
1297
1298stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001299AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
INADA Naokicb41b272017-02-23 00:31:59 +09001300 * decorator_list, expr_ty returns, string docstring, int
1301 lineno, int col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001302{
1303 stmt_ty p;
1304 if (!name) {
1305 PyErr_SetString(PyExc_ValueError,
1306 "field name is required for AsyncFunctionDef");
1307 return NULL;
1308 }
1309 if (!args) {
1310 PyErr_SetString(PyExc_ValueError,
1311 "field args is required for AsyncFunctionDef");
1312 return NULL;
1313 }
1314 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1315 if (!p)
1316 return NULL;
1317 p->kind = AsyncFunctionDef_kind;
1318 p->v.AsyncFunctionDef.name = name;
1319 p->v.AsyncFunctionDef.args = args;
1320 p->v.AsyncFunctionDef.body = body;
1321 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1322 p->v.AsyncFunctionDef.returns = returns;
INADA Naokicb41b272017-02-23 00:31:59 +09001323 p->v.AsyncFunctionDef.docstring = docstring;
Yury Selivanov75445082015-05-11 22:57:16 -04001324 p->lineno = lineno;
1325 p->col_offset = col_offset;
1326 return p;
1327}
1328
1329stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001330ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
INADA Naokicb41b272017-02-23 00:31:59 +09001331 body, asdl_seq * decorator_list, string docstring, int lineno, int
1332 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001334 stmt_ty p;
1335 if (!name) {
1336 PyErr_SetString(PyExc_ValueError,
1337 "field name is required for ClassDef");
1338 return NULL;
1339 }
1340 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1341 if (!p)
1342 return NULL;
1343 p->kind = ClassDef_kind;
1344 p->v.ClassDef.name = name;
1345 p->v.ClassDef.bases = bases;
1346 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001347 p->v.ClassDef.body = body;
1348 p->v.ClassDef.decorator_list = decorator_list;
INADA Naokicb41b272017-02-23 00:31:59 +09001349 p->v.ClassDef.docstring = docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001350 p->lineno = lineno;
1351 p->col_offset = col_offset;
1352 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353}
1354
1355stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001356Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001358 stmt_ty p;
1359 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1360 if (!p)
1361 return NULL;
1362 p->kind = Return_kind;
1363 p->v.Return.value = value;
1364 p->lineno = lineno;
1365 p->col_offset = col_offset;
1366 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367}
1368
1369stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001370Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001372 stmt_ty p;
1373 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1374 if (!p)
1375 return NULL;
1376 p->kind = Delete_kind;
1377 p->v.Delete.targets = targets;
1378 p->lineno = lineno;
1379 p->col_offset = col_offset;
1380 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381}
1382
1383stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001384Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1385 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001387 stmt_ty p;
1388 if (!value) {
1389 PyErr_SetString(PyExc_ValueError,
1390 "field value is required for Assign");
1391 return NULL;
1392 }
1393 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1394 if (!p)
1395 return NULL;
1396 p->kind = Assign_kind;
1397 p->v.Assign.targets = targets;
1398 p->v.Assign.value = value;
1399 p->lineno = lineno;
1400 p->col_offset = col_offset;
1401 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402}
1403
1404stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001405AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1406 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001408 stmt_ty p;
1409 if (!target) {
1410 PyErr_SetString(PyExc_ValueError,
1411 "field target is required for AugAssign");
1412 return NULL;
1413 }
1414 if (!op) {
1415 PyErr_SetString(PyExc_ValueError,
1416 "field op is required for AugAssign");
1417 return NULL;
1418 }
1419 if (!value) {
1420 PyErr_SetString(PyExc_ValueError,
1421 "field value is required for AugAssign");
1422 return NULL;
1423 }
1424 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1425 if (!p)
1426 return NULL;
1427 p->kind = AugAssign_kind;
1428 p->v.AugAssign.target = target;
1429 p->v.AugAssign.op = op;
1430 p->v.AugAssign.value = value;
1431 p->lineno = lineno;
1432 p->col_offset = col_offset;
1433 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434}
1435
1436stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001437AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
1438 lineno, int col_offset, PyArena *arena)
1439{
1440 stmt_ty p;
1441 if (!target) {
1442 PyErr_SetString(PyExc_ValueError,
1443 "field target is required for AnnAssign");
1444 return NULL;
1445 }
1446 if (!annotation) {
1447 PyErr_SetString(PyExc_ValueError,
1448 "field annotation is required for AnnAssign");
1449 return NULL;
1450 }
1451 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1452 if (!p)
1453 return NULL;
1454 p->kind = AnnAssign_kind;
1455 p->v.AnnAssign.target = target;
1456 p->v.AnnAssign.annotation = annotation;
1457 p->v.AnnAssign.value = value;
1458 p->v.AnnAssign.simple = simple;
1459 p->lineno = lineno;
1460 p->col_offset = col_offset;
1461 return p;
1462}
1463
1464stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001466 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001468 stmt_ty p;
1469 if (!target) {
1470 PyErr_SetString(PyExc_ValueError,
1471 "field target is required for For");
1472 return NULL;
1473 }
1474 if (!iter) {
1475 PyErr_SetString(PyExc_ValueError,
1476 "field iter is required for For");
1477 return NULL;
1478 }
1479 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1480 if (!p)
1481 return NULL;
1482 p->kind = For_kind;
1483 p->v.For.target = target;
1484 p->v.For.iter = iter;
1485 p->v.For.body = body;
1486 p->v.For.orelse = orelse;
1487 p->lineno = lineno;
1488 p->col_offset = col_offset;
1489 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490}
1491
1492stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001493AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1494 lineno, int col_offset, PyArena *arena)
1495{
1496 stmt_ty p;
1497 if (!target) {
1498 PyErr_SetString(PyExc_ValueError,
1499 "field target is required for AsyncFor");
1500 return NULL;
1501 }
1502 if (!iter) {
1503 PyErr_SetString(PyExc_ValueError,
1504 "field iter is required for AsyncFor");
1505 return NULL;
1506 }
1507 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1508 if (!p)
1509 return NULL;
1510 p->kind = AsyncFor_kind;
1511 p->v.AsyncFor.target = target;
1512 p->v.AsyncFor.iter = iter;
1513 p->v.AsyncFor.body = body;
1514 p->v.AsyncFor.orelse = orelse;
1515 p->lineno = lineno;
1516 p->col_offset = col_offset;
1517 return p;
1518}
1519
1520stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001521While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1522 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001524 stmt_ty p;
1525 if (!test) {
1526 PyErr_SetString(PyExc_ValueError,
1527 "field test is required for While");
1528 return NULL;
1529 }
1530 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1531 if (!p)
1532 return NULL;
1533 p->kind = While_kind;
1534 p->v.While.test = test;
1535 p->v.While.body = body;
1536 p->v.While.orelse = orelse;
1537 p->lineno = lineno;
1538 p->col_offset = col_offset;
1539 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540}
1541
1542stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001543If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1544 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001546 stmt_ty p;
1547 if (!test) {
1548 PyErr_SetString(PyExc_ValueError,
1549 "field test is required for If");
1550 return NULL;
1551 }
1552 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1553 if (!p)
1554 return NULL;
1555 p->kind = If_kind;
1556 p->v.If.test = test;
1557 p->v.If.body = body;
1558 p->v.If.orelse = orelse;
1559 p->lineno = lineno;
1560 p->col_offset = col_offset;
1561 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562}
1563
1564stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001565With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1566 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001567{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001568 stmt_ty p;
1569 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1570 if (!p)
1571 return NULL;
1572 p->kind = With_kind;
1573 p->v.With.items = items;
1574 p->v.With.body = body;
1575 p->lineno = lineno;
1576 p->col_offset = col_offset;
1577 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001578}
1579
1580stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001581AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1582 PyArena *arena)
1583{
1584 stmt_ty p;
1585 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1586 if (!p)
1587 return NULL;
1588 p->kind = AsyncWith_kind;
1589 p->v.AsyncWith.items = items;
1590 p->v.AsyncWith.body = body;
1591 p->lineno = lineno;
1592 p->col_offset = col_offset;
1593 return p;
1594}
1595
1596stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001597Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001599 stmt_ty p;
1600 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1601 if (!p)
1602 return NULL;
1603 p->kind = Raise_kind;
1604 p->v.Raise.exc = exc;
1605 p->v.Raise.cause = cause;
1606 p->lineno = lineno;
1607 p->col_offset = col_offset;
1608 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609}
1610
1611stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001612Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1613 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001615 stmt_ty p;
1616 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1617 if (!p)
1618 return NULL;
1619 p->kind = Try_kind;
1620 p->v.Try.body = body;
1621 p->v.Try.handlers = handlers;
1622 p->v.Try.orelse = orelse;
1623 p->v.Try.finalbody = finalbody;
1624 p->lineno = lineno;
1625 p->col_offset = col_offset;
1626 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627}
1628
1629stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001630Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001632 stmt_ty p;
1633 if (!test) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field test is required for Assert");
1636 return NULL;
1637 }
1638 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1639 if (!p)
1640 return NULL;
1641 p->kind = Assert_kind;
1642 p->v.Assert.test = test;
1643 p->v.Assert.msg = msg;
1644 p->lineno = lineno;
1645 p->col_offset = col_offset;
1646 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647}
1648
1649stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001650Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001652 stmt_ty p;
1653 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1654 if (!p)
1655 return NULL;
1656 p->kind = Import_kind;
1657 p->v.Import.names = names;
1658 p->lineno = lineno;
1659 p->col_offset = col_offset;
1660 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
1662
1663stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001664ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1665 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001667 stmt_ty p;
1668 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1669 if (!p)
1670 return NULL;
1671 p->kind = ImportFrom_kind;
1672 p->v.ImportFrom.module = module;
1673 p->v.ImportFrom.names = names;
1674 p->v.ImportFrom.level = level;
1675 p->lineno = lineno;
1676 p->col_offset = col_offset;
1677 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678}
1679
1680stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001681Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001683 stmt_ty p;
1684 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1685 if (!p)
1686 return NULL;
1687 p->kind = Global_kind;
1688 p->v.Global.names = names;
1689 p->lineno = lineno;
1690 p->col_offset = col_offset;
1691 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692}
1693
1694stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001695Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1696{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001697 stmt_ty p;
1698 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1699 if (!p)
1700 return NULL;
1701 p->kind = Nonlocal_kind;
1702 p->v.Nonlocal.names = names;
1703 p->lineno = lineno;
1704 p->col_offset = col_offset;
1705 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001706}
1707
1708stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001711 stmt_ty p;
1712 if (!value) {
1713 PyErr_SetString(PyExc_ValueError,
1714 "field value is required for Expr");
1715 return NULL;
1716 }
1717 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1718 if (!p)
1719 return NULL;
1720 p->kind = Expr_kind;
1721 p->v.Expr.value = value;
1722 p->lineno = lineno;
1723 p->col_offset = col_offset;
1724 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
1727stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001730 stmt_ty p;
1731 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1732 if (!p)
1733 return NULL;
1734 p->kind = Pass_kind;
1735 p->lineno = lineno;
1736 p->col_offset = col_offset;
1737 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738}
1739
1740stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001741Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001743 stmt_ty p;
1744 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1745 if (!p)
1746 return NULL;
1747 p->kind = Break_kind;
1748 p->lineno = lineno;
1749 p->col_offset = col_offset;
1750 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751}
1752
1753stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001754Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001756 stmt_ty p;
1757 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1758 if (!p)
1759 return NULL;
1760 p->kind = Continue_kind;
1761 p->lineno = lineno;
1762 p->col_offset = col_offset;
1763 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764}
1765
1766expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001767BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1768 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001770 expr_ty p;
1771 if (!op) {
1772 PyErr_SetString(PyExc_ValueError,
1773 "field op is required for BoolOp");
1774 return NULL;
1775 }
1776 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1777 if (!p)
1778 return NULL;
1779 p->kind = BoolOp_kind;
1780 p->v.BoolOp.op = op;
1781 p->v.BoolOp.values = values;
1782 p->lineno = lineno;
1783 p->col_offset = col_offset;
1784 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785}
1786
1787expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001788BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1789 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001791 expr_ty p;
1792 if (!left) {
1793 PyErr_SetString(PyExc_ValueError,
1794 "field left is required for BinOp");
1795 return NULL;
1796 }
1797 if (!op) {
1798 PyErr_SetString(PyExc_ValueError,
1799 "field op is required for BinOp");
1800 return NULL;
1801 }
1802 if (!right) {
1803 PyErr_SetString(PyExc_ValueError,
1804 "field right is required for BinOp");
1805 return NULL;
1806 }
1807 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1808 if (!p)
1809 return NULL;
1810 p->kind = BinOp_kind;
1811 p->v.BinOp.left = left;
1812 p->v.BinOp.op = op;
1813 p->v.BinOp.right = right;
1814 p->lineno = lineno;
1815 p->col_offset = col_offset;
1816 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817}
1818
1819expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001820UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1821 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001823 expr_ty p;
1824 if (!op) {
1825 PyErr_SetString(PyExc_ValueError,
1826 "field op is required for UnaryOp");
1827 return NULL;
1828 }
1829 if (!operand) {
1830 PyErr_SetString(PyExc_ValueError,
1831 "field operand is required for UnaryOp");
1832 return NULL;
1833 }
1834 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1835 if (!p)
1836 return NULL;
1837 p->kind = UnaryOp_kind;
1838 p->v.UnaryOp.op = op;
1839 p->v.UnaryOp.operand = operand;
1840 p->lineno = lineno;
1841 p->col_offset = col_offset;
1842 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843}
1844
1845expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001846Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1847 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001849 expr_ty p;
1850 if (!args) {
1851 PyErr_SetString(PyExc_ValueError,
1852 "field args is required for Lambda");
1853 return NULL;
1854 }
1855 if (!body) {
1856 PyErr_SetString(PyExc_ValueError,
1857 "field body is required for Lambda");
1858 return NULL;
1859 }
1860 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1861 if (!p)
1862 return NULL;
1863 p->kind = Lambda_kind;
1864 p->v.Lambda.args = args;
1865 p->v.Lambda.body = body;
1866 p->lineno = lineno;
1867 p->col_offset = col_offset;
1868 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869}
1870
1871expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001872IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1873 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001874{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001875 expr_ty p;
1876 if (!test) {
1877 PyErr_SetString(PyExc_ValueError,
1878 "field test is required for IfExp");
1879 return NULL;
1880 }
1881 if (!body) {
1882 PyErr_SetString(PyExc_ValueError,
1883 "field body is required for IfExp");
1884 return NULL;
1885 }
1886 if (!orelse) {
1887 PyErr_SetString(PyExc_ValueError,
1888 "field orelse is required for IfExp");
1889 return NULL;
1890 }
1891 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1892 if (!p)
1893 return NULL;
1894 p->kind = IfExp_kind;
1895 p->v.IfExp.test = test;
1896 p->v.IfExp.body = body;
1897 p->v.IfExp.orelse = orelse;
1898 p->lineno = lineno;
1899 p->col_offset = col_offset;
1900 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001901}
1902
1903expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001904Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1905 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001907 expr_ty p;
1908 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1909 if (!p)
1910 return NULL;
1911 p->kind = Dict_kind;
1912 p->v.Dict.keys = keys;
1913 p->v.Dict.values = values;
1914 p->lineno = lineno;
1915 p->col_offset = col_offset;
1916 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917}
1918
1919expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001920Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1921{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001922 expr_ty p;
1923 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1924 if (!p)
1925 return NULL;
1926 p->kind = Set_kind;
1927 p->v.Set.elts = elts;
1928 p->lineno = lineno;
1929 p->col_offset = col_offset;
1930 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001931}
1932
1933expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001934ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1935 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001937 expr_ty p;
1938 if (!elt) {
1939 PyErr_SetString(PyExc_ValueError,
1940 "field elt is required for ListComp");
1941 return NULL;
1942 }
1943 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1944 if (!p)
1945 return NULL;
1946 p->kind = ListComp_kind;
1947 p->v.ListComp.elt = elt;
1948 p->v.ListComp.generators = generators;
1949 p->lineno = lineno;
1950 p->col_offset = col_offset;
1951 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952}
1953
1954expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001955SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1956 *arena)
1957{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001958 expr_ty p;
1959 if (!elt) {
1960 PyErr_SetString(PyExc_ValueError,
1961 "field elt is required for SetComp");
1962 return NULL;
1963 }
1964 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1965 if (!p)
1966 return NULL;
1967 p->kind = SetComp_kind;
1968 p->v.SetComp.elt = elt;
1969 p->v.SetComp.generators = generators;
1970 p->lineno = lineno;
1971 p->col_offset = col_offset;
1972 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001973}
1974
1975expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001976DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1977 col_offset, PyArena *arena)
1978{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001979 expr_ty p;
1980 if (!key) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field key is required for DictComp");
1983 return NULL;
1984 }
1985 if (!value) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "field value is required for DictComp");
1988 return NULL;
1989 }
1990 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1991 if (!p)
1992 return NULL;
1993 p->kind = DictComp_kind;
1994 p->v.DictComp.key = key;
1995 p->v.DictComp.value = value;
1996 p->v.DictComp.generators = generators;
1997 p->lineno = lineno;
1998 p->col_offset = col_offset;
1999 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002000}
2001
2002expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002003GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
2004 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002006 expr_ty p;
2007 if (!elt) {
2008 PyErr_SetString(PyExc_ValueError,
2009 "field elt is required for GeneratorExp");
2010 return NULL;
2011 }
2012 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2013 if (!p)
2014 return NULL;
2015 p->kind = GeneratorExp_kind;
2016 p->v.GeneratorExp.elt = elt;
2017 p->v.GeneratorExp.generators = generators;
2018 p->lineno = lineno;
2019 p->col_offset = col_offset;
2020 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021}
2022
2023expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002024Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
2025{
2026 expr_ty p;
2027 if (!value) {
2028 PyErr_SetString(PyExc_ValueError,
2029 "field value is required for Await");
2030 return NULL;
2031 }
2032 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2033 if (!p)
2034 return NULL;
2035 p->kind = Await_kind;
2036 p->v.Await.value = value;
2037 p->lineno = lineno;
2038 p->col_offset = col_offset;
2039 return p;
2040}
2041
2042expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002043Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002045 expr_ty p;
2046 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2047 if (!p)
2048 return NULL;
2049 p->kind = Yield_kind;
2050 p->v.Yield.value = value;
2051 p->lineno = lineno;
2052 p->col_offset = col_offset;
2053 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054}
2055
2056expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002057YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2058{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002059 expr_ty p;
2060 if (!value) {
2061 PyErr_SetString(PyExc_ValueError,
2062 "field value is required for YieldFrom");
2063 return NULL;
2064 }
2065 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2066 if (!p)
2067 return NULL;
2068 p->kind = YieldFrom_kind;
2069 p->v.YieldFrom.value = value;
2070 p->lineno = lineno;
2071 p->col_offset = col_offset;
2072 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002073}
2074
2075expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002076Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2077 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002079 expr_ty p;
2080 if (!left) {
2081 PyErr_SetString(PyExc_ValueError,
2082 "field left is required for Compare");
2083 return NULL;
2084 }
2085 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2086 if (!p)
2087 return NULL;
2088 p->kind = Compare_kind;
2089 p->v.Compare.left = left;
2090 p->v.Compare.ops = ops;
2091 p->v.Compare.comparators = comparators;
2092 p->lineno = lineno;
2093 p->col_offset = col_offset;
2094 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095}
2096
2097expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002098Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2099 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002101 expr_ty p;
2102 if (!func) {
2103 PyErr_SetString(PyExc_ValueError,
2104 "field func is required for Call");
2105 return NULL;
2106 }
2107 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2108 if (!p)
2109 return NULL;
2110 p->kind = Call_kind;
2111 p->v.Call.func = func;
2112 p->v.Call.args = args;
2113 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002114 p->lineno = lineno;
2115 p->col_offset = col_offset;
2116 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117}
2118
2119expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002120Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002122 expr_ty p;
2123 if (!n) {
2124 PyErr_SetString(PyExc_ValueError,
2125 "field n is required for Num");
2126 return NULL;
2127 }
2128 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2129 if (!p)
2130 return NULL;
2131 p->kind = Num_kind;
2132 p->v.Num.n = n;
2133 p->lineno = lineno;
2134 p->col_offset = col_offset;
2135 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136}
2137
2138expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002139Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002141 expr_ty p;
2142 if (!s) {
2143 PyErr_SetString(PyExc_ValueError,
2144 "field s is required for Str");
2145 return NULL;
2146 }
2147 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2148 if (!p)
2149 return NULL;
2150 p->kind = Str_kind;
2151 p->v.Str.s = s;
2152 p->lineno = lineno;
2153 p->col_offset = col_offset;
2154 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155}
2156
2157expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002158FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2159 int col_offset, PyArena *arena)
2160{
2161 expr_ty p;
2162 if (!value) {
2163 PyErr_SetString(PyExc_ValueError,
2164 "field value is required for FormattedValue");
2165 return NULL;
2166 }
2167 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2168 if (!p)
2169 return NULL;
2170 p->kind = FormattedValue_kind;
2171 p->v.FormattedValue.value = value;
2172 p->v.FormattedValue.conversion = conversion;
2173 p->v.FormattedValue.format_spec = format_spec;
2174 p->lineno = lineno;
2175 p->col_offset = col_offset;
2176 return p;
2177}
2178
2179expr_ty
2180JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2181{
2182 expr_ty p;
2183 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2184 if (!p)
2185 return NULL;
2186 p->kind = JoinedStr_kind;
2187 p->v.JoinedStr.values = values;
2188 p->lineno = lineno;
2189 p->col_offset = col_offset;
2190 return p;
2191}
2192
2193expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002194Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002195{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002196 expr_ty p;
2197 if (!s) {
2198 PyErr_SetString(PyExc_ValueError,
2199 "field s is required for Bytes");
2200 return NULL;
2201 }
2202 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2203 if (!p)
2204 return NULL;
2205 p->kind = Bytes_kind;
2206 p->v.Bytes.s = s;
2207 p->lineno = lineno;
2208 p->col_offset = col_offset;
2209 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002210}
2211
2212expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002213NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2214{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002215 expr_ty p;
2216 if (!value) {
2217 PyErr_SetString(PyExc_ValueError,
2218 "field value is required for NameConstant");
2219 return NULL;
2220 }
2221 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2222 if (!p)
2223 return NULL;
2224 p->kind = NameConstant_kind;
2225 p->v.NameConstant.value = value;
2226 p->lineno = lineno;
2227 p->col_offset = col_offset;
2228 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002229}
2230
2231expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002232Ellipsis(int lineno, int col_offset, PyArena *arena)
2233{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002234 expr_ty p;
2235 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2236 if (!p)
2237 return NULL;
2238 p->kind = Ellipsis_kind;
2239 p->lineno = lineno;
2240 p->col_offset = col_offset;
2241 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002242}
2243
2244expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002245Constant(constant value, int lineno, int col_offset, PyArena *arena)
2246{
2247 expr_ty p;
2248 if (!value) {
2249 PyErr_SetString(PyExc_ValueError,
2250 "field value is required for Constant");
2251 return NULL;
2252 }
2253 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2254 if (!p)
2255 return NULL;
2256 p->kind = Constant_kind;
2257 p->v.Constant.value = value;
2258 p->lineno = lineno;
2259 p->col_offset = col_offset;
2260 return p;
2261}
2262
2263expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002264Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2265 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002267 expr_ty p;
2268 if (!value) {
2269 PyErr_SetString(PyExc_ValueError,
2270 "field value is required for Attribute");
2271 return NULL;
2272 }
2273 if (!attr) {
2274 PyErr_SetString(PyExc_ValueError,
2275 "field attr is required for Attribute");
2276 return NULL;
2277 }
2278 if (!ctx) {
2279 PyErr_SetString(PyExc_ValueError,
2280 "field ctx is required for Attribute");
2281 return NULL;
2282 }
2283 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2284 if (!p)
2285 return NULL;
2286 p->kind = Attribute_kind;
2287 p->v.Attribute.value = value;
2288 p->v.Attribute.attr = attr;
2289 p->v.Attribute.ctx = ctx;
2290 p->lineno = lineno;
2291 p->col_offset = col_offset;
2292 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293}
2294
2295expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002296Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2297 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002299 expr_ty p;
2300 if (!value) {
2301 PyErr_SetString(PyExc_ValueError,
2302 "field value is required for Subscript");
2303 return NULL;
2304 }
2305 if (!slice) {
2306 PyErr_SetString(PyExc_ValueError,
2307 "field slice is required for Subscript");
2308 return NULL;
2309 }
2310 if (!ctx) {
2311 PyErr_SetString(PyExc_ValueError,
2312 "field ctx is required for Subscript");
2313 return NULL;
2314 }
2315 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2316 if (!p)
2317 return NULL;
2318 p->kind = Subscript_kind;
2319 p->v.Subscript.value = value;
2320 p->v.Subscript.slice = slice;
2321 p->v.Subscript.ctx = ctx;
2322 p->lineno = lineno;
2323 p->col_offset = col_offset;
2324 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325}
2326
2327expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002328Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2329 *arena)
2330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002331 expr_ty p;
2332 if (!value) {
2333 PyErr_SetString(PyExc_ValueError,
2334 "field value is required for Starred");
2335 return NULL;
2336 }
2337 if (!ctx) {
2338 PyErr_SetString(PyExc_ValueError,
2339 "field ctx is required for Starred");
2340 return NULL;
2341 }
2342 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2343 if (!p)
2344 return NULL;
2345 p->kind = Starred_kind;
2346 p->v.Starred.value = value;
2347 p->v.Starred.ctx = ctx;
2348 p->lineno = lineno;
2349 p->col_offset = col_offset;
2350 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002351}
2352
2353expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002354Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2355 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002357 expr_ty p;
2358 if (!id) {
2359 PyErr_SetString(PyExc_ValueError,
2360 "field id is required for Name");
2361 return NULL;
2362 }
2363 if (!ctx) {
2364 PyErr_SetString(PyExc_ValueError,
2365 "field ctx is required for Name");
2366 return NULL;
2367 }
2368 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2369 if (!p)
2370 return NULL;
2371 p->kind = Name_kind;
2372 p->v.Name.id = id;
2373 p->v.Name.ctx = ctx;
2374 p->lineno = lineno;
2375 p->col_offset = col_offset;
2376 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377}
2378
2379expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002380List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2381 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002383 expr_ty p;
2384 if (!ctx) {
2385 PyErr_SetString(PyExc_ValueError,
2386 "field ctx is required for List");
2387 return NULL;
2388 }
2389 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2390 if (!p)
2391 return NULL;
2392 p->kind = List_kind;
2393 p->v.List.elts = elts;
2394 p->v.List.ctx = ctx;
2395 p->lineno = lineno;
2396 p->col_offset = col_offset;
2397 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398}
2399
2400expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002401Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2402 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002404 expr_ty p;
2405 if (!ctx) {
2406 PyErr_SetString(PyExc_ValueError,
2407 "field ctx is required for Tuple");
2408 return NULL;
2409 }
2410 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2411 if (!p)
2412 return NULL;
2413 p->kind = Tuple_kind;
2414 p->v.Tuple.elts = elts;
2415 p->v.Tuple.ctx = ctx;
2416 p->lineno = lineno;
2417 p->col_offset = col_offset;
2418 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419}
2420
2421slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002422Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002424 slice_ty p;
2425 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2426 if (!p)
2427 return NULL;
2428 p->kind = Slice_kind;
2429 p->v.Slice.lower = lower;
2430 p->v.Slice.upper = upper;
2431 p->v.Slice.step = step;
2432 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433}
2434
2435slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002436ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002438 slice_ty p;
2439 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2440 if (!p)
2441 return NULL;
2442 p->kind = ExtSlice_kind;
2443 p->v.ExtSlice.dims = dims;
2444 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445}
2446
2447slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002448Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002450 slice_ty p;
2451 if (!value) {
2452 PyErr_SetString(PyExc_ValueError,
2453 "field value is required for Index");
2454 return NULL;
2455 }
2456 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2457 if (!p)
2458 return NULL;
2459 p->kind = Index_kind;
2460 p->v.Index.value = value;
2461 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
2464comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002465comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2466 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002468 comprehension_ty p;
2469 if (!target) {
2470 PyErr_SetString(PyExc_ValueError,
2471 "field target is required for comprehension");
2472 return NULL;
2473 }
2474 if (!iter) {
2475 PyErr_SetString(PyExc_ValueError,
2476 "field iter is required for comprehension");
2477 return NULL;
2478 }
2479 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2480 if (!p)
2481 return NULL;
2482 p->target = target;
2483 p->iter = iter;
2484 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002485 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002486 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487}
2488
2489excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002490ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002491 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002493 excepthandler_ty p;
2494 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2495 if (!p)
2496 return NULL;
2497 p->kind = ExceptHandler_kind;
2498 p->v.ExceptHandler.type = type;
2499 p->v.ExceptHandler.name = name;
2500 p->v.ExceptHandler.body = body;
2501 p->lineno = lineno;
2502 p->col_offset = col_offset;
2503 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504}
2505
2506arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002507arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2508 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002510 arguments_ty p;
2511 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2512 if (!p)
2513 return NULL;
2514 p->args = args;
2515 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002516 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002517 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002518 p->kwarg = kwarg;
2519 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002520 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521}
2522
Neal Norwitzc1505362006-12-28 06:47:50 +00002523arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002524arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2525 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002526{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002527 arg_ty p;
2528 if (!arg) {
2529 PyErr_SetString(PyExc_ValueError,
2530 "field arg is required for arg");
2531 return NULL;
2532 }
2533 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2534 if (!p)
2535 return NULL;
2536 p->arg = arg;
2537 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002538 p->lineno = lineno;
2539 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002540 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002541}
2542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002544keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002546 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002547 if (!value) {
2548 PyErr_SetString(PyExc_ValueError,
2549 "field value is required for keyword");
2550 return NULL;
2551 }
2552 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2553 if (!p)
2554 return NULL;
2555 p->arg = arg;
2556 p->value = value;
2557 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558}
2559
2560alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002561alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002563 alias_ty p;
2564 if (!name) {
2565 PyErr_SetString(PyExc_ValueError,
2566 "field name is required for alias");
2567 return NULL;
2568 }
2569 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2570 if (!p)
2571 return NULL;
2572 p->name = name;
2573 p->asname = asname;
2574 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575}
2576
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002577withitem_ty
2578withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2579{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002580 withitem_ty p;
2581 if (!context_expr) {
2582 PyErr_SetString(PyExc_ValueError,
2583 "field context_expr is required for withitem");
2584 return NULL;
2585 }
2586 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2587 if (!p)
2588 return NULL;
2589 p->context_expr = context_expr;
2590 p->optional_vars = optional_vars;
2591 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002592}
2593
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002594
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002595PyObject*
2596ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002597{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002598 mod_ty o = (mod_ty)_o;
2599 PyObject *result = NULL, *value = NULL;
2600 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002601 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002602 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002603
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002604 switch (o->kind) {
2605 case Module_kind:
2606 result = PyType_GenericNew(Module_type, NULL, NULL);
2607 if (!result) goto failed;
2608 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2609 if (!value) goto failed;
2610 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002613 value = ast2obj_string(o->v.Module.docstring);
2614 if (!value) goto failed;
2615 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002618 break;
2619 case Interactive_kind:
2620 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2621 if (!result) goto failed;
2622 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2623 if (!value) goto failed;
2624 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 break;
2628 case Expression_kind:
2629 result = PyType_GenericNew(Expression_type, NULL, NULL);
2630 if (!result) goto failed;
2631 value = ast2obj_expr(o->v.Expression.body);
2632 if (!value) goto failed;
2633 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
2636 break;
2637 case Suite_kind:
2638 result = PyType_GenericNew(Suite_type, NULL, NULL);
2639 if (!result) goto failed;
2640 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2641 if (!value) goto failed;
2642 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
2645 break;
2646 }
2647 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002648failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002649 Py_XDECREF(value);
2650 Py_XDECREF(result);
2651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652}
2653
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002654PyObject*
2655ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002657 stmt_ty o = (stmt_ty)_o;
2658 PyObject *result = NULL, *value = NULL;
2659 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002660 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002661 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002662
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002663 switch (o->kind) {
2664 case FunctionDef_kind:
2665 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2666 if (!result) goto failed;
2667 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002668 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002669 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2670 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002671 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002672 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002673 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002674 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2675 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002676 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002677 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2678 if (!value) goto failed;
2679 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2680 goto failed;
2681 Py_DECREF(value);
2682 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2683 if (!value) goto failed;
2684 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 value = ast2obj_expr(o->v.FunctionDef.returns);
2688 if (!value) goto failed;
2689 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2690 goto failed;
2691 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002692 value = ast2obj_string(o->v.FunctionDef.docstring);
2693 if (!value) goto failed;
2694 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002697 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002698 case AsyncFunctionDef_kind:
2699 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2700 if (!result) goto failed;
2701 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2702 if (!value) goto failed;
2703 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2704 goto failed;
2705 Py_DECREF(value);
2706 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2707 if (!value) goto failed;
2708 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
2711 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2712 if (!value) goto failed;
2713 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
2716 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2717 ast2obj_expr);
2718 if (!value) goto failed;
2719 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2723 if (!value) goto failed;
2724 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002727 value = ast2obj_string(o->v.AsyncFunctionDef.docstring);
2728 if (!value) goto failed;
2729 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2730 goto failed;
2731 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04002732 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002733 case ClassDef_kind:
2734 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2735 if (!result) goto failed;
2736 value = ast2obj_identifier(o->v.ClassDef.name);
2737 if (!value) goto failed;
2738 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
2741 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2742 if (!value) goto failed;
2743 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002751 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2752 if (!value) goto failed;
2753 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2757 if (!value) goto failed;
2758 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
INADA Naokicb41b272017-02-23 00:31:59 +09002761 value = ast2obj_string(o->v.ClassDef.docstring);
2762 if (!value) goto failed;
2763 if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
2764 goto failed;
2765 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002766 break;
2767 case Return_kind:
2768 result = PyType_GenericNew(Return_type, NULL, NULL);
2769 if (!result) goto failed;
2770 value = ast2obj_expr(o->v.Return.value);
2771 if (!value) goto failed;
2772 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 break;
2776 case Delete_kind:
2777 result = PyType_GenericNew(Delete_type, NULL, NULL);
2778 if (!result) goto failed;
2779 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 break;
2785 case Assign_kind:
2786 result = PyType_GenericNew(Assign_type, NULL, NULL);
2787 if (!result) goto failed;
2788 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2789 if (!value) goto failed;
2790 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2791 goto failed;
2792 Py_DECREF(value);
2793 value = ast2obj_expr(o->v.Assign.value);
2794 if (!value) goto failed;
2795 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 break;
2799 case AugAssign_kind:
2800 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2801 if (!result) goto failed;
2802 value = ast2obj_expr(o->v.AugAssign.target);
2803 if (!value) goto failed;
2804 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
2807 value = ast2obj_operator(o->v.AugAssign.op);
2808 if (!value) goto failed;
2809 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
2812 value = ast2obj_expr(o->v.AugAssign.value);
2813 if (!value) goto failed;
2814 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2815 goto failed;
2816 Py_DECREF(value);
2817 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002818 case AnnAssign_kind:
2819 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2820 if (!result) goto failed;
2821 value = ast2obj_expr(o->v.AnnAssign.target);
2822 if (!value) goto failed;
2823 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2824 goto failed;
2825 Py_DECREF(value);
2826 value = ast2obj_expr(o->v.AnnAssign.annotation);
2827 if (!value) goto failed;
2828 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
2831 value = ast2obj_expr(o->v.AnnAssign.value);
2832 if (!value) goto failed;
2833 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2834 goto failed;
2835 Py_DECREF(value);
2836 value = ast2obj_int(o->v.AnnAssign.simple);
2837 if (!value) goto failed;
2838 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2839 goto failed;
2840 Py_DECREF(value);
2841 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002842 case For_kind:
2843 result = PyType_GenericNew(For_type, NULL, NULL);
2844 if (!result) goto failed;
2845 value = ast2obj_expr(o->v.For.target);
2846 if (!value) goto failed;
2847 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2848 goto failed;
2849 Py_DECREF(value);
2850 value = ast2obj_expr(o->v.For.iter);
2851 if (!value) goto failed;
2852 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2856 if (!value) goto failed;
2857 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2858 goto failed;
2859 Py_DECREF(value);
2860 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2861 if (!value) goto failed;
2862 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002866 case AsyncFor_kind:
2867 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2868 if (!result) goto failed;
2869 value = ast2obj_expr(o->v.AsyncFor.target);
2870 if (!value) goto failed;
2871 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2872 goto failed;
2873 Py_DECREF(value);
2874 value = ast2obj_expr(o->v.AsyncFor.iter);
2875 if (!value) goto failed;
2876 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2877 goto failed;
2878 Py_DECREF(value);
2879 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2880 if (!value) goto failed;
2881 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
2884 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002890 case While_kind:
2891 result = PyType_GenericNew(While_type, NULL, NULL);
2892 if (!result) goto failed;
2893 value = ast2obj_expr(o->v.While.test);
2894 if (!value) goto failed;
2895 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2896 goto failed;
2897 Py_DECREF(value);
2898 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2904 if (!value) goto failed;
2905 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 break;
2909 case If_kind:
2910 result = PyType_GenericNew(If_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_expr(o->v.If.test);
2913 if (!value) goto failed;
2914 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2918 if (!value) goto failed;
2919 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2920 goto failed;
2921 Py_DECREF(value);
2922 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2923 if (!value) goto failed;
2924 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2925 goto failed;
2926 Py_DECREF(value);
2927 break;
2928 case With_kind:
2929 result = PyType_GenericNew(With_type, NULL, NULL);
2930 if (!result) goto failed;
2931 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2932 if (!value) goto failed;
2933 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
2936 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2937 if (!value) goto failed;
2938 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2939 goto failed;
2940 Py_DECREF(value);
2941 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002942 case AsyncWith_kind:
2943 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2944 if (!result) goto failed;
2945 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2946 if (!value) goto failed;
2947 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2948 goto failed;
2949 Py_DECREF(value);
2950 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2951 if (!value) goto failed;
2952 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2953 goto failed;
2954 Py_DECREF(value);
2955 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002956 case Raise_kind:
2957 result = PyType_GenericNew(Raise_type, NULL, NULL);
2958 if (!result) goto failed;
2959 value = ast2obj_expr(o->v.Raise.exc);
2960 if (!value) goto failed;
2961 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2962 goto failed;
2963 Py_DECREF(value);
2964 value = ast2obj_expr(o->v.Raise.cause);
2965 if (!value) goto failed;
2966 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 break;
2970 case Try_kind:
2971 result = PyType_GenericNew(Try_type, NULL, NULL);
2972 if (!result) goto failed;
2973 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2974 if (!value) goto failed;
2975 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2979 if (!value) goto failed;
2980 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
2983 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2984 if (!value) goto failed;
2985 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2986 goto failed;
2987 Py_DECREF(value);
2988 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2989 if (!value) goto failed;
2990 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2991 goto failed;
2992 Py_DECREF(value);
2993 break;
2994 case Assert_kind:
2995 result = PyType_GenericNew(Assert_type, NULL, NULL);
2996 if (!result) goto failed;
2997 value = ast2obj_expr(o->v.Assert.test);
2998 if (!value) goto failed;
2999 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3000 goto failed;
3001 Py_DECREF(value);
3002 value = ast2obj_expr(o->v.Assert.msg);
3003 if (!value) goto failed;
3004 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
3007 break;
3008 case Import_kind:
3009 result = PyType_GenericNew(Import_type, NULL, NULL);
3010 if (!result) goto failed;
3011 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
3012 if (!value) goto failed;
3013 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3014 goto failed;
3015 Py_DECREF(value);
3016 break;
3017 case ImportFrom_kind:
3018 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
3019 if (!result) goto failed;
3020 value = ast2obj_identifier(o->v.ImportFrom.module);
3021 if (!value) goto failed;
3022 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
3023 goto failed;
3024 Py_DECREF(value);
3025 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
3026 if (!value) goto failed;
3027 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3028 goto failed;
3029 Py_DECREF(value);
3030 value = ast2obj_int(o->v.ImportFrom.level);
3031 if (!value) goto failed;
3032 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3033 goto failed;
3034 Py_DECREF(value);
3035 break;
3036 case Global_kind:
3037 result = PyType_GenericNew(Global_type, NULL, NULL);
3038 if (!result) goto failed;
3039 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3040 if (!value) goto failed;
3041 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3042 goto failed;
3043 Py_DECREF(value);
3044 break;
3045 case Nonlocal_kind:
3046 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3047 if (!result) goto failed;
3048 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3049 if (!value) goto failed;
3050 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3051 goto failed;
3052 Py_DECREF(value);
3053 break;
3054 case Expr_kind:
3055 result = PyType_GenericNew(Expr_type, NULL, NULL);
3056 if (!result) goto failed;
3057 value = ast2obj_expr(o->v.Expr.value);
3058 if (!value) goto failed;
3059 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3060 goto failed;
3061 Py_DECREF(value);
3062 break;
3063 case Pass_kind:
3064 result = PyType_GenericNew(Pass_type, NULL, NULL);
3065 if (!result) goto failed;
3066 break;
3067 case Break_kind:
3068 result = PyType_GenericNew(Break_type, NULL, NULL);
3069 if (!result) goto failed;
3070 break;
3071 case Continue_kind:
3072 result = PyType_GenericNew(Continue_type, NULL, NULL);
3073 if (!result) goto failed;
3074 break;
3075 }
3076 value = ast2obj_int(o->lineno);
3077 if (!value) goto failed;
3078 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3079 goto failed;
3080 Py_DECREF(value);
3081 value = ast2obj_int(o->col_offset);
3082 if (!value) goto failed;
3083 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3084 goto failed;
3085 Py_DECREF(value);
3086 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003087failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003088 Py_XDECREF(value);
3089 Py_XDECREF(result);
3090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091}
3092
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003093PyObject*
3094ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003096 expr_ty o = (expr_ty)_o;
3097 PyObject *result = NULL, *value = NULL;
3098 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003099 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003100 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003101
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003102 switch (o->kind) {
3103 case BoolOp_kind:
3104 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3105 if (!result) goto failed;
3106 value = ast2obj_boolop(o->v.BoolOp.op);
3107 if (!value) goto failed;
3108 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3109 goto failed;
3110 Py_DECREF(value);
3111 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3112 if (!value) goto failed;
3113 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3114 goto failed;
3115 Py_DECREF(value);
3116 break;
3117 case BinOp_kind:
3118 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3119 if (!result) goto failed;
3120 value = ast2obj_expr(o->v.BinOp.left);
3121 if (!value) goto failed;
3122 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3123 goto failed;
3124 Py_DECREF(value);
3125 value = ast2obj_operator(o->v.BinOp.op);
3126 if (!value) goto failed;
3127 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
3130 value = ast2obj_expr(o->v.BinOp.right);
3131 if (!value) goto failed;
3132 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3133 goto failed;
3134 Py_DECREF(value);
3135 break;
3136 case UnaryOp_kind:
3137 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3138 if (!result) goto failed;
3139 value = ast2obj_unaryop(o->v.UnaryOp.op);
3140 if (!value) goto failed;
3141 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3142 goto failed;
3143 Py_DECREF(value);
3144 value = ast2obj_expr(o->v.UnaryOp.operand);
3145 if (!value) goto failed;
3146 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3147 goto failed;
3148 Py_DECREF(value);
3149 break;
3150 case Lambda_kind:
3151 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3152 if (!result) goto failed;
3153 value = ast2obj_arguments(o->v.Lambda.args);
3154 if (!value) goto failed;
3155 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3156 goto failed;
3157 Py_DECREF(value);
3158 value = ast2obj_expr(o->v.Lambda.body);
3159 if (!value) goto failed;
3160 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3161 goto failed;
3162 Py_DECREF(value);
3163 break;
3164 case IfExp_kind:
3165 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3166 if (!result) goto failed;
3167 value = ast2obj_expr(o->v.IfExp.test);
3168 if (!value) goto failed;
3169 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3170 goto failed;
3171 Py_DECREF(value);
3172 value = ast2obj_expr(o->v.IfExp.body);
3173 if (!value) goto failed;
3174 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3175 goto failed;
3176 Py_DECREF(value);
3177 value = ast2obj_expr(o->v.IfExp.orelse);
3178 if (!value) goto failed;
3179 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3180 goto failed;
3181 Py_DECREF(value);
3182 break;
3183 case Dict_kind:
3184 result = PyType_GenericNew(Dict_type, NULL, NULL);
3185 if (!result) goto failed;
3186 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3187 if (!value) goto failed;
3188 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3189 goto failed;
3190 Py_DECREF(value);
3191 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3192 if (!value) goto failed;
3193 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3194 goto failed;
3195 Py_DECREF(value);
3196 break;
3197 case Set_kind:
3198 result = PyType_GenericNew(Set_type, NULL, NULL);
3199 if (!result) goto failed;
3200 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3201 if (!value) goto failed;
3202 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3203 goto failed;
3204 Py_DECREF(value);
3205 break;
3206 case ListComp_kind:
3207 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3208 if (!result) goto failed;
3209 value = ast2obj_expr(o->v.ListComp.elt);
3210 if (!value) goto failed;
3211 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3212 goto failed;
3213 Py_DECREF(value);
3214 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3215 if (!value) goto failed;
3216 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3217 goto failed;
3218 Py_DECREF(value);
3219 break;
3220 case SetComp_kind:
3221 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3222 if (!result) goto failed;
3223 value = ast2obj_expr(o->v.SetComp.elt);
3224 if (!value) goto failed;
3225 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3226 goto failed;
3227 Py_DECREF(value);
3228 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3229 if (!value) goto failed;
3230 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3231 goto failed;
3232 Py_DECREF(value);
3233 break;
3234 case DictComp_kind:
3235 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3236 if (!result) goto failed;
3237 value = ast2obj_expr(o->v.DictComp.key);
3238 if (!value) goto failed;
3239 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3240 goto failed;
3241 Py_DECREF(value);
3242 value = ast2obj_expr(o->v.DictComp.value);
3243 if (!value) goto failed;
3244 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3245 goto failed;
3246 Py_DECREF(value);
3247 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3248 if (!value) goto failed;
3249 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3250 goto failed;
3251 Py_DECREF(value);
3252 break;
3253 case GeneratorExp_kind:
3254 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3255 if (!result) goto failed;
3256 value = ast2obj_expr(o->v.GeneratorExp.elt);
3257 if (!value) goto failed;
3258 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3259 goto failed;
3260 Py_DECREF(value);
3261 value = ast2obj_list(o->v.GeneratorExp.generators,
3262 ast2obj_comprehension);
3263 if (!value) goto failed;
3264 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3265 goto failed;
3266 Py_DECREF(value);
3267 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003268 case Await_kind:
3269 result = PyType_GenericNew(Await_type, NULL, NULL);
3270 if (!result) goto failed;
3271 value = ast2obj_expr(o->v.Await.value);
3272 if (!value) goto failed;
3273 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3274 goto failed;
3275 Py_DECREF(value);
3276 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003277 case Yield_kind:
3278 result = PyType_GenericNew(Yield_type, NULL, NULL);
3279 if (!result) goto failed;
3280 value = ast2obj_expr(o->v.Yield.value);
3281 if (!value) goto failed;
3282 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3283 goto failed;
3284 Py_DECREF(value);
3285 break;
3286 case YieldFrom_kind:
3287 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3288 if (!result) goto failed;
3289 value = ast2obj_expr(o->v.YieldFrom.value);
3290 if (!value) goto failed;
3291 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3292 goto failed;
3293 Py_DECREF(value);
3294 break;
3295 case Compare_kind:
3296 result = PyType_GenericNew(Compare_type, NULL, NULL);
3297 if (!result) goto failed;
3298 value = ast2obj_expr(o->v.Compare.left);
3299 if (!value) goto failed;
3300 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3301 goto failed;
3302 Py_DECREF(value);
3303 {
3304 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3305 value = PyList_New(n);
3306 if (!value) goto failed;
3307 for(i = 0; i < n; i++)
3308 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003310 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003311 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3312 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003313 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003314 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003315 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003316 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3317 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003318 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003319 break;
3320 case Call_kind:
3321 result = PyType_GenericNew(Call_type, NULL, NULL);
3322 if (!result) goto failed;
3323 value = ast2obj_expr(o->v.Call.func);
3324 if (!value) goto failed;
3325 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3326 goto failed;
3327 Py_DECREF(value);
3328 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3329 if (!value) goto failed;
3330 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3331 goto failed;
3332 Py_DECREF(value);
3333 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3334 if (!value) goto failed;
3335 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3336 goto failed;
3337 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 break;
3339 case Num_kind:
3340 result = PyType_GenericNew(Num_type, NULL, NULL);
3341 if (!result) goto failed;
3342 value = ast2obj_object(o->v.Num.n);
3343 if (!value) goto failed;
3344 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3345 goto failed;
3346 Py_DECREF(value);
3347 break;
3348 case Str_kind:
3349 result = PyType_GenericNew(Str_type, NULL, NULL);
3350 if (!result) goto failed;
3351 value = ast2obj_string(o->v.Str.s);
3352 if (!value) goto failed;
3353 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3354 goto failed;
3355 Py_DECREF(value);
3356 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003357 case FormattedValue_kind:
3358 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3359 if (!result) goto failed;
3360 value = ast2obj_expr(o->v.FormattedValue.value);
3361 if (!value) goto failed;
3362 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3363 goto failed;
3364 Py_DECREF(value);
3365 value = ast2obj_int(o->v.FormattedValue.conversion);
3366 if (!value) goto failed;
3367 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3368 goto failed;
3369 Py_DECREF(value);
3370 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3371 if (!value) goto failed;
3372 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3373 goto failed;
3374 Py_DECREF(value);
3375 break;
3376 case JoinedStr_kind:
3377 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3378 if (!result) goto failed;
3379 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3380 if (!value) goto failed;
3381 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3382 goto failed;
3383 Py_DECREF(value);
3384 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003385 case Bytes_kind:
3386 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3387 if (!result) goto failed;
3388 value = ast2obj_bytes(o->v.Bytes.s);
3389 if (!value) goto failed;
3390 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3391 goto failed;
3392 Py_DECREF(value);
3393 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003394 case NameConstant_kind:
3395 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3396 if (!result) goto failed;
3397 value = ast2obj_singleton(o->v.NameConstant.value);
3398 if (!value) goto failed;
3399 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3400 goto failed;
3401 Py_DECREF(value);
3402 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003403 case Ellipsis_kind:
3404 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3405 if (!result) goto failed;
3406 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003407 case Constant_kind:
3408 result = PyType_GenericNew(Constant_type, NULL, NULL);
3409 if (!result) goto failed;
3410 value = ast2obj_constant(o->v.Constant.value);
3411 if (!value) goto failed;
3412 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3413 goto failed;
3414 Py_DECREF(value);
3415 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003416 case Attribute_kind:
3417 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3418 if (!result) goto failed;
3419 value = ast2obj_expr(o->v.Attribute.value);
3420 if (!value) goto failed;
3421 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3422 goto failed;
3423 Py_DECREF(value);
3424 value = ast2obj_identifier(o->v.Attribute.attr);
3425 if (!value) goto failed;
3426 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3427 goto failed;
3428 Py_DECREF(value);
3429 value = ast2obj_expr_context(o->v.Attribute.ctx);
3430 if (!value) goto failed;
3431 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3432 goto failed;
3433 Py_DECREF(value);
3434 break;
3435 case Subscript_kind:
3436 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3437 if (!result) goto failed;
3438 value = ast2obj_expr(o->v.Subscript.value);
3439 if (!value) goto failed;
3440 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3441 goto failed;
3442 Py_DECREF(value);
3443 value = ast2obj_slice(o->v.Subscript.slice);
3444 if (!value) goto failed;
3445 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3446 goto failed;
3447 Py_DECREF(value);
3448 value = ast2obj_expr_context(o->v.Subscript.ctx);
3449 if (!value) goto failed;
3450 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3451 goto failed;
3452 Py_DECREF(value);
3453 break;
3454 case Starred_kind:
3455 result = PyType_GenericNew(Starred_type, NULL, NULL);
3456 if (!result) goto failed;
3457 value = ast2obj_expr(o->v.Starred.value);
3458 if (!value) goto failed;
3459 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3460 goto failed;
3461 Py_DECREF(value);
3462 value = ast2obj_expr_context(o->v.Starred.ctx);
3463 if (!value) goto failed;
3464 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3465 goto failed;
3466 Py_DECREF(value);
3467 break;
3468 case Name_kind:
3469 result = PyType_GenericNew(Name_type, NULL, NULL);
3470 if (!result) goto failed;
3471 value = ast2obj_identifier(o->v.Name.id);
3472 if (!value) goto failed;
3473 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3474 goto failed;
3475 Py_DECREF(value);
3476 value = ast2obj_expr_context(o->v.Name.ctx);
3477 if (!value) goto failed;
3478 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3479 goto failed;
3480 Py_DECREF(value);
3481 break;
3482 case List_kind:
3483 result = PyType_GenericNew(List_type, NULL, NULL);
3484 if (!result) goto failed;
3485 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3486 if (!value) goto failed;
3487 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3488 goto failed;
3489 Py_DECREF(value);
3490 value = ast2obj_expr_context(o->v.List.ctx);
3491 if (!value) goto failed;
3492 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3493 goto failed;
3494 Py_DECREF(value);
3495 break;
3496 case Tuple_kind:
3497 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3498 if (!result) goto failed;
3499 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3500 if (!value) goto failed;
3501 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3502 goto failed;
3503 Py_DECREF(value);
3504 value = ast2obj_expr_context(o->v.Tuple.ctx);
3505 if (!value) goto failed;
3506 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3507 goto failed;
3508 Py_DECREF(value);
3509 break;
3510 }
3511 value = ast2obj_int(o->lineno);
3512 if (!value) goto failed;
3513 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3514 goto failed;
3515 Py_DECREF(value);
3516 value = ast2obj_int(o->col_offset);
3517 if (!value) goto failed;
3518 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3519 goto failed;
3520 Py_DECREF(value);
3521 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003522failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003523 Py_XDECREF(value);
3524 Py_XDECREF(result);
3525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003528PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 switch(o) {
3531 case Load:
3532 Py_INCREF(Load_singleton);
3533 return Load_singleton;
3534 case Store:
3535 Py_INCREF(Store_singleton);
3536 return Store_singleton;
3537 case Del:
3538 Py_INCREF(Del_singleton);
3539 return Del_singleton;
3540 case AugLoad:
3541 Py_INCREF(AugLoad_singleton);
3542 return AugLoad_singleton;
3543 case AugStore:
3544 Py_INCREF(AugStore_singleton);
3545 return AugStore_singleton;
3546 case Param:
3547 Py_INCREF(Param_singleton);
3548 return Param_singleton;
3549 default:
3550 /* should never happen, but just in case ... */
3551 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3552 return NULL;
3553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003555PyObject*
3556ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003558 slice_ty o = (slice_ty)_o;
3559 PyObject *result = NULL, *value = NULL;
3560 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003561 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003563
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003564 switch (o->kind) {
3565 case Slice_kind:
3566 result = PyType_GenericNew(Slice_type, NULL, NULL);
3567 if (!result) goto failed;
3568 value = ast2obj_expr(o->v.Slice.lower);
3569 if (!value) goto failed;
3570 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3571 goto failed;
3572 Py_DECREF(value);
3573 value = ast2obj_expr(o->v.Slice.upper);
3574 if (!value) goto failed;
3575 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3576 goto failed;
3577 Py_DECREF(value);
3578 value = ast2obj_expr(o->v.Slice.step);
3579 if (!value) goto failed;
3580 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3581 goto failed;
3582 Py_DECREF(value);
3583 break;
3584 case ExtSlice_kind:
3585 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3586 if (!result) goto failed;
3587 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3588 if (!value) goto failed;
3589 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3590 goto failed;
3591 Py_DECREF(value);
3592 break;
3593 case Index_kind:
3594 result = PyType_GenericNew(Index_type, NULL, NULL);
3595 if (!result) goto failed;
3596 value = ast2obj_expr(o->v.Index.value);
3597 if (!value) goto failed;
3598 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3599 goto failed;
3600 Py_DECREF(value);
3601 break;
3602 }
3603 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003604failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003605 Py_XDECREF(value);
3606 Py_XDECREF(result);
3607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608}
3609
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003610PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003612 switch(o) {
3613 case And:
3614 Py_INCREF(And_singleton);
3615 return And_singleton;
3616 case Or:
3617 Py_INCREF(Or_singleton);
3618 return Or_singleton;
3619 default:
3620 /* should never happen, but just in case ... */
3621 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3622 return NULL;
3623 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003624}
3625PyObject* ast2obj_operator(operator_ty o)
3626{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003627 switch(o) {
3628 case Add:
3629 Py_INCREF(Add_singleton);
3630 return Add_singleton;
3631 case Sub:
3632 Py_INCREF(Sub_singleton);
3633 return Sub_singleton;
3634 case Mult:
3635 Py_INCREF(Mult_singleton);
3636 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003637 case MatMult:
3638 Py_INCREF(MatMult_singleton);
3639 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003640 case Div:
3641 Py_INCREF(Div_singleton);
3642 return Div_singleton;
3643 case Mod:
3644 Py_INCREF(Mod_singleton);
3645 return Mod_singleton;
3646 case Pow:
3647 Py_INCREF(Pow_singleton);
3648 return Pow_singleton;
3649 case LShift:
3650 Py_INCREF(LShift_singleton);
3651 return LShift_singleton;
3652 case RShift:
3653 Py_INCREF(RShift_singleton);
3654 return RShift_singleton;
3655 case BitOr:
3656 Py_INCREF(BitOr_singleton);
3657 return BitOr_singleton;
3658 case BitXor:
3659 Py_INCREF(BitXor_singleton);
3660 return BitXor_singleton;
3661 case BitAnd:
3662 Py_INCREF(BitAnd_singleton);
3663 return BitAnd_singleton;
3664 case FloorDiv:
3665 Py_INCREF(FloorDiv_singleton);
3666 return FloorDiv_singleton;
3667 default:
3668 /* should never happen, but just in case ... */
3669 PyErr_Format(PyExc_SystemError, "unknown operator found");
3670 return NULL;
3671 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003672}
3673PyObject* ast2obj_unaryop(unaryop_ty o)
3674{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003675 switch(o) {
3676 case Invert:
3677 Py_INCREF(Invert_singleton);
3678 return Invert_singleton;
3679 case Not:
3680 Py_INCREF(Not_singleton);
3681 return Not_singleton;
3682 case UAdd:
3683 Py_INCREF(UAdd_singleton);
3684 return UAdd_singleton;
3685 case USub:
3686 Py_INCREF(USub_singleton);
3687 return USub_singleton;
3688 default:
3689 /* should never happen, but just in case ... */
3690 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3691 return NULL;
3692 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003693}
3694PyObject* ast2obj_cmpop(cmpop_ty o)
3695{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003696 switch(o) {
3697 case Eq:
3698 Py_INCREF(Eq_singleton);
3699 return Eq_singleton;
3700 case NotEq:
3701 Py_INCREF(NotEq_singleton);
3702 return NotEq_singleton;
3703 case Lt:
3704 Py_INCREF(Lt_singleton);
3705 return Lt_singleton;
3706 case LtE:
3707 Py_INCREF(LtE_singleton);
3708 return LtE_singleton;
3709 case Gt:
3710 Py_INCREF(Gt_singleton);
3711 return Gt_singleton;
3712 case GtE:
3713 Py_INCREF(GtE_singleton);
3714 return GtE_singleton;
3715 case Is:
3716 Py_INCREF(Is_singleton);
3717 return Is_singleton;
3718 case IsNot:
3719 Py_INCREF(IsNot_singleton);
3720 return IsNot_singleton;
3721 case In:
3722 Py_INCREF(In_singleton);
3723 return In_singleton;
3724 case NotIn:
3725 Py_INCREF(NotIn_singleton);
3726 return NotIn_singleton;
3727 default:
3728 /* should never happen, but just in case ... */
3729 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3730 return NULL;
3731 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003732}
3733PyObject*
3734ast2obj_comprehension(void* _o)
3735{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 comprehension_ty o = (comprehension_ty)_o;
3737 PyObject *result = NULL, *value = NULL;
3738 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003739 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003740 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003741
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003742 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3743 if (!result) return NULL;
3744 value = ast2obj_expr(o->target);
3745 if (!value) goto failed;
3746 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3747 goto failed;
3748 Py_DECREF(value);
3749 value = ast2obj_expr(o->iter);
3750 if (!value) goto failed;
3751 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3752 goto failed;
3753 Py_DECREF(value);
3754 value = ast2obj_list(o->ifs, ast2obj_expr);
3755 if (!value) goto failed;
3756 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3757 goto failed;
3758 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003759 value = ast2obj_int(o->is_async);
3760 if (!value) goto failed;
3761 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3762 goto failed;
3763 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003764 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003765failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003766 Py_XDECREF(value);
3767 Py_XDECREF(result);
3768 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769}
3770
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003771PyObject*
3772ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003774 excepthandler_ty o = (excepthandler_ty)_o;
3775 PyObject *result = NULL, *value = NULL;
3776 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003777 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003778 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003779
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003780 switch (o->kind) {
3781 case ExceptHandler_kind:
3782 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3783 if (!result) goto failed;
3784 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003785 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003786 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3787 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003788 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003789 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003790 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003791 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3792 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003793 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003794 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3795 if (!value) goto failed;
3796 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3797 goto failed;
3798 Py_DECREF(value);
3799 break;
3800 }
3801 value = ast2obj_int(o->lineno);
3802 if (!value) goto failed;
3803 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3804 goto failed;
3805 Py_DECREF(value);
3806 value = ast2obj_int(o->col_offset);
3807 if (!value) goto failed;
3808 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3809 goto failed;
3810 Py_DECREF(value);
3811 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003812failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003813 Py_XDECREF(value);
3814 Py_XDECREF(result);
3815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816}
3817
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003818PyObject*
3819ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003821 arguments_ty o = (arguments_ty)_o;
3822 PyObject *result = NULL, *value = NULL;
3823 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003824 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003825 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003826
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003827 result = PyType_GenericNew(arguments_type, NULL, NULL);
3828 if (!result) return NULL;
3829 value = ast2obj_list(o->args, ast2obj_arg);
3830 if (!value) goto failed;
3831 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3832 goto failed;
3833 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003834 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003835 if (!value) goto failed;
3836 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3837 goto failed;
3838 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003839 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3840 if (!value) goto failed;
3841 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3842 goto failed;
3843 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003844 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003845 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003846 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003847 goto failed;
3848 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003849 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003850 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003851 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003852 goto failed;
3853 Py_DECREF(value);
3854 value = ast2obj_list(o->defaults, ast2obj_expr);
3855 if (!value) goto failed;
3856 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3857 goto failed;
3858 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003859 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003860failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003861 Py_XDECREF(value);
3862 Py_XDECREF(result);
3863 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864}
3865
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003866PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003867ast2obj_arg(void* _o)
3868{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003869 arg_ty o = (arg_ty)_o;
3870 PyObject *result = NULL, *value = NULL;
3871 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003872 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003873 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003874
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003875 result = PyType_GenericNew(arg_type, NULL, NULL);
3876 if (!result) return NULL;
3877 value = ast2obj_identifier(o->arg);
3878 if (!value) goto failed;
3879 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3880 goto failed;
3881 Py_DECREF(value);
3882 value = ast2obj_expr(o->annotation);
3883 if (!value) goto failed;
3884 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3885 goto failed;
3886 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003887 value = ast2obj_int(o->lineno);
3888 if (!value) goto failed;
3889 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3890 goto failed;
3891 Py_DECREF(value);
3892 value = ast2obj_int(o->col_offset);
3893 if (!value) goto failed;
3894 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3895 goto failed;
3896 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003897 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003898failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003899 Py_XDECREF(value);
3900 Py_XDECREF(result);
3901 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003902}
3903
3904PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003905ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003907 keyword_ty o = (keyword_ty)_o;
3908 PyObject *result = NULL, *value = NULL;
3909 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003910 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003911 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003912
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003913 result = PyType_GenericNew(keyword_type, NULL, NULL);
3914 if (!result) return NULL;
3915 value = ast2obj_identifier(o->arg);
3916 if (!value) goto failed;
3917 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3918 goto failed;
3919 Py_DECREF(value);
3920 value = ast2obj_expr(o->value);
3921 if (!value) goto failed;
3922 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3923 goto failed;
3924 Py_DECREF(value);
3925 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003926failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003927 Py_XDECREF(value);
3928 Py_XDECREF(result);
3929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930}
3931
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003932PyObject*
3933ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003935 alias_ty o = (alias_ty)_o;
3936 PyObject *result = NULL, *value = NULL;
3937 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003938 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003939 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003940
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 result = PyType_GenericNew(alias_type, NULL, NULL);
3942 if (!result) return NULL;
3943 value = ast2obj_identifier(o->name);
3944 if (!value) goto failed;
3945 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3946 goto failed;
3947 Py_DECREF(value);
3948 value = ast2obj_identifier(o->asname);
3949 if (!value) goto failed;
3950 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3951 goto failed;
3952 Py_DECREF(value);
3953 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003954failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003955 Py_XDECREF(value);
3956 Py_XDECREF(result);
3957 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958}
3959
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003960PyObject*
3961ast2obj_withitem(void* _o)
3962{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003963 withitem_ty o = (withitem_ty)_o;
3964 PyObject *result = NULL, *value = NULL;
3965 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003966 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003967 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003968
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003969 result = PyType_GenericNew(withitem_type, NULL, NULL);
3970 if (!result) return NULL;
3971 value = ast2obj_expr(o->context_expr);
3972 if (!value) goto failed;
3973 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3974 goto failed;
3975 Py_DECREF(value);
3976 value = ast2obj_expr(o->optional_vars);
3977 if (!value) goto failed;
3978 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3979 goto failed;
3980 Py_DECREF(value);
3981 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003982failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003983 Py_XDECREF(value);
3984 Py_XDECREF(result);
3985 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003986}
3987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003989int
3990obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3991{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003992 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003993
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003995
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003996 if (obj == Py_None) {
3997 *out = NULL;
3998 return 0;
3999 }
4000 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
4001 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004002 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004003 }
4004 if (isinstance) {
4005 asdl_seq* body;
INADA Naokicb41b272017-02-23 00:31:59 +09004006 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004007
4008 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4009 int res;
4010 Py_ssize_t len;
4011 Py_ssize_t i;
4012 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4013 if (tmp == NULL) goto failed;
4014 if (!PyList_Check(tmp)) {
4015 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4016 goto failed;
4017 }
4018 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004019 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004020 if (body == NULL) goto failed;
4021 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004022 stmt_ty val;
4023 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004024 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004025 if (len != PyList_GET_SIZE(tmp)) {
4026 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4027 goto failed;
4028 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004029 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004030 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004031 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004032 } else {
4033 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4034 return 1;
4035 }
INADA Naokicb41b272017-02-23 00:31:59 +09004036 if (exists_not_none(obj, &PyId_docstring)) {
4037 int res;
4038 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4039 if (tmp == NULL) goto failed;
4040 res = obj2ast_string(tmp, &docstring, arena);
4041 if (res != 0) goto failed;
4042 Py_CLEAR(tmp);
4043 } else {
4044 docstring = NULL;
4045 }
4046 *out = Module(body, docstring, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004047 if (*out == NULL) goto failed;
4048 return 0;
4049 }
4050 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4051 if (isinstance == -1) {
4052 return 1;
4053 }
4054 if (isinstance) {
4055 asdl_seq* body;
4056
4057 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4058 int res;
4059 Py_ssize_t len;
4060 Py_ssize_t i;
4061 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4062 if (tmp == NULL) goto failed;
4063 if (!PyList_Check(tmp)) {
4064 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4065 goto failed;
4066 }
4067 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004068 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004069 if (body == NULL) goto failed;
4070 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004071 stmt_ty val;
4072 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004073 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004074 if (len != PyList_GET_SIZE(tmp)) {
4075 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4076 goto failed;
4077 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004078 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004079 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004080 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004081 } else {
4082 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4083 return 1;
4084 }
4085 *out = Interactive(body, arena);
4086 if (*out == NULL) goto failed;
4087 return 0;
4088 }
4089 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4090 if (isinstance == -1) {
4091 return 1;
4092 }
4093 if (isinstance) {
4094 expr_ty body;
4095
4096 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4097 int res;
4098 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4099 if (tmp == NULL) goto failed;
4100 res = obj2ast_expr(tmp, &body, arena);
4101 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004102 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004103 } else {
4104 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4105 return 1;
4106 }
4107 *out = Expression(body, arena);
4108 if (*out == NULL) goto failed;
4109 return 0;
4110 }
4111 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4112 if (isinstance == -1) {
4113 return 1;
4114 }
4115 if (isinstance) {
4116 asdl_seq* body;
4117
4118 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4119 int res;
4120 Py_ssize_t len;
4121 Py_ssize_t i;
4122 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4123 if (tmp == NULL) goto failed;
4124 if (!PyList_Check(tmp)) {
4125 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4126 goto failed;
4127 }
4128 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004129 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004130 if (body == NULL) goto failed;
4131 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004132 stmt_ty val;
4133 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004134 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004135 if (len != PyList_GET_SIZE(tmp)) {
4136 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4137 goto failed;
4138 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004139 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004140 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004141 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004142 } else {
4143 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4144 return 1;
4145 }
4146 *out = Suite(body, arena);
4147 if (*out == NULL) goto failed;
4148 return 0;
4149 }
4150
4151 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4152 failed:
4153 Py_XDECREF(tmp);
4154 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004155}
4156
4157int
4158obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4159{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004160 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004161
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004162 PyObject *tmp = NULL;
4163 int lineno;
4164 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004165
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004166 if (obj == Py_None) {
4167 *out = NULL;
4168 return 0;
4169 }
4170 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4171 int res;
4172 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4173 if (tmp == NULL) goto failed;
4174 res = obj2ast_int(tmp, &lineno, arena);
4175 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004176 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004177 } else {
4178 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004179 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004180 }
4181 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4182 int res;
4183 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4184 if (tmp == NULL) goto failed;
4185 res = obj2ast_int(tmp, &col_offset, arena);
4186 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004187 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004188 } else {
4189 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4190 return 1;
4191 }
4192 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4193 if (isinstance == -1) {
4194 return 1;
4195 }
4196 if (isinstance) {
4197 identifier name;
4198 arguments_ty args;
4199 asdl_seq* body;
4200 asdl_seq* decorator_list;
4201 expr_ty returns;
INADA Naokicb41b272017-02-23 00:31:59 +09004202 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004203
4204 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4205 int res;
4206 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4207 if (tmp == NULL) goto failed;
4208 res = obj2ast_identifier(tmp, &name, arena);
4209 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004210 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004211 } else {
4212 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4213 return 1;
4214 }
4215 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4216 int res;
4217 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4218 if (tmp == NULL) goto failed;
4219 res = obj2ast_arguments(tmp, &args, arena);
4220 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004221 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004222 } else {
4223 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4224 return 1;
4225 }
4226 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4227 int res;
4228 Py_ssize_t len;
4229 Py_ssize_t i;
4230 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4231 if (tmp == NULL) goto failed;
4232 if (!PyList_Check(tmp)) {
4233 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4234 goto failed;
4235 }
4236 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004237 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004238 if (body == NULL) goto failed;
4239 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004240 stmt_ty val;
4241 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004242 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004243 if (len != PyList_GET_SIZE(tmp)) {
4244 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4245 goto failed;
4246 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004247 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004248 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004249 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004250 } else {
4251 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4252 return 1;
4253 }
4254 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4255 int res;
4256 Py_ssize_t len;
4257 Py_ssize_t i;
4258 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4259 if (tmp == NULL) goto failed;
4260 if (!PyList_Check(tmp)) {
4261 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4262 goto failed;
4263 }
4264 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004265 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004266 if (decorator_list == NULL) goto failed;
4267 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004268 expr_ty val;
4269 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004270 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004271 if (len != PyList_GET_SIZE(tmp)) {
4272 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4273 goto failed;
4274 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004275 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004276 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004277 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004278 } else {
4279 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4280 return 1;
4281 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004282 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004283 int res;
4284 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4285 if (tmp == NULL) goto failed;
4286 res = obj2ast_expr(tmp, &returns, arena);
4287 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004288 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004289 } else {
4290 returns = NULL;
4291 }
INADA Naokicb41b272017-02-23 00:31:59 +09004292 if (exists_not_none(obj, &PyId_docstring)) {
4293 int res;
4294 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4295 if (tmp == NULL) goto failed;
4296 res = obj2ast_string(tmp, &docstring, arena);
4297 if (res != 0) goto failed;
4298 Py_CLEAR(tmp);
4299 } else {
4300 docstring = NULL;
4301 }
4302 *out = FunctionDef(name, args, body, decorator_list, returns,
4303 docstring, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004304 if (*out == NULL) goto failed;
4305 return 0;
4306 }
Yury Selivanov75445082015-05-11 22:57:16 -04004307 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4308 if (isinstance == -1) {
4309 return 1;
4310 }
4311 if (isinstance) {
4312 identifier name;
4313 arguments_ty args;
4314 asdl_seq* body;
4315 asdl_seq* decorator_list;
4316 expr_ty returns;
INADA Naokicb41b272017-02-23 00:31:59 +09004317 string docstring;
Yury Selivanov75445082015-05-11 22:57:16 -04004318
4319 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4320 int res;
4321 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4322 if (tmp == NULL) goto failed;
4323 res = obj2ast_identifier(tmp, &name, arena);
4324 if (res != 0) goto failed;
4325 Py_CLEAR(tmp);
4326 } else {
4327 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4328 return 1;
4329 }
4330 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4331 int res;
4332 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4333 if (tmp == NULL) goto failed;
4334 res = obj2ast_arguments(tmp, &args, arena);
4335 if (res != 0) goto failed;
4336 Py_CLEAR(tmp);
4337 } else {
4338 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4339 return 1;
4340 }
4341 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4342 int res;
4343 Py_ssize_t len;
4344 Py_ssize_t i;
4345 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4346 if (tmp == NULL) goto failed;
4347 if (!PyList_Check(tmp)) {
4348 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4349 goto failed;
4350 }
4351 len = PyList_GET_SIZE(tmp);
4352 body = _Py_asdl_seq_new(len, arena);
4353 if (body == NULL) goto failed;
4354 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004355 stmt_ty val;
4356 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004357 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004358 if (len != PyList_GET_SIZE(tmp)) {
4359 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4360 goto failed;
4361 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004362 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004363 }
4364 Py_CLEAR(tmp);
4365 } else {
4366 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4367 return 1;
4368 }
4369 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4370 int res;
4371 Py_ssize_t len;
4372 Py_ssize_t i;
4373 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4374 if (tmp == NULL) goto failed;
4375 if (!PyList_Check(tmp)) {
4376 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4377 goto failed;
4378 }
4379 len = PyList_GET_SIZE(tmp);
4380 decorator_list = _Py_asdl_seq_new(len, arena);
4381 if (decorator_list == NULL) goto failed;
4382 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004383 expr_ty val;
4384 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004385 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004386 if (len != PyList_GET_SIZE(tmp)) {
4387 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4388 goto failed;
4389 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004390 asdl_seq_SET(decorator_list, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004391 }
4392 Py_CLEAR(tmp);
4393 } else {
4394 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4395 return 1;
4396 }
4397 if (exists_not_none(obj, &PyId_returns)) {
4398 int res;
4399 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4400 if (tmp == NULL) goto failed;
4401 res = obj2ast_expr(tmp, &returns, arena);
4402 if (res != 0) goto failed;
4403 Py_CLEAR(tmp);
4404 } else {
4405 returns = NULL;
4406 }
INADA Naokicb41b272017-02-23 00:31:59 +09004407 if (exists_not_none(obj, &PyId_docstring)) {
4408 int res;
4409 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4410 if (tmp == NULL) goto failed;
4411 res = obj2ast_string(tmp, &docstring, arena);
4412 if (res != 0) goto failed;
4413 Py_CLEAR(tmp);
4414 } else {
4415 docstring = NULL;
4416 }
Yury Selivanov75445082015-05-11 22:57:16 -04004417 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09004418 docstring, lineno, col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004419 if (*out == NULL) goto failed;
4420 return 0;
4421 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004422 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4423 if (isinstance == -1) {
4424 return 1;
4425 }
4426 if (isinstance) {
4427 identifier name;
4428 asdl_seq* bases;
4429 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004430 asdl_seq* body;
4431 asdl_seq* decorator_list;
INADA Naokicb41b272017-02-23 00:31:59 +09004432 string docstring;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004433
4434 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4435 int res;
4436 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4437 if (tmp == NULL) goto failed;
4438 res = obj2ast_identifier(tmp, &name, arena);
4439 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004440 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004441 } else {
4442 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4443 return 1;
4444 }
4445 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4446 int res;
4447 Py_ssize_t len;
4448 Py_ssize_t i;
4449 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4450 if (tmp == NULL) goto failed;
4451 if (!PyList_Check(tmp)) {
4452 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4453 goto failed;
4454 }
4455 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004456 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004457 if (bases == NULL) goto failed;
4458 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004459 expr_ty val;
4460 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004461 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004462 if (len != PyList_GET_SIZE(tmp)) {
4463 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4464 goto failed;
4465 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004466 asdl_seq_SET(bases, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004468 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004469 } else {
4470 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4471 return 1;
4472 }
4473 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4474 int res;
4475 Py_ssize_t len;
4476 Py_ssize_t i;
4477 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4478 if (tmp == NULL) goto failed;
4479 if (!PyList_Check(tmp)) {
4480 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4481 goto failed;
4482 }
4483 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004484 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004485 if (keywords == NULL) goto failed;
4486 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004487 keyword_ty val;
4488 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004489 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004490 if (len != PyList_GET_SIZE(tmp)) {
4491 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4492 goto failed;
4493 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004494 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004495 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004496 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004497 } else {
4498 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4499 return 1;
4500 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004501 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4502 int res;
4503 Py_ssize_t len;
4504 Py_ssize_t i;
4505 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4506 if (tmp == NULL) goto failed;
4507 if (!PyList_Check(tmp)) {
4508 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4509 goto failed;
4510 }
4511 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004512 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004513 if (body == NULL) goto failed;
4514 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004515 stmt_ty val;
4516 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004517 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004518 if (len != PyList_GET_SIZE(tmp)) {
4519 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4520 goto failed;
4521 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004522 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004523 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004524 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004525 } else {
4526 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4527 return 1;
4528 }
4529 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4530 int res;
4531 Py_ssize_t len;
4532 Py_ssize_t i;
4533 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4534 if (tmp == NULL) goto failed;
4535 if (!PyList_Check(tmp)) {
4536 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4537 goto failed;
4538 }
4539 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004540 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004541 if (decorator_list == NULL) goto failed;
4542 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004543 expr_ty val;
4544 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004545 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004546 if (len != PyList_GET_SIZE(tmp)) {
4547 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4548 goto failed;
4549 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004550 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004551 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004552 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004553 } else {
4554 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4555 return 1;
4556 }
INADA Naokicb41b272017-02-23 00:31:59 +09004557 if (exists_not_none(obj, &PyId_docstring)) {
4558 int res;
4559 tmp = _PyObject_GetAttrId(obj, &PyId_docstring);
4560 if (tmp == NULL) goto failed;
4561 res = obj2ast_string(tmp, &docstring, arena);
4562 if (res != 0) goto failed;
4563 Py_CLEAR(tmp);
4564 } else {
4565 docstring = NULL;
4566 }
4567 *out = ClassDef(name, bases, keywords, body, decorator_list, docstring,
4568 lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004569 if (*out == NULL) goto failed;
4570 return 0;
4571 }
4572 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4573 if (isinstance == -1) {
4574 return 1;
4575 }
4576 if (isinstance) {
4577 expr_ty value;
4578
Victor Stinneree4b59c2013-07-27 00:01:35 +02004579 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004580 int res;
4581 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4582 if (tmp == NULL) goto failed;
4583 res = obj2ast_expr(tmp, &value, arena);
4584 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004586 } else {
4587 value = NULL;
4588 }
4589 *out = Return(value, lineno, col_offset, arena);
4590 if (*out == NULL) goto failed;
4591 return 0;
4592 }
4593 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4594 if (isinstance == -1) {
4595 return 1;
4596 }
4597 if (isinstance) {
4598 asdl_seq* targets;
4599
4600 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4601 int res;
4602 Py_ssize_t len;
4603 Py_ssize_t i;
4604 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4605 if (tmp == NULL) goto failed;
4606 if (!PyList_Check(tmp)) {
4607 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4608 goto failed;
4609 }
4610 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004611 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004612 if (targets == NULL) goto failed;
4613 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004614 expr_ty val;
4615 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004616 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004617 if (len != PyList_GET_SIZE(tmp)) {
4618 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4619 goto failed;
4620 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004621 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004622 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004623 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004624 } else {
4625 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4626 return 1;
4627 }
4628 *out = Delete(targets, lineno, col_offset, arena);
4629 if (*out == NULL) goto failed;
4630 return 0;
4631 }
4632 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4633 if (isinstance == -1) {
4634 return 1;
4635 }
4636 if (isinstance) {
4637 asdl_seq* targets;
4638 expr_ty value;
4639
4640 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4641 int res;
4642 Py_ssize_t len;
4643 Py_ssize_t i;
4644 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4645 if (tmp == NULL) goto failed;
4646 if (!PyList_Check(tmp)) {
4647 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4648 goto failed;
4649 }
4650 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004651 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004652 if (targets == NULL) goto failed;
4653 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004654 expr_ty val;
4655 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004656 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004657 if (len != PyList_GET_SIZE(tmp)) {
4658 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4659 goto failed;
4660 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004661 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004662 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004663 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004664 } else {
4665 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4666 return 1;
4667 }
4668 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4669 int res;
4670 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4671 if (tmp == NULL) goto failed;
4672 res = obj2ast_expr(tmp, &value, arena);
4673 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004674 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004675 } else {
4676 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4677 return 1;
4678 }
4679 *out = Assign(targets, value, lineno, col_offset, arena);
4680 if (*out == NULL) goto failed;
4681 return 0;
4682 }
4683 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4684 if (isinstance == -1) {
4685 return 1;
4686 }
4687 if (isinstance) {
4688 expr_ty target;
4689 operator_ty op;
4690 expr_ty value;
4691
4692 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4693 int res;
4694 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4695 if (tmp == NULL) goto failed;
4696 res = obj2ast_expr(tmp, &target, arena);
4697 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004698 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004699 } else {
4700 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4701 return 1;
4702 }
4703 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4704 int res;
4705 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4706 if (tmp == NULL) goto failed;
4707 res = obj2ast_operator(tmp, &op, arena);
4708 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4712 return 1;
4713 }
4714 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4715 int res;
4716 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4717 if (tmp == NULL) goto failed;
4718 res = obj2ast_expr(tmp, &value, arena);
4719 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004720 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004721 } else {
4722 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4723 return 1;
4724 }
4725 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4726 if (*out == NULL) goto failed;
4727 return 0;
4728 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07004729 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
4730 if (isinstance == -1) {
4731 return 1;
4732 }
4733 if (isinstance) {
4734 expr_ty target;
4735 expr_ty annotation;
4736 expr_ty value;
4737 int simple;
4738
4739 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4740 int res;
4741 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4742 if (tmp == NULL) goto failed;
4743 res = obj2ast_expr(tmp, &target, arena);
4744 if (res != 0) goto failed;
4745 Py_CLEAR(tmp);
4746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
4748 return 1;
4749 }
4750 if (_PyObject_HasAttrId(obj, &PyId_annotation)) {
4751 int res;
4752 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
4753 if (tmp == NULL) goto failed;
4754 res = obj2ast_expr(tmp, &annotation, arena);
4755 if (res != 0) goto failed;
4756 Py_CLEAR(tmp);
4757 } else {
4758 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
4759 return 1;
4760 }
4761 if (exists_not_none(obj, &PyId_value)) {
4762 int res;
4763 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4764 if (tmp == NULL) goto failed;
4765 res = obj2ast_expr(tmp, &value, arena);
4766 if (res != 0) goto failed;
4767 Py_CLEAR(tmp);
4768 } else {
4769 value = NULL;
4770 }
4771 if (_PyObject_HasAttrId(obj, &PyId_simple)) {
4772 int res;
4773 tmp = _PyObject_GetAttrId(obj, &PyId_simple);
4774 if (tmp == NULL) goto failed;
4775 res = obj2ast_int(tmp, &simple, arena);
4776 if (res != 0) goto failed;
4777 Py_CLEAR(tmp);
4778 } else {
4779 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
4780 return 1;
4781 }
4782 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
4783 arena);
4784 if (*out == NULL) goto failed;
4785 return 0;
4786 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004787 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4788 if (isinstance == -1) {
4789 return 1;
4790 }
4791 if (isinstance) {
4792 expr_ty target;
4793 expr_ty iter;
4794 asdl_seq* body;
4795 asdl_seq* orelse;
4796
4797 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4798 int res;
4799 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4800 if (tmp == NULL) goto failed;
4801 res = obj2ast_expr(tmp, &target, arena);
4802 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004803 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004804 } else {
4805 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4806 return 1;
4807 }
4808 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4809 int res;
4810 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4811 if (tmp == NULL) goto failed;
4812 res = obj2ast_expr(tmp, &iter, arena);
4813 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004814 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004815 } else {
4816 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4817 return 1;
4818 }
4819 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4820 int res;
4821 Py_ssize_t len;
4822 Py_ssize_t i;
4823 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4824 if (tmp == NULL) goto failed;
4825 if (!PyList_Check(tmp)) {
4826 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4827 goto failed;
4828 }
4829 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004830 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004831 if (body == NULL) goto failed;
4832 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004833 stmt_ty val;
4834 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004835 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004836 if (len != PyList_GET_SIZE(tmp)) {
4837 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4838 goto failed;
4839 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004840 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004841 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004842 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004843 } else {
4844 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4845 return 1;
4846 }
4847 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4848 int res;
4849 Py_ssize_t len;
4850 Py_ssize_t i;
4851 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4852 if (tmp == NULL) goto failed;
4853 if (!PyList_Check(tmp)) {
4854 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4855 goto failed;
4856 }
4857 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004858 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004859 if (orelse == NULL) goto failed;
4860 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004861 stmt_ty val;
4862 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004863 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004864 if (len != PyList_GET_SIZE(tmp)) {
4865 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4866 goto failed;
4867 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004868 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004869 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004870 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004871 } else {
4872 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4873 return 1;
4874 }
4875 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4876 if (*out == NULL) goto failed;
4877 return 0;
4878 }
Yury Selivanov75445082015-05-11 22:57:16 -04004879 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4880 if (isinstance == -1) {
4881 return 1;
4882 }
4883 if (isinstance) {
4884 expr_ty target;
4885 expr_ty iter;
4886 asdl_seq* body;
4887 asdl_seq* orelse;
4888
4889 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4890 int res;
4891 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4892 if (tmp == NULL) goto failed;
4893 res = obj2ast_expr(tmp, &target, arena);
4894 if (res != 0) goto failed;
4895 Py_CLEAR(tmp);
4896 } else {
4897 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4898 return 1;
4899 }
4900 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4901 int res;
4902 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4903 if (tmp == NULL) goto failed;
4904 res = obj2ast_expr(tmp, &iter, arena);
4905 if (res != 0) goto failed;
4906 Py_CLEAR(tmp);
4907 } else {
4908 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4909 return 1;
4910 }
4911 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4912 int res;
4913 Py_ssize_t len;
4914 Py_ssize_t i;
4915 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4916 if (tmp == NULL) goto failed;
4917 if (!PyList_Check(tmp)) {
4918 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4919 goto failed;
4920 }
4921 len = PyList_GET_SIZE(tmp);
4922 body = _Py_asdl_seq_new(len, arena);
4923 if (body == NULL) goto failed;
4924 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004925 stmt_ty val;
4926 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004927 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004928 if (len != PyList_GET_SIZE(tmp)) {
4929 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4930 goto failed;
4931 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004932 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004933 }
4934 Py_CLEAR(tmp);
4935 } else {
4936 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4937 return 1;
4938 }
4939 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4940 int res;
4941 Py_ssize_t len;
4942 Py_ssize_t i;
4943 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4944 if (tmp == NULL) goto failed;
4945 if (!PyList_Check(tmp)) {
4946 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4947 goto failed;
4948 }
4949 len = PyList_GET_SIZE(tmp);
4950 orelse = _Py_asdl_seq_new(len, arena);
4951 if (orelse == NULL) goto failed;
4952 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004953 stmt_ty val;
4954 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004955 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004956 if (len != PyList_GET_SIZE(tmp)) {
4957 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4958 goto failed;
4959 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004960 asdl_seq_SET(orelse, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004961 }
4962 Py_CLEAR(tmp);
4963 } else {
4964 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4965 return 1;
4966 }
4967 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4968 if (*out == NULL) goto failed;
4969 return 0;
4970 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004971 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4972 if (isinstance == -1) {
4973 return 1;
4974 }
4975 if (isinstance) {
4976 expr_ty test;
4977 asdl_seq* body;
4978 asdl_seq* orelse;
4979
4980 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4981 int res;
4982 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4983 if (tmp == NULL) goto failed;
4984 res = obj2ast_expr(tmp, &test, arena);
4985 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004986 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004987 } else {
4988 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4989 return 1;
4990 }
4991 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4992 int res;
4993 Py_ssize_t len;
4994 Py_ssize_t i;
4995 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4996 if (tmp == NULL) goto failed;
4997 if (!PyList_Check(tmp)) {
4998 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4999 goto failed;
5000 }
5001 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005002 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005003 if (body == NULL) goto failed;
5004 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005005 stmt_ty val;
5006 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005007 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005008 if (len != PyList_GET_SIZE(tmp)) {
5009 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
5010 goto failed;
5011 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005012 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005013 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005014 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005015 } else {
5016 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
5017 return 1;
5018 }
5019 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5020 int res;
5021 Py_ssize_t len;
5022 Py_ssize_t i;
5023 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5024 if (tmp == NULL) goto failed;
5025 if (!PyList_Check(tmp)) {
5026 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5027 goto failed;
5028 }
5029 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005030 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005031 if (orelse == NULL) goto failed;
5032 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005033 stmt_ty val;
5034 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005035 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005036 if (len != PyList_GET_SIZE(tmp)) {
5037 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
5038 goto failed;
5039 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005040 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005041 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005042 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005043 } else {
5044 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
5045 return 1;
5046 }
5047 *out = While(test, body, orelse, lineno, col_offset, arena);
5048 if (*out == NULL) goto failed;
5049 return 0;
5050 }
5051 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
5052 if (isinstance == -1) {
5053 return 1;
5054 }
5055 if (isinstance) {
5056 expr_ty test;
5057 asdl_seq* body;
5058 asdl_seq* orelse;
5059
5060 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5061 int res;
5062 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5063 if (tmp == NULL) goto failed;
5064 res = obj2ast_expr(tmp, &test, arena);
5065 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005066 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005067 } else {
5068 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5069 return 1;
5070 }
5071 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5072 int res;
5073 Py_ssize_t len;
5074 Py_ssize_t i;
5075 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5076 if (tmp == NULL) goto failed;
5077 if (!PyList_Check(tmp)) {
5078 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5079 goto failed;
5080 }
5081 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005082 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005083 if (body == NULL) goto failed;
5084 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005085 stmt_ty val;
5086 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005087 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005088 if (len != PyList_GET_SIZE(tmp)) {
5089 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5090 goto failed;
5091 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005092 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005093 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005094 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005095 } else {
5096 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5097 return 1;
5098 }
5099 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5100 int res;
5101 Py_ssize_t len;
5102 Py_ssize_t i;
5103 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5104 if (tmp == NULL) goto failed;
5105 if (!PyList_Check(tmp)) {
5106 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5107 goto failed;
5108 }
5109 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005110 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005111 if (orelse == NULL) goto failed;
5112 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005113 stmt_ty val;
5114 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005115 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005116 if (len != PyList_GET_SIZE(tmp)) {
5117 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5118 goto failed;
5119 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005120 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005121 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005122 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005123 } else {
5124 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5125 return 1;
5126 }
5127 *out = If(test, body, orelse, lineno, col_offset, arena);
5128 if (*out == NULL) goto failed;
5129 return 0;
5130 }
5131 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5132 if (isinstance == -1) {
5133 return 1;
5134 }
5135 if (isinstance) {
5136 asdl_seq* items;
5137 asdl_seq* body;
5138
5139 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5140 int res;
5141 Py_ssize_t len;
5142 Py_ssize_t i;
5143 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5144 if (tmp == NULL) goto failed;
5145 if (!PyList_Check(tmp)) {
5146 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5147 goto failed;
5148 }
5149 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005150 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005151 if (items == NULL) goto failed;
5152 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005153 withitem_ty val;
5154 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005155 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005156 if (len != PyList_GET_SIZE(tmp)) {
5157 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5158 goto failed;
5159 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005160 asdl_seq_SET(items, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005161 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005162 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005163 } else {
5164 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5165 return 1;
5166 }
5167 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5168 int res;
5169 Py_ssize_t len;
5170 Py_ssize_t i;
5171 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5172 if (tmp == NULL) goto failed;
5173 if (!PyList_Check(tmp)) {
5174 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5175 goto failed;
5176 }
5177 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005178 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005179 if (body == NULL) goto failed;
5180 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005181 stmt_ty val;
5182 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005183 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005184 if (len != PyList_GET_SIZE(tmp)) {
5185 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5186 goto failed;
5187 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005188 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005189 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005190 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005191 } else {
5192 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5193 return 1;
5194 }
5195 *out = With(items, body, lineno, col_offset, arena);
5196 if (*out == NULL) goto failed;
5197 return 0;
5198 }
Yury Selivanov75445082015-05-11 22:57:16 -04005199 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5200 if (isinstance == -1) {
5201 return 1;
5202 }
5203 if (isinstance) {
5204 asdl_seq* items;
5205 asdl_seq* body;
5206
5207 if (_PyObject_HasAttrId(obj, &PyId_items)) {
5208 int res;
5209 Py_ssize_t len;
5210 Py_ssize_t i;
5211 tmp = _PyObject_GetAttrId(obj, &PyId_items);
5212 if (tmp == NULL) goto failed;
5213 if (!PyList_Check(tmp)) {
5214 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5215 goto failed;
5216 }
5217 len = PyList_GET_SIZE(tmp);
5218 items = _Py_asdl_seq_new(len, arena);
5219 if (items == NULL) goto failed;
5220 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005221 withitem_ty val;
5222 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005223 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005224 if (len != PyList_GET_SIZE(tmp)) {
5225 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5226 goto failed;
5227 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005228 asdl_seq_SET(items, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005229 }
5230 Py_CLEAR(tmp);
5231 } else {
5232 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5233 return 1;
5234 }
5235 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5236 int res;
5237 Py_ssize_t len;
5238 Py_ssize_t i;
5239 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5240 if (tmp == NULL) goto failed;
5241 if (!PyList_Check(tmp)) {
5242 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5243 goto failed;
5244 }
5245 len = PyList_GET_SIZE(tmp);
5246 body = _Py_asdl_seq_new(len, arena);
5247 if (body == NULL) goto failed;
5248 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005249 stmt_ty val;
5250 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005251 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005252 if (len != PyList_GET_SIZE(tmp)) {
5253 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5254 goto failed;
5255 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005256 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005257 }
5258 Py_CLEAR(tmp);
5259 } else {
5260 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5261 return 1;
5262 }
5263 *out = AsyncWith(items, body, lineno, col_offset, arena);
5264 if (*out == NULL) goto failed;
5265 return 0;
5266 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005267 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5268 if (isinstance == -1) {
5269 return 1;
5270 }
5271 if (isinstance) {
5272 expr_ty exc;
5273 expr_ty cause;
5274
Victor Stinneree4b59c2013-07-27 00:01:35 +02005275 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005276 int res;
5277 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
5278 if (tmp == NULL) goto failed;
5279 res = obj2ast_expr(tmp, &exc, arena);
5280 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005281 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005282 } else {
5283 exc = NULL;
5284 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005285 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005286 int res;
5287 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
5288 if (tmp == NULL) goto failed;
5289 res = obj2ast_expr(tmp, &cause, arena);
5290 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005291 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005292 } else {
5293 cause = NULL;
5294 }
5295 *out = Raise(exc, cause, lineno, col_offset, arena);
5296 if (*out == NULL) goto failed;
5297 return 0;
5298 }
5299 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5300 if (isinstance == -1) {
5301 return 1;
5302 }
5303 if (isinstance) {
5304 asdl_seq* body;
5305 asdl_seq* handlers;
5306 asdl_seq* orelse;
5307 asdl_seq* finalbody;
5308
5309 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5310 int res;
5311 Py_ssize_t len;
5312 Py_ssize_t i;
5313 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5314 if (tmp == NULL) goto failed;
5315 if (!PyList_Check(tmp)) {
5316 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5317 goto failed;
5318 }
5319 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005320 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005321 if (body == NULL) goto failed;
5322 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005323 stmt_ty val;
5324 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005325 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005326 if (len != PyList_GET_SIZE(tmp)) {
5327 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5328 goto failed;
5329 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005330 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005331 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005332 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005333 } else {
5334 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5335 return 1;
5336 }
5337 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5338 int res;
5339 Py_ssize_t len;
5340 Py_ssize_t i;
5341 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5342 if (tmp == NULL) goto failed;
5343 if (!PyList_Check(tmp)) {
5344 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5345 goto failed;
5346 }
5347 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005348 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005349 if (handlers == NULL) goto failed;
5350 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005351 excepthandler_ty val;
5352 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005353 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005354 if (len != PyList_GET_SIZE(tmp)) {
5355 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5356 goto failed;
5357 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005358 asdl_seq_SET(handlers, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005359 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005360 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005361 } else {
5362 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5363 return 1;
5364 }
5365 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5366 int res;
5367 Py_ssize_t len;
5368 Py_ssize_t i;
5369 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5370 if (tmp == NULL) goto failed;
5371 if (!PyList_Check(tmp)) {
5372 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5373 goto failed;
5374 }
5375 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005376 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005377 if (orelse == NULL) goto failed;
5378 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005379 stmt_ty val;
5380 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005381 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005382 if (len != PyList_GET_SIZE(tmp)) {
5383 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5384 goto failed;
5385 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005386 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005387 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005388 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005389 } else {
5390 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5391 return 1;
5392 }
5393 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5394 int res;
5395 Py_ssize_t len;
5396 Py_ssize_t i;
5397 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5398 if (tmp == NULL) goto failed;
5399 if (!PyList_Check(tmp)) {
5400 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5401 goto failed;
5402 }
5403 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005404 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005405 if (finalbody == NULL) goto failed;
5406 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005407 stmt_ty val;
5408 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005409 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005410 if (len != PyList_GET_SIZE(tmp)) {
5411 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5412 goto failed;
5413 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005414 asdl_seq_SET(finalbody, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005415 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005416 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005417 } else {
5418 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5419 return 1;
5420 }
5421 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5422 arena);
5423 if (*out == NULL) goto failed;
5424 return 0;
5425 }
5426 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5427 if (isinstance == -1) {
5428 return 1;
5429 }
5430 if (isinstance) {
5431 expr_ty test;
5432 expr_ty msg;
5433
5434 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5435 int res;
5436 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_expr(tmp, &test, arena);
5439 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005440 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005441 } else {
5442 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5443 return 1;
5444 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005445 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005446 int res;
5447 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5448 if (tmp == NULL) goto failed;
5449 res = obj2ast_expr(tmp, &msg, arena);
5450 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005451 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005452 } else {
5453 msg = NULL;
5454 }
5455 *out = Assert(test, msg, lineno, col_offset, arena);
5456 if (*out == NULL) goto failed;
5457 return 0;
5458 }
5459 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5460 if (isinstance == -1) {
5461 return 1;
5462 }
5463 if (isinstance) {
5464 asdl_seq* names;
5465
5466 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5467 int res;
5468 Py_ssize_t len;
5469 Py_ssize_t i;
5470 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5471 if (tmp == NULL) goto failed;
5472 if (!PyList_Check(tmp)) {
5473 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5474 goto failed;
5475 }
5476 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005477 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005478 if (names == NULL) goto failed;
5479 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005480 alias_ty val;
5481 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005482 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005483 if (len != PyList_GET_SIZE(tmp)) {
5484 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5485 goto failed;
5486 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005487 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005488 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005489 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005490 } else {
5491 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5492 return 1;
5493 }
5494 *out = Import(names, lineno, col_offset, arena);
5495 if (*out == NULL) goto failed;
5496 return 0;
5497 }
5498 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5499 if (isinstance == -1) {
5500 return 1;
5501 }
5502 if (isinstance) {
5503 identifier module;
5504 asdl_seq* names;
5505 int level;
5506
Victor Stinneree4b59c2013-07-27 00:01:35 +02005507 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005508 int res;
5509 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5510 if (tmp == NULL) goto failed;
5511 res = obj2ast_identifier(tmp, &module, arena);
5512 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005513 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005514 } else {
5515 module = NULL;
5516 }
5517 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5518 int res;
5519 Py_ssize_t len;
5520 Py_ssize_t i;
5521 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5522 if (tmp == NULL) goto failed;
5523 if (!PyList_Check(tmp)) {
5524 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5525 goto failed;
5526 }
5527 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005528 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005529 if (names == NULL) goto failed;
5530 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005531 alias_ty val;
5532 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005533 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005534 if (len != PyList_GET_SIZE(tmp)) {
5535 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5536 goto failed;
5537 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005538 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005539 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005540 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005541 } else {
5542 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5543 return 1;
5544 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005545 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005546 int res;
5547 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5548 if (tmp == NULL) goto failed;
5549 res = obj2ast_int(tmp, &level, arena);
5550 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005551 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005552 } else {
5553 level = 0;
5554 }
5555 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5556 if (*out == NULL) goto failed;
5557 return 0;
5558 }
5559 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5560 if (isinstance == -1) {
5561 return 1;
5562 }
5563 if (isinstance) {
5564 asdl_seq* names;
5565
5566 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5567 int res;
5568 Py_ssize_t len;
5569 Py_ssize_t i;
5570 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5571 if (tmp == NULL) goto failed;
5572 if (!PyList_Check(tmp)) {
5573 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5574 goto failed;
5575 }
5576 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005577 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005578 if (names == NULL) goto failed;
5579 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005580 identifier val;
5581 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005582 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005583 if (len != PyList_GET_SIZE(tmp)) {
5584 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5585 goto failed;
5586 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005587 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005588 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005590 } else {
5591 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5592 return 1;
5593 }
5594 *out = Global(names, lineno, col_offset, arena);
5595 if (*out == NULL) goto failed;
5596 return 0;
5597 }
5598 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5599 if (isinstance == -1) {
5600 return 1;
5601 }
5602 if (isinstance) {
5603 asdl_seq* names;
5604
5605 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5606 int res;
5607 Py_ssize_t len;
5608 Py_ssize_t i;
5609 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5610 if (tmp == NULL) goto failed;
5611 if (!PyList_Check(tmp)) {
5612 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5613 goto failed;
5614 }
5615 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005616 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005617 if (names == NULL) goto failed;
5618 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005619 identifier val;
5620 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005621 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005622 if (len != PyList_GET_SIZE(tmp)) {
5623 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5624 goto failed;
5625 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005626 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005627 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005628 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005629 } else {
5630 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5631 return 1;
5632 }
5633 *out = Nonlocal(names, lineno, col_offset, arena);
5634 if (*out == NULL) goto failed;
5635 return 0;
5636 }
5637 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5638 if (isinstance == -1) {
5639 return 1;
5640 }
5641 if (isinstance) {
5642 expr_ty value;
5643
5644 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5645 int res;
5646 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5647 if (tmp == NULL) goto failed;
5648 res = obj2ast_expr(tmp, &value, arena);
5649 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005650 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005651 } else {
5652 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5653 return 1;
5654 }
5655 *out = Expr(value, lineno, col_offset, arena);
5656 if (*out == NULL) goto failed;
5657 return 0;
5658 }
5659 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5660 if (isinstance == -1) {
5661 return 1;
5662 }
5663 if (isinstance) {
5664
5665 *out = Pass(lineno, col_offset, arena);
5666 if (*out == NULL) goto failed;
5667 return 0;
5668 }
5669 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5670 if (isinstance == -1) {
5671 return 1;
5672 }
5673 if (isinstance) {
5674
5675 *out = Break(lineno, col_offset, arena);
5676 if (*out == NULL) goto failed;
5677 return 0;
5678 }
5679 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5680 if (isinstance == -1) {
5681 return 1;
5682 }
5683 if (isinstance) {
5684
5685 *out = Continue(lineno, col_offset, arena);
5686 if (*out == NULL) goto failed;
5687 return 0;
5688 }
5689
5690 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5691 failed:
5692 Py_XDECREF(tmp);
5693 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005694}
5695
5696int
5697obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5698{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005699 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005700
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005701 PyObject *tmp = NULL;
5702 int lineno;
5703 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005704
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005705 if (obj == Py_None) {
5706 *out = NULL;
5707 return 0;
5708 }
5709 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5710 int res;
5711 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5712 if (tmp == NULL) goto failed;
5713 res = obj2ast_int(tmp, &lineno, arena);
5714 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005715 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005716 } else {
5717 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005718 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005719 }
5720 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5721 int res;
5722 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5723 if (tmp == NULL) goto failed;
5724 res = obj2ast_int(tmp, &col_offset, arena);
5725 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005726 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005727 } else {
5728 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5729 return 1;
5730 }
5731 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5732 if (isinstance == -1) {
5733 return 1;
5734 }
5735 if (isinstance) {
5736 boolop_ty op;
5737 asdl_seq* values;
5738
5739 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5740 int res;
5741 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5742 if (tmp == NULL) goto failed;
5743 res = obj2ast_boolop(tmp, &op, arena);
5744 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005745 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005746 } else {
5747 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5748 return 1;
5749 }
5750 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5751 int res;
5752 Py_ssize_t len;
5753 Py_ssize_t i;
5754 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5755 if (tmp == NULL) goto failed;
5756 if (!PyList_Check(tmp)) {
5757 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5758 goto failed;
5759 }
5760 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005761 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005762 if (values == NULL) goto failed;
5763 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005764 expr_ty val;
5765 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005766 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005767 if (len != PyList_GET_SIZE(tmp)) {
5768 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5769 goto failed;
5770 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005771 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005772 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005773 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005774 } else {
5775 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5776 return 1;
5777 }
5778 *out = BoolOp(op, values, lineno, col_offset, arena);
5779 if (*out == NULL) goto failed;
5780 return 0;
5781 }
5782 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5783 if (isinstance == -1) {
5784 return 1;
5785 }
5786 if (isinstance) {
5787 expr_ty left;
5788 operator_ty op;
5789 expr_ty right;
5790
5791 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5792 int res;
5793 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5794 if (tmp == NULL) goto failed;
5795 res = obj2ast_expr(tmp, &left, arena);
5796 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005798 } else {
5799 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5800 return 1;
5801 }
5802 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5803 int res;
5804 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5805 if (tmp == NULL) goto failed;
5806 res = obj2ast_operator(tmp, &op, arena);
5807 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005808 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005809 } else {
5810 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5811 return 1;
5812 }
5813 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5814 int res;
5815 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5816 if (tmp == NULL) goto failed;
5817 res = obj2ast_expr(tmp, &right, arena);
5818 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005819 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005820 } else {
5821 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5822 return 1;
5823 }
5824 *out = BinOp(left, op, right, lineno, col_offset, arena);
5825 if (*out == NULL) goto failed;
5826 return 0;
5827 }
5828 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5829 if (isinstance == -1) {
5830 return 1;
5831 }
5832 if (isinstance) {
5833 unaryop_ty op;
5834 expr_ty operand;
5835
5836 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5837 int res;
5838 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5839 if (tmp == NULL) goto failed;
5840 res = obj2ast_unaryop(tmp, &op, arena);
5841 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005842 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005843 } else {
5844 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5845 return 1;
5846 }
5847 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5848 int res;
5849 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5850 if (tmp == NULL) goto failed;
5851 res = obj2ast_expr(tmp, &operand, arena);
5852 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005853 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005854 } else {
5855 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5856 return 1;
5857 }
5858 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5859 if (*out == NULL) goto failed;
5860 return 0;
5861 }
5862 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5863 if (isinstance == -1) {
5864 return 1;
5865 }
5866 if (isinstance) {
5867 arguments_ty args;
5868 expr_ty body;
5869
5870 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5871 int res;
5872 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5873 if (tmp == NULL) goto failed;
5874 res = obj2ast_arguments(tmp, &args, arena);
5875 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005876 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005877 } else {
5878 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5879 return 1;
5880 }
5881 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5882 int res;
5883 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5884 if (tmp == NULL) goto failed;
5885 res = obj2ast_expr(tmp, &body, arena);
5886 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005887 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005888 } else {
5889 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5890 return 1;
5891 }
5892 *out = Lambda(args, body, lineno, col_offset, arena);
5893 if (*out == NULL) goto failed;
5894 return 0;
5895 }
5896 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5897 if (isinstance == -1) {
5898 return 1;
5899 }
5900 if (isinstance) {
5901 expr_ty test;
5902 expr_ty body;
5903 expr_ty orelse;
5904
5905 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5906 int res;
5907 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5908 if (tmp == NULL) goto failed;
5909 res = obj2ast_expr(tmp, &test, arena);
5910 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005911 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005912 } else {
5913 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5914 return 1;
5915 }
5916 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5917 int res;
5918 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5919 if (tmp == NULL) goto failed;
5920 res = obj2ast_expr(tmp, &body, arena);
5921 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005922 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005923 } else {
5924 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5925 return 1;
5926 }
5927 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5928 int res;
5929 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5930 if (tmp == NULL) goto failed;
5931 res = obj2ast_expr(tmp, &orelse, arena);
5932 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005933 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005934 } else {
5935 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5936 return 1;
5937 }
5938 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5939 if (*out == NULL) goto failed;
5940 return 0;
5941 }
5942 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5943 if (isinstance == -1) {
5944 return 1;
5945 }
5946 if (isinstance) {
5947 asdl_seq* keys;
5948 asdl_seq* values;
5949
5950 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5951 int res;
5952 Py_ssize_t len;
5953 Py_ssize_t i;
5954 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5955 if (tmp == NULL) goto failed;
5956 if (!PyList_Check(tmp)) {
5957 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5958 goto failed;
5959 }
5960 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005961 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005962 if (keys == NULL) goto failed;
5963 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005964 expr_ty val;
5965 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005966 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005967 if (len != PyList_GET_SIZE(tmp)) {
5968 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5969 goto failed;
5970 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005971 asdl_seq_SET(keys, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005972 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005973 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005974 } else {
5975 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5976 return 1;
5977 }
5978 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5979 int res;
5980 Py_ssize_t len;
5981 Py_ssize_t i;
5982 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5983 if (tmp == NULL) goto failed;
5984 if (!PyList_Check(tmp)) {
5985 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5986 goto failed;
5987 }
5988 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005989 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005990 if (values == NULL) goto failed;
5991 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005992 expr_ty val;
5993 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005994 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005995 if (len != PyList_GET_SIZE(tmp)) {
5996 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5997 goto failed;
5998 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005999 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006000 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006001 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006002 } else {
6003 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
6004 return 1;
6005 }
6006 *out = Dict(keys, values, lineno, col_offset, arena);
6007 if (*out == NULL) goto failed;
6008 return 0;
6009 }
6010 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
6011 if (isinstance == -1) {
6012 return 1;
6013 }
6014 if (isinstance) {
6015 asdl_seq* elts;
6016
6017 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6018 int res;
6019 Py_ssize_t len;
6020 Py_ssize_t i;
6021 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6022 if (tmp == NULL) goto failed;
6023 if (!PyList_Check(tmp)) {
6024 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6025 goto failed;
6026 }
6027 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006028 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006029 if (elts == NULL) goto failed;
6030 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006031 expr_ty val;
6032 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006033 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006034 if (len != PyList_GET_SIZE(tmp)) {
6035 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
6036 goto failed;
6037 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006038 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006039 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006041 } else {
6042 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
6043 return 1;
6044 }
6045 *out = Set(elts, lineno, col_offset, arena);
6046 if (*out == NULL) goto failed;
6047 return 0;
6048 }
6049 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
6050 if (isinstance == -1) {
6051 return 1;
6052 }
6053 if (isinstance) {
6054 expr_ty elt;
6055 asdl_seq* generators;
6056
6057 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6058 int res;
6059 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6060 if (tmp == NULL) goto failed;
6061 res = obj2ast_expr(tmp, &elt, arena);
6062 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006063 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006064 } else {
6065 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
6066 return 1;
6067 }
6068 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6069 int res;
6070 Py_ssize_t len;
6071 Py_ssize_t i;
6072 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6073 if (tmp == NULL) goto failed;
6074 if (!PyList_Check(tmp)) {
6075 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6076 goto failed;
6077 }
6078 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006079 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006080 if (generators == NULL) goto failed;
6081 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006082 comprehension_ty val;
6083 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006084 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006085 if (len != PyList_GET_SIZE(tmp)) {
6086 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6087 goto failed;
6088 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006089 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006090 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006091 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006092 } else {
6093 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6094 return 1;
6095 }
6096 *out = ListComp(elt, generators, lineno, col_offset, arena);
6097 if (*out == NULL) goto failed;
6098 return 0;
6099 }
6100 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6101 if (isinstance == -1) {
6102 return 1;
6103 }
6104 if (isinstance) {
6105 expr_ty elt;
6106 asdl_seq* generators;
6107
6108 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6109 int res;
6110 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6111 if (tmp == NULL) goto failed;
6112 res = obj2ast_expr(tmp, &elt, arena);
6113 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006114 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006115 } else {
6116 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6117 return 1;
6118 }
6119 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6120 int res;
6121 Py_ssize_t len;
6122 Py_ssize_t i;
6123 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6124 if (tmp == NULL) goto failed;
6125 if (!PyList_Check(tmp)) {
6126 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6127 goto failed;
6128 }
6129 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006130 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006131 if (generators == NULL) goto failed;
6132 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006133 comprehension_ty val;
6134 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006135 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006136 if (len != PyList_GET_SIZE(tmp)) {
6137 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6138 goto failed;
6139 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006140 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006141 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006142 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006143 } else {
6144 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6145 return 1;
6146 }
6147 *out = SetComp(elt, generators, lineno, col_offset, arena);
6148 if (*out == NULL) goto failed;
6149 return 0;
6150 }
6151 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6152 if (isinstance == -1) {
6153 return 1;
6154 }
6155 if (isinstance) {
6156 expr_ty key;
6157 expr_ty value;
6158 asdl_seq* generators;
6159
6160 if (_PyObject_HasAttrId(obj, &PyId_key)) {
6161 int res;
6162 tmp = _PyObject_GetAttrId(obj, &PyId_key);
6163 if (tmp == NULL) goto failed;
6164 res = obj2ast_expr(tmp, &key, arena);
6165 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006166 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006167 } else {
6168 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6169 return 1;
6170 }
6171 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6172 int res;
6173 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6174 if (tmp == NULL) goto failed;
6175 res = obj2ast_expr(tmp, &value, arena);
6176 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006177 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006178 } else {
6179 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6180 return 1;
6181 }
6182 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6183 int res;
6184 Py_ssize_t len;
6185 Py_ssize_t i;
6186 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6187 if (tmp == NULL) goto failed;
6188 if (!PyList_Check(tmp)) {
6189 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6190 goto failed;
6191 }
6192 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006193 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006194 if (generators == NULL) goto failed;
6195 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006196 comprehension_ty val;
6197 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006198 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006199 if (len != PyList_GET_SIZE(tmp)) {
6200 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6201 goto failed;
6202 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006203 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006204 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006205 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006206 } else {
6207 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6208 return 1;
6209 }
6210 *out = DictComp(key, value, generators, lineno, col_offset, arena);
6211 if (*out == NULL) goto failed;
6212 return 0;
6213 }
6214 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6215 if (isinstance == -1) {
6216 return 1;
6217 }
6218 if (isinstance) {
6219 expr_ty elt;
6220 asdl_seq* generators;
6221
6222 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
6223 int res;
6224 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
6225 if (tmp == NULL) goto failed;
6226 res = obj2ast_expr(tmp, &elt, arena);
6227 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006228 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006229 } else {
6230 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6231 return 1;
6232 }
6233 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
6234 int res;
6235 Py_ssize_t len;
6236 Py_ssize_t i;
6237 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
6238 if (tmp == NULL) goto failed;
6239 if (!PyList_Check(tmp)) {
6240 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6241 goto failed;
6242 }
6243 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006244 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006245 if (generators == NULL) goto failed;
6246 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006247 comprehension_ty val;
6248 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006249 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006250 if (len != PyList_GET_SIZE(tmp)) {
6251 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6252 goto failed;
6253 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006254 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006255 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006256 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006257 } else {
6258 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6259 return 1;
6260 }
6261 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
6262 if (*out == NULL) goto failed;
6263 return 0;
6264 }
Yury Selivanov75445082015-05-11 22:57:16 -04006265 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6266 if (isinstance == -1) {
6267 return 1;
6268 }
6269 if (isinstance) {
6270 expr_ty value;
6271
6272 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6273 int res;
6274 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6275 if (tmp == NULL) goto failed;
6276 res = obj2ast_expr(tmp, &value, arena);
6277 if (res != 0) goto failed;
6278 Py_CLEAR(tmp);
6279 } else {
6280 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6281 return 1;
6282 }
6283 *out = Await(value, lineno, col_offset, arena);
6284 if (*out == NULL) goto failed;
6285 return 0;
6286 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006287 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6288 if (isinstance == -1) {
6289 return 1;
6290 }
6291 if (isinstance) {
6292 expr_ty value;
6293
Victor Stinneree4b59c2013-07-27 00:01:35 +02006294 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006295 int res;
6296 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6297 if (tmp == NULL) goto failed;
6298 res = obj2ast_expr(tmp, &value, arena);
6299 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006300 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006301 } else {
6302 value = NULL;
6303 }
6304 *out = Yield(value, lineno, col_offset, arena);
6305 if (*out == NULL) goto failed;
6306 return 0;
6307 }
6308 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6309 if (isinstance == -1) {
6310 return 1;
6311 }
6312 if (isinstance) {
6313 expr_ty value;
6314
6315 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6316 int res;
6317 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6318 if (tmp == NULL) goto failed;
6319 res = obj2ast_expr(tmp, &value, arena);
6320 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006321 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006322 } else {
6323 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6324 return 1;
6325 }
6326 *out = YieldFrom(value, lineno, col_offset, arena);
6327 if (*out == NULL) goto failed;
6328 return 0;
6329 }
6330 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6331 if (isinstance == -1) {
6332 return 1;
6333 }
6334 if (isinstance) {
6335 expr_ty left;
6336 asdl_int_seq* ops;
6337 asdl_seq* comparators;
6338
6339 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6340 int res;
6341 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6342 if (tmp == NULL) goto failed;
6343 res = obj2ast_expr(tmp, &left, arena);
6344 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006345 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006346 } else {
6347 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6348 return 1;
6349 }
6350 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6351 int res;
6352 Py_ssize_t len;
6353 Py_ssize_t i;
6354 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6355 if (tmp == NULL) goto failed;
6356 if (!PyList_Check(tmp)) {
6357 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6358 goto failed;
6359 }
6360 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006361 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006362 if (ops == NULL) goto failed;
6363 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006364 cmpop_ty val;
6365 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006366 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006367 if (len != PyList_GET_SIZE(tmp)) {
6368 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6369 goto failed;
6370 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006371 asdl_seq_SET(ops, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006372 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006373 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006374 } else {
6375 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6376 return 1;
6377 }
6378 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6379 int res;
6380 Py_ssize_t len;
6381 Py_ssize_t i;
6382 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6383 if (tmp == NULL) goto failed;
6384 if (!PyList_Check(tmp)) {
6385 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6386 goto failed;
6387 }
6388 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006389 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006390 if (comparators == NULL) goto failed;
6391 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006392 expr_ty val;
6393 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006394 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006395 if (len != PyList_GET_SIZE(tmp)) {
6396 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6397 goto failed;
6398 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006399 asdl_seq_SET(comparators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006400 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006401 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006402 } else {
6403 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6404 return 1;
6405 }
6406 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6407 if (*out == NULL) goto failed;
6408 return 0;
6409 }
6410 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6411 if (isinstance == -1) {
6412 return 1;
6413 }
6414 if (isinstance) {
6415 expr_ty func;
6416 asdl_seq* args;
6417 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006418
6419 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6420 int res;
6421 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6422 if (tmp == NULL) goto failed;
6423 res = obj2ast_expr(tmp, &func, arena);
6424 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006425 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006426 } else {
6427 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6428 return 1;
6429 }
6430 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6431 int res;
6432 Py_ssize_t len;
6433 Py_ssize_t i;
6434 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6435 if (tmp == NULL) goto failed;
6436 if (!PyList_Check(tmp)) {
6437 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6438 goto failed;
6439 }
6440 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006441 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006442 if (args == NULL) goto failed;
6443 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006444 expr_ty val;
6445 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006446 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006447 if (len != PyList_GET_SIZE(tmp)) {
6448 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6449 goto failed;
6450 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006451 asdl_seq_SET(args, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006452 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006453 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006454 } else {
6455 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6456 return 1;
6457 }
6458 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6459 int res;
6460 Py_ssize_t len;
6461 Py_ssize_t i;
6462 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6463 if (tmp == NULL) goto failed;
6464 if (!PyList_Check(tmp)) {
6465 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6466 goto failed;
6467 }
6468 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006469 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006470 if (keywords == NULL) goto failed;
6471 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006472 keyword_ty val;
6473 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006474 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006475 if (len != PyList_GET_SIZE(tmp)) {
6476 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6477 goto failed;
6478 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006479 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006480 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006481 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006482 } else {
6483 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6484 return 1;
6485 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006486 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006487 if (*out == NULL) goto failed;
6488 return 0;
6489 }
6490 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6491 if (isinstance == -1) {
6492 return 1;
6493 }
6494 if (isinstance) {
6495 object n;
6496
6497 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6498 int res;
6499 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6500 if (tmp == NULL) goto failed;
6501 res = obj2ast_object(tmp, &n, arena);
6502 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006503 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 } else {
6505 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6506 return 1;
6507 }
6508 *out = Num(n, lineno, col_offset, arena);
6509 if (*out == NULL) goto failed;
6510 return 0;
6511 }
6512 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6513 if (isinstance == -1) {
6514 return 1;
6515 }
6516 if (isinstance) {
6517 string s;
6518
6519 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6520 int res;
6521 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6522 if (tmp == NULL) goto failed;
6523 res = obj2ast_string(tmp, &s, arena);
6524 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006525 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006526 } else {
6527 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6528 return 1;
6529 }
6530 *out = Str(s, lineno, col_offset, arena);
6531 if (*out == NULL) goto failed;
6532 return 0;
6533 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006534 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6535 if (isinstance == -1) {
6536 return 1;
6537 }
6538 if (isinstance) {
6539 expr_ty value;
6540 int conversion;
6541 expr_ty format_spec;
6542
6543 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6544 int res;
6545 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6546 if (tmp == NULL) goto failed;
6547 res = obj2ast_expr(tmp, &value, arena);
6548 if (res != 0) goto failed;
6549 Py_CLEAR(tmp);
6550 } else {
6551 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6552 return 1;
6553 }
6554 if (exists_not_none(obj, &PyId_conversion)) {
6555 int res;
6556 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6557 if (tmp == NULL) goto failed;
6558 res = obj2ast_int(tmp, &conversion, arena);
6559 if (res != 0) goto failed;
6560 Py_CLEAR(tmp);
6561 } else {
6562 conversion = 0;
6563 }
6564 if (exists_not_none(obj, &PyId_format_spec)) {
6565 int res;
6566 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6567 if (tmp == NULL) goto failed;
6568 res = obj2ast_expr(tmp, &format_spec, arena);
6569 if (res != 0) goto failed;
6570 Py_CLEAR(tmp);
6571 } else {
6572 format_spec = NULL;
6573 }
6574 *out = FormattedValue(value, conversion, format_spec, lineno,
6575 col_offset, arena);
6576 if (*out == NULL) goto failed;
6577 return 0;
6578 }
6579 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6580 if (isinstance == -1) {
6581 return 1;
6582 }
6583 if (isinstance) {
6584 asdl_seq* values;
6585
6586 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6587 int res;
6588 Py_ssize_t len;
6589 Py_ssize_t i;
6590 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6591 if (tmp == NULL) goto failed;
6592 if (!PyList_Check(tmp)) {
6593 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6594 goto failed;
6595 }
6596 len = PyList_GET_SIZE(tmp);
6597 values = _Py_asdl_seq_new(len, arena);
6598 if (values == NULL) goto failed;
6599 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006600 expr_ty val;
6601 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04006602 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03006603 if (len != PyList_GET_SIZE(tmp)) {
6604 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
6605 goto failed;
6606 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006607 asdl_seq_SET(values, i, val);
Eric V. Smith235a6f02015-09-19 14:51:32 -04006608 }
6609 Py_CLEAR(tmp);
6610 } else {
6611 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6612 return 1;
6613 }
6614 *out = JoinedStr(values, lineno, col_offset, arena);
6615 if (*out == NULL) goto failed;
6616 return 0;
6617 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006618 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6619 if (isinstance == -1) {
6620 return 1;
6621 }
6622 if (isinstance) {
6623 bytes s;
6624
6625 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6626 int res;
6627 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6628 if (tmp == NULL) goto failed;
6629 res = obj2ast_bytes(tmp, &s, arena);
6630 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006631 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006632 } else {
6633 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6634 return 1;
6635 }
6636 *out = Bytes(s, lineno, col_offset, arena);
6637 if (*out == NULL) goto failed;
6638 return 0;
6639 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006640 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6641 if (isinstance == -1) {
6642 return 1;
6643 }
6644 if (isinstance) {
6645 singleton value;
6646
6647 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6648 int res;
6649 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6650 if (tmp == NULL) goto failed;
6651 res = obj2ast_singleton(tmp, &value, arena);
6652 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006653 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006654 } else {
6655 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6656 return 1;
6657 }
6658 *out = NameConstant(value, lineno, col_offset, arena);
6659 if (*out == NULL) goto failed;
6660 return 0;
6661 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6663 if (isinstance == -1) {
6664 return 1;
6665 }
6666 if (isinstance) {
6667
6668 *out = Ellipsis(lineno, col_offset, arena);
6669 if (*out == NULL) goto failed;
6670 return 0;
6671 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006672 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6673 if (isinstance == -1) {
6674 return 1;
6675 }
6676 if (isinstance) {
6677 constant value;
6678
6679 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6680 int res;
6681 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6682 if (tmp == NULL) goto failed;
6683 res = obj2ast_constant(tmp, &value, arena);
6684 if (res != 0) goto failed;
6685 Py_CLEAR(tmp);
6686 } else {
6687 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6688 return 1;
6689 }
6690 *out = Constant(value, lineno, col_offset, arena);
6691 if (*out == NULL) goto failed;
6692 return 0;
6693 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006694 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6695 if (isinstance == -1) {
6696 return 1;
6697 }
6698 if (isinstance) {
6699 expr_ty value;
6700 identifier attr;
6701 expr_context_ty ctx;
6702
6703 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6704 int res;
6705 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6706 if (tmp == NULL) goto failed;
6707 res = obj2ast_expr(tmp, &value, arena);
6708 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006710 } else {
6711 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6712 return 1;
6713 }
6714 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6715 int res;
6716 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6717 if (tmp == NULL) goto failed;
6718 res = obj2ast_identifier(tmp, &attr, arena);
6719 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006720 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006721 } else {
6722 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6723 return 1;
6724 }
6725 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6726 int res;
6727 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6728 if (tmp == NULL) goto failed;
6729 res = obj2ast_expr_context(tmp, &ctx, arena);
6730 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006731 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006732 } else {
6733 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6734 return 1;
6735 }
6736 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6737 if (*out == NULL) goto failed;
6738 return 0;
6739 }
6740 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6741 if (isinstance == -1) {
6742 return 1;
6743 }
6744 if (isinstance) {
6745 expr_ty value;
6746 slice_ty slice;
6747 expr_context_ty ctx;
6748
6749 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6750 int res;
6751 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6752 if (tmp == NULL) goto failed;
6753 res = obj2ast_expr(tmp, &value, arena);
6754 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006755 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006756 } else {
6757 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6758 return 1;
6759 }
6760 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6761 int res;
6762 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6763 if (tmp == NULL) goto failed;
6764 res = obj2ast_slice(tmp, &slice, arena);
6765 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006767 } else {
6768 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6769 return 1;
6770 }
6771 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6772 int res;
6773 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6774 if (tmp == NULL) goto failed;
6775 res = obj2ast_expr_context(tmp, &ctx, arena);
6776 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006777 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006778 } else {
6779 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6780 return 1;
6781 }
6782 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6783 if (*out == NULL) goto failed;
6784 return 0;
6785 }
6786 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6787 if (isinstance == -1) {
6788 return 1;
6789 }
6790 if (isinstance) {
6791 expr_ty value;
6792 expr_context_ty ctx;
6793
6794 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6795 int res;
6796 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6797 if (tmp == NULL) goto failed;
6798 res = obj2ast_expr(tmp, &value, arena);
6799 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006800 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006801 } else {
6802 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6803 return 1;
6804 }
6805 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6806 int res;
6807 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6808 if (tmp == NULL) goto failed;
6809 res = obj2ast_expr_context(tmp, &ctx, arena);
6810 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006811 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006812 } else {
6813 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6814 return 1;
6815 }
6816 *out = Starred(value, ctx, lineno, col_offset, arena);
6817 if (*out == NULL) goto failed;
6818 return 0;
6819 }
6820 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6821 if (isinstance == -1) {
6822 return 1;
6823 }
6824 if (isinstance) {
6825 identifier id;
6826 expr_context_ty ctx;
6827
6828 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6829 int res;
6830 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6831 if (tmp == NULL) goto failed;
6832 res = obj2ast_identifier(tmp, &id, arena);
6833 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006834 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006835 } else {
6836 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6837 return 1;
6838 }
6839 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6840 int res;
6841 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6842 if (tmp == NULL) goto failed;
6843 res = obj2ast_expr_context(tmp, &ctx, arena);
6844 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006845 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006846 } else {
6847 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6848 return 1;
6849 }
6850 *out = Name(id, ctx, lineno, col_offset, arena);
6851 if (*out == NULL) goto failed;
6852 return 0;
6853 }
6854 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6855 if (isinstance == -1) {
6856 return 1;
6857 }
6858 if (isinstance) {
6859 asdl_seq* elts;
6860 expr_context_ty ctx;
6861
6862 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6863 int res;
6864 Py_ssize_t len;
6865 Py_ssize_t i;
6866 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6867 if (tmp == NULL) goto failed;
6868 if (!PyList_Check(tmp)) {
6869 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6870 goto failed;
6871 }
6872 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006873 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006874 if (elts == NULL) goto failed;
6875 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006876 expr_ty val;
6877 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006878 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006879 if (len != PyList_GET_SIZE(tmp)) {
6880 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6881 goto failed;
6882 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006883 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006884 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006885 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006886 } else {
6887 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6888 return 1;
6889 }
6890 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6891 int res;
6892 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6893 if (tmp == NULL) goto failed;
6894 res = obj2ast_expr_context(tmp, &ctx, arena);
6895 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006896 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006897 } else {
6898 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6899 return 1;
6900 }
6901 *out = List(elts, ctx, lineno, col_offset, arena);
6902 if (*out == NULL) goto failed;
6903 return 0;
6904 }
6905 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6906 if (isinstance == -1) {
6907 return 1;
6908 }
6909 if (isinstance) {
6910 asdl_seq* elts;
6911 expr_context_ty ctx;
6912
6913 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6914 int res;
6915 Py_ssize_t len;
6916 Py_ssize_t i;
6917 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6918 if (tmp == NULL) goto failed;
6919 if (!PyList_Check(tmp)) {
6920 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6921 goto failed;
6922 }
6923 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006924 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006925 if (elts == NULL) goto failed;
6926 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006927 expr_ty val;
6928 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006929 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006930 if (len != PyList_GET_SIZE(tmp)) {
6931 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6932 goto failed;
6933 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006934 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006935 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006936 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006937 } else {
6938 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6939 return 1;
6940 }
6941 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6942 int res;
6943 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6944 if (tmp == NULL) goto failed;
6945 res = obj2ast_expr_context(tmp, &ctx, arena);
6946 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006947 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006948 } else {
6949 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6950 return 1;
6951 }
6952 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6953 if (*out == NULL) goto failed;
6954 return 0;
6955 }
6956
6957 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6958 failed:
6959 Py_XDECREF(tmp);
6960 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006961}
6962
6963int
6964obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6965{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006966 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006967
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006968 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6969 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006970 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006971 }
6972 if (isinstance) {
6973 *out = Load;
6974 return 0;
6975 }
6976 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6977 if (isinstance == -1) {
6978 return 1;
6979 }
6980 if (isinstance) {
6981 *out = Store;
6982 return 0;
6983 }
6984 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6985 if (isinstance == -1) {
6986 return 1;
6987 }
6988 if (isinstance) {
6989 *out = Del;
6990 return 0;
6991 }
6992 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6993 if (isinstance == -1) {
6994 return 1;
6995 }
6996 if (isinstance) {
6997 *out = AugLoad;
6998 return 0;
6999 }
7000 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
7001 if (isinstance == -1) {
7002 return 1;
7003 }
7004 if (isinstance) {
7005 *out = AugStore;
7006 return 0;
7007 }
7008 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
7009 if (isinstance == -1) {
7010 return 1;
7011 }
7012 if (isinstance) {
7013 *out = Param;
7014 return 0;
7015 }
7016
7017 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
7018 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007019}
7020
7021int
7022obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
7023{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007024 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007025
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007026 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007027
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007028 if (obj == Py_None) {
7029 *out = NULL;
7030 return 0;
7031 }
7032 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
7033 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007034 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007035 }
7036 if (isinstance) {
7037 expr_ty lower;
7038 expr_ty upper;
7039 expr_ty step;
7040
Victor Stinneree4b59c2013-07-27 00:01:35 +02007041 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007042 int res;
7043 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
7044 if (tmp == NULL) goto failed;
7045 res = obj2ast_expr(tmp, &lower, arena);
7046 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007048 } else {
7049 lower = NULL;
7050 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007051 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007052 int res;
7053 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
7054 if (tmp == NULL) goto failed;
7055 res = obj2ast_expr(tmp, &upper, arena);
7056 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007057 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007058 } else {
7059 upper = NULL;
7060 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007061 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007062 int res;
7063 tmp = _PyObject_GetAttrId(obj, &PyId_step);
7064 if (tmp == NULL) goto failed;
7065 res = obj2ast_expr(tmp, &step, arena);
7066 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007067 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007068 } else {
7069 step = NULL;
7070 }
7071 *out = Slice(lower, upper, step, arena);
7072 if (*out == NULL) goto failed;
7073 return 0;
7074 }
7075 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7076 if (isinstance == -1) {
7077 return 1;
7078 }
7079 if (isinstance) {
7080 asdl_seq* dims;
7081
7082 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
7083 int res;
7084 Py_ssize_t len;
7085 Py_ssize_t i;
7086 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
7087 if (tmp == NULL) goto failed;
7088 if (!PyList_Check(tmp)) {
7089 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7090 goto failed;
7091 }
7092 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007093 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007094 if (dims == NULL) goto failed;
7095 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007096 slice_ty val;
7097 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007098 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007099 if (len != PyList_GET_SIZE(tmp)) {
7100 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7101 goto failed;
7102 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007103 asdl_seq_SET(dims, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007104 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007105 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007106 } else {
7107 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7108 return 1;
7109 }
7110 *out = ExtSlice(dims, arena);
7111 if (*out == NULL) goto failed;
7112 return 0;
7113 }
7114 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7115 if (isinstance == -1) {
7116 return 1;
7117 }
7118 if (isinstance) {
7119 expr_ty value;
7120
7121 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7122 int res;
7123 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7124 if (tmp == NULL) goto failed;
7125 res = obj2ast_expr(tmp, &value, arena);
7126 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007127 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007128 } else {
7129 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7130 return 1;
7131 }
7132 *out = Index(value, arena);
7133 if (*out == NULL) goto failed;
7134 return 0;
7135 }
7136
7137 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7138 failed:
7139 Py_XDECREF(tmp);
7140 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007141}
7142
7143int
7144obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7145{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007146 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007147
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007148 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7149 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007150 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007151 }
7152 if (isinstance) {
7153 *out = And;
7154 return 0;
7155 }
7156 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7157 if (isinstance == -1) {
7158 return 1;
7159 }
7160 if (isinstance) {
7161 *out = Or;
7162 return 0;
7163 }
7164
7165 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7166 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007167}
7168
7169int
7170obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7171{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007172 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007173
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007174 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7175 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007176 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007177 }
7178 if (isinstance) {
7179 *out = Add;
7180 return 0;
7181 }
7182 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7183 if (isinstance == -1) {
7184 return 1;
7185 }
7186 if (isinstance) {
7187 *out = Sub;
7188 return 0;
7189 }
7190 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7191 if (isinstance == -1) {
7192 return 1;
7193 }
7194 if (isinstance) {
7195 *out = Mult;
7196 return 0;
7197 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007198 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7199 if (isinstance == -1) {
7200 return 1;
7201 }
7202 if (isinstance) {
7203 *out = MatMult;
7204 return 0;
7205 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007206 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7207 if (isinstance == -1) {
7208 return 1;
7209 }
7210 if (isinstance) {
7211 *out = Div;
7212 return 0;
7213 }
7214 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7215 if (isinstance == -1) {
7216 return 1;
7217 }
7218 if (isinstance) {
7219 *out = Mod;
7220 return 0;
7221 }
7222 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7223 if (isinstance == -1) {
7224 return 1;
7225 }
7226 if (isinstance) {
7227 *out = Pow;
7228 return 0;
7229 }
7230 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7231 if (isinstance == -1) {
7232 return 1;
7233 }
7234 if (isinstance) {
7235 *out = LShift;
7236 return 0;
7237 }
7238 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7239 if (isinstance == -1) {
7240 return 1;
7241 }
7242 if (isinstance) {
7243 *out = RShift;
7244 return 0;
7245 }
7246 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7247 if (isinstance == -1) {
7248 return 1;
7249 }
7250 if (isinstance) {
7251 *out = BitOr;
7252 return 0;
7253 }
7254 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7255 if (isinstance == -1) {
7256 return 1;
7257 }
7258 if (isinstance) {
7259 *out = BitXor;
7260 return 0;
7261 }
7262 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7263 if (isinstance == -1) {
7264 return 1;
7265 }
7266 if (isinstance) {
7267 *out = BitAnd;
7268 return 0;
7269 }
7270 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7271 if (isinstance == -1) {
7272 return 1;
7273 }
7274 if (isinstance) {
7275 *out = FloorDiv;
7276 return 0;
7277 }
7278
7279 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7280 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007281}
7282
7283int
7284obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7285{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007286 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007287
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007288 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7289 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007290 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007291 }
7292 if (isinstance) {
7293 *out = Invert;
7294 return 0;
7295 }
7296 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7297 if (isinstance == -1) {
7298 return 1;
7299 }
7300 if (isinstance) {
7301 *out = Not;
7302 return 0;
7303 }
7304 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7305 if (isinstance == -1) {
7306 return 1;
7307 }
7308 if (isinstance) {
7309 *out = UAdd;
7310 return 0;
7311 }
7312 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7313 if (isinstance == -1) {
7314 return 1;
7315 }
7316 if (isinstance) {
7317 *out = USub;
7318 return 0;
7319 }
7320
7321 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7322 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007323}
7324
7325int
7326obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7327{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007328 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007329
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007330 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7331 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007332 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007333 }
7334 if (isinstance) {
7335 *out = Eq;
7336 return 0;
7337 }
7338 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7339 if (isinstance == -1) {
7340 return 1;
7341 }
7342 if (isinstance) {
7343 *out = NotEq;
7344 return 0;
7345 }
7346 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7347 if (isinstance == -1) {
7348 return 1;
7349 }
7350 if (isinstance) {
7351 *out = Lt;
7352 return 0;
7353 }
7354 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7355 if (isinstance == -1) {
7356 return 1;
7357 }
7358 if (isinstance) {
7359 *out = LtE;
7360 return 0;
7361 }
7362 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7363 if (isinstance == -1) {
7364 return 1;
7365 }
7366 if (isinstance) {
7367 *out = Gt;
7368 return 0;
7369 }
7370 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7371 if (isinstance == -1) {
7372 return 1;
7373 }
7374 if (isinstance) {
7375 *out = GtE;
7376 return 0;
7377 }
7378 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7379 if (isinstance == -1) {
7380 return 1;
7381 }
7382 if (isinstance) {
7383 *out = Is;
7384 return 0;
7385 }
7386 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
7387 if (isinstance == -1) {
7388 return 1;
7389 }
7390 if (isinstance) {
7391 *out = IsNot;
7392 return 0;
7393 }
7394 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7395 if (isinstance == -1) {
7396 return 1;
7397 }
7398 if (isinstance) {
7399 *out = In;
7400 return 0;
7401 }
7402 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7403 if (isinstance == -1) {
7404 return 1;
7405 }
7406 if (isinstance) {
7407 *out = NotIn;
7408 return 0;
7409 }
7410
7411 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7412 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007413}
7414
7415int
7416obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7417{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007418 PyObject* tmp = NULL;
7419 expr_ty target;
7420 expr_ty iter;
7421 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007422 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007423
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007424 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7425 int res;
7426 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7427 if (tmp == NULL) goto failed;
7428 res = obj2ast_expr(tmp, &target, arena);
7429 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007430 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007431 } else {
7432 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007433 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007434 }
7435 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7436 int res;
7437 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7438 if (tmp == NULL) goto failed;
7439 res = obj2ast_expr(tmp, &iter, arena);
7440 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007441 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007442 } else {
7443 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7444 return 1;
7445 }
7446 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7447 int res;
7448 Py_ssize_t len;
7449 Py_ssize_t i;
7450 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7451 if (tmp == NULL) goto failed;
7452 if (!PyList_Check(tmp)) {
7453 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7454 goto failed;
7455 }
7456 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007457 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007458 if (ifs == NULL) goto failed;
7459 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007460 expr_ty val;
7461 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007462 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007463 if (len != PyList_GET_SIZE(tmp)) {
7464 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7465 goto failed;
7466 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007467 asdl_seq_SET(ifs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007468 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007469 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007470 } else {
7471 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7472 return 1;
7473 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07007474 if (_PyObject_HasAttrId(obj, &PyId_is_async)) {
7475 int res;
7476 tmp = _PyObject_GetAttrId(obj, &PyId_is_async);
7477 if (tmp == NULL) goto failed;
7478 res = obj2ast_int(tmp, &is_async, arena);
7479 if (res != 0) goto failed;
7480 Py_CLEAR(tmp);
7481 } else {
7482 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
7483 return 1;
7484 }
7485 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007486 return 0;
7487failed:
7488 Py_XDECREF(tmp);
7489 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007490}
7491
7492int
7493obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7494{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007495 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007496
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007497 PyObject *tmp = NULL;
7498 int lineno;
7499 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007500
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007501 if (obj == Py_None) {
7502 *out = NULL;
7503 return 0;
7504 }
7505 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7506 int res;
7507 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7508 if (tmp == NULL) goto failed;
7509 res = obj2ast_int(tmp, &lineno, arena);
7510 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007511 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007512 } else {
7513 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007514 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007515 }
7516 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7517 int res;
7518 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7519 if (tmp == NULL) goto failed;
7520 res = obj2ast_int(tmp, &col_offset, arena);
7521 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007522 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007523 } else {
7524 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7525 return 1;
7526 }
7527 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7528 if (isinstance == -1) {
7529 return 1;
7530 }
7531 if (isinstance) {
7532 expr_ty type;
7533 identifier name;
7534 asdl_seq* body;
7535
Victor Stinneree4b59c2013-07-27 00:01:35 +02007536 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007537 int res;
7538 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7539 if (tmp == NULL) goto failed;
7540 res = obj2ast_expr(tmp, &type, arena);
7541 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007542 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007543 } else {
7544 type = NULL;
7545 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007546 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007547 int res;
7548 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7549 if (tmp == NULL) goto failed;
7550 res = obj2ast_identifier(tmp, &name, arena);
7551 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007552 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007553 } else {
7554 name = NULL;
7555 }
7556 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7557 int res;
7558 Py_ssize_t len;
7559 Py_ssize_t i;
7560 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7561 if (tmp == NULL) goto failed;
7562 if (!PyList_Check(tmp)) {
7563 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7564 goto failed;
7565 }
7566 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007567 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007568 if (body == NULL) goto failed;
7569 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007570 stmt_ty val;
7571 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007572 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007573 if (len != PyList_GET_SIZE(tmp)) {
7574 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7575 goto failed;
7576 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007577 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007578 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007579 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007580 } else {
7581 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7582 return 1;
7583 }
7584 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7585 if (*out == NULL) goto failed;
7586 return 0;
7587 }
7588
7589 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7590 failed:
7591 Py_XDECREF(tmp);
7592 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007593}
7594
7595int
7596obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7597{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007598 PyObject* tmp = NULL;
7599 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007600 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007601 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007602 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007603 arg_ty kwarg;
7604 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007605
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007606 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7607 int res;
7608 Py_ssize_t len;
7609 Py_ssize_t i;
7610 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7611 if (tmp == NULL) goto failed;
7612 if (!PyList_Check(tmp)) {
7613 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7614 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007615 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007616 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007617 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007618 if (args == NULL) goto failed;
7619 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007620 arg_ty val;
7621 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007622 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007623 if (len != PyList_GET_SIZE(tmp)) {
7624 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7625 goto failed;
7626 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007627 asdl_seq_SET(args, i, val);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007628 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007629 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007630 } else {
7631 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007632 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007633 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007634 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007635 int res;
7636 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7637 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007638 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007639 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007640 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007641 } else {
7642 vararg = NULL;
7643 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007644 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7645 int res;
7646 Py_ssize_t len;
7647 Py_ssize_t i;
7648 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7649 if (tmp == NULL) goto failed;
7650 if (!PyList_Check(tmp)) {
7651 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7652 goto failed;
7653 }
7654 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007655 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007656 if (kwonlyargs == NULL) goto failed;
7657 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007658 arg_ty val;
7659 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007660 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007661 if (len != PyList_GET_SIZE(tmp)) {
7662 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7663 goto failed;
7664 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007665 asdl_seq_SET(kwonlyargs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007666 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007667 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007668 } else {
7669 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7670 return 1;
7671 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007672 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7673 int res;
7674 Py_ssize_t len;
7675 Py_ssize_t i;
7676 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7677 if (tmp == NULL) goto failed;
7678 if (!PyList_Check(tmp)) {
7679 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7680 goto failed;
7681 }
7682 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007683 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007684 if (kw_defaults == NULL) goto failed;
7685 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007686 expr_ty val;
7687 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007688 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007689 if (len != PyList_GET_SIZE(tmp)) {
7690 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7691 goto failed;
7692 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007693 asdl_seq_SET(kw_defaults, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007694 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007696 } else {
7697 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7698 return 1;
7699 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007700 if (exists_not_none(obj, &PyId_kwarg)) {
7701 int res;
7702 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7703 if (tmp == NULL) goto failed;
7704 res = obj2ast_arg(tmp, &kwarg, arena);
7705 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007706 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007707 } else {
7708 kwarg = NULL;
7709 }
7710 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7711 int res;
7712 Py_ssize_t len;
7713 Py_ssize_t i;
7714 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7715 if (tmp == NULL) goto failed;
7716 if (!PyList_Check(tmp)) {
7717 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7718 goto failed;
7719 }
7720 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007721 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007722 if (defaults == NULL) goto failed;
7723 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007724 expr_ty val;
7725 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007726 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007727 if (len != PyList_GET_SIZE(tmp)) {
7728 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7729 goto failed;
7730 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007731 asdl_seq_SET(defaults, i, val);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007732 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007733 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007734 } else {
7735 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7736 return 1;
7737 }
7738 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7739 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007740 return 0;
7741failed:
7742 Py_XDECREF(tmp);
7743 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007744}
7745
7746int
7747obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7748{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007749 PyObject* tmp = NULL;
7750 identifier arg;
7751 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007752 int lineno;
7753 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007754
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007755 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7756 int res;
7757 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7758 if (tmp == NULL) goto failed;
7759 res = obj2ast_identifier(tmp, &arg, arena);
7760 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007761 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007762 } else {
7763 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007764 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007765 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007766 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007767 int res;
7768 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7769 if (tmp == NULL) goto failed;
7770 res = obj2ast_expr(tmp, &annotation, arena);
7771 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007772 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007773 } else {
7774 annotation = NULL;
7775 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007776 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7777 int res;
7778 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7779 if (tmp == NULL) goto failed;
7780 res = obj2ast_int(tmp, &lineno, arena);
7781 if (res != 0) goto failed;
7782 Py_CLEAR(tmp);
7783 } else {
7784 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7785 return 1;
7786 }
7787 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7788 int res;
7789 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7790 if (tmp == NULL) goto failed;
7791 res = obj2ast_int(tmp, &col_offset, arena);
7792 if (res != 0) goto failed;
7793 Py_CLEAR(tmp);
7794 } else {
7795 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7796 return 1;
7797 }
7798 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007799 return 0;
7800failed:
7801 Py_XDECREF(tmp);
7802 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007803}
7804
7805int
7806obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7807{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007808 PyObject* tmp = NULL;
7809 identifier arg;
7810 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007811
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007812 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007813 int res;
7814 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7815 if (tmp == NULL) goto failed;
7816 res = obj2ast_identifier(tmp, &arg, arena);
7817 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007818 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007819 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007820 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007821 }
7822 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7823 int res;
7824 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7825 if (tmp == NULL) goto failed;
7826 res = obj2ast_expr(tmp, &value, arena);
7827 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007828 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007829 } else {
7830 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7831 return 1;
7832 }
7833 *out = keyword(arg, value, arena);
7834 return 0;
7835failed:
7836 Py_XDECREF(tmp);
7837 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007838}
7839
7840int
7841obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7842{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007843 PyObject* tmp = NULL;
7844 identifier name;
7845 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007846
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007847 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7848 int res;
7849 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7850 if (tmp == NULL) goto failed;
7851 res = obj2ast_identifier(tmp, &name, arena);
7852 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007853 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007854 } else {
7855 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007856 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007857 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007858 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007859 int res;
7860 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7861 if (tmp == NULL) goto failed;
7862 res = obj2ast_identifier(tmp, &asname, arena);
7863 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007864 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007865 } else {
7866 asname = NULL;
7867 }
7868 *out = alias(name, asname, arena);
7869 return 0;
7870failed:
7871 Py_XDECREF(tmp);
7872 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007873}
7874
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007875int
7876obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7877{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007878 PyObject* tmp = NULL;
7879 expr_ty context_expr;
7880 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007881
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007882 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7883 int res;
7884 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7885 if (tmp == NULL) goto failed;
7886 res = obj2ast_expr(tmp, &context_expr, arena);
7887 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007888 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007889 } else {
7890 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007891 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007892 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007893 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007894 int res;
7895 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7896 if (tmp == NULL) goto failed;
7897 res = obj2ast_expr(tmp, &optional_vars, arena);
7898 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007899 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007900 } else {
7901 optional_vars = NULL;
7902 }
7903 *out = withitem(context_expr, optional_vars, arena);
7904 return 0;
7905failed:
7906 Py_XDECREF(tmp);
7907 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007908}
7909
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007910
Martin v. Löwis1a214512008-06-11 05:26:20 +00007911static struct PyModuleDef _astmodule = {
7912 PyModuleDef_HEAD_INIT, "_ast"
7913};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007914PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007915PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007916{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007917 PyObject *m, *d;
7918 if (!init_types()) return NULL;
7919 m = PyModule_Create(&_astmodule);
7920 if (!m) return NULL;
7921 d = PyModule_GetDict(m);
7922 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007923 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007924 return NULL;
7925 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7926 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7927 NULL;
7928 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7929 0) return NULL;
7930 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7931 return NULL;
7932 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7933 NULL;
7934 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7935 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7936 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007937 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7938 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007939 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7940 return NULL;
7941 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7942 NULL;
7943 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7944 NULL;
7945 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7946 NULL;
7947 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7948 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07007949 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
7950 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007951 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007952 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7953 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007954 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7955 NULL;
7956 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7957 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007958 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7959 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007960 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7961 NULL;
7962 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7963 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7964 NULL;
7965 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7966 NULL;
7967 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7968 return NULL;
7969 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7970 NULL;
7971 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7972 return NULL;
7973 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7974 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7975 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7976 NULL;
7977 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7978 return NULL;
7979 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7980 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7981 NULL;
7982 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7983 NULL;
7984 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7985 NULL;
7986 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7987 NULL;
7988 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7989 NULL;
7990 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7991 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7992 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7993 return NULL;
7994 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7995 NULL;
7996 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7997 return NULL;
7998 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7999 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008000 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
8001 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008002 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
8003 NULL;
8004 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
8005 return NULL;
8006 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
8007 NULL;
8008 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
8009 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
8010 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04008011 if (PyDict_SetItemString(d, "FormattedValue",
8012 (PyObject*)FormattedValue_type) < 0) return NULL;
8013 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
8014 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008015 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
8016 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008017 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
8018 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008019 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
8020 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008021 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
8022 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008023 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
8024 return NULL;
8025 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
8026 return NULL;
8027 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
8028 NULL;
8029 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
8030 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
8031 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
8032 NULL;
8033 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
8034 0) return NULL;
8035 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
8036 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
8037 NULL;
8038 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
8039 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
8040 NULL;
8041 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
8042 return NULL;
8043 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
8044 NULL;
8045 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
8046 NULL;
8047 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
8048 NULL;
8049 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
8050 return NULL;
8051 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
8052 NULL;
8053 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
8054 NULL;
8055 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
8056 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
8057 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
8058 return NULL;
8059 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
8060 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
8061 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04008062 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
8063 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008064 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
8065 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
8066 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
8067 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
8068 NULL;
8069 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
8070 NULL;
8071 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
8072 NULL;
8073 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8074 NULL;
8075 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8076 NULL;
8077 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8078 return NULL;
8079 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8080 NULL;
8081 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8082 NULL;
8083 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8084 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8085 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8086 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8087 NULL;
8088 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8089 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8090 NULL;
8091 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8092 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8093 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8094 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8095 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8096 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8097 NULL;
8098 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8099 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8100 NULL;
8101 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8102 < 0) return NULL;
8103 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8104 < 0) return NULL;
8105 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8106 < 0) return NULL;
8107 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8108 return NULL;
8109 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8110 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8111 NULL;
8112 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8113 NULL;
8114 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8115 return NULL;
8116 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008117}
8118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008119
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008120PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008121{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008122 if (!init_types())
8123 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008124 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008125}
Martin v. Löwis5b222132007-06-10 09:51:05 +00008126
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008127/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
8128mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008129{
8130 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008131 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008132 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008133 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008134
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008135 req_type[0] = (PyObject*)Module_type;
8136 req_type[1] = (PyObject*)Expression_type;
8137 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008138
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008139 assert(0 <= mode && mode <= 2);
8140
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008141 if (!init_types())
8142 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008143
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008144 isinstance = PyObject_IsInstance(ast, req_type[mode]);
8145 if (isinstance == -1)
8146 return NULL;
8147 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008148 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8149 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008150 return NULL;
8151 }
8152 if (obj2ast_mod(ast, &res, arena) != 0)
8153 return NULL;
8154 else
8155 return res;
8156}
8157
8158int PyAST_Check(PyObject* obj)
8159{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008160 if (!init_types())
8161 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00008162 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008163}
8164
Martin v. Löwis5b222132007-06-10 09:51:05 +00008165