blob: 44ad5327572855991b60119eb240e00aeb8a4286 [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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030014static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +030019static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +030023static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030029static const char * const FunctionType_fields[]={
Guido van Rossumdcfcd142019-01-31 03:40:27 -080030 "argtypes",
31 "returns",
32};
Neal Norwitz53d960c2006-02-28 22:47:29 +000033static PyTypeObject *Suite_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +030034static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030042static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030054static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +030063static const char * const AsyncFunctionDef_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -040064 "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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030074static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030083static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +030088static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +030092static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300100static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300108static const char * const AnnAssign_fields[]={
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700109 "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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300117static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300125static const char * const AsyncFor_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400126 "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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300134static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300140static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300147static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300153static const char * const AsyncWith_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400154 "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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300161static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300168static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300176static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300182static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300188static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300194static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300198static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300202static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300209static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300218static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300223static const char * const NamedExpr_fields[]={
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700224 "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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300230static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300237static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300242static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300247static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300254static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300260static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300266static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300271static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300277static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300283static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300288static const char * const Await_fields[]={
Yury Selivanov75445082015-05-11 22:57:16 -0400289 "value",
290};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000291static PyTypeObject *Yield_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300292static const char * const Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200293 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500294};
295static PyTypeObject *YieldFrom_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300296static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300302static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300309static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300317static const char * const FormattedValue_fields[]={
Eric V. Smith235a6f02015-09-19 14:51:32 -0400318 "value",
319 "conversion",
320 "format_spec",
321};
322static PyTypeObject *JoinedStr_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300323static const char * const JoinedStr_fields[]={
Eric V. Smith235a6f02015-09-19 14:51:32 -0400324 "values",
325};
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100326static PyTypeObject *Constant_type;
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700327_Py_IDENTIFIER(kind);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300328static const char * const Constant_fields[]={
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100329 "value",
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700330 "kind",
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100331};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000332static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200333_Py_IDENTIFIER(attr);
334_Py_IDENTIFIER(ctx);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300335static const char * const Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200336 "value",
337 "attr",
338 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000339};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000340static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200341_Py_IDENTIFIER(slice);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300342static const char * const Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200343 "value",
344 "slice",
345 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346};
Guido van Rossum0368b722007-05-11 16:50:42 +0000347static PyTypeObject *Starred_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300348static const char * const Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200349 "value",
350 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000351};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200353_Py_IDENTIFIER(id);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300354static const char * const Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200355 "id",
356 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *List_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300359static const char * const List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200360 "elts",
361 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000363static PyTypeObject *Tuple_type;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300364static const char * const Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200365 "elts",
366 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000368static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
Serhiy Storchakad8b3a982019-03-05 20:42:06 +0200370*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000371static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000372static PyTypeObject *Load_type;
373static PyTypeObject *Store_type;
374static PyTypeObject *Del_type;
375static PyTypeObject *AugLoad_type;
376static PyTypeObject *AugStore_type;
377static PyTypeObject *Param_type;
378static 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300384static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300391static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300395static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300449static const char * const 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;
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300456static const char * const 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);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300465static const char * const 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*);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100472_Py_IDENTIFIER(posonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200473_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200474_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200475_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700476_Py_IDENTIFIER(kwarg);
477_Py_IDENTIFIER(defaults);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300478static const char * const arguments_fields[]={
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100479 "posonlyargs",
Pablo Galindocd6e83b2019-07-15 01:32:18 +0200480 "args",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200481 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200482 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200483 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200484 "kwarg",
485 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000486};
Neal Norwitzc1505362006-12-28 06:47:50 +0000487static PyTypeObject *arg_type;
488static PyObject* ast2obj_arg(void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300489static const char * const arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200490 "lineno",
491 "col_offset",
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000492 "end_lineno",
493 "end_col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700494};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200495_Py_IDENTIFIER(arg);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300496static const char * const arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200497 "arg",
498 "annotation",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800499 "type_comment",
Neal Norwitzc1505362006-12-28 06:47:50 +0000500};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000501static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000502static PyObject* ast2obj_keyword(void*);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300503static const char * const keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200504 "arg",
505 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000506};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000507static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000508static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200509_Py_IDENTIFIER(asname);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300510static const char * const alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200511 "name",
512 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000513};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500514static PyTypeObject *withitem_type;
515static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200516_Py_IDENTIFIER(context_expr);
517_Py_IDENTIFIER(optional_vars);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300518static const char * const withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200519 "context_expr",
520 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500521};
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800522static PyTypeObject *type_ignore_type;
523static PyObject* ast2obj_type_ignore(void*);
524static PyTypeObject *TypeIgnore_type;
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700525_Py_IDENTIFIER(tag);
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300526static const char * const TypeIgnore_fields[]={
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800527 "lineno",
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700528 "tag",
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800529};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000530
531
INADA Naokifc489082017-01-25 22:33:43 +0900532_Py_IDENTIFIER(_fields);
533_Py_IDENTIFIER(_attributes);
534
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700535typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100536 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700537 PyObject *dict;
538} AST_object;
539
Benjamin Peterson1767e022012-03-14 21:50:29 -0500540static void
541ast_dealloc(AST_object *self)
542{
INADA Naokia6296d32017-08-24 14:55:17 +0900543 /* bpo-31095: UnTrack is needed before calling any callbacks */
544 PyObject_GC_UnTrack(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500545 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200546 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500547}
548
Neal Norwitz207c9f32008-03-31 04:42:11 +0000549static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700550ast_traverse(AST_object *self, visitproc visit, void *arg)
551{
552 Py_VISIT(self->dict);
553 return 0;
554}
555
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300556static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700557ast_clear(AST_object *self)
558{
559 Py_CLEAR(self->dict);
Serhiy Storchakaa5c42282018-05-31 07:34:34 +0300560 return 0;
Benjamin Peterson81071762012-07-08 11:03:46 -0700561}
562
563static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000564ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
565{
566 Py_ssize_t i, numfields = 0;
567 int res = -1;
568 PyObject *key, *value, *fields;
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200569 if (_PyObject_LookupAttrId((PyObject*)Py_TYPE(self), &PyId__fields, &fields) < 0) {
570 goto cleanup;
571 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000572 if (fields) {
573 numfields = PySequence_Size(fields);
574 if (numfields == -1)
575 goto cleanup;
576 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900577
Neal Norwitz207c9f32008-03-31 04:42:11 +0000578 res = 0; /* if no error occurs, this stays 0 to the end */
INADA Naoki4c78c522017-02-24 02:48:17 +0900579 if (numfields < PyTuple_GET_SIZE(args)) {
580 PyErr_Format(PyExc_TypeError, "%.400s constructor takes at most "
581 "%zd positional argument%s",
582 Py_TYPE(self)->tp_name,
583 numfields, numfields == 1 ? "" : "s");
584 res = -1;
585 goto cleanup;
586 }
587 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
588 /* cannot be reached when fields is NULL */
589 PyObject *name = PySequence_GetItem(fields, i);
590 if (!name) {
Neal Norwitz207c9f32008-03-31 04:42:11 +0000591 res = -1;
592 goto cleanup;
593 }
INADA Naoki4c78c522017-02-24 02:48:17 +0900594 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
595 Py_DECREF(name);
596 if (res < 0)
597 goto cleanup;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000598 }
599 if (kw) {
600 i = 0; /* needed by PyDict_Next */
601 while (PyDict_Next(kw, &i, &key, &value)) {
602 res = PyObject_SetAttr(self, key, value);
603 if (res < 0)
604 goto cleanup;
605 }
606 }
607 cleanup:
608 Py_XDECREF(fields);
609 return res;
610}
611
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000612/* Pickling support */
613static PyObject *
614ast_type_reduce(PyObject *self, PyObject *unused)
615{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200616 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200617 PyObject *dict;
618 if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) {
619 return NULL;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000620 }
621 if (dict) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200622 return Py_BuildValue("O()N", Py_TYPE(self), dict);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000623 }
624 return Py_BuildValue("O()", Py_TYPE(self));
625}
626
627static PyMethodDef ast_type_methods[] = {
628 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
629 {NULL}
630};
631
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700632static PyGetSetDef ast_type_getsets[] = {
633 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
634 {NULL}
635};
636
Neal Norwitz207c9f32008-03-31 04:42:11 +0000637static PyTypeObject AST_type = {
638 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000639 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700640 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500642 (destructor)ast_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200643 0, /* tp_vectorcall_offset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000644 0, /* tp_getattr */
645 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200646 0, /* tp_as_async */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000647 0, /* tp_repr */
648 0, /* tp_as_number */
649 0, /* tp_as_sequence */
650 0, /* tp_as_mapping */
651 0, /* tp_hash */
652 0, /* tp_call */
653 0, /* tp_str */
654 PyObject_GenericGetAttr, /* tp_getattro */
655 PyObject_GenericSetAttr, /* tp_setattro */
656 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700657 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000658 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700659 (traverseproc)ast_traverse, /* tp_traverse */
660 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000661 0, /* tp_richcompare */
662 0, /* tp_weaklistoffset */
663 0, /* tp_iter */
664 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000665 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000666 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700667 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000668 0, /* tp_base */
669 0, /* tp_dict */
670 0, /* tp_descr_get */
671 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700672 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000673 (initproc)ast_type_init, /* tp_init */
674 PyType_GenericAlloc, /* tp_alloc */
675 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700676 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000677};
678
679
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300680static PyTypeObject *
681make_type(const char *type, PyTypeObject *base, const char * const *fields, int num_fields)
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682{
INADA Naokifc489082017-01-25 22:33:43 +0900683 _Py_IDENTIFIER(__module__);
684 _Py_IDENTIFIER(_ast);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 PyObject *fnames, *result;
686 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000687 fnames = PyTuple_New(num_fields);
688 if (!fnames) return NULL;
689 for (i = 0; i < num_fields; i++) {
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300690 PyObject *field = PyUnicode_InternFromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 if (!field) {
692 Py_DECREF(fnames);
693 return NULL;
694 }
695 PyTuple_SET_ITEM(fnames, i, field);
696 }
INADA Naokifc489082017-01-25 22:33:43 +0900697 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO}",
698 type, base,
699 _PyUnicode_FromId(&PyId__fields), fnames,
700 _PyUnicode_FromId(&PyId___module__),
701 _PyUnicode_FromId(&PyId__ast));
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 Py_DECREF(fnames);
703 return (PyTypeObject*)result;
704}
705
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300706static int
707add_attributes(PyTypeObject *type, const char * const *attrs, int num_fields)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000710 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000711 if (!l)
712 return 0;
713 for (i = 0; i < num_fields; i++) {
Serhiy Storchaka43c97312019-09-10 13:02:30 +0300714 s = PyUnicode_InternFromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!s) {
716 Py_DECREF(l);
717 return 0;
718 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000719 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200721 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000722 Py_DECREF(l);
723 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724}
725
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000726/* Conversion AST -> Python */
727
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
729{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700730 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 PyObject *result = PyList_New(n);
732 PyObject *value;
733 if (!result)
734 return NULL;
735 for (i = 0; i < n; i++) {
736 value = func(asdl_seq_GET(seq, i));
737 if (!value) {
738 Py_DECREF(result);
739 return NULL;
740 }
741 PyList_SET_ITEM(result, i, value);
742 }
743 return result;
744}
745
746static PyObject* ast2obj_object(void *o)
747{
748 if (!o)
749 o = Py_None;
750 Py_INCREF((PyObject*)o);
751 return (PyObject*)o;
752}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500753#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100754#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755#define ast2obj_identifier ast2obj_object
756#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500757#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000759static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760{
Christian Heimes217cfd12007-12-02 14:31:20 +0000761 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762}
763
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000764/* Conversion Python -> AST */
765
766static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
767{
768 if (obj == Py_None)
769 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200770 if (obj) {
771 if (PyArena_AddPyObject(arena, obj) < 0) {
772 *out = NULL;
773 return -1;
774 }
775 Py_INCREF(obj);
776 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000777 *out = obj;
778 return 0;
779}
780
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100781static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
782{
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300783 if (PyArena_AddPyObject(arena, obj) < 0) {
784 *out = NULL;
785 return -1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100786 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300787 Py_INCREF(obj);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100788 *out = obj;
789 return 0;
790}
791
Benjamin Peterson180e6352011-07-22 11:09:07 -0500792static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500793{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500794 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
795 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500796 return 1;
797 }
798 return obj2ast_object(obj, out, arena);
799}
800
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800801static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
802{
803 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
804 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
805 return 1;
806 }
807 return obj2ast_object(obj, out, arena);
808}
809
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000810static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
811{
812 int i;
813 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100814 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000815 return 1;
816 }
817
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300818 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000819 if (i == -1 && PyErr_Occurred())
820 return 1;
821 *out = i;
822 return 0;
823}
824
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000825static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000826{
827 PyObject *empty_tuple, *d;
828 if (PyType_Ready(&AST_type) < 0)
829 return -1;
830 d = AST_type.tp_dict;
831 empty_tuple = PyTuple_New(0);
832 if (!empty_tuple ||
INADA Naokifc489082017-01-25 22:33:43 +0900833 _PyDict_SetItemId(d, &PyId__fields, empty_tuple) < 0 ||
834 _PyDict_SetItemId(d, &PyId__attributes, empty_tuple) < 0) {
Benjamin Petersonce825f12008-10-24 23:11:02 +0000835 Py_XDECREF(empty_tuple);
836 return -1;
837 }
838 Py_DECREF(empty_tuple);
839 return 0;
840}
841
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000842
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843static int init_types(void)
844{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200845 static int initialized;
846 if (initialized) return 1;
847 if (add_ast_fields() < 0) return 0;
848 mod_type = make_type("mod", &AST_type, NULL, 0);
849 if (!mod_type) return 0;
850 if (!add_attributes(mod_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800851 Module_type = make_type("Module", mod_type, Module_fields, 2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200852 if (!Module_type) return 0;
853 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
854 1);
855 if (!Interactive_type) return 0;
856 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
857 if (!Expression_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800858 FunctionType_type = make_type("FunctionType", mod_type,
859 FunctionType_fields, 2);
860 if (!FunctionType_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200861 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
862 if (!Suite_type) return 0;
863 stmt_type = make_type("stmt", &AST_type, NULL, 0);
864 if (!stmt_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000865 if (!add_attributes(stmt_type, stmt_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200866 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800867 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200868 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400869 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800870 AsyncFunctionDef_fields, 6);
Yury Selivanov75445082015-05-11 22:57:16 -0400871 if (!AsyncFunctionDef_type) return 0;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300872 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200873 if (!ClassDef_type) return 0;
874 Return_type = make_type("Return", stmt_type, Return_fields, 1);
875 if (!Return_type) return 0;
876 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
877 if (!Delete_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800878 Assign_type = make_type("Assign", stmt_type, Assign_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200879 if (!Assign_type) return 0;
880 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
881 if (!AugAssign_type) return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700882 AnnAssign_type = make_type("AnnAssign", stmt_type, AnnAssign_fields, 4);
883 if (!AnnAssign_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800884 For_type = make_type("For", stmt_type, For_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200885 if (!For_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800886 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 5);
Yury Selivanov75445082015-05-11 22:57:16 -0400887 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200888 While_type = make_type("While", stmt_type, While_fields, 3);
889 if (!While_type) return 0;
890 If_type = make_type("If", stmt_type, If_fields, 3);
891 if (!If_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800892 With_type = make_type("With", stmt_type, With_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200893 if (!With_type) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800894 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 3);
Yury Selivanov75445082015-05-11 22:57:16 -0400895 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200896 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
897 if (!Raise_type) return 0;
898 Try_type = make_type("Try", stmt_type, Try_fields, 4);
899 if (!Try_type) return 0;
900 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
901 if (!Assert_type) return 0;
902 Import_type = make_type("Import", stmt_type, Import_fields, 1);
903 if (!Import_type) return 0;
904 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
905 if (!ImportFrom_type) return 0;
906 Global_type = make_type("Global", stmt_type, Global_fields, 1);
907 if (!Global_type) return 0;
908 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
909 if (!Nonlocal_type) return 0;
910 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
911 if (!Expr_type) return 0;
912 Pass_type = make_type("Pass", stmt_type, NULL, 0);
913 if (!Pass_type) return 0;
914 Break_type = make_type("Break", stmt_type, NULL, 0);
915 if (!Break_type) return 0;
916 Continue_type = make_type("Continue", stmt_type, NULL, 0);
917 if (!Continue_type) return 0;
918 expr_type = make_type("expr", &AST_type, NULL, 0);
919 if (!expr_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000920 if (!add_attributes(expr_type, expr_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200921 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
922 if (!BoolOp_type) return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700923 NamedExpr_type = make_type("NamedExpr", expr_type, NamedExpr_fields, 2);
924 if (!NamedExpr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200925 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
926 if (!BinOp_type) return 0;
927 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
928 if (!UnaryOp_type) return 0;
929 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
930 if (!Lambda_type) return 0;
931 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
932 if (!IfExp_type) return 0;
933 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
934 if (!Dict_type) return 0;
935 Set_type = make_type("Set", expr_type, Set_fields, 1);
936 if (!Set_type) return 0;
937 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
938 if (!ListComp_type) return 0;
939 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
940 if (!SetComp_type) return 0;
941 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
942 if (!DictComp_type) return 0;
943 GeneratorExp_type = make_type("GeneratorExp", expr_type,
944 GeneratorExp_fields, 2);
945 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400946 Await_type = make_type("Await", expr_type, Await_fields, 1);
947 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200948 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
949 if (!Yield_type) return 0;
950 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
951 if (!YieldFrom_type) return 0;
952 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
953 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400954 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200955 if (!Call_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400956 FormattedValue_type = make_type("FormattedValue", expr_type,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -0400957 FormattedValue_fields, 3);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400958 if (!FormattedValue_type) return 0;
959 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
960 if (!JoinedStr_type) return 0;
Guido van Rossum10f8ce62019-03-13 13:00:46 -0700961 Constant_type = make_type("Constant", expr_type, Constant_fields, 2);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100962 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200963 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
964 if (!Attribute_type) return 0;
965 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
966 if (!Subscript_type) return 0;
967 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
968 if (!Starred_type) return 0;
969 Name_type = make_type("Name", expr_type, Name_fields, 2);
970 if (!Name_type) return 0;
971 List_type = make_type("List", expr_type, List_fields, 2);
972 if (!List_type) return 0;
973 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
974 if (!Tuple_type) return 0;
975 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
976 if (!expr_context_type) return 0;
977 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
978 Load_type = make_type("Load", expr_context_type, NULL, 0);
979 if (!Load_type) return 0;
980 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
981 if (!Load_singleton) return 0;
982 Store_type = make_type("Store", expr_context_type, NULL, 0);
983 if (!Store_type) return 0;
984 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
985 if (!Store_singleton) return 0;
986 Del_type = make_type("Del", expr_context_type, NULL, 0);
987 if (!Del_type) return 0;
988 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
989 if (!Del_singleton) return 0;
990 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
991 if (!AugLoad_type) return 0;
992 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
993 if (!AugLoad_singleton) return 0;
994 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
995 if (!AugStore_type) return 0;
996 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
997 if (!AugStore_singleton) return 0;
998 Param_type = make_type("Param", expr_context_type, NULL, 0);
999 if (!Param_type) return 0;
1000 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1001 if (!Param_singleton) return 0;
1002 slice_type = make_type("slice", &AST_type, NULL, 0);
1003 if (!slice_type) return 0;
1004 if (!add_attributes(slice_type, NULL, 0)) return 0;
1005 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1006 if (!Slice_type) return 0;
1007 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1008 if (!ExtSlice_type) return 0;
1009 Index_type = make_type("Index", slice_type, Index_fields, 1);
1010 if (!Index_type) return 0;
1011 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1012 if (!boolop_type) return 0;
1013 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1014 And_type = make_type("And", boolop_type, NULL, 0);
1015 if (!And_type) return 0;
1016 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1017 if (!And_singleton) return 0;
1018 Or_type = make_type("Or", boolop_type, NULL, 0);
1019 if (!Or_type) return 0;
1020 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1021 if (!Or_singleton) return 0;
1022 operator_type = make_type("operator", &AST_type, NULL, 0);
1023 if (!operator_type) return 0;
1024 if (!add_attributes(operator_type, NULL, 0)) return 0;
1025 Add_type = make_type("Add", operator_type, NULL, 0);
1026 if (!Add_type) return 0;
1027 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1028 if (!Add_singleton) return 0;
1029 Sub_type = make_type("Sub", operator_type, NULL, 0);
1030 if (!Sub_type) return 0;
1031 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1032 if (!Sub_singleton) return 0;
1033 Mult_type = make_type("Mult", operator_type, NULL, 0);
1034 if (!Mult_type) return 0;
1035 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1036 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001037 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1038 if (!MatMult_type) return 0;
1039 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1040 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001041 Div_type = make_type("Div", operator_type, NULL, 0);
1042 if (!Div_type) return 0;
1043 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1044 if (!Div_singleton) return 0;
1045 Mod_type = make_type("Mod", operator_type, NULL, 0);
1046 if (!Mod_type) return 0;
1047 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1048 if (!Mod_singleton) return 0;
1049 Pow_type = make_type("Pow", operator_type, NULL, 0);
1050 if (!Pow_type) return 0;
1051 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1052 if (!Pow_singleton) return 0;
1053 LShift_type = make_type("LShift", operator_type, NULL, 0);
1054 if (!LShift_type) return 0;
1055 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1056 if (!LShift_singleton) return 0;
1057 RShift_type = make_type("RShift", operator_type, NULL, 0);
1058 if (!RShift_type) return 0;
1059 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1060 if (!RShift_singleton) return 0;
1061 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1062 if (!BitOr_type) return 0;
1063 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1064 if (!BitOr_singleton) return 0;
1065 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1066 if (!BitXor_type) return 0;
1067 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1068 if (!BitXor_singleton) return 0;
1069 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1070 if (!BitAnd_type) return 0;
1071 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1072 if (!BitAnd_singleton) return 0;
1073 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1074 if (!FloorDiv_type) return 0;
1075 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1076 if (!FloorDiv_singleton) return 0;
1077 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1078 if (!unaryop_type) return 0;
1079 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1080 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1081 if (!Invert_type) return 0;
1082 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1083 if (!Invert_singleton) return 0;
1084 Not_type = make_type("Not", unaryop_type, NULL, 0);
1085 if (!Not_type) return 0;
1086 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1087 if (!Not_singleton) return 0;
1088 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1089 if (!UAdd_type) return 0;
1090 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1091 if (!UAdd_singleton) return 0;
1092 USub_type = make_type("USub", unaryop_type, NULL, 0);
1093 if (!USub_type) return 0;
1094 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1095 if (!USub_singleton) return 0;
1096 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1097 if (!cmpop_type) return 0;
1098 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1099 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1100 if (!Eq_type) return 0;
1101 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1102 if (!Eq_singleton) return 0;
1103 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1104 if (!NotEq_type) return 0;
1105 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1106 if (!NotEq_singleton) return 0;
1107 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1108 if (!Lt_type) return 0;
1109 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1110 if (!Lt_singleton) return 0;
1111 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1112 if (!LtE_type) return 0;
1113 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1114 if (!LtE_singleton) return 0;
1115 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1116 if (!Gt_type) return 0;
1117 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1118 if (!Gt_singleton) return 0;
1119 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1120 if (!GtE_type) return 0;
1121 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1122 if (!GtE_singleton) return 0;
1123 Is_type = make_type("Is", cmpop_type, NULL, 0);
1124 if (!Is_type) return 0;
1125 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1126 if (!Is_singleton) return 0;
1127 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1128 if (!IsNot_type) return 0;
1129 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1130 if (!IsNot_singleton) return 0;
1131 In_type = make_type("In", cmpop_type, NULL, 0);
1132 if (!In_type) return 0;
1133 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1134 if (!In_singleton) return 0;
1135 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1136 if (!NotIn_type) return 0;
1137 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1138 if (!NotIn_singleton) return 0;
1139 comprehension_type = make_type("comprehension", &AST_type,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001140 comprehension_fields, 4);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001141 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001142 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001143 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1144 if (!excepthandler_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001145 if (!add_attributes(excepthandler_type, excepthandler_attributes, 4))
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001146 return 0;
1147 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1148 ExceptHandler_fields, 3);
1149 if (!ExceptHandler_type) return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001150 arguments_type = make_type("arguments", &AST_type, arguments_fields, 7);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001151 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001152 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001153 arg_type = make_type("arg", &AST_type, arg_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 if (!arg_type) return 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001155 if (!add_attributes(arg_type, arg_attributes, 4)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001156 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1157 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001158 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001159 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1160 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001161 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001162 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1163 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001164 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001165 type_ignore_type = make_type("type_ignore", &AST_type, NULL, 0);
1166 if (!type_ignore_type) return 0;
1167 if (!add_attributes(type_ignore_type, NULL, 0)) return 0;
1168 TypeIgnore_type = make_type("TypeIgnore", type_ignore_type,
Michael J. Sullivan933e1502019-05-22 07:54:20 -07001169 TypeIgnore_fields, 2);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001170 if (!TypeIgnore_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001171 initialized = 1;
1172 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001173}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001174
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001175static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1176static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1177static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1178static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1179 arena);
1180static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1181static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1182static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1183static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1184static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1185static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1186 arena);
1187static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1188 arena);
1189static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1190static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1191static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1192static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001193static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001194static int obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena*
1195 arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197mod_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001198Module(asdl_seq * body, asdl_seq * type_ignores, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001200 mod_ty p;
1201 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1202 if (!p)
1203 return NULL;
1204 p->kind = Module_kind;
1205 p->v.Module.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001206 p->v.Module.type_ignores = type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001207 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208}
1209
1210mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001211Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001213 mod_ty p;
1214 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1215 if (!p)
1216 return NULL;
1217 p->kind = Interactive_kind;
1218 p->v.Interactive.body = body;
1219 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220}
1221
1222mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001225 mod_ty p;
1226 if (!body) {
1227 PyErr_SetString(PyExc_ValueError,
1228 "field body is required for Expression");
1229 return NULL;
1230 }
1231 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1232 if (!p)
1233 return NULL;
1234 p->kind = Expression_kind;
1235 p->v.Expression.body = body;
1236 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237}
1238
1239mod_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001240FunctionType(asdl_seq * argtypes, expr_ty returns, PyArena *arena)
1241{
1242 mod_ty p;
1243 if (!returns) {
1244 PyErr_SetString(PyExc_ValueError,
1245 "field returns is required for FunctionType");
1246 return NULL;
1247 }
1248 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1249 if (!p)
1250 return NULL;
1251 p->kind = FunctionType_kind;
1252 p->v.FunctionType.argtypes = argtypes;
1253 p->v.FunctionType.returns = returns;
1254 return p;
1255}
1256
1257mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001258Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001260 mod_ty p;
1261 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1262 if (!p)
1263 return NULL;
1264 p->kind = Suite_kind;
1265 p->v.Suite.body = body;
1266 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
1269stmt_ty
1270FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001271 decorator_list, expr_ty returns, string type_comment, int lineno,
1272 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001274 stmt_ty p;
1275 if (!name) {
1276 PyErr_SetString(PyExc_ValueError,
1277 "field name is required for FunctionDef");
1278 return NULL;
1279 }
1280 if (!args) {
1281 PyErr_SetString(PyExc_ValueError,
1282 "field args is required for FunctionDef");
1283 return NULL;
1284 }
1285 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1286 if (!p)
1287 return NULL;
1288 p->kind = FunctionDef_kind;
1289 p->v.FunctionDef.name = name;
1290 p->v.FunctionDef.args = args;
1291 p->v.FunctionDef.body = body;
1292 p->v.FunctionDef.decorator_list = decorator_list;
1293 p->v.FunctionDef.returns = returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001294 p->v.FunctionDef.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001295 p->lineno = lineno;
1296 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001297 p->end_lineno = end_lineno;
1298 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001299 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300}
1301
1302stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001303AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001304 * decorator_list, expr_ty returns, string type_comment, int
1305 lineno, int col_offset, int end_lineno, int end_col_offset,
1306 PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001307{
1308 stmt_ty p;
1309 if (!name) {
1310 PyErr_SetString(PyExc_ValueError,
1311 "field name is required for AsyncFunctionDef");
1312 return NULL;
1313 }
1314 if (!args) {
1315 PyErr_SetString(PyExc_ValueError,
1316 "field args is required for AsyncFunctionDef");
1317 return NULL;
1318 }
1319 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1320 if (!p)
1321 return NULL;
1322 p->kind = AsyncFunctionDef_kind;
1323 p->v.AsyncFunctionDef.name = name;
1324 p->v.AsyncFunctionDef.args = args;
1325 p->v.AsyncFunctionDef.body = body;
1326 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1327 p->v.AsyncFunctionDef.returns = returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001328 p->v.AsyncFunctionDef.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001329 p->lineno = lineno;
1330 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001331 p->end_lineno = end_lineno;
1332 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001333 return p;
1334}
1335
1336stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001337ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001338 body, asdl_seq * decorator_list, int lineno, int col_offset, int
1339 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001341 stmt_ty p;
1342 if (!name) {
1343 PyErr_SetString(PyExc_ValueError,
1344 "field name is required for ClassDef");
1345 return NULL;
1346 }
1347 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1348 if (!p)
1349 return NULL;
1350 p->kind = ClassDef_kind;
1351 p->v.ClassDef.name = name;
1352 p->v.ClassDef.bases = bases;
1353 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001354 p->v.ClassDef.body = body;
1355 p->v.ClassDef.decorator_list = decorator_list;
1356 p->lineno = lineno;
1357 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001358 p->end_lineno = end_lineno;
1359 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001360 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361}
1362
1363stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001364Return(expr_ty value, int lineno, int col_offset, int end_lineno, int
1365 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001367 stmt_ty p;
1368 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1369 if (!p)
1370 return NULL;
1371 p->kind = Return_kind;
1372 p->v.Return.value = value;
1373 p->lineno = lineno;
1374 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001375 p->end_lineno = end_lineno;
1376 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001377 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378}
1379
1380stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001381Delete(asdl_seq * targets, int lineno, int col_offset, int end_lineno, int
1382 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001384 stmt_ty p;
1385 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1386 if (!p)
1387 return NULL;
1388 p->kind = Delete_kind;
1389 p->v.Delete.targets = targets;
1390 p->lineno = lineno;
1391 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001392 p->end_lineno = end_lineno;
1393 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001394 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395}
1396
1397stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001398Assign(asdl_seq * targets, expr_ty value, string type_comment, int lineno, int
1399 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001401 stmt_ty p;
1402 if (!value) {
1403 PyErr_SetString(PyExc_ValueError,
1404 "field value is required for Assign");
1405 return NULL;
1406 }
1407 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1408 if (!p)
1409 return NULL;
1410 p->kind = Assign_kind;
1411 p->v.Assign.targets = targets;
1412 p->v.Assign.value = value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001413 p->v.Assign.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001414 p->lineno = lineno;
1415 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001416 p->end_lineno = end_lineno;
1417 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001418 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419}
1420
1421stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001422AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001423 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001425 stmt_ty p;
1426 if (!target) {
1427 PyErr_SetString(PyExc_ValueError,
1428 "field target is required for AugAssign");
1429 return NULL;
1430 }
1431 if (!op) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "field op is required for AugAssign");
1434 return NULL;
1435 }
1436 if (!value) {
1437 PyErr_SetString(PyExc_ValueError,
1438 "field value is required for AugAssign");
1439 return NULL;
1440 }
1441 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1442 if (!p)
1443 return NULL;
1444 p->kind = AugAssign_kind;
1445 p->v.AugAssign.target = target;
1446 p->v.AugAssign.op = op;
1447 p->v.AugAssign.value = value;
1448 p->lineno = lineno;
1449 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001450 p->end_lineno = end_lineno;
1451 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001452 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453}
1454
1455stmt_ty
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001456AnnAssign(expr_ty target, expr_ty annotation, expr_ty value, int simple, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001457 lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1458 *arena)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001459{
1460 stmt_ty p;
1461 if (!target) {
1462 PyErr_SetString(PyExc_ValueError,
1463 "field target is required for AnnAssign");
1464 return NULL;
1465 }
1466 if (!annotation) {
1467 PyErr_SetString(PyExc_ValueError,
1468 "field annotation is required for AnnAssign");
1469 return NULL;
1470 }
1471 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1472 if (!p)
1473 return NULL;
1474 p->kind = AnnAssign_kind;
1475 p->v.AnnAssign.target = target;
1476 p->v.AnnAssign.annotation = annotation;
1477 p->v.AnnAssign.value = value;
1478 p->v.AnnAssign.simple = simple;
1479 p->lineno = lineno;
1480 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001481 p->end_lineno = end_lineno;
1482 p->end_col_offset = end_col_offset;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001483 return p;
1484}
1485
1486stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001487For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, string
1488 type_comment, int lineno, int col_offset, int end_lineno, int
1489 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001491 stmt_ty p;
1492 if (!target) {
1493 PyErr_SetString(PyExc_ValueError,
1494 "field target is required for For");
1495 return NULL;
1496 }
1497 if (!iter) {
1498 PyErr_SetString(PyExc_ValueError,
1499 "field iter is required for For");
1500 return NULL;
1501 }
1502 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1503 if (!p)
1504 return NULL;
1505 p->kind = For_kind;
1506 p->v.For.target = target;
1507 p->v.For.iter = iter;
1508 p->v.For.body = body;
1509 p->v.For.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001510 p->v.For.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001511 p->lineno = lineno;
1512 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001513 p->end_lineno = end_lineno;
1514 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001515 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516}
1517
1518stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001519AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse,
1520 string type_comment, int lineno, int col_offset, int end_lineno, int
1521 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001522{
1523 stmt_ty p;
1524 if (!target) {
1525 PyErr_SetString(PyExc_ValueError,
1526 "field target is required for AsyncFor");
1527 return NULL;
1528 }
1529 if (!iter) {
1530 PyErr_SetString(PyExc_ValueError,
1531 "field iter is required for AsyncFor");
1532 return NULL;
1533 }
1534 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1535 if (!p)
1536 return NULL;
1537 p->kind = AsyncFor_kind;
1538 p->v.AsyncFor.target = target;
1539 p->v.AsyncFor.iter = iter;
1540 p->v.AsyncFor.body = body;
1541 p->v.AsyncFor.orelse = orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001542 p->v.AsyncFor.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001543 p->lineno = lineno;
1544 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001545 p->end_lineno = end_lineno;
1546 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001547 return p;
1548}
1549
1550stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001551While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001552 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001554 stmt_ty p;
1555 if (!test) {
1556 PyErr_SetString(PyExc_ValueError,
1557 "field test is required for While");
1558 return NULL;
1559 }
1560 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1561 if (!p)
1562 return NULL;
1563 p->kind = While_kind;
1564 p->v.While.test = test;
1565 p->v.While.body = body;
1566 p->v.While.orelse = orelse;
1567 p->lineno = lineno;
1568 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001569 p->end_lineno = end_lineno;
1570 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001571 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572}
1573
1574stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001575If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001576 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001578 stmt_ty p;
1579 if (!test) {
1580 PyErr_SetString(PyExc_ValueError,
1581 "field test is required for If");
1582 return NULL;
1583 }
1584 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1585 if (!p)
1586 return NULL;
1587 p->kind = If_kind;
1588 p->v.If.test = test;
1589 p->v.If.body = body;
1590 p->v.If.orelse = orelse;
1591 p->lineno = lineno;
1592 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001593 p->end_lineno = end_lineno;
1594 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001595 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001599With(asdl_seq * items, asdl_seq * body, string type_comment, int lineno, int
1600 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001601{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001602 stmt_ty p;
1603 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1604 if (!p)
1605 return NULL;
1606 p->kind = With_kind;
1607 p->v.With.items = items;
1608 p->v.With.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001609 p->v.With.type_comment = type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001610 p->lineno = lineno;
1611 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001612 p->end_lineno = end_lineno;
1613 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001614 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001615}
1616
1617stmt_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001618AsyncWith(asdl_seq * items, asdl_seq * body, string type_comment, int lineno,
1619 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04001620{
1621 stmt_ty p;
1622 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1623 if (!p)
1624 return NULL;
1625 p->kind = AsyncWith_kind;
1626 p->v.AsyncWith.items = items;
1627 p->v.AsyncWith.body = body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001628 p->v.AsyncWith.type_comment = type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04001629 p->lineno = lineno;
1630 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001631 p->end_lineno = end_lineno;
1632 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04001633 return p;
1634}
1635
1636stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001637Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, int end_lineno,
1638 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001640 stmt_ty p;
1641 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1642 if (!p)
1643 return NULL;
1644 p->kind = Raise_kind;
1645 p->v.Raise.exc = exc;
1646 p->v.Raise.cause = cause;
1647 p->lineno = lineno;
1648 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001649 p->end_lineno = end_lineno;
1650 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001651 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652}
1653
1654stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001655Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001656 finalbody, int lineno, int col_offset, int end_lineno, int end_col_offset,
1657 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001659 stmt_ty p;
1660 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1661 if (!p)
1662 return NULL;
1663 p->kind = Try_kind;
1664 p->v.Try.body = body;
1665 p->v.Try.handlers = handlers;
1666 p->v.Try.orelse = orelse;
1667 p->v.Try.finalbody = finalbody;
1668 p->lineno = lineno;
1669 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001670 p->end_lineno = end_lineno;
1671 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001672 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673}
1674
1675stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001676Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, int end_lineno,
1677 int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001679 stmt_ty p;
1680 if (!test) {
1681 PyErr_SetString(PyExc_ValueError,
1682 "field test is required for Assert");
1683 return NULL;
1684 }
1685 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1686 if (!p)
1687 return NULL;
1688 p->kind = Assert_kind;
1689 p->v.Assert.test = test;
1690 p->v.Assert.msg = msg;
1691 p->lineno = lineno;
1692 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001693 p->end_lineno = end_lineno;
1694 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001695 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696}
1697
1698stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001699Import(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1700 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001702 stmt_ty p;
1703 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1704 if (!p)
1705 return NULL;
1706 p->kind = Import_kind;
1707 p->v.Import.names = names;
1708 p->lineno = lineno;
1709 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001710 p->end_lineno = end_lineno;
1711 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001712 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713}
1714
1715stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001716ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001717 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001719 stmt_ty p;
1720 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1721 if (!p)
1722 return NULL;
1723 p->kind = ImportFrom_kind;
1724 p->v.ImportFrom.module = module;
1725 p->v.ImportFrom.names = names;
1726 p->v.ImportFrom.level = level;
1727 p->lineno = lineno;
1728 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001729 p->end_lineno = end_lineno;
1730 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001731 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732}
1733
1734stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001735Global(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1736 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001738 stmt_ty p;
1739 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1740 if (!p)
1741 return NULL;
1742 p->kind = Global_kind;
1743 p->v.Global.names = names;
1744 p->lineno = lineno;
1745 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001746 p->end_lineno = end_lineno;
1747 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001748 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
1751stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001752Nonlocal(asdl_seq * names, int lineno, int col_offset, int end_lineno, int
1753 end_col_offset, PyArena *arena)
Jeremy Hylton81e95022007-02-27 06:50:52 +00001754{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001755 stmt_ty p;
1756 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1757 if (!p)
1758 return NULL;
1759 p->kind = Nonlocal_kind;
1760 p->v.Nonlocal.names = names;
1761 p->lineno = lineno;
1762 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001763 p->end_lineno = end_lineno;
1764 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001765 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001766}
1767
1768stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001769Expr(expr_ty value, int lineno, int col_offset, int end_lineno, int
1770 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001772 stmt_ty p;
1773 if (!value) {
1774 PyErr_SetString(PyExc_ValueError,
1775 "field value is required for Expr");
1776 return NULL;
1777 }
1778 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1779 if (!p)
1780 return NULL;
1781 p->kind = Expr_kind;
1782 p->v.Expr.value = value;
1783 p->lineno = lineno;
1784 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001785 p->end_lineno = end_lineno;
1786 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001787 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788}
1789
1790stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001791Pass(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1792 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001794 stmt_ty p;
1795 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1796 if (!p)
1797 return NULL;
1798 p->kind = Pass_kind;
1799 p->lineno = lineno;
1800 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001801 p->end_lineno = end_lineno;
1802 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001803 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804}
1805
1806stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001807Break(int lineno, int col_offset, int end_lineno, int end_col_offset, PyArena
1808 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001810 stmt_ty p;
1811 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1812 if (!p)
1813 return NULL;
1814 p->kind = Break_kind;
1815 p->lineno = lineno;
1816 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001817 p->end_lineno = end_lineno;
1818 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001819 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820}
1821
1822stmt_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001823Continue(int lineno, int col_offset, int end_lineno, int end_col_offset,
1824 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001826 stmt_ty p;
1827 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1828 if (!p)
1829 return NULL;
1830 p->kind = Continue_kind;
1831 p->lineno = lineno;
1832 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001833 p->end_lineno = end_lineno;
1834 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001835 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836}
1837
1838expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001839BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, int
1840 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001842 expr_ty p;
1843 if (!op) {
1844 PyErr_SetString(PyExc_ValueError,
1845 "field op is required for BoolOp");
1846 return NULL;
1847 }
1848 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1849 if (!p)
1850 return NULL;
1851 p->kind = BoolOp_kind;
1852 p->v.BoolOp.op = op;
1853 p->v.BoolOp.values = values;
1854 p->lineno = lineno;
1855 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001856 p->end_lineno = end_lineno;
1857 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001858 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859}
1860
1861expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001862NamedExpr(expr_ty target, expr_ty value, int lineno, int col_offset, int
1863 end_lineno, int end_col_offset, PyArena *arena)
1864{
1865 expr_ty p;
1866 if (!target) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "field target is required for NamedExpr");
1869 return NULL;
1870 }
1871 if (!value) {
1872 PyErr_SetString(PyExc_ValueError,
1873 "field value is required for NamedExpr");
1874 return NULL;
1875 }
1876 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1877 if (!p)
1878 return NULL;
1879 p->kind = NamedExpr_kind;
1880 p->v.NamedExpr.target = target;
1881 p->v.NamedExpr.value = value;
1882 p->lineno = lineno;
1883 p->col_offset = col_offset;
1884 p->end_lineno = end_lineno;
1885 p->end_col_offset = end_col_offset;
1886 return p;
1887}
1888
1889expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001890BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001891 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001893 expr_ty p;
1894 if (!left) {
1895 PyErr_SetString(PyExc_ValueError,
1896 "field left is required for BinOp");
1897 return NULL;
1898 }
1899 if (!op) {
1900 PyErr_SetString(PyExc_ValueError,
1901 "field op is required for BinOp");
1902 return NULL;
1903 }
1904 if (!right) {
1905 PyErr_SetString(PyExc_ValueError,
1906 "field right is required for BinOp");
1907 return NULL;
1908 }
1909 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1910 if (!p)
1911 return NULL;
1912 p->kind = BinOp_kind;
1913 p->v.BinOp.left = left;
1914 p->v.BinOp.op = op;
1915 p->v.BinOp.right = right;
1916 p->lineno = lineno;
1917 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001918 p->end_lineno = end_lineno;
1919 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001920 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921}
1922
1923expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001924UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, int
1925 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001927 expr_ty p;
1928 if (!op) {
1929 PyErr_SetString(PyExc_ValueError,
1930 "field op is required for UnaryOp");
1931 return NULL;
1932 }
1933 if (!operand) {
1934 PyErr_SetString(PyExc_ValueError,
1935 "field operand is required for UnaryOp");
1936 return NULL;
1937 }
1938 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1939 if (!p)
1940 return NULL;
1941 p->kind = UnaryOp_kind;
1942 p->v.UnaryOp.op = op;
1943 p->v.UnaryOp.operand = operand;
1944 p->lineno = lineno;
1945 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001946 p->end_lineno = end_lineno;
1947 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001948 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949}
1950
1951expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001952Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, int
1953 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001955 expr_ty p;
1956 if (!args) {
1957 PyErr_SetString(PyExc_ValueError,
1958 "field args is required for Lambda");
1959 return NULL;
1960 }
1961 if (!body) {
1962 PyErr_SetString(PyExc_ValueError,
1963 "field body is required for Lambda");
1964 return NULL;
1965 }
1966 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1967 if (!p)
1968 return NULL;
1969 p->kind = Lambda_kind;
1970 p->v.Lambda.args = args;
1971 p->v.Lambda.body = body;
1972 p->lineno = lineno;
1973 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001974 p->end_lineno = end_lineno;
1975 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001976 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977}
1978
1979expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001980IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001981 int end_lineno, int end_col_offset, PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001982{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001983 expr_ty p;
1984 if (!test) {
1985 PyErr_SetString(PyExc_ValueError,
1986 "field test is required for IfExp");
1987 return NULL;
1988 }
1989 if (!body) {
1990 PyErr_SetString(PyExc_ValueError,
1991 "field body is required for IfExp");
1992 return NULL;
1993 }
1994 if (!orelse) {
1995 PyErr_SetString(PyExc_ValueError,
1996 "field orelse is required for IfExp");
1997 return NULL;
1998 }
1999 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2000 if (!p)
2001 return NULL;
2002 p->kind = IfExp_kind;
2003 p->v.IfExp.test = test;
2004 p->v.IfExp.body = body;
2005 p->v.IfExp.orelse = orelse;
2006 p->lineno = lineno;
2007 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002008 p->end_lineno = end_lineno;
2009 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002010 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002011}
2012
2013expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002014Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, int
2015 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002017 expr_ty p;
2018 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2019 if (!p)
2020 return NULL;
2021 p->kind = Dict_kind;
2022 p->v.Dict.keys = keys;
2023 p->v.Dict.values = values;
2024 p->lineno = lineno;
2025 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002026 p->end_lineno = end_lineno;
2027 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002028 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029}
2030
2031expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002032Set(asdl_seq * elts, int lineno, int col_offset, int end_lineno, int
2033 end_col_offset, PyArena *arena)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002034{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002035 expr_ty p;
2036 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2037 if (!p)
2038 return NULL;
2039 p->kind = Set_kind;
2040 p->v.Set.elts = elts;
2041 p->lineno = lineno;
2042 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002043 p->end_lineno = end_lineno;
2044 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002045 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002046}
2047
2048expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002049ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2050 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002052 expr_ty p;
2053 if (!elt) {
2054 PyErr_SetString(PyExc_ValueError,
2055 "field elt is required for ListComp");
2056 return NULL;
2057 }
2058 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2059 if (!p)
2060 return NULL;
2061 p->kind = ListComp_kind;
2062 p->v.ListComp.elt = elt;
2063 p->v.ListComp.generators = generators;
2064 p->lineno = lineno;
2065 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002066 p->end_lineno = end_lineno;
2067 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002068 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069}
2070
2071expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002072SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, int
2073 end_lineno, int end_col_offset, PyArena *arena)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002074{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002075 expr_ty p;
2076 if (!elt) {
2077 PyErr_SetString(PyExc_ValueError,
2078 "field elt is required for SetComp");
2079 return NULL;
2080 }
2081 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2082 if (!p)
2083 return NULL;
2084 p->kind = SetComp_kind;
2085 p->v.SetComp.elt = elt;
2086 p->v.SetComp.generators = generators;
2087 p->lineno = lineno;
2088 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002089 p->end_lineno = end_lineno;
2090 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002091 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002092}
2093
2094expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002095DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002096 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossum992d4a32007-07-11 13:09:30 +00002097{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002098 expr_ty p;
2099 if (!key) {
2100 PyErr_SetString(PyExc_ValueError,
2101 "field key is required for DictComp");
2102 return NULL;
2103 }
2104 if (!value) {
2105 PyErr_SetString(PyExc_ValueError,
2106 "field value is required for DictComp");
2107 return NULL;
2108 }
2109 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2110 if (!p)
2111 return NULL;
2112 p->kind = DictComp_kind;
2113 p->v.DictComp.key = key;
2114 p->v.DictComp.value = value;
2115 p->v.DictComp.generators = generators;
2116 p->lineno = lineno;
2117 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002118 p->end_lineno = end_lineno;
2119 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002120 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002121}
2122
2123expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002124GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002125 int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002127 expr_ty p;
2128 if (!elt) {
2129 PyErr_SetString(PyExc_ValueError,
2130 "field elt is required for GeneratorExp");
2131 return NULL;
2132 }
2133 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2134 if (!p)
2135 return NULL;
2136 p->kind = GeneratorExp_kind;
2137 p->v.GeneratorExp.elt = elt;
2138 p->v.GeneratorExp.generators = generators;
2139 p->lineno = lineno;
2140 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002141 p->end_lineno = end_lineno;
2142 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002143 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144}
2145
2146expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002147Await(expr_ty value, int lineno, int col_offset, int end_lineno, int
2148 end_col_offset, PyArena *arena)
Yury Selivanov75445082015-05-11 22:57:16 -04002149{
2150 expr_ty p;
2151 if (!value) {
2152 PyErr_SetString(PyExc_ValueError,
2153 "field value is required for Await");
2154 return NULL;
2155 }
2156 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2157 if (!p)
2158 return NULL;
2159 p->kind = Await_kind;
2160 p->v.Await.value = value;
2161 p->lineno = lineno;
2162 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002163 p->end_lineno = end_lineno;
2164 p->end_col_offset = end_col_offset;
Yury Selivanov75445082015-05-11 22:57:16 -04002165 return p;
2166}
2167
2168expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002169Yield(expr_ty value, int lineno, int col_offset, int end_lineno, int
2170 end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002172 expr_ty p;
2173 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2174 if (!p)
2175 return NULL;
2176 p->kind = Yield_kind;
2177 p->v.Yield.value = value;
2178 p->lineno = lineno;
2179 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002180 p->end_lineno = end_lineno;
2181 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002182 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002183}
2184
2185expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002186YieldFrom(expr_ty value, int lineno, int col_offset, int end_lineno, int
2187 end_col_offset, PyArena *arena)
Benjamin Peterson527c6222012-01-14 08:58:23 -05002188{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002189 expr_ty p;
2190 if (!value) {
2191 PyErr_SetString(PyExc_ValueError,
2192 "field value is required for YieldFrom");
2193 return NULL;
2194 }
2195 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2196 if (!p)
2197 return NULL;
2198 p->kind = YieldFrom_kind;
2199 p->v.YieldFrom.value = value;
2200 p->lineno = lineno;
2201 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002202 p->end_lineno = end_lineno;
2203 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002204 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002205}
2206
2207expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002209 int col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002211 expr_ty p;
2212 if (!left) {
2213 PyErr_SetString(PyExc_ValueError,
2214 "field left is required for Compare");
2215 return NULL;
2216 }
2217 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2218 if (!p)
2219 return NULL;
2220 p->kind = Compare_kind;
2221 p->v.Compare.left = left;
2222 p->v.Compare.ops = ops;
2223 p->v.Compare.comparators = comparators;
2224 p->lineno = lineno;
2225 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002226 p->end_lineno = end_lineno;
2227 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002228 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229}
2230
2231expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002232Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002233 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002235 expr_ty p;
2236 if (!func) {
2237 PyErr_SetString(PyExc_ValueError,
2238 "field func is required for Call");
2239 return NULL;
2240 }
2241 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2242 if (!p)
2243 return NULL;
2244 p->kind = Call_kind;
2245 p->v.Call.func = func;
2246 p->v.Call.args = args;
2247 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002248 p->lineno = lineno;
2249 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002250 p->end_lineno = end_lineno;
2251 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002252 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253}
2254
2255expr_ty
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04002256FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2257 int col_offset, int end_lineno, int end_col_offset, PyArena
2258 *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04002259{
2260 expr_ty p;
2261 if (!value) {
2262 PyErr_SetString(PyExc_ValueError,
2263 "field value is required for FormattedValue");
2264 return NULL;
2265 }
2266 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2267 if (!p)
2268 return NULL;
2269 p->kind = FormattedValue_kind;
2270 p->v.FormattedValue.value = value;
2271 p->v.FormattedValue.conversion = conversion;
2272 p->v.FormattedValue.format_spec = format_spec;
2273 p->lineno = lineno;
2274 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002275 p->end_lineno = end_lineno;
2276 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04002277 return p;
2278}
2279
2280expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002281JoinedStr(asdl_seq * values, int lineno, int col_offset, int end_lineno, int
2282 end_col_offset, PyArena *arena)
Eric V. Smith235a6f02015-09-19 14:51:32 -04002283{
2284 expr_ty p;
2285 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2286 if (!p)
2287 return NULL;
2288 p->kind = JoinedStr_kind;
2289 p->v.JoinedStr.values = values;
2290 p->lineno = lineno;
2291 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002292 p->end_lineno = end_lineno;
2293 p->end_col_offset = end_col_offset;
Eric V. Smith235a6f02015-09-19 14:51:32 -04002294 return p;
2295}
2296
2297expr_ty
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002298Constant(constant value, string kind, int lineno, int col_offset, int
2299 end_lineno, int end_col_offset, PyArena *arena)
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002300{
2301 expr_ty p;
2302 if (!value) {
2303 PyErr_SetString(PyExc_ValueError,
2304 "field value is required for Constant");
2305 return NULL;
2306 }
2307 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2308 if (!p)
2309 return NULL;
2310 p->kind = Constant_kind;
2311 p->v.Constant.value = value;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002312 p->v.Constant.kind = kind;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002313 p->lineno = lineno;
2314 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002315 p->end_lineno = end_lineno;
2316 p->end_col_offset = end_col_offset;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002317 return p;
2318}
2319
2320expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002321Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002322 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002324 expr_ty p;
2325 if (!value) {
2326 PyErr_SetString(PyExc_ValueError,
2327 "field value is required for Attribute");
2328 return NULL;
2329 }
2330 if (!attr) {
2331 PyErr_SetString(PyExc_ValueError,
2332 "field attr is required for Attribute");
2333 return NULL;
2334 }
2335 if (!ctx) {
2336 PyErr_SetString(PyExc_ValueError,
2337 "field ctx is required for Attribute");
2338 return NULL;
2339 }
2340 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2341 if (!p)
2342 return NULL;
2343 p->kind = Attribute_kind;
2344 p->v.Attribute.value = value;
2345 p->v.Attribute.attr = attr;
2346 p->v.Attribute.ctx = ctx;
2347 p->lineno = lineno;
2348 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002349 p->end_lineno = end_lineno;
2350 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002351 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352}
2353
2354expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002355Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002356 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002358 expr_ty p;
2359 if (!value) {
2360 PyErr_SetString(PyExc_ValueError,
2361 "field value is required for Subscript");
2362 return NULL;
2363 }
2364 if (!slice) {
2365 PyErr_SetString(PyExc_ValueError,
2366 "field slice is required for Subscript");
2367 return NULL;
2368 }
2369 if (!ctx) {
2370 PyErr_SetString(PyExc_ValueError,
2371 "field ctx is required for Subscript");
2372 return NULL;
2373 }
2374 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2375 if (!p)
2376 return NULL;
2377 p->kind = Subscript_kind;
2378 p->v.Subscript.value = value;
2379 p->v.Subscript.slice = slice;
2380 p->v.Subscript.ctx = ctx;
2381 p->lineno = lineno;
2382 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002383 p->end_lineno = end_lineno;
2384 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002385 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386}
2387
2388expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002389Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, int
2390 end_lineno, int end_col_offset, PyArena *arena)
Guido van Rossum0368b722007-05-11 16:50:42 +00002391{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002392 expr_ty p;
2393 if (!value) {
2394 PyErr_SetString(PyExc_ValueError,
2395 "field value is required for Starred");
2396 return NULL;
2397 }
2398 if (!ctx) {
2399 PyErr_SetString(PyExc_ValueError,
2400 "field ctx is required for Starred");
2401 return NULL;
2402 }
2403 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2404 if (!p)
2405 return NULL;
2406 p->kind = Starred_kind;
2407 p->v.Starred.value = value;
2408 p->v.Starred.ctx = ctx;
2409 p->lineno = lineno;
2410 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002411 p->end_lineno = end_lineno;
2412 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002413 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002414}
2415
2416expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002417Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, int
2418 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002420 expr_ty p;
2421 if (!id) {
2422 PyErr_SetString(PyExc_ValueError,
2423 "field id is required for Name");
2424 return NULL;
2425 }
2426 if (!ctx) {
2427 PyErr_SetString(PyExc_ValueError,
2428 "field ctx is required for Name");
2429 return NULL;
2430 }
2431 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2432 if (!p)
2433 return NULL;
2434 p->kind = Name_kind;
2435 p->v.Name.id = id;
2436 p->v.Name.ctx = ctx;
2437 p->lineno = lineno;
2438 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002439 p->end_lineno = end_lineno;
2440 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002441 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442}
2443
2444expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002445List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
2446 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002448 expr_ty p;
2449 if (!ctx) {
2450 PyErr_SetString(PyExc_ValueError,
2451 "field ctx is required for List");
2452 return NULL;
2453 }
2454 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2455 if (!p)
2456 return NULL;
2457 p->kind = List_kind;
2458 p->v.List.elts = elts;
2459 p->v.List.ctx = ctx;
2460 p->lineno = lineno;
2461 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002462 p->end_lineno = end_lineno;
2463 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002464 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465}
2466
2467expr_ty
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002468Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, int
2469 end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002471 expr_ty p;
2472 if (!ctx) {
2473 PyErr_SetString(PyExc_ValueError,
2474 "field ctx is required for Tuple");
2475 return NULL;
2476 }
2477 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2478 if (!p)
2479 return NULL;
2480 p->kind = Tuple_kind;
2481 p->v.Tuple.elts = elts;
2482 p->v.Tuple.ctx = ctx;
2483 p->lineno = lineno;
2484 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002485 p->end_lineno = end_lineno;
2486 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002487 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488}
2489
2490slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002491Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002493 slice_ty p;
2494 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2495 if (!p)
2496 return NULL;
2497 p->kind = Slice_kind;
2498 p->v.Slice.lower = lower;
2499 p->v.Slice.upper = upper;
2500 p->v.Slice.step = step;
2501 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502}
2503
2504slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002505ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002507 slice_ty p;
2508 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2509 if (!p)
2510 return NULL;
2511 p->kind = ExtSlice_kind;
2512 p->v.ExtSlice.dims = dims;
2513 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514}
2515
2516slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002517Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002519 slice_ty p;
2520 if (!value) {
2521 PyErr_SetString(PyExc_ValueError,
2522 "field value is required for Index");
2523 return NULL;
2524 }
2525 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2526 if (!p)
2527 return NULL;
2528 p->kind = Index_kind;
2529 p->v.Index.value = value;
2530 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531}
2532
2533comprehension_ty
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002534comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, int is_async,
2535 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002537 comprehension_ty p;
2538 if (!target) {
2539 PyErr_SetString(PyExc_ValueError,
2540 "field target is required for comprehension");
2541 return NULL;
2542 }
2543 if (!iter) {
2544 PyErr_SetString(PyExc_ValueError,
2545 "field iter is required for comprehension");
2546 return NULL;
2547 }
2548 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2549 if (!p)
2550 return NULL;
2551 p->target = target;
2552 p->iter = iter;
2553 p->ifs = ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002554 p->is_async = is_async;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002555 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002559ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002560 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002562 excepthandler_ty p;
2563 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2564 if (!p)
2565 return NULL;
2566 p->kind = ExceptHandler_kind;
2567 p->v.ExceptHandler.type = type;
2568 p->v.ExceptHandler.name = name;
2569 p->v.ExceptHandler.body = body;
2570 p->lineno = lineno;
2571 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002572 p->end_lineno = end_lineno;
2573 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002574 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575}
2576
2577arguments_ty
Pablo Galindocd6e83b2019-07-15 01:32:18 +02002578arguments(asdl_seq * posonlyargs, asdl_seq * args, arg_ty vararg, asdl_seq *
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002579 kwonlyargs, asdl_seq * kw_defaults, arg_ty kwarg, asdl_seq *
2580 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002582 arguments_ty p;
2583 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2584 if (!p)
2585 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002586 p->posonlyargs = posonlyargs;
Pablo Galindocd6e83b2019-07-15 01:32:18 +02002587 p->args = args;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002588 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002589 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002590 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002591 p->kwarg = kwarg;
2592 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002593 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594}
2595
Neal Norwitzc1505362006-12-28 06:47:50 +00002596arg_ty
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002597arg(identifier arg, expr_ty annotation, string type_comment, int lineno, int
2598 col_offset, int end_lineno, int end_col_offset, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002599{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002600 arg_ty p;
2601 if (!arg) {
2602 PyErr_SetString(PyExc_ValueError,
2603 "field arg is required for arg");
2604 return NULL;
2605 }
2606 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2607 if (!p)
2608 return NULL;
2609 p->arg = arg;
2610 p->annotation = annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002611 p->type_comment = type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01002612 p->lineno = lineno;
2613 p->col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002614 p->end_lineno = end_lineno;
2615 p->end_col_offset = end_col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002616 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002617}
2618
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002620keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002622 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002623 if (!value) {
2624 PyErr_SetString(PyExc_ValueError,
2625 "field value is required for keyword");
2626 return NULL;
2627 }
2628 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2629 if (!p)
2630 return NULL;
2631 p->arg = arg;
2632 p->value = value;
2633 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634}
2635
2636alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002637alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002639 alias_ty p;
2640 if (!name) {
2641 PyErr_SetString(PyExc_ValueError,
2642 "field name is required for alias");
2643 return NULL;
2644 }
2645 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2646 if (!p)
2647 return NULL;
2648 p->name = name;
2649 p->asname = asname;
2650 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651}
2652
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002653withitem_ty
2654withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2655{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002656 withitem_ty p;
2657 if (!context_expr) {
2658 PyErr_SetString(PyExc_ValueError,
2659 "field context_expr is required for withitem");
2660 return NULL;
2661 }
2662 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2663 if (!p)
2664 return NULL;
2665 p->context_expr = context_expr;
2666 p->optional_vars = optional_vars;
2667 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002668}
2669
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002670type_ignore_ty
Michael J. Sullivan933e1502019-05-22 07:54:20 -07002671TypeIgnore(int lineno, string tag, PyArena *arena)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002672{
2673 type_ignore_ty p;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07002674 if (!tag) {
2675 PyErr_SetString(PyExc_ValueError,
2676 "field tag is required for TypeIgnore");
2677 return NULL;
2678 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002679 p = (type_ignore_ty)PyArena_Malloc(arena, sizeof(*p));
2680 if (!p)
2681 return NULL;
2682 p->kind = TypeIgnore_kind;
2683 p->v.TypeIgnore.lineno = lineno;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07002684 p->v.TypeIgnore.tag = tag;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002685 return p;
2686}
2687
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002688
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002689PyObject*
2690ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002691{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002692 mod_ty o = (mod_ty)_o;
2693 PyObject *result = NULL, *value = NULL;
2694 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002695 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002696 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002697
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002698 switch (o->kind) {
2699 case Module_kind:
2700 result = PyType_GenericNew(Module_type, NULL, NULL);
2701 if (!result) goto failed;
2702 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2703 if (!value) goto failed;
2704 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002707 value = ast2obj_list(o->v.Module.type_ignores, ast2obj_type_ignore);
2708 if (!value) goto failed;
2709 if (_PyObject_SetAttrId(result, &PyId_type_ignores, value) == -1)
2710 goto failed;
2711 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002712 break;
2713 case Interactive_kind:
2714 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2715 if (!result) goto failed;
2716 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2717 if (!value) goto failed;
2718 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2719 goto failed;
2720 Py_DECREF(value);
2721 break;
2722 case Expression_kind:
2723 result = PyType_GenericNew(Expression_type, NULL, NULL);
2724 if (!result) goto failed;
2725 value = ast2obj_expr(o->v.Expression.body);
2726 if (!value) goto failed;
2727 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2728 goto failed;
2729 Py_DECREF(value);
2730 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002731 case FunctionType_kind:
2732 result = PyType_GenericNew(FunctionType_type, NULL, NULL);
2733 if (!result) goto failed;
2734 value = ast2obj_list(o->v.FunctionType.argtypes, ast2obj_expr);
2735 if (!value) goto failed;
2736 if (_PyObject_SetAttrId(result, &PyId_argtypes, value) == -1)
2737 goto failed;
2738 Py_DECREF(value);
2739 value = ast2obj_expr(o->v.FunctionType.returns);
2740 if (!value) goto failed;
2741 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2742 goto failed;
2743 Py_DECREF(value);
2744 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002745 case Suite_kind:
2746 result = PyType_GenericNew(Suite_type, NULL, NULL);
2747 if (!result) goto failed;
2748 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2749 if (!value) goto failed;
2750 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2751 goto failed;
2752 Py_DECREF(value);
2753 break;
2754 }
2755 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002756failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002757 Py_XDECREF(value);
2758 Py_XDECREF(result);
2759 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760}
2761
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002762PyObject*
2763ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002765 stmt_ty o = (stmt_ty)_o;
2766 PyObject *result = NULL, *value = NULL;
2767 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02002768 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002769 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002770
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002771 switch (o->kind) {
2772 case FunctionDef_kind:
2773 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2774 if (!result) goto failed;
2775 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002776 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002777 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2778 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002779 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002780 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002781 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002782 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2783 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002784 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002785 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2786 if (!value) goto failed;
2787 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2788 goto failed;
2789 Py_DECREF(value);
2790 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2791 if (!value) goto failed;
2792 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2793 goto failed;
2794 Py_DECREF(value);
2795 value = ast2obj_expr(o->v.FunctionDef.returns);
2796 if (!value) goto failed;
2797 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2798 goto failed;
2799 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002800 value = ast2obj_string(o->v.FunctionDef.type_comment);
2801 if (!value) goto failed;
2802 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2803 goto failed;
2804 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002805 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002806 case AsyncFunctionDef_kind:
2807 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2808 if (!result) goto failed;
2809 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2810 if (!value) goto failed;
2811 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2812 goto failed;
2813 Py_DECREF(value);
2814 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2815 if (!value) goto failed;
2816 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2817 goto failed;
2818 Py_DECREF(value);
2819 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2820 if (!value) goto failed;
2821 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2825 ast2obj_expr);
2826 if (!value) goto failed;
2827 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2828 goto failed;
2829 Py_DECREF(value);
2830 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2831 if (!value) goto failed;
2832 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2833 goto failed;
2834 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002835 value = ast2obj_string(o->v.AsyncFunctionDef.type_comment);
2836 if (!value) goto failed;
2837 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2838 goto failed;
2839 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04002840 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002841 case ClassDef_kind:
2842 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2843 if (!result) goto failed;
2844 value = ast2obj_identifier(o->v.ClassDef.name);
2845 if (!value) goto failed;
2846 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2847 goto failed;
2848 Py_DECREF(value);
2849 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2850 if (!value) goto failed;
2851 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2852 goto failed;
2853 Py_DECREF(value);
2854 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2855 if (!value) goto failed;
2856 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2857 goto failed;
2858 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002859 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2860 if (!value) goto failed;
2861 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2862 goto failed;
2863 Py_DECREF(value);
2864 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2865 if (!value) goto failed;
2866 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2867 goto failed;
2868 Py_DECREF(value);
2869 break;
2870 case Return_kind:
2871 result = PyType_GenericNew(Return_type, NULL, NULL);
2872 if (!result) goto failed;
2873 value = ast2obj_expr(o->v.Return.value);
2874 if (!value) goto failed;
2875 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2876 goto failed;
2877 Py_DECREF(value);
2878 break;
2879 case Delete_kind:
2880 result = PyType_GenericNew(Delete_type, NULL, NULL);
2881 if (!result) goto failed;
2882 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2883 if (!value) goto failed;
2884 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2885 goto failed;
2886 Py_DECREF(value);
2887 break;
2888 case Assign_kind:
2889 result = PyType_GenericNew(Assign_type, NULL, NULL);
2890 if (!result) goto failed;
2891 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2892 if (!value) goto failed;
2893 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
2896 value = ast2obj_expr(o->v.Assign.value);
2897 if (!value) goto failed;
2898 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2899 goto failed;
2900 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002901 value = ast2obj_string(o->v.Assign.type_comment);
2902 if (!value) goto failed;
2903 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2904 goto failed;
2905 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002906 break;
2907 case AugAssign_kind:
2908 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2909 if (!result) goto failed;
2910 value = ast2obj_expr(o->v.AugAssign.target);
2911 if (!value) goto failed;
2912 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2913 goto failed;
2914 Py_DECREF(value);
2915 value = ast2obj_operator(o->v.AugAssign.op);
2916 if (!value) goto failed;
2917 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2918 goto failed;
2919 Py_DECREF(value);
2920 value = ast2obj_expr(o->v.AugAssign.value);
2921 if (!value) goto failed;
2922 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2923 goto failed;
2924 Py_DECREF(value);
2925 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002926 case AnnAssign_kind:
2927 result = PyType_GenericNew(AnnAssign_type, NULL, NULL);
2928 if (!result) goto failed;
2929 value = ast2obj_expr(o->v.AnnAssign.target);
2930 if (!value) goto failed;
2931 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2932 goto failed;
2933 Py_DECREF(value);
2934 value = ast2obj_expr(o->v.AnnAssign.annotation);
2935 if (!value) goto failed;
2936 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
2937 goto failed;
2938 Py_DECREF(value);
2939 value = ast2obj_expr(o->v.AnnAssign.value);
2940 if (!value) goto failed;
2941 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2942 goto failed;
2943 Py_DECREF(value);
2944 value = ast2obj_int(o->v.AnnAssign.simple);
2945 if (!value) goto failed;
2946 if (_PyObject_SetAttrId(result, &PyId_simple, value) == -1)
2947 goto failed;
2948 Py_DECREF(value);
2949 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002950 case For_kind:
2951 result = PyType_GenericNew(For_type, NULL, NULL);
2952 if (!result) goto failed;
2953 value = ast2obj_expr(o->v.For.target);
2954 if (!value) goto failed;
2955 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2956 goto failed;
2957 Py_DECREF(value);
2958 value = ast2obj_expr(o->v.For.iter);
2959 if (!value) goto failed;
2960 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2961 goto failed;
2962 Py_DECREF(value);
2963 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2964 if (!value) goto failed;
2965 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2969 if (!value) goto failed;
2970 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002973 value = ast2obj_string(o->v.For.type_comment);
2974 if (!value) goto failed;
2975 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002978 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002979 case AsyncFor_kind:
2980 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2981 if (!result) goto failed;
2982 value = ast2obj_expr(o->v.AsyncFor.target);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 value = ast2obj_expr(o->v.AsyncFor.iter);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2993 if (!value) goto failed;
2994 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2995 goto failed;
2996 Py_DECREF(value);
2997 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2998 if (!value) goto failed;
2999 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3000 goto failed;
3001 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003002 value = ast2obj_string(o->v.AsyncFor.type_comment);
3003 if (!value) goto failed;
3004 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003007 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003008 case While_kind:
3009 result = PyType_GenericNew(While_type, NULL, NULL);
3010 if (!result) goto failed;
3011 value = ast2obj_expr(o->v.While.test);
3012 if (!value) goto failed;
3013 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3014 goto failed;
3015 Py_DECREF(value);
3016 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
3017 if (!value) goto failed;
3018 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
3021 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
3022 if (!value) goto failed;
3023 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3024 goto failed;
3025 Py_DECREF(value);
3026 break;
3027 case If_kind:
3028 result = PyType_GenericNew(If_type, NULL, NULL);
3029 if (!result) goto failed;
3030 value = ast2obj_expr(o->v.If.test);
3031 if (!value) goto failed;
3032 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3033 goto failed;
3034 Py_DECREF(value);
3035 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
3036 if (!value) goto failed;
3037 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3038 goto failed;
3039 Py_DECREF(value);
3040 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
3041 if (!value) goto failed;
3042 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3043 goto failed;
3044 Py_DECREF(value);
3045 break;
3046 case With_kind:
3047 result = PyType_GenericNew(With_type, NULL, NULL);
3048 if (!result) goto failed;
3049 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
3050 if (!value) goto failed;
3051 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
3052 goto failed;
3053 Py_DECREF(value);
3054 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
3055 if (!value) goto failed;
3056 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3057 goto failed;
3058 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003059 value = ast2obj_string(o->v.With.type_comment);
3060 if (!value) goto failed;
3061 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
3062 goto failed;
3063 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003064 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003065 case AsyncWith_kind:
3066 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
3067 if (!result) goto failed;
3068 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
3069 if (!value) goto failed;
3070 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
3071 goto failed;
3072 Py_DECREF(value);
3073 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
3074 if (!value) goto failed;
3075 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3076 goto failed;
3077 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003078 value = ast2obj_string(o->v.AsyncWith.type_comment);
3079 if (!value) goto failed;
3080 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
3081 goto failed;
3082 Py_DECREF(value);
Yury Selivanov75445082015-05-11 22:57:16 -04003083 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003084 case Raise_kind:
3085 result = PyType_GenericNew(Raise_type, NULL, NULL);
3086 if (!result) goto failed;
3087 value = ast2obj_expr(o->v.Raise.exc);
3088 if (!value) goto failed;
3089 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
3090 goto failed;
3091 Py_DECREF(value);
3092 value = ast2obj_expr(o->v.Raise.cause);
3093 if (!value) goto failed;
3094 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
3095 goto failed;
3096 Py_DECREF(value);
3097 break;
3098 case Try_kind:
3099 result = PyType_GenericNew(Try_type, NULL, NULL);
3100 if (!result) goto failed;
3101 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
3102 if (!value) goto failed;
3103 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3104 goto failed;
3105 Py_DECREF(value);
3106 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
3107 if (!value) goto failed;
3108 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
3109 goto failed;
3110 Py_DECREF(value);
3111 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
3112 if (!value) goto failed;
3113 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3114 goto failed;
3115 Py_DECREF(value);
3116 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
3117 if (!value) goto failed;
3118 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
3119 goto failed;
3120 Py_DECREF(value);
3121 break;
3122 case Assert_kind:
3123 result = PyType_GenericNew(Assert_type, NULL, NULL);
3124 if (!result) goto failed;
3125 value = ast2obj_expr(o->v.Assert.test);
3126 if (!value) goto failed;
3127 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
3130 value = ast2obj_expr(o->v.Assert.msg);
3131 if (!value) goto failed;
3132 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
3133 goto failed;
3134 Py_DECREF(value);
3135 break;
3136 case Import_kind:
3137 result = PyType_GenericNew(Import_type, NULL, NULL);
3138 if (!result) goto failed;
3139 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
3140 if (!value) goto failed;
3141 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3142 goto failed;
3143 Py_DECREF(value);
3144 break;
3145 case ImportFrom_kind:
3146 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
3147 if (!result) goto failed;
3148 value = ast2obj_identifier(o->v.ImportFrom.module);
3149 if (!value) goto failed;
3150 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
3151 goto failed;
3152 Py_DECREF(value);
3153 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
3154 if (!value) goto failed;
3155 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3156 goto failed;
3157 Py_DECREF(value);
3158 value = ast2obj_int(o->v.ImportFrom.level);
3159 if (!value) goto failed;
3160 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
3161 goto failed;
3162 Py_DECREF(value);
3163 break;
3164 case Global_kind:
3165 result = PyType_GenericNew(Global_type, NULL, NULL);
3166 if (!result) goto failed;
3167 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
3168 if (!value) goto failed;
3169 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3170 goto failed;
3171 Py_DECREF(value);
3172 break;
3173 case Nonlocal_kind:
3174 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
3175 if (!result) goto failed;
3176 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
3177 if (!value) goto failed;
3178 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
3179 goto failed;
3180 Py_DECREF(value);
3181 break;
3182 case Expr_kind:
3183 result = PyType_GenericNew(Expr_type, NULL, NULL);
3184 if (!result) goto failed;
3185 value = ast2obj_expr(o->v.Expr.value);
3186 if (!value) goto failed;
3187 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3188 goto failed;
3189 Py_DECREF(value);
3190 break;
3191 case Pass_kind:
3192 result = PyType_GenericNew(Pass_type, NULL, NULL);
3193 if (!result) goto failed;
3194 break;
3195 case Break_kind:
3196 result = PyType_GenericNew(Break_type, NULL, NULL);
3197 if (!result) goto failed;
3198 break;
3199 case Continue_kind:
3200 result = PyType_GenericNew(Continue_type, NULL, NULL);
3201 if (!result) goto failed;
3202 break;
3203 }
3204 value = ast2obj_int(o->lineno);
3205 if (!value) goto failed;
3206 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3207 goto failed;
3208 Py_DECREF(value);
3209 value = ast2obj_int(o->col_offset);
3210 if (!value) goto failed;
3211 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3212 goto failed;
3213 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003214 value = ast2obj_int(o->end_lineno);
3215 if (!value) goto failed;
3216 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3217 goto failed;
3218 Py_DECREF(value);
3219 value = ast2obj_int(o->end_col_offset);
3220 if (!value) goto failed;
3221 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3222 goto failed;
3223 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003224 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003225failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003226 Py_XDECREF(value);
3227 Py_XDECREF(result);
3228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229}
3230
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003231PyObject*
3232ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003234 expr_ty o = (expr_ty)_o;
3235 PyObject *result = NULL, *value = NULL;
3236 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003237 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003238 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003239
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003240 switch (o->kind) {
3241 case BoolOp_kind:
3242 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3243 if (!result) goto failed;
3244 value = ast2obj_boolop(o->v.BoolOp.op);
3245 if (!value) goto failed;
3246 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3247 goto failed;
3248 Py_DECREF(value);
3249 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3250 if (!value) goto failed;
3251 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3252 goto failed;
3253 Py_DECREF(value);
3254 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003255 case NamedExpr_kind:
3256 result = PyType_GenericNew(NamedExpr_type, NULL, NULL);
3257 if (!result) goto failed;
3258 value = ast2obj_expr(o->v.NamedExpr.target);
3259 if (!value) goto failed;
3260 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3261 goto failed;
3262 Py_DECREF(value);
3263 value = ast2obj_expr(o->v.NamedExpr.value);
3264 if (!value) goto failed;
3265 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3266 goto failed;
3267 Py_DECREF(value);
3268 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003269 case BinOp_kind:
3270 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3271 if (!result) goto failed;
3272 value = ast2obj_expr(o->v.BinOp.left);
3273 if (!value) goto failed;
3274 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
3277 value = ast2obj_operator(o->v.BinOp.op);
3278 if (!value) goto failed;
3279 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
3282 value = ast2obj_expr(o->v.BinOp.right);
3283 if (!value) goto failed;
3284 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3285 goto failed;
3286 Py_DECREF(value);
3287 break;
3288 case UnaryOp_kind:
3289 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3290 if (!result) goto failed;
3291 value = ast2obj_unaryop(o->v.UnaryOp.op);
3292 if (!value) goto failed;
3293 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3294 goto failed;
3295 Py_DECREF(value);
3296 value = ast2obj_expr(o->v.UnaryOp.operand);
3297 if (!value) goto failed;
3298 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3299 goto failed;
3300 Py_DECREF(value);
3301 break;
3302 case Lambda_kind:
3303 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3304 if (!result) goto failed;
3305 value = ast2obj_arguments(o->v.Lambda.args);
3306 if (!value) goto failed;
3307 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3308 goto failed;
3309 Py_DECREF(value);
3310 value = ast2obj_expr(o->v.Lambda.body);
3311 if (!value) goto failed;
3312 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3313 goto failed;
3314 Py_DECREF(value);
3315 break;
3316 case IfExp_kind:
3317 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3318 if (!result) goto failed;
3319 value = ast2obj_expr(o->v.IfExp.test);
3320 if (!value) goto failed;
3321 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3322 goto failed;
3323 Py_DECREF(value);
3324 value = ast2obj_expr(o->v.IfExp.body);
3325 if (!value) goto failed;
3326 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3327 goto failed;
3328 Py_DECREF(value);
3329 value = ast2obj_expr(o->v.IfExp.orelse);
3330 if (!value) goto failed;
3331 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3332 goto failed;
3333 Py_DECREF(value);
3334 break;
3335 case Dict_kind:
3336 result = PyType_GenericNew(Dict_type, NULL, NULL);
3337 if (!result) goto failed;
3338 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3339 if (!value) goto failed;
3340 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3341 goto failed;
3342 Py_DECREF(value);
3343 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3344 if (!value) goto failed;
3345 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3346 goto failed;
3347 Py_DECREF(value);
3348 break;
3349 case Set_kind:
3350 result = PyType_GenericNew(Set_type, NULL, NULL);
3351 if (!result) goto failed;
3352 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3353 if (!value) goto failed;
3354 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3355 goto failed;
3356 Py_DECREF(value);
3357 break;
3358 case ListComp_kind:
3359 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3360 if (!result) goto failed;
3361 value = ast2obj_expr(o->v.ListComp.elt);
3362 if (!value) goto failed;
3363 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3364 goto failed;
3365 Py_DECREF(value);
3366 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3367 if (!value) goto failed;
3368 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3369 goto failed;
3370 Py_DECREF(value);
3371 break;
3372 case SetComp_kind:
3373 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3374 if (!result) goto failed;
3375 value = ast2obj_expr(o->v.SetComp.elt);
3376 if (!value) goto failed;
3377 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3378 goto failed;
3379 Py_DECREF(value);
3380 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3381 if (!value) goto failed;
3382 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3383 goto failed;
3384 Py_DECREF(value);
3385 break;
3386 case DictComp_kind:
3387 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3388 if (!result) goto failed;
3389 value = ast2obj_expr(o->v.DictComp.key);
3390 if (!value) goto failed;
3391 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3392 goto failed;
3393 Py_DECREF(value);
3394 value = ast2obj_expr(o->v.DictComp.value);
3395 if (!value) goto failed;
3396 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3397 goto failed;
3398 Py_DECREF(value);
3399 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3400 if (!value) goto failed;
3401 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3402 goto failed;
3403 Py_DECREF(value);
3404 break;
3405 case GeneratorExp_kind:
3406 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3407 if (!result) goto failed;
3408 value = ast2obj_expr(o->v.GeneratorExp.elt);
3409 if (!value) goto failed;
3410 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3411 goto failed;
3412 Py_DECREF(value);
3413 value = ast2obj_list(o->v.GeneratorExp.generators,
3414 ast2obj_comprehension);
3415 if (!value) goto failed;
3416 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3417 goto failed;
3418 Py_DECREF(value);
3419 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003420 case Await_kind:
3421 result = PyType_GenericNew(Await_type, NULL, NULL);
3422 if (!result) goto failed;
3423 value = ast2obj_expr(o->v.Await.value);
3424 if (!value) goto failed;
3425 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3426 goto failed;
3427 Py_DECREF(value);
3428 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003429 case Yield_kind:
3430 result = PyType_GenericNew(Yield_type, NULL, NULL);
3431 if (!result) goto failed;
3432 value = ast2obj_expr(o->v.Yield.value);
3433 if (!value) goto failed;
3434 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3435 goto failed;
3436 Py_DECREF(value);
3437 break;
3438 case YieldFrom_kind:
3439 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3440 if (!result) goto failed;
3441 value = ast2obj_expr(o->v.YieldFrom.value);
3442 if (!value) goto failed;
3443 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3444 goto failed;
3445 Py_DECREF(value);
3446 break;
3447 case Compare_kind:
3448 result = PyType_GenericNew(Compare_type, NULL, NULL);
3449 if (!result) goto failed;
3450 value = ast2obj_expr(o->v.Compare.left);
3451 if (!value) goto failed;
3452 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3453 goto failed;
3454 Py_DECREF(value);
3455 {
3456 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3457 value = PyList_New(n);
3458 if (!value) goto failed;
3459 for(i = 0; i < n; i++)
3460 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003462 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003463 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3464 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003465 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003466 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003467 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003468 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3469 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003470 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003471 break;
3472 case Call_kind:
3473 result = PyType_GenericNew(Call_type, NULL, NULL);
3474 if (!result) goto failed;
3475 value = ast2obj_expr(o->v.Call.func);
3476 if (!value) goto failed;
3477 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3478 goto failed;
3479 Py_DECREF(value);
3480 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3481 if (!value) goto failed;
3482 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3483 goto failed;
3484 Py_DECREF(value);
3485 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3486 if (!value) goto failed;
3487 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3488 goto failed;
3489 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003490 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003491 case FormattedValue_kind:
3492 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3493 if (!result) goto failed;
3494 value = ast2obj_expr(o->v.FormattedValue.value);
3495 if (!value) goto failed;
3496 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3497 goto failed;
3498 Py_DECREF(value);
3499 value = ast2obj_int(o->v.FormattedValue.conversion);
3500 if (!value) goto failed;
3501 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3502 goto failed;
3503 Py_DECREF(value);
3504 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3505 if (!value) goto failed;
3506 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3507 goto failed;
3508 Py_DECREF(value);
3509 break;
3510 case JoinedStr_kind:
3511 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3512 if (!result) goto failed;
3513 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3514 if (!value) goto failed;
3515 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3516 goto failed;
3517 Py_DECREF(value);
3518 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003519 case Constant_kind:
3520 result = PyType_GenericNew(Constant_type, NULL, NULL);
3521 if (!result) goto failed;
3522 value = ast2obj_constant(o->v.Constant.value);
3523 if (!value) goto failed;
3524 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3525 goto failed;
3526 Py_DECREF(value);
Guido van Rossum10f8ce62019-03-13 13:00:46 -07003527 value = ast2obj_string(o->v.Constant.kind);
3528 if (!value) goto failed;
3529 if (_PyObject_SetAttrId(result, &PyId_kind, value) == -1)
3530 goto failed;
3531 Py_DECREF(value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003532 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 case Attribute_kind:
3534 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3535 if (!result) goto failed;
3536 value = ast2obj_expr(o->v.Attribute.value);
3537 if (!value) goto failed;
3538 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3539 goto failed;
3540 Py_DECREF(value);
3541 value = ast2obj_identifier(o->v.Attribute.attr);
3542 if (!value) goto failed;
3543 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3544 goto failed;
3545 Py_DECREF(value);
3546 value = ast2obj_expr_context(o->v.Attribute.ctx);
3547 if (!value) goto failed;
3548 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3549 goto failed;
3550 Py_DECREF(value);
3551 break;
3552 case Subscript_kind:
3553 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3554 if (!result) goto failed;
3555 value = ast2obj_expr(o->v.Subscript.value);
3556 if (!value) goto failed;
3557 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3558 goto failed;
3559 Py_DECREF(value);
3560 value = ast2obj_slice(o->v.Subscript.slice);
3561 if (!value) goto failed;
3562 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3563 goto failed;
3564 Py_DECREF(value);
3565 value = ast2obj_expr_context(o->v.Subscript.ctx);
3566 if (!value) goto failed;
3567 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3568 goto failed;
3569 Py_DECREF(value);
3570 break;
3571 case Starred_kind:
3572 result = PyType_GenericNew(Starred_type, NULL, NULL);
3573 if (!result) goto failed;
3574 value = ast2obj_expr(o->v.Starred.value);
3575 if (!value) goto failed;
3576 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3577 goto failed;
3578 Py_DECREF(value);
3579 value = ast2obj_expr_context(o->v.Starred.ctx);
3580 if (!value) goto failed;
3581 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3582 goto failed;
3583 Py_DECREF(value);
3584 break;
3585 case Name_kind:
3586 result = PyType_GenericNew(Name_type, NULL, NULL);
3587 if (!result) goto failed;
3588 value = ast2obj_identifier(o->v.Name.id);
3589 if (!value) goto failed;
3590 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3591 goto failed;
3592 Py_DECREF(value);
3593 value = ast2obj_expr_context(o->v.Name.ctx);
3594 if (!value) goto failed;
3595 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3596 goto failed;
3597 Py_DECREF(value);
3598 break;
3599 case List_kind:
3600 result = PyType_GenericNew(List_type, NULL, NULL);
3601 if (!result) goto failed;
3602 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3603 if (!value) goto failed;
3604 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3605 goto failed;
3606 Py_DECREF(value);
3607 value = ast2obj_expr_context(o->v.List.ctx);
3608 if (!value) goto failed;
3609 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3610 goto failed;
3611 Py_DECREF(value);
3612 break;
3613 case Tuple_kind:
3614 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3615 if (!result) goto failed;
3616 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3617 if (!value) goto failed;
3618 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3619 goto failed;
3620 Py_DECREF(value);
3621 value = ast2obj_expr_context(o->v.Tuple.ctx);
3622 if (!value) goto failed;
3623 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3624 goto failed;
3625 Py_DECREF(value);
3626 break;
3627 }
3628 value = ast2obj_int(o->lineno);
3629 if (!value) goto failed;
3630 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3631 goto failed;
3632 Py_DECREF(value);
3633 value = ast2obj_int(o->col_offset);
3634 if (!value) goto failed;
3635 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3636 goto failed;
3637 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003638 value = ast2obj_int(o->end_lineno);
3639 if (!value) goto failed;
3640 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3641 goto failed;
3642 Py_DECREF(value);
3643 value = ast2obj_int(o->end_col_offset);
3644 if (!value) goto failed;
3645 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3646 goto failed;
3647 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003648 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003649failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003650 Py_XDECREF(value);
3651 Py_XDECREF(result);
3652 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653}
3654
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003655PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003657 switch(o) {
3658 case Load:
3659 Py_INCREF(Load_singleton);
3660 return Load_singleton;
3661 case Store:
3662 Py_INCREF(Store_singleton);
3663 return Store_singleton;
3664 case Del:
3665 Py_INCREF(Del_singleton);
3666 return Del_singleton;
3667 case AugLoad:
3668 Py_INCREF(AugLoad_singleton);
3669 return AugLoad_singleton;
3670 case AugStore:
3671 Py_INCREF(AugStore_singleton);
3672 return AugStore_singleton;
3673 case Param:
3674 Py_INCREF(Param_singleton);
3675 return Param_singleton;
3676 default:
3677 /* should never happen, but just in case ... */
3678 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3679 return NULL;
3680 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003682PyObject*
3683ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003685 slice_ty o = (slice_ty)_o;
3686 PyObject *result = NULL, *value = NULL;
3687 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003688 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003690
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003691 switch (o->kind) {
3692 case Slice_kind:
3693 result = PyType_GenericNew(Slice_type, NULL, NULL);
3694 if (!result) goto failed;
3695 value = ast2obj_expr(o->v.Slice.lower);
3696 if (!value) goto failed;
3697 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3698 goto failed;
3699 Py_DECREF(value);
3700 value = ast2obj_expr(o->v.Slice.upper);
3701 if (!value) goto failed;
3702 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3703 goto failed;
3704 Py_DECREF(value);
3705 value = ast2obj_expr(o->v.Slice.step);
3706 if (!value) goto failed;
3707 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3708 goto failed;
3709 Py_DECREF(value);
3710 break;
3711 case ExtSlice_kind:
3712 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3713 if (!result) goto failed;
3714 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3715 if (!value) goto failed;
3716 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3717 goto failed;
3718 Py_DECREF(value);
3719 break;
3720 case Index_kind:
3721 result = PyType_GenericNew(Index_type, NULL, NULL);
3722 if (!result) goto failed;
3723 value = ast2obj_expr(o->v.Index.value);
3724 if (!value) goto failed;
3725 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3726 goto failed;
3727 Py_DECREF(value);
3728 break;
3729 }
3730 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003731failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003732 Py_XDECREF(value);
3733 Py_XDECREF(result);
3734 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735}
3736
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003737PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003739 switch(o) {
3740 case And:
3741 Py_INCREF(And_singleton);
3742 return And_singleton;
3743 case Or:
3744 Py_INCREF(Or_singleton);
3745 return Or_singleton;
3746 default:
3747 /* should never happen, but just in case ... */
3748 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3749 return NULL;
3750 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003751}
3752PyObject* ast2obj_operator(operator_ty o)
3753{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003754 switch(o) {
3755 case Add:
3756 Py_INCREF(Add_singleton);
3757 return Add_singleton;
3758 case Sub:
3759 Py_INCREF(Sub_singleton);
3760 return Sub_singleton;
3761 case Mult:
3762 Py_INCREF(Mult_singleton);
3763 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003764 case MatMult:
3765 Py_INCREF(MatMult_singleton);
3766 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003767 case Div:
3768 Py_INCREF(Div_singleton);
3769 return Div_singleton;
3770 case Mod:
3771 Py_INCREF(Mod_singleton);
3772 return Mod_singleton;
3773 case Pow:
3774 Py_INCREF(Pow_singleton);
3775 return Pow_singleton;
3776 case LShift:
3777 Py_INCREF(LShift_singleton);
3778 return LShift_singleton;
3779 case RShift:
3780 Py_INCREF(RShift_singleton);
3781 return RShift_singleton;
3782 case BitOr:
3783 Py_INCREF(BitOr_singleton);
3784 return BitOr_singleton;
3785 case BitXor:
3786 Py_INCREF(BitXor_singleton);
3787 return BitXor_singleton;
3788 case BitAnd:
3789 Py_INCREF(BitAnd_singleton);
3790 return BitAnd_singleton;
3791 case FloorDiv:
3792 Py_INCREF(FloorDiv_singleton);
3793 return FloorDiv_singleton;
3794 default:
3795 /* should never happen, but just in case ... */
3796 PyErr_Format(PyExc_SystemError, "unknown operator found");
3797 return NULL;
3798 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003799}
3800PyObject* ast2obj_unaryop(unaryop_ty o)
3801{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003802 switch(o) {
3803 case Invert:
3804 Py_INCREF(Invert_singleton);
3805 return Invert_singleton;
3806 case Not:
3807 Py_INCREF(Not_singleton);
3808 return Not_singleton;
3809 case UAdd:
3810 Py_INCREF(UAdd_singleton);
3811 return UAdd_singleton;
3812 case USub:
3813 Py_INCREF(USub_singleton);
3814 return USub_singleton;
3815 default:
3816 /* should never happen, but just in case ... */
3817 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3818 return NULL;
3819 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003820}
3821PyObject* ast2obj_cmpop(cmpop_ty o)
3822{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003823 switch(o) {
3824 case Eq:
3825 Py_INCREF(Eq_singleton);
3826 return Eq_singleton;
3827 case NotEq:
3828 Py_INCREF(NotEq_singleton);
3829 return NotEq_singleton;
3830 case Lt:
3831 Py_INCREF(Lt_singleton);
3832 return Lt_singleton;
3833 case LtE:
3834 Py_INCREF(LtE_singleton);
3835 return LtE_singleton;
3836 case Gt:
3837 Py_INCREF(Gt_singleton);
3838 return Gt_singleton;
3839 case GtE:
3840 Py_INCREF(GtE_singleton);
3841 return GtE_singleton;
3842 case Is:
3843 Py_INCREF(Is_singleton);
3844 return Is_singleton;
3845 case IsNot:
3846 Py_INCREF(IsNot_singleton);
3847 return IsNot_singleton;
3848 case In:
3849 Py_INCREF(In_singleton);
3850 return In_singleton;
3851 case NotIn:
3852 Py_INCREF(NotIn_singleton);
3853 return NotIn_singleton;
3854 default:
3855 /* should never happen, but just in case ... */
3856 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3857 return NULL;
3858 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003859}
3860PyObject*
3861ast2obj_comprehension(void* _o)
3862{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003863 comprehension_ty o = (comprehension_ty)_o;
3864 PyObject *result = NULL, *value = NULL;
3865 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003866 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003867 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003868
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003869 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3870 if (!result) return NULL;
3871 value = ast2obj_expr(o->target);
3872 if (!value) goto failed;
3873 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3874 goto failed;
3875 Py_DECREF(value);
3876 value = ast2obj_expr(o->iter);
3877 if (!value) goto failed;
3878 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3879 goto failed;
3880 Py_DECREF(value);
3881 value = ast2obj_list(o->ifs, ast2obj_expr);
3882 if (!value) goto failed;
3883 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3884 goto failed;
3885 Py_DECREF(value);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07003886 value = ast2obj_int(o->is_async);
3887 if (!value) goto failed;
3888 if (_PyObject_SetAttrId(result, &PyId_is_async, value) == -1)
3889 goto failed;
3890 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003891 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003892failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003893 Py_XDECREF(value);
3894 Py_XDECREF(result);
3895 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896}
3897
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003898PyObject*
3899ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003901 excepthandler_ty o = (excepthandler_ty)_o;
3902 PyObject *result = NULL, *value = NULL;
3903 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003904 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003905 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003906
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003907 switch (o->kind) {
3908 case ExceptHandler_kind:
3909 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3910 if (!result) goto failed;
3911 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003912 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003913 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3914 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003915 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003916 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003917 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003918 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3919 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003920 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003921 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3922 if (!value) goto failed;
3923 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3924 goto failed;
3925 Py_DECREF(value);
3926 break;
3927 }
3928 value = ast2obj_int(o->lineno);
3929 if (!value) goto failed;
3930 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3931 goto failed;
3932 Py_DECREF(value);
3933 value = ast2obj_int(o->col_offset);
3934 if (!value) goto failed;
3935 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3936 goto failed;
3937 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003938 value = ast2obj_int(o->end_lineno);
3939 if (!value) goto failed;
3940 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
3941 goto failed;
3942 Py_DECREF(value);
3943 value = ast2obj_int(o->end_col_offset);
3944 if (!value) goto failed;
3945 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
3946 goto failed;
3947 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003948 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003949failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003950 Py_XDECREF(value);
3951 Py_XDECREF(result);
3952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953}
3954
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003955PyObject*
3956ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003958 arguments_ty o = (arguments_ty)_o;
3959 PyObject *result = NULL, *value = NULL;
3960 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02003961 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003962 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003963
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003964 result = PyType_GenericNew(arguments_type, NULL, NULL);
3965 if (!result) return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01003966 value = ast2obj_list(o->posonlyargs, ast2obj_arg);
3967 if (!value) goto failed;
3968 if (_PyObject_SetAttrId(result, &PyId_posonlyargs, value) == -1)
3969 goto failed;
3970 Py_DECREF(value);
Pablo Galindocd6e83b2019-07-15 01:32:18 +02003971 value = ast2obj_list(o->args, ast2obj_arg);
3972 if (!value) goto failed;
3973 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3974 goto failed;
3975 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003976 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003977 if (!value) goto failed;
3978 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3979 goto failed;
3980 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003981 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3982 if (!value) goto failed;
3983 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3984 goto failed;
3985 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003986 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003987 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003988 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003989 goto failed;
3990 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003991 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003992 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003993 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 goto failed;
3995 Py_DECREF(value);
3996 value = ast2obj_list(o->defaults, ast2obj_expr);
3997 if (!value) goto failed;
3998 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3999 goto failed;
4000 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004001 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004002failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004003 Py_XDECREF(value);
4004 Py_XDECREF(result);
4005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006}
4007
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004008PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00004009ast2obj_arg(void* _o)
4010{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 arg_ty o = (arg_ty)_o;
4012 PyObject *result = NULL, *value = NULL;
4013 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004014 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004015 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004016
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004017 result = PyType_GenericNew(arg_type, NULL, NULL);
4018 if (!result) return NULL;
4019 value = ast2obj_identifier(o->arg);
4020 if (!value) goto failed;
4021 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
4022 goto failed;
4023 Py_DECREF(value);
4024 value = ast2obj_expr(o->annotation);
4025 if (!value) goto failed;
4026 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
4027 goto failed;
4028 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004029 value = ast2obj_string(o->type_comment);
4030 if (!value) goto failed;
4031 if (_PyObject_SetAttrId(result, &PyId_type_comment, value) == -1)
4032 goto failed;
4033 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02004034 value = ast2obj_int(o->lineno);
4035 if (!value) goto failed;
4036 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
4037 goto failed;
4038 Py_DECREF(value);
4039 value = ast2obj_int(o->col_offset);
4040 if (!value) goto failed;
4041 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
4042 goto failed;
4043 Py_DECREF(value);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004044 value = ast2obj_int(o->end_lineno);
4045 if (!value) goto failed;
4046 if (_PyObject_SetAttrId(result, &PyId_end_lineno, value) < 0)
4047 goto failed;
4048 Py_DECREF(value);
4049 value = ast2obj_int(o->end_col_offset);
4050 if (!value) goto failed;
4051 if (_PyObject_SetAttrId(result, &PyId_end_col_offset, value) < 0)
4052 goto failed;
4053 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004054 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00004055failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004056 Py_XDECREF(value);
4057 Py_XDECREF(result);
4058 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00004059}
4060
4061PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004062ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004064 keyword_ty o = (keyword_ty)_o;
4065 PyObject *result = NULL, *value = NULL;
4066 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004067 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004068 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004069
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004070 result = PyType_GenericNew(keyword_type, NULL, NULL);
4071 if (!result) return NULL;
4072 value = ast2obj_identifier(o->arg);
4073 if (!value) goto failed;
4074 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
4075 goto failed;
4076 Py_DECREF(value);
4077 value = ast2obj_expr(o->value);
4078 if (!value) goto failed;
4079 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
4080 goto failed;
4081 Py_DECREF(value);
4082 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004083failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004084 Py_XDECREF(value);
4085 Py_XDECREF(result);
4086 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087}
4088
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004089PyObject*
4090ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004092 alias_ty o = (alias_ty)_o;
4093 PyObject *result = NULL, *value = NULL;
4094 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004095 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004096 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004097
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004098 result = PyType_GenericNew(alias_type, NULL, NULL);
4099 if (!result) return NULL;
4100 value = ast2obj_identifier(o->name);
4101 if (!value) goto failed;
4102 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
4103 goto failed;
4104 Py_DECREF(value);
4105 value = ast2obj_identifier(o->asname);
4106 if (!value) goto failed;
4107 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
4108 goto failed;
4109 Py_DECREF(value);
4110 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00004111failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004112 Py_XDECREF(value);
4113 Py_XDECREF(result);
4114 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115}
4116
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004117PyObject*
4118ast2obj_withitem(void* _o)
4119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004120 withitem_ty o = (withitem_ty)_o;
4121 PyObject *result = NULL, *value = NULL;
4122 if (!o) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02004123 Py_RETURN_NONE;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004124 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004125
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004126 result = PyType_GenericNew(withitem_type, NULL, NULL);
4127 if (!result) return NULL;
4128 value = ast2obj_expr(o->context_expr);
4129 if (!value) goto failed;
4130 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
4131 goto failed;
4132 Py_DECREF(value);
4133 value = ast2obj_expr(o->optional_vars);
4134 if (!value) goto failed;
4135 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
4136 goto failed;
4137 Py_DECREF(value);
4138 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004139failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004140 Py_XDECREF(value);
4141 Py_XDECREF(result);
4142 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004143}
4144
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004145PyObject*
4146ast2obj_type_ignore(void* _o)
4147{
4148 type_ignore_ty o = (type_ignore_ty)_o;
4149 PyObject *result = NULL, *value = NULL;
4150 if (!o) {
4151 Py_RETURN_NONE;
4152 }
4153
4154 switch (o->kind) {
4155 case TypeIgnore_kind:
4156 result = PyType_GenericNew(TypeIgnore_type, NULL, NULL);
4157 if (!result) goto failed;
4158 value = ast2obj_int(o->v.TypeIgnore.lineno);
4159 if (!value) goto failed;
4160 if (_PyObject_SetAttrId(result, &PyId_lineno, value) == -1)
4161 goto failed;
4162 Py_DECREF(value);
Michael J. Sullivan933e1502019-05-22 07:54:20 -07004163 value = ast2obj_string(o->v.TypeIgnore.tag);
4164 if (!value) goto failed;
4165 if (_PyObject_SetAttrId(result, &PyId_tag, value) == -1)
4166 goto failed;
4167 Py_DECREF(value);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004168 break;
4169 }
4170 return result;
4171failed:
4172 Py_XDECREF(value);
4173 Py_XDECREF(result);
4174 return NULL;
4175}
4176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004177
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004178int
4179obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
4180{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004181 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004182
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004183 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004184
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004185 if (obj == Py_None) {
4186 *out = NULL;
4187 return 0;
4188 }
4189 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
4190 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004191 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004192 }
4193 if (isinstance) {
4194 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004195 asdl_seq* type_ignores;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004196
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004197 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4198 return 1;
4199 }
4200 if (tmp == NULL) {
4201 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
4202 return 1;
4203 }
4204 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004205 int res;
4206 Py_ssize_t len;
4207 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004208 if (!PyList_Check(tmp)) {
4209 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4210 goto failed;
4211 }
4212 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004213 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004214 if (body == NULL) goto failed;
4215 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004216 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004217 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4218 Py_INCREF(tmp2);
4219 res = obj2ast_stmt(tmp2, &val, arena);
4220 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004221 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004222 if (len != PyList_GET_SIZE(tmp)) {
4223 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
4224 goto failed;
4225 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004226 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004227 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004228 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004229 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004230 if (_PyObject_LookupAttrId(obj, &PyId_type_ignores, &tmp) < 0) {
4231 return 1;
4232 }
4233 if (tmp == NULL) {
4234 PyErr_SetString(PyExc_TypeError, "required field \"type_ignores\" missing from Module");
4235 return 1;
4236 }
4237 else {
4238 int res;
4239 Py_ssize_t len;
4240 Py_ssize_t i;
4241 if (!PyList_Check(tmp)) {
4242 PyErr_Format(PyExc_TypeError, "Module field \"type_ignores\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4243 goto failed;
4244 }
4245 len = PyList_GET_SIZE(tmp);
4246 type_ignores = _Py_asdl_seq_new(len, arena);
4247 if (type_ignores == NULL) goto failed;
4248 for (i = 0; i < len; i++) {
4249 type_ignore_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004250 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4251 Py_INCREF(tmp2);
4252 res = obj2ast_type_ignore(tmp2, &val, arena);
4253 Py_DECREF(tmp2);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004254 if (res != 0) goto failed;
4255 if (len != PyList_GET_SIZE(tmp)) {
4256 PyErr_SetString(PyExc_RuntimeError, "Module field \"type_ignores\" changed size during iteration");
4257 goto failed;
4258 }
4259 asdl_seq_SET(type_ignores, i, val);
4260 }
4261 Py_CLEAR(tmp);
4262 }
4263 *out = Module(body, type_ignores, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004264 if (*out == NULL) goto failed;
4265 return 0;
4266 }
4267 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
4268 if (isinstance == -1) {
4269 return 1;
4270 }
4271 if (isinstance) {
4272 asdl_seq* body;
4273
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004274 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4275 return 1;
4276 }
4277 if (tmp == NULL) {
4278 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
4279 return 1;
4280 }
4281 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004282 int res;
4283 Py_ssize_t len;
4284 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004285 if (!PyList_Check(tmp)) {
4286 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4287 goto failed;
4288 }
4289 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004290 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004291 if (body == NULL) goto failed;
4292 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004293 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004294 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4295 Py_INCREF(tmp2);
4296 res = obj2ast_stmt(tmp2, &val, arena);
4297 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004298 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004299 if (len != PyList_GET_SIZE(tmp)) {
4300 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
4301 goto failed;
4302 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004303 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004304 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004305 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004306 }
4307 *out = Interactive(body, arena);
4308 if (*out == NULL) goto failed;
4309 return 0;
4310 }
4311 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
4312 if (isinstance == -1) {
4313 return 1;
4314 }
4315 if (isinstance) {
4316 expr_ty body;
4317
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004318 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4319 return 1;
4320 }
4321 if (tmp == NULL) {
4322 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
4323 return 1;
4324 }
4325 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004326 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004327 res = obj2ast_expr(tmp, &body, arena);
4328 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004329 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004330 }
4331 *out = Expression(body, arena);
4332 if (*out == NULL) goto failed;
4333 return 0;
4334 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004335 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionType_type);
4336 if (isinstance == -1) {
4337 return 1;
4338 }
4339 if (isinstance) {
4340 asdl_seq* argtypes;
4341 expr_ty returns;
4342
4343 if (_PyObject_LookupAttrId(obj, &PyId_argtypes, &tmp) < 0) {
4344 return 1;
4345 }
4346 if (tmp == NULL) {
4347 PyErr_SetString(PyExc_TypeError, "required field \"argtypes\" missing from FunctionType");
4348 return 1;
4349 }
4350 else {
4351 int res;
4352 Py_ssize_t len;
4353 Py_ssize_t i;
4354 if (!PyList_Check(tmp)) {
4355 PyErr_Format(PyExc_TypeError, "FunctionType field \"argtypes\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4356 goto failed;
4357 }
4358 len = PyList_GET_SIZE(tmp);
4359 argtypes = _Py_asdl_seq_new(len, arena);
4360 if (argtypes == NULL) goto failed;
4361 for (i = 0; i < len; i++) {
4362 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004363 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4364 Py_INCREF(tmp2);
4365 res = obj2ast_expr(tmp2, &val, arena);
4366 Py_DECREF(tmp2);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004367 if (res != 0) goto failed;
4368 if (len != PyList_GET_SIZE(tmp)) {
4369 PyErr_SetString(PyExc_RuntimeError, "FunctionType field \"argtypes\" changed size during iteration");
4370 goto failed;
4371 }
4372 asdl_seq_SET(argtypes, i, val);
4373 }
4374 Py_CLEAR(tmp);
4375 }
4376 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
4377 return 1;
4378 }
4379 if (tmp == NULL) {
4380 PyErr_SetString(PyExc_TypeError, "required field \"returns\" missing from FunctionType");
4381 return 1;
4382 }
4383 else {
4384 int res;
4385 res = obj2ast_expr(tmp, &returns, arena);
4386 if (res != 0) goto failed;
4387 Py_CLEAR(tmp);
4388 }
4389 *out = FunctionType(argtypes, returns, arena);
4390 if (*out == NULL) goto failed;
4391 return 0;
4392 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004393 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
4394 if (isinstance == -1) {
4395 return 1;
4396 }
4397 if (isinstance) {
4398 asdl_seq* body;
4399
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004400 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
4401 return 1;
4402 }
4403 if (tmp == NULL) {
4404 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4405 return 1;
4406 }
4407 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004408 int res;
4409 Py_ssize_t len;
4410 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004411 if (!PyList_Check(tmp)) {
4412 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4413 goto failed;
4414 }
4415 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004416 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004417 if (body == NULL) goto failed;
4418 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004419 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004420 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4421 Py_INCREF(tmp2);
4422 res = obj2ast_stmt(tmp2, &val, arena);
4423 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004424 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004425 if (len != PyList_GET_SIZE(tmp)) {
4426 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
4427 goto failed;
4428 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004429 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004430 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004431 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004432 }
4433 *out = Suite(body, arena);
4434 if (*out == NULL) goto failed;
4435 return 0;
4436 }
4437
4438 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4439 failed:
4440 Py_XDECREF(tmp);
4441 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004442}
4443
4444int
4445obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4446{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004447 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004448
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004449 PyObject *tmp = NULL;
4450 int lineno;
4451 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004452 int end_lineno;
4453 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004454
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004455 if (obj == Py_None) {
4456 *out = NULL;
4457 return 0;
4458 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004459 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
4460 return 1;
4461 }
4462 if (tmp == NULL) {
4463 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
4464 return 1;
4465 }
4466 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004467 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004468 res = obj2ast_int(tmp, &lineno, arena);
4469 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004470 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004471 }
4472 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004473 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004474 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004475 if (tmp == NULL) {
4476 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4477 return 1;
4478 }
4479 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004480 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004481 res = obj2ast_int(tmp, &col_offset, arena);
4482 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004483 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004484 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004485 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
4486 return 1;
4487 }
4488 if (tmp == NULL || tmp == Py_None) {
4489 Py_CLEAR(tmp);
4490 end_lineno = 0;
4491 }
4492 else {
4493 int res;
4494 res = obj2ast_int(tmp, &end_lineno, arena);
4495 if (res != 0) goto failed;
4496 Py_CLEAR(tmp);
4497 }
4498 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
4499 return 1;
4500 }
4501 if (tmp == NULL || tmp == Py_None) {
4502 Py_CLEAR(tmp);
4503 end_col_offset = 0;
4504 }
4505 else {
4506 int res;
4507 res = obj2ast_int(tmp, &end_col_offset, arena);
4508 if (res != 0) goto failed;
4509 Py_CLEAR(tmp);
4510 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004511 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4512 if (isinstance == -1) {
4513 return 1;
4514 }
4515 if (isinstance) {
4516 identifier name;
4517 arguments_ty args;
4518 asdl_seq* body;
4519 asdl_seq* decorator_list;
4520 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004521 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004522
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004523 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4524 return 1;
4525 }
4526 if (tmp == NULL) {
4527 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4528 return 1;
4529 }
4530 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004531 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004532 res = obj2ast_identifier(tmp, &name, arena);
4533 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004534 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004535 }
4536 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004537 return 1;
4538 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004539 if (tmp == NULL) {
4540 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4541 return 1;
4542 }
4543 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004544 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004545 res = obj2ast_arguments(tmp, &args, arena);
4546 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004547 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004548 }
4549 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004550 return 1;
4551 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004552 if (tmp == NULL) {
4553 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4554 return 1;
4555 }
4556 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004557 int res;
4558 Py_ssize_t len;
4559 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004560 if (!PyList_Check(tmp)) {
4561 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4562 goto failed;
4563 }
4564 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004565 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004566 if (body == NULL) goto failed;
4567 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004568 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004569 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4570 Py_INCREF(tmp2);
4571 res = obj2ast_stmt(tmp2, &val, arena);
4572 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004573 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004574 if (len != PyList_GET_SIZE(tmp)) {
4575 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4576 goto failed;
4577 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004578 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004579 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004580 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004581 }
4582 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004583 return 1;
4584 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004585 if (tmp == NULL) {
4586 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4587 return 1;
4588 }
4589 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004590 int res;
4591 Py_ssize_t len;
4592 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004593 if (!PyList_Check(tmp)) {
4594 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4595 goto failed;
4596 }
4597 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004598 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004599 if (decorator_list == NULL) goto failed;
4600 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004601 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004602 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4603 Py_INCREF(tmp2);
4604 res = obj2ast_expr(tmp2, &val, arena);
4605 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004606 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004607 if (len != PyList_GET_SIZE(tmp)) {
4608 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4609 goto failed;
4610 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004611 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004612 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004613 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004614 }
4615 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004616 return 1;
4617 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004618 if (tmp == NULL || tmp == Py_None) {
4619 Py_CLEAR(tmp);
4620 returns = NULL;
4621 }
4622 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004623 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004624 res = obj2ast_expr(tmp, &returns, arena);
4625 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004626 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004627 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004628 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
4629 return 1;
4630 }
4631 if (tmp == NULL || tmp == Py_None) {
4632 Py_CLEAR(tmp);
4633 type_comment = NULL;
4634 }
4635 else {
4636 int res;
4637 res = obj2ast_string(tmp, &type_comment, arena);
4638 if (res != 0) goto failed;
4639 Py_CLEAR(tmp);
4640 }
4641 *out = FunctionDef(name, args, body, decorator_list, returns,
4642 type_comment, lineno, col_offset, end_lineno,
4643 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004644 if (*out == NULL) goto failed;
4645 return 0;
4646 }
Yury Selivanov75445082015-05-11 22:57:16 -04004647 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4648 if (isinstance == -1) {
4649 return 1;
4650 }
4651 if (isinstance) {
4652 identifier name;
4653 arguments_ty args;
4654 asdl_seq* body;
4655 asdl_seq* decorator_list;
4656 expr_ty returns;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004657 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04004658
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004659 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4660 return 1;
4661 }
4662 if (tmp == NULL) {
4663 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4664 return 1;
4665 }
4666 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004667 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004668 res = obj2ast_identifier(tmp, &name, arena);
4669 if (res != 0) goto failed;
4670 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004671 }
4672 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004673 return 1;
4674 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004675 if (tmp == NULL) {
4676 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4677 return 1;
4678 }
4679 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004680 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004681 res = obj2ast_arguments(tmp, &args, arena);
4682 if (res != 0) goto failed;
4683 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004684 }
4685 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004686 return 1;
4687 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004688 if (tmp == NULL) {
4689 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4690 return 1;
4691 }
4692 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004693 int res;
4694 Py_ssize_t len;
4695 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04004696 if (!PyList_Check(tmp)) {
4697 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4698 goto failed;
4699 }
4700 len = PyList_GET_SIZE(tmp);
4701 body = _Py_asdl_seq_new(len, arena);
4702 if (body == NULL) goto failed;
4703 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004704 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004705 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4706 Py_INCREF(tmp2);
4707 res = obj2ast_stmt(tmp2, &val, arena);
4708 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04004709 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004710 if (len != PyList_GET_SIZE(tmp)) {
4711 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4712 goto failed;
4713 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004714 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004715 }
4716 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004717 }
4718 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004719 return 1;
4720 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004721 if (tmp == NULL) {
4722 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4723 return 1;
4724 }
4725 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004726 int res;
4727 Py_ssize_t len;
4728 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04004729 if (!PyList_Check(tmp)) {
4730 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4731 goto failed;
4732 }
4733 len = PyList_GET_SIZE(tmp);
4734 decorator_list = _Py_asdl_seq_new(len, arena);
4735 if (decorator_list == NULL) goto failed;
4736 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004737 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004738 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4739 Py_INCREF(tmp2);
4740 res = obj2ast_expr(tmp2, &val, arena);
4741 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04004742 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004743 if (len != PyList_GET_SIZE(tmp)) {
4744 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4745 goto failed;
4746 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004747 asdl_seq_SET(decorator_list, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04004748 }
4749 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004750 }
4751 if (_PyObject_LookupAttrId(obj, &PyId_returns, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04004752 return 1;
4753 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004754 if (tmp == NULL || tmp == Py_None) {
4755 Py_CLEAR(tmp);
4756 returns = NULL;
4757 }
4758 else {
Yury Selivanov75445082015-05-11 22:57:16 -04004759 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04004760 res = obj2ast_expr(tmp, &returns, arena);
4761 if (res != 0) goto failed;
4762 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04004763 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004764 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
4765 return 1;
4766 }
4767 if (tmp == NULL || tmp == Py_None) {
4768 Py_CLEAR(tmp);
4769 type_comment = NULL;
4770 }
4771 else {
4772 int res;
4773 res = obj2ast_string(tmp, &type_comment, arena);
4774 if (res != 0) goto failed;
4775 Py_CLEAR(tmp);
4776 }
Yury Selivanov75445082015-05-11 22:57:16 -04004777 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004778 type_comment, lineno, col_offset, end_lineno,
4779 end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004780 if (*out == NULL) goto failed;
4781 return 0;
4782 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004783 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4784 if (isinstance == -1) {
4785 return 1;
4786 }
4787 if (isinstance) {
4788 identifier name;
4789 asdl_seq* bases;
4790 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004791 asdl_seq* body;
4792 asdl_seq* decorator_list;
4793
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004794 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
4795 return 1;
4796 }
4797 if (tmp == NULL) {
4798 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4799 return 1;
4800 }
4801 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004802 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004803 res = obj2ast_identifier(tmp, &name, arena);
4804 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004805 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004806 }
4807 if (_PyObject_LookupAttrId(obj, &PyId_bases, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004808 return 1;
4809 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004810 if (tmp == NULL) {
4811 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4812 return 1;
4813 }
4814 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004815 int res;
4816 Py_ssize_t len;
4817 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004818 if (!PyList_Check(tmp)) {
4819 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4820 goto failed;
4821 }
4822 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004823 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004824 if (bases == NULL) goto failed;
4825 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004826 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004827 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4828 Py_INCREF(tmp2);
4829 res = obj2ast_expr(tmp2, &val, arena);
4830 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004831 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004832 if (len != PyList_GET_SIZE(tmp)) {
4833 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4834 goto failed;
4835 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004836 asdl_seq_SET(bases, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004837 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004838 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004839 }
4840 if (_PyObject_LookupAttrId(obj, &PyId_keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004841 return 1;
4842 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004843 if (tmp == NULL) {
4844 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4845 return 1;
4846 }
4847 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004848 int res;
4849 Py_ssize_t len;
4850 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004851 if (!PyList_Check(tmp)) {
4852 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4853 goto failed;
4854 }
4855 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004856 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004857 if (keywords == NULL) goto failed;
4858 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004859 keyword_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004860 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4861 Py_INCREF(tmp2);
4862 res = obj2ast_keyword(tmp2, &val, arena);
4863 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004864 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004865 if (len != PyList_GET_SIZE(tmp)) {
4866 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4867 goto failed;
4868 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004869 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004870 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004871 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004872 }
4873 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004874 return 1;
4875 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004876 if (tmp == NULL) {
4877 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4878 return 1;
4879 }
4880 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004881 int res;
4882 Py_ssize_t len;
4883 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004884 if (!PyList_Check(tmp)) {
4885 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4886 goto failed;
4887 }
4888 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004889 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004890 if (body == NULL) goto failed;
4891 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004892 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004893 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4894 Py_INCREF(tmp2);
4895 res = obj2ast_stmt(tmp2, &val, arena);
4896 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004897 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004898 if (len != PyList_GET_SIZE(tmp)) {
4899 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4900 goto failed;
4901 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004902 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004903 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004904 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004905 }
4906 if (_PyObject_LookupAttrId(obj, &PyId_decorator_list, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004907 return 1;
4908 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004909 if (tmp == NULL) {
4910 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4911 return 1;
4912 }
4913 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004914 int res;
4915 Py_ssize_t len;
4916 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004917 if (!PyList_Check(tmp)) {
4918 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4919 goto failed;
4920 }
4921 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004922 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004923 if (decorator_list == NULL) goto failed;
4924 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004925 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004926 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4927 Py_INCREF(tmp2);
4928 res = obj2ast_expr(tmp2, &val, arena);
4929 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004930 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004931 if (len != PyList_GET_SIZE(tmp)) {
4932 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4933 goto failed;
4934 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004935 asdl_seq_SET(decorator_list, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004936 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004937 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004938 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004939 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004940 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004941 if (*out == NULL) goto failed;
4942 return 0;
4943 }
4944 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4945 if (isinstance == -1) {
4946 return 1;
4947 }
4948 if (isinstance) {
4949 expr_ty value;
4950
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004951 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
4952 return 1;
4953 }
4954 if (tmp == NULL || tmp == Py_None) {
4955 Py_CLEAR(tmp);
4956 value = NULL;
4957 }
4958 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004959 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004960 res = obj2ast_expr(tmp, &value, arena);
4961 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004962 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004963 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004964 *out = Return(value, lineno, col_offset, end_lineno, end_col_offset,
4965 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004966 if (*out == NULL) goto failed;
4967 return 0;
4968 }
4969 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4970 if (isinstance == -1) {
4971 return 1;
4972 }
4973 if (isinstance) {
4974 asdl_seq* targets;
4975
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004976 if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
4977 return 1;
4978 }
4979 if (tmp == NULL) {
4980 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4981 return 1;
4982 }
4983 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004984 int res;
4985 Py_ssize_t len;
4986 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004987 if (!PyList_Check(tmp)) {
4988 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4989 goto failed;
4990 }
4991 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004992 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004993 if (targets == NULL) goto failed;
4994 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07004995 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03004996 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
4997 Py_INCREF(tmp2);
4998 res = obj2ast_expr(tmp2, &val, arena);
4999 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005000 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005001 if (len != PyList_GET_SIZE(tmp)) {
5002 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
5003 goto failed;
5004 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005005 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005006 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005007 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005008 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005009 *out = Delete(targets, lineno, col_offset, end_lineno, end_col_offset,
5010 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005011 if (*out == NULL) goto failed;
5012 return 0;
5013 }
5014 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
5015 if (isinstance == -1) {
5016 return 1;
5017 }
5018 if (isinstance) {
5019 asdl_seq* targets;
5020 expr_ty value;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005021 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005022
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005023 if (_PyObject_LookupAttrId(obj, &PyId_targets, &tmp) < 0) {
5024 return 1;
5025 }
5026 if (tmp == NULL) {
5027 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
5028 return 1;
5029 }
5030 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005031 int res;
5032 Py_ssize_t len;
5033 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005034 if (!PyList_Check(tmp)) {
5035 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5036 goto failed;
5037 }
5038 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005039 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005040 if (targets == NULL) goto failed;
5041 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005042 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005043 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5044 Py_INCREF(tmp2);
5045 res = obj2ast_expr(tmp2, &val, arena);
5046 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005047 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005048 if (len != PyList_GET_SIZE(tmp)) {
5049 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
5050 goto failed;
5051 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005052 asdl_seq_SET(targets, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005053 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005054 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005055 }
5056 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005057 return 1;
5058 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005059 if (tmp == NULL) {
5060 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
5061 return 1;
5062 }
5063 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005064 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005065 res = obj2ast_expr(tmp, &value, arena);
5066 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005067 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005068 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005069 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5070 return 1;
5071 }
5072 if (tmp == NULL || tmp == Py_None) {
5073 Py_CLEAR(tmp);
5074 type_comment = NULL;
5075 }
5076 else {
5077 int res;
5078 res = obj2ast_string(tmp, &type_comment, arena);
5079 if (res != 0) goto failed;
5080 Py_CLEAR(tmp);
5081 }
5082 *out = Assign(targets, value, type_comment, lineno, col_offset,
5083 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005084 if (*out == NULL) goto failed;
5085 return 0;
5086 }
5087 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
5088 if (isinstance == -1) {
5089 return 1;
5090 }
5091 if (isinstance) {
5092 expr_ty target;
5093 operator_ty op;
5094 expr_ty value;
5095
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005096 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5097 return 1;
5098 }
5099 if (tmp == NULL) {
5100 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
5101 return 1;
5102 }
5103 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005104 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005105 res = obj2ast_expr(tmp, &target, arena);
5106 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005107 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005108 }
5109 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005110 return 1;
5111 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005112 if (tmp == NULL) {
5113 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
5114 return 1;
5115 }
5116 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005117 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005118 res = obj2ast_operator(tmp, &op, arena);
5119 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005120 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005121 }
5122 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005123 return 1;
5124 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005125 if (tmp == NULL) {
5126 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
5127 return 1;
5128 }
5129 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005130 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005131 res = obj2ast_expr(tmp, &value, arena);
5132 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005133 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005134 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005135 *out = AugAssign(target, op, value, lineno, col_offset, end_lineno,
5136 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005137 if (*out == NULL) goto failed;
5138 return 0;
5139 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005140 isinstance = PyObject_IsInstance(obj, (PyObject*)AnnAssign_type);
5141 if (isinstance == -1) {
5142 return 1;
5143 }
5144 if (isinstance) {
5145 expr_ty target;
5146 expr_ty annotation;
5147 expr_ty value;
5148 int simple;
5149
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 AnnAssign");
5155 return 1;
5156 }
5157 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005158 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005159 res = obj2ast_expr(tmp, &target, arena);
5160 if (res != 0) goto failed;
5161 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005162 }
5163 if (_PyObject_LookupAttrId(obj, &PyId_annotation, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005164 return 1;
5165 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005166 if (tmp == NULL) {
5167 PyErr_SetString(PyExc_TypeError, "required field \"annotation\" missing from AnnAssign");
5168 return 1;
5169 }
5170 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005171 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005172 res = obj2ast_expr(tmp, &annotation, arena);
5173 if (res != 0) goto failed;
5174 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005175 }
5176 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005177 return 1;
5178 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005179 if (tmp == NULL || tmp == Py_None) {
5180 Py_CLEAR(tmp);
5181 value = NULL;
5182 }
5183 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005184 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005185 res = obj2ast_expr(tmp, &value, arena);
5186 if (res != 0) goto failed;
5187 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005188 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005189 if (_PyObject_LookupAttrId(obj, &PyId_simple, &tmp) < 0) {
5190 return 1;
5191 }
5192 if (tmp == NULL) {
5193 PyErr_SetString(PyExc_TypeError, "required field \"simple\" missing from AnnAssign");
5194 return 1;
5195 }
5196 else {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005197 int res;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005198 res = obj2ast_int(tmp, &simple, arena);
5199 if (res != 0) goto failed;
5200 Py_CLEAR(tmp);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005201 }
5202 *out = AnnAssign(target, annotation, value, simple, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005203 end_lineno, end_col_offset, arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005204 if (*out == NULL) goto failed;
5205 return 0;
5206 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005207 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
5208 if (isinstance == -1) {
5209 return 1;
5210 }
5211 if (isinstance) {
5212 expr_ty target;
5213 expr_ty iter;
5214 asdl_seq* body;
5215 asdl_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005216 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005217
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005218 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5219 return 1;
5220 }
5221 if (tmp == NULL) {
5222 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
5223 return 1;
5224 }
5225 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005226 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005227 res = obj2ast_expr(tmp, &target, arena);
5228 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005229 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005230 }
5231 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005232 return 1;
5233 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005234 if (tmp == NULL) {
5235 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
5236 return 1;
5237 }
5238 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005239 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005240 res = obj2ast_expr(tmp, &iter, arena);
5241 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005242 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005243 }
5244 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005245 return 1;
5246 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005247 if (tmp == NULL) {
5248 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
5249 return 1;
5250 }
5251 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005252 int res;
5253 Py_ssize_t len;
5254 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005255 if (!PyList_Check(tmp)) {
5256 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5257 goto failed;
5258 }
5259 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005260 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005261 if (body == NULL) goto failed;
5262 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005263 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005264 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5265 Py_INCREF(tmp2);
5266 res = obj2ast_stmt(tmp2, &val, arena);
5267 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005268 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005269 if (len != PyList_GET_SIZE(tmp)) {
5270 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
5271 goto failed;
5272 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005273 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005274 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005275 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005276 }
5277 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005278 return 1;
5279 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005280 if (tmp == NULL) {
5281 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
5282 return 1;
5283 }
5284 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005285 int res;
5286 Py_ssize_t len;
5287 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005288 if (!PyList_Check(tmp)) {
5289 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5290 goto failed;
5291 }
5292 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005293 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005294 if (orelse == NULL) goto failed;
5295 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005296 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005297 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5298 Py_INCREF(tmp2);
5299 res = obj2ast_stmt(tmp2, &val, arena);
5300 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005301 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005302 if (len != PyList_GET_SIZE(tmp)) {
5303 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
5304 goto failed;
5305 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005306 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005307 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005308 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005309 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005310 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5311 return 1;
5312 }
5313 if (tmp == NULL || tmp == Py_None) {
5314 Py_CLEAR(tmp);
5315 type_comment = NULL;
5316 }
5317 else {
5318 int res;
5319 res = obj2ast_string(tmp, &type_comment, arena);
5320 if (res != 0) goto failed;
5321 Py_CLEAR(tmp);
5322 }
5323 *out = For(target, iter, body, orelse, type_comment, lineno,
5324 col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005325 if (*out == NULL) goto failed;
5326 return 0;
5327 }
Yury Selivanov75445082015-05-11 22:57:16 -04005328 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
5329 if (isinstance == -1) {
5330 return 1;
5331 }
5332 if (isinstance) {
5333 expr_ty target;
5334 expr_ty iter;
5335 asdl_seq* body;
5336 asdl_seq* orelse;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005337 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04005338
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005339 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
5340 return 1;
5341 }
5342 if (tmp == NULL) {
5343 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
5344 return 1;
5345 }
5346 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005347 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04005348 res = obj2ast_expr(tmp, &target, arena);
5349 if (res != 0) goto failed;
5350 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005351 }
5352 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005353 return 1;
5354 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005355 if (tmp == NULL) {
5356 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
5357 return 1;
5358 }
5359 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005360 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04005361 res = obj2ast_expr(tmp, &iter, arena);
5362 if (res != 0) goto failed;
5363 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005364 }
5365 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005366 return 1;
5367 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005368 if (tmp == NULL) {
5369 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
5370 return 1;
5371 }
5372 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005373 int res;
5374 Py_ssize_t len;
5375 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005376 if (!PyList_Check(tmp)) {
5377 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5378 goto failed;
5379 }
5380 len = PyList_GET_SIZE(tmp);
5381 body = _Py_asdl_seq_new(len, arena);
5382 if (body == NULL) goto failed;
5383 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005384 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005385 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5386 Py_INCREF(tmp2);
5387 res = obj2ast_stmt(tmp2, &val, arena);
5388 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005389 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005390 if (len != PyList_GET_SIZE(tmp)) {
5391 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
5392 goto failed;
5393 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005394 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005395 }
5396 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005397 }
5398 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005399 return 1;
5400 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005401 if (tmp == NULL) {
5402 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
5403 return 1;
5404 }
5405 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005406 int res;
5407 Py_ssize_t len;
5408 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005409 if (!PyList_Check(tmp)) {
5410 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5411 goto failed;
5412 }
5413 len = PyList_GET_SIZE(tmp);
5414 orelse = _Py_asdl_seq_new(len, arena);
5415 if (orelse == NULL) goto failed;
5416 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005417 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005418 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5419 Py_INCREF(tmp2);
5420 res = obj2ast_stmt(tmp2, &val, arena);
5421 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005422 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005423 if (len != PyList_GET_SIZE(tmp)) {
5424 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
5425 goto failed;
5426 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005427 asdl_seq_SET(orelse, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005428 }
5429 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04005430 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005431 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5432 return 1;
5433 }
5434 if (tmp == NULL || tmp == Py_None) {
5435 Py_CLEAR(tmp);
5436 type_comment = NULL;
5437 }
5438 else {
5439 int res;
5440 res = obj2ast_string(tmp, &type_comment, arena);
5441 if (res != 0) goto failed;
5442 Py_CLEAR(tmp);
5443 }
5444 *out = AsyncFor(target, iter, body, orelse, type_comment, lineno,
5445 col_offset, end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005446 if (*out == NULL) goto failed;
5447 return 0;
5448 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005449 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
5450 if (isinstance == -1) {
5451 return 1;
5452 }
5453 if (isinstance) {
5454 expr_ty test;
5455 asdl_seq* body;
5456 asdl_seq* orelse;
5457
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005458 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
5459 return 1;
5460 }
5461 if (tmp == NULL) {
5462 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
5463 return 1;
5464 }
5465 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005466 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005467 res = obj2ast_expr(tmp, &test, arena);
5468 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005469 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005470 }
5471 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005472 return 1;
5473 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005474 if (tmp == NULL) {
5475 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
5476 return 1;
5477 }
5478 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005479 int res;
5480 Py_ssize_t len;
5481 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005482 if (!PyList_Check(tmp)) {
5483 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5484 goto failed;
5485 }
5486 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005487 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005488 if (body == NULL) goto failed;
5489 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005490 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005491 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5492 Py_INCREF(tmp2);
5493 res = obj2ast_stmt(tmp2, &val, arena);
5494 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005495 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005496 if (len != PyList_GET_SIZE(tmp)) {
5497 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
5498 goto failed;
5499 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005500 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005501 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005502 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005503 }
5504 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005505 return 1;
5506 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005507 if (tmp == NULL) {
5508 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
5509 return 1;
5510 }
5511 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005512 int res;
5513 Py_ssize_t len;
5514 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005515 if (!PyList_Check(tmp)) {
5516 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5517 goto failed;
5518 }
5519 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005520 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005521 if (orelse == NULL) goto failed;
5522 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005523 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005524 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5525 Py_INCREF(tmp2);
5526 res = obj2ast_stmt(tmp2, &val, arena);
5527 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005528 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005529 if (len != PyList_GET_SIZE(tmp)) {
5530 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
5531 goto failed;
5532 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005533 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005534 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005535 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005536 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005537 *out = While(test, body, orelse, lineno, col_offset, end_lineno,
5538 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005539 if (*out == NULL) goto failed;
5540 return 0;
5541 }
5542 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
5543 if (isinstance == -1) {
5544 return 1;
5545 }
5546 if (isinstance) {
5547 expr_ty test;
5548 asdl_seq* body;
5549 asdl_seq* orelse;
5550
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005551 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
5552 return 1;
5553 }
5554 if (tmp == NULL) {
5555 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
5556 return 1;
5557 }
5558 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005559 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005560 res = obj2ast_expr(tmp, &test, arena);
5561 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005562 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005563 }
5564 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005565 return 1;
5566 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005567 if (tmp == NULL) {
5568 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
5569 return 1;
5570 }
5571 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005572 int res;
5573 Py_ssize_t len;
5574 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005575 if (!PyList_Check(tmp)) {
5576 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5577 goto failed;
5578 }
5579 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005580 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005581 if (body == NULL) goto failed;
5582 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005583 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005584 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5585 Py_INCREF(tmp2);
5586 res = obj2ast_stmt(tmp2, &val, arena);
5587 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005588 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005589 if (len != PyList_GET_SIZE(tmp)) {
5590 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
5591 goto failed;
5592 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005593 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005594 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005595 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005596 }
5597 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005598 return 1;
5599 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005600 if (tmp == NULL) {
5601 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
5602 return 1;
5603 }
5604 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005605 int res;
5606 Py_ssize_t len;
5607 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005608 if (!PyList_Check(tmp)) {
5609 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5610 goto failed;
5611 }
5612 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005613 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005614 if (orelse == NULL) goto failed;
5615 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005616 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005617 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5618 Py_INCREF(tmp2);
5619 res = obj2ast_stmt(tmp2, &val, arena);
5620 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005621 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005622 if (len != PyList_GET_SIZE(tmp)) {
5623 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
5624 goto failed;
5625 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005626 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005627 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005628 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005629 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005630 *out = If(test, body, orelse, lineno, col_offset, end_lineno,
5631 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005632 if (*out == NULL) goto failed;
5633 return 0;
5634 }
5635 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
5636 if (isinstance == -1) {
5637 return 1;
5638 }
5639 if (isinstance) {
5640 asdl_seq* items;
5641 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005642 string type_comment;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005643
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005644 if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
5645 return 1;
5646 }
5647 if (tmp == NULL) {
5648 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
5649 return 1;
5650 }
5651 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005652 int res;
5653 Py_ssize_t len;
5654 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005655 if (!PyList_Check(tmp)) {
5656 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5657 goto failed;
5658 }
5659 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005660 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005661 if (items == NULL) goto failed;
5662 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005663 withitem_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005664 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5665 Py_INCREF(tmp2);
5666 res = obj2ast_withitem(tmp2, &val, arena);
5667 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005668 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005669 if (len != PyList_GET_SIZE(tmp)) {
5670 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
5671 goto failed;
5672 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005673 asdl_seq_SET(items, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005674 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005675 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005676 }
5677 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005678 return 1;
5679 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005680 if (tmp == NULL) {
5681 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
5682 return 1;
5683 }
5684 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005685 int res;
5686 Py_ssize_t len;
5687 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005688 if (!PyList_Check(tmp)) {
5689 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5690 goto failed;
5691 }
5692 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005693 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005694 if (body == NULL) goto failed;
5695 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005696 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005697 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5698 Py_INCREF(tmp2);
5699 res = obj2ast_stmt(tmp2, &val, arena);
5700 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005701 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005702 if (len != PyList_GET_SIZE(tmp)) {
5703 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
5704 goto failed;
5705 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005706 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005707 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005708 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005709 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005710 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5711 return 1;
5712 }
5713 if (tmp == NULL || tmp == Py_None) {
5714 Py_CLEAR(tmp);
5715 type_comment = NULL;
5716 }
5717 else {
5718 int res;
5719 res = obj2ast_string(tmp, &type_comment, arena);
5720 if (res != 0) goto failed;
5721 Py_CLEAR(tmp);
5722 }
5723 *out = With(items, body, type_comment, lineno, col_offset, end_lineno,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005724 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005725 if (*out == NULL) goto failed;
5726 return 0;
5727 }
Yury Selivanov75445082015-05-11 22:57:16 -04005728 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
5729 if (isinstance == -1) {
5730 return 1;
5731 }
5732 if (isinstance) {
5733 asdl_seq* items;
5734 asdl_seq* body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005735 string type_comment;
Yury Selivanov75445082015-05-11 22:57:16 -04005736
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005737 if (_PyObject_LookupAttrId(obj, &PyId_items, &tmp) < 0) {
5738 return 1;
5739 }
5740 if (tmp == NULL) {
5741 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
5742 return 1;
5743 }
5744 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005745 int res;
5746 Py_ssize_t len;
5747 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005748 if (!PyList_Check(tmp)) {
5749 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5750 goto failed;
5751 }
5752 len = PyList_GET_SIZE(tmp);
5753 items = _Py_asdl_seq_new(len, arena);
5754 if (items == NULL) goto failed;
5755 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005756 withitem_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005757 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5758 Py_INCREF(tmp2);
5759 res = obj2ast_withitem(tmp2, &val, arena);
5760 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005761 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005762 if (len != PyList_GET_SIZE(tmp)) {
5763 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
5764 goto failed;
5765 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005766 asdl_seq_SET(items, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005767 }
5768 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005769 }
5770 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04005771 return 1;
5772 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005773 if (tmp == NULL) {
5774 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
5775 return 1;
5776 }
5777 else {
Yury Selivanov75445082015-05-11 22:57:16 -04005778 int res;
5779 Py_ssize_t len;
5780 Py_ssize_t i;
Yury Selivanov75445082015-05-11 22:57:16 -04005781 if (!PyList_Check(tmp)) {
5782 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5783 goto failed;
5784 }
5785 len = PyList_GET_SIZE(tmp);
5786 body = _Py_asdl_seq_new(len, arena);
5787 if (body == NULL) goto failed;
5788 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005789 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005790 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5791 Py_INCREF(tmp2);
5792 res = obj2ast_stmt(tmp2, &val, arena);
5793 Py_DECREF(tmp2);
Yury Selivanov75445082015-05-11 22:57:16 -04005794 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005795 if (len != PyList_GET_SIZE(tmp)) {
5796 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
5797 goto failed;
5798 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005799 asdl_seq_SET(body, i, val);
Yury Selivanov75445082015-05-11 22:57:16 -04005800 }
5801 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04005802 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005803 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
5804 return 1;
5805 }
5806 if (tmp == NULL || tmp == Py_None) {
5807 Py_CLEAR(tmp);
5808 type_comment = NULL;
5809 }
5810 else {
5811 int res;
5812 res = obj2ast_string(tmp, &type_comment, arena);
5813 if (res != 0) goto failed;
5814 Py_CLEAR(tmp);
5815 }
5816 *out = AsyncWith(items, body, type_comment, lineno, col_offset,
5817 end_lineno, end_col_offset, arena);
Yury Selivanov75445082015-05-11 22:57:16 -04005818 if (*out == NULL) goto failed;
5819 return 0;
5820 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005821 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
5822 if (isinstance == -1) {
5823 return 1;
5824 }
5825 if (isinstance) {
5826 expr_ty exc;
5827 expr_ty cause;
5828
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005829 if (_PyObject_LookupAttrId(obj, &PyId_exc, &tmp) < 0) {
5830 return 1;
5831 }
5832 if (tmp == NULL || tmp == Py_None) {
5833 Py_CLEAR(tmp);
5834 exc = NULL;
5835 }
5836 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005837 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005838 res = obj2ast_expr(tmp, &exc, arena);
5839 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005842 if (_PyObject_LookupAttrId(obj, &PyId_cause, &tmp) < 0) {
5843 return 1;
5844 }
5845 if (tmp == NULL || tmp == Py_None) {
5846 Py_CLEAR(tmp);
5847 cause = NULL;
5848 }
5849 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005850 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005851 res = obj2ast_expr(tmp, &cause, arena);
5852 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005853 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005854 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005855 *out = Raise(exc, cause, lineno, col_offset, end_lineno,
5856 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005857 if (*out == NULL) goto failed;
5858 return 0;
5859 }
5860 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5861 if (isinstance == -1) {
5862 return 1;
5863 }
5864 if (isinstance) {
5865 asdl_seq* body;
5866 asdl_seq* handlers;
5867 asdl_seq* orelse;
5868 asdl_seq* finalbody;
5869
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005870 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
5871 return 1;
5872 }
5873 if (tmp == NULL) {
5874 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5875 return 1;
5876 }
5877 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005878 int res;
5879 Py_ssize_t len;
5880 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005881 if (!PyList_Check(tmp)) {
5882 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5883 goto failed;
5884 }
5885 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005886 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005887 if (body == NULL) goto failed;
5888 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005889 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005890 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5891 Py_INCREF(tmp2);
5892 res = obj2ast_stmt(tmp2, &val, arena);
5893 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005894 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005895 if (len != PyList_GET_SIZE(tmp)) {
5896 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5897 goto failed;
5898 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005899 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005900 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005901 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005902 }
5903 if (_PyObject_LookupAttrId(obj, &PyId_handlers, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005904 return 1;
5905 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005906 if (tmp == NULL) {
5907 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5908 return 1;
5909 }
5910 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005911 int res;
5912 Py_ssize_t len;
5913 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005914 if (!PyList_Check(tmp)) {
5915 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5916 goto failed;
5917 }
5918 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005919 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005920 if (handlers == NULL) goto failed;
5921 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005922 excepthandler_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005923 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5924 Py_INCREF(tmp2);
5925 res = obj2ast_excepthandler(tmp2, &val, arena);
5926 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005927 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005928 if (len != PyList_GET_SIZE(tmp)) {
5929 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5930 goto failed;
5931 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005932 asdl_seq_SET(handlers, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005933 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005934 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005935 }
5936 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005937 return 1;
5938 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005939 if (tmp == NULL) {
5940 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5941 return 1;
5942 }
5943 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005944 int res;
5945 Py_ssize_t len;
5946 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005947 if (!PyList_Check(tmp)) {
5948 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5949 goto failed;
5950 }
5951 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005952 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005953 if (orelse == NULL) goto failed;
5954 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005955 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005956 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5957 Py_INCREF(tmp2);
5958 res = obj2ast_stmt(tmp2, &val, arena);
5959 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005960 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005961 if (len != PyList_GET_SIZE(tmp)) {
5962 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5963 goto failed;
5964 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005965 asdl_seq_SET(orelse, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005966 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005967 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005968 }
5969 if (_PyObject_LookupAttrId(obj, &PyId_finalbody, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005970 return 1;
5971 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005972 if (tmp == NULL) {
5973 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5974 return 1;
5975 }
5976 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005977 int res;
5978 Py_ssize_t len;
5979 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005980 if (!PyList_Check(tmp)) {
5981 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5982 goto failed;
5983 }
5984 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005985 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005986 if (finalbody == NULL) goto failed;
5987 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005988 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03005989 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
5990 Py_INCREF(tmp2);
5991 res = obj2ast_stmt(tmp2, &val, arena);
5992 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005993 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005994 if (len != PyList_GET_SIZE(tmp)) {
5995 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5996 goto failed;
5997 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07005998 asdl_seq_SET(finalbody, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005999 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006000 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006001 }
6002 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006003 end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006004 if (*out == NULL) goto failed;
6005 return 0;
6006 }
6007 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
6008 if (isinstance == -1) {
6009 return 1;
6010 }
6011 if (isinstance) {
6012 expr_ty test;
6013 expr_ty msg;
6014
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006015 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
6016 return 1;
6017 }
6018 if (tmp == NULL) {
6019 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
6020 return 1;
6021 }
6022 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006023 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006024 res = obj2ast_expr(tmp, &test, arena);
6025 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006026 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006027 }
6028 if (_PyObject_LookupAttrId(obj, &PyId_msg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006029 return 1;
6030 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006031 if (tmp == NULL || tmp == Py_None) {
6032 Py_CLEAR(tmp);
6033 msg = NULL;
6034 }
6035 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006037 res = obj2ast_expr(tmp, &msg, arena);
6038 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006039 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006040 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006041 *out = Assert(test, msg, lineno, col_offset, end_lineno,
6042 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006043 if (*out == NULL) goto failed;
6044 return 0;
6045 }
6046 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
6047 if (isinstance == -1) {
6048 return 1;
6049 }
6050 if (isinstance) {
6051 asdl_seq* names;
6052
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006053 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6054 return 1;
6055 }
6056 if (tmp == NULL) {
6057 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
6058 return 1;
6059 }
6060 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006061 int res;
6062 Py_ssize_t len;
6063 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006064 if (!PyList_Check(tmp)) {
6065 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6066 goto failed;
6067 }
6068 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006069 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006070 if (names == NULL) goto failed;
6071 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006072 alias_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006073 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6074 Py_INCREF(tmp2);
6075 res = obj2ast_alias(tmp2, &val, arena);
6076 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006077 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006078 if (len != PyList_GET_SIZE(tmp)) {
6079 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
6080 goto failed;
6081 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006082 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006083 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006084 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006085 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006086 *out = Import(names, lineno, col_offset, end_lineno, end_col_offset,
6087 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006088 if (*out == NULL) goto failed;
6089 return 0;
6090 }
6091 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
6092 if (isinstance == -1) {
6093 return 1;
6094 }
6095 if (isinstance) {
6096 identifier module;
6097 asdl_seq* names;
6098 int level;
6099
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006100 if (_PyObject_LookupAttrId(obj, &PyId_module, &tmp) < 0) {
6101 return 1;
6102 }
6103 if (tmp == NULL || tmp == Py_None) {
6104 Py_CLEAR(tmp);
6105 module = NULL;
6106 }
6107 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006108 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006109 res = obj2ast_identifier(tmp, &module, arena);
6110 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006111 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006112 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006113 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6114 return 1;
6115 }
6116 if (tmp == NULL) {
6117 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
6118 return 1;
6119 }
6120 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006121 int res;
6122 Py_ssize_t len;
6123 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006124 if (!PyList_Check(tmp)) {
6125 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6126 goto failed;
6127 }
6128 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006129 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006130 if (names == NULL) goto failed;
6131 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006132 alias_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006133 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6134 Py_INCREF(tmp2);
6135 res = obj2ast_alias(tmp2, &val, arena);
6136 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006137 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006138 if (len != PyList_GET_SIZE(tmp)) {
6139 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
6140 goto failed;
6141 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006142 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006143 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006144 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006145 }
6146 if (_PyObject_LookupAttrId(obj, &PyId_level, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006147 return 1;
6148 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006149 if (tmp == NULL || tmp == Py_None) {
6150 Py_CLEAR(tmp);
6151 level = 0;
6152 }
6153 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006154 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006155 res = obj2ast_int(tmp, &level, arena);
6156 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006157 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006158 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006159 *out = ImportFrom(module, names, level, lineno, col_offset, end_lineno,
6160 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006161 if (*out == NULL) goto failed;
6162 return 0;
6163 }
6164 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
6165 if (isinstance == -1) {
6166 return 1;
6167 }
6168 if (isinstance) {
6169 asdl_seq* names;
6170
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006171 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6172 return 1;
6173 }
6174 if (tmp == NULL) {
6175 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
6176 return 1;
6177 }
6178 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006179 int res;
6180 Py_ssize_t len;
6181 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006182 if (!PyList_Check(tmp)) {
6183 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6184 goto failed;
6185 }
6186 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006187 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006188 if (names == NULL) goto failed;
6189 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006190 identifier val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006191 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6192 Py_INCREF(tmp2);
6193 res = obj2ast_identifier(tmp2, &val, arena);
6194 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006195 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006196 if (len != PyList_GET_SIZE(tmp)) {
6197 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
6198 goto failed;
6199 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006200 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006201 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006202 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006203 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006204 *out = Global(names, lineno, col_offset, end_lineno, end_col_offset,
6205 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006206 if (*out == NULL) goto failed;
6207 return 0;
6208 }
6209 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
6210 if (isinstance == -1) {
6211 return 1;
6212 }
6213 if (isinstance) {
6214 asdl_seq* names;
6215
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006216 if (_PyObject_LookupAttrId(obj, &PyId_names, &tmp) < 0) {
6217 return 1;
6218 }
6219 if (tmp == NULL) {
6220 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
6221 return 1;
6222 }
6223 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006224 int res;
6225 Py_ssize_t len;
6226 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006227 if (!PyList_Check(tmp)) {
6228 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6229 goto failed;
6230 }
6231 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006232 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006233 if (names == NULL) goto failed;
6234 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006235 identifier val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006236 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6237 Py_INCREF(tmp2);
6238 res = obj2ast_identifier(tmp2, &val, arena);
6239 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006240 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006241 if (len != PyList_GET_SIZE(tmp)) {
6242 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
6243 goto failed;
6244 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006245 asdl_seq_SET(names, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006246 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006248 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006249 *out = Nonlocal(names, lineno, col_offset, end_lineno, end_col_offset,
6250 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006251 if (*out == NULL) goto failed;
6252 return 0;
6253 }
6254 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
6255 if (isinstance == -1) {
6256 return 1;
6257 }
6258 if (isinstance) {
6259 expr_ty value;
6260
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006261 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6262 return 1;
6263 }
6264 if (tmp == NULL) {
6265 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
6266 return 1;
6267 }
6268 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006269 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006270 res = obj2ast_expr(tmp, &value, arena);
6271 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006272 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006273 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006274 *out = Expr(value, lineno, col_offset, end_lineno, end_col_offset,
6275 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006276 if (*out == NULL) goto failed;
6277 return 0;
6278 }
6279 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
6280 if (isinstance == -1) {
6281 return 1;
6282 }
6283 if (isinstance) {
6284
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006285 *out = Pass(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006286 if (*out == NULL) goto failed;
6287 return 0;
6288 }
6289 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
6290 if (isinstance == -1) {
6291 return 1;
6292 }
6293 if (isinstance) {
6294
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006295 *out = Break(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006296 if (*out == NULL) goto failed;
6297 return 0;
6298 }
6299 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
6300 if (isinstance == -1) {
6301 return 1;
6302 }
6303 if (isinstance) {
6304
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006305 *out = Continue(lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006306 if (*out == NULL) goto failed;
6307 return 0;
6308 }
6309
6310 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
6311 failed:
6312 Py_XDECREF(tmp);
6313 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006314}
6315
6316int
6317obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
6318{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006319 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006320
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006321 PyObject *tmp = NULL;
6322 int lineno;
6323 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006324 int end_lineno;
6325 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006326
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006327 if (obj == Py_None) {
6328 *out = NULL;
6329 return 0;
6330 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006331 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
6332 return 1;
6333 }
6334 if (tmp == NULL) {
6335 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
6336 return 1;
6337 }
6338 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006339 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006340 res = obj2ast_int(tmp, &lineno, arena);
6341 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006342 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006343 }
6344 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006345 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006346 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006347 if (tmp == NULL) {
6348 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
6349 return 1;
6350 }
6351 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006352 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006353 res = obj2ast_int(tmp, &col_offset, arena);
6354 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006355 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006356 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006357 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
6358 return 1;
6359 }
6360 if (tmp == NULL || tmp == Py_None) {
6361 Py_CLEAR(tmp);
6362 end_lineno = 0;
6363 }
6364 else {
6365 int res;
6366 res = obj2ast_int(tmp, &end_lineno, arena);
6367 if (res != 0) goto failed;
6368 Py_CLEAR(tmp);
6369 }
6370 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
6371 return 1;
6372 }
6373 if (tmp == NULL || tmp == Py_None) {
6374 Py_CLEAR(tmp);
6375 end_col_offset = 0;
6376 }
6377 else {
6378 int res;
6379 res = obj2ast_int(tmp, &end_col_offset, arena);
6380 if (res != 0) goto failed;
6381 Py_CLEAR(tmp);
6382 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006383 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
6384 if (isinstance == -1) {
6385 return 1;
6386 }
6387 if (isinstance) {
6388 boolop_ty op;
6389 asdl_seq* values;
6390
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006391 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
6392 return 1;
6393 }
6394 if (tmp == NULL) {
6395 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
6396 return 1;
6397 }
6398 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006399 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006400 res = obj2ast_boolop(tmp, &op, arena);
6401 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006402 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006403 }
6404 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006405 return 1;
6406 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006407 if (tmp == NULL) {
6408 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
6409 return 1;
6410 }
6411 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006412 int res;
6413 Py_ssize_t len;
6414 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006415 if (!PyList_Check(tmp)) {
6416 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6417 goto failed;
6418 }
6419 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006420 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006421 if (values == NULL) goto failed;
6422 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006423 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006424 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6425 Py_INCREF(tmp2);
6426 res = obj2ast_expr(tmp2, &val, arena);
6427 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006428 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006429 if (len != PyList_GET_SIZE(tmp)) {
6430 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
6431 goto failed;
6432 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006433 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006434 }
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 = BoolOp(op, values, 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 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07006442 isinstance = PyObject_IsInstance(obj, (PyObject*)NamedExpr_type);
6443 if (isinstance == -1) {
6444 return 1;
6445 }
6446 if (isinstance) {
6447 expr_ty target;
6448 expr_ty value;
6449
6450 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
6451 return 1;
6452 }
6453 if (tmp == NULL) {
6454 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from NamedExpr");
6455 return 1;
6456 }
6457 else {
6458 int res;
6459 res = obj2ast_expr(tmp, &target, arena);
6460 if (res != 0) goto failed;
6461 Py_CLEAR(tmp);
6462 }
6463 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
6464 return 1;
6465 }
6466 if (tmp == NULL) {
6467 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NamedExpr");
6468 return 1;
6469 }
6470 else {
6471 int res;
6472 res = obj2ast_expr(tmp, &value, arena);
6473 if (res != 0) goto failed;
6474 Py_CLEAR(tmp);
6475 }
6476 *out = NamedExpr(target, value, lineno, col_offset, end_lineno,
6477 end_col_offset, arena);
6478 if (*out == NULL) goto failed;
6479 return 0;
6480 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006481 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
6482 if (isinstance == -1) {
6483 return 1;
6484 }
6485 if (isinstance) {
6486 expr_ty left;
6487 operator_ty op;
6488 expr_ty right;
6489
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006490 if (_PyObject_LookupAttrId(obj, &PyId_left, &tmp) < 0) {
6491 return 1;
6492 }
6493 if (tmp == NULL) {
6494 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
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, &left, 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_op, &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 \"op\" missing from BinOp");
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_operator(tmp, &op, 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_right, &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 \"right\" missing from BinOp");
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, &right, 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 = BinOp(left, op, right, 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*)UnaryOp_type);
6535 if (isinstance == -1) {
6536 return 1;
6537 }
6538 if (isinstance) {
6539 unaryop_ty op;
6540 expr_ty operand;
6541
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006542 if (_PyObject_LookupAttrId(obj, &PyId_op, &tmp) < 0) {
6543 return 1;
6544 }
6545 if (tmp == NULL) {
6546 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
6547 return 1;
6548 }
6549 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006551 res = obj2ast_unaryop(tmp, &op, arena);
6552 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006553 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006554 }
6555 if (_PyObject_LookupAttrId(obj, &PyId_operand, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006556 return 1;
6557 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006558 if (tmp == NULL) {
6559 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
6560 return 1;
6561 }
6562 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006563 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006564 res = obj2ast_expr(tmp, &operand, arena);
6565 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006566 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006567 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006568 *out = UnaryOp(op, operand, lineno, col_offset, end_lineno,
6569 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006570 if (*out == NULL) goto failed;
6571 return 0;
6572 }
6573 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
6574 if (isinstance == -1) {
6575 return 1;
6576 }
6577 if (isinstance) {
6578 arguments_ty args;
6579 expr_ty body;
6580
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006581 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
6582 return 1;
6583 }
6584 if (tmp == NULL) {
6585 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
6586 return 1;
6587 }
6588 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006590 res = obj2ast_arguments(tmp, &args, arena);
6591 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006592 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006593 }
6594 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006595 return 1;
6596 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006597 if (tmp == NULL) {
6598 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
6599 return 1;
6600 }
6601 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006602 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006603 res = obj2ast_expr(tmp, &body, arena);
6604 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006605 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006606 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006607 *out = Lambda(args, body, lineno, col_offset, end_lineno,
6608 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006609 if (*out == NULL) goto failed;
6610 return 0;
6611 }
6612 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
6613 if (isinstance == -1) {
6614 return 1;
6615 }
6616 if (isinstance) {
6617 expr_ty test;
6618 expr_ty body;
6619 expr_ty orelse;
6620
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006621 if (_PyObject_LookupAttrId(obj, &PyId_test, &tmp) < 0) {
6622 return 1;
6623 }
6624 if (tmp == NULL) {
6625 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
6626 return 1;
6627 }
6628 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006629 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006630 res = obj2ast_expr(tmp, &test, arena);
6631 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006632 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006633 }
6634 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006635 return 1;
6636 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006637 if (tmp == NULL) {
6638 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
6639 return 1;
6640 }
6641 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006642 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006643 res = obj2ast_expr(tmp, &body, arena);
6644 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006645 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006646 }
6647 if (_PyObject_LookupAttrId(obj, &PyId_orelse, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006648 return 1;
6649 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006650 if (tmp == NULL) {
6651 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
6652 return 1;
6653 }
6654 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006655 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006656 res = obj2ast_expr(tmp, &orelse, arena);
6657 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006658 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006659 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006660 *out = IfExp(test, body, orelse, lineno, col_offset, end_lineno,
6661 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 if (*out == NULL) goto failed;
6663 return 0;
6664 }
6665 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
6666 if (isinstance == -1) {
6667 return 1;
6668 }
6669 if (isinstance) {
6670 asdl_seq* keys;
6671 asdl_seq* values;
6672
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006673 if (_PyObject_LookupAttrId(obj, &PyId_keys, &tmp) < 0) {
6674 return 1;
6675 }
6676 if (tmp == NULL) {
6677 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
6678 return 1;
6679 }
6680 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006681 int res;
6682 Py_ssize_t len;
6683 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006684 if (!PyList_Check(tmp)) {
6685 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6686 goto failed;
6687 }
6688 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006689 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006690 if (keys == NULL) goto failed;
6691 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006692 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006693 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6694 Py_INCREF(tmp2);
6695 res = obj2ast_expr(tmp2, &val, arena);
6696 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006697 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006698 if (len != PyList_GET_SIZE(tmp)) {
6699 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
6700 goto failed;
6701 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006702 asdl_seq_SET(keys, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006703 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006704 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006705 }
6706 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006707 return 1;
6708 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006709 if (tmp == NULL) {
6710 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
6711 return 1;
6712 }
6713 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006714 int res;
6715 Py_ssize_t len;
6716 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006717 if (!PyList_Check(tmp)) {
6718 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6719 goto failed;
6720 }
6721 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006722 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006723 if (values == NULL) goto failed;
6724 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006725 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006726 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6727 Py_INCREF(tmp2);
6728 res = obj2ast_expr(tmp2, &val, arena);
6729 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006730 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006731 if (len != PyList_GET_SIZE(tmp)) {
6732 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
6733 goto failed;
6734 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006735 asdl_seq_SET(values, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006736 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006737 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006738 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006739 *out = Dict(keys, values, lineno, col_offset, end_lineno,
6740 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006741 if (*out == NULL) goto failed;
6742 return 0;
6743 }
6744 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
6745 if (isinstance == -1) {
6746 return 1;
6747 }
6748 if (isinstance) {
6749 asdl_seq* elts;
6750
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006751 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
6752 return 1;
6753 }
6754 if (tmp == NULL) {
6755 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
6756 return 1;
6757 }
6758 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006759 int res;
6760 Py_ssize_t len;
6761 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006762 if (!PyList_Check(tmp)) {
6763 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6764 goto failed;
6765 }
6766 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006767 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006768 if (elts == NULL) goto failed;
6769 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006770 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006771 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6772 Py_INCREF(tmp2);
6773 res = obj2ast_expr(tmp2, &val, arena);
6774 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006775 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006776 if (len != PyList_GET_SIZE(tmp)) {
6777 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
6778 goto failed;
6779 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006780 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006781 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006782 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006783 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006784 *out = Set(elts, lineno, col_offset, end_lineno, end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006785 if (*out == NULL) goto failed;
6786 return 0;
6787 }
6788 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
6789 if (isinstance == -1) {
6790 return 1;
6791 }
6792 if (isinstance) {
6793 expr_ty elt;
6794 asdl_seq* generators;
6795
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006796 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6797 return 1;
6798 }
6799 if (tmp == NULL) {
6800 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
6801 return 1;
6802 }
6803 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006804 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006805 res = obj2ast_expr(tmp, &elt, arena);
6806 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006807 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006808 }
6809 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006810 return 1;
6811 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006812 if (tmp == NULL) {
6813 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
6814 return 1;
6815 }
6816 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006817 int res;
6818 Py_ssize_t len;
6819 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006820 if (!PyList_Check(tmp)) {
6821 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6822 goto failed;
6823 }
6824 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006825 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006826 if (generators == NULL) goto failed;
6827 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006828 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006829 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6830 Py_INCREF(tmp2);
6831 res = obj2ast_comprehension(tmp2, &val, arena);
6832 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006834 if (len != PyList_GET_SIZE(tmp)) {
6835 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
6836 goto failed;
6837 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006838 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006839 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006841 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006842 *out = ListComp(elt, generators, lineno, col_offset, end_lineno,
6843 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006844 if (*out == NULL) goto failed;
6845 return 0;
6846 }
6847 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
6848 if (isinstance == -1) {
6849 return 1;
6850 }
6851 if (isinstance) {
6852 expr_ty elt;
6853 asdl_seq* generators;
6854
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006855 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6856 return 1;
6857 }
6858 if (tmp == NULL) {
6859 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
6860 return 1;
6861 }
6862 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006863 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006864 res = obj2ast_expr(tmp, &elt, arena);
6865 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006866 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006867 }
6868 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006869 return 1;
6870 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006871 if (tmp == NULL) {
6872 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
6873 return 1;
6874 }
6875 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006876 int res;
6877 Py_ssize_t len;
6878 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006879 if (!PyList_Check(tmp)) {
6880 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6881 goto failed;
6882 }
6883 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006884 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006885 if (generators == NULL) goto failed;
6886 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006887 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006888 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6889 Py_INCREF(tmp2);
6890 res = obj2ast_comprehension(tmp2, &val, arena);
6891 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006892 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006893 if (len != PyList_GET_SIZE(tmp)) {
6894 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
6895 goto failed;
6896 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006897 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006898 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006899 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006900 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006901 *out = SetComp(elt, generators, lineno, col_offset, end_lineno,
6902 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006903 if (*out == NULL) goto failed;
6904 return 0;
6905 }
6906 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
6907 if (isinstance == -1) {
6908 return 1;
6909 }
6910 if (isinstance) {
6911 expr_ty key;
6912 expr_ty value;
6913 asdl_seq* generators;
6914
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006915 if (_PyObject_LookupAttrId(obj, &PyId_key, &tmp) < 0) {
6916 return 1;
6917 }
6918 if (tmp == NULL) {
6919 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
6920 return 1;
6921 }
6922 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006923 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006924 res = obj2ast_expr(tmp, &key, arena);
6925 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006926 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006927 }
6928 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006929 return 1;
6930 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006931 if (tmp == NULL) {
6932 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
6933 return 1;
6934 }
6935 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006936 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006937 res = obj2ast_expr(tmp, &value, arena);
6938 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006939 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006940 }
6941 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006942 return 1;
6943 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006944 if (tmp == NULL) {
6945 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
6946 return 1;
6947 }
6948 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006949 int res;
6950 Py_ssize_t len;
6951 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006952 if (!PyList_Check(tmp)) {
6953 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6954 goto failed;
6955 }
6956 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006957 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006958 if (generators == NULL) goto failed;
6959 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006960 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03006961 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
6962 Py_INCREF(tmp2);
6963 res = obj2ast_comprehension(tmp2, &val, arena);
6964 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006965 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006966 if (len != PyList_GET_SIZE(tmp)) {
6967 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
6968 goto failed;
6969 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07006970 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006971 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006972 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006973 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00006974 *out = DictComp(key, value, generators, lineno, col_offset, end_lineno,
6975 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006976 if (*out == NULL) goto failed;
6977 return 0;
6978 }
6979 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
6980 if (isinstance == -1) {
6981 return 1;
6982 }
6983 if (isinstance) {
6984 expr_ty elt;
6985 asdl_seq* generators;
6986
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006987 if (_PyObject_LookupAttrId(obj, &PyId_elt, &tmp) < 0) {
6988 return 1;
6989 }
6990 if (tmp == NULL) {
6991 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
6992 return 1;
6993 }
6994 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006995 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006996 res = obj2ast_expr(tmp, &elt, arena);
6997 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006998 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006999 }
7000 if (_PyObject_LookupAttrId(obj, &PyId_generators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007001 return 1;
7002 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007003 if (tmp == NULL) {
7004 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
7005 return 1;
7006 }
7007 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007008 int res;
7009 Py_ssize_t len;
7010 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007011 if (!PyList_Check(tmp)) {
7012 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7013 goto failed;
7014 }
7015 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007016 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007017 if (generators == NULL) goto failed;
7018 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007019 comprehension_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007020 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7021 Py_INCREF(tmp2);
7022 res = obj2ast_comprehension(tmp2, &val, arena);
7023 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007024 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007025 if (len != PyList_GET_SIZE(tmp)) {
7026 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
7027 goto failed;
7028 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007029 asdl_seq_SET(generators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007030 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007031 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007032 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007033 *out = GeneratorExp(elt, generators, lineno, col_offset, end_lineno,
7034 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007035 if (*out == NULL) goto failed;
7036 return 0;
7037 }
Yury Selivanov75445082015-05-11 22:57:16 -04007038 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
7039 if (isinstance == -1) {
7040 return 1;
7041 }
7042 if (isinstance) {
7043 expr_ty value;
7044
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007045 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7046 return 1;
7047 }
7048 if (tmp == NULL) {
7049 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
7050 return 1;
7051 }
7052 else {
Yury Selivanov75445082015-05-11 22:57:16 -04007053 int res;
Yury Selivanov75445082015-05-11 22:57:16 -04007054 res = obj2ast_expr(tmp, &value, arena);
7055 if (res != 0) goto failed;
7056 Py_CLEAR(tmp);
Yury Selivanov75445082015-05-11 22:57:16 -04007057 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007058 *out = Await(value, lineno, col_offset, end_lineno, end_col_offset,
7059 arena);
Yury Selivanov75445082015-05-11 22:57:16 -04007060 if (*out == NULL) goto failed;
7061 return 0;
7062 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007063 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
7064 if (isinstance == -1) {
7065 return 1;
7066 }
7067 if (isinstance) {
7068 expr_ty value;
7069
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007070 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7071 return 1;
7072 }
7073 if (tmp == NULL || tmp == Py_None) {
7074 Py_CLEAR(tmp);
7075 value = NULL;
7076 }
7077 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007078 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007079 res = obj2ast_expr(tmp, &value, arena);
7080 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007081 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007082 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007083 *out = Yield(value, lineno, col_offset, end_lineno, end_col_offset,
7084 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007085 if (*out == NULL) goto failed;
7086 return 0;
7087 }
7088 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
7089 if (isinstance == -1) {
7090 return 1;
7091 }
7092 if (isinstance) {
7093 expr_ty value;
7094
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007095 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7096 return 1;
7097 }
7098 if (tmp == NULL) {
7099 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
7100 return 1;
7101 }
7102 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007103 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007104 res = obj2ast_expr(tmp, &value, arena);
7105 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007106 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007107 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007108 *out = YieldFrom(value, lineno, col_offset, end_lineno, end_col_offset,
7109 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007110 if (*out == NULL) goto failed;
7111 return 0;
7112 }
7113 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
7114 if (isinstance == -1) {
7115 return 1;
7116 }
7117 if (isinstance) {
7118 expr_ty left;
7119 asdl_int_seq* ops;
7120 asdl_seq* comparators;
7121
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007122 if (_PyObject_LookupAttrId(obj, &PyId_left, &tmp) < 0) {
7123 return 1;
7124 }
7125 if (tmp == NULL) {
7126 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
7127 return 1;
7128 }
7129 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007130 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007131 res = obj2ast_expr(tmp, &left, arena);
7132 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007133 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007134 }
7135 if (_PyObject_LookupAttrId(obj, &PyId_ops, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007136 return 1;
7137 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007138 if (tmp == NULL) {
7139 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
7140 return 1;
7141 }
7142 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007143 int res;
7144 Py_ssize_t len;
7145 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007146 if (!PyList_Check(tmp)) {
7147 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7148 goto failed;
7149 }
7150 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007151 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007152 if (ops == NULL) goto failed;
7153 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007154 cmpop_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007155 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7156 Py_INCREF(tmp2);
7157 res = obj2ast_cmpop(tmp2, &val, arena);
7158 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007159 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007160 if (len != PyList_GET_SIZE(tmp)) {
7161 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
7162 goto failed;
7163 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007164 asdl_seq_SET(ops, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007165 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007166 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007167 }
7168 if (_PyObject_LookupAttrId(obj, &PyId_comparators, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007169 return 1;
7170 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007171 if (tmp == NULL) {
7172 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
7173 return 1;
7174 }
7175 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007176 int res;
7177 Py_ssize_t len;
7178 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007179 if (!PyList_Check(tmp)) {
7180 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7181 goto failed;
7182 }
7183 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007184 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007185 if (comparators == NULL) goto failed;
7186 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007187 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007188 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7189 Py_INCREF(tmp2);
7190 res = obj2ast_expr(tmp2, &val, arena);
7191 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007192 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007193 if (len != PyList_GET_SIZE(tmp)) {
7194 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
7195 goto failed;
7196 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007197 asdl_seq_SET(comparators, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007198 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007200 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007201 *out = Compare(left, ops, comparators, lineno, col_offset, end_lineno,
7202 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007203 if (*out == NULL) goto failed;
7204 return 0;
7205 }
7206 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
7207 if (isinstance == -1) {
7208 return 1;
7209 }
7210 if (isinstance) {
7211 expr_ty func;
7212 asdl_seq* args;
7213 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007214
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007215 if (_PyObject_LookupAttrId(obj, &PyId_func, &tmp) < 0) {
7216 return 1;
7217 }
7218 if (tmp == NULL) {
7219 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
7220 return 1;
7221 }
7222 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007223 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007224 res = obj2ast_expr(tmp, &func, arena);
7225 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007226 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007227 }
7228 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007229 return 1;
7230 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007231 if (tmp == NULL) {
7232 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
7233 return 1;
7234 }
7235 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007236 int res;
7237 Py_ssize_t len;
7238 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007239 if (!PyList_Check(tmp)) {
7240 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7241 goto failed;
7242 }
7243 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007244 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007245 if (args == NULL) goto failed;
7246 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007247 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007248 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7249 Py_INCREF(tmp2);
7250 res = obj2ast_expr(tmp2, &val, arena);
7251 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007252 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007253 if (len != PyList_GET_SIZE(tmp)) {
7254 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
7255 goto failed;
7256 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007257 asdl_seq_SET(args, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007258 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007259 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007260 }
7261 if (_PyObject_LookupAttrId(obj, &PyId_keywords, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007262 return 1;
7263 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007264 if (tmp == NULL) {
7265 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
7266 return 1;
7267 }
7268 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007269 int res;
7270 Py_ssize_t len;
7271 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007272 if (!PyList_Check(tmp)) {
7273 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7274 goto failed;
7275 }
7276 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007277 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007278 if (keywords == NULL) goto failed;
7279 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007280 keyword_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007281 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7282 Py_INCREF(tmp2);
7283 res = obj2ast_keyword(tmp2, &val, arena);
7284 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007285 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007286 if (len != PyList_GET_SIZE(tmp)) {
7287 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
7288 goto failed;
7289 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007290 asdl_seq_SET(keywords, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007291 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007292 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007293 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007294 *out = Call(func, args, keywords, lineno, col_offset, end_lineno,
7295 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007296 if (*out == NULL) goto failed;
7297 return 0;
7298 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04007299 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
7300 if (isinstance == -1) {
7301 return 1;
7302 }
7303 if (isinstance) {
7304 expr_ty value;
7305 int conversion;
7306 expr_ty format_spec;
7307
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007308 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7309 return 1;
7310 }
7311 if (tmp == NULL) {
7312 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
7313 return 1;
7314 }
7315 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007316 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007317 res = obj2ast_expr(tmp, &value, arena);
7318 if (res != 0) goto failed;
7319 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007320 }
7321 if (_PyObject_LookupAttrId(obj, &PyId_conversion, &tmp) < 0) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007322 return 1;
7323 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007324 if (tmp == NULL || tmp == Py_None) {
7325 Py_CLEAR(tmp);
7326 conversion = 0;
7327 }
7328 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007329 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007330 res = obj2ast_int(tmp, &conversion, arena);
7331 if (res != 0) goto failed;
7332 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007333 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007334 if (_PyObject_LookupAttrId(obj, &PyId_format_spec, &tmp) < 0) {
7335 return 1;
7336 }
7337 if (tmp == NULL || tmp == Py_None) {
7338 Py_CLEAR(tmp);
7339 format_spec = NULL;
7340 }
7341 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007342 int res;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007343 res = obj2ast_expr(tmp, &format_spec, arena);
7344 if (res != 0) goto failed;
7345 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007346 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04007347 *out = FormattedValue(value, conversion, format_spec, lineno,
7348 col_offset, end_lineno, end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007349 if (*out == NULL) goto failed;
7350 return 0;
7351 }
7352 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
7353 if (isinstance == -1) {
7354 return 1;
7355 }
7356 if (isinstance) {
7357 asdl_seq* values;
7358
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007359 if (_PyObject_LookupAttrId(obj, &PyId_values, &tmp) < 0) {
7360 return 1;
7361 }
7362 if (tmp == NULL) {
7363 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
7364 return 1;
7365 }
7366 else {
Eric V. Smith235a6f02015-09-19 14:51:32 -04007367 int res;
7368 Py_ssize_t len;
7369 Py_ssize_t i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007370 if (!PyList_Check(tmp)) {
7371 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7372 goto failed;
7373 }
7374 len = PyList_GET_SIZE(tmp);
7375 values = _Py_asdl_seq_new(len, arena);
7376 if (values == NULL) goto failed;
7377 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007378 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007379 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7380 Py_INCREF(tmp2);
7381 res = obj2ast_expr(tmp2, &val, arena);
7382 Py_DECREF(tmp2);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007383 if (res != 0) goto failed;
Serhiy Storchaka5e808552016-10-07 21:55:49 +03007384 if (len != PyList_GET_SIZE(tmp)) {
7385 PyErr_SetString(PyExc_RuntimeError, "JoinedStr field \"values\" changed size during iteration");
7386 goto failed;
7387 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007388 asdl_seq_SET(values, i, val);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007389 }
7390 Py_CLEAR(tmp);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007391 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007392 *out = JoinedStr(values, lineno, col_offset, end_lineno,
7393 end_col_offset, arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04007394 if (*out == NULL) goto failed;
7395 return 0;
7396 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007397 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
7398 if (isinstance == -1) {
7399 return 1;
7400 }
7401 if (isinstance) {
7402 constant value;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07007403 string kind;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007404
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007405 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7406 return 1;
7407 }
7408 if (tmp == NULL) {
7409 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
7410 return 1;
7411 }
7412 else {
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007413 int res;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007414 res = obj2ast_constant(tmp, &value, arena);
7415 if (res != 0) goto failed;
7416 Py_CLEAR(tmp);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007417 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07007418 if (_PyObject_LookupAttrId(obj, &PyId_kind, &tmp) < 0) {
7419 return 1;
7420 }
7421 if (tmp == NULL || tmp == Py_None) {
7422 Py_CLEAR(tmp);
7423 kind = NULL;
7424 }
7425 else {
7426 int res;
7427 res = obj2ast_string(tmp, &kind, arena);
7428 if (res != 0) goto failed;
7429 Py_CLEAR(tmp);
7430 }
7431 *out = Constant(value, kind, lineno, col_offset, end_lineno,
7432 end_col_offset, arena);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007433 if (*out == NULL) goto failed;
7434 return 0;
7435 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007436 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
7437 if (isinstance == -1) {
7438 return 1;
7439 }
7440 if (isinstance) {
7441 expr_ty value;
7442 identifier attr;
7443 expr_context_ty ctx;
7444
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007445 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7446 return 1;
7447 }
7448 if (tmp == NULL) {
7449 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
7450 return 1;
7451 }
7452 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007453 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007454 res = obj2ast_expr(tmp, &value, arena);
7455 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007456 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007457 }
7458 if (_PyObject_LookupAttrId(obj, &PyId_attr, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007459 return 1;
7460 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007461 if (tmp == NULL) {
7462 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
7463 return 1;
7464 }
7465 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007466 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007467 res = obj2ast_identifier(tmp, &attr, arena);
7468 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007469 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007470 }
7471 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007472 return 1;
7473 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007474 if (tmp == NULL) {
7475 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
7476 return 1;
7477 }
7478 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007479 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007480 res = obj2ast_expr_context(tmp, &ctx, arena);
7481 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007482 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007483 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007484 *out = Attribute(value, attr, ctx, lineno, col_offset, end_lineno,
7485 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007486 if (*out == NULL) goto failed;
7487 return 0;
7488 }
7489 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
7490 if (isinstance == -1) {
7491 return 1;
7492 }
7493 if (isinstance) {
7494 expr_ty value;
7495 slice_ty slice;
7496 expr_context_ty ctx;
7497
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007498 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7499 return 1;
7500 }
7501 if (tmp == NULL) {
7502 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
7503 return 1;
7504 }
7505 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007506 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007507 res = obj2ast_expr(tmp, &value, arena);
7508 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007509 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007510 }
7511 if (_PyObject_LookupAttrId(obj, &PyId_slice, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007512 return 1;
7513 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007514 if (tmp == NULL) {
7515 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
7516 return 1;
7517 }
7518 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007519 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007520 res = obj2ast_slice(tmp, &slice, arena);
7521 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007522 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007523 }
7524 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007525 return 1;
7526 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007527 if (tmp == NULL) {
7528 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
7529 return 1;
7530 }
7531 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007532 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007533 res = obj2ast_expr_context(tmp, &ctx, arena);
7534 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007535 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007536 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007537 *out = Subscript(value, slice, ctx, lineno, col_offset, end_lineno,
7538 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007539 if (*out == NULL) goto failed;
7540 return 0;
7541 }
7542 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
7543 if (isinstance == -1) {
7544 return 1;
7545 }
7546 if (isinstance) {
7547 expr_ty value;
7548 expr_context_ty ctx;
7549
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007550 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7551 return 1;
7552 }
7553 if (tmp == NULL) {
7554 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
7555 return 1;
7556 }
7557 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007558 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007559 res = obj2ast_expr(tmp, &value, arena);
7560 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007561 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007562 }
7563 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007564 return 1;
7565 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007566 if (tmp == NULL) {
7567 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
7568 return 1;
7569 }
7570 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007571 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007572 res = obj2ast_expr_context(tmp, &ctx, arena);
7573 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007574 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007575 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007576 *out = Starred(value, ctx, lineno, col_offset, end_lineno,
7577 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007578 if (*out == NULL) goto failed;
7579 return 0;
7580 }
7581 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
7582 if (isinstance == -1) {
7583 return 1;
7584 }
7585 if (isinstance) {
7586 identifier id;
7587 expr_context_ty ctx;
7588
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007589 if (_PyObject_LookupAttrId(obj, &PyId_id, &tmp) < 0) {
7590 return 1;
7591 }
7592 if (tmp == NULL) {
7593 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
7594 return 1;
7595 }
7596 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007597 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007598 res = obj2ast_identifier(tmp, &id, arena);
7599 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007600 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007601 }
7602 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007603 return 1;
7604 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007605 if (tmp == NULL) {
7606 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
7607 return 1;
7608 }
7609 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007610 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007611 res = obj2ast_expr_context(tmp, &ctx, arena);
7612 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007613 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007614 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007615 *out = Name(id, ctx, lineno, col_offset, end_lineno, end_col_offset,
7616 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007617 if (*out == NULL) goto failed;
7618 return 0;
7619 }
7620 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
7621 if (isinstance == -1) {
7622 return 1;
7623 }
7624 if (isinstance) {
7625 asdl_seq* elts;
7626 expr_context_ty ctx;
7627
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007628 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
7629 return 1;
7630 }
7631 if (tmp == NULL) {
7632 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
7633 return 1;
7634 }
7635 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007636 int res;
7637 Py_ssize_t len;
7638 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007639 if (!PyList_Check(tmp)) {
7640 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7641 goto failed;
7642 }
7643 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007644 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007645 if (elts == NULL) goto failed;
7646 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007647 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007648 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7649 Py_INCREF(tmp2);
7650 res = obj2ast_expr(tmp2, &val, arena);
7651 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007652 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007653 if (len != PyList_GET_SIZE(tmp)) {
7654 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
7655 goto failed;
7656 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007657 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007658 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007659 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007660 }
7661 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007662 return 1;
7663 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007664 if (tmp == NULL) {
7665 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
7666 return 1;
7667 }
7668 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007669 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007670 res = obj2ast_expr_context(tmp, &ctx, arena);
7671 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007672 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007673 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007674 *out = List(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
7675 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007676 if (*out == NULL) goto failed;
7677 return 0;
7678 }
7679 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
7680 if (isinstance == -1) {
7681 return 1;
7682 }
7683 if (isinstance) {
7684 asdl_seq* elts;
7685 expr_context_ty ctx;
7686
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007687 if (_PyObject_LookupAttrId(obj, &PyId_elts, &tmp) < 0) {
7688 return 1;
7689 }
7690 if (tmp == NULL) {
7691 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
7692 return 1;
7693 }
7694 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007695 int res;
7696 Py_ssize_t len;
7697 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007698 if (!PyList_Check(tmp)) {
7699 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7700 goto failed;
7701 }
7702 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007703 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007704 if (elts == NULL) goto failed;
7705 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007706 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007707 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7708 Py_INCREF(tmp2);
7709 res = obj2ast_expr(tmp2, &val, arena);
7710 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007711 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007712 if (len != PyList_GET_SIZE(tmp)) {
7713 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
7714 goto failed;
7715 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007716 asdl_seq_SET(elts, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007717 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007718 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007719 }
7720 if (_PyObject_LookupAttrId(obj, &PyId_ctx, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007721 return 1;
7722 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007723 if (tmp == NULL) {
7724 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
7725 return 1;
7726 }
7727 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007728 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007729 res = obj2ast_expr_context(tmp, &ctx, arena);
7730 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007731 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007732 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00007733 *out = Tuple(elts, ctx, lineno, col_offset, end_lineno, end_col_offset,
7734 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007735 if (*out == NULL) goto failed;
7736 return 0;
7737 }
7738
7739 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
7740 failed:
7741 Py_XDECREF(tmp);
7742 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007743}
7744
7745int
7746obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
7747{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007748 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007749
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007750 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
7751 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007752 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007753 }
7754 if (isinstance) {
7755 *out = Load;
7756 return 0;
7757 }
7758 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
7759 if (isinstance == -1) {
7760 return 1;
7761 }
7762 if (isinstance) {
7763 *out = Store;
7764 return 0;
7765 }
7766 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
7767 if (isinstance == -1) {
7768 return 1;
7769 }
7770 if (isinstance) {
7771 *out = Del;
7772 return 0;
7773 }
7774 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
7775 if (isinstance == -1) {
7776 return 1;
7777 }
7778 if (isinstance) {
7779 *out = AugLoad;
7780 return 0;
7781 }
7782 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
7783 if (isinstance == -1) {
7784 return 1;
7785 }
7786 if (isinstance) {
7787 *out = AugStore;
7788 return 0;
7789 }
7790 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
7791 if (isinstance == -1) {
7792 return 1;
7793 }
7794 if (isinstance) {
7795 *out = Param;
7796 return 0;
7797 }
7798
7799 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
7800 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007801}
7802
7803int
7804obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
7805{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007806 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007807
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007808 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007809
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007810 if (obj == Py_None) {
7811 *out = NULL;
7812 return 0;
7813 }
7814 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
7815 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007816 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007817 }
7818 if (isinstance) {
7819 expr_ty lower;
7820 expr_ty upper;
7821 expr_ty step;
7822
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007823 if (_PyObject_LookupAttrId(obj, &PyId_lower, &tmp) < 0) {
7824 return 1;
7825 }
7826 if (tmp == NULL || tmp == Py_None) {
7827 Py_CLEAR(tmp);
7828 lower = NULL;
7829 }
7830 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007831 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007832 res = obj2ast_expr(tmp, &lower, arena);
7833 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007834 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007835 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007836 if (_PyObject_LookupAttrId(obj, &PyId_upper, &tmp) < 0) {
7837 return 1;
7838 }
7839 if (tmp == NULL || tmp == Py_None) {
7840 Py_CLEAR(tmp);
7841 upper = NULL;
7842 }
7843 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007844 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007845 res = obj2ast_expr(tmp, &upper, arena);
7846 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007847 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007848 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007849 if (_PyObject_LookupAttrId(obj, &PyId_step, &tmp) < 0) {
7850 return 1;
7851 }
7852 if (tmp == NULL || tmp == Py_None) {
7853 Py_CLEAR(tmp);
7854 step = NULL;
7855 }
7856 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007857 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007858 res = obj2ast_expr(tmp, &step, arena);
7859 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007860 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007861 }
7862 *out = Slice(lower, upper, step, arena);
7863 if (*out == NULL) goto failed;
7864 return 0;
7865 }
7866 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
7867 if (isinstance == -1) {
7868 return 1;
7869 }
7870 if (isinstance) {
7871 asdl_seq* dims;
7872
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007873 if (_PyObject_LookupAttrId(obj, &PyId_dims, &tmp) < 0) {
7874 return 1;
7875 }
7876 if (tmp == NULL) {
7877 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
7878 return 1;
7879 }
7880 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007881 int res;
7882 Py_ssize_t len;
7883 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007884 if (!PyList_Check(tmp)) {
7885 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7886 goto failed;
7887 }
7888 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007889 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007890 if (dims == NULL) goto failed;
7891 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007892 slice_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03007893 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
7894 Py_INCREF(tmp2);
7895 res = obj2ast_slice(tmp2, &val, arena);
7896 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007897 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007898 if (len != PyList_GET_SIZE(tmp)) {
7899 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
7900 goto failed;
7901 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07007902 asdl_seq_SET(dims, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007903 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007904 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007905 }
7906 *out = ExtSlice(dims, arena);
7907 if (*out == NULL) goto failed;
7908 return 0;
7909 }
7910 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
7911 if (isinstance == -1) {
7912 return 1;
7913 }
7914 if (isinstance) {
7915 expr_ty value;
7916
Serhiy Storchakaf320be72018-01-25 10:49:40 +02007917 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
7918 return 1;
7919 }
7920 if (tmp == NULL) {
7921 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
7922 return 1;
7923 }
7924 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007925 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007926 res = obj2ast_expr(tmp, &value, arena);
7927 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007928 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007929 }
7930 *out = Index(value, arena);
7931 if (*out == NULL) goto failed;
7932 return 0;
7933 }
7934
7935 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
7936 failed:
7937 Py_XDECREF(tmp);
7938 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007939}
7940
7941int
7942obj2ast_boolop(PyObject* obj, boolop_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 *)And_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 = And;
7952 return 0;
7953 }
7954 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
7955 if (isinstance == -1) {
7956 return 1;
7957 }
7958 if (isinstance) {
7959 *out = Or;
7960 return 0;
7961 }
7962
7963 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
7964 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007965}
7966
7967int
7968obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
7969{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007970 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007971
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007972 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
7973 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007974 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007975 }
7976 if (isinstance) {
7977 *out = Add;
7978 return 0;
7979 }
7980 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
7981 if (isinstance == -1) {
7982 return 1;
7983 }
7984 if (isinstance) {
7985 *out = Sub;
7986 return 0;
7987 }
7988 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
7989 if (isinstance == -1) {
7990 return 1;
7991 }
7992 if (isinstance) {
7993 *out = Mult;
7994 return 0;
7995 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04007996 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
7997 if (isinstance == -1) {
7998 return 1;
7999 }
8000 if (isinstance) {
8001 *out = MatMult;
8002 return 0;
8003 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008004 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
8005 if (isinstance == -1) {
8006 return 1;
8007 }
8008 if (isinstance) {
8009 *out = Div;
8010 return 0;
8011 }
8012 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
8013 if (isinstance == -1) {
8014 return 1;
8015 }
8016 if (isinstance) {
8017 *out = Mod;
8018 return 0;
8019 }
8020 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
8021 if (isinstance == -1) {
8022 return 1;
8023 }
8024 if (isinstance) {
8025 *out = Pow;
8026 return 0;
8027 }
8028 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
8029 if (isinstance == -1) {
8030 return 1;
8031 }
8032 if (isinstance) {
8033 *out = LShift;
8034 return 0;
8035 }
8036 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
8037 if (isinstance == -1) {
8038 return 1;
8039 }
8040 if (isinstance) {
8041 *out = RShift;
8042 return 0;
8043 }
8044 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
8045 if (isinstance == -1) {
8046 return 1;
8047 }
8048 if (isinstance) {
8049 *out = BitOr;
8050 return 0;
8051 }
8052 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
8053 if (isinstance == -1) {
8054 return 1;
8055 }
8056 if (isinstance) {
8057 *out = BitXor;
8058 return 0;
8059 }
8060 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
8061 if (isinstance == -1) {
8062 return 1;
8063 }
8064 if (isinstance) {
8065 *out = BitAnd;
8066 return 0;
8067 }
8068 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
8069 if (isinstance == -1) {
8070 return 1;
8071 }
8072 if (isinstance) {
8073 *out = FloorDiv;
8074 return 0;
8075 }
8076
8077 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
8078 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008079}
8080
8081int
8082obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
8083{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008084 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008085
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008086 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
8087 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008088 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008089 }
8090 if (isinstance) {
8091 *out = Invert;
8092 return 0;
8093 }
8094 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
8095 if (isinstance == -1) {
8096 return 1;
8097 }
8098 if (isinstance) {
8099 *out = Not;
8100 return 0;
8101 }
8102 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
8103 if (isinstance == -1) {
8104 return 1;
8105 }
8106 if (isinstance) {
8107 *out = UAdd;
8108 return 0;
8109 }
8110 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
8111 if (isinstance == -1) {
8112 return 1;
8113 }
8114 if (isinstance) {
8115 *out = USub;
8116 return 0;
8117 }
8118
8119 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
8120 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008121}
8122
8123int
8124obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
8125{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008126 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008127
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008128 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
8129 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008130 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008131 }
8132 if (isinstance) {
8133 *out = Eq;
8134 return 0;
8135 }
8136 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
8137 if (isinstance == -1) {
8138 return 1;
8139 }
8140 if (isinstance) {
8141 *out = NotEq;
8142 return 0;
8143 }
8144 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
8145 if (isinstance == -1) {
8146 return 1;
8147 }
8148 if (isinstance) {
8149 *out = Lt;
8150 return 0;
8151 }
8152 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
8153 if (isinstance == -1) {
8154 return 1;
8155 }
8156 if (isinstance) {
8157 *out = LtE;
8158 return 0;
8159 }
8160 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
8161 if (isinstance == -1) {
8162 return 1;
8163 }
8164 if (isinstance) {
8165 *out = Gt;
8166 return 0;
8167 }
8168 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
8169 if (isinstance == -1) {
8170 return 1;
8171 }
8172 if (isinstance) {
8173 *out = GtE;
8174 return 0;
8175 }
8176 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
8177 if (isinstance == -1) {
8178 return 1;
8179 }
8180 if (isinstance) {
8181 *out = Is;
8182 return 0;
8183 }
8184 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
8185 if (isinstance == -1) {
8186 return 1;
8187 }
8188 if (isinstance) {
8189 *out = IsNot;
8190 return 0;
8191 }
8192 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
8193 if (isinstance == -1) {
8194 return 1;
8195 }
8196 if (isinstance) {
8197 *out = In;
8198 return 0;
8199 }
8200 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
8201 if (isinstance == -1) {
8202 return 1;
8203 }
8204 if (isinstance) {
8205 *out = NotIn;
8206 return 0;
8207 }
8208
8209 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
8210 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008211}
8212
8213int
8214obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
8215{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008216 PyObject* tmp = NULL;
8217 expr_ty target;
8218 expr_ty iter;
8219 asdl_seq* ifs;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008220 int is_async;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008221
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008222 if (_PyObject_LookupAttrId(obj, &PyId_target, &tmp) < 0) {
8223 return 1;
8224 }
8225 if (tmp == NULL) {
8226 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
8227 return 1;
8228 }
8229 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008230 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008231 res = obj2ast_expr(tmp, &target, arena);
8232 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008233 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008234 }
8235 if (_PyObject_LookupAttrId(obj, &PyId_iter, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008236 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008237 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008238 if (tmp == NULL) {
8239 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
8240 return 1;
8241 }
8242 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008243 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008244 res = obj2ast_expr(tmp, &iter, arena);
8245 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008246 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008247 }
8248 if (_PyObject_LookupAttrId(obj, &PyId_ifs, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008249 return 1;
8250 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008251 if (tmp == NULL) {
8252 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
8253 return 1;
8254 }
8255 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008256 int res;
8257 Py_ssize_t len;
8258 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008259 if (!PyList_Check(tmp)) {
8260 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8261 goto failed;
8262 }
8263 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008264 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008265 if (ifs == NULL) goto failed;
8266 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008267 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008268 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8269 Py_INCREF(tmp2);
8270 res = obj2ast_expr(tmp2, &val, arena);
8271 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008272 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008273 if (len != PyList_GET_SIZE(tmp)) {
8274 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
8275 goto failed;
8276 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008277 asdl_seq_SET(ifs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008278 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008279 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008280 }
8281 if (_PyObject_LookupAttrId(obj, &PyId_is_async, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008282 return 1;
8283 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008284 if (tmp == NULL) {
8285 PyErr_SetString(PyExc_TypeError, "required field \"is_async\" missing from comprehension");
8286 return 1;
8287 }
8288 else {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008289 int res;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008290 res = obj2ast_int(tmp, &is_async, arena);
8291 if (res != 0) goto failed;
8292 Py_CLEAR(tmp);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07008293 }
8294 *out = comprehension(target, iter, ifs, is_async, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008295 return 0;
8296failed:
8297 Py_XDECREF(tmp);
8298 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008299}
8300
8301int
8302obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
8303{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008304 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00008305
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008306 PyObject *tmp = NULL;
8307 int lineno;
8308 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008309 int end_lineno;
8310 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008311
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008312 if (obj == Py_None) {
8313 *out = NULL;
8314 return 0;
8315 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008316 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8317 return 1;
8318 }
8319 if (tmp == NULL) {
8320 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
8321 return 1;
8322 }
8323 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008324 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008325 res = obj2ast_int(tmp, &lineno, arena);
8326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008327 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008328 }
8329 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008330 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008331 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008332 if (tmp == NULL) {
8333 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
8334 return 1;
8335 }
8336 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008337 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008338 res = obj2ast_int(tmp, &col_offset, arena);
8339 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008340 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008341 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008342 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
8343 return 1;
8344 }
8345 if (tmp == NULL || tmp == Py_None) {
8346 Py_CLEAR(tmp);
8347 end_lineno = 0;
8348 }
8349 else {
8350 int res;
8351 res = obj2ast_int(tmp, &end_lineno, arena);
8352 if (res != 0) goto failed;
8353 Py_CLEAR(tmp);
8354 }
8355 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
8356 return 1;
8357 }
8358 if (tmp == NULL || tmp == Py_None) {
8359 Py_CLEAR(tmp);
8360 end_col_offset = 0;
8361 }
8362 else {
8363 int res;
8364 res = obj2ast_int(tmp, &end_col_offset, arena);
8365 if (res != 0) goto failed;
8366 Py_CLEAR(tmp);
8367 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008368 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
8369 if (isinstance == -1) {
8370 return 1;
8371 }
8372 if (isinstance) {
8373 expr_ty type;
8374 identifier name;
8375 asdl_seq* body;
8376
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008377 if (_PyObject_LookupAttrId(obj, &PyId_type, &tmp) < 0) {
8378 return 1;
8379 }
8380 if (tmp == NULL || tmp == Py_None) {
8381 Py_CLEAR(tmp);
8382 type = NULL;
8383 }
8384 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008385 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008386 res = obj2ast_expr(tmp, &type, arena);
8387 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008388 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008389 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008390 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
8391 return 1;
8392 }
8393 if (tmp == NULL || tmp == Py_None) {
8394 Py_CLEAR(tmp);
8395 name = NULL;
8396 }
8397 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008398 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008399 res = obj2ast_identifier(tmp, &name, arena);
8400 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008401 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008402 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008403 if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
8404 return 1;
8405 }
8406 if (tmp == NULL) {
8407 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
8408 return 1;
8409 }
8410 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008411 int res;
8412 Py_ssize_t len;
8413 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008414 if (!PyList_Check(tmp)) {
8415 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8416 goto failed;
8417 }
8418 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008419 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008420 if (body == NULL) goto failed;
8421 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008422 stmt_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008423 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8424 Py_INCREF(tmp2);
8425 res = obj2ast_stmt(tmp2, &val, arena);
8426 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008427 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008428 if (len != PyList_GET_SIZE(tmp)) {
8429 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
8430 goto failed;
8431 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008432 asdl_seq_SET(body, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008433 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008434 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008435 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008436 *out = ExceptHandler(type, name, body, lineno, col_offset, end_lineno,
8437 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008438 if (*out == NULL) goto failed;
8439 return 0;
8440 }
8441
8442 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
8443 failed:
8444 Py_XDECREF(tmp);
8445 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008446}
8447
8448int
8449obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
8450{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008451 PyObject* tmp = NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01008452 asdl_seq* posonlyargs;
Pablo Galindocd6e83b2019-07-15 01:32:18 +02008453 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008454 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008455 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008456 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008457 arg_ty kwarg;
8458 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008459
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01008460 if (_PyObject_LookupAttrId(obj, &PyId_posonlyargs, &tmp) < 0) {
8461 return 1;
8462 }
8463 if (tmp == NULL) {
8464 PyErr_SetString(PyExc_TypeError, "required field \"posonlyargs\" missing from arguments");
8465 return 1;
8466 }
8467 else {
8468 int res;
8469 Py_ssize_t len;
8470 Py_ssize_t i;
8471 if (!PyList_Check(tmp)) {
8472 PyErr_Format(PyExc_TypeError, "arguments field \"posonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8473 goto failed;
8474 }
8475 len = PyList_GET_SIZE(tmp);
8476 posonlyargs = _Py_asdl_seq_new(len, arena);
8477 if (posonlyargs == NULL) goto failed;
8478 for (i = 0; i < len; i++) {
8479 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008480 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8481 Py_INCREF(tmp2);
8482 res = obj2ast_arg(tmp2, &val, arena);
8483 Py_DECREF(tmp2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01008484 if (res != 0) goto failed;
8485 if (len != PyList_GET_SIZE(tmp)) {
8486 PyErr_SetString(PyExc_RuntimeError, "arguments field \"posonlyargs\" changed size during iteration");
8487 goto failed;
8488 }
8489 asdl_seq_SET(posonlyargs, i, val);
8490 }
8491 Py_CLEAR(tmp);
8492 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02008493 if (_PyObject_LookupAttrId(obj, &PyId_args, &tmp) < 0) {
8494 return 1;
8495 }
8496 if (tmp == NULL) {
8497 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
8498 return 1;
8499 }
8500 else {
8501 int res;
8502 Py_ssize_t len;
8503 Py_ssize_t i;
8504 if (!PyList_Check(tmp)) {
8505 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8506 goto failed;
8507 }
8508 len = PyList_GET_SIZE(tmp);
8509 args = _Py_asdl_seq_new(len, arena);
8510 if (args == NULL) goto failed;
8511 for (i = 0; i < len; i++) {
8512 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008513 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8514 Py_INCREF(tmp2);
8515 res = obj2ast_arg(tmp2, &val, arena);
8516 Py_DECREF(tmp2);
Pablo Galindocd6e83b2019-07-15 01:32:18 +02008517 if (res != 0) goto failed;
8518 if (len != PyList_GET_SIZE(tmp)) {
8519 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
8520 goto failed;
8521 }
8522 asdl_seq_SET(args, i, val);
8523 }
8524 Py_CLEAR(tmp);
8525 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008526 if (_PyObject_LookupAttrId(obj, &PyId_vararg, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008527 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008528 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008529 if (tmp == NULL || tmp == Py_None) {
8530 Py_CLEAR(tmp);
8531 vararg = NULL;
8532 }
8533 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008534 int res;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008535 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008536 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008537 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008538 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008539 if (_PyObject_LookupAttrId(obj, &PyId_kwonlyargs, &tmp) < 0) {
8540 return 1;
8541 }
8542 if (tmp == NULL) {
8543 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
8544 return 1;
8545 }
8546 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008547 int res;
8548 Py_ssize_t len;
8549 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008550 if (!PyList_Check(tmp)) {
8551 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8552 goto failed;
8553 }
8554 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008555 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008556 if (kwonlyargs == NULL) goto failed;
8557 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008558 arg_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008559 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8560 Py_INCREF(tmp2);
8561 res = obj2ast_arg(tmp2, &val, arena);
8562 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008563 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008564 if (len != PyList_GET_SIZE(tmp)) {
8565 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
8566 goto failed;
8567 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008568 asdl_seq_SET(kwonlyargs, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008569 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008570 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008571 }
8572 if (_PyObject_LookupAttrId(obj, &PyId_kw_defaults, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008573 return 1;
8574 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008575 if (tmp == NULL) {
8576 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
8577 return 1;
8578 }
8579 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008580 int res;
8581 Py_ssize_t len;
8582 Py_ssize_t i;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008583 if (!PyList_Check(tmp)) {
8584 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8585 goto failed;
8586 }
8587 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008588 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008589 if (kw_defaults == NULL) goto failed;
8590 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008591 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008592 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8593 Py_INCREF(tmp2);
8594 res = obj2ast_expr(tmp2, &val, arena);
8595 Py_DECREF(tmp2);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008596 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008597 if (len != PyList_GET_SIZE(tmp)) {
8598 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
8599 goto failed;
8600 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008601 asdl_seq_SET(kw_defaults, i, val);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008602 }
Victor Stinner1acc1292013-07-27 00:03:47 +02008603 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008604 }
8605 if (_PyObject_LookupAttrId(obj, &PyId_kwarg, &tmp) < 0) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008606 return 1;
8607 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008608 if (tmp == NULL || tmp == Py_None) {
8609 Py_CLEAR(tmp);
8610 kwarg = NULL;
8611 }
8612 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02008613 int res;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008614 res = obj2ast_arg(tmp, &kwarg, arena);
8615 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02008616 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008617 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008618 if (_PyObject_LookupAttrId(obj, &PyId_defaults, &tmp) < 0) {
8619 return 1;
8620 }
8621 if (tmp == NULL) {
8622 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
8623 return 1;
8624 }
8625 else {
Victor Stinneree4b59c2013-07-27 00:01:35 +02008626 int res;
8627 Py_ssize_t len;
8628 Py_ssize_t i;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008629 if (!PyList_Check(tmp)) {
8630 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
8631 goto failed;
8632 }
8633 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02008634 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008635 if (defaults == NULL) goto failed;
8636 for (i = 0; i < len; i++) {
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008637 expr_ty val;
Serhiy Storchaka43c97312019-09-10 13:02:30 +03008638 PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
8639 Py_INCREF(tmp2);
8640 res = obj2ast_expr(tmp2, &val, arena);
8641 Py_DECREF(tmp2);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008642 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03008643 if (len != PyList_GET_SIZE(tmp)) {
8644 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
8645 goto failed;
8646 }
Yuan Chao Chou2af565b2017-08-04 10:53:12 -07008647 asdl_seq_SET(defaults, i, val);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008648 }
Victor Stinnerb3189902013-07-27 00:04:42 +02008649 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02008650 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02008651 *out = arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01008652 defaults, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008653 return 0;
8654failed:
8655 Py_XDECREF(tmp);
8656 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008657}
8658
8659int
8660obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
8661{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008662 PyObject* tmp = NULL;
8663 identifier arg;
8664 expr_ty annotation;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008665 string type_comment;
Victor Stinnerc106c682015-11-06 17:01:48 +01008666 int lineno;
8667 int col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008668 int end_lineno;
8669 int end_col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008670
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008671 if (_PyObject_LookupAttrId(obj, &PyId_arg, &tmp) < 0) {
8672 return 1;
8673 }
8674 if (tmp == NULL) {
8675 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
8676 return 1;
8677 }
8678 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008679 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008680 res = obj2ast_identifier(tmp, &arg, arena);
8681 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008682 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008683 }
8684 if (_PyObject_LookupAttrId(obj, &PyId_annotation, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008685 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008686 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008687 if (tmp == NULL || tmp == Py_None) {
8688 Py_CLEAR(tmp);
8689 annotation = NULL;
8690 }
8691 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008692 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008693 res = obj2ast_expr(tmp, &annotation, arena);
8694 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008696 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008697 if (_PyObject_LookupAttrId(obj, &PyId_type_comment, &tmp) < 0) {
8698 return 1;
8699 }
8700 if (tmp == NULL || tmp == Py_None) {
8701 Py_CLEAR(tmp);
8702 type_comment = NULL;
8703 }
8704 else {
8705 int res;
8706 res = obj2ast_string(tmp, &type_comment, arena);
8707 if (res != 0) goto failed;
8708 Py_CLEAR(tmp);
8709 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008710 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8711 return 1;
8712 }
8713 if (tmp == NULL) {
8714 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
8715 return 1;
8716 }
8717 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01008718 int res;
Victor Stinnerc106c682015-11-06 17:01:48 +01008719 res = obj2ast_int(tmp, &lineno, arena);
8720 if (res != 0) goto failed;
8721 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008722 }
8723 if (_PyObject_LookupAttrId(obj, &PyId_col_offset, &tmp) < 0) {
Victor Stinnerc106c682015-11-06 17:01:48 +01008724 return 1;
8725 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008726 if (tmp == NULL) {
8727 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
8728 return 1;
8729 }
8730 else {
Victor Stinnerc106c682015-11-06 17:01:48 +01008731 int res;
Victor Stinnerc106c682015-11-06 17:01:48 +01008732 res = obj2ast_int(tmp, &col_offset, arena);
8733 if (res != 0) goto failed;
8734 Py_CLEAR(tmp);
Victor Stinnerc106c682015-11-06 17:01:48 +01008735 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00008736 if (_PyObject_LookupAttrId(obj, &PyId_end_lineno, &tmp) < 0) {
8737 return 1;
8738 }
8739 if (tmp == NULL || tmp == Py_None) {
8740 Py_CLEAR(tmp);
8741 end_lineno = 0;
8742 }
8743 else {
8744 int res;
8745 res = obj2ast_int(tmp, &end_lineno, arena);
8746 if (res != 0) goto failed;
8747 Py_CLEAR(tmp);
8748 }
8749 if (_PyObject_LookupAttrId(obj, &PyId_end_col_offset, &tmp) < 0) {
8750 return 1;
8751 }
8752 if (tmp == NULL || tmp == Py_None) {
8753 Py_CLEAR(tmp);
8754 end_col_offset = 0;
8755 }
8756 else {
8757 int res;
8758 res = obj2ast_int(tmp, &end_col_offset, arena);
8759 if (res != 0) goto failed;
8760 Py_CLEAR(tmp);
8761 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008762 *out = arg(arg, annotation, type_comment, lineno, col_offset, end_lineno,
8763 end_col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008764 return 0;
8765failed:
8766 Py_XDECREF(tmp);
8767 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008768}
8769
8770int
8771obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
8772{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008773 PyObject* tmp = NULL;
8774 identifier arg;
8775 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008776
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008777 if (_PyObject_LookupAttrId(obj, &PyId_arg, &tmp) < 0) {
8778 return 1;
8779 }
8780 if (tmp == NULL || tmp == Py_None) {
8781 Py_CLEAR(tmp);
8782 arg = NULL;
8783 }
8784 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008785 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008786 res = obj2ast_identifier(tmp, &arg, arena);
8787 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008788 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008789 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008790 if (_PyObject_LookupAttrId(obj, &PyId_value, &tmp) < 0) {
8791 return 1;
8792 }
8793 if (tmp == NULL) {
8794 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
8795 return 1;
8796 }
8797 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008798 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008799 res = obj2ast_expr(tmp, &value, arena);
8800 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008801 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008802 }
8803 *out = keyword(arg, value, arena);
8804 return 0;
8805failed:
8806 Py_XDECREF(tmp);
8807 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008808}
8809
8810int
8811obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
8812{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008813 PyObject* tmp = NULL;
8814 identifier name;
8815 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008816
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008817 if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
8818 return 1;
8819 }
8820 if (tmp == NULL) {
8821 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
8822 return 1;
8823 }
8824 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008825 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008826 res = obj2ast_identifier(tmp, &name, arena);
8827 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008828 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008829 }
8830 if (_PyObject_LookupAttrId(obj, &PyId_asname, &tmp) < 0) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008831 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008832 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008833 if (tmp == NULL || tmp == Py_None) {
8834 Py_CLEAR(tmp);
8835 asname = NULL;
8836 }
8837 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008838 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008839 res = obj2ast_identifier(tmp, &asname, arena);
8840 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008841 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008842 }
8843 *out = alias(name, asname, arena);
8844 return 0;
8845failed:
8846 Py_XDECREF(tmp);
8847 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008848}
8849
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008850int
8851obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
8852{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008853 PyObject* tmp = NULL;
8854 expr_ty context_expr;
8855 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008856
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008857 if (_PyObject_LookupAttrId(obj, &PyId_context_expr, &tmp) < 0) {
8858 return 1;
8859 }
8860 if (tmp == NULL) {
8861 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
8862 return 1;
8863 }
8864 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008865 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008866 res = obj2ast_expr(tmp, &context_expr, arena);
8867 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008868 Py_CLEAR(tmp);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008869 }
8870 if (_PyObject_LookupAttrId(obj, &PyId_optional_vars, &tmp) < 0) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008871 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008872 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02008873 if (tmp == NULL || tmp == Py_None) {
8874 Py_CLEAR(tmp);
8875 optional_vars = NULL;
8876 }
8877 else {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008878 int res;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008879 res = obj2ast_expr(tmp, &optional_vars, arena);
8880 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02008881 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008882 }
8883 *out = withitem(context_expr, optional_vars, arena);
8884 return 0;
8885failed:
8886 Py_XDECREF(tmp);
8887 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05008888}
8889
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008890int
8891obj2ast_type_ignore(PyObject* obj, type_ignore_ty* out, PyArena* arena)
8892{
8893 int isinstance;
8894
8895 PyObject *tmp = NULL;
8896
8897 if (obj == Py_None) {
8898 *out = NULL;
8899 return 0;
8900 }
8901 isinstance = PyObject_IsInstance(obj, (PyObject*)TypeIgnore_type);
8902 if (isinstance == -1) {
8903 return 1;
8904 }
8905 if (isinstance) {
8906 int lineno;
Michael J. Sullivan933e1502019-05-22 07:54:20 -07008907 string tag;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008908
8909 if (_PyObject_LookupAttrId(obj, &PyId_lineno, &tmp) < 0) {
8910 return 1;
8911 }
8912 if (tmp == NULL) {
8913 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from TypeIgnore");
8914 return 1;
8915 }
8916 else {
8917 int res;
8918 res = obj2ast_int(tmp, &lineno, arena);
8919 if (res != 0) goto failed;
8920 Py_CLEAR(tmp);
8921 }
Michael J. Sullivan933e1502019-05-22 07:54:20 -07008922 if (_PyObject_LookupAttrId(obj, &PyId_tag, &tmp) < 0) {
8923 return 1;
8924 }
8925 if (tmp == NULL) {
8926 PyErr_SetString(PyExc_TypeError, "required field \"tag\" missing from TypeIgnore");
8927 return 1;
8928 }
8929 else {
8930 int res;
8931 res = obj2ast_string(tmp, &tag, arena);
8932 if (res != 0) goto failed;
8933 Py_CLEAR(tmp);
8934 }
8935 *out = TypeIgnore(lineno, tag, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008936 if (*out == NULL) goto failed;
8937 return 0;
8938 }
8939
8940 PyErr_Format(PyExc_TypeError, "expected some sort of type_ignore, but got %R", obj);
8941 failed:
8942 Py_XDECREF(tmp);
8943 return 1;
8944}
8945
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00008946
Martin v. Löwis1a214512008-06-11 05:26:20 +00008947static struct PyModuleDef _astmodule = {
8948 PyModuleDef_HEAD_INIT, "_ast"
8949};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008950PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00008951PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00008952{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008953 PyObject *m, *d;
8954 if (!init_types()) return NULL;
8955 m = PyModule_Create(&_astmodule);
8956 if (!m) return NULL;
8957 d = PyModule_GetDict(m);
8958 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07008959 if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0)
8960 return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02008961 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008962 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008963 if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0)
8964 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008965 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
8966 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
8967 NULL;
8968 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
8969 0) return NULL;
8970 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
8971 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08008972 if (PyDict_SetItemString(d, "FunctionType", (PyObject*)FunctionType_type) <
8973 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008974 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
8975 NULL;
8976 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
8977 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
8978 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008979 if (PyDict_SetItemString(d, "AsyncFunctionDef",
8980 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008981 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
8982 return NULL;
8983 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
8984 NULL;
8985 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
8986 NULL;
8987 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
8988 NULL;
8989 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
8990 return NULL;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07008991 if (PyDict_SetItemString(d, "AnnAssign", (PyObject*)AnnAssign_type) < 0)
8992 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008993 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04008994 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
8995 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02008996 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
8997 NULL;
8998 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
8999 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04009000 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
9001 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009002 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
9003 NULL;
9004 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
9005 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
9006 NULL;
9007 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
9008 NULL;
9009 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
9010 return NULL;
9011 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
9012 NULL;
9013 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
9014 return NULL;
9015 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
9016 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
9017 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
9018 NULL;
9019 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
9020 return NULL;
9021 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
9022 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
9023 NULL;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07009024 if (PyDict_SetItemString(d, "NamedExpr", (PyObject*)NamedExpr_type) < 0)
9025 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009026 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
9027 NULL;
9028 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
9029 NULL;
9030 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
9031 NULL;
9032 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
9033 NULL;
9034 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
9035 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
9036 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
9037 return NULL;
9038 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
9039 NULL;
9040 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
9041 return NULL;
9042 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
9043 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04009044 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
9045 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009046 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
9047 NULL;
9048 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
9049 return NULL;
9050 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
9051 NULL;
9052 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04009053 if (PyDict_SetItemString(d, "FormattedValue",
9054 (PyObject*)FormattedValue_type) < 0) return NULL;
9055 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
9056 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01009057 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
9058 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009059 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
9060 return NULL;
9061 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
9062 return NULL;
9063 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
9064 NULL;
9065 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
9066 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
9067 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
9068 NULL;
9069 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
9070 0) return NULL;
9071 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
9072 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
9073 NULL;
9074 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
9075 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
9076 NULL;
9077 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
9078 return NULL;
9079 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
9080 NULL;
9081 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
9082 NULL;
9083 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
9084 NULL;
9085 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
9086 return NULL;
9087 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
9088 NULL;
9089 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
9090 NULL;
9091 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
9092 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
9093 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
9094 return NULL;
9095 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
9096 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
9097 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04009098 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
9099 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009100 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
9101 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
9102 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
9103 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
9104 NULL;
9105 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
9106 NULL;
9107 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
9108 NULL;
9109 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
9110 NULL;
9111 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
9112 NULL;
9113 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
9114 return NULL;
9115 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
9116 NULL;
9117 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
9118 NULL;
9119 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
9120 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
9121 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
9122 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
9123 NULL;
9124 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
9125 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
9126 NULL;
9127 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
9128 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
9129 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
9130 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
9131 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
9132 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
9133 NULL;
9134 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
9135 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
9136 NULL;
9137 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
9138 < 0) return NULL;
9139 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
9140 < 0) return NULL;
9141 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
9142 < 0) return NULL;
9143 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
9144 return NULL;
9145 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
9146 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
9147 NULL;
9148 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
9149 NULL;
9150 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
9151 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08009152 if (PyDict_SetItemString(d, "type_ignore", (PyObject*)type_ignore_type) <
9153 0) return NULL;
9154 if (PyDict_SetItemString(d, "TypeIgnore", (PyObject*)TypeIgnore_type) < 0)
9155 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02009156 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00009157}
9158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009159
Martin v. Löwisbd260da2006-02-26 19:42:26 +00009160PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009161{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02009162 if (!init_types())
9163 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00009164 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00009165}
Martin v. Löwis5b222132007-06-10 09:51:05 +00009166
Neal Norwitzdb4115f2008-03-31 04:20:05 +00009167/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
9168mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009169{
9170 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05009171 PyObject *req_type[3];
Serhiy Storchaka43c97312019-09-10 13:02:30 +03009172 const char * const req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00009173 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05009174
Steve Dowerb82e17e2019-05-23 08:45:22 -07009175 if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
9176 return NULL;
9177 }
9178
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05009179 req_type[0] = (PyObject*)Module_type;
9180 req_type[1] = (PyObject*)Expression_type;
9181 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05009182
Guido van Rossum3a32e3b2019-02-01 11:37:34 -08009183 assert(0 <= mode && mode <= 2);
Neal Norwitzdb4115f2008-03-31 04:20:05 +00009184
Victor Stinnerbdf630c2013-07-17 00:17:15 +02009185 if (!init_types())
9186 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00009187
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00009188 isinstance = PyObject_IsInstance(ast, req_type[mode]);
9189 if (isinstance == -1)
9190 return NULL;
9191 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00009192 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
9193 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009194 return NULL;
9195 }
9196 if (obj2ast_mod(ast, &res, arena) != 0)
9197 return NULL;
9198 else
9199 return res;
9200}
9201
9202int PyAST_Check(PyObject* obj)
9203{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02009204 if (!init_types())
9205 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00009206 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00009207}
9208
Martin v. Löwis5b222132007-06-10 09:51:05 +00009209