blob: 5467d192ee69c07443bd39150df550dcca6842f1 [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);
Guido van Rossumdcfcd142019-01-31 03:40:27 -080013_Py_IDENTIFIER(type_ignores);
Neal Norwitz53d960c2006-02-28 22:47:29 +000014static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020015 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -080016 "type_ignores",
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};
Guido van Rossumdcfcd142019-01-31 03:40:27 -080026static PyTypeObject *FunctionType_type;
27_Py_IDENTIFIER(argtypes);
28_Py_IDENTIFIER(returns);
29static char *FunctionType_fields[]={
30 "argtypes",
31 "returns",
32};
Neal Norwitz53d960c2006-02-28 22:47:29 +000033static PyTypeObject *Suite_type;
34static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020035 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000036};
Neal Norwitz53d960c2006-02-28 22:47:29 +000037static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020038_Py_IDENTIFIER(lineno);
39_Py_IDENTIFIER(col_offset);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000040_Py_IDENTIFIER(end_lineno);
41_Py_IDENTIFIER(end_col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000042static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020043 "lineno",
44 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000045 "end_lineno",
46 "end_col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000047};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000048static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000049static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020050_Py_IDENTIFIER(name);
51_Py_IDENTIFIER(args);
52_Py_IDENTIFIER(decorator_list);
Guido van Rossumdcfcd142019-01-31 03:40:27 -080053_Py_IDENTIFIER(type_comment);
Neal Norwitz53d960c2006-02-28 22:47:29 +000054static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020055 "name",
56 "args",
57 "body",
58 "decorator_list",
59 "returns",
Guido van Rossumdcfcd142019-01-31 03:40:27 -080060 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000061};
Yury Selivanov75445082015-05-11 22:57:16 -040062static PyTypeObject *AsyncFunctionDef_type;
63static char *AsyncFunctionDef_fields[]={
64 "name",
65 "args",
66 "body",
67 "decorator_list",
68 "returns",
Guido van Rossumdcfcd142019-01-31 03:40:27 -080069 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -040070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(bases);
73_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000074static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020075 "name",
76 "bases",
77 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020078 "body",
79 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000083static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020084 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000085};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020087_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000088static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020089 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000090};
Neal Norwitz53d960c2006-02-28 22:47:29 +000091static PyTypeObject *Assign_type;
92static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020093 "targets",
94 "value",
Guido van Rossumdcfcd142019-01-31 03:40:27 -080095 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000096};
Neal Norwitz53d960c2006-02-28 22:47:29 +000097static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020098_Py_IDENTIFIER(target);
99_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000100static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200101 "target",
102 "op",
103 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000104};
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700105static PyTypeObject *AnnAssign_type;
106_Py_IDENTIFIER(annotation);
107_Py_IDENTIFIER(simple);
108static char *AnnAssign_fields[]={
109 "target",
110 "annotation",
111 "value",
112 "simple",
113};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000114static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200115_Py_IDENTIFIER(iter);
116_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000117static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200118 "target",
119 "iter",
120 "body",
121 "orelse",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800122 "type_comment",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000123};
Yury Selivanov75445082015-05-11 22:57:16 -0400124static PyTypeObject *AsyncFor_type;
125static char *AsyncFor_fields[]={
126 "target",
127 "iter",
128 "body",
129 "orelse",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800130 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -0400131};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000132static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200133_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000134static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200135 "test",
136 "body",
137 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000139static PyTypeObject *If_type;
140static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200141 "test",
142 "body",
143 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000147static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200148 "items",
149 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800150 "type_comment",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000151};
Yury Selivanov75445082015-05-11 22:57:16 -0400152static PyTypeObject *AsyncWith_type;
153static char *AsyncWith_fields[]={
154 "items",
155 "body",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800156 "type_comment",
Yury Selivanov75445082015-05-11 22:57:16 -0400157};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000158static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200159_Py_IDENTIFIER(exc);
160_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000161static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200162 "exc",
163 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000164};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500165static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200166_Py_IDENTIFIER(handlers);
167_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500168static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200169 "body",
170 "handlers",
171 "orelse",
172 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000173};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000174static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200175_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000176static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200177 "test",
178 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200181_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000182static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200183 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200186_Py_IDENTIFIER(module);
187_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000188static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200189 "module",
190 "names",
191 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000192};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000193static PyTypeObject *Global_type;
194static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200195 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000196};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000197static PyTypeObject *Nonlocal_type;
198static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200199 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *Expr_type;
202static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200203 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Pass_type;
206static PyTypeObject *Break_type;
207static PyTypeObject *Continue_type;
208static PyTypeObject *expr_type;
209static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200210 "lineno",
211 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000212 "end_lineno",
213 "end_col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000214};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000215static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000216static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200217_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000218static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200219 "op",
220 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221};
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700222static PyTypeObject *NamedExpr_type;
223static char *NamedExpr_fields[]={
224 "target",
225 "value",
226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200228_Py_IDENTIFIER(left);
229_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000230static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200231 "left",
232 "op",
233 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000234};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000235static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200236_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000237static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200238 "op",
239 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000240};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000241static PyTypeObject *Lambda_type;
242static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200243 "args",
244 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000245};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000246static PyTypeObject *IfExp_type;
247static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200248 "test",
249 "body",
250 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000251};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000252static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200253_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000254static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200255 "keys",
256 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000257};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000258static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200259_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000260static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200261 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000262};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000263static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200264_Py_IDENTIFIER(elt);
265_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000266static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200267 "elt",
268 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000269};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000270static PyTypeObject *SetComp_type;
271static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200272 "elt",
273 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000274};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000275static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200276_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000277static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200278 "key",
279 "value",
280 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000281};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000282static PyTypeObject *GeneratorExp_type;
283static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200284 "elt",
285 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286};
Yury Selivanov75445082015-05-11 22:57:16 -0400287static PyTypeObject *Await_type;
288static char *Await_fields[]={
289 "value",
290};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000291static PyTypeObject *Yield_type;
292static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200293 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500294};
295static PyTypeObject *YieldFrom_type;
296static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200297 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000299static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200300_Py_IDENTIFIER(ops);
301_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000302static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200303 "left",
304 "ops",
305 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000306};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000307static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200308_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000309static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200310 "func",
311 "args",
312 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000313};
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314static PyTypeObject *FormattedValue_type;
315_Py_IDENTIFIER(conversion);
316_Py_IDENTIFIER(format_spec);
317static char *FormattedValue_fields[]={
318 "value",
319 "conversion",
320 "format_spec",
321};
322static PyTypeObject *JoinedStr_type;
323static char *JoinedStr_fields[]={
324 "values",
325};
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100326static PyTypeObject *Constant_type;
327static char *Constant_fields[]={
328 "value",
329};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000330static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200331_Py_IDENTIFIER(attr);
332_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200334 "value",
335 "attr",
336 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000337};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000338static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200339_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000340static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200341 "value",
342 "slice",
343 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344};
Guido van Rossum0368b722007-05-11 16:50:42 +0000345static PyTypeObject *Starred_type;
346static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200347 "value",
348 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000349};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000350static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200351_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200353 "id",
354 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000355};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static PyTypeObject *List_type;
357static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200358 "elts",
359 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000360};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000361static PyTypeObject *Tuple_type;
362static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200363 "elts",
364 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700368*AugLoad_singleton, *AugStore_singleton, *Param_singleton,
369*NamedStore_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000370static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000371static PyTypeObject *Load_type;
372static PyTypeObject *Store_type;
373static PyTypeObject *Del_type;
374static PyTypeObject *AugLoad_type;
375static PyTypeObject *AugStore_type;
376static PyTypeObject *Param_type;
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700377static PyTypeObject *NamedStore_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000378static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000379static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000380static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200381_Py_IDENTIFIER(lower);
382_Py_IDENTIFIER(upper);
383_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000384static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200385 "lower",
386 "upper",
387 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000388};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000389static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200390_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200392 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000393};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000394static PyTypeObject *Index_type;
395static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200396 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000397};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000398static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000399static PyObject *And_singleton, *Or_singleton;
400static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000401static PyTypeObject *And_type;
402static PyTypeObject *Or_type;
403static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000404static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400405*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
406*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
407*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000408static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000409static PyTypeObject *Add_type;
410static PyTypeObject *Sub_type;
411static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400412static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000413static PyTypeObject *Div_type;
414static PyTypeObject *Mod_type;
415static PyTypeObject *Pow_type;
416static PyTypeObject *LShift_type;
417static PyTypeObject *RShift_type;
418static PyTypeObject *BitOr_type;
419static PyTypeObject *BitXor_type;
420static PyTypeObject *BitAnd_type;
421static PyTypeObject *FloorDiv_type;
422static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000423static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
424*USub_singleton;
425static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000426static PyTypeObject *Invert_type;
427static PyTypeObject *Not_type;
428static PyTypeObject *UAdd_type;
429static PyTypeObject *USub_type;
430static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000431static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
432*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
433*NotIn_singleton;
434static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000435static PyTypeObject *Eq_type;
436static PyTypeObject *NotEq_type;
437static PyTypeObject *Lt_type;
438static PyTypeObject *LtE_type;
439static PyTypeObject *Gt_type;
440static PyTypeObject *GtE_type;
441static PyTypeObject *Is_type;
442static PyTypeObject *IsNot_type;
443static PyTypeObject *In_type;
444static PyTypeObject *NotIn_type;
445static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000446static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200447_Py_IDENTIFIER(ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700448_Py_IDENTIFIER(is_async);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000449static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200450 "target",
451 "iter",
452 "ifs",
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700453 "is_async",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000454};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000455static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000456static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200457 "lineno",
458 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000459 "end_lineno",
460 "end_col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000461};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000462static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000463static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200464_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000465static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200466 "type",
467 "name",
468 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000469};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000470static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000471static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200472_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200473_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200474_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700475_Py_IDENTIFIER(kwarg);
476_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000477static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200478 "args",
479 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200480 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200481 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200482 "kwarg",
483 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000484};
Neal Norwitzc1505362006-12-28 06:47:50 +0000485static PyTypeObject *arg_type;
486static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700487static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200488 "lineno",
489 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000490 "end_lineno",
491 "end_col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700492};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200493_Py_IDENTIFIER(arg);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000494static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200495 "arg",
496 "annotation",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800497 "type_comment",
Neal Norwitzc1505362006-12-28 06:47:50 +0000498};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000499static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000500static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000501static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200502 "arg",
503 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000504};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000505static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000506static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200507_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000508static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200509 "name",
510 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000511};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500512static PyTypeObject *withitem_type;
513static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200514_Py_IDENTIFIER(context_expr);
515_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500516static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200517 "context_expr",
518 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500519};
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800520static PyTypeObject *type_ignore_type;
521static PyObject* ast2obj_type_ignore(void*);
522static PyTypeObject *TypeIgnore_type;
523static char *TypeIgnore_fields[]={
524 "lineno",
525};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000526
527
INADA Naokifc489082017-01-25 22:33:43 +0900528_Py_IDENTIFIER(_fields);
529_Py_IDENTIFIER(_attributes);
530
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700531typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100532 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700533 PyObject *dict;
534} AST_object;
535
Benjamin Peterson1767e022012-03-14 21:50:29 -0500536static void
537ast_dealloc(AST_object *self)
538{
INADA Naokia6296d32017-08-24 14:55:17 +0900539 /* bpo-31095: UnTrack is needed before calling any callbacks */
540 PyObject_GC_UnTrack(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500541 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200542 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500543}
544
Neal Norwitz207c9f32008-03-31 04:42:11 +0000545static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700546ast_traverse(AST_object *self, visitproc visit, void *arg)
547{
548 Py_VISIT(self->dict);
549 return 0;
550}
551
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300552static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700553ast_clear(AST_object *self)
554{
555 Py_CLEAR(self->dict);
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300556 return 0;
Benjamin Peterson81071762012-07-08 11:03:46 -0700557}
558
559static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000560ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
561{
562 Py_ssize_t i, numfields = 0;
563 int res = -1;
564 PyObject *key, *value, *fields;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200565 if (_PyObject_LookupAttrId((PyObject*)Py_TYPE(self), &PyId__fields, &fields) < 0) {
566 goto cleanup;
567 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000568 if (fields) {
569 numfields = PySequence_Size(fields);
570 if (numfields == -1)
571 goto cleanup;
572 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900573
Neal Norwitz207c9f32008-03-31 04:42:11 +0000574 res = 0; /* if no error occurs, this stays 0 to the end */
INADA Naoki4c78c522017-02-24 02:48:17 +0900575 if (numfields < PyTuple_GET_SIZE(args)) {
576 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
577 "%zd positional argument%s",
578 Py_TYPE(self)->tp_name,
579 numfields, numfields == 1 ? "" : "s");
580 res = -1;
581 goto cleanup;
582 }
583 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
584 /* cannot be reached when fields is NULL */
585 PyObject *name = PySequence_GetItem(fields, i);
586 if (!name) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000587 res = -1;
588 goto cleanup;
589 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900590 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
591 Py_DECREF(name);
592 if (res < 0)
593 goto cleanup;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000594 }
595 if (kw) {
596 i = 0; /* needed by PyDict_Next */
597 while (PyDict_Next(kw, &i, &key, &value)) {
598 res = PyObject_SetAttr(self, key, value);
599 if (res < 0)
600 goto cleanup;
601 }
602 }
603 cleanup:
604 Py_XDECREF(fields);
605 return res;
606}
607
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000608/* Pickling support */
609static PyObject *
610ast_type_reduce(PyObject *self, PyObject *unused)
611{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200612 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200613 PyObject *dict;
614 if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) {
615 return NULL;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000616 }
617 if (dict) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200618 return Py_BuildValue("O()N", Py_TYPE(self), dict);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000619 }
620 return Py_BuildValue("O()", Py_TYPE(self));
621}
622
623static PyMethodDef ast_type_methods[] = {
624 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
625 {NULL}
626};
627
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700628static PyGetSetDef ast_type_getsets[] = {
629 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
630 {NULL}
631};
632
Neal Norwitz207c9f32008-03-31 04:42:11 +0000633static PyTypeObject AST_type = {
634 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000635 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700636 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000637 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500638 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000639 0, /* tp_print */
640 0, /* tp_getattr */
641 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000642 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000643 0, /* tp_repr */
644 0, /* tp_as_number */
645 0, /* tp_as_sequence */
646 0, /* tp_as_mapping */
647 0, /* tp_hash */
648 0, /* tp_call */
649 0, /* tp_str */
650 PyObject_GenericGetAttr, /* tp_getattro */
651 PyObject_GenericSetAttr, /* tp_setattro */
652 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000654 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700655 (traverseproc)ast_traverse, /* tp_traverse */
656 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
659 0, /* tp_iter */
660 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000661 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000662 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700663 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000664 0, /* tp_base */
665 0, /* tp_dict */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700668 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000669 (initproc)ast_type_init, /* tp_init */
670 PyType_GenericAlloc, /* tp_alloc */
671 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700672 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000673};
674
675
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
677{
INADA Naokifc489082017-01-25 22:33:43 +0900678 _Py_IDENTIFIER(__module__);
679 _Py_IDENTIFIER(_ast);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 PyObject *fnames, *result;
681 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000682 fnames = PyTuple_New(num_fields);
683 if (!fnames) return NULL;
684 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000685 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 if (!field) {
687 Py_DECREF(fnames);
688 return NULL;
689 }
690 PyTuple_SET_ITEM(fnames, i, field);
691 }
INADA Naokifc489082017-01-25 22:33:43 +0900692 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
693 type, base,
694 _PyUnicode_FromId(&PyId__fields), fnames,
695 _PyUnicode_FromId(&PyId___module__),
696 _PyUnicode_FromId(&PyId__ast));
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Py_DECREF(fnames);
698 return (PyTypeObject*)result;
699}
700
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
702{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000704 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000705 if (!l)
706 return 0;
707 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000708 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!s) {
710 Py_DECREF(l);
711 return 0;
712 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000713 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200715 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716 Py_DECREF(l);
717 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718}
719
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000720/* Conversion AST -> Python */
721
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
723{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700724 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000725 PyObject *result = PyList_New(n);
726 PyObject *value;
727 if (!result)
728 return NULL;
729 for (i = 0; i < n; i++) {
730 value = func(asdl_seq_GET(seq, i));
731 if (!value) {
732 Py_DECREF(result);
733 return NULL;
734 }
735 PyList_SET_ITEM(result, i, value);
736 }
737 return result;
738}
739
740static PyObject* ast2obj_object(void *o)
741{
742 if (!o)
743 o = Py_None;
744 Py_INCREF((PyObject*)o);
745 return (PyObject*)o;
746}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500747#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100748#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000749#define ast2obj_identifier ast2obj_object
750#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500751#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000753static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754{
Christian Heimes217cfd12007-12-02 14:31:20 +0000755 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756}
757
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000758/* Conversion Python -> AST */
759
760static 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{
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300777 if (PyArena_AddPyObject(arena, obj) < 0) {
778 *out = NULL;
779 return -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100780 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300781 Py_INCREF(obj);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100782 *out = obj;
783 return 0;
784}
785
Benjamin Peterson180e6352011-07-22 11:09:07 -0500786static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500787{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500788 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
789 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500790 return 1;
791 }
792 return obj2ast_object(obj, out, arena);
793}
794
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800795static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
796{
797 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
798 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
799 return 1;
800 }
801 return obj2ast_object(obj, out, arena);
802}
803
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000804static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
805{
806 int i;
807 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100808 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809 return 1;
810 }
811
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300812 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000813 if (i == -1 && PyErr_Occurred())
814 return 1;
815 *out = i;
816 return 0;
817}
818
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000819static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000820{
821 PyObject *empty_tuple, *d;
822 if (PyType_Ready(&AST_type) < 0)
823 return -1;
824 d = AST_type.tp_dict;
825 empty_tuple = PyTuple_New(0);
826 if (!empty_tuple ||
INADA Naokifc489082017-01-25 22:33:43 +0900827 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
828 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +0000829 Py_XDECREF(empty_tuple);
830 return -1;
831 }
832 Py_DECREF(empty_tuple);
833 return 0;
834}
835
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000836
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000837static int init_types(void)
838{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200839 static int initialized;
840 if (initialized) return 1;
841 if (add_ast_fields() < 0) return 0;
842 mod_type = make_type("mod", &AST_type, NULL, 0);
843 if (!mod_type) return 0;
844 if (!add_attributes(mod_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800845 Module_type = make_type("Module", mod_type, Module_fields, 2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200846 if (!Module_type) return 0;
847 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
848 1);
849 if (!Interactive_type) return 0;
850 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
851 if (!Expression_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800852 FunctionType_type = make_type("FunctionType", mod_type,
853 FunctionType_fields, 2);
854 if (!FunctionType_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200855 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
856 if (!Suite_type) return 0;
857 stmt_type = make_type("stmt", &AST_type, NULL, 0);
858 if (!stmt_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000859 if (!add_attributes(stmt_type, stmt_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200860 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800861 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200862 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400863 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800864 AsyncFunctionDef_fields, 6);
Yury Selivanov75445082015-05-11 22:57:16 -0400865 if (!AsyncFunctionDef_type) return 0;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300866 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200867 if (!ClassDef_type) return 0;
868 Return_type = make_type("Return", stmt_type, Return_fields, 1);
869 if (!Return_type) return 0;
870 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
871 if (!Delete_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800872 Assign_type = make_type("Assign", stmt_type, Assign_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200873 if (!Assign_type) return 0;
874 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
875 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700876 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
877 if (!AnnAssign_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800878 For_type = make_type("For", stmt_type, For_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200879 if (!For_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800880 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 5);
Yury Selivanov75445082015-05-11 22:57:16 -0400881 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200882 While_type = make_type("While", stmt_type, While_fields, 3);
883 if (!While_type) return 0;
884 If_type = make_type("If", stmt_type, If_fields, 3);
885 if (!If_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800886 With_type = make_type("With", stmt_type, With_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200887 if (!With_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800888 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 3);
Yury Selivanov75445082015-05-11 22:57:16 -0400889 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200890 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
891 if (!Raise_type) return 0;
892 Try_type = make_type("Try", stmt_type, Try_fields, 4);
893 if (!Try_type) return 0;
894 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
895 if (!Assert_type) return 0;
896 Import_type = make_type("Import", stmt_type, Import_fields, 1);
897 if (!Import_type) return 0;
898 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
899 if (!ImportFrom_type) return 0;
900 Global_type = make_type("Global", stmt_type, Global_fields, 1);
901 if (!Global_type) return 0;
902 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
903 if (!Nonlocal_type) return 0;
904 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
905 if (!Expr_type) return 0;
906 Pass_type = make_type("Pass", stmt_type, NULL, 0);
907 if (!Pass_type) return 0;
908 Break_type = make_type("Break", stmt_type, NULL, 0);
909 if (!Break_type) return 0;
910 Continue_type = make_type("Continue", stmt_type, NULL, 0);
911 if (!Continue_type) return 0;
912 expr_type = make_type("expr", &AST_type, NULL, 0);
913 if (!expr_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000914 if (!add_attributes(expr_type, expr_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200915 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
916 if (!BoolOp_type) return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700917 NamedExpr_type = make_type("NamedExpr", expr_type, NamedExpr_fields, 2);
918 if (!NamedExpr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200919 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
920 if (!BinOp_type) return 0;
921 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
922 if (!UnaryOp_type) return 0;
923 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
924 if (!Lambda_type) return 0;
925 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
926 if (!IfExp_type) return 0;
927 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
928 if (!Dict_type) return 0;
929 Set_type = make_type("Set", expr_type, Set_fields, 1);
930 if (!Set_type) return 0;
931 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
932 if (!ListComp_type) return 0;
933 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
934 if (!SetComp_type) return 0;
935 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
936 if (!DictComp_type) return 0;
937 GeneratorExp_type = make_type("GeneratorExp", expr_type,
938 GeneratorExp_fields, 2);
939 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400940 Await_type = make_type("Await", expr_type, Await_fields, 1);
941 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200942 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
943 if (!Yield_type) return 0;
944 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
945 if (!YieldFrom_type) return 0;
946 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
947 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400948 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200949 if (!Call_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400950 FormattedValue_type = make_type("FormattedValue", expr_type,
951 FormattedValue_fields, 3);
952 if (!FormattedValue_type) return 0;
953 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
954 if (!JoinedStr_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100955 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
956 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200957 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
958 if (!Attribute_type) return 0;
959 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
960 if (!Subscript_type) return 0;
961 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
962 if (!Starred_type) return 0;
963 Name_type = make_type("Name", expr_type, Name_fields, 2);
964 if (!Name_type) return 0;
965 List_type = make_type("List", expr_type, List_fields, 2);
966 if (!List_type) return 0;
967 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
968 if (!Tuple_type) return 0;
969 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
970 if (!expr_context_type) return 0;
971 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
972 Load_type = make_type("Load", expr_context_type, NULL, 0);
973 if (!Load_type) return 0;
974 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
975 if (!Load_singleton) return 0;
976 Store_type = make_type("Store", expr_context_type, NULL, 0);
977 if (!Store_type) return 0;
978 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
979 if (!Store_singleton) return 0;
980 Del_type = make_type("Del", expr_context_type, NULL, 0);
981 if (!Del_type) return 0;
982 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
983 if (!Del_singleton) return 0;
984 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
985 if (!AugLoad_type) return 0;
986 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
987 if (!AugLoad_singleton) return 0;
988 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
989 if (!AugStore_type) return 0;
990 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
991 if (!AugStore_singleton) return 0;
992 Param_type = make_type("Param", expr_context_type, NULL, 0);
993 if (!Param_type) return 0;
994 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
995 if (!Param_singleton) return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700996 NamedStore_type = make_type("NamedStore", expr_context_type, NULL, 0);
997 if (!NamedStore_type) return 0;
998 NamedStore_singleton = PyType_GenericNew(NamedStore_type, NULL, NULL);
999 if (!NamedStore_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001000 slice_type = make_type("slice", &AST_type, NULL, 0);
1001 if (!slice_type) return 0;
1002 if (!add_attributes(slice_type, NULL, 0)) return 0;
1003 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1004 if (!Slice_type) return 0;
1005 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1006 if (!ExtSlice_type) return 0;
1007 Index_type = make_type("Index", slice_type, Index_fields, 1);
1008 if (!Index_type) return 0;
1009 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1010 if (!boolop_type) return 0;
1011 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1012 And_type = make_type("And", boolop_type, NULL, 0);
1013 if (!And_type) return 0;
1014 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1015 if (!And_singleton) return 0;
1016 Or_type = make_type("Or", boolop_type, NULL, 0);
1017 if (!Or_type) return 0;
1018 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1019 if (!Or_singleton) return 0;
1020 operator_type = make_type("operator", &AST_type, NULL, 0);
1021 if (!operator_type) return 0;
1022 if (!add_attributes(operator_type, NULL, 0)) return 0;
1023 Add_type = make_type("Add", operator_type, NULL, 0);
1024 if (!Add_type) return 0;
1025 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1026 if (!Add_singleton) return 0;
1027 Sub_type = make_type("Sub", operator_type, NULL, 0);
1028 if (!Sub_type) return 0;
1029 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1030 if (!Sub_singleton) return 0;
1031 Mult_type = make_type("Mult", operator_type, NULL, 0);
1032 if (!Mult_type) return 0;
1033 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1034 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001035 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1036 if (!MatMult_type) return 0;
1037 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1038 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001039 Div_type = make_type("Div", operator_type, NULL, 0);
1040 if (!Div_type) return 0;
1041 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1042 if (!Div_singleton) return 0;
1043 Mod_type = make_type("Mod", operator_type, NULL, 0);
1044 if (!Mod_type) return 0;
1045 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1046 if (!Mod_singleton) return 0;
1047 Pow_type = make_type("Pow", operator_type, NULL, 0);
1048 if (!Pow_type) return 0;
1049 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1050 if (!Pow_singleton) return 0;
1051 LShift_type = make_type("LShift", operator_type, NULL, 0);
1052 if (!LShift_type) return 0;
1053 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1054 if (!LShift_singleton) return 0;
1055 RShift_type = make_type("RShift", operator_type, NULL, 0);
1056 if (!RShift_type) return 0;
1057 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1058 if (!RShift_singleton) return 0;
1059 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1060 if (!BitOr_type) return 0;
1061 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1062 if (!BitOr_singleton) return 0;
1063 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1064 if (!BitXor_type) return 0;
1065 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1066 if (!BitXor_singleton) return 0;
1067 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1068 if (!BitAnd_type) return 0;
1069 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1070 if (!BitAnd_singleton) return 0;
1071 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1072 if (!FloorDiv_type) return 0;
1073 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1074 if (!FloorDiv_singleton) return 0;
1075 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1076 if (!unaryop_type) return 0;
1077 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1078 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1079 if (!Invert_type) return 0;
1080 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1081 if (!Invert_singleton) return 0;
1082 Not_type = make_type("Not", unaryop_type, NULL, 0);
1083 if (!Not_type) return 0;
1084 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1085 if (!Not_singleton) return 0;
1086 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1087 if (!UAdd_type) return 0;
1088 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1089 if (!UAdd_singleton) return 0;
1090 USub_type = make_type("USub", unaryop_type, NULL, 0);
1091 if (!USub_type) return 0;
1092 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1093 if (!USub_singleton) return 0;
1094 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1095 if (!cmpop_type) return 0;
1096 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1097 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1098 if (!Eq_type) return 0;
1099 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1100 if (!Eq_singleton) return 0;
1101 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1102 if (!NotEq_type) return 0;
1103 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1104 if (!NotEq_singleton) return 0;
1105 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1106 if (!Lt_type) return 0;
1107 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1108 if (!Lt_singleton) return 0;
1109 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1110 if (!LtE_type) return 0;
1111 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1112 if (!LtE_singleton) return 0;
1113 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1114 if (!Gt_type) return 0;
1115 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1116 if (!Gt_singleton) return 0;
1117 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1118 if (!GtE_type) return 0;
1119 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1120 if (!GtE_singleton) return 0;
1121 Is_type = make_type("Is", cmpop_type, NULL, 0);
1122 if (!Is_type) return 0;
1123 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1124 if (!Is_singleton) return 0;
1125 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1126 if (!IsNot_type) return 0;
1127 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1128 if (!IsNot_singleton) return 0;
1129 In_type = make_type("In", cmpop_type, NULL, 0);
1130 if (!In_type) return 0;
1131 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1132 if (!In_singleton) return 0;
1133 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1134 if (!NotIn_type) return 0;
1135 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1136 if (!NotIn_singleton) return 0;
1137 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001138 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001139 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001140 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001141 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1142 if (!excepthandler_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001143 if (!add_attributes(excepthandler_type, excepthandler_attributes, 4))
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001144 return 0;
1145 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1146 ExceptHandler_fields, 3);
1147 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001148 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001149 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001150 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001151 arg_type = make_type("arg", &AST_type, arg_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001152 if (!arg_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001153 if (!add_attributes(arg_type, arg_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1155 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001156 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001157 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1158 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001159 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001160 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1161 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001162 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001163 type_ignore_type = make_type("type_ignore", &AST_type, NULL, 0);
1164 if (!type_ignore_type) return 0;
1165 if (!add_attributes(type_ignore_type, NULL, 0)) return 0;
1166 TypeIgnore_type = make_type("TypeIgnore", type_ignore_type,
1167 TypeIgnore_fields, 1);
1168 if (!TypeIgnore_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001169 initialized = 1;
1170 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001171}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001172
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001173static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1174static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1175static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1176static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1177 arena);
1178static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1179static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1180static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1181static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1182static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1183static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1184 arena);
1185static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1186 arena);
1187static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1188static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1189static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1190static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001191static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001192static int obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena*
1193 arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195mod_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001196Module(asdl_seq * body, asdl_seq * type_ignores, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001198 mod_ty p;
1199 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1200 if (!p)
1201 return NULL;
1202 p->kind = Module_kind;
1203 p->v.Module.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001204 p->v.Module.type_ignores = type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001205 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206}
1207
1208mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001209Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001211 mod_ty p;
1212 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1213 if (!p)
1214 return NULL;
1215 p->kind = Interactive_kind;
1216 p->v.Interactive.body = body;
1217 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218}
1219
1220mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001221Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001223 mod_ty p;
1224 if (!body) {
1225 PyErr_SetString(PyExc_ValueError,
1226 "field body is required for Expression");
1227 return NULL;
1228 }
1229 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1230 if (!p)
1231 return NULL;
1232 p->kind = Expression_kind;
1233 p->v.Expression.body = body;
1234 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235}
1236
1237mod_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001238FunctionType(asdl_seq * argtypes, expr_ty returns, PyArena *arena)
1239{
1240 mod_ty p;
1241 if (!returns) {
1242 PyErr_SetString(PyExc_ValueError,
1243 "field returns is required for FunctionType");
1244 return NULL;
1245 }
1246 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1247 if (!p)
1248 return NULL;
1249 p->kind = FunctionType_kind;
1250 p->v.FunctionType.argtypes = argtypes;
1251 p->v.FunctionType.returns = returns;
1252 return p;
1253}
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 *
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001269 decorator_list, expr_ty returns, string type_comment, int lineno,
1270 int col_offset, int end_lineno, int end_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;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001292 p->v.FunctionDef.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001293 p->lineno = lineno;
1294 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001295 p->end_lineno = end_lineno;
1296 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001297 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298}
1299
1300stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001301AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001302 * decorator_list, expr_ty returns, string type_comment, int
1303 lineno, int col_offset, int end_lineno, int end_col_offset,
1304 PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001305{
1306 stmt_ty p;
1307 if (!name) {
1308 PyErr_SetString(PyExc_ValueError,
1309 "field name is required for AsyncFunctionDef");
1310 return NULL;
1311 }
1312 if (!args) {
1313 PyErr_SetString(PyExc_ValueError,
1314 "field args is required for AsyncFunctionDef");
1315 return NULL;
1316 }
1317 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1318 if (!p)
1319 return NULL;
1320 p->kind = AsyncFunctionDef_kind;
1321 p->v.AsyncFunctionDef.name = name;
1322 p->v.AsyncFunctionDef.args = args;
1323 p->v.AsyncFunctionDef.body = body;
1324 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1325 p->v.AsyncFunctionDef.returns = returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001326 p->v.AsyncFunctionDef.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001327 p->lineno = lineno;
1328 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001329 p->end_lineno = end_lineno;
1330 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001331 return p;
1332}
1333
1334stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001335ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001336 body, asdl_seq * decorator_list, int lineno, int col_offset, int
1337 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001339 stmt_ty p;
1340 if (!name) {
1341 PyErr_SetString(PyExc_ValueError,
1342 "field name is required for ClassDef");
1343 return NULL;
1344 }
1345 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1346 if (!p)
1347 return NULL;
1348 p->kind = ClassDef_kind;
1349 p->v.ClassDef.name = name;
1350 p->v.ClassDef.bases = bases;
1351 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001352 p->v.ClassDef.body = body;
1353 p->v.ClassDef.decorator_list = decorator_list;
1354 p->lineno = lineno;
1355 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001356 p->end_lineno = end_lineno;
1357 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001358 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359}
1360
1361stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001362Return(expr_ty value, int lineno, int col_offset, int end_lineno, int
1363 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001365 stmt_ty p;
1366 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1367 if (!p)
1368 return NULL;
1369 p->kind = Return_kind;
1370 p->v.Return.value = value;
1371 p->lineno = lineno;
1372 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001373 p->end_lineno = end_lineno;
1374 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001375 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376}
1377
1378stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001379Delete(asdl_seq * targets, int lineno, int col_offset, int end_lineno, int
1380 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001382 stmt_ty p;
1383 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1384 if (!p)
1385 return NULL;
1386 p->kind = Delete_kind;
1387 p->v.Delete.targets = targets;
1388 p->lineno = lineno;
1389 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001390 p->end_lineno = end_lineno;
1391 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001392 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393}
1394
1395stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001396Assign(asdl_seq * targets, expr_ty value, string type_comment, int lineno, int
1397 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001399 stmt_ty p;
1400 if (!value) {
1401 PyErr_SetString(PyExc_ValueError,
1402 "field value is required for Assign");
1403 return NULL;
1404 }
1405 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1406 if (!p)
1407 return NULL;
1408 p->kind = Assign_kind;
1409 p->v.Assign.targets = targets;
1410 p->v.Assign.value = value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001411 p->v.Assign.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001412 p->lineno = lineno;
1413 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001414 p->end_lineno = end_lineno;
1415 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001416 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417}
1418
1419stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001420AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001421 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001423 stmt_ty p;
1424 if (!target) {
1425 PyErr_SetString(PyExc_ValueError,
1426 "field target is required for AugAssign");
1427 return NULL;
1428 }
1429 if (!op) {
1430 PyErr_SetString(PyExc_ValueError,
1431 "field op is required for AugAssign");
1432 return NULL;
1433 }
1434 if (!value) {
1435 PyErr_SetString(PyExc_ValueError,
1436 "field value is required for AugAssign");
1437 return NULL;
1438 }
1439 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1440 if (!p)
1441 return NULL;
1442 p->kind = AugAssign_kind;
1443 p->v.AugAssign.target = target;
1444 p->v.AugAssign.op = op;
1445 p->v.AugAssign.value = value;
1446 p->lineno = lineno;
1447 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001448 p->end_lineno = end_lineno;
1449 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001450 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451}
1452
1453stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001454AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001455 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1456 *arena)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001457{
1458 stmt_ty p;
1459 if (!target) {
1460 PyErr_SetString(PyExc_ValueError,
1461 "field target is required for AnnAssign");
1462 return NULL;
1463 }
1464 if (!annotation) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "field annotation is required for AnnAssign");
1467 return NULL;
1468 }
1469 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1470 if (!p)
1471 return NULL;
1472 p->kind = AnnAssign_kind;
1473 p->v.AnnAssign.target = target;
1474 p->v.AnnAssign.annotation = annotation;
1475 p->v.AnnAssign.value = value;
1476 p->v.AnnAssign.simple = simple;
1477 p->lineno = lineno;
1478 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001479 p->end_lineno = end_lineno;
1480 p->end_col_offset = end_col_offset;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001481 return p;
1482}
1483
1484stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001485For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, string
1486 type_comment, int lineno, int col_offset, int end_lineno, int
1487 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001489 stmt_ty p;
1490 if (!target) {
1491 PyErr_SetString(PyExc_ValueError,
1492 "field target is required for For");
1493 return NULL;
1494 }
1495 if (!iter) {
1496 PyErr_SetString(PyExc_ValueError,
1497 "field iter is required for For");
1498 return NULL;
1499 }
1500 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1501 if (!p)
1502 return NULL;
1503 p->kind = For_kind;
1504 p->v.For.target = target;
1505 p->v.For.iter = iter;
1506 p->v.For.body = body;
1507 p->v.For.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001508 p->v.For.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001509 p->lineno = lineno;
1510 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001511 p->end_lineno = end_lineno;
1512 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001513 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001517AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse,
1518 string type_comment, int lineno, int col_offset, int end_lineno, int
1519 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001520{
1521 stmt_ty p;
1522 if (!target) {
1523 PyErr_SetString(PyExc_ValueError,
1524 "field target is required for AsyncFor");
1525 return NULL;
1526 }
1527 if (!iter) {
1528 PyErr_SetString(PyExc_ValueError,
1529 "field iter is required for AsyncFor");
1530 return NULL;
1531 }
1532 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1533 if (!p)
1534 return NULL;
1535 p->kind = AsyncFor_kind;
1536 p->v.AsyncFor.target = target;
1537 p->v.AsyncFor.iter = iter;
1538 p->v.AsyncFor.body = body;
1539 p->v.AsyncFor.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001540 p->v.AsyncFor.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001541 p->lineno = lineno;
1542 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001543 p->end_lineno = end_lineno;
1544 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001545 return p;
1546}
1547
1548stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001549While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001550 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001552 stmt_ty p;
1553 if (!test) {
1554 PyErr_SetString(PyExc_ValueError,
1555 "field test is required for While");
1556 return NULL;
1557 }
1558 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1559 if (!p)
1560 return NULL;
1561 p->kind = While_kind;
1562 p->v.While.test = test;
1563 p->v.While.body = body;
1564 p->v.While.orelse = orelse;
1565 p->lineno = lineno;
1566 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001567 p->end_lineno = end_lineno;
1568 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001569 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570}
1571
1572stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001573If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001574 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001576 stmt_ty p;
1577 if (!test) {
1578 PyErr_SetString(PyExc_ValueError,
1579 "field test is required for If");
1580 return NULL;
1581 }
1582 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1583 if (!p)
1584 return NULL;
1585 p->kind = If_kind;
1586 p->v.If.test = test;
1587 p->v.If.body = body;
1588 p->v.If.orelse = orelse;
1589 p->lineno = lineno;
1590 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001591 p->end_lineno = end_lineno;
1592 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001593 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001597With(asdl_seq * items, asdl_seq * body, string type_comment, int lineno, int
1598 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001599{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001600 stmt_ty p;
1601 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1602 if (!p)
1603 return NULL;
1604 p->kind = With_kind;
1605 p->v.With.items = items;
1606 p->v.With.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001607 p->v.With.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001608 p->lineno = lineno;
1609 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001610 p->end_lineno = end_lineno;
1611 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001612 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001613}
1614
1615stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001616AsyncWith(asdl_seq * items, asdl_seq * body, string type_comment, int lineno,
1617 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001618{
1619 stmt_ty p;
1620 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1621 if (!p)
1622 return NULL;
1623 p->kind = AsyncWith_kind;
1624 p->v.AsyncWith.items = items;
1625 p->v.AsyncWith.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001626 p->v.AsyncWith.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001627 p->lineno = lineno;
1628 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001629 p->end_lineno = end_lineno;
1630 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001631 return p;
1632}
1633
1634stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001635Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, int end_lineno,
1636 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001638 stmt_ty p;
1639 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1640 if (!p)
1641 return NULL;
1642 p->kind = Raise_kind;
1643 p->v.Raise.exc = exc;
1644 p->v.Raise.cause = cause;
1645 p->lineno = lineno;
1646 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001647 p->end_lineno = end_lineno;
1648 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001649 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001653Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001654 finalbody, int lineno, int col_offset, int end_lineno, int end_col_offset,
1655 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001657 stmt_ty p;
1658 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1659 if (!p)
1660 return NULL;
1661 p->kind = Try_kind;
1662 p->v.Try.body = body;
1663 p->v.Try.handlers = handlers;
1664 p->v.Try.orelse = orelse;
1665 p->v.Try.finalbody = finalbody;
1666 p->lineno = lineno;
1667 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001668 p->end_lineno = end_lineno;
1669 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001670 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671}
1672
1673stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001674Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, int end_lineno,
1675 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001677 stmt_ty p;
1678 if (!test) {
1679 PyErr_SetString(PyExc_ValueError,
1680 "field test is required for Assert");
1681 return NULL;
1682 }
1683 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1684 if (!p)
1685 return NULL;
1686 p->kind = Assert_kind;
1687 p->v.Assert.test = test;
1688 p->v.Assert.msg = msg;
1689 p->lineno = lineno;
1690 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001691 p->end_lineno = end_lineno;
1692 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001693 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694}
1695
1696stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001697Import(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1698 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001700 stmt_ty p;
1701 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1702 if (!p)
1703 return NULL;
1704 p->kind = Import_kind;
1705 p->v.Import.names = names;
1706 p->lineno = lineno;
1707 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001708 p->end_lineno = end_lineno;
1709 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001710 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711}
1712
1713stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001714ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001715 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001717 stmt_ty p;
1718 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1719 if (!p)
1720 return NULL;
1721 p->kind = ImportFrom_kind;
1722 p->v.ImportFrom.module = module;
1723 p->v.ImportFrom.names = names;
1724 p->v.ImportFrom.level = level;
1725 p->lineno = lineno;
1726 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001727 p->end_lineno = end_lineno;
1728 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001729 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730}
1731
1732stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001733Global(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1734 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001736 stmt_ty p;
1737 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1738 if (!p)
1739 return NULL;
1740 p->kind = Global_kind;
1741 p->v.Global.names = names;
1742 p->lineno = lineno;
1743 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001744 p->end_lineno = end_lineno;
1745 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001746 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747}
1748
1749stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001750Nonlocal(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1751 end_col_offset, PyArena *arena)
Jeremy Hylton81e95022007-02-27 06:50:52 +00001752{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001753 stmt_ty p;
1754 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1755 if (!p)
1756 return NULL;
1757 p->kind = Nonlocal_kind;
1758 p->v.Nonlocal.names = names;
1759 p->lineno = lineno;
1760 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001761 p->end_lineno = end_lineno;
1762 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001763 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001764}
1765
1766stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001767Expr(expr_ty value, int lineno, int col_offset, int end_lineno, int
1768 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001770 stmt_ty p;
1771 if (!value) {
1772 PyErr_SetString(PyExc_ValueError,
1773 "field value is required for Expr");
1774 return NULL;
1775 }
1776 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1777 if (!p)
1778 return NULL;
1779 p->kind = Expr_kind;
1780 p->v.Expr.value = value;
1781 p->lineno = lineno;
1782 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001783 p->end_lineno = end_lineno;
1784 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001785 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786}
1787
1788stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001789Pass(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1790 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001792 stmt_ty p;
1793 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1794 if (!p)
1795 return NULL;
1796 p->kind = Pass_kind;
1797 p->lineno = lineno;
1798 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001799 p->end_lineno = end_lineno;
1800 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001801 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802}
1803
1804stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001805Break(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1806 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001808 stmt_ty p;
1809 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1810 if (!p)
1811 return NULL;
1812 p->kind = Break_kind;
1813 p->lineno = lineno;
1814 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001815 p->end_lineno = end_lineno;
1816 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001817 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818}
1819
1820stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001821Continue(int lineno, int col_offset, int end_lineno, int end_col_offset,
1822 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001824 stmt_ty p;
1825 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1826 if (!p)
1827 return NULL;
1828 p->kind = Continue_kind;
1829 p->lineno = lineno;
1830 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001831 p->end_lineno = end_lineno;
1832 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001833 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834}
1835
1836expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001837BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, int
1838 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001840 expr_ty p;
1841 if (!op) {
1842 PyErr_SetString(PyExc_ValueError,
1843 "field op is required for BoolOp");
1844 return NULL;
1845 }
1846 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1847 if (!p)
1848 return NULL;
1849 p->kind = BoolOp_kind;
1850 p->v.BoolOp.op = op;
1851 p->v.BoolOp.values = values;
1852 p->lineno = lineno;
1853 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001854 p->end_lineno = end_lineno;
1855 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001856 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857}
1858
1859expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001860NamedExpr(expr_ty target, expr_ty value, int lineno, int col_offset, int
1861 end_lineno, int end_col_offset, PyArena *arena)
1862{
1863 expr_ty p;
1864 if (!target) {
1865 PyErr_SetString(PyExc_ValueError,
1866 "field target is required for NamedExpr");
1867 return NULL;
1868 }
1869 if (!value) {
1870 PyErr_SetString(PyExc_ValueError,
1871 "field value is required for NamedExpr");
1872 return NULL;
1873 }
1874 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1875 if (!p)
1876 return NULL;
1877 p->kind = NamedExpr_kind;
1878 p->v.NamedExpr.target = target;
1879 p->v.NamedExpr.value = value;
1880 p->lineno = lineno;
1881 p->col_offset = col_offset;
1882 p->end_lineno = end_lineno;
1883 p->end_col_offset = end_col_offset;
1884 return p;
1885}
1886
1887expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001888BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001889 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001891 expr_ty p;
1892 if (!left) {
1893 PyErr_SetString(PyExc_ValueError,
1894 "field left is required for BinOp");
1895 return NULL;
1896 }
1897 if (!op) {
1898 PyErr_SetString(PyExc_ValueError,
1899 "field op is required for BinOp");
1900 return NULL;
1901 }
1902 if (!right) {
1903 PyErr_SetString(PyExc_ValueError,
1904 "field right is required for BinOp");
1905 return NULL;
1906 }
1907 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1908 if (!p)
1909 return NULL;
1910 p->kind = BinOp_kind;
1911 p->v.BinOp.left = left;
1912 p->v.BinOp.op = op;
1913 p->v.BinOp.right = right;
1914 p->lineno = lineno;
1915 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001916 p->end_lineno = end_lineno;
1917 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001918 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
1921expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001922UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, int
1923 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001925 expr_ty p;
1926 if (!op) {
1927 PyErr_SetString(PyExc_ValueError,
1928 "field op is required for UnaryOp");
1929 return NULL;
1930 }
1931 if (!operand) {
1932 PyErr_SetString(PyExc_ValueError,
1933 "field operand is required for UnaryOp");
1934 return NULL;
1935 }
1936 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1937 if (!p)
1938 return NULL;
1939 p->kind = UnaryOp_kind;
1940 p->v.UnaryOp.op = op;
1941 p->v.UnaryOp.operand = operand;
1942 p->lineno = lineno;
1943 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001944 p->end_lineno = end_lineno;
1945 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001946 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947}
1948
1949expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001950Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, int
1951 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001953 expr_ty p;
1954 if (!args) {
1955 PyErr_SetString(PyExc_ValueError,
1956 "field args is required for Lambda");
1957 return NULL;
1958 }
1959 if (!body) {
1960 PyErr_SetString(PyExc_ValueError,
1961 "field body is required for Lambda");
1962 return NULL;
1963 }
1964 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1965 if (!p)
1966 return NULL;
1967 p->kind = Lambda_kind;
1968 p->v.Lambda.args = args;
1969 p->v.Lambda.body = body;
1970 p->lineno = lineno;
1971 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001972 p->end_lineno = end_lineno;
1973 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001974 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975}
1976
1977expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001978IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001979 int end_lineno, int end_col_offset, PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001980{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001981 expr_ty p;
1982 if (!test) {
1983 PyErr_SetString(PyExc_ValueError,
1984 "field test is required for IfExp");
1985 return NULL;
1986 }
1987 if (!body) {
1988 PyErr_SetString(PyExc_ValueError,
1989 "field body is required for IfExp");
1990 return NULL;
1991 }
1992 if (!orelse) {
1993 PyErr_SetString(PyExc_ValueError,
1994 "field orelse is required for IfExp");
1995 return NULL;
1996 }
1997 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1998 if (!p)
1999 return NULL;
2000 p->kind = IfExp_kind;
2001 p->v.IfExp.test = test;
2002 p->v.IfExp.body = body;
2003 p->v.IfExp.orelse = orelse;
2004 p->lineno = lineno;
2005 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002006 p->end_lineno = end_lineno;
2007 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002008 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002009}
2010
2011expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002012Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, int
2013 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002015 expr_ty p;
2016 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2017 if (!p)
2018 return NULL;
2019 p->kind = Dict_kind;
2020 p->v.Dict.keys = keys;
2021 p->v.Dict.values = values;
2022 p->lineno = lineno;
2023 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002024 p->end_lineno = end_lineno;
2025 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002026 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027}
2028
2029expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002030Set(asdl_seq * elts, int lineno, int col_offset, int end_lineno, int
2031 end_col_offset, PyArena *arena)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002032{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002033 expr_ty p;
2034 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2035 if (!p)
2036 return NULL;
2037 p->kind = Set_kind;
2038 p->v.Set.elts = elts;
2039 p->lineno = lineno;
2040 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002041 p->end_lineno = end_lineno;
2042 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002043 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002044}
2045
2046expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002047ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2048 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002050 expr_ty p;
2051 if (!elt) {
2052 PyErr_SetString(PyExc_ValueError,
2053 "field elt is required for ListComp");
2054 return NULL;
2055 }
2056 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2057 if (!p)
2058 return NULL;
2059 p->kind = ListComp_kind;
2060 p->v.ListComp.elt = elt;
2061 p->v.ListComp.generators = generators;
2062 p->lineno = lineno;
2063 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002064 p->end_lineno = end_lineno;
2065 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002066 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067}
2068
2069expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002070SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2071 end_lineno, int end_col_offset, PyArena *arena)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002072{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002073 expr_ty p;
2074 if (!elt) {
2075 PyErr_SetString(PyExc_ValueError,
2076 "field elt is required for SetComp");
2077 return NULL;
2078 }
2079 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2080 if (!p)
2081 return NULL;
2082 p->kind = SetComp_kind;
2083 p->v.SetComp.elt = elt;
2084 p->v.SetComp.generators = generators;
2085 p->lineno = lineno;
2086 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002087 p->end_lineno = end_lineno;
2088 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002089 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090}
2091
2092expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002094 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossum992d4a32007-07-11 13:09:30 +00002095{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002096 expr_ty p;
2097 if (!key) {
2098 PyErr_SetString(PyExc_ValueError,
2099 "field key is required for DictComp");
2100 return NULL;
2101 }
2102 if (!value) {
2103 PyErr_SetString(PyExc_ValueError,
2104 "field value is required for DictComp");
2105 return NULL;
2106 }
2107 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2108 if (!p)
2109 return NULL;
2110 p->kind = DictComp_kind;
2111 p->v.DictComp.key = key;
2112 p->v.DictComp.value = value;
2113 p->v.DictComp.generators = generators;
2114 p->lineno = lineno;
2115 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002116 p->end_lineno = end_lineno;
2117 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002118 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002119}
2120
2121expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002122GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002123 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002125 expr_ty p;
2126 if (!elt) {
2127 PyErr_SetString(PyExc_ValueError,
2128 "field elt is required for GeneratorExp");
2129 return NULL;
2130 }
2131 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2132 if (!p)
2133 return NULL;
2134 p->kind = GeneratorExp_kind;
2135 p->v.GeneratorExp.elt = elt;
2136 p->v.GeneratorExp.generators = generators;
2137 p->lineno = lineno;
2138 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002139 p->end_lineno = end_lineno;
2140 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002141 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142}
2143
2144expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002145Await(expr_ty value, int lineno, int col_offset, int end_lineno, int
2146 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002147{
2148 expr_ty p;
2149 if (!value) {
2150 PyErr_SetString(PyExc_ValueError,
2151 "field value is required for Await");
2152 return NULL;
2153 }
2154 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2155 if (!p)
2156 return NULL;
2157 p->kind = Await_kind;
2158 p->v.Await.value = value;
2159 p->lineno = lineno;
2160 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002161 p->end_lineno = end_lineno;
2162 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002163 return p;
2164}
2165
2166expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002167Yield(expr_ty value, int lineno, int col_offset, int end_lineno, int
2168 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002170 expr_ty p;
2171 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2172 if (!p)
2173 return NULL;
2174 p->kind = Yield_kind;
2175 p->v.Yield.value = value;
2176 p->lineno = lineno;
2177 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002178 p->end_lineno = end_lineno;
2179 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002180 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181}
2182
2183expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002184YieldFrom(expr_ty value, int lineno, int col_offset, int end_lineno, int
2185 end_col_offset, PyArena *arena)
Benjamin Peterson527c6222012-01-14 08:58:23 -05002186{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002187 expr_ty p;
2188 if (!value) {
2189 PyErr_SetString(PyExc_ValueError,
2190 "field value is required for YieldFrom");
2191 return NULL;
2192 }
2193 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2194 if (!p)
2195 return NULL;
2196 p->kind = YieldFrom_kind;
2197 p->v.YieldFrom.value = value;
2198 p->lineno = lineno;
2199 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002200 p->end_lineno = end_lineno;
2201 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002202 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002203}
2204
2205expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002207 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002209 expr_ty p;
2210 if (!left) {
2211 PyErr_SetString(PyExc_ValueError,
2212 "field left is required for Compare");
2213 return NULL;
2214 }
2215 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2216 if (!p)
2217 return NULL;
2218 p->kind = Compare_kind;
2219 p->v.Compare.left = left;
2220 p->v.Compare.ops = ops;
2221 p->v.Compare.comparators = comparators;
2222 p->lineno = lineno;
2223 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002224 p->end_lineno = end_lineno;
2225 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002226 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227}
2228
2229expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002230Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002231 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002233 expr_ty p;
2234 if (!func) {
2235 PyErr_SetString(PyExc_ValueError,
2236 "field func is required for Call");
2237 return NULL;
2238 }
2239 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2240 if (!p)
2241 return NULL;
2242 p->kind = Call_kind;
2243 p->v.Call.func = func;
2244 p->v.Call.args = args;
2245 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002246 p->lineno = lineno;
2247 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002248 p->end_lineno = end_lineno;
2249 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
2253expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002254FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002255 int col_offset, int end_lineno, int end_col_offset, PyArena
2256 *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04002257{
2258 expr_ty p;
2259 if (!value) {
2260 PyErr_SetString(PyExc_ValueError,
2261 "field value is required for FormattedValue");
2262 return NULL;
2263 }
2264 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2265 if (!p)
2266 return NULL;
2267 p->kind = FormattedValue_kind;
2268 p->v.FormattedValue.value = value;
2269 p->v.FormattedValue.conversion = conversion;
2270 p->v.FormattedValue.format_spec = format_spec;
2271 p->lineno = lineno;
2272 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002273 p->end_lineno = end_lineno;
2274 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04002275 return p;
2276}
2277
2278expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002279JoinedStr(asdl_seq * values, int lineno, int col_offset, int end_lineno, int
2280 end_col_offset, PyArena *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04002281{
2282 expr_ty p;
2283 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2284 if (!p)
2285 return NULL;
2286 p->kind = JoinedStr_kind;
2287 p->v.JoinedStr.values = values;
2288 p->lineno = lineno;
2289 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002290 p->end_lineno = end_lineno;
2291 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04002292 return p;
2293}
2294
2295expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002296Constant(constant value, int lineno, int col_offset, int end_lineno, int
2297 end_col_offset, PyArena *arena)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002298{
2299 expr_ty p;
2300 if (!value) {
2301 PyErr_SetString(PyExc_ValueError,
2302 "field value is required for Constant");
2303 return NULL;
2304 }
2305 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2306 if (!p)
2307 return NULL;
2308 p->kind = Constant_kind;
2309 p->v.Constant.value = value;
2310 p->lineno = lineno;
2311 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002312 p->end_lineno = end_lineno;
2313 p->end_col_offset = end_col_offset;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002314 return p;
2315}
2316
2317expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002318Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002319 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002321 expr_ty p;
2322 if (!value) {
2323 PyErr_SetString(PyExc_ValueError,
2324 "field value is required for Attribute");
2325 return NULL;
2326 }
2327 if (!attr) {
2328 PyErr_SetString(PyExc_ValueError,
2329 "field attr is required for Attribute");
2330 return NULL;
2331 }
2332 if (!ctx) {
2333 PyErr_SetString(PyExc_ValueError,
2334 "field ctx is required for Attribute");
2335 return NULL;
2336 }
2337 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2338 if (!p)
2339 return NULL;
2340 p->kind = Attribute_kind;
2341 p->v.Attribute.value = value;
2342 p->v.Attribute.attr = attr;
2343 p->v.Attribute.ctx = ctx;
2344 p->lineno = lineno;
2345 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002346 p->end_lineno = end_lineno;
2347 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002348 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349}
2350
2351expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002352Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002353 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002355 expr_ty p;
2356 if (!value) {
2357 PyErr_SetString(PyExc_ValueError,
2358 "field value is required for Subscript");
2359 return NULL;
2360 }
2361 if (!slice) {
2362 PyErr_SetString(PyExc_ValueError,
2363 "field slice is required for Subscript");
2364 return NULL;
2365 }
2366 if (!ctx) {
2367 PyErr_SetString(PyExc_ValueError,
2368 "field ctx is required for Subscript");
2369 return NULL;
2370 }
2371 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2372 if (!p)
2373 return NULL;
2374 p->kind = Subscript_kind;
2375 p->v.Subscript.value = value;
2376 p->v.Subscript.slice = slice;
2377 p->v.Subscript.ctx = ctx;
2378 p->lineno = lineno;
2379 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002380 p->end_lineno = end_lineno;
2381 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002382 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383}
2384
2385expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002386Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, int
2387 end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossum0368b722007-05-11 16:50:42 +00002388{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002389 expr_ty p;
2390 if (!value) {
2391 PyErr_SetString(PyExc_ValueError,
2392 "field value is required for Starred");
2393 return NULL;
2394 }
2395 if (!ctx) {
2396 PyErr_SetString(PyExc_ValueError,
2397 "field ctx is required for Starred");
2398 return NULL;
2399 }
2400 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2401 if (!p)
2402 return NULL;
2403 p->kind = Starred_kind;
2404 p->v.Starred.value = value;
2405 p->v.Starred.ctx = ctx;
2406 p->lineno = lineno;
2407 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002408 p->end_lineno = end_lineno;
2409 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002410 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002411}
2412
2413expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002414Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, int
2415 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002417 expr_ty p;
2418 if (!id) {
2419 PyErr_SetString(PyExc_ValueError,
2420 "field id is required for Name");
2421 return NULL;
2422 }
2423 if (!ctx) {
2424 PyErr_SetString(PyExc_ValueError,
2425 "field ctx is required for Name");
2426 return NULL;
2427 }
2428 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2429 if (!p)
2430 return NULL;
2431 p->kind = Name_kind;
2432 p->v.Name.id = id;
2433 p->v.Name.ctx = ctx;
2434 p->lineno = lineno;
2435 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002436 p->end_lineno = end_lineno;
2437 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002438 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439}
2440
2441expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002442List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
2443 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002445 expr_ty p;
2446 if (!ctx) {
2447 PyErr_SetString(PyExc_ValueError,
2448 "field ctx is required for List");
2449 return NULL;
2450 }
2451 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2452 if (!p)
2453 return NULL;
2454 p->kind = List_kind;
2455 p->v.List.elts = elts;
2456 p->v.List.ctx = ctx;
2457 p->lineno = lineno;
2458 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002459 p->end_lineno = end_lineno;
2460 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002461 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
2464expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002465Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
2466 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002468 expr_ty p;
2469 if (!ctx) {
2470 PyErr_SetString(PyExc_ValueError,
2471 "field ctx is required for Tuple");
2472 return NULL;
2473 }
2474 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2475 if (!p)
2476 return NULL;
2477 p->kind = Tuple_kind;
2478 p->v.Tuple.elts = elts;
2479 p->v.Tuple.ctx = ctx;
2480 p->lineno = lineno;
2481 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002482 p->end_lineno = end_lineno;
2483 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002484 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485}
2486
2487slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002488Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002490 slice_ty p;
2491 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2492 if (!p)
2493 return NULL;
2494 p->kind = Slice_kind;
2495 p->v.Slice.lower = lower;
2496 p->v.Slice.upper = upper;
2497 p->v.Slice.step = step;
2498 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499}
2500
2501slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002502ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002504 slice_ty p;
2505 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2506 if (!p)
2507 return NULL;
2508 p->kind = ExtSlice_kind;
2509 p->v.ExtSlice.dims = dims;
2510 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511}
2512
2513slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002514Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002516 slice_ty p;
2517 if (!value) {
2518 PyErr_SetString(PyExc_ValueError,
2519 "field value is required for Index");
2520 return NULL;
2521 }
2522 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2523 if (!p)
2524 return NULL;
2525 p->kind = Index_kind;
2526 p->v.Index.value = value;
2527 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528}
2529
2530comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002531comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2532 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002534 comprehension_ty p;
2535 if (!target) {
2536 PyErr_SetString(PyExc_ValueError,
2537 "field target is required for comprehension");
2538 return NULL;
2539 }
2540 if (!iter) {
2541 PyErr_SetString(PyExc_ValueError,
2542 "field iter is required for comprehension");
2543 return NULL;
2544 }
2545 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2546 if (!p)
2547 return NULL;
2548 p->target = target;
2549 p->iter = iter;
2550 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002551 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002552 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553}
2554
2555excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002556ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002557 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002559 excepthandler_ty p;
2560 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2561 if (!p)
2562 return NULL;
2563 p->kind = ExceptHandler_kind;
2564 p->v.ExceptHandler.type = type;
2565 p->v.ExceptHandler.name = name;
2566 p->v.ExceptHandler.body = body;
2567 p->lineno = lineno;
2568 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002569 p->end_lineno = end_lineno;
2570 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002571 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572}
2573
2574arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002575arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2576 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002578 arguments_ty p;
2579 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2580 if (!p)
2581 return NULL;
2582 p->args = args;
2583 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002584 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002585 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002586 p->kwarg = kwarg;
2587 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002588 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589}
2590
Neal Norwitzc1505362006-12-28 06:47:50 +00002591arg_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002592arg(identifier arg, expr_ty annotation, string type_comment, int lineno, int
2593 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002594{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002595 arg_ty p;
2596 if (!arg) {
2597 PyErr_SetString(PyExc_ValueError,
2598 "field arg is required for arg");
2599 return NULL;
2600 }
2601 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2602 if (!p)
2603 return NULL;
2604 p->arg = arg;
2605 p->annotation = annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002606 p->type_comment = type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01002607 p->lineno = lineno;
2608 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002609 p->end_lineno = end_lineno;
2610 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002611 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002612}
2613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002615keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002617 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002618 if (!value) {
2619 PyErr_SetString(PyExc_ValueError,
2620 "field value is required for keyword");
2621 return NULL;
2622 }
2623 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2624 if (!p)
2625 return NULL;
2626 p->arg = arg;
2627 p->value = value;
2628 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629}
2630
2631alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002632alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002634 alias_ty p;
2635 if (!name) {
2636 PyErr_SetString(PyExc_ValueError,
2637 "field name is required for alias");
2638 return NULL;
2639 }
2640 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2641 if (!p)
2642 return NULL;
2643 p->name = name;
2644 p->asname = asname;
2645 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646}
2647
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002648withitem_ty
2649withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2650{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002651 withitem_ty p;
2652 if (!context_expr) {
2653 PyErr_SetString(PyExc_ValueError,
2654 "field context_expr is required for withitem");
2655 return NULL;
2656 }
2657 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2658 if (!p)
2659 return NULL;
2660 p->context_expr = context_expr;
2661 p->optional_vars = optional_vars;
2662 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002663}
2664
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002665type_ignore_ty
2666TypeIgnore(int lineno, PyArena *arena)
2667{
2668 type_ignore_ty p;
2669 p = (type_ignore_ty)PyArena_Malloc(arena, sizeof(*p));
2670 if (!p)
2671 return NULL;
2672 p->kind = TypeIgnore_kind;
2673 p->v.TypeIgnore.lineno = lineno;
2674 return p;
2675}
2676
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002677
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002678PyObject*
2679ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002680{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002681 mod_ty o = (mod_ty)_o;
2682 PyObject *result = NULL, *value = NULL;
2683 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002684 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002685 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002686
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002687 switch (o->kind) {
2688 case Module_kind:
2689 result = PyType_GenericNew(Module_type, NULL, NULL);
2690 if (!result) goto failed;
2691 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2692 if (!value) goto failed;
2693 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2694 goto failed;
2695 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002696 value = ast2obj_list(o->v.Module.type_ignores, ast2obj_type_ignore);
2697 if (!value) goto failed;
2698 if (_PyObject_SetAttrId(result, &PyId_type_ignores, value) == -1)
2699 goto failed;
2700 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002701 break;
2702 case Interactive_kind:
2703 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2704 if (!result) goto failed;
2705 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2706 if (!value) goto failed;
2707 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2708 goto failed;
2709 Py_DECREF(value);
2710 break;
2711 case Expression_kind:
2712 result = PyType_GenericNew(Expression_type, NULL, NULL);
2713 if (!result) goto failed;
2714 value = ast2obj_expr(o->v.Expression.body);
2715 if (!value) goto failed;
2716 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
2719 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002720 case FunctionType_kind:
2721 result = PyType_GenericNew(FunctionType_type, NULL, NULL);
2722 if (!result) goto failed;
2723 value = ast2obj_list(o->v.FunctionType.argtypes, ast2obj_expr);
2724 if (!value) goto failed;
2725 if (_PyObject_SetAttrId(result, &PyId_argtypes, value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_expr(o->v.FunctionType.returns);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002734 case Suite_kind:
2735 result = PyType_GenericNew(Suite_type, NULL, NULL);
2736 if (!result) goto failed;
2737 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 break;
2743 }
2744 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002745failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002746 Py_XDECREF(value);
2747 Py_XDECREF(result);
2748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749}
2750
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002751PyObject*
2752ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002754 stmt_ty o = (stmt_ty)_o;
2755 PyObject *result = NULL, *value = NULL;
2756 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002757 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002758 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002759
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002760 switch (o->kind) {
2761 case FunctionDef_kind:
2762 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2763 if (!result) goto failed;
2764 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002765 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002766 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2767 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002768 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002769 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002770 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002771 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2772 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002773 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002774 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2775 if (!value) goto failed;
2776 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 value = ast2obj_expr(o->v.FunctionDef.returns);
2785 if (!value) goto failed;
2786 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002789 value = ast2obj_string(o->v.FunctionDef.type_comment);
2790 if (!value) goto failed;
2791 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2792 goto failed;
2793 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002794 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002795 case AsyncFunctionDef_kind:
2796 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2797 if (!result) goto failed;
2798 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2799 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2804 if (!value) goto failed;
2805 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2809 if (!value) goto failed;
2810 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2811 goto failed;
2812 Py_DECREF(value);
2813 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2814 ast2obj_expr);
2815 if (!value) goto failed;
2816 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2817 goto failed;
2818 Py_DECREF(value);
2819 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2820 if (!value) goto failed;
2821 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002824 value = ast2obj_string(o->v.AsyncFunctionDef.type_comment);
2825 if (!value) goto failed;
2826 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04002829 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002830 case ClassDef_kind:
2831 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2832 if (!result) goto failed;
2833 value = ast2obj_identifier(o->v.ClassDef.name);
2834 if (!value) goto failed;
2835 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2836 goto failed;
2837 Py_DECREF(value);
2838 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2839 if (!value) goto failed;
2840 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
2843 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2844 if (!value) goto failed;
2845 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002848 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2849 if (!value) goto failed;
2850 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2854 if (!value) goto failed;
2855 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2856 goto failed;
2857 Py_DECREF(value);
2858 break;
2859 case Return_kind:
2860 result = PyType_GenericNew(Return_type, NULL, NULL);
2861 if (!result) goto failed;
2862 value = ast2obj_expr(o->v.Return.value);
2863 if (!value) goto failed;
2864 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2865 goto failed;
2866 Py_DECREF(value);
2867 break;
2868 case Delete_kind:
2869 result = PyType_GenericNew(Delete_type, NULL, NULL);
2870 if (!result) goto failed;
2871 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2872 if (!value) goto failed;
2873 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
2876 break;
2877 case Assign_kind:
2878 result = PyType_GenericNew(Assign_type, NULL, NULL);
2879 if (!result) goto failed;
2880 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2881 if (!value) goto failed;
2882 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
2885 value = ast2obj_expr(o->v.Assign.value);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002890 value = ast2obj_string(o->v.Assign.type_comment);
2891 if (!value) goto failed;
2892 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2893 goto failed;
2894 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002895 break;
2896 case AugAssign_kind:
2897 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2898 if (!result) goto failed;
2899 value = ast2obj_expr(o->v.AugAssign.target);
2900 if (!value) goto failed;
2901 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2902 goto failed;
2903 Py_DECREF(value);
2904 value = ast2obj_operator(o->v.AugAssign.op);
2905 if (!value) goto failed;
2906 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2907 goto failed;
2908 Py_DECREF(value);
2909 value = ast2obj_expr(o->v.AugAssign.value);
2910 if (!value) goto failed;
2911 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2912 goto failed;
2913 Py_DECREF(value);
2914 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002915 case AnnAssign_kind:
2916 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2917 if (!result) goto failed;
2918 value = ast2obj_expr(o->v.AnnAssign.target);
2919 if (!value) goto failed;
2920 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2921 goto failed;
2922 Py_DECREF(value);
2923 value = ast2obj_expr(o->v.AnnAssign.annotation);
2924 if (!value) goto failed;
2925 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2926 goto failed;
2927 Py_DECREF(value);
2928 value = ast2obj_expr(o->v.AnnAssign.value);
2929 if (!value) goto failed;
2930 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2931 goto failed;
2932 Py_DECREF(value);
2933 value = ast2obj_int(o->v.AnnAssign.simple);
2934 if (!value) goto failed;
2935 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2936 goto failed;
2937 Py_DECREF(value);
2938 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002939 case For_kind:
2940 result = PyType_GenericNew(For_type, NULL, NULL);
2941 if (!result) goto failed;
2942 value = ast2obj_expr(o->v.For.target);
2943 if (!value) goto failed;
2944 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2945 goto failed;
2946 Py_DECREF(value);
2947 value = ast2obj_expr(o->v.For.iter);
2948 if (!value) goto failed;
2949 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2950 goto failed;
2951 Py_DECREF(value);
2952 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2953 if (!value) goto failed;
2954 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2955 goto failed;
2956 Py_DECREF(value);
2957 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2958 if (!value) goto failed;
2959 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2960 goto failed;
2961 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002962 value = ast2obj_string(o->v.For.type_comment);
2963 if (!value) goto failed;
2964 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2965 goto failed;
2966 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002967 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002968 case AsyncFor_kind:
2969 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2970 if (!result) goto failed;
2971 value = ast2obj_expr(o->v.AsyncFor.target);
2972 if (!value) goto failed;
2973 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2974 goto failed;
2975 Py_DECREF(value);
2976 value = ast2obj_expr(o->v.AsyncFor.iter);
2977 if (!value) goto failed;
2978 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2979 goto failed;
2980 Py_DECREF(value);
2981 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2982 if (!value) goto failed;
2983 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2984 goto failed;
2985 Py_DECREF(value);
2986 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2987 if (!value) goto failed;
2988 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2989 goto failed;
2990 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002991 value = ast2obj_string(o->v.AsyncFor.type_comment);
2992 if (!value) goto failed;
2993 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2994 goto failed;
2995 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04002996 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002997 case While_kind:
2998 result = PyType_GenericNew(While_type, NULL, NULL);
2999 if (!result) goto failed;
3000 value = ast2obj_expr(o->v.While.test);
3001 if (!value) goto failed;
3002 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3003 goto failed;
3004 Py_DECREF(value);
3005 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
3006 if (!value) goto failed;
3007 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3008 goto failed;
3009 Py_DECREF(value);
3010 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 break;
3016 case If_kind:
3017 result = PyType_GenericNew(If_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_expr(o->v.If.test);
3020 if (!value) goto failed;
3021 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
3024 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
3030 if (!value) goto failed;
3031 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3032 goto failed;
3033 Py_DECREF(value);
3034 break;
3035 case With_kind:
3036 result = PyType_GenericNew(With_type, NULL, NULL);
3037 if (!result) goto failed;
3038 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
3039 if (!value) goto failed;
3040 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
3041 goto failed;
3042 Py_DECREF(value);
3043 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
3044 if (!value) goto failed;
3045 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3046 goto failed;
3047 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003048 value = ast2obj_string(o->v.With.type_comment);
3049 if (!value) goto failed;
3050 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
3051 goto failed;
3052 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003053 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003054 case AsyncWith_kind:
3055 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
3056 if (!result) goto failed;
3057 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
3058 if (!value) goto failed;
3059 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
3060 goto failed;
3061 Py_DECREF(value);
3062 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
3063 if (!value) goto failed;
3064 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3065 goto failed;
3066 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003067 value = ast2obj_string(o->v.AsyncWith.type_comment);
3068 if (!value) goto failed;
3069 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
3070 goto failed;
3071 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003072 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003073 case Raise_kind:
3074 result = PyType_GenericNew(Raise_type, NULL, NULL);
3075 if (!result) goto failed;
3076 value = ast2obj_expr(o->v.Raise.exc);
3077 if (!value) goto failed;
3078 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
3079 goto failed;
3080 Py_DECREF(value);
3081 value = ast2obj_expr(o->v.Raise.cause);
3082 if (!value) goto failed;
3083 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
3084 goto failed;
3085 Py_DECREF(value);
3086 break;
3087 case Try_kind:
3088 result = PyType_GenericNew(Try_type, NULL, NULL);
3089 if (!result) goto failed;
3090 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
3091 if (!value) goto failed;
3092 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3093 goto failed;
3094 Py_DECREF(value);
3095 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
3096 if (!value) goto failed;
3097 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
3098 goto failed;
3099 Py_DECREF(value);
3100 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
3101 if (!value) goto failed;
3102 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3103 goto failed;
3104 Py_DECREF(value);
3105 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
3106 if (!value) goto failed;
3107 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
3108 goto failed;
3109 Py_DECREF(value);
3110 break;
3111 case Assert_kind:
3112 result = PyType_GenericNew(Assert_type, NULL, NULL);
3113 if (!result) goto failed;
3114 value = ast2obj_expr(o->v.Assert.test);
3115 if (!value) goto failed;
3116 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3117 goto failed;
3118 Py_DECREF(value);
3119 value = ast2obj_expr(o->v.Assert.msg);
3120 if (!value) goto failed;
3121 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
3122 goto failed;
3123 Py_DECREF(value);
3124 break;
3125 case Import_kind:
3126 result = PyType_GenericNew(Import_type, NULL, NULL);
3127 if (!result) goto failed;
3128 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
3129 if (!value) goto failed;
3130 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3131 goto failed;
3132 Py_DECREF(value);
3133 break;
3134 case ImportFrom_kind:
3135 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
3136 if (!result) goto failed;
3137 value = ast2obj_identifier(o->v.ImportFrom.module);
3138 if (!value) goto failed;
3139 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
3140 goto failed;
3141 Py_DECREF(value);
3142 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
3143 if (!value) goto failed;
3144 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3145 goto failed;
3146 Py_DECREF(value);
3147 value = ast2obj_int(o->v.ImportFrom.level);
3148 if (!value) goto failed;
3149 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3150 goto failed;
3151 Py_DECREF(value);
3152 break;
3153 case Global_kind:
3154 result = PyType_GenericNew(Global_type, NULL, NULL);
3155 if (!result) goto failed;
3156 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3157 if (!value) goto failed;
3158 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 break;
3162 case Nonlocal_kind:
3163 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3164 if (!result) goto failed;
3165 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3166 if (!value) goto failed;
3167 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3168 goto failed;
3169 Py_DECREF(value);
3170 break;
3171 case Expr_kind:
3172 result = PyType_GenericNew(Expr_type, NULL, NULL);
3173 if (!result) goto failed;
3174 value = ast2obj_expr(o->v.Expr.value);
3175 if (!value) goto failed;
3176 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3177 goto failed;
3178 Py_DECREF(value);
3179 break;
3180 case Pass_kind:
3181 result = PyType_GenericNew(Pass_type, NULL, NULL);
3182 if (!result) goto failed;
3183 break;
3184 case Break_kind:
3185 result = PyType_GenericNew(Break_type, NULL, NULL);
3186 if (!result) goto failed;
3187 break;
3188 case Continue_kind:
3189 result = PyType_GenericNew(Continue_type, NULL, NULL);
3190 if (!result) goto failed;
3191 break;
3192 }
3193 value = ast2obj_int(o->lineno);
3194 if (!value) goto failed;
3195 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3196 goto failed;
3197 Py_DECREF(value);
3198 value = ast2obj_int(o->col_offset);
3199 if (!value) goto failed;
3200 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3201 goto failed;
3202 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003203 value = ast2obj_int(o->end_lineno);
3204 if (!value) goto failed;
3205 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3206 goto failed;
3207 Py_DECREF(value);
3208 value = ast2obj_int(o->end_col_offset);
3209 if (!value) goto failed;
3210 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3211 goto failed;
3212 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003213 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003214failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003215 Py_XDECREF(value);
3216 Py_XDECREF(result);
3217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218}
3219
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003220PyObject*
3221ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003223 expr_ty o = (expr_ty)_o;
3224 PyObject *result = NULL, *value = NULL;
3225 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003226 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003227 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003228
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003229 switch (o->kind) {
3230 case BoolOp_kind:
3231 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3232 if (!result) goto failed;
3233 value = ast2obj_boolop(o->v.BoolOp.op);
3234 if (!value) goto failed;
3235 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3236 goto failed;
3237 Py_DECREF(value);
3238 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3239 if (!value) goto failed;
3240 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3241 goto failed;
3242 Py_DECREF(value);
3243 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003244 case NamedExpr_kind:
3245 result = PyType_GenericNew(NamedExpr_type, NULL, NULL);
3246 if (!result) goto failed;
3247 value = ast2obj_expr(o->v.NamedExpr.target);
3248 if (!value) goto failed;
3249 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3250 goto failed;
3251 Py_DECREF(value);
3252 value = ast2obj_expr(o->v.NamedExpr.value);
3253 if (!value) goto failed;
3254 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3255 goto failed;
3256 Py_DECREF(value);
3257 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003258 case BinOp_kind:
3259 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3260 if (!result) goto failed;
3261 value = ast2obj_expr(o->v.BinOp.left);
3262 if (!value) goto failed;
3263 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3264 goto failed;
3265 Py_DECREF(value);
3266 value = ast2obj_operator(o->v.BinOp.op);
3267 if (!value) goto failed;
3268 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3269 goto failed;
3270 Py_DECREF(value);
3271 value = ast2obj_expr(o->v.BinOp.right);
3272 if (!value) goto failed;
3273 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3274 goto failed;
3275 Py_DECREF(value);
3276 break;
3277 case UnaryOp_kind:
3278 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3279 if (!result) goto failed;
3280 value = ast2obj_unaryop(o->v.UnaryOp.op);
3281 if (!value) goto failed;
3282 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3283 goto failed;
3284 Py_DECREF(value);
3285 value = ast2obj_expr(o->v.UnaryOp.operand);
3286 if (!value) goto failed;
3287 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3288 goto failed;
3289 Py_DECREF(value);
3290 break;
3291 case Lambda_kind:
3292 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3293 if (!result) goto failed;
3294 value = ast2obj_arguments(o->v.Lambda.args);
3295 if (!value) goto failed;
3296 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3297 goto failed;
3298 Py_DECREF(value);
3299 value = ast2obj_expr(o->v.Lambda.body);
3300 if (!value) goto failed;
3301 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3302 goto failed;
3303 Py_DECREF(value);
3304 break;
3305 case IfExp_kind:
3306 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3307 if (!result) goto failed;
3308 value = ast2obj_expr(o->v.IfExp.test);
3309 if (!value) goto failed;
3310 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3311 goto failed;
3312 Py_DECREF(value);
3313 value = ast2obj_expr(o->v.IfExp.body);
3314 if (!value) goto failed;
3315 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
3318 value = ast2obj_expr(o->v.IfExp.orelse);
3319 if (!value) goto failed;
3320 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3321 goto failed;
3322 Py_DECREF(value);
3323 break;
3324 case Dict_kind:
3325 result = PyType_GenericNew(Dict_type, NULL, NULL);
3326 if (!result) goto failed;
3327 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3328 if (!value) goto failed;
3329 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3330 goto failed;
3331 Py_DECREF(value);
3332 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3333 if (!value) goto failed;
3334 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3335 goto failed;
3336 Py_DECREF(value);
3337 break;
3338 case Set_kind:
3339 result = PyType_GenericNew(Set_type, NULL, NULL);
3340 if (!result) goto failed;
3341 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3342 if (!value) goto failed;
3343 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3344 goto failed;
3345 Py_DECREF(value);
3346 break;
3347 case ListComp_kind:
3348 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3349 if (!result) goto failed;
3350 value = ast2obj_expr(o->v.ListComp.elt);
3351 if (!value) goto failed;
3352 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3353 goto failed;
3354 Py_DECREF(value);
3355 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3356 if (!value) goto failed;
3357 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3358 goto failed;
3359 Py_DECREF(value);
3360 break;
3361 case SetComp_kind:
3362 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3363 if (!result) goto failed;
3364 value = ast2obj_expr(o->v.SetComp.elt);
3365 if (!value) goto failed;
3366 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3367 goto failed;
3368 Py_DECREF(value);
3369 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3370 if (!value) goto failed;
3371 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3372 goto failed;
3373 Py_DECREF(value);
3374 break;
3375 case DictComp_kind:
3376 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3377 if (!result) goto failed;
3378 value = ast2obj_expr(o->v.DictComp.key);
3379 if (!value) goto failed;
3380 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3381 goto failed;
3382 Py_DECREF(value);
3383 value = ast2obj_expr(o->v.DictComp.value);
3384 if (!value) goto failed;
3385 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3386 goto failed;
3387 Py_DECREF(value);
3388 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3389 if (!value) goto failed;
3390 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3391 goto failed;
3392 Py_DECREF(value);
3393 break;
3394 case GeneratorExp_kind:
3395 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3396 if (!result) goto failed;
3397 value = ast2obj_expr(o->v.GeneratorExp.elt);
3398 if (!value) goto failed;
3399 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3400 goto failed;
3401 Py_DECREF(value);
3402 value = ast2obj_list(o->v.GeneratorExp.generators,
3403 ast2obj_comprehension);
3404 if (!value) goto failed;
3405 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3406 goto failed;
3407 Py_DECREF(value);
3408 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003409 case Await_kind:
3410 result = PyType_GenericNew(Await_type, NULL, NULL);
3411 if (!result) goto failed;
3412 value = ast2obj_expr(o->v.Await.value);
3413 if (!value) goto failed;
3414 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3415 goto failed;
3416 Py_DECREF(value);
3417 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003418 case Yield_kind:
3419 result = PyType_GenericNew(Yield_type, NULL, NULL);
3420 if (!result) goto failed;
3421 value = ast2obj_expr(o->v.Yield.value);
3422 if (!value) goto failed;
3423 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3424 goto failed;
3425 Py_DECREF(value);
3426 break;
3427 case YieldFrom_kind:
3428 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3429 if (!result) goto failed;
3430 value = ast2obj_expr(o->v.YieldFrom.value);
3431 if (!value) goto failed;
3432 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
3435 break;
3436 case Compare_kind:
3437 result = PyType_GenericNew(Compare_type, NULL, NULL);
3438 if (!result) goto failed;
3439 value = ast2obj_expr(o->v.Compare.left);
3440 if (!value) goto failed;
3441 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3442 goto failed;
3443 Py_DECREF(value);
3444 {
3445 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3446 value = PyList_New(n);
3447 if (!value) goto failed;
3448 for(i = 0; i < n; i++)
3449 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003451 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003452 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3453 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003454 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003455 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003456 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003457 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3458 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003459 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003460 break;
3461 case Call_kind:
3462 result = PyType_GenericNew(Call_type, NULL, NULL);
3463 if (!result) goto failed;
3464 value = ast2obj_expr(o->v.Call.func);
3465 if (!value) goto failed;
3466 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3467 goto failed;
3468 Py_DECREF(value);
3469 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3470 if (!value) goto failed;
3471 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3472 goto failed;
3473 Py_DECREF(value);
3474 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3475 if (!value) goto failed;
3476 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3477 goto failed;
3478 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003479 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003480 case FormattedValue_kind:
3481 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3482 if (!result) goto failed;
3483 value = ast2obj_expr(o->v.FormattedValue.value);
3484 if (!value) goto failed;
3485 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3486 goto failed;
3487 Py_DECREF(value);
3488 value = ast2obj_int(o->v.FormattedValue.conversion);
3489 if (!value) goto failed;
3490 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3491 goto failed;
3492 Py_DECREF(value);
3493 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3494 if (!value) goto failed;
3495 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3496 goto failed;
3497 Py_DECREF(value);
3498 break;
3499 case JoinedStr_kind:
3500 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3501 if (!result) goto failed;
3502 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3503 if (!value) goto failed;
3504 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3505 goto failed;
3506 Py_DECREF(value);
3507 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003508 case Constant_kind:
3509 result = PyType_GenericNew(Constant_type, NULL, NULL);
3510 if (!result) goto failed;
3511 value = ast2obj_constant(o->v.Constant.value);
3512 if (!value) goto failed;
3513 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3514 goto failed;
3515 Py_DECREF(value);
3516 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003517 case Attribute_kind:
3518 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3519 if (!result) goto failed;
3520 value = ast2obj_expr(o->v.Attribute.value);
3521 if (!value) goto failed;
3522 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3523 goto failed;
3524 Py_DECREF(value);
3525 value = ast2obj_identifier(o->v.Attribute.attr);
3526 if (!value) goto failed;
3527 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3528 goto failed;
3529 Py_DECREF(value);
3530 value = ast2obj_expr_context(o->v.Attribute.ctx);
3531 if (!value) goto failed;
3532 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3533 goto failed;
3534 Py_DECREF(value);
3535 break;
3536 case Subscript_kind:
3537 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3538 if (!result) goto failed;
3539 value = ast2obj_expr(o->v.Subscript.value);
3540 if (!value) goto failed;
3541 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3542 goto failed;
3543 Py_DECREF(value);
3544 value = ast2obj_slice(o->v.Subscript.slice);
3545 if (!value) goto failed;
3546 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3547 goto failed;
3548 Py_DECREF(value);
3549 value = ast2obj_expr_context(o->v.Subscript.ctx);
3550 if (!value) goto failed;
3551 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3552 goto failed;
3553 Py_DECREF(value);
3554 break;
3555 case Starred_kind:
3556 result = PyType_GenericNew(Starred_type, NULL, NULL);
3557 if (!result) goto failed;
3558 value = ast2obj_expr(o->v.Starred.value);
3559 if (!value) goto failed;
3560 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3561 goto failed;
3562 Py_DECREF(value);
3563 value = ast2obj_expr_context(o->v.Starred.ctx);
3564 if (!value) goto failed;
3565 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3566 goto failed;
3567 Py_DECREF(value);
3568 break;
3569 case Name_kind:
3570 result = PyType_GenericNew(Name_type, NULL, NULL);
3571 if (!result) goto failed;
3572 value = ast2obj_identifier(o->v.Name.id);
3573 if (!value) goto failed;
3574 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3575 goto failed;
3576 Py_DECREF(value);
3577 value = ast2obj_expr_context(o->v.Name.ctx);
3578 if (!value) goto failed;
3579 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3580 goto failed;
3581 Py_DECREF(value);
3582 break;
3583 case List_kind:
3584 result = PyType_GenericNew(List_type, NULL, NULL);
3585 if (!result) goto failed;
3586 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3587 if (!value) goto failed;
3588 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3589 goto failed;
3590 Py_DECREF(value);
3591 value = ast2obj_expr_context(o->v.List.ctx);
3592 if (!value) goto failed;
3593 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3594 goto failed;
3595 Py_DECREF(value);
3596 break;
3597 case Tuple_kind:
3598 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3599 if (!result) goto failed;
3600 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3601 if (!value) goto failed;
3602 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3603 goto failed;
3604 Py_DECREF(value);
3605 value = ast2obj_expr_context(o->v.Tuple.ctx);
3606 if (!value) goto failed;
3607 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3608 goto failed;
3609 Py_DECREF(value);
3610 break;
3611 }
3612 value = ast2obj_int(o->lineno);
3613 if (!value) goto failed;
3614 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3615 goto failed;
3616 Py_DECREF(value);
3617 value = ast2obj_int(o->col_offset);
3618 if (!value) goto failed;
3619 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3620 goto failed;
3621 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003622 value = ast2obj_int(o->end_lineno);
3623 if (!value) goto failed;
3624 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3625 goto failed;
3626 Py_DECREF(value);
3627 value = ast2obj_int(o->end_col_offset);
3628 if (!value) goto failed;
3629 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3630 goto failed;
3631 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003632 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003633failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003634 Py_XDECREF(value);
3635 Py_XDECREF(result);
3636 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637}
3638
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003639PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003641 switch(o) {
3642 case Load:
3643 Py_INCREF(Load_singleton);
3644 return Load_singleton;
3645 case Store:
3646 Py_INCREF(Store_singleton);
3647 return Store_singleton;
3648 case Del:
3649 Py_INCREF(Del_singleton);
3650 return Del_singleton;
3651 case AugLoad:
3652 Py_INCREF(AugLoad_singleton);
3653 return AugLoad_singleton;
3654 case AugStore:
3655 Py_INCREF(AugStore_singleton);
3656 return AugStore_singleton;
3657 case Param:
3658 Py_INCREF(Param_singleton);
3659 return Param_singleton;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003660 case NamedStore:
3661 Py_INCREF(NamedStore_singleton);
3662 return NamedStore_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003663 default:
3664 /* should never happen, but just in case ... */
3665 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3666 return NULL;
3667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003669PyObject*
3670ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003672 slice_ty o = (slice_ty)_o;
3673 PyObject *result = NULL, *value = NULL;
3674 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003675 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003676 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003677
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003678 switch (o->kind) {
3679 case Slice_kind:
3680 result = PyType_GenericNew(Slice_type, NULL, NULL);
3681 if (!result) goto failed;
3682 value = ast2obj_expr(o->v.Slice.lower);
3683 if (!value) goto failed;
3684 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3685 goto failed;
3686 Py_DECREF(value);
3687 value = ast2obj_expr(o->v.Slice.upper);
3688 if (!value) goto failed;
3689 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3690 goto failed;
3691 Py_DECREF(value);
3692 value = ast2obj_expr(o->v.Slice.step);
3693 if (!value) goto failed;
3694 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3695 goto failed;
3696 Py_DECREF(value);
3697 break;
3698 case ExtSlice_kind:
3699 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3700 if (!result) goto failed;
3701 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3702 if (!value) goto failed;
3703 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3704 goto failed;
3705 Py_DECREF(value);
3706 break;
3707 case Index_kind:
3708 result = PyType_GenericNew(Index_type, NULL, NULL);
3709 if (!result) goto failed;
3710 value = ast2obj_expr(o->v.Index.value);
3711 if (!value) goto failed;
3712 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3713 goto failed;
3714 Py_DECREF(value);
3715 break;
3716 }
3717 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003718failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003719 Py_XDECREF(value);
3720 Py_XDECREF(result);
3721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722}
3723
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003724PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003726 switch(o) {
3727 case And:
3728 Py_INCREF(And_singleton);
3729 return And_singleton;
3730 case Or:
3731 Py_INCREF(Or_singleton);
3732 return Or_singleton;
3733 default:
3734 /* should never happen, but just in case ... */
3735 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3736 return NULL;
3737 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003738}
3739PyObject* ast2obj_operator(operator_ty o)
3740{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003741 switch(o) {
3742 case Add:
3743 Py_INCREF(Add_singleton);
3744 return Add_singleton;
3745 case Sub:
3746 Py_INCREF(Sub_singleton);
3747 return Sub_singleton;
3748 case Mult:
3749 Py_INCREF(Mult_singleton);
3750 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003751 case MatMult:
3752 Py_INCREF(MatMult_singleton);
3753 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003754 case Div:
3755 Py_INCREF(Div_singleton);
3756 return Div_singleton;
3757 case Mod:
3758 Py_INCREF(Mod_singleton);
3759 return Mod_singleton;
3760 case Pow:
3761 Py_INCREF(Pow_singleton);
3762 return Pow_singleton;
3763 case LShift:
3764 Py_INCREF(LShift_singleton);
3765 return LShift_singleton;
3766 case RShift:
3767 Py_INCREF(RShift_singleton);
3768 return RShift_singleton;
3769 case BitOr:
3770 Py_INCREF(BitOr_singleton);
3771 return BitOr_singleton;
3772 case BitXor:
3773 Py_INCREF(BitXor_singleton);
3774 return BitXor_singleton;
3775 case BitAnd:
3776 Py_INCREF(BitAnd_singleton);
3777 return BitAnd_singleton;
3778 case FloorDiv:
3779 Py_INCREF(FloorDiv_singleton);
3780 return FloorDiv_singleton;
3781 default:
3782 /* should never happen, but just in case ... */
3783 PyErr_Format(PyExc_SystemError, "unknown operator found");
3784 return NULL;
3785 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003786}
3787PyObject* ast2obj_unaryop(unaryop_ty o)
3788{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003789 switch(o) {
3790 case Invert:
3791 Py_INCREF(Invert_singleton);
3792 return Invert_singleton;
3793 case Not:
3794 Py_INCREF(Not_singleton);
3795 return Not_singleton;
3796 case UAdd:
3797 Py_INCREF(UAdd_singleton);
3798 return UAdd_singleton;
3799 case USub:
3800 Py_INCREF(USub_singleton);
3801 return USub_singleton;
3802 default:
3803 /* should never happen, but just in case ... */
3804 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3805 return NULL;
3806 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003807}
3808PyObject* ast2obj_cmpop(cmpop_ty o)
3809{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003810 switch(o) {
3811 case Eq:
3812 Py_INCREF(Eq_singleton);
3813 return Eq_singleton;
3814 case NotEq:
3815 Py_INCREF(NotEq_singleton);
3816 return NotEq_singleton;
3817 case Lt:
3818 Py_INCREF(Lt_singleton);
3819 return Lt_singleton;
3820 case LtE:
3821 Py_INCREF(LtE_singleton);
3822 return LtE_singleton;
3823 case Gt:
3824 Py_INCREF(Gt_singleton);
3825 return Gt_singleton;
3826 case GtE:
3827 Py_INCREF(GtE_singleton);
3828 return GtE_singleton;
3829 case Is:
3830 Py_INCREF(Is_singleton);
3831 return Is_singleton;
3832 case IsNot:
3833 Py_INCREF(IsNot_singleton);
3834 return IsNot_singleton;
3835 case In:
3836 Py_INCREF(In_singleton);
3837 return In_singleton;
3838 case NotIn:
3839 Py_INCREF(NotIn_singleton);
3840 return NotIn_singleton;
3841 default:
3842 /* should never happen, but just in case ... */
3843 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3844 return NULL;
3845 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003846}
3847PyObject*
3848ast2obj_comprehension(void* _o)
3849{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003850 comprehension_ty o = (comprehension_ty)_o;
3851 PyObject *result = NULL, *value = NULL;
3852 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003853 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003854 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003855
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003856 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3857 if (!result) return NULL;
3858 value = ast2obj_expr(o->target);
3859 if (!value) goto failed;
3860 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3861 goto failed;
3862 Py_DECREF(value);
3863 value = ast2obj_expr(o->iter);
3864 if (!value) goto failed;
3865 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3866 goto failed;
3867 Py_DECREF(value);
3868 value = ast2obj_list(o->ifs, ast2obj_expr);
3869 if (!value) goto failed;
3870 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3871 goto failed;
3872 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003873 value = ast2obj_int(o->is_async);
3874 if (!value) goto failed;
3875 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3876 goto failed;
3877 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003878 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003879failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003880 Py_XDECREF(value);
3881 Py_XDECREF(result);
3882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883}
3884
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003885PyObject*
3886ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003888 excepthandler_ty o = (excepthandler_ty)_o;
3889 PyObject *result = NULL, *value = NULL;
3890 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003891 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003892 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003893
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003894 switch (o->kind) {
3895 case ExceptHandler_kind:
3896 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3897 if (!result) goto failed;
3898 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003899 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003900 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3901 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003902 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003903 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003904 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003905 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3906 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003907 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003908 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3909 if (!value) goto failed;
3910 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3911 goto failed;
3912 Py_DECREF(value);
3913 break;
3914 }
3915 value = ast2obj_int(o->lineno);
3916 if (!value) goto failed;
3917 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3918 goto failed;
3919 Py_DECREF(value);
3920 value = ast2obj_int(o->col_offset);
3921 if (!value) goto failed;
3922 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3923 goto failed;
3924 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003925 value = ast2obj_int(o->end_lineno);
3926 if (!value) goto failed;
3927 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3928 goto failed;
3929 Py_DECREF(value);
3930 value = ast2obj_int(o->end_col_offset);
3931 if (!value) goto failed;
3932 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3933 goto failed;
3934 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003935 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003936failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003937 Py_XDECREF(value);
3938 Py_XDECREF(result);
3939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940}
3941
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003942PyObject*
3943ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 arguments_ty o = (arguments_ty)_o;
3946 PyObject *result = NULL, *value = NULL;
3947 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003948 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003949 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003950
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003951 result = PyType_GenericNew(arguments_type, NULL, NULL);
3952 if (!result) return NULL;
3953 value = ast2obj_list(o->args, ast2obj_arg);
3954 if (!value) goto failed;
3955 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3956 goto failed;
3957 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003958 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 if (!value) goto failed;
3960 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3961 goto failed;
3962 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003963 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3964 if (!value) goto failed;
3965 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3966 goto failed;
3967 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003968 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003969 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003970 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003971 goto failed;
3972 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003973 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003975 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003976 goto failed;
3977 Py_DECREF(value);
3978 value = ast2obj_list(o->defaults, ast2obj_expr);
3979 if (!value) goto failed;
3980 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3981 goto failed;
3982 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003983 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003984failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003985 Py_XDECREF(value);
3986 Py_XDECREF(result);
3987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988}
3989
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003990PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003991ast2obj_arg(void* _o)
3992{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003993 arg_ty o = (arg_ty)_o;
3994 PyObject *result = NULL, *value = NULL;
3995 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003996 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003997 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003998
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003999 result = PyType_GenericNew(arg_type, NULL, NULL);
4000 if (!result) return NULL;
4001 value = ast2obj_identifier(o->arg);
4002 if (!value) goto failed;
4003 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
4004 goto failed;
4005 Py_DECREF(value);
4006 value = ast2obj_expr(o->annotation);
4007 if (!value) goto failed;
4008 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
4009 goto failed;
4010 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004011 value = ast2obj_string(o->type_comment);
4012 if (!value) goto failed;
4013 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
4014 goto failed;
4015 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02004016 value = ast2obj_int(o->lineno);
4017 if (!value) goto failed;
4018 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
4019 goto failed;
4020 Py_DECREF(value);
4021 value = ast2obj_int(o->col_offset);
4022 if (!value) goto failed;
4023 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
4024 goto failed;
4025 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004026 value = ast2obj_int(o->end_lineno);
4027 if (!value) goto failed;
4028 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
4029 goto failed;
4030 Py_DECREF(value);
4031 value = ast2obj_int(o->end_col_offset);
4032 if (!value) goto failed;
4033 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
4034 goto failed;
4035 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004036 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00004037failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004038 Py_XDECREF(value);
4039 Py_XDECREF(result);
4040 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00004041}
4042
4043PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004044ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004046 keyword_ty o = (keyword_ty)_o;
4047 PyObject *result = NULL, *value = NULL;
4048 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004049 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004050 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004051
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004052 result = PyType_GenericNew(keyword_type, NULL, NULL);
4053 if (!result) return NULL;
4054 value = ast2obj_identifier(o->arg);
4055 if (!value) goto failed;
4056 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
4057 goto failed;
4058 Py_DECREF(value);
4059 value = ast2obj_expr(o->value);
4060 if (!value) goto failed;
4061 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
4062 goto failed;
4063 Py_DECREF(value);
4064 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004065failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004066 Py_XDECREF(value);
4067 Py_XDECREF(result);
4068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069}
4070
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004071PyObject*
4072ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004074 alias_ty o = (alias_ty)_o;
4075 PyObject *result = NULL, *value = NULL;
4076 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004077 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004078 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004079
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004080 result = PyType_GenericNew(alias_type, NULL, NULL);
4081 if (!result) return NULL;
4082 value = ast2obj_identifier(o->name);
4083 if (!value) goto failed;
4084 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
4085 goto failed;
4086 Py_DECREF(value);
4087 value = ast2obj_identifier(o->asname);
4088 if (!value) goto failed;
4089 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
4090 goto failed;
4091 Py_DECREF(value);
4092 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004093failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004094 Py_XDECREF(value);
4095 Py_XDECREF(result);
4096 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097}
4098
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004099PyObject*
4100ast2obj_withitem(void* _o)
4101{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004102 withitem_ty o = (withitem_ty)_o;
4103 PyObject *result = NULL, *value = NULL;
4104 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004105 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004106 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004107
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004108 result = PyType_GenericNew(withitem_type, NULL, NULL);
4109 if (!result) return NULL;
4110 value = ast2obj_expr(o->context_expr);
4111 if (!value) goto failed;
4112 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
4113 goto failed;
4114 Py_DECREF(value);
4115 value = ast2obj_expr(o->optional_vars);
4116 if (!value) goto failed;
4117 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
4118 goto failed;
4119 Py_DECREF(value);
4120 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004121failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004122 Py_XDECREF(value);
4123 Py_XDECREF(result);
4124 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004125}
4126
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004127PyObject*
4128ast2obj_type_ignore(void* _o)
4129{
4130 type_ignore_ty o = (type_ignore_ty)_o;
4131 PyObject *result = NULL, *value = NULL;
4132 if (!o) {
4133 Py_RETURN_NONE;
4134 }
4135
4136 switch (o->kind) {
4137 case TypeIgnore_kind:
4138 result = PyType_GenericNew(TypeIgnore_type, NULL, NULL);
4139 if (!result) goto failed;
4140 value = ast2obj_int(o->v.TypeIgnore.lineno);
4141 if (!value) goto failed;
4142 if (_PyObject_SetAttrId(result, &PyId_lineno, value) == -1)
4143 goto failed;
4144 Py_DECREF(value);
4145 break;
4146 }
4147 return result;
4148failed:
4149 Py_XDECREF(value);
4150 Py_XDECREF(result);
4151 return NULL;
4152}
4153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004155int
4156obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
4157{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004158 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004159
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004160 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004161
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004162 if (obj == Py_None) {
4163 *out = NULL;
4164 return 0;
4165 }
4166 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
4167 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004168 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004169 }
4170 if (isinstance) {
4171 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004172 asdl_seq* type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004173
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004174 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4175 return 1;
4176 }
4177 if (tmp == NULL) {
4178 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4179 return 1;
4180 }
4181 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004182 int res;
4183 Py_ssize_t len;
4184 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004185 if (!PyList_Check(tmp)) {
4186 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4187 goto failed;
4188 }
4189 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004190 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004191 if (body == NULL) goto failed;
4192 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004193 stmt_ty val;
4194 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004195 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004196 if (len != PyList_GET_SIZE(tmp)) {
4197 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4198 goto failed;
4199 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004200 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004201 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004202 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004203 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004204 if (_PyObject_LookupAttrId(obj, &PyId_type_ignores, &tmp) < 0) {
4205 return 1;
4206 }
4207 if (tmp == NULL) {
4208 PyErr_SetString(PyExc_TypeError, "required field \"type_ignores\" missing from Module");
4209 return 1;
4210 }
4211 else {
4212 int res;
4213 Py_ssize_t len;
4214 Py_ssize_t i;
4215 if (!PyList_Check(tmp)) {
4216 PyErr_Format(PyExc_TypeError, "Module field \"type_ignores\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4217 goto failed;
4218 }
4219 len = PyList_GET_SIZE(tmp);
4220 type_ignores = _Py_asdl_seq_new(len, arena);
4221 if (type_ignores == NULL) goto failed;
4222 for (i = 0; i < len; i++) {
4223 type_ignore_ty val;
4224 res = obj2ast_type_ignore(PyList_GET_ITEM(tmp, i), &val, arena);
4225 if (res != 0) goto failed;
4226 if (len != PyList_GET_SIZE(tmp)) {
4227 PyErr_SetString(PyExc_RuntimeError, "Module field \"type_ignores\" changed size during iteration");
4228 goto failed;
4229 }
4230 asdl_seq_SET(type_ignores, i, val);
4231 }
4232 Py_CLEAR(tmp);
4233 }
4234 *out = Module(body, type_ignores, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004235 if (*out == NULL) goto failed;
4236 return 0;
4237 }
4238 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4239 if (isinstance == -1) {
4240 return 1;
4241 }
4242 if (isinstance) {
4243 asdl_seq* body;
4244
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004245 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4246 return 1;
4247 }
4248 if (tmp == NULL) {
4249 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4250 return 1;
4251 }
4252 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004253 int res;
4254 Py_ssize_t len;
4255 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004256 if (!PyList_Check(tmp)) {
4257 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4258 goto failed;
4259 }
4260 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004261 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004262 if (body == NULL) goto failed;
4263 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004264 stmt_ty val;
4265 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004266 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004267 if (len != PyList_GET_SIZE(tmp)) {
4268 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4269 goto failed;
4270 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004271 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004272 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004273 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004274 }
4275 *out = Interactive(body, arena);
4276 if (*out == NULL) goto failed;
4277 return 0;
4278 }
4279 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4280 if (isinstance == -1) {
4281 return 1;
4282 }
4283 if (isinstance) {
4284 expr_ty body;
4285
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004286 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4287 return 1;
4288 }
4289 if (tmp == NULL) {
4290 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4291 return 1;
4292 }
4293 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004294 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004295 res = obj2ast_expr(tmp, &body, arena);
4296 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004297 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004298 }
4299 *out = Expression(body, arena);
4300 if (*out == NULL) goto failed;
4301 return 0;
4302 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionType_type);
4304 if (isinstance == -1) {
4305 return 1;
4306 }
4307 if (isinstance) {
4308 asdl_seq* argtypes;
4309 expr_ty returns;
4310
4311 if (_PyObject_LookupAttrId(obj, &PyId_argtypes, &tmp) < 0) {
4312 return 1;
4313 }
4314 if (tmp == NULL) {
4315 PyErr_SetString(PyExc_TypeError, "required field \"argtypes\" missing from FunctionType");
4316 return 1;
4317 }
4318 else {
4319 int res;
4320 Py_ssize_t len;
4321 Py_ssize_t i;
4322 if (!PyList_Check(tmp)) {
4323 PyErr_Format(PyExc_TypeError, "FunctionType field \"argtypes\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4324 goto failed;
4325 }
4326 len = PyList_GET_SIZE(tmp);
4327 argtypes = _Py_asdl_seq_new(len, arena);
4328 if (argtypes == NULL) goto failed;
4329 for (i = 0; i < len; i++) {
4330 expr_ty val;
4331 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
4332 if (res != 0) goto failed;
4333 if (len != PyList_GET_SIZE(tmp)) {
4334 PyErr_SetString(PyExc_RuntimeError, "FunctionType field \"argtypes\" changed size during iteration");
4335 goto failed;
4336 }
4337 asdl_seq_SET(argtypes, i, val);
4338 }
4339 Py_CLEAR(tmp);
4340 }
4341 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
4342 return 1;
4343 }
4344 if (tmp == NULL) {
4345 PyErr_SetString(PyExc_TypeError, "required field \"returns\" missing from FunctionType");
4346 return 1;
4347 }
4348 else {
4349 int res;
4350 res = obj2ast_expr(tmp, &returns, arena);
4351 if (res != 0) goto failed;
4352 Py_CLEAR(tmp);
4353 }
4354 *out = FunctionType(argtypes, returns, arena);
4355 if (*out == NULL) goto failed;
4356 return 0;
4357 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004358 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4359 if (isinstance == -1) {
4360 return 1;
4361 }
4362 if (isinstance) {
4363 asdl_seq* body;
4364
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004365 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4366 return 1;
4367 }
4368 if (tmp == NULL) {
4369 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4370 return 1;
4371 }
4372 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004373 int res;
4374 Py_ssize_t len;
4375 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004376 if (!PyList_Check(tmp)) {
4377 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4378 goto failed;
4379 }
4380 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004381 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004382 if (body == NULL) goto failed;
4383 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004384 stmt_ty val;
4385 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004386 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004387 if (len != PyList_GET_SIZE(tmp)) {
4388 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4389 goto failed;
4390 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004391 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004392 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004393 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004394 }
4395 *out = Suite(body, arena);
4396 if (*out == NULL) goto failed;
4397 return 0;
4398 }
4399
4400 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4401 failed:
4402 Py_XDECREF(tmp);
4403 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004404}
4405
4406int
4407obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004409 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004410
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004411 PyObject *tmp = NULL;
4412 int lineno;
4413 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004414 int end_lineno;
4415 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004416
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004417 if (obj == Py_None) {
4418 *out = NULL;
4419 return 0;
4420 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004421 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
4422 return 1;
4423 }
4424 if (tmp == NULL) {
4425 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
4426 return 1;
4427 }
4428 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004429 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004430 res = obj2ast_int(tmp, &lineno, arena);
4431 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004432 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004433 }
4434 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004435 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004436 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004437 if (tmp == NULL) {
4438 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4439 return 1;
4440 }
4441 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004442 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004443 res = obj2ast_int(tmp, &col_offset, arena);
4444 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004445 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004446 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004447 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
4448 return 1;
4449 }
4450 if (tmp == NULL || tmp == Py_None) {
4451 Py_CLEAR(tmp);
4452 end_lineno = 0;
4453 }
4454 else {
4455 int res;
4456 res = obj2ast_int(tmp, &end_lineno, arena);
4457 if (res != 0) goto failed;
4458 Py_CLEAR(tmp);
4459 }
4460 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
4461 return 1;
4462 }
4463 if (tmp == NULL || tmp == Py_None) {
4464 Py_CLEAR(tmp);
4465 end_col_offset = 0;
4466 }
4467 else {
4468 int res;
4469 res = obj2ast_int(tmp, &end_col_offset, arena);
4470 if (res != 0) goto failed;
4471 Py_CLEAR(tmp);
4472 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004473 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4474 if (isinstance == -1) {
4475 return 1;
4476 }
4477 if (isinstance) {
4478 identifier name;
4479 arguments_ty args;
4480 asdl_seq* body;
4481 asdl_seq* decorator_list;
4482 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004483 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004484
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004485 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4486 return 1;
4487 }
4488 if (tmp == NULL) {
4489 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4490 return 1;
4491 }
4492 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004493 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004494 res = obj2ast_identifier(tmp, &name, arena);
4495 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004496 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004497 }
4498 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004499 return 1;
4500 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004501 if (tmp == NULL) {
4502 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4503 return 1;
4504 }
4505 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004506 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004507 res = obj2ast_arguments(tmp, &args, arena);
4508 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004509 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004510 }
4511 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004512 return 1;
4513 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004514 if (tmp == NULL) {
4515 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4516 return 1;
4517 }
4518 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004519 int res;
4520 Py_ssize_t len;
4521 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004522 if (!PyList_Check(tmp)) {
4523 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4524 goto failed;
4525 }
4526 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004527 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004528 if (body == NULL) goto failed;
4529 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004530 stmt_ty val;
4531 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004532 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004533 if (len != PyList_GET_SIZE(tmp)) {
4534 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4535 goto failed;
4536 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004537 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004538 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004539 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004540 }
4541 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004542 return 1;
4543 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004544 if (tmp == NULL) {
4545 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4546 return 1;
4547 }
4548 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004549 int res;
4550 Py_ssize_t len;
4551 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004552 if (!PyList_Check(tmp)) {
4553 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4554 goto failed;
4555 }
4556 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004557 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004558 if (decorator_list == NULL) goto failed;
4559 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004560 expr_ty val;
4561 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004562 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004563 if (len != PyList_GET_SIZE(tmp)) {
4564 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4565 goto failed;
4566 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004567 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004568 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004569 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004570 }
4571 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004572 return 1;
4573 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004574 if (tmp == NULL || tmp == Py_None) {
4575 Py_CLEAR(tmp);
4576 returns = NULL;
4577 }
4578 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004579 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004580 res = obj2ast_expr(tmp, &returns, arena);
4581 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004582 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004583 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004584 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
4585 return 1;
4586 }
4587 if (tmp == NULL || tmp == Py_None) {
4588 Py_CLEAR(tmp);
4589 type_comment = NULL;
4590 }
4591 else {
4592 int res;
4593 res = obj2ast_string(tmp, &type_comment, arena);
4594 if (res != 0) goto failed;
4595 Py_CLEAR(tmp);
4596 }
4597 *out = FunctionDef(name, args, body, decorator_list, returns,
4598 type_comment, lineno, col_offset, end_lineno,
4599 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004600 if (*out == NULL) goto failed;
4601 return 0;
4602 }
Yury Selivanov75445082015-05-11 22:57:16 -04004603 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4604 if (isinstance == -1) {
4605 return 1;
4606 }
4607 if (isinstance) {
4608 identifier name;
4609 arguments_ty args;
4610 asdl_seq* body;
4611 asdl_seq* decorator_list;
4612 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004613 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04004614
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004615 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4616 return 1;
4617 }
4618 if (tmp == NULL) {
4619 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4620 return 1;
4621 }
4622 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004623 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004624 res = obj2ast_identifier(tmp, &name, arena);
4625 if (res != 0) goto failed;
4626 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004627 }
4628 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004629 return 1;
4630 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004631 if (tmp == NULL) {
4632 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4633 return 1;
4634 }
4635 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004636 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004637 res = obj2ast_arguments(tmp, &args, arena);
4638 if (res != 0) goto failed;
4639 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004640 }
4641 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004642 return 1;
4643 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004644 if (tmp == NULL) {
4645 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4646 return 1;
4647 }
4648 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004649 int res;
4650 Py_ssize_t len;
4651 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04004652 if (!PyList_Check(tmp)) {
4653 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4654 goto failed;
4655 }
4656 len = PyList_GET_SIZE(tmp);
4657 body = _Py_asdl_seq_new(len, arena);
4658 if (body == NULL) goto failed;
4659 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004660 stmt_ty val;
4661 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004662 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004663 if (len != PyList_GET_SIZE(tmp)) {
4664 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4665 goto failed;
4666 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004667 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004668 }
4669 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004670 }
4671 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004672 return 1;
4673 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004674 if (tmp == NULL) {
4675 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4676 return 1;
4677 }
4678 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004679 int res;
4680 Py_ssize_t len;
4681 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04004682 if (!PyList_Check(tmp)) {
4683 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4684 goto failed;
4685 }
4686 len = PyList_GET_SIZE(tmp);
4687 decorator_list = _Py_asdl_seq_new(len, arena);
4688 if (decorator_list == NULL) goto failed;
4689 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004690 expr_ty val;
4691 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004692 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004693 if (len != PyList_GET_SIZE(tmp)) {
4694 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4695 goto failed;
4696 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004697 asdl_seq_SET(decorator_list, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004698 }
4699 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004700 }
4701 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004702 return 1;
4703 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004704 if (tmp == NULL || tmp == Py_None) {
4705 Py_CLEAR(tmp);
4706 returns = NULL;
4707 }
4708 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004709 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004710 res = obj2ast_expr(tmp, &returns, arena);
4711 if (res != 0) goto failed;
4712 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04004713 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004714 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
4715 return 1;
4716 }
4717 if (tmp == NULL || tmp == Py_None) {
4718 Py_CLEAR(tmp);
4719 type_comment = NULL;
4720 }
4721 else {
4722 int res;
4723 res = obj2ast_string(tmp, &type_comment, arena);
4724 if (res != 0) goto failed;
4725 Py_CLEAR(tmp);
4726 }
Yury Selivanov75445082015-05-11 22:57:16 -04004727 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004728 type_comment, lineno, col_offset, end_lineno,
4729 end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004730 if (*out == NULL) goto failed;
4731 return 0;
4732 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004733 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4734 if (isinstance == -1) {
4735 return 1;
4736 }
4737 if (isinstance) {
4738 identifier name;
4739 asdl_seq* bases;
4740 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004741 asdl_seq* body;
4742 asdl_seq* decorator_list;
4743
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004744 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4745 return 1;
4746 }
4747 if (tmp == NULL) {
4748 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4749 return 1;
4750 }
4751 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004752 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004753 res = obj2ast_identifier(tmp, &name, arena);
4754 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004755 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004756 }
4757 if (_PyObject_LookupAttrId(obj, &PyId_bases, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004758 return 1;
4759 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004760 if (tmp == NULL) {
4761 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4762 return 1;
4763 }
4764 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004765 int res;
4766 Py_ssize_t len;
4767 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004768 if (!PyList_Check(tmp)) {
4769 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4770 goto failed;
4771 }
4772 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004773 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004774 if (bases == NULL) goto failed;
4775 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004776 expr_ty val;
4777 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004778 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004779 if (len != PyList_GET_SIZE(tmp)) {
4780 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4781 goto failed;
4782 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004783 asdl_seq_SET(bases, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004784 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004785 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004786 }
4787 if (_PyObject_LookupAttrId(obj, &PyId_keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004788 return 1;
4789 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004790 if (tmp == NULL) {
4791 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4792 return 1;
4793 }
4794 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004795 int res;
4796 Py_ssize_t len;
4797 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004798 if (!PyList_Check(tmp)) {
4799 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4800 goto failed;
4801 }
4802 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004803 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004804 if (keywords == NULL) goto failed;
4805 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004806 keyword_ty val;
4807 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004808 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004809 if (len != PyList_GET_SIZE(tmp)) {
4810 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4811 goto failed;
4812 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004813 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004814 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004815 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004816 }
4817 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004818 return 1;
4819 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004820 if (tmp == NULL) {
4821 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4822 return 1;
4823 }
4824 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004825 int res;
4826 Py_ssize_t len;
4827 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004828 if (!PyList_Check(tmp)) {
4829 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4830 goto failed;
4831 }
4832 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004833 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004834 if (body == NULL) goto failed;
4835 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004836 stmt_ty val;
4837 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004838 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004839 if (len != PyList_GET_SIZE(tmp)) {
4840 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4841 goto failed;
4842 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004843 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004844 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004845 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004846 }
4847 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004848 return 1;
4849 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004850 if (tmp == NULL) {
4851 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4852 return 1;
4853 }
4854 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004855 int res;
4856 Py_ssize_t len;
4857 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004858 if (!PyList_Check(tmp)) {
4859 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4860 goto failed;
4861 }
4862 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004863 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004864 if (decorator_list == NULL) goto failed;
4865 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004866 expr_ty val;
4867 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004868 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004869 if (len != PyList_GET_SIZE(tmp)) {
4870 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4871 goto failed;
4872 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004873 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004874 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004875 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004876 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004877 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004878 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004879 if (*out == NULL) goto failed;
4880 return 0;
4881 }
4882 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4883 if (isinstance == -1) {
4884 return 1;
4885 }
4886 if (isinstance) {
4887 expr_ty value;
4888
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004889 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
4890 return 1;
4891 }
4892 if (tmp == NULL || tmp == Py_None) {
4893 Py_CLEAR(tmp);
4894 value = NULL;
4895 }
4896 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004897 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004898 res = obj2ast_expr(tmp, &value, arena);
4899 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004900 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004901 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004902 *out = Return(value, lineno, col_offset, end_lineno, end_col_offset,
4903 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004904 if (*out == NULL) goto failed;
4905 return 0;
4906 }
4907 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4908 if (isinstance == -1) {
4909 return 1;
4910 }
4911 if (isinstance) {
4912 asdl_seq* targets;
4913
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004914 if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
4915 return 1;
4916 }
4917 if (tmp == NULL) {
4918 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4919 return 1;
4920 }
4921 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004922 int res;
4923 Py_ssize_t len;
4924 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004925 if (!PyList_Check(tmp)) {
4926 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4927 goto failed;
4928 }
4929 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004930 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004931 if (targets == NULL) goto failed;
4932 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004933 expr_ty val;
4934 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004935 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004936 if (len != PyList_GET_SIZE(tmp)) {
4937 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4938 goto failed;
4939 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004940 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004941 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004942 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004943 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004944 *out = Delete(targets, lineno, col_offset, end_lineno, end_col_offset,
4945 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004946 if (*out == NULL) goto failed;
4947 return 0;
4948 }
4949 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4950 if (isinstance == -1) {
4951 return 1;
4952 }
4953 if (isinstance) {
4954 asdl_seq* targets;
4955 expr_ty value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004956 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004957
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004958 if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
4959 return 1;
4960 }
4961 if (tmp == NULL) {
4962 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4963 return 1;
4964 }
4965 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004966 int res;
4967 Py_ssize_t len;
4968 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004969 if (!PyList_Check(tmp)) {
4970 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4971 goto failed;
4972 }
4973 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004974 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004975 if (targets == NULL) goto failed;
4976 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004977 expr_ty val;
4978 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004979 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004980 if (len != PyList_GET_SIZE(tmp)) {
4981 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4982 goto failed;
4983 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004984 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004985 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004986 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004987 }
4988 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004989 return 1;
4990 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004991 if (tmp == NULL) {
4992 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4993 return 1;
4994 }
4995 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004996 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004997 res = obj2ast_expr(tmp, &value, arena);
4998 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004999 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005000 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005001 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5002 return 1;
5003 }
5004 if (tmp == NULL || tmp == Py_None) {
5005 Py_CLEAR(tmp);
5006 type_comment = NULL;
5007 }
5008 else {
5009 int res;
5010 res = obj2ast_string(tmp, &type_comment, arena);
5011 if (res != 0) goto failed;
5012 Py_CLEAR(tmp);
5013 }
5014 *out = Assign(targets, value, type_comment, lineno, col_offset,
5015 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005016 if (*out == NULL) goto failed;
5017 return 0;
5018 }
5019 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
5020 if (isinstance == -1) {
5021 return 1;
5022 }
5023 if (isinstance) {
5024 expr_ty target;
5025 operator_ty op;
5026 expr_ty value;
5027
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005028 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5029 return 1;
5030 }
5031 if (tmp == NULL) {
5032 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
5033 return 1;
5034 }
5035 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005036 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005037 res = obj2ast_expr(tmp, &target, arena);
5038 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005039 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005040 }
5041 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005042 return 1;
5043 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005044 if (tmp == NULL) {
5045 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
5046 return 1;
5047 }
5048 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005049 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005050 res = obj2ast_operator(tmp, &op, arena);
5051 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005052 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005053 }
5054 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005055 return 1;
5056 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005057 if (tmp == NULL) {
5058 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
5059 return 1;
5060 }
5061 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005062 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005063 res = obj2ast_expr(tmp, &value, arena);
5064 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005065 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005066 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005067 *out = AugAssign(target, op, value, lineno, col_offset, end_lineno,
5068 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005069 if (*out == NULL) goto failed;
5070 return 0;
5071 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005072 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
5073 if (isinstance == -1) {
5074 return 1;
5075 }
5076 if (isinstance) {
5077 expr_ty target;
5078 expr_ty annotation;
5079 expr_ty value;
5080 int simple;
5081
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005082 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5083 return 1;
5084 }
5085 if (tmp == NULL) {
5086 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AnnAssign");
5087 return 1;
5088 }
5089 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005090 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005091 res = obj2ast_expr(tmp, &target, arena);
5092 if (res != 0) goto failed;
5093 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005094 }
5095 if (_PyObject_LookupAttrId(obj, &PyId_annotation, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005096 return 1;
5097 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005098 if (tmp == NULL) {
5099 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
5100 return 1;
5101 }
5102 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005103 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005104 res = obj2ast_expr(tmp, &annotation, arena);
5105 if (res != 0) goto failed;
5106 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005107 }
5108 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005109 return 1;
5110 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005111 if (tmp == NULL || tmp == Py_None) {
5112 Py_CLEAR(tmp);
5113 value = NULL;
5114 }
5115 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005116 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005117 res = obj2ast_expr(tmp, &value, arena);
5118 if (res != 0) goto failed;
5119 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005120 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005121 if (_PyObject_LookupAttrId(obj, &PyId_simple, &tmp) < 0) {
5122 return 1;
5123 }
5124 if (tmp == NULL) {
5125 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
5126 return 1;
5127 }
5128 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005129 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005130 res = obj2ast_int(tmp, &simple, arena);
5131 if (res != 0) goto failed;
5132 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005133 }
5134 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005135 end_lineno, end_col_offset, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005136 if (*out == NULL) goto failed;
5137 return 0;
5138 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005139 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
5140 if (isinstance == -1) {
5141 return 1;
5142 }
5143 if (isinstance) {
5144 expr_ty target;
5145 expr_ty iter;
5146 asdl_seq* body;
5147 asdl_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005148 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005149
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005150 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5151 return 1;
5152 }
5153 if (tmp == NULL) {
5154 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
5155 return 1;
5156 }
5157 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005158 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005159 res = obj2ast_expr(tmp, &target, arena);
5160 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005161 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005162 }
5163 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005164 return 1;
5165 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005166 if (tmp == NULL) {
5167 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
5168 return 1;
5169 }
5170 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005171 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005172 res = obj2ast_expr(tmp, &iter, arena);
5173 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005174 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005175 }
5176 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005177 return 1;
5178 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005179 if (tmp == NULL) {
5180 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
5181 return 1;
5182 }
5183 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005184 int res;
5185 Py_ssize_t len;
5186 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005187 if (!PyList_Check(tmp)) {
5188 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5189 goto failed;
5190 }
5191 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005192 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005193 if (body == NULL) goto failed;
5194 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005195 stmt_ty val;
5196 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005197 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005198 if (len != PyList_GET_SIZE(tmp)) {
5199 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
5200 goto failed;
5201 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005202 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005203 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005204 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005205 }
5206 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005207 return 1;
5208 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005209 if (tmp == NULL) {
5210 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
5211 return 1;
5212 }
5213 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005214 int res;
5215 Py_ssize_t len;
5216 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005217 if (!PyList_Check(tmp)) {
5218 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5219 goto failed;
5220 }
5221 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005222 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005223 if (orelse == NULL) goto failed;
5224 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005225 stmt_ty val;
5226 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005227 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005228 if (len != PyList_GET_SIZE(tmp)) {
5229 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
5230 goto failed;
5231 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005232 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005233 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005234 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005235 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005236 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5237 return 1;
5238 }
5239 if (tmp == NULL || tmp == Py_None) {
5240 Py_CLEAR(tmp);
5241 type_comment = NULL;
5242 }
5243 else {
5244 int res;
5245 res = obj2ast_string(tmp, &type_comment, arena);
5246 if (res != 0) goto failed;
5247 Py_CLEAR(tmp);
5248 }
5249 *out = For(target, iter, body, orelse, type_comment, lineno,
5250 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005251 if (*out == NULL) goto failed;
5252 return 0;
5253 }
Yury Selivanov75445082015-05-11 22:57:16 -04005254 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
5255 if (isinstance == -1) {
5256 return 1;
5257 }
5258 if (isinstance) {
5259 expr_ty target;
5260 expr_ty iter;
5261 asdl_seq* body;
5262 asdl_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005263 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04005264
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005265 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5266 return 1;
5267 }
5268 if (tmp == NULL) {
5269 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
5270 return 1;
5271 }
5272 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005273 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04005274 res = obj2ast_expr(tmp, &target, arena);
5275 if (res != 0) goto failed;
5276 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005277 }
5278 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005279 return 1;
5280 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005281 if (tmp == NULL) {
5282 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
5283 return 1;
5284 }
5285 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005286 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04005287 res = obj2ast_expr(tmp, &iter, arena);
5288 if (res != 0) goto failed;
5289 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005290 }
5291 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005292 return 1;
5293 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005294 if (tmp == NULL) {
5295 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
5296 return 1;
5297 }
5298 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005299 int res;
5300 Py_ssize_t len;
5301 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005302 if (!PyList_Check(tmp)) {
5303 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5304 goto failed;
5305 }
5306 len = PyList_GET_SIZE(tmp);
5307 body = _Py_asdl_seq_new(len, arena);
5308 if (body == NULL) goto failed;
5309 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005310 stmt_ty val;
5311 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005312 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005313 if (len != PyList_GET_SIZE(tmp)) {
5314 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
5315 goto failed;
5316 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005317 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005318 }
5319 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005320 }
5321 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005322 return 1;
5323 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005324 if (tmp == NULL) {
5325 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
5326 return 1;
5327 }
5328 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005329 int res;
5330 Py_ssize_t len;
5331 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005332 if (!PyList_Check(tmp)) {
5333 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5334 goto failed;
5335 }
5336 len = PyList_GET_SIZE(tmp);
5337 orelse = _Py_asdl_seq_new(len, arena);
5338 if (orelse == NULL) goto failed;
5339 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005340 stmt_ty val;
5341 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005342 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005343 if (len != PyList_GET_SIZE(tmp)) {
5344 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
5345 goto failed;
5346 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005347 asdl_seq_SET(orelse, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005348 }
5349 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04005350 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005351 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5352 return 1;
5353 }
5354 if (tmp == NULL || tmp == Py_None) {
5355 Py_CLEAR(tmp);
5356 type_comment = NULL;
5357 }
5358 else {
5359 int res;
5360 res = obj2ast_string(tmp, &type_comment, arena);
5361 if (res != 0) goto failed;
5362 Py_CLEAR(tmp);
5363 }
5364 *out = AsyncFor(target, iter, body, orelse, type_comment, lineno,
5365 col_offset, end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005366 if (*out == NULL) goto failed;
5367 return 0;
5368 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005369 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
5370 if (isinstance == -1) {
5371 return 1;
5372 }
5373 if (isinstance) {
5374 expr_ty test;
5375 asdl_seq* body;
5376 asdl_seq* orelse;
5377
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005378 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
5379 return 1;
5380 }
5381 if (tmp == NULL) {
5382 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
5383 return 1;
5384 }
5385 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005386 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005387 res = obj2ast_expr(tmp, &test, arena);
5388 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005389 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005390 }
5391 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005392 return 1;
5393 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005394 if (tmp == NULL) {
5395 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
5396 return 1;
5397 }
5398 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005399 int res;
5400 Py_ssize_t len;
5401 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005402 if (!PyList_Check(tmp)) {
5403 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5404 goto failed;
5405 }
5406 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005407 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005408 if (body == NULL) goto failed;
5409 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005410 stmt_ty val;
5411 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005412 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005413 if (len != PyList_GET_SIZE(tmp)) {
5414 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
5415 goto failed;
5416 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005417 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005418 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005419 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005420 }
5421 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005422 return 1;
5423 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005424 if (tmp == NULL) {
5425 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
5426 return 1;
5427 }
5428 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005429 int res;
5430 Py_ssize_t len;
5431 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005432 if (!PyList_Check(tmp)) {
5433 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5434 goto failed;
5435 }
5436 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005437 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005438 if (orelse == NULL) goto failed;
5439 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005440 stmt_ty val;
5441 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005442 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005443 if (len != PyList_GET_SIZE(tmp)) {
5444 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
5445 goto failed;
5446 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005447 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005448 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005449 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005450 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005451 *out = While(test, body, orelse, lineno, col_offset, end_lineno,
5452 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005453 if (*out == NULL) goto failed;
5454 return 0;
5455 }
5456 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
5457 if (isinstance == -1) {
5458 return 1;
5459 }
5460 if (isinstance) {
5461 expr_ty test;
5462 asdl_seq* body;
5463 asdl_seq* orelse;
5464
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005465 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
5466 return 1;
5467 }
5468 if (tmp == NULL) {
5469 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5470 return 1;
5471 }
5472 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005473 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005474 res = obj2ast_expr(tmp, &test, arena);
5475 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005476 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005477 }
5478 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005479 return 1;
5480 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005481 if (tmp == NULL) {
5482 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5483 return 1;
5484 }
5485 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005486 int res;
5487 Py_ssize_t len;
5488 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005489 if (!PyList_Check(tmp)) {
5490 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5491 goto failed;
5492 }
5493 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005494 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005495 if (body == NULL) goto failed;
5496 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005497 stmt_ty val;
5498 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005499 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005500 if (len != PyList_GET_SIZE(tmp)) {
5501 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5502 goto failed;
5503 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005504 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005505 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005506 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005507 }
5508 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005509 return 1;
5510 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005511 if (tmp == NULL) {
5512 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5513 return 1;
5514 }
5515 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005516 int res;
5517 Py_ssize_t len;
5518 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005519 if (!PyList_Check(tmp)) {
5520 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5521 goto failed;
5522 }
5523 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005524 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005525 if (orelse == NULL) goto failed;
5526 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005527 stmt_ty val;
5528 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005529 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005530 if (len != PyList_GET_SIZE(tmp)) {
5531 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5532 goto failed;
5533 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005534 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005535 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005536 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005537 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005538 *out = If(test, body, orelse, lineno, col_offset, end_lineno,
5539 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005540 if (*out == NULL) goto failed;
5541 return 0;
5542 }
5543 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5544 if (isinstance == -1) {
5545 return 1;
5546 }
5547 if (isinstance) {
5548 asdl_seq* items;
5549 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005550 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005551
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005552 if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
5553 return 1;
5554 }
5555 if (tmp == NULL) {
5556 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5557 return 1;
5558 }
5559 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005560 int res;
5561 Py_ssize_t len;
5562 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005563 if (!PyList_Check(tmp)) {
5564 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5565 goto failed;
5566 }
5567 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005568 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005569 if (items == NULL) goto failed;
5570 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005571 withitem_ty val;
5572 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005573 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005574 if (len != PyList_GET_SIZE(tmp)) {
5575 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5576 goto failed;
5577 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005578 asdl_seq_SET(items, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005579 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005580 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005581 }
5582 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005583 return 1;
5584 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005585 if (tmp == NULL) {
5586 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5587 return 1;
5588 }
5589 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005590 int res;
5591 Py_ssize_t len;
5592 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005593 if (!PyList_Check(tmp)) {
5594 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5595 goto failed;
5596 }
5597 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005598 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005599 if (body == NULL) goto failed;
5600 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005601 stmt_ty val;
5602 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005603 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005604 if (len != PyList_GET_SIZE(tmp)) {
5605 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5606 goto failed;
5607 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005608 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005609 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005610 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005611 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005612 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5613 return 1;
5614 }
5615 if (tmp == NULL || tmp == Py_None) {
5616 Py_CLEAR(tmp);
5617 type_comment = NULL;
5618 }
5619 else {
5620 int res;
5621 res = obj2ast_string(tmp, &type_comment, arena);
5622 if (res != 0) goto failed;
5623 Py_CLEAR(tmp);
5624 }
5625 *out = With(items, body, type_comment, lineno, col_offset, end_lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005626 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005627 if (*out == NULL) goto failed;
5628 return 0;
5629 }
Yury Selivanov75445082015-05-11 22:57:16 -04005630 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5631 if (isinstance == -1) {
5632 return 1;
5633 }
5634 if (isinstance) {
5635 asdl_seq* items;
5636 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005637 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04005638
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005639 if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
5640 return 1;
5641 }
5642 if (tmp == NULL) {
5643 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5644 return 1;
5645 }
5646 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005647 int res;
5648 Py_ssize_t len;
5649 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005650 if (!PyList_Check(tmp)) {
5651 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5652 goto failed;
5653 }
5654 len = PyList_GET_SIZE(tmp);
5655 items = _Py_asdl_seq_new(len, arena);
5656 if (items == NULL) goto failed;
5657 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005658 withitem_ty val;
5659 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005660 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005661 if (len != PyList_GET_SIZE(tmp)) {
5662 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5663 goto failed;
5664 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005665 asdl_seq_SET(items, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005666 }
5667 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005668 }
5669 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005670 return 1;
5671 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005672 if (tmp == NULL) {
5673 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5674 return 1;
5675 }
5676 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005677 int res;
5678 Py_ssize_t len;
5679 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005680 if (!PyList_Check(tmp)) {
5681 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5682 goto failed;
5683 }
5684 len = PyList_GET_SIZE(tmp);
5685 body = _Py_asdl_seq_new(len, arena);
5686 if (body == NULL) goto failed;
5687 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005688 stmt_ty val;
5689 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005690 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005691 if (len != PyList_GET_SIZE(tmp)) {
5692 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5693 goto failed;
5694 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005695 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005696 }
5697 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04005698 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005699 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5700 return 1;
5701 }
5702 if (tmp == NULL || tmp == Py_None) {
5703 Py_CLEAR(tmp);
5704 type_comment = NULL;
5705 }
5706 else {
5707 int res;
5708 res = obj2ast_string(tmp, &type_comment, arena);
5709 if (res != 0) goto failed;
5710 Py_CLEAR(tmp);
5711 }
5712 *out = AsyncWith(items, body, type_comment, lineno, col_offset,
5713 end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005714 if (*out == NULL) goto failed;
5715 return 0;
5716 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005717 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5718 if (isinstance == -1) {
5719 return 1;
5720 }
5721 if (isinstance) {
5722 expr_ty exc;
5723 expr_ty cause;
5724
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005725 if (_PyObject_LookupAttrId(obj, &PyId_exc, &tmp) < 0) {
5726 return 1;
5727 }
5728 if (tmp == NULL || tmp == Py_None) {
5729 Py_CLEAR(tmp);
5730 exc = NULL;
5731 }
5732 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005733 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005734 res = obj2ast_expr(tmp, &exc, arena);
5735 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005736 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005737 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005738 if (_PyObject_LookupAttrId(obj, &PyId_cause, &tmp) < 0) {
5739 return 1;
5740 }
5741 if (tmp == NULL || tmp == Py_None) {
5742 Py_CLEAR(tmp);
5743 cause = NULL;
5744 }
5745 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005746 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005747 res = obj2ast_expr(tmp, &cause, arena);
5748 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005749 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005750 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005751 *out = Raise(exc, cause, lineno, col_offset, end_lineno,
5752 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005753 if (*out == NULL) goto failed;
5754 return 0;
5755 }
5756 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5757 if (isinstance == -1) {
5758 return 1;
5759 }
5760 if (isinstance) {
5761 asdl_seq* body;
5762 asdl_seq* handlers;
5763 asdl_seq* orelse;
5764 asdl_seq* finalbody;
5765
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005766 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
5767 return 1;
5768 }
5769 if (tmp == NULL) {
5770 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5771 return 1;
5772 }
5773 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005774 int res;
5775 Py_ssize_t len;
5776 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005777 if (!PyList_Check(tmp)) {
5778 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5779 goto failed;
5780 }
5781 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005782 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005783 if (body == NULL) goto failed;
5784 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005785 stmt_ty val;
5786 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005787 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005788 if (len != PyList_GET_SIZE(tmp)) {
5789 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5790 goto failed;
5791 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005792 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005793 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005794 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005795 }
5796 if (_PyObject_LookupAttrId(obj, &PyId_handlers, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005797 return 1;
5798 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005799 if (tmp == NULL) {
5800 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5801 return 1;
5802 }
5803 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005804 int res;
5805 Py_ssize_t len;
5806 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005807 if (!PyList_Check(tmp)) {
5808 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5809 goto failed;
5810 }
5811 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005812 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005813 if (handlers == NULL) goto failed;
5814 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005815 excepthandler_ty val;
5816 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005817 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005818 if (len != PyList_GET_SIZE(tmp)) {
5819 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5820 goto failed;
5821 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005822 asdl_seq_SET(handlers, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005823 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005824 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005825 }
5826 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005827 return 1;
5828 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005829 if (tmp == NULL) {
5830 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5831 return 1;
5832 }
5833 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005834 int res;
5835 Py_ssize_t len;
5836 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005837 if (!PyList_Check(tmp)) {
5838 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5839 goto failed;
5840 }
5841 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005842 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005843 if (orelse == NULL) goto failed;
5844 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005845 stmt_ty val;
5846 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005847 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005848 if (len != PyList_GET_SIZE(tmp)) {
5849 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5850 goto failed;
5851 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005852 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005853 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005854 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005855 }
5856 if (_PyObject_LookupAttrId(obj, &PyId_finalbody, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005857 return 1;
5858 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005859 if (tmp == NULL) {
5860 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5861 return 1;
5862 }
5863 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005864 int res;
5865 Py_ssize_t len;
5866 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005867 if (!PyList_Check(tmp)) {
5868 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5869 goto failed;
5870 }
5871 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005872 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005873 if (finalbody == NULL) goto failed;
5874 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005875 stmt_ty val;
5876 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005877 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005878 if (len != PyList_GET_SIZE(tmp)) {
5879 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5880 goto failed;
5881 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005882 asdl_seq_SET(finalbody, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005883 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005884 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005885 }
5886 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005887 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005888 if (*out == NULL) goto failed;
5889 return 0;
5890 }
5891 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5892 if (isinstance == -1) {
5893 return 1;
5894 }
5895 if (isinstance) {
5896 expr_ty test;
5897 expr_ty msg;
5898
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005899 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
5900 return 1;
5901 }
5902 if (tmp == NULL) {
5903 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5904 return 1;
5905 }
5906 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005907 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005908 res = obj2ast_expr(tmp, &test, arena);
5909 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005910 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005911 }
5912 if (_PyObject_LookupAttrId(obj, &PyId_msg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005913 return 1;
5914 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005915 if (tmp == NULL || tmp == Py_None) {
5916 Py_CLEAR(tmp);
5917 msg = NULL;
5918 }
5919 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005920 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005921 res = obj2ast_expr(tmp, &msg, arena);
5922 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005923 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005924 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005925 *out = Assert(test, msg, lineno, col_offset, end_lineno,
5926 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005927 if (*out == NULL) goto failed;
5928 return 0;
5929 }
5930 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5931 if (isinstance == -1) {
5932 return 1;
5933 }
5934 if (isinstance) {
5935 asdl_seq* names;
5936
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005937 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
5938 return 1;
5939 }
5940 if (tmp == NULL) {
5941 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5942 return 1;
5943 }
5944 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005945 int res;
5946 Py_ssize_t len;
5947 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005948 if (!PyList_Check(tmp)) {
5949 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5950 goto failed;
5951 }
5952 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005953 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005954 if (names == NULL) goto failed;
5955 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005956 alias_ty val;
5957 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005958 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005959 if (len != PyList_GET_SIZE(tmp)) {
5960 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5961 goto failed;
5962 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005963 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005964 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005965 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005966 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005967 *out = Import(names, lineno, col_offset, end_lineno, end_col_offset,
5968 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005969 if (*out == NULL) goto failed;
5970 return 0;
5971 }
5972 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5973 if (isinstance == -1) {
5974 return 1;
5975 }
5976 if (isinstance) {
5977 identifier module;
5978 asdl_seq* names;
5979 int level;
5980
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005981 if (_PyObject_LookupAttrId(obj, &PyId_module, &tmp) < 0) {
5982 return 1;
5983 }
5984 if (tmp == NULL || tmp == Py_None) {
5985 Py_CLEAR(tmp);
5986 module = NULL;
5987 }
5988 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005989 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005990 res = obj2ast_identifier(tmp, &module, arena);
5991 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005992 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005993 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005994 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
5995 return 1;
5996 }
5997 if (tmp == NULL) {
5998 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5999 return 1;
6000 }
6001 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006002 int res;
6003 Py_ssize_t len;
6004 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006005 if (!PyList_Check(tmp)) {
6006 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6007 goto failed;
6008 }
6009 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006010 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006011 if (names == NULL) goto failed;
6012 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006013 alias_ty val;
6014 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006015 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006016 if (len != PyList_GET_SIZE(tmp)) {
6017 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
6018 goto failed;
6019 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006020 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006021 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006022 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006023 }
6024 if (_PyObject_LookupAttrId(obj, &PyId_level, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006025 return 1;
6026 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006027 if (tmp == NULL || tmp == Py_None) {
6028 Py_CLEAR(tmp);
6029 level = 0;
6030 }
6031 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006032 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006033 res = obj2ast_int(tmp, &level, arena);
6034 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006035 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006037 *out = ImportFrom(module, names, level, lineno, col_offset, end_lineno,
6038 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006039 if (*out == NULL) goto failed;
6040 return 0;
6041 }
6042 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
6043 if (isinstance == -1) {
6044 return 1;
6045 }
6046 if (isinstance) {
6047 asdl_seq* names;
6048
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006049 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6050 return 1;
6051 }
6052 if (tmp == NULL) {
6053 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
6054 return 1;
6055 }
6056 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006057 int res;
6058 Py_ssize_t len;
6059 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006060 if (!PyList_Check(tmp)) {
6061 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6062 goto failed;
6063 }
6064 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006065 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006066 if (names == NULL) goto failed;
6067 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006068 identifier val;
6069 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006070 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006071 if (len != PyList_GET_SIZE(tmp)) {
6072 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
6073 goto failed;
6074 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006075 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006076 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006077 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006078 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006079 *out = Global(names, lineno, col_offset, end_lineno, end_col_offset,
6080 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006081 if (*out == NULL) goto failed;
6082 return 0;
6083 }
6084 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
6085 if (isinstance == -1) {
6086 return 1;
6087 }
6088 if (isinstance) {
6089 asdl_seq* names;
6090
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006091 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6092 return 1;
6093 }
6094 if (tmp == NULL) {
6095 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
6096 return 1;
6097 }
6098 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006099 int res;
6100 Py_ssize_t len;
6101 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006102 if (!PyList_Check(tmp)) {
6103 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6104 goto failed;
6105 }
6106 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006107 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006108 if (names == NULL) goto failed;
6109 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006110 identifier val;
6111 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006112 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006113 if (len != PyList_GET_SIZE(tmp)) {
6114 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
6115 goto failed;
6116 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006117 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006118 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006119 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006120 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006121 *out = Nonlocal(names, lineno, col_offset, end_lineno, end_col_offset,
6122 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006123 if (*out == NULL) goto failed;
6124 return 0;
6125 }
6126 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
6127 if (isinstance == -1) {
6128 return 1;
6129 }
6130 if (isinstance) {
6131 expr_ty value;
6132
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006133 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6134 return 1;
6135 }
6136 if (tmp == NULL) {
6137 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
6138 return 1;
6139 }
6140 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006141 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006142 res = obj2ast_expr(tmp, &value, arena);
6143 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006144 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006145 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006146 *out = Expr(value, lineno, col_offset, end_lineno, end_col_offset,
6147 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006148 if (*out == NULL) goto failed;
6149 return 0;
6150 }
6151 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
6152 if (isinstance == -1) {
6153 return 1;
6154 }
6155 if (isinstance) {
6156
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006157 *out = Pass(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006158 if (*out == NULL) goto failed;
6159 return 0;
6160 }
6161 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
6162 if (isinstance == -1) {
6163 return 1;
6164 }
6165 if (isinstance) {
6166
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006167 *out = Break(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006168 if (*out == NULL) goto failed;
6169 return 0;
6170 }
6171 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
6172 if (isinstance == -1) {
6173 return 1;
6174 }
6175 if (isinstance) {
6176
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006177 *out = Continue(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006178 if (*out == NULL) goto failed;
6179 return 0;
6180 }
6181
6182 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
6183 failed:
6184 Py_XDECREF(tmp);
6185 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006186}
6187
6188int
6189obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
6190{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006191 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006192
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006193 PyObject *tmp = NULL;
6194 int lineno;
6195 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006196 int end_lineno;
6197 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006198
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006199 if (obj == Py_None) {
6200 *out = NULL;
6201 return 0;
6202 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006203 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
6204 return 1;
6205 }
6206 if (tmp == NULL) {
6207 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
6208 return 1;
6209 }
6210 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006211 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006212 res = obj2ast_int(tmp, &lineno, arena);
6213 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006214 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006215 }
6216 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006217 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006218 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006219 if (tmp == NULL) {
6220 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
6221 return 1;
6222 }
6223 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006224 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006225 res = obj2ast_int(tmp, &col_offset, arena);
6226 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006227 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006228 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006229 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
6230 return 1;
6231 }
6232 if (tmp == NULL || tmp == Py_None) {
6233 Py_CLEAR(tmp);
6234 end_lineno = 0;
6235 }
6236 else {
6237 int res;
6238 res = obj2ast_int(tmp, &end_lineno, arena);
6239 if (res != 0) goto failed;
6240 Py_CLEAR(tmp);
6241 }
6242 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
6243 return 1;
6244 }
6245 if (tmp == NULL || tmp == Py_None) {
6246 Py_CLEAR(tmp);
6247 end_col_offset = 0;
6248 }
6249 else {
6250 int res;
6251 res = obj2ast_int(tmp, &end_col_offset, arena);
6252 if (res != 0) goto failed;
6253 Py_CLEAR(tmp);
6254 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006255 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
6256 if (isinstance == -1) {
6257 return 1;
6258 }
6259 if (isinstance) {
6260 boolop_ty op;
6261 asdl_seq* values;
6262
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006263 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
6264 return 1;
6265 }
6266 if (tmp == NULL) {
6267 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
6268 return 1;
6269 }
6270 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006271 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006272 res = obj2ast_boolop(tmp, &op, arena);
6273 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006274 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006275 }
6276 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006277 return 1;
6278 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006279 if (tmp == NULL) {
6280 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
6281 return 1;
6282 }
6283 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006284 int res;
6285 Py_ssize_t len;
6286 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006287 if (!PyList_Check(tmp)) {
6288 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6289 goto failed;
6290 }
6291 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006292 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006293 if (values == NULL) goto failed;
6294 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006295 expr_ty val;
6296 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006297 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006298 if (len != PyList_GET_SIZE(tmp)) {
6299 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
6300 goto failed;
6301 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006302 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006303 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006304 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006305 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006306 *out = BoolOp(op, values, lineno, col_offset, end_lineno,
6307 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006308 if (*out == NULL) goto failed;
6309 return 0;
6310 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07006311 isinstance = PyObject_IsInstance(obj, (PyObject*)NamedExpr_type);
6312 if (isinstance == -1) {
6313 return 1;
6314 }
6315 if (isinstance) {
6316 expr_ty target;
6317 expr_ty value;
6318
6319 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
6320 return 1;
6321 }
6322 if (tmp == NULL) {
6323 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from NamedExpr");
6324 return 1;
6325 }
6326 else {
6327 int res;
6328 res = obj2ast_expr(tmp, &target, arena);
6329 if (res != 0) goto failed;
6330 Py_CLEAR(tmp);
6331 }
6332 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6333 return 1;
6334 }
6335 if (tmp == NULL) {
6336 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NamedExpr");
6337 return 1;
6338 }
6339 else {
6340 int res;
6341 res = obj2ast_expr(tmp, &value, arena);
6342 if (res != 0) goto failed;
6343 Py_CLEAR(tmp);
6344 }
6345 *out = NamedExpr(target, value, lineno, col_offset, end_lineno,
6346 end_col_offset, arena);
6347 if (*out == NULL) goto failed;
6348 return 0;
6349 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006350 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
6351 if (isinstance == -1) {
6352 return 1;
6353 }
6354 if (isinstance) {
6355 expr_ty left;
6356 operator_ty op;
6357 expr_ty right;
6358
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006359 if (_PyObject_LookupAttrId(obj, &PyId_left, &tmp) < 0) {
6360 return 1;
6361 }
6362 if (tmp == NULL) {
6363 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
6364 return 1;
6365 }
6366 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006367 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006368 res = obj2ast_expr(tmp, &left, arena);
6369 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006370 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006371 }
6372 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006373 return 1;
6374 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006375 if (tmp == NULL) {
6376 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
6377 return 1;
6378 }
6379 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006380 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006381 res = obj2ast_operator(tmp, &op, arena);
6382 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006383 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006384 }
6385 if (_PyObject_LookupAttrId(obj, &PyId_right, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386 return 1;
6387 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006388 if (tmp == NULL) {
6389 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
6390 return 1;
6391 }
6392 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006393 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006394 res = obj2ast_expr(tmp, &right, arena);
6395 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006396 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006397 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006398 *out = BinOp(left, op, right, lineno, col_offset, end_lineno,
6399 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006400 if (*out == NULL) goto failed;
6401 return 0;
6402 }
6403 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
6404 if (isinstance == -1) {
6405 return 1;
6406 }
6407 if (isinstance) {
6408 unaryop_ty op;
6409 expr_ty operand;
6410
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006411 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
6412 return 1;
6413 }
6414 if (tmp == NULL) {
6415 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
6416 return 1;
6417 }
6418 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006419 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006420 res = obj2ast_unaryop(tmp, &op, arena);
6421 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006422 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006423 }
6424 if (_PyObject_LookupAttrId(obj, &PyId_operand, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006425 return 1;
6426 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006427 if (tmp == NULL) {
6428 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
6429 return 1;
6430 }
6431 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006432 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006433 res = obj2ast_expr(tmp, &operand, arena);
6434 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006436 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006437 *out = UnaryOp(op, operand, lineno, col_offset, end_lineno,
6438 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006439 if (*out == NULL) goto failed;
6440 return 0;
6441 }
6442 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
6443 if (isinstance == -1) {
6444 return 1;
6445 }
6446 if (isinstance) {
6447 arguments_ty args;
6448 expr_ty body;
6449
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006450 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
6451 return 1;
6452 }
6453 if (tmp == NULL) {
6454 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
6455 return 1;
6456 }
6457 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006458 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006459 res = obj2ast_arguments(tmp, &args, arena);
6460 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006461 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006462 }
6463 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006464 return 1;
6465 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006466 if (tmp == NULL) {
6467 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
6468 return 1;
6469 }
6470 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006471 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006472 res = obj2ast_expr(tmp, &body, arena);
6473 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006474 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006475 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006476 *out = Lambda(args, body, lineno, col_offset, end_lineno,
6477 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006478 if (*out == NULL) goto failed;
6479 return 0;
6480 }
6481 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
6482 if (isinstance == -1) {
6483 return 1;
6484 }
6485 if (isinstance) {
6486 expr_ty test;
6487 expr_ty body;
6488 expr_ty orelse;
6489
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006490 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
6491 return 1;
6492 }
6493 if (tmp == NULL) {
6494 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
6495 return 1;
6496 }
6497 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006498 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006499 res = obj2ast_expr(tmp, &test, arena);
6500 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006501 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006502 }
6503 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 return 1;
6505 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006506 if (tmp == NULL) {
6507 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
6508 return 1;
6509 }
6510 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006511 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006512 res = obj2ast_expr(tmp, &body, arena);
6513 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006514 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006515 }
6516 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006517 return 1;
6518 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006519 if (tmp == NULL) {
6520 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
6521 return 1;
6522 }
6523 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006524 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006525 res = obj2ast_expr(tmp, &orelse, arena);
6526 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006527 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006528 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006529 *out = IfExp(test, body, orelse, lineno, col_offset, end_lineno,
6530 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006531 if (*out == NULL) goto failed;
6532 return 0;
6533 }
6534 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
6535 if (isinstance == -1) {
6536 return 1;
6537 }
6538 if (isinstance) {
6539 asdl_seq* keys;
6540 asdl_seq* values;
6541
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006542 if (_PyObject_LookupAttrId(obj, &PyId_keys, &tmp) < 0) {
6543 return 1;
6544 }
6545 if (tmp == NULL) {
6546 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
6547 return 1;
6548 }
6549 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 int res;
6551 Py_ssize_t len;
6552 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006553 if (!PyList_Check(tmp)) {
6554 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6555 goto failed;
6556 }
6557 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006558 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006559 if (keys == NULL) goto failed;
6560 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006561 expr_ty val;
6562 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006563 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006564 if (len != PyList_GET_SIZE(tmp)) {
6565 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
6566 goto failed;
6567 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006568 asdl_seq_SET(keys, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006569 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006570 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006571 }
6572 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006573 return 1;
6574 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006575 if (tmp == NULL) {
6576 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
6577 return 1;
6578 }
6579 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006580 int res;
6581 Py_ssize_t len;
6582 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 if (!PyList_Check(tmp)) {
6584 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6585 goto failed;
6586 }
6587 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006588 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 if (values == NULL) goto failed;
6590 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006591 expr_ty val;
6592 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006593 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006594 if (len != PyList_GET_SIZE(tmp)) {
6595 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
6596 goto failed;
6597 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006598 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006599 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006600 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006601 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006602 *out = Dict(keys, values, lineno, col_offset, end_lineno,
6603 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006604 if (*out == NULL) goto failed;
6605 return 0;
6606 }
6607 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
6608 if (isinstance == -1) {
6609 return 1;
6610 }
6611 if (isinstance) {
6612 asdl_seq* elts;
6613
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006614 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
6615 return 1;
6616 }
6617 if (tmp == NULL) {
6618 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
6619 return 1;
6620 }
6621 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006622 int res;
6623 Py_ssize_t len;
6624 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006625 if (!PyList_Check(tmp)) {
6626 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6627 goto failed;
6628 }
6629 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006630 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006631 if (elts == NULL) goto failed;
6632 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006633 expr_ty val;
6634 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006635 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006636 if (len != PyList_GET_SIZE(tmp)) {
6637 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
6638 goto failed;
6639 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006640 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006641 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006642 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006643 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006644 *out = Set(elts, lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006645 if (*out == NULL) goto failed;
6646 return 0;
6647 }
6648 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
6649 if (isinstance == -1) {
6650 return 1;
6651 }
6652 if (isinstance) {
6653 expr_ty elt;
6654 asdl_seq* generators;
6655
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006656 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6657 return 1;
6658 }
6659 if (tmp == NULL) {
6660 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
6661 return 1;
6662 }
6663 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006664 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006665 res = obj2ast_expr(tmp, &elt, arena);
6666 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006667 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006668 }
6669 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006670 return 1;
6671 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006672 if (tmp == NULL) {
6673 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6674 return 1;
6675 }
6676 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006677 int res;
6678 Py_ssize_t len;
6679 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006680 if (!PyList_Check(tmp)) {
6681 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6682 goto failed;
6683 }
6684 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006685 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006686 if (generators == NULL) goto failed;
6687 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006688 comprehension_ty val;
6689 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006690 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006691 if (len != PyList_GET_SIZE(tmp)) {
6692 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6693 goto failed;
6694 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006695 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006696 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006697 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006698 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006699 *out = ListComp(elt, generators, lineno, col_offset, end_lineno,
6700 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006701 if (*out == NULL) goto failed;
6702 return 0;
6703 }
6704 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6705 if (isinstance == -1) {
6706 return 1;
6707 }
6708 if (isinstance) {
6709 expr_ty elt;
6710 asdl_seq* generators;
6711
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006712 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6713 return 1;
6714 }
6715 if (tmp == NULL) {
6716 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6717 return 1;
6718 }
6719 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006720 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006721 res = obj2ast_expr(tmp, &elt, arena);
6722 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006723 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006724 }
6725 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006726 return 1;
6727 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006728 if (tmp == NULL) {
6729 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6730 return 1;
6731 }
6732 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006733 int res;
6734 Py_ssize_t len;
6735 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006736 if (!PyList_Check(tmp)) {
6737 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6738 goto failed;
6739 }
6740 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006741 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006742 if (generators == NULL) goto failed;
6743 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006744 comprehension_ty val;
6745 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006746 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006747 if (len != PyList_GET_SIZE(tmp)) {
6748 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6749 goto failed;
6750 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006751 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006752 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006753 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006754 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006755 *out = SetComp(elt, generators, lineno, col_offset, end_lineno,
6756 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006757 if (*out == NULL) goto failed;
6758 return 0;
6759 }
6760 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6761 if (isinstance == -1) {
6762 return 1;
6763 }
6764 if (isinstance) {
6765 expr_ty key;
6766 expr_ty value;
6767 asdl_seq* generators;
6768
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006769 if (_PyObject_LookupAttrId(obj, &PyId_key, &tmp) < 0) {
6770 return 1;
6771 }
6772 if (tmp == NULL) {
6773 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6774 return 1;
6775 }
6776 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006777 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006778 res = obj2ast_expr(tmp, &key, arena);
6779 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006780 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006781 }
6782 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006783 return 1;
6784 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006785 if (tmp == NULL) {
6786 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6787 return 1;
6788 }
6789 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006790 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006791 res = obj2ast_expr(tmp, &value, arena);
6792 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006793 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006794 }
6795 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006796 return 1;
6797 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006798 if (tmp == NULL) {
6799 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6800 return 1;
6801 }
6802 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006803 int res;
6804 Py_ssize_t len;
6805 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006806 if (!PyList_Check(tmp)) {
6807 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6808 goto failed;
6809 }
6810 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006811 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006812 if (generators == NULL) goto failed;
6813 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006814 comprehension_ty val;
6815 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006816 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006817 if (len != PyList_GET_SIZE(tmp)) {
6818 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6819 goto failed;
6820 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006821 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006822 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006823 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006824 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006825 *out = DictComp(key, value, generators, lineno, col_offset, end_lineno,
6826 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006827 if (*out == NULL) goto failed;
6828 return 0;
6829 }
6830 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6831 if (isinstance == -1) {
6832 return 1;
6833 }
6834 if (isinstance) {
6835 expr_ty elt;
6836 asdl_seq* generators;
6837
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006838 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6839 return 1;
6840 }
6841 if (tmp == NULL) {
6842 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6843 return 1;
6844 }
6845 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006846 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006847 res = obj2ast_expr(tmp, &elt, arena);
6848 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006849 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006850 }
6851 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006852 return 1;
6853 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006854 if (tmp == NULL) {
6855 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
6856 return 1;
6857 }
6858 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006859 int res;
6860 Py_ssize_t len;
6861 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006862 if (!PyList_Check(tmp)) {
6863 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6864 goto failed;
6865 }
6866 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006867 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006868 if (generators == NULL) goto failed;
6869 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006870 comprehension_ty val;
6871 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006872 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006873 if (len != PyList_GET_SIZE(tmp)) {
6874 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
6875 goto failed;
6876 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006877 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006878 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006880 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006881 *out = GeneratorExp(elt, generators, lineno, col_offset, end_lineno,
6882 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006883 if (*out == NULL) goto failed;
6884 return 0;
6885 }
Yury Selivanov75445082015-05-11 22:57:16 -04006886 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
6887 if (isinstance == -1) {
6888 return 1;
6889 }
6890 if (isinstance) {
6891 expr_ty value;
6892
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006893 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6894 return 1;
6895 }
6896 if (tmp == NULL) {
6897 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
6898 return 1;
6899 }
6900 else {
Yury Selivanov75445082015-05-11 22:57:16 -04006901 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04006902 res = obj2ast_expr(tmp, &value, arena);
6903 if (res != 0) goto failed;
6904 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04006905 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006906 *out = Await(value, lineno, col_offset, end_lineno, end_col_offset,
6907 arena);
Yury Selivanov75445082015-05-11 22:57:16 -04006908 if (*out == NULL) goto failed;
6909 return 0;
6910 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006911 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
6912 if (isinstance == -1) {
6913 return 1;
6914 }
6915 if (isinstance) {
6916 expr_ty value;
6917
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006918 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6919 return 1;
6920 }
6921 if (tmp == NULL || tmp == Py_None) {
6922 Py_CLEAR(tmp);
6923 value = NULL;
6924 }
6925 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006926 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006927 res = obj2ast_expr(tmp, &value, arena);
6928 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006929 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006930 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006931 *out = Yield(value, lineno, col_offset, end_lineno, end_col_offset,
6932 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006933 if (*out == NULL) goto failed;
6934 return 0;
6935 }
6936 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
6937 if (isinstance == -1) {
6938 return 1;
6939 }
6940 if (isinstance) {
6941 expr_ty value;
6942
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006943 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6944 return 1;
6945 }
6946 if (tmp == NULL) {
6947 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
6948 return 1;
6949 }
6950 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006951 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006952 res = obj2ast_expr(tmp, &value, arena);
6953 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006954 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006955 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006956 *out = YieldFrom(value, lineno, col_offset, end_lineno, end_col_offset,
6957 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006958 if (*out == NULL) goto failed;
6959 return 0;
6960 }
6961 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6962 if (isinstance == -1) {
6963 return 1;
6964 }
6965 if (isinstance) {
6966 expr_ty left;
6967 asdl_int_seq* ops;
6968 asdl_seq* comparators;
6969
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006970 if (_PyObject_LookupAttrId(obj, &PyId_left, &tmp) < 0) {
6971 return 1;
6972 }
6973 if (tmp == NULL) {
6974 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6975 return 1;
6976 }
6977 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006978 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006979 res = obj2ast_expr(tmp, &left, arena);
6980 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006981 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006982 }
6983 if (_PyObject_LookupAttrId(obj, &PyId_ops, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006984 return 1;
6985 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006986 if (tmp == NULL) {
6987 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6988 return 1;
6989 }
6990 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006991 int res;
6992 Py_ssize_t len;
6993 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006994 if (!PyList_Check(tmp)) {
6995 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6996 goto failed;
6997 }
6998 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006999 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007000 if (ops == NULL) goto failed;
7001 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007002 cmpop_ty val;
7003 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007004 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007005 if (len != PyList_GET_SIZE(tmp)) {
7006 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
7007 goto failed;
7008 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007009 asdl_seq_SET(ops, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007010 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007011 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007012 }
7013 if (_PyObject_LookupAttrId(obj, &PyId_comparators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007014 return 1;
7015 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007016 if (tmp == NULL) {
7017 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
7018 return 1;
7019 }
7020 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007021 int res;
7022 Py_ssize_t len;
7023 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007024 if (!PyList_Check(tmp)) {
7025 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7026 goto failed;
7027 }
7028 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007029 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007030 if (comparators == NULL) goto failed;
7031 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007032 expr_ty val;
7033 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007034 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007035 if (len != PyList_GET_SIZE(tmp)) {
7036 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
7037 goto failed;
7038 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007039 asdl_seq_SET(comparators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007040 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007042 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007043 *out = Compare(left, ops, comparators, lineno, col_offset, end_lineno,
7044 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007045 if (*out == NULL) goto failed;
7046 return 0;
7047 }
7048 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
7049 if (isinstance == -1) {
7050 return 1;
7051 }
7052 if (isinstance) {
7053 expr_ty func;
7054 asdl_seq* args;
7055 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007056
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007057 if (_PyObject_LookupAttrId(obj, &PyId_func, &tmp) < 0) {
7058 return 1;
7059 }
7060 if (tmp == NULL) {
7061 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
7062 return 1;
7063 }
7064 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007065 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007066 res = obj2ast_expr(tmp, &func, arena);
7067 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007068 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007069 }
7070 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007071 return 1;
7072 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007073 if (tmp == NULL) {
7074 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
7075 return 1;
7076 }
7077 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007078 int res;
7079 Py_ssize_t len;
7080 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007081 if (!PyList_Check(tmp)) {
7082 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7083 goto failed;
7084 }
7085 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007086 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007087 if (args == NULL) goto failed;
7088 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007089 expr_ty val;
7090 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007091 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007092 if (len != PyList_GET_SIZE(tmp)) {
7093 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
7094 goto failed;
7095 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007096 asdl_seq_SET(args, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007097 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007098 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007099 }
7100 if (_PyObject_LookupAttrId(obj, &PyId_keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007101 return 1;
7102 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007103 if (tmp == NULL) {
7104 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
7105 return 1;
7106 }
7107 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007108 int res;
7109 Py_ssize_t len;
7110 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007111 if (!PyList_Check(tmp)) {
7112 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7113 goto failed;
7114 }
7115 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007116 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007117 if (keywords == NULL) goto failed;
7118 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007119 keyword_ty val;
7120 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007121 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007122 if (len != PyList_GET_SIZE(tmp)) {
7123 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
7124 goto failed;
7125 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007126 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007127 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007128 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007129 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007130 *out = Call(func, args, keywords, lineno, col_offset, end_lineno,
7131 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007132 if (*out == NULL) goto failed;
7133 return 0;
7134 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04007135 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
7136 if (isinstance == -1) {
7137 return 1;
7138 }
7139 if (isinstance) {
7140 expr_ty value;
7141 int conversion;
7142 expr_ty format_spec;
7143
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007144 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7145 return 1;
7146 }
7147 if (tmp == NULL) {
7148 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
7149 return 1;
7150 }
7151 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007152 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007153 res = obj2ast_expr(tmp, &value, arena);
7154 if (res != 0) goto failed;
7155 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007156 }
7157 if (_PyObject_LookupAttrId(obj, &PyId_conversion, &tmp) < 0) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007158 return 1;
7159 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007160 if (tmp == NULL || tmp == Py_None) {
7161 Py_CLEAR(tmp);
7162 conversion = 0;
7163 }
7164 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007165 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007166 res = obj2ast_int(tmp, &conversion, arena);
7167 if (res != 0) goto failed;
7168 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007169 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007170 if (_PyObject_LookupAttrId(obj, &PyId_format_spec, &tmp) < 0) {
7171 return 1;
7172 }
7173 if (tmp == NULL || tmp == Py_None) {
7174 Py_CLEAR(tmp);
7175 format_spec = NULL;
7176 }
7177 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007178 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007179 res = obj2ast_expr(tmp, &format_spec, arena);
7180 if (res != 0) goto failed;
7181 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007182 }
7183 *out = FormattedValue(value, conversion, format_spec, lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007184 col_offset, end_lineno, end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007185 if (*out == NULL) goto failed;
7186 return 0;
7187 }
7188 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
7189 if (isinstance == -1) {
7190 return 1;
7191 }
7192 if (isinstance) {
7193 asdl_seq* values;
7194
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007195 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
7196 return 1;
7197 }
7198 if (tmp == NULL) {
7199 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
7200 return 1;
7201 }
7202 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007203 int res;
7204 Py_ssize_t len;
7205 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007206 if (!PyList_Check(tmp)) {
7207 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7208 goto failed;
7209 }
7210 len = PyList_GET_SIZE(tmp);
7211 values = _Py_asdl_seq_new(len, arena);
7212 if (values == NULL) goto failed;
7213 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007214 expr_ty val;
7215 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007216 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03007217 if (len != PyList_GET_SIZE(tmp)) {
7218 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
7219 goto failed;
7220 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007221 asdl_seq_SET(values, i, val);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007222 }
7223 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007224 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007225 *out = JoinedStr(values, lineno, col_offset, end_lineno,
7226 end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007227 if (*out == NULL) goto failed;
7228 return 0;
7229 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007230 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
7231 if (isinstance == -1) {
7232 return 1;
7233 }
7234 if (isinstance) {
7235 constant value;
7236
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007237 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7238 return 1;
7239 }
7240 if (tmp == NULL) {
7241 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
7242 return 1;
7243 }
7244 else {
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007245 int res;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007246 res = obj2ast_constant(tmp, &value, arena);
7247 if (res != 0) goto failed;
7248 Py_CLEAR(tmp);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007249 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007250 *out = Constant(value, lineno, col_offset, end_lineno, end_col_offset,
7251 arena);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007252 if (*out == NULL) goto failed;
7253 return 0;
7254 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007255 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
7256 if (isinstance == -1) {
7257 return 1;
7258 }
7259 if (isinstance) {
7260 expr_ty value;
7261 identifier attr;
7262 expr_context_ty ctx;
7263
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007264 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7265 return 1;
7266 }
7267 if (tmp == NULL) {
7268 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
7269 return 1;
7270 }
7271 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007272 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007273 res = obj2ast_expr(tmp, &value, arena);
7274 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007275 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007276 }
7277 if (_PyObject_LookupAttrId(obj, &PyId_attr, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007278 return 1;
7279 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007280 if (tmp == NULL) {
7281 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
7282 return 1;
7283 }
7284 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007285 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007286 res = obj2ast_identifier(tmp, &attr, arena);
7287 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007288 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007289 }
7290 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007291 return 1;
7292 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007293 if (tmp == NULL) {
7294 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
7295 return 1;
7296 }
7297 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007298 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007299 res = obj2ast_expr_context(tmp, &ctx, arena);
7300 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007301 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007302 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007303 *out = Attribute(value, attr, ctx, lineno, col_offset, end_lineno,
7304 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007305 if (*out == NULL) goto failed;
7306 return 0;
7307 }
7308 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
7309 if (isinstance == -1) {
7310 return 1;
7311 }
7312 if (isinstance) {
7313 expr_ty value;
7314 slice_ty slice;
7315 expr_context_ty ctx;
7316
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007317 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7318 return 1;
7319 }
7320 if (tmp == NULL) {
7321 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
7322 return 1;
7323 }
7324 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007325 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 res = obj2ast_expr(tmp, &value, arena);
7327 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007328 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007329 }
7330 if (_PyObject_LookupAttrId(obj, &PyId_slice, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007331 return 1;
7332 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007333 if (tmp == NULL) {
7334 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
7335 return 1;
7336 }
7337 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007338 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007339 res = obj2ast_slice(tmp, &slice, arena);
7340 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007341 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007342 }
7343 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007344 return 1;
7345 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007346 if (tmp == NULL) {
7347 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
7348 return 1;
7349 }
7350 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007351 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007352 res = obj2ast_expr_context(tmp, &ctx, arena);
7353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007355 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007356 *out = Subscript(value, slice, ctx, lineno, col_offset, end_lineno,
7357 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007358 if (*out == NULL) goto failed;
7359 return 0;
7360 }
7361 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
7362 if (isinstance == -1) {
7363 return 1;
7364 }
7365 if (isinstance) {
7366 expr_ty value;
7367 expr_context_ty ctx;
7368
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007369 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7370 return 1;
7371 }
7372 if (tmp == NULL) {
7373 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
7374 return 1;
7375 }
7376 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007377 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007378 res = obj2ast_expr(tmp, &value, arena);
7379 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007380 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007381 }
7382 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007383 return 1;
7384 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007385 if (tmp == NULL) {
7386 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
7387 return 1;
7388 }
7389 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007390 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007391 res = obj2ast_expr_context(tmp, &ctx, arena);
7392 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007393 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007394 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007395 *out = Starred(value, ctx, lineno, col_offset, end_lineno,
7396 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007397 if (*out == NULL) goto failed;
7398 return 0;
7399 }
7400 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
7401 if (isinstance == -1) {
7402 return 1;
7403 }
7404 if (isinstance) {
7405 identifier id;
7406 expr_context_ty ctx;
7407
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007408 if (_PyObject_LookupAttrId(obj, &PyId_id, &tmp) < 0) {
7409 return 1;
7410 }
7411 if (tmp == NULL) {
7412 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
7413 return 1;
7414 }
7415 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007416 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007417 res = obj2ast_identifier(tmp, &id, arena);
7418 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007419 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007420 }
7421 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007422 return 1;
7423 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007424 if (tmp == NULL) {
7425 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
7426 return 1;
7427 }
7428 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007429 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007430 res = obj2ast_expr_context(tmp, &ctx, arena);
7431 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007432 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007433 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007434 *out = Name(id, ctx, lineno, col_offset, end_lineno, end_col_offset,
7435 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007436 if (*out == NULL) goto failed;
7437 return 0;
7438 }
7439 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
7440 if (isinstance == -1) {
7441 return 1;
7442 }
7443 if (isinstance) {
7444 asdl_seq* elts;
7445 expr_context_ty ctx;
7446
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007447 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
7448 return 1;
7449 }
7450 if (tmp == NULL) {
7451 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
7452 return 1;
7453 }
7454 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007455 int res;
7456 Py_ssize_t len;
7457 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007458 if (!PyList_Check(tmp)) {
7459 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7460 goto failed;
7461 }
7462 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007463 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007464 if (elts == NULL) goto failed;
7465 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007466 expr_ty val;
7467 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007468 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007469 if (len != PyList_GET_SIZE(tmp)) {
7470 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
7471 goto failed;
7472 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007473 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007474 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007475 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007476 }
7477 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007478 return 1;
7479 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007480 if (tmp == NULL) {
7481 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
7482 return 1;
7483 }
7484 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007485 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007486 res = obj2ast_expr_context(tmp, &ctx, arena);
7487 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007488 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007489 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007490 *out = List(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
7491 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007492 if (*out == NULL) goto failed;
7493 return 0;
7494 }
7495 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
7496 if (isinstance == -1) {
7497 return 1;
7498 }
7499 if (isinstance) {
7500 asdl_seq* elts;
7501 expr_context_ty ctx;
7502
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007503 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
7504 return 1;
7505 }
7506 if (tmp == NULL) {
7507 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
7508 return 1;
7509 }
7510 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007511 int res;
7512 Py_ssize_t len;
7513 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007514 if (!PyList_Check(tmp)) {
7515 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7516 goto failed;
7517 }
7518 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007519 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007520 if (elts == NULL) goto failed;
7521 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007522 expr_ty val;
7523 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007524 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007525 if (len != PyList_GET_SIZE(tmp)) {
7526 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
7527 goto failed;
7528 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007529 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007530 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007531 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007532 }
7533 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007534 return 1;
7535 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007536 if (tmp == NULL) {
7537 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
7538 return 1;
7539 }
7540 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007541 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007542 res = obj2ast_expr_context(tmp, &ctx, arena);
7543 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007544 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007545 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007546 *out = Tuple(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
7547 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007548 if (*out == NULL) goto failed;
7549 return 0;
7550 }
7551
7552 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
7553 failed:
7554 Py_XDECREF(tmp);
7555 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007556}
7557
7558int
7559obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
7560{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007561 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007562
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007563 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
7564 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007565 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007566 }
7567 if (isinstance) {
7568 *out = Load;
7569 return 0;
7570 }
7571 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
7572 if (isinstance == -1) {
7573 return 1;
7574 }
7575 if (isinstance) {
7576 *out = Store;
7577 return 0;
7578 }
7579 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
7580 if (isinstance == -1) {
7581 return 1;
7582 }
7583 if (isinstance) {
7584 *out = Del;
7585 return 0;
7586 }
7587 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
7588 if (isinstance == -1) {
7589 return 1;
7590 }
7591 if (isinstance) {
7592 *out = AugLoad;
7593 return 0;
7594 }
7595 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
7596 if (isinstance == -1) {
7597 return 1;
7598 }
7599 if (isinstance) {
7600 *out = AugStore;
7601 return 0;
7602 }
7603 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
7604 if (isinstance == -1) {
7605 return 1;
7606 }
7607 if (isinstance) {
7608 *out = Param;
7609 return 0;
7610 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07007611 isinstance = PyObject_IsInstance(obj, (PyObject *)NamedStore_type);
7612 if (isinstance == -1) {
7613 return 1;
7614 }
7615 if (isinstance) {
7616 *out = NamedStore;
7617 return 0;
7618 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007619
7620 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
7621 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007622}
7623
7624int
7625obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
7626{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007627 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007628
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007629 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007630
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007631 if (obj == Py_None) {
7632 *out = NULL;
7633 return 0;
7634 }
7635 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
7636 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007637 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007638 }
7639 if (isinstance) {
7640 expr_ty lower;
7641 expr_ty upper;
7642 expr_ty step;
7643
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007644 if (_PyObject_LookupAttrId(obj, &PyId_lower, &tmp) < 0) {
7645 return 1;
7646 }
7647 if (tmp == NULL || tmp == Py_None) {
7648 Py_CLEAR(tmp);
7649 lower = NULL;
7650 }
7651 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007652 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007653 res = obj2ast_expr(tmp, &lower, arena);
7654 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007655 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007656 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007657 if (_PyObject_LookupAttrId(obj, &PyId_upper, &tmp) < 0) {
7658 return 1;
7659 }
7660 if (tmp == NULL || tmp == Py_None) {
7661 Py_CLEAR(tmp);
7662 upper = NULL;
7663 }
7664 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007665 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007666 res = obj2ast_expr(tmp, &upper, arena);
7667 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007668 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007669 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007670 if (_PyObject_LookupAttrId(obj, &PyId_step, &tmp) < 0) {
7671 return 1;
7672 }
7673 if (tmp == NULL || tmp == Py_None) {
7674 Py_CLEAR(tmp);
7675 step = NULL;
7676 }
7677 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007678 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007679 res = obj2ast_expr(tmp, &step, arena);
7680 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007681 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007682 }
7683 *out = Slice(lower, upper, step, arena);
7684 if (*out == NULL) goto failed;
7685 return 0;
7686 }
7687 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7688 if (isinstance == -1) {
7689 return 1;
7690 }
7691 if (isinstance) {
7692 asdl_seq* dims;
7693
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007694 if (_PyObject_LookupAttrId(obj, &PyId_dims, &tmp) < 0) {
7695 return 1;
7696 }
7697 if (tmp == NULL) {
7698 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7699 return 1;
7700 }
7701 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007702 int res;
7703 Py_ssize_t len;
7704 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007705 if (!PyList_Check(tmp)) {
7706 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7707 goto failed;
7708 }
7709 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007710 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007711 if (dims == NULL) goto failed;
7712 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007713 slice_ty val;
7714 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007715 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007716 if (len != PyList_GET_SIZE(tmp)) {
7717 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7718 goto failed;
7719 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007720 asdl_seq_SET(dims, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007721 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007722 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007723 }
7724 *out = ExtSlice(dims, arena);
7725 if (*out == NULL) goto failed;
7726 return 0;
7727 }
7728 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7729 if (isinstance == -1) {
7730 return 1;
7731 }
7732 if (isinstance) {
7733 expr_ty value;
7734
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007735 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7736 return 1;
7737 }
7738 if (tmp == NULL) {
7739 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7740 return 1;
7741 }
7742 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007743 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007744 res = obj2ast_expr(tmp, &value, arena);
7745 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007746 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007747 }
7748 *out = Index(value, arena);
7749 if (*out == NULL) goto failed;
7750 return 0;
7751 }
7752
7753 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7754 failed:
7755 Py_XDECREF(tmp);
7756 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007757}
7758
7759int
7760obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
7761{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007762 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007763
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007764 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
7765 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007766 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007767 }
7768 if (isinstance) {
7769 *out = And;
7770 return 0;
7771 }
7772 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7773 if (isinstance == -1) {
7774 return 1;
7775 }
7776 if (isinstance) {
7777 *out = Or;
7778 return 0;
7779 }
7780
7781 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7782 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007783}
7784
7785int
7786obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7787{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007788 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007789
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007790 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7791 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007792 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007793 }
7794 if (isinstance) {
7795 *out = Add;
7796 return 0;
7797 }
7798 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7799 if (isinstance == -1) {
7800 return 1;
7801 }
7802 if (isinstance) {
7803 *out = Sub;
7804 return 0;
7805 }
7806 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7807 if (isinstance == -1) {
7808 return 1;
7809 }
7810 if (isinstance) {
7811 *out = Mult;
7812 return 0;
7813 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007814 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7815 if (isinstance == -1) {
7816 return 1;
7817 }
7818 if (isinstance) {
7819 *out = MatMult;
7820 return 0;
7821 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007822 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
7823 if (isinstance == -1) {
7824 return 1;
7825 }
7826 if (isinstance) {
7827 *out = Div;
7828 return 0;
7829 }
7830 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
7831 if (isinstance == -1) {
7832 return 1;
7833 }
7834 if (isinstance) {
7835 *out = Mod;
7836 return 0;
7837 }
7838 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
7839 if (isinstance == -1) {
7840 return 1;
7841 }
7842 if (isinstance) {
7843 *out = Pow;
7844 return 0;
7845 }
7846 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
7847 if (isinstance == -1) {
7848 return 1;
7849 }
7850 if (isinstance) {
7851 *out = LShift;
7852 return 0;
7853 }
7854 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
7855 if (isinstance == -1) {
7856 return 1;
7857 }
7858 if (isinstance) {
7859 *out = RShift;
7860 return 0;
7861 }
7862 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
7863 if (isinstance == -1) {
7864 return 1;
7865 }
7866 if (isinstance) {
7867 *out = BitOr;
7868 return 0;
7869 }
7870 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
7871 if (isinstance == -1) {
7872 return 1;
7873 }
7874 if (isinstance) {
7875 *out = BitXor;
7876 return 0;
7877 }
7878 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
7879 if (isinstance == -1) {
7880 return 1;
7881 }
7882 if (isinstance) {
7883 *out = BitAnd;
7884 return 0;
7885 }
7886 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
7887 if (isinstance == -1) {
7888 return 1;
7889 }
7890 if (isinstance) {
7891 *out = FloorDiv;
7892 return 0;
7893 }
7894
7895 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
7896 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007897}
7898
7899int
7900obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
7901{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007902 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007903
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007904 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
7905 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007906 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007907 }
7908 if (isinstance) {
7909 *out = Invert;
7910 return 0;
7911 }
7912 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
7913 if (isinstance == -1) {
7914 return 1;
7915 }
7916 if (isinstance) {
7917 *out = Not;
7918 return 0;
7919 }
7920 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
7921 if (isinstance == -1) {
7922 return 1;
7923 }
7924 if (isinstance) {
7925 *out = UAdd;
7926 return 0;
7927 }
7928 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
7929 if (isinstance == -1) {
7930 return 1;
7931 }
7932 if (isinstance) {
7933 *out = USub;
7934 return 0;
7935 }
7936
7937 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
7938 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007939}
7940
7941int
7942obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
7943{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007944 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007945
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007946 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
7947 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007948 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007949 }
7950 if (isinstance) {
7951 *out = Eq;
7952 return 0;
7953 }
7954 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
7955 if (isinstance == -1) {
7956 return 1;
7957 }
7958 if (isinstance) {
7959 *out = NotEq;
7960 return 0;
7961 }
7962 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
7963 if (isinstance == -1) {
7964 return 1;
7965 }
7966 if (isinstance) {
7967 *out = Lt;
7968 return 0;
7969 }
7970 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
7971 if (isinstance == -1) {
7972 return 1;
7973 }
7974 if (isinstance) {
7975 *out = LtE;
7976 return 0;
7977 }
7978 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
7979 if (isinstance == -1) {
7980 return 1;
7981 }
7982 if (isinstance) {
7983 *out = Gt;
7984 return 0;
7985 }
7986 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
7987 if (isinstance == -1) {
7988 return 1;
7989 }
7990 if (isinstance) {
7991 *out = GtE;
7992 return 0;
7993 }
7994 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
7995 if (isinstance == -1) {
7996 return 1;
7997 }
7998 if (isinstance) {
7999 *out = Is;
8000 return 0;
8001 }
8002 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
8003 if (isinstance == -1) {
8004 return 1;
8005 }
8006 if (isinstance) {
8007 *out = IsNot;
8008 return 0;
8009 }
8010 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
8011 if (isinstance == -1) {
8012 return 1;
8013 }
8014 if (isinstance) {
8015 *out = In;
8016 return 0;
8017 }
8018 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
8019 if (isinstance == -1) {
8020 return 1;
8021 }
8022 if (isinstance) {
8023 *out = NotIn;
8024 return 0;
8025 }
8026
8027 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
8028 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008029}
8030
8031int
8032obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
8033{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008034 PyObject* tmp = NULL;
8035 expr_ty target;
8036 expr_ty iter;
8037 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008038 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008039
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008040 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
8041 return 1;
8042 }
8043 if (tmp == NULL) {
8044 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
8045 return 1;
8046 }
8047 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008048 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008049 res = obj2ast_expr(tmp, &target, arena);
8050 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008051 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008052 }
8053 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008054 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008055 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008056 if (tmp == NULL) {
8057 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
8058 return 1;
8059 }
8060 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008061 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008062 res = obj2ast_expr(tmp, &iter, arena);
8063 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008064 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008065 }
8066 if (_PyObject_LookupAttrId(obj, &PyId_ifs, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008067 return 1;
8068 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008069 if (tmp == NULL) {
8070 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
8071 return 1;
8072 }
8073 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008074 int res;
8075 Py_ssize_t len;
8076 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008077 if (!PyList_Check(tmp)) {
8078 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8079 goto failed;
8080 }
8081 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008082 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008083 if (ifs == NULL) goto failed;
8084 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008085 expr_ty val;
8086 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008087 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008088 if (len != PyList_GET_SIZE(tmp)) {
8089 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
8090 goto failed;
8091 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008092 asdl_seq_SET(ifs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008093 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008094 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008095 }
8096 if (_PyObject_LookupAttrId(obj, &PyId_is_async, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008097 return 1;
8098 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008099 if (tmp == NULL) {
8100 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
8101 return 1;
8102 }
8103 else {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008104 int res;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008105 res = obj2ast_int(tmp, &is_async, arena);
8106 if (res != 0) goto failed;
8107 Py_CLEAR(tmp);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008108 }
8109 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008110 return 0;
8111failed:
8112 Py_XDECREF(tmp);
8113 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008114}
8115
8116int
8117obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
8118{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008119 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00008120
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008121 PyObject *tmp = NULL;
8122 int lineno;
8123 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008124 int end_lineno;
8125 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008126
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008127 if (obj == Py_None) {
8128 *out = NULL;
8129 return 0;
8130 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008131 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8132 return 1;
8133 }
8134 if (tmp == NULL) {
8135 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
8136 return 1;
8137 }
8138 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008139 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008140 res = obj2ast_int(tmp, &lineno, arena);
8141 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008142 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008143 }
8144 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008145 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008146 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008147 if (tmp == NULL) {
8148 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
8149 return 1;
8150 }
8151 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008152 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008153 res = obj2ast_int(tmp, &col_offset, arena);
8154 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008155 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008156 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008157 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
8158 return 1;
8159 }
8160 if (tmp == NULL || tmp == Py_None) {
8161 Py_CLEAR(tmp);
8162 end_lineno = 0;
8163 }
8164 else {
8165 int res;
8166 res = obj2ast_int(tmp, &end_lineno, arena);
8167 if (res != 0) goto failed;
8168 Py_CLEAR(tmp);
8169 }
8170 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
8171 return 1;
8172 }
8173 if (tmp == NULL || tmp == Py_None) {
8174 Py_CLEAR(tmp);
8175 end_col_offset = 0;
8176 }
8177 else {
8178 int res;
8179 res = obj2ast_int(tmp, &end_col_offset, arena);
8180 if (res != 0) goto failed;
8181 Py_CLEAR(tmp);
8182 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008183 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
8184 if (isinstance == -1) {
8185 return 1;
8186 }
8187 if (isinstance) {
8188 expr_ty type;
8189 identifier name;
8190 asdl_seq* body;
8191
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008192 if (_PyObject_LookupAttrId(obj, &PyId_type, &tmp) < 0) {
8193 return 1;
8194 }
8195 if (tmp == NULL || tmp == Py_None) {
8196 Py_CLEAR(tmp);
8197 type = NULL;
8198 }
8199 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008200 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008201 res = obj2ast_expr(tmp, &type, arena);
8202 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008203 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008204 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008205 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
8206 return 1;
8207 }
8208 if (tmp == NULL || tmp == Py_None) {
8209 Py_CLEAR(tmp);
8210 name = NULL;
8211 }
8212 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008213 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008214 res = obj2ast_identifier(tmp, &name, arena);
8215 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008216 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008217 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008218 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
8219 return 1;
8220 }
8221 if (tmp == NULL) {
8222 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
8223 return 1;
8224 }
8225 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008226 int res;
8227 Py_ssize_t len;
8228 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008229 if (!PyList_Check(tmp)) {
8230 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8231 goto failed;
8232 }
8233 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008234 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008235 if (body == NULL) goto failed;
8236 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008237 stmt_ty val;
8238 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008239 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008240 if (len != PyList_GET_SIZE(tmp)) {
8241 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
8242 goto failed;
8243 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008244 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008245 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008246 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008247 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008248 *out = ExceptHandler(type, name, body, lineno, col_offset, end_lineno,
8249 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008250 if (*out == NULL) goto failed;
8251 return 0;
8252 }
8253
8254 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
8255 failed:
8256 Py_XDECREF(tmp);
8257 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008258}
8259
8260int
8261obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
8262{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008263 PyObject* tmp = NULL;
8264 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008265 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008266 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008267 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008268 arg_ty kwarg;
8269 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008270
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008271 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
8272 return 1;
8273 }
8274 if (tmp == NULL) {
8275 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
8276 return 1;
8277 }
8278 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008279 int res;
8280 Py_ssize_t len;
8281 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008282 if (!PyList_Check(tmp)) {
8283 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8284 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008285 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008286 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008287 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008288 if (args == NULL) goto failed;
8289 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008290 arg_ty val;
8291 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008292 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008293 if (len != PyList_GET_SIZE(tmp)) {
8294 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
8295 goto failed;
8296 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008297 asdl_seq_SET(args, i, val);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008298 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008299 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008300 }
8301 if (_PyObject_LookupAttrId(obj, &PyId_vararg, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008302 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008303 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008304 if (tmp == NULL || tmp == Py_None) {
8305 Py_CLEAR(tmp);
8306 vararg = NULL;
8307 }
8308 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008309 int res;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008310 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008311 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008312 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008313 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008314 if (_PyObject_LookupAttrId(obj, &PyId_kwonlyargs, &tmp) < 0) {
8315 return 1;
8316 }
8317 if (tmp == NULL) {
8318 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
8319 return 1;
8320 }
8321 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008322 int res;
8323 Py_ssize_t len;
8324 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008325 if (!PyList_Check(tmp)) {
8326 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8327 goto failed;
8328 }
8329 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008330 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008331 if (kwonlyargs == NULL) goto failed;
8332 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008333 arg_ty val;
8334 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008335 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008336 if (len != PyList_GET_SIZE(tmp)) {
8337 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
8338 goto failed;
8339 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008340 asdl_seq_SET(kwonlyargs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008341 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008342 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008343 }
8344 if (_PyObject_LookupAttrId(obj, &PyId_kw_defaults, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008345 return 1;
8346 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008347 if (tmp == NULL) {
8348 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
8349 return 1;
8350 }
8351 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008352 int res;
8353 Py_ssize_t len;
8354 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008355 if (!PyList_Check(tmp)) {
8356 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8357 goto failed;
8358 }
8359 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008360 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008361 if (kw_defaults == NULL) goto failed;
8362 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008363 expr_ty val;
8364 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008365 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008366 if (len != PyList_GET_SIZE(tmp)) {
8367 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
8368 goto failed;
8369 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008370 asdl_seq_SET(kw_defaults, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008371 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008372 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008373 }
8374 if (_PyObject_LookupAttrId(obj, &PyId_kwarg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008375 return 1;
8376 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008377 if (tmp == NULL || tmp == Py_None) {
8378 Py_CLEAR(tmp);
8379 kwarg = NULL;
8380 }
8381 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02008382 int res;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008383 res = obj2ast_arg(tmp, &kwarg, arena);
8384 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02008385 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008386 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008387 if (_PyObject_LookupAttrId(obj, &PyId_defaults, &tmp) < 0) {
8388 return 1;
8389 }
8390 if (tmp == NULL) {
8391 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
8392 return 1;
8393 }
8394 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02008395 int res;
8396 Py_ssize_t len;
8397 Py_ssize_t i;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008398 if (!PyList_Check(tmp)) {
8399 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8400 goto failed;
8401 }
8402 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008403 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008404 if (defaults == NULL) goto failed;
8405 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008406 expr_ty val;
8407 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &val, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008408 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008409 if (len != PyList_GET_SIZE(tmp)) {
8410 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
8411 goto failed;
8412 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008413 asdl_seq_SET(defaults, i, val);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008414 }
Victor Stinnerb3189902013-07-27 00:04:42 +02008415 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008416 }
8417 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
8418 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008419 return 0;
8420failed:
8421 Py_XDECREF(tmp);
8422 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008423}
8424
8425int
8426obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
8427{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008428 PyObject* tmp = NULL;
8429 identifier arg;
8430 expr_ty annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008431 string type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01008432 int lineno;
8433 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008434 int end_lineno;
8435 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008436
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008437 if (_PyObject_LookupAttrId(obj, &PyId_arg, &tmp) < 0) {
8438 return 1;
8439 }
8440 if (tmp == NULL) {
8441 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
8442 return 1;
8443 }
8444 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008445 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008446 res = obj2ast_identifier(tmp, &arg, arena);
8447 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008448 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008449 }
8450 if (_PyObject_LookupAttrId(obj, &PyId_annotation, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008451 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008452 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008453 if (tmp == NULL || tmp == Py_None) {
8454 Py_CLEAR(tmp);
8455 annotation = NULL;
8456 }
8457 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008458 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008459 res = obj2ast_expr(tmp, &annotation, arena);
8460 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008461 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008462 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008463 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
8464 return 1;
8465 }
8466 if (tmp == NULL || tmp == Py_None) {
8467 Py_CLEAR(tmp);
8468 type_comment = NULL;
8469 }
8470 else {
8471 int res;
8472 res = obj2ast_string(tmp, &type_comment, arena);
8473 if (res != 0) goto failed;
8474 Py_CLEAR(tmp);
8475 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008476 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8477 return 1;
8478 }
8479 if (tmp == NULL) {
8480 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
8481 return 1;
8482 }
8483 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01008484 int res;
Victor Stinnerc106c682015-11-06 17:01:48 +01008485 res = obj2ast_int(tmp, &lineno, arena);
8486 if (res != 0) goto failed;
8487 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008488 }
8489 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Victor Stinnerc106c682015-11-06 17:01:48 +01008490 return 1;
8491 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008492 if (tmp == NULL) {
8493 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
8494 return 1;
8495 }
8496 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01008497 int res;
Victor Stinnerc106c682015-11-06 17:01:48 +01008498 res = obj2ast_int(tmp, &col_offset, arena);
8499 if (res != 0) goto failed;
8500 Py_CLEAR(tmp);
Victor Stinnerc106c682015-11-06 17:01:48 +01008501 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008502 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
8503 return 1;
8504 }
8505 if (tmp == NULL || tmp == Py_None) {
8506 Py_CLEAR(tmp);
8507 end_lineno = 0;
8508 }
8509 else {
8510 int res;
8511 res = obj2ast_int(tmp, &end_lineno, arena);
8512 if (res != 0) goto failed;
8513 Py_CLEAR(tmp);
8514 }
8515 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
8516 return 1;
8517 }
8518 if (tmp == NULL || tmp == Py_None) {
8519 Py_CLEAR(tmp);
8520 end_col_offset = 0;
8521 }
8522 else {
8523 int res;
8524 res = obj2ast_int(tmp, &end_col_offset, arena);
8525 if (res != 0) goto failed;
8526 Py_CLEAR(tmp);
8527 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008528 *out = arg(arg, annotation, type_comment, lineno, col_offset, end_lineno,
8529 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008530 return 0;
8531failed:
8532 Py_XDECREF(tmp);
8533 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008534}
8535
8536int
8537obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
8538{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008539 PyObject* tmp = NULL;
8540 identifier arg;
8541 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008542
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008543 if (_PyObject_LookupAttrId(obj, &PyId_arg, &tmp) < 0) {
8544 return 1;
8545 }
8546 if (tmp == NULL || tmp == Py_None) {
8547 Py_CLEAR(tmp);
8548 arg = NULL;
8549 }
8550 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008551 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008552 res = obj2ast_identifier(tmp, &arg, arena);
8553 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008554 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008555 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008556 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
8557 return 1;
8558 }
8559 if (tmp == NULL) {
8560 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
8561 return 1;
8562 }
8563 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008564 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008565 res = obj2ast_expr(tmp, &value, arena);
8566 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008567 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008568 }
8569 *out = keyword(arg, value, arena);
8570 return 0;
8571failed:
8572 Py_XDECREF(tmp);
8573 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008574}
8575
8576int
8577obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
8578{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008579 PyObject* tmp = NULL;
8580 identifier name;
8581 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008582
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008583 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
8584 return 1;
8585 }
8586 if (tmp == NULL) {
8587 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
8588 return 1;
8589 }
8590 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008591 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008592 res = obj2ast_identifier(tmp, &name, arena);
8593 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008594 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008595 }
8596 if (_PyObject_LookupAttrId(obj, &PyId_asname, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008597 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008598 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008599 if (tmp == NULL || tmp == Py_None) {
8600 Py_CLEAR(tmp);
8601 asname = NULL;
8602 }
8603 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008604 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008605 res = obj2ast_identifier(tmp, &asname, arena);
8606 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008607 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008608 }
8609 *out = alias(name, asname, arena);
8610 return 0;
8611failed:
8612 Py_XDECREF(tmp);
8613 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008614}
8615
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008616int
8617obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
8618{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008619 PyObject* tmp = NULL;
8620 expr_ty context_expr;
8621 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008622
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008623 if (_PyObject_LookupAttrId(obj, &PyId_context_expr, &tmp) < 0) {
8624 return 1;
8625 }
8626 if (tmp == NULL) {
8627 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
8628 return 1;
8629 }
8630 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008631 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008632 res = obj2ast_expr(tmp, &context_expr, arena);
8633 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008634 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008635 }
8636 if (_PyObject_LookupAttrId(obj, &PyId_optional_vars, &tmp) < 0) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008637 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008638 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008639 if (tmp == NULL || tmp == Py_None) {
8640 Py_CLEAR(tmp);
8641 optional_vars = NULL;
8642 }
8643 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008644 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008645 res = obj2ast_expr(tmp, &optional_vars, arena);
8646 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008648 }
8649 *out = withitem(context_expr, optional_vars, arena);
8650 return 0;
8651failed:
8652 Py_XDECREF(tmp);
8653 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008654}
8655
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008656int
8657obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena* arena)
8658{
8659 int isinstance;
8660
8661 PyObject *tmp = NULL;
8662
8663 if (obj == Py_None) {
8664 *out = NULL;
8665 return 0;
8666 }
8667 isinstance = PyObject_IsInstance(obj, (PyObject*)TypeIgnore_type);
8668 if (isinstance == -1) {
8669 return 1;
8670 }
8671 if (isinstance) {
8672 int lineno;
8673
8674 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8675 return 1;
8676 }
8677 if (tmp == NULL) {
8678 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from TypeIgnore");
8679 return 1;
8680 }
8681 else {
8682 int res;
8683 res = obj2ast_int(tmp, &lineno, arena);
8684 if (res != 0) goto failed;
8685 Py_CLEAR(tmp);
8686 }
8687 *out = TypeIgnore(lineno, arena);
8688 if (*out == NULL) goto failed;
8689 return 0;
8690 }
8691
8692 PyErr_Format(PyExc_TypeError, "expected some sort of type_ignore, but got %R", obj);
8693 failed:
8694 Py_XDECREF(tmp);
8695 return 1;
8696}
8697
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008698
Martin v. Löwis1a214512008-06-11 05:26:20 +00008699static struct PyModuleDef _astmodule = {
8700 PyModuleDef_HEAD_INIT, "_ast"
8701};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008702PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00008703PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008704{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008705 PyObject *m, *d;
8706 if (!init_types()) return NULL;
8707 m = PyModule_Create(&_astmodule);
8708 if (!m) return NULL;
8709 d = PyModule_GetDict(m);
8710 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008711 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008712 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008713 if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0)
8714 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008715 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
8716 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
8717 NULL;
8718 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
8719 0) return NULL;
8720 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
8721 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008722 if (PyDict_SetItemString(d, "FunctionType", (PyObject*)FunctionType_type) <
8723 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008724 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
8725 NULL;
8726 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
8727 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
8728 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008729 if (PyDict_SetItemString(d, "AsyncFunctionDef",
8730 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008731 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
8732 return NULL;
8733 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
8734 NULL;
8735 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
8736 NULL;
8737 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
8738 NULL;
8739 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
8740 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07008741 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
8742 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008743 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008744 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
8745 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008746 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
8747 NULL;
8748 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
8749 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008750 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
8751 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008752 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
8753 NULL;
8754 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
8755 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
8756 NULL;
8757 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
8758 NULL;
8759 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
8760 return NULL;
8761 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
8762 NULL;
8763 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
8764 return NULL;
8765 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
8766 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
8767 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
8768 NULL;
8769 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
8770 return NULL;
8771 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
8772 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
8773 NULL;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07008774 if (PyDict_SetItemString(d, "NamedExpr", (PyObject*)NamedExpr_type) < 0)
8775 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008776 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
8777 NULL;
8778 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
8779 NULL;
8780 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
8781 NULL;
8782 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
8783 NULL;
8784 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
8785 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
8786 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
8787 return NULL;
8788 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
8789 NULL;
8790 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
8791 return NULL;
8792 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
8793 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008794 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
8795 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008796 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
8797 NULL;
8798 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
8799 return NULL;
8800 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
8801 NULL;
8802 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04008803 if (PyDict_SetItemString(d, "FormattedValue",
8804 (PyObject*)FormattedValue_type) < 0) return NULL;
8805 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
8806 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01008807 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
8808 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008809 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
8810 return NULL;
8811 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
8812 return NULL;
8813 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
8814 NULL;
8815 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
8816 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
8817 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
8818 NULL;
8819 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
8820 0) return NULL;
8821 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
8822 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
8823 NULL;
8824 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
8825 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
8826 NULL;
8827 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
8828 return NULL;
8829 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
8830 NULL;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07008831 if (PyDict_SetItemString(d, "NamedStore", (PyObject*)NamedStore_type) < 0)
8832 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008833 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
8834 NULL;
8835 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
8836 NULL;
8837 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
8838 return NULL;
8839 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
8840 NULL;
8841 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
8842 NULL;
8843 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
8844 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
8845 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
8846 return NULL;
8847 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
8848 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
8849 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04008850 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
8851 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008852 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
8853 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
8854 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
8855 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
8856 NULL;
8857 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
8858 NULL;
8859 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
8860 NULL;
8861 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
8862 NULL;
8863 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
8864 NULL;
8865 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
8866 return NULL;
8867 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
8868 NULL;
8869 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
8870 NULL;
8871 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
8872 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
8873 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
8874 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
8875 NULL;
8876 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
8877 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
8878 NULL;
8879 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
8880 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
8881 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
8882 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
8883 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
8884 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
8885 NULL;
8886 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
8887 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
8888 NULL;
8889 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
8890 < 0) return NULL;
8891 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
8892 < 0) return NULL;
8893 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
8894 < 0) return NULL;
8895 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
8896 return NULL;
8897 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
8898 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
8899 NULL;
8900 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
8901 NULL;
8902 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
8903 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008904 if (PyDict_SetItemString(d, "type_ignore", (PyObject*)type_ignore_type) <
8905 0) return NULL;
8906 if (PyDict_SetItemString(d, "TypeIgnore", (PyObject*)TypeIgnore_type) < 0)
8907 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008908 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008909}
8910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008911
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008912PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008913{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008914 if (!init_types())
8915 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00008916 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008917}
Martin v. Löwis5b222132007-06-10 09:51:05 +00008918
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008919/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
8920mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008921{
8922 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008923 PyObject *req_type[3];
Guido van Rossum3a32e3b2019-02-01 11:37:34 -08008924 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008925 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008926
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05008927 req_type[0] = (PyObject*)Module_type;
8928 req_type[1] = (PyObject*)Expression_type;
8929 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05008930
Guido van Rossum3a32e3b2019-02-01 11:37:34 -08008931 assert(0 <= mode && mode <= 2);
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008932
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008933 if (!init_types())
8934 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008935
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00008936 isinstance = PyObject_IsInstance(ast, req_type[mode]);
8937 if (isinstance == -1)
8938 return NULL;
8939 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00008940 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
8941 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008942 return NULL;
8943 }
8944 if (obj2ast_mod(ast, &res, arena) != 0)
8945 return NULL;
8946 else
8947 return res;
8948}
8949
8950int PyAST_Check(PyObject* obj)
8951{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02008952 if (!init_types())
8953 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00008954 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008955}
8956
Martin v. Löwis5b222132007-06-10 09:51:05 +00008957