blob: ad53ba3585e83d693ebbe53724644a7b80907375 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07003#include <stddef.h>
4
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "Python.h"
6#include "Python-ast.h"
7
Neal Norwitz207c9f32008-03-31 04:42:11 +00008static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +00009static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000010static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000011static PyTypeObject *Module_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020012_Py_IDENTIFIER(body);
Neal Norwitz53d960c2006-02-28 22:47:29 +000013static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020014 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000015};
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *Interactive_type;
17static char *Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020018 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000019};
Neal Norwitz53d960c2006-02-28 22:47:29 +000020static PyTypeObject *Expression_type;
21static char *Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020022 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000023};
Neal Norwitz53d960c2006-02-28 22:47:29 +000024static PyTypeObject *Suite_type;
25static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020026 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000027};
Neal Norwitz53d960c2006-02-28 22:47:29 +000028static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020029_Py_IDENTIFIER(lineno);
30_Py_IDENTIFIER(col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000031static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020032 "lineno",
33 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000034};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000035static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000036static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020037_Py_IDENTIFIER(name);
38_Py_IDENTIFIER(args);
39_Py_IDENTIFIER(decorator_list);
40_Py_IDENTIFIER(returns);
Neal Norwitz53d960c2006-02-28 22:47:29 +000041static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020042 "name",
43 "args",
44 "body",
45 "decorator_list",
46 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Yury Selivanov75445082015-05-11 22:57:16 -040048static PyTypeObject *AsyncFunctionDef_type;
49static char *AsyncFunctionDef_fields[]={
50 "name",
51 "args",
52 "body",
53 "decorator_list",
54 "returns",
55};
Neal Norwitz53d960c2006-02-28 22:47:29 +000056static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020057_Py_IDENTIFIER(bases);
58_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000059static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020060 "name",
61 "bases",
62 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020063 "body",
64 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000065};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020067_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020069 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075};
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static PyTypeObject *Assign_type;
77static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020078 "targets",
79 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(target);
83_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020085 "target",
86 "op",
87 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088};
Neal Norwitz53d960c2006-02-28 22:47:29 +000089static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020090_Py_IDENTIFIER(iter);
91_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020093 "target",
94 "iter",
95 "body",
96 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000097};
Yury Selivanov75445082015-05-11 22:57:16 -040098static PyTypeObject *AsyncFor_type;
99static char *AsyncFor_fields[]={
100 "target",
101 "iter",
102 "body",
103 "orelse",
104};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000105static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200106_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200108 "test",
109 "body",
110 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000111};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000112static PyTypeObject *If_type;
113static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200114 "test",
115 "body",
116 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200119_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000120static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200121 "items",
122 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123};
Yury Selivanov75445082015-05-11 22:57:16 -0400124static PyTypeObject *AsyncWith_type;
125static char *AsyncWith_fields[]={
126 "items",
127 "body",
128};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000129static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200130_Py_IDENTIFIER(exc);
131_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000132static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200133 "exc",
134 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500136static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200137_Py_IDENTIFIER(handlers);
138_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500139static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200140 "body",
141 "handlers",
142 "orelse",
143 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000147static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200148 "test",
149 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000150};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000151static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200152_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000153static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200154 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000155};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200157_Py_IDENTIFIER(module);
158_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200160 "module",
161 "names",
162 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *Global_type;
165static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200166 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000168static PyTypeObject *Nonlocal_type;
169static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200170 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000171};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000172static PyTypeObject *Expr_type;
173static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200174 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000175};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000176static PyTypeObject *Pass_type;
177static PyTypeObject *Break_type;
178static PyTypeObject *Continue_type;
179static PyTypeObject *expr_type;
180static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200181 "lineno",
182 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000183};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200186_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000187static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200188 "op",
189 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000190};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000191static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200192_Py_IDENTIFIER(left);
193_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000194static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200195 "left",
196 "op",
197 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000199static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200200_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200202 "op",
203 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Lambda_type;
206static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200207 "args",
208 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000209};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static PyTypeObject *IfExp_type;
211static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200212 "test",
213 "body",
214 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000215};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000216static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200217_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000218static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200219 "keys",
220 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000222static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200223_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000224static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200225 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200228_Py_IDENTIFIER(elt);
229_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000230static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200231 "elt",
232 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000234static PyTypeObject *SetComp_type;
235static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200236 "elt",
237 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000238};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000239static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200240_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000241static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200242 "key",
243 "value",
244 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000245};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000246static PyTypeObject *GeneratorExp_type;
247static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200248 "elt",
249 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250};
Yury Selivanov75445082015-05-11 22:57:16 -0400251static PyTypeObject *Await_type;
252static char *Await_fields[]={
253 "value",
254};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000255static PyTypeObject *Yield_type;
256static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200257 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500258};
259static PyTypeObject *YieldFrom_type;
260static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200261 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000262};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000263static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200264_Py_IDENTIFIER(ops);
265_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000266static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200267 "left",
268 "ops",
269 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000270};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000271static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200272_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200274 "func",
275 "args",
276 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000277};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200279_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000280static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200281 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000282};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000283static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200284_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200286 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000288static PyTypeObject *Bytes_type;
289static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000291};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500292static PyTypeObject *NameConstant_type;
293static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200294 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500295};
Georg Brandl52318d62006-09-06 07:06:08 +0000296static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000297static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200298_Py_IDENTIFIER(attr);
299_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200301 "value",
302 "attr",
303 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000304};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000305static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200306_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000307static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200308 "value",
309 "slice",
310 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000311};
Guido van Rossum0368b722007-05-11 16:50:42 +0000312static PyTypeObject *Starred_type;
313static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200314 "value",
315 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000316};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000317static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200318_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000319static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200320 "id",
321 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000322};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000323static PyTypeObject *List_type;
324static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200325 "elts",
326 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000327};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000328static PyTypeObject *Tuple_type;
329static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200330 "elts",
331 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
335*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
336static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000337static PyTypeObject *Load_type;
338static PyTypeObject *Store_type;
339static PyTypeObject *Del_type;
340static PyTypeObject *AugLoad_type;
341static PyTypeObject *AugStore_type;
342static PyTypeObject *Param_type;
343static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000345static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200346_Py_IDENTIFIER(lower);
347_Py_IDENTIFIER(upper);
348_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200350 "lower",
351 "upper",
352 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000354static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200355_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200357 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000359static PyTypeObject *Index_type;
360static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200361 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000363static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000364static PyObject *And_singleton, *Or_singleton;
365static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static PyTypeObject *And_type;
367static PyTypeObject *Or_type;
368static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400370*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
371*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
372*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000373static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000374static PyTypeObject *Add_type;
375static PyTypeObject *Sub_type;
376static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400377static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000378static PyTypeObject *Div_type;
379static PyTypeObject *Mod_type;
380static PyTypeObject *Pow_type;
381static PyTypeObject *LShift_type;
382static PyTypeObject *RShift_type;
383static PyTypeObject *BitOr_type;
384static PyTypeObject *BitXor_type;
385static PyTypeObject *BitAnd_type;
386static PyTypeObject *FloorDiv_type;
387static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000388static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
389*USub_singleton;
390static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static PyTypeObject *Invert_type;
392static PyTypeObject *Not_type;
393static PyTypeObject *UAdd_type;
394static PyTypeObject *USub_type;
395static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000396static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
397*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
398*NotIn_singleton;
399static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000400static PyTypeObject *Eq_type;
401static PyTypeObject *NotEq_type;
402static PyTypeObject *Lt_type;
403static PyTypeObject *LtE_type;
404static PyTypeObject *Gt_type;
405static PyTypeObject *GtE_type;
406static PyTypeObject *Is_type;
407static PyTypeObject *IsNot_type;
408static PyTypeObject *In_type;
409static PyTypeObject *NotIn_type;
410static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000411static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200412_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000413static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200414 "target",
415 "iter",
416 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000417};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000418static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000419static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200420 "lineno",
421 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000422};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000423static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000424static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200425_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000426static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200427 "type",
428 "name",
429 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000430};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000431static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000432static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200433_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200434_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200435_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700436_Py_IDENTIFIER(kwarg);
437_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000438static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200439 "args",
440 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200441 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200442 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200443 "kwarg",
444 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000445};
Neal Norwitzc1505362006-12-28 06:47:50 +0000446static PyTypeObject *arg_type;
447static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700448static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200449 "lineno",
450 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700451};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200452_Py_IDENTIFIER(arg);
453_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000454static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200455 "arg",
456 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000457};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000458static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000459static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000460static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200461 "arg",
462 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000463};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000464static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000465static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200466_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000467static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200468 "name",
469 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000470};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500471static PyTypeObject *withitem_type;
472static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200473_Py_IDENTIFIER(context_expr);
474_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500475static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200476 "context_expr",
477 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500478};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000479
480
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700481typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100482 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700483 PyObject *dict;
484} AST_object;
485
Benjamin Peterson1767e022012-03-14 21:50:29 -0500486static void
487ast_dealloc(AST_object *self)
488{
489 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200490 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500491}
492
Neal Norwitz207c9f32008-03-31 04:42:11 +0000493static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700494ast_traverse(AST_object *self, visitproc visit, void *arg)
495{
496 Py_VISIT(self->dict);
497 return 0;
498}
499
500static void
501ast_clear(AST_object *self)
502{
503 Py_CLEAR(self->dict);
504}
505
506static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000507ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
508{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200509 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000510 Py_ssize_t i, numfields = 0;
511 int res = -1;
512 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200513 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000514 if (!fields)
515 PyErr_Clear();
516 if (fields) {
517 numfields = PySequence_Size(fields);
518 if (numfields == -1)
519 goto cleanup;
520 }
521 res = 0; /* if no error occurs, this stays 0 to the end */
522 if (PyTuple_GET_SIZE(args) > 0) {
523 if (numfields != PyTuple_GET_SIZE(args)) {
524 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000525 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000526 Py_TYPE(self)->tp_name,
527 numfields == 0 ? "" : "either 0 or ",
528 numfields, numfields == 1 ? "" : "s");
529 res = -1;
530 goto cleanup;
531 }
532 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
533 /* cannot be reached when fields is NULL */
534 PyObject *name = PySequence_GetItem(fields, i);
535 if (!name) {
536 res = -1;
537 goto cleanup;
538 }
539 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
540 Py_DECREF(name);
541 if (res < 0)
542 goto cleanup;
543 }
544 }
545 if (kw) {
546 i = 0; /* needed by PyDict_Next */
547 while (PyDict_Next(kw, &i, &key, &value)) {
548 res = PyObject_SetAttr(self, key, value);
549 if (res < 0)
550 goto cleanup;
551 }
552 }
553 cleanup:
554 Py_XDECREF(fields);
555 return res;
556}
557
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000558/* Pickling support */
559static PyObject *
560ast_type_reduce(PyObject *self, PyObject *unused)
561{
562 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200563 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200564 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000565 if (dict == NULL) {
566 if (PyErr_ExceptionMatches(PyExc_AttributeError))
567 PyErr_Clear();
568 else
569 return NULL;
570 }
571 if (dict) {
572 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
573 Py_DECREF(dict);
574 return res;
575 }
576 return Py_BuildValue("O()", Py_TYPE(self));
577}
578
579static PyMethodDef ast_type_methods[] = {
580 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
581 {NULL}
582};
583
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700584static PyGetSetDef ast_type_getsets[] = {
585 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
586 {NULL}
587};
588
Neal Norwitz207c9f32008-03-31 04:42:11 +0000589static PyTypeObject AST_type = {
590 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000591 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700592 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000593 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500594 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000595 0, /* tp_print */
596 0, /* tp_getattr */
597 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000598 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000599 0, /* tp_repr */
600 0, /* tp_as_number */
601 0, /* tp_as_sequence */
602 0, /* tp_as_mapping */
603 0, /* tp_hash */
604 0, /* tp_call */
605 0, /* tp_str */
606 PyObject_GenericGetAttr, /* tp_getattro */
607 PyObject_GenericSetAttr, /* tp_setattro */
608 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000610 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700611 (traverseproc)ast_traverse, /* tp_traverse */
612 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000613 0, /* tp_richcompare */
614 0, /* tp_weaklistoffset */
615 0, /* tp_iter */
616 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000617 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000618 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700619 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000620 0, /* tp_base */
621 0, /* tp_dict */
622 0, /* tp_descr_get */
623 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700624 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000625 (initproc)ast_type_init, /* tp_init */
626 PyType_GenericAlloc, /* tp_alloc */
627 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700628 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000629};
630
631
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000632static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
633{
634 PyObject *fnames, *result;
635 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000636 fnames = PyTuple_New(num_fields);
637 if (!fnames) return NULL;
638 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000639 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000640 if (!field) {
641 Py_DECREF(fnames);
642 return NULL;
643 }
644 PyTuple_SET_ITEM(fnames, i, field);
645 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000646 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 Py_DECREF(fnames);
649 return (PyTypeObject*)result;
650}
651
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
653{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200655 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000656 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000657 if (!l)
658 return 0;
659 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000660 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!s) {
662 Py_DECREF(l);
663 return 0;
664 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000665 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200667 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 Py_DECREF(l);
669 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670}
671
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000672/* Conversion AST -> Python */
673
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
675{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700676 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000677 PyObject *result = PyList_New(n);
678 PyObject *value;
679 if (!result)
680 return NULL;
681 for (i = 0; i < n; i++) {
682 value = func(asdl_seq_GET(seq, i));
683 if (!value) {
684 Py_DECREF(result);
685 return NULL;
686 }
687 PyList_SET_ITEM(result, i, value);
688 }
689 return result;
690}
691
692static PyObject* ast2obj_object(void *o)
693{
694 if (!o)
695 o = Py_None;
696 Py_INCREF((PyObject*)o);
697 return (PyObject*)o;
698}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500699#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700#define ast2obj_identifier ast2obj_object
701#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500702#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000704static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705{
Christian Heimes217cfd12007-12-02 14:31:20 +0000706 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707}
708
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000709/* Conversion Python -> AST */
710
Benjamin Peterson442f2092012-12-06 17:41:04 -0500711static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
712{
713 if (obj != Py_None && obj != Py_True && obj != Py_False) {
714 PyErr_SetString(PyExc_ValueError,
715 "AST singleton must be True, False, or None");
716 return 1;
717 }
718 *out = obj;
719 return 0;
720}
721
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000722static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
723{
724 if (obj == Py_None)
725 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200726 if (obj) {
727 if (PyArena_AddPyObject(arena, obj) < 0) {
728 *out = NULL;
729 return -1;
730 }
731 Py_INCREF(obj);
732 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000733 *out = obj;
734 return 0;
735}
736
Benjamin Peterson180e6352011-07-22 11:09:07 -0500737static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500738{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500739 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
740 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500741 return 1;
742 }
743 return obj2ast_object(obj, out, arena);
744}
745
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500746static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
747{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400748 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500749 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
750 return 1;
751 }
752 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500753}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000754
Benjamin Petersone2498412011-08-09 16:08:39 -0500755static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
756{
757 if (!PyBytes_CheckExact(obj)) {
758 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
759 return 1;
760 }
761 return obj2ast_object(obj, out, arena);
762}
763
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000764static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
765{
766 int i;
767 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100768 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000769 return 1;
770 }
771
772 i = (int)PyLong_AsLong(obj);
773 if (i == -1 && PyErr_Occurred())
774 return 1;
775 *out = i;
776 return 0;
777}
778
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000779static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000780{
781 PyObject *empty_tuple, *d;
782 if (PyType_Ready(&AST_type) < 0)
783 return -1;
784 d = AST_type.tp_dict;
785 empty_tuple = PyTuple_New(0);
786 if (!empty_tuple ||
787 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
788 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
789 Py_XDECREF(empty_tuple);
790 return -1;
791 }
792 Py_DECREF(empty_tuple);
793 return 0;
794}
795
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700796static int exists_not_none(PyObject *obj, _Py_Identifier *id)
797{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700798 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700799 PyObject *attr = _PyObject_GetAttrId(obj, id);
800 if (!attr) {
801 PyErr_Clear();
802 return 0;
803 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700804 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700805 Py_DECREF(attr);
806 return !isnone;
807}
808
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810static int init_types(void)
811{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200812 static int initialized;
813 if (initialized) return 1;
814 if (add_ast_fields() < 0) return 0;
815 mod_type = make_type("mod", &AST_type, NULL, 0);
816 if (!mod_type) return 0;
817 if (!add_attributes(mod_type, NULL, 0)) return 0;
818 Module_type = make_type("Module", mod_type, Module_fields, 1);
819 if (!Module_type) return 0;
820 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
821 1);
822 if (!Interactive_type) return 0;
823 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
824 if (!Expression_type) return 0;
825 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
826 if (!Suite_type) return 0;
827 stmt_type = make_type("stmt", &AST_type, NULL, 0);
828 if (!stmt_type) return 0;
829 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
830 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
831 5);
832 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400833 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
834 AsyncFunctionDef_fields, 5);
835 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400836 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200837 if (!ClassDef_type) return 0;
838 Return_type = make_type("Return", stmt_type, Return_fields, 1);
839 if (!Return_type) return 0;
840 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
841 if (!Delete_type) return 0;
842 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
843 if (!Assign_type) return 0;
844 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
845 if (!AugAssign_type) return 0;
846 For_type = make_type("For", stmt_type, For_fields, 4);
847 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400848 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
849 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200850 While_type = make_type("While", stmt_type, While_fields, 3);
851 if (!While_type) return 0;
852 If_type = make_type("If", stmt_type, If_fields, 3);
853 if (!If_type) return 0;
854 With_type = make_type("With", stmt_type, With_fields, 2);
855 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400856 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
857 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200858 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
859 if (!Raise_type) return 0;
860 Try_type = make_type("Try", stmt_type, Try_fields, 4);
861 if (!Try_type) return 0;
862 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
863 if (!Assert_type) return 0;
864 Import_type = make_type("Import", stmt_type, Import_fields, 1);
865 if (!Import_type) return 0;
866 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
867 if (!ImportFrom_type) return 0;
868 Global_type = make_type("Global", stmt_type, Global_fields, 1);
869 if (!Global_type) return 0;
870 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
871 if (!Nonlocal_type) return 0;
872 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
873 if (!Expr_type) return 0;
874 Pass_type = make_type("Pass", stmt_type, NULL, 0);
875 if (!Pass_type) return 0;
876 Break_type = make_type("Break", stmt_type, NULL, 0);
877 if (!Break_type) return 0;
878 Continue_type = make_type("Continue", stmt_type, NULL, 0);
879 if (!Continue_type) return 0;
880 expr_type = make_type("expr", &AST_type, NULL, 0);
881 if (!expr_type) return 0;
882 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
883 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
884 if (!BoolOp_type) return 0;
885 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
886 if (!BinOp_type) return 0;
887 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
888 if (!UnaryOp_type) return 0;
889 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
890 if (!Lambda_type) return 0;
891 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
892 if (!IfExp_type) return 0;
893 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
894 if (!Dict_type) return 0;
895 Set_type = make_type("Set", expr_type, Set_fields, 1);
896 if (!Set_type) return 0;
897 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
898 if (!ListComp_type) return 0;
899 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
900 if (!SetComp_type) return 0;
901 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
902 if (!DictComp_type) return 0;
903 GeneratorExp_type = make_type("GeneratorExp", expr_type,
904 GeneratorExp_fields, 2);
905 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400906 Await_type = make_type("Await", expr_type, Await_fields, 1);
907 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200908 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
909 if (!Yield_type) return 0;
910 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
911 if (!YieldFrom_type) return 0;
912 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
913 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400914 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200915 if (!Call_type) return 0;
916 Num_type = make_type("Num", expr_type, Num_fields, 1);
917 if (!Num_type) return 0;
918 Str_type = make_type("Str", expr_type, Str_fields, 1);
919 if (!Str_type) return 0;
920 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
921 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200922 NameConstant_type = make_type("NameConstant", expr_type,
923 NameConstant_fields, 1);
924 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200925 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
926 if (!Ellipsis_type) return 0;
927 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
928 if (!Attribute_type) return 0;
929 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
930 if (!Subscript_type) return 0;
931 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
932 if (!Starred_type) return 0;
933 Name_type = make_type("Name", expr_type, Name_fields, 2);
934 if (!Name_type) return 0;
935 List_type = make_type("List", expr_type, List_fields, 2);
936 if (!List_type) return 0;
937 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
938 if (!Tuple_type) return 0;
939 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
940 if (!expr_context_type) return 0;
941 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
942 Load_type = make_type("Load", expr_context_type, NULL, 0);
943 if (!Load_type) return 0;
944 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
945 if (!Load_singleton) return 0;
946 Store_type = make_type("Store", expr_context_type, NULL, 0);
947 if (!Store_type) return 0;
948 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
949 if (!Store_singleton) return 0;
950 Del_type = make_type("Del", expr_context_type, NULL, 0);
951 if (!Del_type) return 0;
952 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
953 if (!Del_singleton) return 0;
954 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
955 if (!AugLoad_type) return 0;
956 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
957 if (!AugLoad_singleton) return 0;
958 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
959 if (!AugStore_type) return 0;
960 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
961 if (!AugStore_singleton) return 0;
962 Param_type = make_type("Param", expr_context_type, NULL, 0);
963 if (!Param_type) return 0;
964 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
965 if (!Param_singleton) return 0;
966 slice_type = make_type("slice", &AST_type, NULL, 0);
967 if (!slice_type) return 0;
968 if (!add_attributes(slice_type, NULL, 0)) return 0;
969 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
970 if (!Slice_type) return 0;
971 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
972 if (!ExtSlice_type) return 0;
973 Index_type = make_type("Index", slice_type, Index_fields, 1);
974 if (!Index_type) return 0;
975 boolop_type = make_type("boolop", &AST_type, NULL, 0);
976 if (!boolop_type) return 0;
977 if (!add_attributes(boolop_type, NULL, 0)) return 0;
978 And_type = make_type("And", boolop_type, NULL, 0);
979 if (!And_type) return 0;
980 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
981 if (!And_singleton) return 0;
982 Or_type = make_type("Or", boolop_type, NULL, 0);
983 if (!Or_type) return 0;
984 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
985 if (!Or_singleton) return 0;
986 operator_type = make_type("operator", &AST_type, NULL, 0);
987 if (!operator_type) return 0;
988 if (!add_attributes(operator_type, NULL, 0)) return 0;
989 Add_type = make_type("Add", operator_type, NULL, 0);
990 if (!Add_type) return 0;
991 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
992 if (!Add_singleton) return 0;
993 Sub_type = make_type("Sub", operator_type, NULL, 0);
994 if (!Sub_type) return 0;
995 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
996 if (!Sub_singleton) return 0;
997 Mult_type = make_type("Mult", operator_type, NULL, 0);
998 if (!Mult_type) return 0;
999 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1000 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001001 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1002 if (!MatMult_type) return 0;
1003 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1004 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001005 Div_type = make_type("Div", operator_type, NULL, 0);
1006 if (!Div_type) return 0;
1007 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1008 if (!Div_singleton) return 0;
1009 Mod_type = make_type("Mod", operator_type, NULL, 0);
1010 if (!Mod_type) return 0;
1011 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1012 if (!Mod_singleton) return 0;
1013 Pow_type = make_type("Pow", operator_type, NULL, 0);
1014 if (!Pow_type) return 0;
1015 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1016 if (!Pow_singleton) return 0;
1017 LShift_type = make_type("LShift", operator_type, NULL, 0);
1018 if (!LShift_type) return 0;
1019 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1020 if (!LShift_singleton) return 0;
1021 RShift_type = make_type("RShift", operator_type, NULL, 0);
1022 if (!RShift_type) return 0;
1023 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1024 if (!RShift_singleton) return 0;
1025 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1026 if (!BitOr_type) return 0;
1027 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1028 if (!BitOr_singleton) return 0;
1029 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1030 if (!BitXor_type) return 0;
1031 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1032 if (!BitXor_singleton) return 0;
1033 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1034 if (!BitAnd_type) return 0;
1035 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1036 if (!BitAnd_singleton) return 0;
1037 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1038 if (!FloorDiv_type) return 0;
1039 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1040 if (!FloorDiv_singleton) return 0;
1041 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1042 if (!unaryop_type) return 0;
1043 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1044 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1045 if (!Invert_type) return 0;
1046 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1047 if (!Invert_singleton) return 0;
1048 Not_type = make_type("Not", unaryop_type, NULL, 0);
1049 if (!Not_type) return 0;
1050 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1051 if (!Not_singleton) return 0;
1052 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1053 if (!UAdd_type) return 0;
1054 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1055 if (!UAdd_singleton) return 0;
1056 USub_type = make_type("USub", unaryop_type, NULL, 0);
1057 if (!USub_type) return 0;
1058 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1059 if (!USub_singleton) return 0;
1060 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1061 if (!cmpop_type) return 0;
1062 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1063 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1064 if (!Eq_type) return 0;
1065 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1066 if (!Eq_singleton) return 0;
1067 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1068 if (!NotEq_type) return 0;
1069 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1070 if (!NotEq_singleton) return 0;
1071 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1072 if (!Lt_type) return 0;
1073 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1074 if (!Lt_singleton) return 0;
1075 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1076 if (!LtE_type) return 0;
1077 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1078 if (!LtE_singleton) return 0;
1079 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1080 if (!Gt_type) return 0;
1081 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1082 if (!Gt_singleton) return 0;
1083 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1084 if (!GtE_type) return 0;
1085 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1086 if (!GtE_singleton) return 0;
1087 Is_type = make_type("Is", cmpop_type, NULL, 0);
1088 if (!Is_type) return 0;
1089 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1090 if (!Is_singleton) return 0;
1091 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1092 if (!IsNot_type) return 0;
1093 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1094 if (!IsNot_singleton) return 0;
1095 In_type = make_type("In", cmpop_type, NULL, 0);
1096 if (!In_type) return 0;
1097 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1098 if (!In_singleton) return 0;
1099 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1100 if (!NotIn_type) return 0;
1101 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1102 if (!NotIn_singleton) return 0;
1103 comprehension_type = make_type("comprehension", &AST_type,
1104 comprehension_fields, 3);
1105 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001106 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001107 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1108 if (!excepthandler_type) return 0;
1109 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1110 return 0;
1111 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1112 ExceptHandler_fields, 3);
1113 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001114 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001115 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001116 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001117 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1118 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001119 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001120 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1121 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001122 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001123 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1124 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001125 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001126 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1127 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001128 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001129 initialized = 1;
1130 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001131}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001132
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001133static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1134static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1135static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1136static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1137 arena);
1138static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1139static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1140static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1141static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1142static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1143static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1144 arena);
1145static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1146 arena);
1147static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1148static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1149static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1150static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001151static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001154Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001156 mod_ty p;
1157 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1158 if (!p)
1159 return NULL;
1160 p->kind = Module_kind;
1161 p->v.Module.body = body;
1162 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163}
1164
1165mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001168 mod_ty p;
1169 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1170 if (!p)
1171 return NULL;
1172 p->kind = Interactive_kind;
1173 p->v.Interactive.body = body;
1174 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175}
1176
1177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001178Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001180 mod_ty p;
1181 if (!body) {
1182 PyErr_SetString(PyExc_ValueError,
1183 "field body is required for Expression");
1184 return NULL;
1185 }
1186 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1187 if (!p)
1188 return NULL;
1189 p->kind = Expression_kind;
1190 p->v.Expression.body = body;
1191 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192}
1193
1194mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001197 mod_ty p;
1198 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1199 if (!p)
1200 return NULL;
1201 p->kind = Suite_kind;
1202 p->v.Suite.body = body;
1203 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204}
1205
1206stmt_ty
1207FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001208 decorator_list, expr_ty returns, int lineno, int col_offset,
1209 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001211 stmt_ty p;
1212 if (!name) {
1213 PyErr_SetString(PyExc_ValueError,
1214 "field name is required for FunctionDef");
1215 return NULL;
1216 }
1217 if (!args) {
1218 PyErr_SetString(PyExc_ValueError,
1219 "field args is required for FunctionDef");
1220 return NULL;
1221 }
1222 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1223 if (!p)
1224 return NULL;
1225 p->kind = FunctionDef_kind;
1226 p->v.FunctionDef.name = name;
1227 p->v.FunctionDef.args = args;
1228 p->v.FunctionDef.body = body;
1229 p->v.FunctionDef.decorator_list = decorator_list;
1230 p->v.FunctionDef.returns = returns;
1231 p->lineno = lineno;
1232 p->col_offset = col_offset;
1233 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234}
1235
1236stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001237AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1238 * decorator_list, expr_ty returns, int lineno, int col_offset,
1239 PyArena *arena)
1240{
1241 stmt_ty p;
1242 if (!name) {
1243 PyErr_SetString(PyExc_ValueError,
1244 "field name is required for AsyncFunctionDef");
1245 return NULL;
1246 }
1247 if (!args) {
1248 PyErr_SetString(PyExc_ValueError,
1249 "field args is required for AsyncFunctionDef");
1250 return NULL;
1251 }
1252 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1253 if (!p)
1254 return NULL;
1255 p->kind = AsyncFunctionDef_kind;
1256 p->v.AsyncFunctionDef.name = name;
1257 p->v.AsyncFunctionDef.args = args;
1258 p->v.AsyncFunctionDef.body = body;
1259 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1260 p->v.AsyncFunctionDef.returns = returns;
1261 p->lineno = lineno;
1262 p->col_offset = col_offset;
1263 return p;
1264}
1265
1266stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001267ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1268 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1269 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001271 stmt_ty p;
1272 if (!name) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "field name is required for ClassDef");
1275 return NULL;
1276 }
1277 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1278 if (!p)
1279 return NULL;
1280 p->kind = ClassDef_kind;
1281 p->v.ClassDef.name = name;
1282 p->v.ClassDef.bases = bases;
1283 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001284 p->v.ClassDef.body = body;
1285 p->v.ClassDef.decorator_list = decorator_list;
1286 p->lineno = lineno;
1287 p->col_offset = col_offset;
1288 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001292Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001294 stmt_ty p;
1295 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1296 if (!p)
1297 return NULL;
1298 p->kind = Return_kind;
1299 p->v.Return.value = value;
1300 p->lineno = lineno;
1301 p->col_offset = col_offset;
1302 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303}
1304
1305stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001306Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001308 stmt_ty p;
1309 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1310 if (!p)
1311 return NULL;
1312 p->kind = Delete_kind;
1313 p->v.Delete.targets = targets;
1314 p->lineno = lineno;
1315 p->col_offset = col_offset;
1316 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
1319stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001320Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1321 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001323 stmt_ty p;
1324 if (!value) {
1325 PyErr_SetString(PyExc_ValueError,
1326 "field value is required for Assign");
1327 return NULL;
1328 }
1329 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1330 if (!p)
1331 return NULL;
1332 p->kind = Assign_kind;
1333 p->v.Assign.targets = targets;
1334 p->v.Assign.value = value;
1335 p->lineno = lineno;
1336 p->col_offset = col_offset;
1337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1342 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001344 stmt_ty p;
1345 if (!target) {
1346 PyErr_SetString(PyExc_ValueError,
1347 "field target is required for AugAssign");
1348 return NULL;
1349 }
1350 if (!op) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field op is required for AugAssign");
1353 return NULL;
1354 }
1355 if (!value) {
1356 PyErr_SetString(PyExc_ValueError,
1357 "field value is required for AugAssign");
1358 return NULL;
1359 }
1360 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1361 if (!p)
1362 return NULL;
1363 p->kind = AugAssign_kind;
1364 p->v.AugAssign.target = target;
1365 p->v.AugAssign.op = op;
1366 p->v.AugAssign.value = value;
1367 p->lineno = lineno;
1368 p->col_offset = col_offset;
1369 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370}
1371
1372stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001374 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001376 stmt_ty p;
1377 if (!target) {
1378 PyErr_SetString(PyExc_ValueError,
1379 "field target is required for For");
1380 return NULL;
1381 }
1382 if (!iter) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "field iter is required for For");
1385 return NULL;
1386 }
1387 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1388 if (!p)
1389 return NULL;
1390 p->kind = For_kind;
1391 p->v.For.target = target;
1392 p->v.For.iter = iter;
1393 p->v.For.body = body;
1394 p->v.For.orelse = orelse;
1395 p->lineno = lineno;
1396 p->col_offset = col_offset;
1397 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398}
1399
1400stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001401AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1402 lineno, int col_offset, PyArena *arena)
1403{
1404 stmt_ty p;
1405 if (!target) {
1406 PyErr_SetString(PyExc_ValueError,
1407 "field target is required for AsyncFor");
1408 return NULL;
1409 }
1410 if (!iter) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field iter is required for AsyncFor");
1413 return NULL;
1414 }
1415 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1416 if (!p)
1417 return NULL;
1418 p->kind = AsyncFor_kind;
1419 p->v.AsyncFor.target = target;
1420 p->v.AsyncFor.iter = iter;
1421 p->v.AsyncFor.body = body;
1422 p->v.AsyncFor.orelse = orelse;
1423 p->lineno = lineno;
1424 p->col_offset = col_offset;
1425 return p;
1426}
1427
1428stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001429While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1430 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001432 stmt_ty p;
1433 if (!test) {
1434 PyErr_SetString(PyExc_ValueError,
1435 "field test is required for While");
1436 return NULL;
1437 }
1438 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1439 if (!p)
1440 return NULL;
1441 p->kind = While_kind;
1442 p->v.While.test = test;
1443 p->v.While.body = body;
1444 p->v.While.orelse = orelse;
1445 p->lineno = lineno;
1446 p->col_offset = col_offset;
1447 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448}
1449
1450stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001451If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1452 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001454 stmt_ty p;
1455 if (!test) {
1456 PyErr_SetString(PyExc_ValueError,
1457 "field test is required for If");
1458 return NULL;
1459 }
1460 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1461 if (!p)
1462 return NULL;
1463 p->kind = If_kind;
1464 p->v.If.test = test;
1465 p->v.If.body = body;
1466 p->v.If.orelse = orelse;
1467 p->lineno = lineno;
1468 p->col_offset = col_offset;
1469 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470}
1471
1472stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001473With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1474 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001475{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001476 stmt_ty p;
1477 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1478 if (!p)
1479 return NULL;
1480 p->kind = With_kind;
1481 p->v.With.items = items;
1482 p->v.With.body = body;
1483 p->lineno = lineno;
1484 p->col_offset = col_offset;
1485 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001486}
1487
1488stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001489AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1490 PyArena *arena)
1491{
1492 stmt_ty p;
1493 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1494 if (!p)
1495 return NULL;
1496 p->kind = AsyncWith_kind;
1497 p->v.AsyncWith.items = items;
1498 p->v.AsyncWith.body = body;
1499 p->lineno = lineno;
1500 p->col_offset = col_offset;
1501 return p;
1502}
1503
1504stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001505Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001507 stmt_ty p;
1508 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1509 if (!p)
1510 return NULL;
1511 p->kind = Raise_kind;
1512 p->v.Raise.exc = exc;
1513 p->v.Raise.cause = cause;
1514 p->lineno = lineno;
1515 p->col_offset = col_offset;
1516 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517}
1518
1519stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001520Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1521 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001523 stmt_ty p;
1524 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1525 if (!p)
1526 return NULL;
1527 p->kind = Try_kind;
1528 p->v.Try.body = body;
1529 p->v.Try.handlers = handlers;
1530 p->v.Try.orelse = orelse;
1531 p->v.Try.finalbody = finalbody;
1532 p->lineno = lineno;
1533 p->col_offset = col_offset;
1534 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
1537stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001538Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001540 stmt_ty p;
1541 if (!test) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "field test is required for Assert");
1544 return NULL;
1545 }
1546 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1547 if (!p)
1548 return NULL;
1549 p->kind = Assert_kind;
1550 p->v.Assert.test = test;
1551 p->v.Assert.msg = msg;
1552 p->lineno = lineno;
1553 p->col_offset = col_offset;
1554 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001560 stmt_ty p;
1561 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1562 if (!p)
1563 return NULL;
1564 p->kind = Import_kind;
1565 p->v.Import.names = names;
1566 p->lineno = lineno;
1567 p->col_offset = col_offset;
1568 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
1571stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001572ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1573 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001575 stmt_ty p;
1576 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1577 if (!p)
1578 return NULL;
1579 p->kind = ImportFrom_kind;
1580 p->v.ImportFrom.module = module;
1581 p->v.ImportFrom.names = names;
1582 p->v.ImportFrom.level = level;
1583 p->lineno = lineno;
1584 p->col_offset = col_offset;
1585 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
1588stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001591 stmt_ty p;
1592 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1593 if (!p)
1594 return NULL;
1595 p->kind = Global_kind;
1596 p->v.Global.names = names;
1597 p->lineno = lineno;
1598 p->col_offset = col_offset;
1599 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001603Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1604{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001605 stmt_ty p;
1606 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1607 if (!p)
1608 return NULL;
1609 p->kind = Nonlocal_kind;
1610 p->v.Nonlocal.names = names;
1611 p->lineno = lineno;
1612 p->col_offset = col_offset;
1613 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001614}
1615
1616stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001617Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001619 stmt_ty p;
1620 if (!value) {
1621 PyErr_SetString(PyExc_ValueError,
1622 "field value is required for Expr");
1623 return NULL;
1624 }
1625 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1626 if (!p)
1627 return NULL;
1628 p->kind = Expr_kind;
1629 p->v.Expr.value = value;
1630 p->lineno = lineno;
1631 p->col_offset = col_offset;
1632 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633}
1634
1635stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001636Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001638 stmt_ty p;
1639 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1640 if (!p)
1641 return NULL;
1642 p->kind = Pass_kind;
1643 p->lineno = lineno;
1644 p->col_offset = col_offset;
1645 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001651 stmt_ty p;
1652 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1653 if (!p)
1654 return NULL;
1655 p->kind = Break_kind;
1656 p->lineno = lineno;
1657 p->col_offset = col_offset;
1658 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
1661stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001662Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001664 stmt_ty p;
1665 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1666 if (!p)
1667 return NULL;
1668 p->kind = Continue_kind;
1669 p->lineno = lineno;
1670 p->col_offset = col_offset;
1671 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672}
1673
1674expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001675BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1676 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001678 expr_ty p;
1679 if (!op) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field op is required for BoolOp");
1682 return NULL;
1683 }
1684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1685 if (!p)
1686 return NULL;
1687 p->kind = BoolOp_kind;
1688 p->v.BoolOp.op = op;
1689 p->v.BoolOp.values = values;
1690 p->lineno = lineno;
1691 p->col_offset = col_offset;
1692 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693}
1694
1695expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1697 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001699 expr_ty p;
1700 if (!left) {
1701 PyErr_SetString(PyExc_ValueError,
1702 "field left is required for BinOp");
1703 return NULL;
1704 }
1705 if (!op) {
1706 PyErr_SetString(PyExc_ValueError,
1707 "field op is required for BinOp");
1708 return NULL;
1709 }
1710 if (!right) {
1711 PyErr_SetString(PyExc_ValueError,
1712 "field right is required for BinOp");
1713 return NULL;
1714 }
1715 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1716 if (!p)
1717 return NULL;
1718 p->kind = BinOp_kind;
1719 p->v.BinOp.left = left;
1720 p->v.BinOp.op = op;
1721 p->v.BinOp.right = right;
1722 p->lineno = lineno;
1723 p->col_offset = col_offset;
1724 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
1727expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1729 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001731 expr_ty p;
1732 if (!op) {
1733 PyErr_SetString(PyExc_ValueError,
1734 "field op is required for UnaryOp");
1735 return NULL;
1736 }
1737 if (!operand) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "field operand is required for UnaryOp");
1740 return NULL;
1741 }
1742 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1743 if (!p)
1744 return NULL;
1745 p->kind = UnaryOp_kind;
1746 p->v.UnaryOp.op = op;
1747 p->v.UnaryOp.operand = operand;
1748 p->lineno = lineno;
1749 p->col_offset = col_offset;
1750 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751}
1752
1753expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001754Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1755 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001757 expr_ty p;
1758 if (!args) {
1759 PyErr_SetString(PyExc_ValueError,
1760 "field args is required for Lambda");
1761 return NULL;
1762 }
1763 if (!body) {
1764 PyErr_SetString(PyExc_ValueError,
1765 "field body is required for Lambda");
1766 return NULL;
1767 }
1768 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1769 if (!p)
1770 return NULL;
1771 p->kind = Lambda_kind;
1772 p->v.Lambda.args = args;
1773 p->v.Lambda.body = body;
1774 p->lineno = lineno;
1775 p->col_offset = col_offset;
1776 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777}
1778
1779expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001780IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1781 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001782{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001783 expr_ty p;
1784 if (!test) {
1785 PyErr_SetString(PyExc_ValueError,
1786 "field test is required for IfExp");
1787 return NULL;
1788 }
1789 if (!body) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field body is required for IfExp");
1792 return NULL;
1793 }
1794 if (!orelse) {
1795 PyErr_SetString(PyExc_ValueError,
1796 "field orelse is required for IfExp");
1797 return NULL;
1798 }
1799 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1800 if (!p)
1801 return NULL;
1802 p->kind = IfExp_kind;
1803 p->v.IfExp.test = test;
1804 p->v.IfExp.body = body;
1805 p->v.IfExp.orelse = orelse;
1806 p->lineno = lineno;
1807 p->col_offset = col_offset;
1808 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001809}
1810
1811expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001812Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1813 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001815 expr_ty p;
1816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1817 if (!p)
1818 return NULL;
1819 p->kind = Dict_kind;
1820 p->v.Dict.keys = keys;
1821 p->v.Dict.values = values;
1822 p->lineno = lineno;
1823 p->col_offset = col_offset;
1824 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825}
1826
1827expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001828Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1829{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001830 expr_ty p;
1831 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1832 if (!p)
1833 return NULL;
1834 p->kind = Set_kind;
1835 p->v.Set.elts = elts;
1836 p->lineno = lineno;
1837 p->col_offset = col_offset;
1838 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001839}
1840
1841expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001842ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1843 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001845 expr_ty p;
1846 if (!elt) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field elt is required for ListComp");
1849 return NULL;
1850 }
1851 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1852 if (!p)
1853 return NULL;
1854 p->kind = ListComp_kind;
1855 p->v.ListComp.elt = elt;
1856 p->v.ListComp.generators = generators;
1857 p->lineno = lineno;
1858 p->col_offset = col_offset;
1859 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860}
1861
1862expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001863SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1864 *arena)
1865{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001866 expr_ty p;
1867 if (!elt) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field elt is required for SetComp");
1870 return NULL;
1871 }
1872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1873 if (!p)
1874 return NULL;
1875 p->kind = SetComp_kind;
1876 p->v.SetComp.elt = elt;
1877 p->v.SetComp.generators = generators;
1878 p->lineno = lineno;
1879 p->col_offset = col_offset;
1880 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001881}
1882
1883expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001884DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1885 col_offset, PyArena *arena)
1886{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001887 expr_ty p;
1888 if (!key) {
1889 PyErr_SetString(PyExc_ValueError,
1890 "field key is required for DictComp");
1891 return NULL;
1892 }
1893 if (!value) {
1894 PyErr_SetString(PyExc_ValueError,
1895 "field value is required for DictComp");
1896 return NULL;
1897 }
1898 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1899 if (!p)
1900 return NULL;
1901 p->kind = DictComp_kind;
1902 p->v.DictComp.key = key;
1903 p->v.DictComp.value = value;
1904 p->v.DictComp.generators = generators;
1905 p->lineno = lineno;
1906 p->col_offset = col_offset;
1907 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908}
1909
1910expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001911GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1912 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001914 expr_ty p;
1915 if (!elt) {
1916 PyErr_SetString(PyExc_ValueError,
1917 "field elt is required for GeneratorExp");
1918 return NULL;
1919 }
1920 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1921 if (!p)
1922 return NULL;
1923 p->kind = GeneratorExp_kind;
1924 p->v.GeneratorExp.elt = elt;
1925 p->v.GeneratorExp.generators = generators;
1926 p->lineno = lineno;
1927 p->col_offset = col_offset;
1928 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
1931expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001932Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
1933{
1934 expr_ty p;
1935 if (!value) {
1936 PyErr_SetString(PyExc_ValueError,
1937 "field value is required for Await");
1938 return NULL;
1939 }
1940 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1941 if (!p)
1942 return NULL;
1943 p->kind = Await_kind;
1944 p->v.Await.value = value;
1945 p->lineno = lineno;
1946 p->col_offset = col_offset;
1947 return p;
1948}
1949
1950expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001951Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001953 expr_ty p;
1954 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1955 if (!p)
1956 return NULL;
1957 p->kind = Yield_kind;
1958 p->v.Yield.value = value;
1959 p->lineno = lineno;
1960 p->col_offset = col_offset;
1961 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962}
1963
1964expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001965YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1966{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001967 expr_ty p;
1968 if (!value) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field value is required for YieldFrom");
1971 return NULL;
1972 }
1973 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1974 if (!p)
1975 return NULL;
1976 p->kind = YieldFrom_kind;
1977 p->v.YieldFrom.value = value;
1978 p->lineno = lineno;
1979 p->col_offset = col_offset;
1980 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001981}
1982
1983expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001984Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1985 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001987 expr_ty p;
1988 if (!left) {
1989 PyErr_SetString(PyExc_ValueError,
1990 "field left is required for Compare");
1991 return NULL;
1992 }
1993 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1994 if (!p)
1995 return NULL;
1996 p->kind = Compare_kind;
1997 p->v.Compare.left = left;
1998 p->v.Compare.ops = ops;
1999 p->v.Compare.comparators = comparators;
2000 p->lineno = lineno;
2001 p->col_offset = col_offset;
2002 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003}
2004
2005expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2007 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002009 expr_ty p;
2010 if (!func) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "field func is required for Call");
2013 return NULL;
2014 }
2015 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2016 if (!p)
2017 return NULL;
2018 p->kind = Call_kind;
2019 p->v.Call.func = func;
2020 p->v.Call.args = args;
2021 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002022 p->lineno = lineno;
2023 p->col_offset = col_offset;
2024 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002028Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002030 expr_ty p;
2031 if (!n) {
2032 PyErr_SetString(PyExc_ValueError,
2033 "field n is required for Num");
2034 return NULL;
2035 }
2036 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2037 if (!p)
2038 return NULL;
2039 p->kind = Num_kind;
2040 p->v.Num.n = n;
2041 p->lineno = lineno;
2042 p->col_offset = col_offset;
2043 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002047Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002049 expr_ty p;
2050 if (!s) {
2051 PyErr_SetString(PyExc_ValueError,
2052 "field s is required for Str");
2053 return NULL;
2054 }
2055 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2056 if (!p)
2057 return NULL;
2058 p->kind = Str_kind;
2059 p->v.Str.s = s;
2060 p->lineno = lineno;
2061 p->col_offset = col_offset;
2062 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063}
2064
2065expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002066Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002067{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002068 expr_ty p;
2069 if (!s) {
2070 PyErr_SetString(PyExc_ValueError,
2071 "field s is required for Bytes");
2072 return NULL;
2073 }
2074 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2075 if (!p)
2076 return NULL;
2077 p->kind = Bytes_kind;
2078 p->v.Bytes.s = s;
2079 p->lineno = lineno;
2080 p->col_offset = col_offset;
2081 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002082}
2083
2084expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002085NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2086{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002087 expr_ty p;
2088 if (!value) {
2089 PyErr_SetString(PyExc_ValueError,
2090 "field value is required for NameConstant");
2091 return NULL;
2092 }
2093 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2094 if (!p)
2095 return NULL;
2096 p->kind = NameConstant_kind;
2097 p->v.NameConstant.value = value;
2098 p->lineno = lineno;
2099 p->col_offset = col_offset;
2100 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002101}
2102
2103expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002104Ellipsis(int lineno, int col_offset, PyArena *arena)
2105{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002106 expr_ty p;
2107 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2108 if (!p)
2109 return NULL;
2110 p->kind = Ellipsis_kind;
2111 p->lineno = lineno;
2112 p->col_offset = col_offset;
2113 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002114}
2115
2116expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2118 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002120 expr_ty p;
2121 if (!value) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "field value is required for Attribute");
2124 return NULL;
2125 }
2126 if (!attr) {
2127 PyErr_SetString(PyExc_ValueError,
2128 "field attr is required for Attribute");
2129 return NULL;
2130 }
2131 if (!ctx) {
2132 PyErr_SetString(PyExc_ValueError,
2133 "field ctx is required for Attribute");
2134 return NULL;
2135 }
2136 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2137 if (!p)
2138 return NULL;
2139 p->kind = Attribute_kind;
2140 p->v.Attribute.value = value;
2141 p->v.Attribute.attr = attr;
2142 p->v.Attribute.ctx = ctx;
2143 p->lineno = lineno;
2144 p->col_offset = col_offset;
2145 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146}
2147
2148expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2150 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002152 expr_ty p;
2153 if (!value) {
2154 PyErr_SetString(PyExc_ValueError,
2155 "field value is required for Subscript");
2156 return NULL;
2157 }
2158 if (!slice) {
2159 PyErr_SetString(PyExc_ValueError,
2160 "field slice is required for Subscript");
2161 return NULL;
2162 }
2163 if (!ctx) {
2164 PyErr_SetString(PyExc_ValueError,
2165 "field ctx is required for Subscript");
2166 return NULL;
2167 }
2168 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2169 if (!p)
2170 return NULL;
2171 p->kind = Subscript_kind;
2172 p->v.Subscript.value = value;
2173 p->v.Subscript.slice = slice;
2174 p->v.Subscript.ctx = ctx;
2175 p->lineno = lineno;
2176 p->col_offset = col_offset;
2177 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178}
2179
2180expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002181Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2182 *arena)
2183{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002184 expr_ty p;
2185 if (!value) {
2186 PyErr_SetString(PyExc_ValueError,
2187 "field value is required for Starred");
2188 return NULL;
2189 }
2190 if (!ctx) {
2191 PyErr_SetString(PyExc_ValueError,
2192 "field ctx is required for Starred");
2193 return NULL;
2194 }
2195 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2196 if (!p)
2197 return NULL;
2198 p->kind = Starred_kind;
2199 p->v.Starred.value = value;
2200 p->v.Starred.ctx = ctx;
2201 p->lineno = lineno;
2202 p->col_offset = col_offset;
2203 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002204}
2205
2206expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002207Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2208 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002210 expr_ty p;
2211 if (!id) {
2212 PyErr_SetString(PyExc_ValueError,
2213 "field id is required for Name");
2214 return NULL;
2215 }
2216 if (!ctx) {
2217 PyErr_SetString(PyExc_ValueError,
2218 "field ctx is required for Name");
2219 return NULL;
2220 }
2221 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2222 if (!p)
2223 return NULL;
2224 p->kind = Name_kind;
2225 p->v.Name.id = id;
2226 p->v.Name.ctx = ctx;
2227 p->lineno = lineno;
2228 p->col_offset = col_offset;
2229 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
2232expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002233List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2234 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002236 expr_ty p;
2237 if (!ctx) {
2238 PyErr_SetString(PyExc_ValueError,
2239 "field ctx is required for List");
2240 return NULL;
2241 }
2242 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2243 if (!p)
2244 return NULL;
2245 p->kind = List_kind;
2246 p->v.List.elts = elts;
2247 p->v.List.ctx = ctx;
2248 p->lineno = lineno;
2249 p->col_offset = col_offset;
2250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
2253expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002254Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2255 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002257 expr_ty p;
2258 if (!ctx) {
2259 PyErr_SetString(PyExc_ValueError,
2260 "field ctx is required for Tuple");
2261 return NULL;
2262 }
2263 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2264 if (!p)
2265 return NULL;
2266 p->kind = Tuple_kind;
2267 p->v.Tuple.elts = elts;
2268 p->v.Tuple.ctx = ctx;
2269 p->lineno = lineno;
2270 p->col_offset = col_offset;
2271 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272}
2273
2274slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002275Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002277 slice_ty p;
2278 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2279 if (!p)
2280 return NULL;
2281 p->kind = Slice_kind;
2282 p->v.Slice.lower = lower;
2283 p->v.Slice.upper = upper;
2284 p->v.Slice.step = step;
2285 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286}
2287
2288slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002291 slice_ty p;
2292 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2293 if (!p)
2294 return NULL;
2295 p->kind = ExtSlice_kind;
2296 p->v.ExtSlice.dims = dims;
2297 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298}
2299
2300slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002301Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002303 slice_ty p;
2304 if (!value) {
2305 PyErr_SetString(PyExc_ValueError,
2306 "field value is required for Index");
2307 return NULL;
2308 }
2309 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2310 if (!p)
2311 return NULL;
2312 p->kind = Index_kind;
2313 p->v.Index.value = value;
2314 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315}
2316
2317comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002318comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002320 comprehension_ty p;
2321 if (!target) {
2322 PyErr_SetString(PyExc_ValueError,
2323 "field target is required for comprehension");
2324 return NULL;
2325 }
2326 if (!iter) {
2327 PyErr_SetString(PyExc_ValueError,
2328 "field iter is required for comprehension");
2329 return NULL;
2330 }
2331 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2332 if (!p)
2333 return NULL;
2334 p->target = target;
2335 p->iter = iter;
2336 p->ifs = ifs;
2337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338}
2339
2340excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002341ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002344 excepthandler_ty p;
2345 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2346 if (!p)
2347 return NULL;
2348 p->kind = ExceptHandler_kind;
2349 p->v.ExceptHandler.type = type;
2350 p->v.ExceptHandler.name = name;
2351 p->v.ExceptHandler.body = body;
2352 p->lineno = lineno;
2353 p->col_offset = col_offset;
2354 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355}
2356
2357arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002358arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2359 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002361 arguments_ty p;
2362 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2363 if (!p)
2364 return NULL;
2365 p->args = args;
2366 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002367 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002368 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002369 p->kwarg = kwarg;
2370 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002371 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372}
2373
Neal Norwitzc1505362006-12-28 06:47:50 +00002374arg_ty
Victor Stinnerf9827ea2015-11-06 17:01:48 +01002375arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2376 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002377{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002378 arg_ty p;
2379 if (!arg) {
2380 PyErr_SetString(PyExc_ValueError,
2381 "field arg is required for arg");
2382 return NULL;
2383 }
2384 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2385 if (!p)
2386 return NULL;
2387 p->arg = arg;
2388 p->annotation = annotation;
Victor Stinnerf9827ea2015-11-06 17:01:48 +01002389 p->lineno = lineno;
2390 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002391 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002392}
2393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002395keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002397 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002398 if (!value) {
2399 PyErr_SetString(PyExc_ValueError,
2400 "field value is required for keyword");
2401 return NULL;
2402 }
2403 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2404 if (!p)
2405 return NULL;
2406 p->arg = arg;
2407 p->value = value;
2408 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409}
2410
2411alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002412alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002414 alias_ty p;
2415 if (!name) {
2416 PyErr_SetString(PyExc_ValueError,
2417 "field name is required for alias");
2418 return NULL;
2419 }
2420 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2421 if (!p)
2422 return NULL;
2423 p->name = name;
2424 p->asname = asname;
2425 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426}
2427
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002428withitem_ty
2429withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2430{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002431 withitem_ty p;
2432 if (!context_expr) {
2433 PyErr_SetString(PyExc_ValueError,
2434 "field context_expr is required for withitem");
2435 return NULL;
2436 }
2437 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2438 if (!p)
2439 return NULL;
2440 p->context_expr = context_expr;
2441 p->optional_vars = optional_vars;
2442 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002443}
2444
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002445
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002446PyObject*
2447ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002449 mod_ty o = (mod_ty)_o;
2450 PyObject *result = NULL, *value = NULL;
2451 if (!o) {
2452 Py_INCREF(Py_None);
2453 return Py_None;
2454 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002455
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002456 switch (o->kind) {
2457 case Module_kind:
2458 result = PyType_GenericNew(Module_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2461 if (!value) goto failed;
2462 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 break;
2466 case Interactive_kind:
2467 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2468 if (!result) goto failed;
2469 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2470 if (!value) goto failed;
2471 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
2474 break;
2475 case Expression_kind:
2476 result = PyType_GenericNew(Expression_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_expr(o->v.Expression.body);
2479 if (!value) goto failed;
2480 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 break;
2484 case Suite_kind:
2485 result = PyType_GenericNew(Suite_type, NULL, NULL);
2486 if (!result) goto failed;
2487 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2488 if (!value) goto failed;
2489 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 break;
2493 }
2494 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002495failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002496 Py_XDECREF(value);
2497 Py_XDECREF(result);
2498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499}
2500
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002501PyObject*
2502ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002504 stmt_ty o = (stmt_ty)_o;
2505 PyObject *result = NULL, *value = NULL;
2506 if (!o) {
2507 Py_INCREF(Py_None);
2508 return Py_None;
2509 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002510
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002511 switch (o->kind) {
2512 case FunctionDef_kind:
2513 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2514 if (!result) goto failed;
2515 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002516 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002517 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2518 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002519 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002520 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002521 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002522 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2523 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002524 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002525 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2526 if (!value) goto failed;
2527 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2531 if (!value) goto failed;
2532 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 value = ast2obj_expr(o->v.FunctionDef.returns);
2536 if (!value) goto failed;
2537 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2538 goto failed;
2539 Py_DECREF(value);
2540 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002541 case AsyncFunctionDef_kind:
2542 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2543 if (!result) goto failed;
2544 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2545 if (!value) goto failed;
2546 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2550 if (!value) goto failed;
2551 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2555 if (!value) goto failed;
2556 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2557 goto failed;
2558 Py_DECREF(value);
2559 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2560 ast2obj_expr);
2561 if (!value) goto failed;
2562 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2563 goto failed;
2564 Py_DECREF(value);
2565 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2566 if (!value) goto failed;
2567 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2568 goto failed;
2569 Py_DECREF(value);
2570 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002571 case ClassDef_kind:
2572 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2573 if (!result) goto failed;
2574 value = ast2obj_identifier(o->v.ClassDef.name);
2575 if (!value) goto failed;
2576 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2577 goto failed;
2578 Py_DECREF(value);
2579 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2580 if (!value) goto failed;
2581 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2582 goto failed;
2583 Py_DECREF(value);
2584 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2585 if (!value) goto failed;
2586 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002589 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2590 if (!value) goto failed;
2591 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 break;
2600 case Return_kind:
2601 result = PyType_GenericNew(Return_type, NULL, NULL);
2602 if (!result) goto failed;
2603 value = ast2obj_expr(o->v.Return.value);
2604 if (!value) goto failed;
2605 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 break;
2609 case Delete_kind:
2610 result = PyType_GenericNew(Delete_type, NULL, NULL);
2611 if (!result) goto failed;
2612 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2613 if (!value) goto failed;
2614 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
2617 break;
2618 case Assign_kind:
2619 result = PyType_GenericNew(Assign_type, NULL, NULL);
2620 if (!result) goto failed;
2621 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2622 if (!value) goto failed;
2623 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
2626 value = ast2obj_expr(o->v.Assign.value);
2627 if (!value) goto failed;
2628 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
2631 break;
2632 case AugAssign_kind:
2633 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2634 if (!result) goto failed;
2635 value = ast2obj_expr(o->v.AugAssign.target);
2636 if (!value) goto failed;
2637 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2638 goto failed;
2639 Py_DECREF(value);
2640 value = ast2obj_operator(o->v.AugAssign.op);
2641 if (!value) goto failed;
2642 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
2645 value = ast2obj_expr(o->v.AugAssign.value);
2646 if (!value) goto failed;
2647 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2648 goto failed;
2649 Py_DECREF(value);
2650 break;
2651 case For_kind:
2652 result = PyType_GenericNew(For_type, NULL, NULL);
2653 if (!result) goto failed;
2654 value = ast2obj_expr(o->v.For.target);
2655 if (!value) goto failed;
2656 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2657 goto failed;
2658 Py_DECREF(value);
2659 value = ast2obj_expr(o->v.For.iter);
2660 if (!value) goto failed;
2661 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2665 if (!value) goto failed;
2666 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2670 if (!value) goto failed;
2671 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002675 case AsyncFor_kind:
2676 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_expr(o->v.AsyncFor.target);
2679 if (!value) goto failed;
2680 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_expr(o->v.AsyncFor.iter);
2684 if (!value) goto failed;
2685 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
2688 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2694 if (!value) goto failed;
2695 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2696 goto failed;
2697 Py_DECREF(value);
2698 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002699 case While_kind:
2700 result = PyType_GenericNew(While_type, NULL, NULL);
2701 if (!result) goto failed;
2702 value = ast2obj_expr(o->v.While.test);
2703 if (!value) goto failed;
2704 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
2707 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2708 if (!value) goto failed;
2709 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2710 goto failed;
2711 Py_DECREF(value);
2712 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2713 if (!value) goto failed;
2714 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 break;
2718 case If_kind:
2719 result = PyType_GenericNew(If_type, NULL, NULL);
2720 if (!result) goto failed;
2721 value = ast2obj_expr(o->v.If.test);
2722 if (!value) goto failed;
2723 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2727 if (!value) goto failed;
2728 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
2731 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2732 if (!value) goto failed;
2733 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 break;
2737 case With_kind:
2738 result = PyType_GenericNew(With_type, NULL, NULL);
2739 if (!result) goto failed;
2740 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2741 if (!value) goto failed;
2742 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2743 goto failed;
2744 Py_DECREF(value);
2745 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2746 if (!value) goto failed;
2747 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
2750 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002751 case AsyncWith_kind:
2752 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2753 if (!result) goto failed;
2754 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2755 if (!value) goto failed;
2756 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
2759 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2760 if (!value) goto failed;
2761 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002765 case Raise_kind:
2766 result = PyType_GenericNew(Raise_type, NULL, NULL);
2767 if (!result) goto failed;
2768 value = ast2obj_expr(o->v.Raise.exc);
2769 if (!value) goto failed;
2770 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 value = ast2obj_expr(o->v.Raise.cause);
2774 if (!value) goto failed;
2775 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 break;
2779 case Try_kind:
2780 result = PyType_GenericNew(Try_type, NULL, NULL);
2781 if (!result) goto failed;
2782 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2783 if (!value) goto failed;
2784 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2788 if (!value) goto failed;
2789 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2793 if (!value) goto failed;
2794 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2798 if (!value) goto failed;
2799 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
2802 break;
2803 case Assert_kind:
2804 result = PyType_GenericNew(Assert_type, NULL, NULL);
2805 if (!result) goto failed;
2806 value = ast2obj_expr(o->v.Assert.test);
2807 if (!value) goto failed;
2808 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2809 goto failed;
2810 Py_DECREF(value);
2811 value = ast2obj_expr(o->v.Assert.msg);
2812 if (!value) goto failed;
2813 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2814 goto failed;
2815 Py_DECREF(value);
2816 break;
2817 case Import_kind:
2818 result = PyType_GenericNew(Import_type, NULL, NULL);
2819 if (!result) goto failed;
2820 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2821 if (!value) goto failed;
2822 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
2825 break;
2826 case ImportFrom_kind:
2827 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2828 if (!result) goto failed;
2829 value = ast2obj_identifier(o->v.ImportFrom.module);
2830 if (!value) goto failed;
2831 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2835 if (!value) goto failed;
2836 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2837 goto failed;
2838 Py_DECREF(value);
2839 value = ast2obj_int(o->v.ImportFrom.level);
2840 if (!value) goto failed;
2841 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2842 goto failed;
2843 Py_DECREF(value);
2844 break;
2845 case Global_kind:
2846 result = PyType_GenericNew(Global_type, NULL, NULL);
2847 if (!result) goto failed;
2848 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2849 if (!value) goto failed;
2850 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 break;
2854 case Nonlocal_kind:
2855 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2856 if (!result) goto failed;
2857 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2858 if (!value) goto failed;
2859 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 break;
2863 case Expr_kind:
2864 result = PyType_GenericNew(Expr_type, NULL, NULL);
2865 if (!result) goto failed;
2866 value = ast2obj_expr(o->v.Expr.value);
2867 if (!value) goto failed;
2868 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 break;
2872 case Pass_kind:
2873 result = PyType_GenericNew(Pass_type, NULL, NULL);
2874 if (!result) goto failed;
2875 break;
2876 case Break_kind:
2877 result = PyType_GenericNew(Break_type, NULL, NULL);
2878 if (!result) goto failed;
2879 break;
2880 case Continue_kind:
2881 result = PyType_GenericNew(Continue_type, NULL, NULL);
2882 if (!result) goto failed;
2883 break;
2884 }
2885 value = ast2obj_int(o->lineno);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_int(o->col_offset);
2891 if (!value) goto failed;
2892 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2893 goto failed;
2894 Py_DECREF(value);
2895 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002896failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002897 Py_XDECREF(value);
2898 Py_XDECREF(result);
2899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
2901
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002902PyObject*
2903ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002905 expr_ty o = (expr_ty)_o;
2906 PyObject *result = NULL, *value = NULL;
2907 if (!o) {
2908 Py_INCREF(Py_None);
2909 return Py_None;
2910 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002911
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002912 switch (o->kind) {
2913 case BoolOp_kind:
2914 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2915 if (!result) goto failed;
2916 value = ast2obj_boolop(o->v.BoolOp.op);
2917 if (!value) goto failed;
2918 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
2921 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2922 if (!value) goto failed;
2923 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
2926 break;
2927 case BinOp_kind:
2928 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2929 if (!result) goto failed;
2930 value = ast2obj_expr(o->v.BinOp.left);
2931 if (!value) goto failed;
2932 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2933 goto failed;
2934 Py_DECREF(value);
2935 value = ast2obj_operator(o->v.BinOp.op);
2936 if (!value) goto failed;
2937 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2938 goto failed;
2939 Py_DECREF(value);
2940 value = ast2obj_expr(o->v.BinOp.right);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 break;
2946 case UnaryOp_kind:
2947 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2948 if (!result) goto failed;
2949 value = ast2obj_unaryop(o->v.UnaryOp.op);
2950 if (!value) goto failed;
2951 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2952 goto failed;
2953 Py_DECREF(value);
2954 value = ast2obj_expr(o->v.UnaryOp.operand);
2955 if (!value) goto failed;
2956 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2957 goto failed;
2958 Py_DECREF(value);
2959 break;
2960 case Lambda_kind:
2961 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2962 if (!result) goto failed;
2963 value = ast2obj_arguments(o->v.Lambda.args);
2964 if (!value) goto failed;
2965 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 value = ast2obj_expr(o->v.Lambda.body);
2969 if (!value) goto failed;
2970 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 break;
2974 case IfExp_kind:
2975 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2976 if (!result) goto failed;
2977 value = ast2obj_expr(o->v.IfExp.test);
2978 if (!value) goto failed;
2979 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2980 goto failed;
2981 Py_DECREF(value);
2982 value = ast2obj_expr(o->v.IfExp.body);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 value = ast2obj_expr(o->v.IfExp.orelse);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 break;
2993 case Dict_kind:
2994 result = PyType_GenericNew(Dict_type, NULL, NULL);
2995 if (!result) goto failed;
2996 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2997 if (!value) goto failed;
2998 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3002 if (!value) goto failed;
3003 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
3006 break;
3007 case Set_kind:
3008 result = PyType_GenericNew(Set_type, NULL, NULL);
3009 if (!result) goto failed;
3010 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 break;
3016 case ListComp_kind:
3017 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_expr(o->v.ListComp.elt);
3020 if (!value) goto failed;
3021 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
3024 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 break;
3030 case SetComp_kind:
3031 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3032 if (!result) goto failed;
3033 value = ast2obj_expr(o->v.SetComp.elt);
3034 if (!value) goto failed;
3035 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3036 goto failed;
3037 Py_DECREF(value);
3038 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3039 if (!value) goto failed;
3040 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3041 goto failed;
3042 Py_DECREF(value);
3043 break;
3044 case DictComp_kind:
3045 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3046 if (!result) goto failed;
3047 value = ast2obj_expr(o->v.DictComp.key);
3048 if (!value) goto failed;
3049 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3050 goto failed;
3051 Py_DECREF(value);
3052 value = ast2obj_expr(o->v.DictComp.value);
3053 if (!value) goto failed;
3054 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3055 goto failed;
3056 Py_DECREF(value);
3057 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3058 if (!value) goto failed;
3059 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3060 goto failed;
3061 Py_DECREF(value);
3062 break;
3063 case GeneratorExp_kind:
3064 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3065 if (!result) goto failed;
3066 value = ast2obj_expr(o->v.GeneratorExp.elt);
3067 if (!value) goto failed;
3068 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3069 goto failed;
3070 Py_DECREF(value);
3071 value = ast2obj_list(o->v.GeneratorExp.generators,
3072 ast2obj_comprehension);
3073 if (!value) goto failed;
3074 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3075 goto failed;
3076 Py_DECREF(value);
3077 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003078 case Await_kind:
3079 result = PyType_GenericNew(Await_type, NULL, NULL);
3080 if (!result) goto failed;
3081 value = ast2obj_expr(o->v.Await.value);
3082 if (!value) goto failed;
3083 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3084 goto failed;
3085 Py_DECREF(value);
3086 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003087 case Yield_kind:
3088 result = PyType_GenericNew(Yield_type, NULL, NULL);
3089 if (!result) goto failed;
3090 value = ast2obj_expr(o->v.Yield.value);
3091 if (!value) goto failed;
3092 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3093 goto failed;
3094 Py_DECREF(value);
3095 break;
3096 case YieldFrom_kind:
3097 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3098 if (!result) goto failed;
3099 value = ast2obj_expr(o->v.YieldFrom.value);
3100 if (!value) goto failed;
3101 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3102 goto failed;
3103 Py_DECREF(value);
3104 break;
3105 case Compare_kind:
3106 result = PyType_GenericNew(Compare_type, NULL, NULL);
3107 if (!result) goto failed;
3108 value = ast2obj_expr(o->v.Compare.left);
3109 if (!value) goto failed;
3110 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3111 goto failed;
3112 Py_DECREF(value);
3113 {
3114 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3115 value = PyList_New(n);
3116 if (!value) goto failed;
3117 for(i = 0; i < n; i++)
3118 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003120 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003121 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3122 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003123 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003124 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003126 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3127 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003128 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003129 break;
3130 case Call_kind:
3131 result = PyType_GenericNew(Call_type, NULL, NULL);
3132 if (!result) goto failed;
3133 value = ast2obj_expr(o->v.Call.func);
3134 if (!value) goto failed;
3135 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3136 goto failed;
3137 Py_DECREF(value);
3138 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3139 if (!value) goto failed;
3140 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3141 goto failed;
3142 Py_DECREF(value);
3143 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3144 if (!value) goto failed;
3145 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003148 break;
3149 case Num_kind:
3150 result = PyType_GenericNew(Num_type, NULL, NULL);
3151 if (!result) goto failed;
3152 value = ast2obj_object(o->v.Num.n);
3153 if (!value) goto failed;
3154 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3155 goto failed;
3156 Py_DECREF(value);
3157 break;
3158 case Str_kind:
3159 result = PyType_GenericNew(Str_type, NULL, NULL);
3160 if (!result) goto failed;
3161 value = ast2obj_string(o->v.Str.s);
3162 if (!value) goto failed;
3163 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 break;
3167 case Bytes_kind:
3168 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3169 if (!result) goto failed;
3170 value = ast2obj_bytes(o->v.Bytes.s);
3171 if (!value) goto failed;
3172 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003176 case NameConstant_kind:
3177 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3178 if (!result) goto failed;
3179 value = ast2obj_singleton(o->v.NameConstant.value);
3180 if (!value) goto failed;
3181 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3182 goto failed;
3183 Py_DECREF(value);
3184 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003185 case Ellipsis_kind:
3186 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3187 if (!result) goto failed;
3188 break;
3189 case Attribute_kind:
3190 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3191 if (!result) goto failed;
3192 value = ast2obj_expr(o->v.Attribute.value);
3193 if (!value) goto failed;
3194 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3195 goto failed;
3196 Py_DECREF(value);
3197 value = ast2obj_identifier(o->v.Attribute.attr);
3198 if (!value) goto failed;
3199 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3200 goto failed;
3201 Py_DECREF(value);
3202 value = ast2obj_expr_context(o->v.Attribute.ctx);
3203 if (!value) goto failed;
3204 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3205 goto failed;
3206 Py_DECREF(value);
3207 break;
3208 case Subscript_kind:
3209 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3210 if (!result) goto failed;
3211 value = ast2obj_expr(o->v.Subscript.value);
3212 if (!value) goto failed;
3213 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 value = ast2obj_slice(o->v.Subscript.slice);
3217 if (!value) goto failed;
3218 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3219 goto failed;
3220 Py_DECREF(value);
3221 value = ast2obj_expr_context(o->v.Subscript.ctx);
3222 if (!value) goto failed;
3223 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3224 goto failed;
3225 Py_DECREF(value);
3226 break;
3227 case Starred_kind:
3228 result = PyType_GenericNew(Starred_type, NULL, NULL);
3229 if (!result) goto failed;
3230 value = ast2obj_expr(o->v.Starred.value);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 value = ast2obj_expr_context(o->v.Starred.ctx);
3236 if (!value) goto failed;
3237 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
3240 break;
3241 case Name_kind:
3242 result = PyType_GenericNew(Name_type, NULL, NULL);
3243 if (!result) goto failed;
3244 value = ast2obj_identifier(o->v.Name.id);
3245 if (!value) goto failed;
3246 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3247 goto failed;
3248 Py_DECREF(value);
3249 value = ast2obj_expr_context(o->v.Name.ctx);
3250 if (!value) goto failed;
3251 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3252 goto failed;
3253 Py_DECREF(value);
3254 break;
3255 case List_kind:
3256 result = PyType_GenericNew(List_type, NULL, NULL);
3257 if (!result) goto failed;
3258 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3259 if (!value) goto failed;
3260 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3261 goto failed;
3262 Py_DECREF(value);
3263 value = ast2obj_expr_context(o->v.List.ctx);
3264 if (!value) goto failed;
3265 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3266 goto failed;
3267 Py_DECREF(value);
3268 break;
3269 case Tuple_kind:
3270 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3271 if (!result) goto failed;
3272 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3273 if (!value) goto failed;
3274 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
3277 value = ast2obj_expr_context(o->v.Tuple.ctx);
3278 if (!value) goto failed;
3279 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
3282 break;
3283 }
3284 value = ast2obj_int(o->lineno);
3285 if (!value) goto failed;
3286 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3287 goto failed;
3288 Py_DECREF(value);
3289 value = ast2obj_int(o->col_offset);
3290 if (!value) goto failed;
3291 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3292 goto failed;
3293 Py_DECREF(value);
3294 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003295failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003296 Py_XDECREF(value);
3297 Py_XDECREF(result);
3298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
3300
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003301PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003303 switch(o) {
3304 case Load:
3305 Py_INCREF(Load_singleton);
3306 return Load_singleton;
3307 case Store:
3308 Py_INCREF(Store_singleton);
3309 return Store_singleton;
3310 case Del:
3311 Py_INCREF(Del_singleton);
3312 return Del_singleton;
3313 case AugLoad:
3314 Py_INCREF(AugLoad_singleton);
3315 return AugLoad_singleton;
3316 case AugStore:
3317 Py_INCREF(AugStore_singleton);
3318 return AugStore_singleton;
3319 case Param:
3320 Py_INCREF(Param_singleton);
3321 return Param_singleton;
3322 default:
3323 /* should never happen, but just in case ... */
3324 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3325 return NULL;
3326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003328PyObject*
3329ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003331 slice_ty o = (slice_ty)_o;
3332 PyObject *result = NULL, *value = NULL;
3333 if (!o) {
3334 Py_INCREF(Py_None);
3335 return Py_None;
3336 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003337
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 switch (o->kind) {
3339 case Slice_kind:
3340 result = PyType_GenericNew(Slice_type, NULL, NULL);
3341 if (!result) goto failed;
3342 value = ast2obj_expr(o->v.Slice.lower);
3343 if (!value) goto failed;
3344 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3345 goto failed;
3346 Py_DECREF(value);
3347 value = ast2obj_expr(o->v.Slice.upper);
3348 if (!value) goto failed;
3349 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3350 goto failed;
3351 Py_DECREF(value);
3352 value = ast2obj_expr(o->v.Slice.step);
3353 if (!value) goto failed;
3354 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3355 goto failed;
3356 Py_DECREF(value);
3357 break;
3358 case ExtSlice_kind:
3359 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3360 if (!result) goto failed;
3361 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3362 if (!value) goto failed;
3363 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3364 goto failed;
3365 Py_DECREF(value);
3366 break;
3367 case Index_kind:
3368 result = PyType_GenericNew(Index_type, NULL, NULL);
3369 if (!result) goto failed;
3370 value = ast2obj_expr(o->v.Index.value);
3371 if (!value) goto failed;
3372 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3373 goto failed;
3374 Py_DECREF(value);
3375 break;
3376 }
3377 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003378failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003379 Py_XDECREF(value);
3380 Py_XDECREF(result);
3381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}
3383
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003384PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003386 switch(o) {
3387 case And:
3388 Py_INCREF(And_singleton);
3389 return And_singleton;
3390 case Or:
3391 Py_INCREF(Or_singleton);
3392 return Or_singleton;
3393 default:
3394 /* should never happen, but just in case ... */
3395 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3396 return NULL;
3397 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003398}
3399PyObject* ast2obj_operator(operator_ty o)
3400{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 switch(o) {
3402 case Add:
3403 Py_INCREF(Add_singleton);
3404 return Add_singleton;
3405 case Sub:
3406 Py_INCREF(Sub_singleton);
3407 return Sub_singleton;
3408 case Mult:
3409 Py_INCREF(Mult_singleton);
3410 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003411 case MatMult:
3412 Py_INCREF(MatMult_singleton);
3413 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003414 case Div:
3415 Py_INCREF(Div_singleton);
3416 return Div_singleton;
3417 case Mod:
3418 Py_INCREF(Mod_singleton);
3419 return Mod_singleton;
3420 case Pow:
3421 Py_INCREF(Pow_singleton);
3422 return Pow_singleton;
3423 case LShift:
3424 Py_INCREF(LShift_singleton);
3425 return LShift_singleton;
3426 case RShift:
3427 Py_INCREF(RShift_singleton);
3428 return RShift_singleton;
3429 case BitOr:
3430 Py_INCREF(BitOr_singleton);
3431 return BitOr_singleton;
3432 case BitXor:
3433 Py_INCREF(BitXor_singleton);
3434 return BitXor_singleton;
3435 case BitAnd:
3436 Py_INCREF(BitAnd_singleton);
3437 return BitAnd_singleton;
3438 case FloorDiv:
3439 Py_INCREF(FloorDiv_singleton);
3440 return FloorDiv_singleton;
3441 default:
3442 /* should never happen, but just in case ... */
3443 PyErr_Format(PyExc_SystemError, "unknown operator found");
3444 return NULL;
3445 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003446}
3447PyObject* ast2obj_unaryop(unaryop_ty o)
3448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003449 switch(o) {
3450 case Invert:
3451 Py_INCREF(Invert_singleton);
3452 return Invert_singleton;
3453 case Not:
3454 Py_INCREF(Not_singleton);
3455 return Not_singleton;
3456 case UAdd:
3457 Py_INCREF(UAdd_singleton);
3458 return UAdd_singleton;
3459 case USub:
3460 Py_INCREF(USub_singleton);
3461 return USub_singleton;
3462 default:
3463 /* should never happen, but just in case ... */
3464 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3465 return NULL;
3466 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003467}
3468PyObject* ast2obj_cmpop(cmpop_ty o)
3469{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003470 switch(o) {
3471 case Eq:
3472 Py_INCREF(Eq_singleton);
3473 return Eq_singleton;
3474 case NotEq:
3475 Py_INCREF(NotEq_singleton);
3476 return NotEq_singleton;
3477 case Lt:
3478 Py_INCREF(Lt_singleton);
3479 return Lt_singleton;
3480 case LtE:
3481 Py_INCREF(LtE_singleton);
3482 return LtE_singleton;
3483 case Gt:
3484 Py_INCREF(Gt_singleton);
3485 return Gt_singleton;
3486 case GtE:
3487 Py_INCREF(GtE_singleton);
3488 return GtE_singleton;
3489 case Is:
3490 Py_INCREF(Is_singleton);
3491 return Is_singleton;
3492 case IsNot:
3493 Py_INCREF(IsNot_singleton);
3494 return IsNot_singleton;
3495 case In:
3496 Py_INCREF(In_singleton);
3497 return In_singleton;
3498 case NotIn:
3499 Py_INCREF(NotIn_singleton);
3500 return NotIn_singleton;
3501 default:
3502 /* should never happen, but just in case ... */
3503 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3504 return NULL;
3505 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003506}
3507PyObject*
3508ast2obj_comprehension(void* _o)
3509{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003510 comprehension_ty o = (comprehension_ty)_o;
3511 PyObject *result = NULL, *value = NULL;
3512 if (!o) {
3513 Py_INCREF(Py_None);
3514 return Py_None;
3515 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003516
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003517 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3518 if (!result) return NULL;
3519 value = ast2obj_expr(o->target);
3520 if (!value) goto failed;
3521 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3522 goto failed;
3523 Py_DECREF(value);
3524 value = ast2obj_expr(o->iter);
3525 if (!value) goto failed;
3526 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3527 goto failed;
3528 Py_DECREF(value);
3529 value = ast2obj_list(o->ifs, ast2obj_expr);
3530 if (!value) goto failed;
3531 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3532 goto failed;
3533 Py_DECREF(value);
3534 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003535failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003536 Py_XDECREF(value);
3537 Py_XDECREF(result);
3538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539}
3540
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003541PyObject*
3542ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003544 excepthandler_ty o = (excepthandler_ty)_o;
3545 PyObject *result = NULL, *value = NULL;
3546 if (!o) {
3547 Py_INCREF(Py_None);
3548 return Py_None;
3549 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003550
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003551 switch (o->kind) {
3552 case ExceptHandler_kind:
3553 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3554 if (!result) goto failed;
3555 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003556 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003557 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3558 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003559 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003560 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003561 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3563 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003564 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003565 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3566 if (!value) goto failed;
3567 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3568 goto failed;
3569 Py_DECREF(value);
3570 break;
3571 }
3572 value = ast2obj_int(o->lineno);
3573 if (!value) goto failed;
3574 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3575 goto failed;
3576 Py_DECREF(value);
3577 value = ast2obj_int(o->col_offset);
3578 if (!value) goto failed;
3579 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3580 goto failed;
3581 Py_DECREF(value);
3582 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003583failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003584 Py_XDECREF(value);
3585 Py_XDECREF(result);
3586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587}
3588
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003589PyObject*
3590ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003592 arguments_ty o = (arguments_ty)_o;
3593 PyObject *result = NULL, *value = NULL;
3594 if (!o) {
3595 Py_INCREF(Py_None);
3596 return Py_None;
3597 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003598
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003599 result = PyType_GenericNew(arguments_type, NULL, NULL);
3600 if (!result) return NULL;
3601 value = ast2obj_list(o->args, ast2obj_arg);
3602 if (!value) goto failed;
3603 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3604 goto failed;
3605 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003606 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003607 if (!value) goto failed;
3608 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3609 goto failed;
3610 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003611 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3612 if (!value) goto failed;
3613 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3614 goto failed;
3615 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003616 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003617 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003618 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003619 goto failed;
3620 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003621 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003622 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003623 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003624 goto failed;
3625 Py_DECREF(value);
3626 value = ast2obj_list(o->defaults, ast2obj_expr);
3627 if (!value) goto failed;
3628 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3629 goto failed;
3630 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003631 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003632failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003633 Py_XDECREF(value);
3634 Py_XDECREF(result);
3635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636}
3637
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003638PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003639ast2obj_arg(void* _o)
3640{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003641 arg_ty o = (arg_ty)_o;
3642 PyObject *result = NULL, *value = NULL;
3643 if (!o) {
3644 Py_INCREF(Py_None);
3645 return Py_None;
3646 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003647
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003648 result = PyType_GenericNew(arg_type, NULL, NULL);
3649 if (!result) return NULL;
3650 value = ast2obj_identifier(o->arg);
3651 if (!value) goto failed;
3652 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3653 goto failed;
3654 Py_DECREF(value);
3655 value = ast2obj_expr(o->annotation);
3656 if (!value) goto failed;
3657 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3658 goto failed;
3659 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003660 value = ast2obj_int(o->lineno);
3661 if (!value) goto failed;
3662 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3663 goto failed;
3664 Py_DECREF(value);
3665 value = ast2obj_int(o->col_offset);
3666 if (!value) goto failed;
3667 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3668 goto failed;
3669 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003670 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003671failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003672 Py_XDECREF(value);
3673 Py_XDECREF(result);
3674 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003675}
3676
3677PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003678ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003680 keyword_ty o = (keyword_ty)_o;
3681 PyObject *result = NULL, *value = NULL;
3682 if (!o) {
3683 Py_INCREF(Py_None);
3684 return Py_None;
3685 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003686
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003687 result = PyType_GenericNew(keyword_type, NULL, NULL);
3688 if (!result) return NULL;
3689 value = ast2obj_identifier(o->arg);
3690 if (!value) goto failed;
3691 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3692 goto failed;
3693 Py_DECREF(value);
3694 value = ast2obj_expr(o->value);
3695 if (!value) goto failed;
3696 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3697 goto failed;
3698 Py_DECREF(value);
3699 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003700failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003701 Py_XDECREF(value);
3702 Py_XDECREF(result);
3703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704}
3705
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003706PyObject*
3707ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003709 alias_ty o = (alias_ty)_o;
3710 PyObject *result = NULL, *value = NULL;
3711 if (!o) {
3712 Py_INCREF(Py_None);
3713 return Py_None;
3714 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003715
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003716 result = PyType_GenericNew(alias_type, NULL, NULL);
3717 if (!result) return NULL;
3718 value = ast2obj_identifier(o->name);
3719 if (!value) goto failed;
3720 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3721 goto failed;
3722 Py_DECREF(value);
3723 value = ast2obj_identifier(o->asname);
3724 if (!value) goto failed;
3725 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3726 goto failed;
3727 Py_DECREF(value);
3728 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003729failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003730 Py_XDECREF(value);
3731 Py_XDECREF(result);
3732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733}
3734
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003735PyObject*
3736ast2obj_withitem(void* _o)
3737{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003738 withitem_ty o = (withitem_ty)_o;
3739 PyObject *result = NULL, *value = NULL;
3740 if (!o) {
3741 Py_INCREF(Py_None);
3742 return Py_None;
3743 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003744
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003745 result = PyType_GenericNew(withitem_type, NULL, NULL);
3746 if (!result) return NULL;
3747 value = ast2obj_expr(o->context_expr);
3748 if (!value) goto failed;
3749 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3750 goto failed;
3751 Py_DECREF(value);
3752 value = ast2obj_expr(o->optional_vars);
3753 if (!value) goto failed;
3754 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3755 goto failed;
3756 Py_DECREF(value);
3757 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003758failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003759 Py_XDECREF(value);
3760 Py_XDECREF(result);
3761 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003762}
3763
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003765int
3766obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3767{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003768 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003769
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003770 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003771
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003772 if (obj == Py_None) {
3773 *out = NULL;
3774 return 0;
3775 }
3776 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3777 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003778 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003779 }
3780 if (isinstance) {
3781 asdl_seq* body;
3782
3783 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3784 int res;
3785 Py_ssize_t len;
3786 Py_ssize_t i;
3787 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3788 if (tmp == NULL) goto failed;
3789 if (!PyList_Check(tmp)) {
3790 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3791 goto failed;
3792 }
3793 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003794 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003795 if (body == NULL) goto failed;
3796 for (i = 0; i < len; i++) {
3797 stmt_ty value;
3798 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3799 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03003800 if (len != PyList_GET_SIZE(tmp)) {
3801 PyErr_SetString(PyExc_RuntimeError, "Module field \"body\" changed size during iteration");
3802 goto failed;
3803 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003804 asdl_seq_SET(body, i, value);
3805 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003806 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003807 } else {
3808 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3809 return 1;
3810 }
3811 *out = Module(body, arena);
3812 if (*out == NULL) goto failed;
3813 return 0;
3814 }
3815 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3816 if (isinstance == -1) {
3817 return 1;
3818 }
3819 if (isinstance) {
3820 asdl_seq* body;
3821
3822 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3823 int res;
3824 Py_ssize_t len;
3825 Py_ssize_t i;
3826 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3827 if (tmp == NULL) goto failed;
3828 if (!PyList_Check(tmp)) {
3829 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3830 goto failed;
3831 }
3832 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003833 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003834 if (body == NULL) goto failed;
3835 for (i = 0; i < len; i++) {
3836 stmt_ty value;
3837 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3838 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03003839 if (len != PyList_GET_SIZE(tmp)) {
3840 PyErr_SetString(PyExc_RuntimeError, "Interactive field \"body\" changed size during iteration");
3841 goto failed;
3842 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003843 asdl_seq_SET(body, i, value);
3844 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003845 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003846 } else {
3847 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3848 return 1;
3849 }
3850 *out = Interactive(body, arena);
3851 if (*out == NULL) goto failed;
3852 return 0;
3853 }
3854 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3855 if (isinstance == -1) {
3856 return 1;
3857 }
3858 if (isinstance) {
3859 expr_ty body;
3860
3861 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3862 int res;
3863 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3864 if (tmp == NULL) goto failed;
3865 res = obj2ast_expr(tmp, &body, arena);
3866 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003867 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003868 } else {
3869 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3870 return 1;
3871 }
3872 *out = Expression(body, arena);
3873 if (*out == NULL) goto failed;
3874 return 0;
3875 }
3876 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3877 if (isinstance == -1) {
3878 return 1;
3879 }
3880 if (isinstance) {
3881 asdl_seq* body;
3882
3883 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3884 int res;
3885 Py_ssize_t len;
3886 Py_ssize_t i;
3887 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3888 if (tmp == NULL) goto failed;
3889 if (!PyList_Check(tmp)) {
3890 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3891 goto failed;
3892 }
3893 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003894 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003895 if (body == NULL) goto failed;
3896 for (i = 0; i < len; i++) {
3897 stmt_ty value;
3898 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3899 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03003900 if (len != PyList_GET_SIZE(tmp)) {
3901 PyErr_SetString(PyExc_RuntimeError, "Suite field \"body\" changed size during iteration");
3902 goto failed;
3903 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003904 asdl_seq_SET(body, i, value);
3905 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003906 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003907 } else {
3908 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3909 return 1;
3910 }
3911 *out = Suite(body, arena);
3912 if (*out == NULL) goto failed;
3913 return 0;
3914 }
3915
3916 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3917 failed:
3918 Py_XDECREF(tmp);
3919 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003920}
3921
3922int
3923obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3924{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003925 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003926
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003927 PyObject *tmp = NULL;
3928 int lineno;
3929 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003930
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003931 if (obj == Py_None) {
3932 *out = NULL;
3933 return 0;
3934 }
3935 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3936 int res;
3937 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3938 if (tmp == NULL) goto failed;
3939 res = obj2ast_int(tmp, &lineno, arena);
3940 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003941 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003942 } else {
3943 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003944 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003945 }
3946 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3947 int res;
3948 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3949 if (tmp == NULL) goto failed;
3950 res = obj2ast_int(tmp, &col_offset, arena);
3951 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003952 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003953 } else {
3954 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3955 return 1;
3956 }
3957 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3958 if (isinstance == -1) {
3959 return 1;
3960 }
3961 if (isinstance) {
3962 identifier name;
3963 arguments_ty args;
3964 asdl_seq* body;
3965 asdl_seq* decorator_list;
3966 expr_ty returns;
3967
3968 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3969 int res;
3970 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3971 if (tmp == NULL) goto failed;
3972 res = obj2ast_identifier(tmp, &name, arena);
3973 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003975 } else {
3976 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3977 return 1;
3978 }
3979 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3980 int res;
3981 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3982 if (tmp == NULL) goto failed;
3983 res = obj2ast_arguments(tmp, &args, arena);
3984 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003985 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003986 } else {
3987 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3988 return 1;
3989 }
3990 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3991 int res;
3992 Py_ssize_t len;
3993 Py_ssize_t i;
3994 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3995 if (tmp == NULL) goto failed;
3996 if (!PyList_Check(tmp)) {
3997 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3998 goto failed;
3999 }
4000 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004001 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004002 if (body == NULL) goto failed;
4003 for (i = 0; i < len; i++) {
4004 stmt_ty value;
4005 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4006 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004007 if (len != PyList_GET_SIZE(tmp)) {
4008 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"body\" changed size during iteration");
4009 goto failed;
4010 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 asdl_seq_SET(body, i, value);
4012 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004013 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004014 } else {
4015 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4016 return 1;
4017 }
4018 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4019 int res;
4020 Py_ssize_t len;
4021 Py_ssize_t i;
4022 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4023 if (tmp == NULL) goto failed;
4024 if (!PyList_Check(tmp)) {
4025 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4026 goto failed;
4027 }
4028 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004029 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004030 if (decorator_list == NULL) goto failed;
4031 for (i = 0; i < len; i++) {
4032 expr_ty value;
4033 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4034 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004035 if (len != PyList_GET_SIZE(tmp)) {
4036 PyErr_SetString(PyExc_RuntimeError, "FunctionDef field \"decorator_list\" changed size during iteration");
4037 goto failed;
4038 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004039 asdl_seq_SET(decorator_list, i, value);
4040 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004042 } else {
4043 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4044 return 1;
4045 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004046 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004047 int res;
4048 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4049 if (tmp == NULL) goto failed;
4050 res = obj2ast_expr(tmp, &returns, arena);
4051 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004052 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004053 } else {
4054 returns = NULL;
4055 }
4056 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4057 col_offset, arena);
4058 if (*out == NULL) goto failed;
4059 return 0;
4060 }
Yury Selivanov75445082015-05-11 22:57:16 -04004061 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4062 if (isinstance == -1) {
4063 return 1;
4064 }
4065 if (isinstance) {
4066 identifier name;
4067 arguments_ty args;
4068 asdl_seq* body;
4069 asdl_seq* decorator_list;
4070 expr_ty returns;
4071
4072 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4073 int res;
4074 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4075 if (tmp == NULL) goto failed;
4076 res = obj2ast_identifier(tmp, &name, arena);
4077 if (res != 0) goto failed;
4078 Py_CLEAR(tmp);
4079 } else {
4080 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4081 return 1;
4082 }
4083 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4084 int res;
4085 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4086 if (tmp == NULL) goto failed;
4087 res = obj2ast_arguments(tmp, &args, arena);
4088 if (res != 0) goto failed;
4089 Py_CLEAR(tmp);
4090 } else {
4091 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4092 return 1;
4093 }
4094 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4095 int res;
4096 Py_ssize_t len;
4097 Py_ssize_t i;
4098 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4099 if (tmp == NULL) goto failed;
4100 if (!PyList_Check(tmp)) {
4101 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4102 goto failed;
4103 }
4104 len = PyList_GET_SIZE(tmp);
4105 body = _Py_asdl_seq_new(len, arena);
4106 if (body == NULL) goto failed;
4107 for (i = 0; i < len; i++) {
4108 stmt_ty value;
4109 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4110 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004111 if (len != PyList_GET_SIZE(tmp)) {
4112 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"body\" changed size during iteration");
4113 goto failed;
4114 }
Yury Selivanov75445082015-05-11 22:57:16 -04004115 asdl_seq_SET(body, i, value);
4116 }
4117 Py_CLEAR(tmp);
4118 } else {
4119 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4120 return 1;
4121 }
4122 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4123 int res;
4124 Py_ssize_t len;
4125 Py_ssize_t i;
4126 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4127 if (tmp == NULL) goto failed;
4128 if (!PyList_Check(tmp)) {
4129 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4130 goto failed;
4131 }
4132 len = PyList_GET_SIZE(tmp);
4133 decorator_list = _Py_asdl_seq_new(len, arena);
4134 if (decorator_list == NULL) goto failed;
4135 for (i = 0; i < len; i++) {
4136 expr_ty value;
4137 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4138 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004139 if (len != PyList_GET_SIZE(tmp)) {
4140 PyErr_SetString(PyExc_RuntimeError, "AsyncFunctionDef field \"decorator_list\" changed size during iteration");
4141 goto failed;
4142 }
Yury Selivanov75445082015-05-11 22:57:16 -04004143 asdl_seq_SET(decorator_list, i, value);
4144 }
4145 Py_CLEAR(tmp);
4146 } else {
4147 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4148 return 1;
4149 }
4150 if (exists_not_none(obj, &PyId_returns)) {
4151 int res;
4152 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4153 if (tmp == NULL) goto failed;
4154 res = obj2ast_expr(tmp, &returns, arena);
4155 if (res != 0) goto failed;
4156 Py_CLEAR(tmp);
4157 } else {
4158 returns = NULL;
4159 }
4160 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4161 lineno, col_offset, arena);
4162 if (*out == NULL) goto failed;
4163 return 0;
4164 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004165 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4166 if (isinstance == -1) {
4167 return 1;
4168 }
4169 if (isinstance) {
4170 identifier name;
4171 asdl_seq* bases;
4172 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004173 asdl_seq* body;
4174 asdl_seq* decorator_list;
4175
4176 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4177 int res;
4178 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4179 if (tmp == NULL) goto failed;
4180 res = obj2ast_identifier(tmp, &name, arena);
4181 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004182 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004183 } else {
4184 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4185 return 1;
4186 }
4187 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4188 int res;
4189 Py_ssize_t len;
4190 Py_ssize_t i;
4191 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4192 if (tmp == NULL) goto failed;
4193 if (!PyList_Check(tmp)) {
4194 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4195 goto failed;
4196 }
4197 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004198 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004199 if (bases == NULL) goto failed;
4200 for (i = 0; i < len; i++) {
4201 expr_ty value;
4202 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4203 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004204 if (len != PyList_GET_SIZE(tmp)) {
4205 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"bases\" changed size during iteration");
4206 goto failed;
4207 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004208 asdl_seq_SET(bases, i, value);
4209 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004210 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004211 } else {
4212 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4213 return 1;
4214 }
4215 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4216 int res;
4217 Py_ssize_t len;
4218 Py_ssize_t i;
4219 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4220 if (tmp == NULL) goto failed;
4221 if (!PyList_Check(tmp)) {
4222 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4223 goto failed;
4224 }
4225 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004226 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004227 if (keywords == NULL) goto failed;
4228 for (i = 0; i < len; i++) {
4229 keyword_ty value;
4230 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4231 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004232 if (len != PyList_GET_SIZE(tmp)) {
4233 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"keywords\" changed size during iteration");
4234 goto failed;
4235 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004236 asdl_seq_SET(keywords, i, value);
4237 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004238 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004239 } else {
4240 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4241 return 1;
4242 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004243 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4244 int res;
4245 Py_ssize_t len;
4246 Py_ssize_t i;
4247 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4248 if (tmp == NULL) goto failed;
4249 if (!PyList_Check(tmp)) {
4250 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4251 goto failed;
4252 }
4253 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004254 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004255 if (body == NULL) goto failed;
4256 for (i = 0; i < len; i++) {
4257 stmt_ty value;
4258 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4259 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004260 if (len != PyList_GET_SIZE(tmp)) {
4261 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"body\" changed size during iteration");
4262 goto failed;
4263 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004264 asdl_seq_SET(body, i, value);
4265 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004266 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004267 } else {
4268 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4269 return 1;
4270 }
4271 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4272 int res;
4273 Py_ssize_t len;
4274 Py_ssize_t i;
4275 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4276 if (tmp == NULL) goto failed;
4277 if (!PyList_Check(tmp)) {
4278 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4279 goto failed;
4280 }
4281 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004282 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004283 if (decorator_list == NULL) goto failed;
4284 for (i = 0; i < len; i++) {
4285 expr_ty value;
4286 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4287 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004288 if (len != PyList_GET_SIZE(tmp)) {
4289 PyErr_SetString(PyExc_RuntimeError, "ClassDef field \"decorator_list\" changed size during iteration");
4290 goto failed;
4291 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004292 asdl_seq_SET(decorator_list, i, value);
4293 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004294 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004295 } else {
4296 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4297 return 1;
4298 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004299 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4300 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004301 if (*out == NULL) goto failed;
4302 return 0;
4303 }
4304 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4305 if (isinstance == -1) {
4306 return 1;
4307 }
4308 if (isinstance) {
4309 expr_ty value;
4310
Victor Stinneree4b59c2013-07-27 00:01:35 +02004311 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004312 int res;
4313 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4314 if (tmp == NULL) goto failed;
4315 res = obj2ast_expr(tmp, &value, arena);
4316 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004317 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004318 } else {
4319 value = NULL;
4320 }
4321 *out = Return(value, lineno, col_offset, arena);
4322 if (*out == NULL) goto failed;
4323 return 0;
4324 }
4325 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4326 if (isinstance == -1) {
4327 return 1;
4328 }
4329 if (isinstance) {
4330 asdl_seq* targets;
4331
4332 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4333 int res;
4334 Py_ssize_t len;
4335 Py_ssize_t i;
4336 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4337 if (tmp == NULL) goto failed;
4338 if (!PyList_Check(tmp)) {
4339 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4340 goto failed;
4341 }
4342 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004343 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004344 if (targets == NULL) goto failed;
4345 for (i = 0; i < len; i++) {
4346 expr_ty value;
4347 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4348 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004349 if (len != PyList_GET_SIZE(tmp)) {
4350 PyErr_SetString(PyExc_RuntimeError, "Delete field \"targets\" changed size during iteration");
4351 goto failed;
4352 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004353 asdl_seq_SET(targets, i, value);
4354 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004355 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004356 } else {
4357 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4358 return 1;
4359 }
4360 *out = Delete(targets, lineno, col_offset, arena);
4361 if (*out == NULL) goto failed;
4362 return 0;
4363 }
4364 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4365 if (isinstance == -1) {
4366 return 1;
4367 }
4368 if (isinstance) {
4369 asdl_seq* targets;
4370 expr_ty value;
4371
4372 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4373 int res;
4374 Py_ssize_t len;
4375 Py_ssize_t i;
4376 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4377 if (tmp == NULL) goto failed;
4378 if (!PyList_Check(tmp)) {
4379 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4380 goto failed;
4381 }
4382 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004383 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004384 if (targets == NULL) goto failed;
4385 for (i = 0; i < len; i++) {
4386 expr_ty value;
4387 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4388 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004389 if (len != PyList_GET_SIZE(tmp)) {
4390 PyErr_SetString(PyExc_RuntimeError, "Assign field \"targets\" changed size during iteration");
4391 goto failed;
4392 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004393 asdl_seq_SET(targets, i, value);
4394 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004395 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004396 } else {
4397 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4398 return 1;
4399 }
4400 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4401 int res;
4402 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4403 if (tmp == NULL) goto failed;
4404 res = obj2ast_expr(tmp, &value, arena);
4405 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004406 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004407 } else {
4408 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4409 return 1;
4410 }
4411 *out = Assign(targets, value, lineno, col_offset, arena);
4412 if (*out == NULL) goto failed;
4413 return 0;
4414 }
4415 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4416 if (isinstance == -1) {
4417 return 1;
4418 }
4419 if (isinstance) {
4420 expr_ty target;
4421 operator_ty op;
4422 expr_ty value;
4423
4424 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4425 int res;
4426 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4427 if (tmp == NULL) goto failed;
4428 res = obj2ast_expr(tmp, &target, arena);
4429 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004430 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004431 } else {
4432 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4433 return 1;
4434 }
4435 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4436 int res;
4437 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4438 if (tmp == NULL) goto failed;
4439 res = obj2ast_operator(tmp, &op, arena);
4440 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004441 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004442 } else {
4443 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4444 return 1;
4445 }
4446 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4447 int res;
4448 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4449 if (tmp == NULL) goto failed;
4450 res = obj2ast_expr(tmp, &value, arena);
4451 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004452 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004453 } else {
4454 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4455 return 1;
4456 }
4457 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4458 if (*out == NULL) goto failed;
4459 return 0;
4460 }
4461 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4462 if (isinstance == -1) {
4463 return 1;
4464 }
4465 if (isinstance) {
4466 expr_ty target;
4467 expr_ty iter;
4468 asdl_seq* body;
4469 asdl_seq* orelse;
4470
4471 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4472 int res;
4473 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4474 if (tmp == NULL) goto failed;
4475 res = obj2ast_expr(tmp, &target, arena);
4476 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004477 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004478 } else {
4479 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4480 return 1;
4481 }
4482 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4483 int res;
4484 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4485 if (tmp == NULL) goto failed;
4486 res = obj2ast_expr(tmp, &iter, arena);
4487 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004488 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004489 } else {
4490 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4491 return 1;
4492 }
4493 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4494 int res;
4495 Py_ssize_t len;
4496 Py_ssize_t i;
4497 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4498 if (tmp == NULL) goto failed;
4499 if (!PyList_Check(tmp)) {
4500 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4501 goto failed;
4502 }
4503 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004504 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004505 if (body == NULL) goto failed;
4506 for (i = 0; i < len; i++) {
4507 stmt_ty value;
4508 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4509 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004510 if (len != PyList_GET_SIZE(tmp)) {
4511 PyErr_SetString(PyExc_RuntimeError, "For field \"body\" changed size during iteration");
4512 goto failed;
4513 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004514 asdl_seq_SET(body, i, value);
4515 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004516 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004517 } else {
4518 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4519 return 1;
4520 }
4521 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4522 int res;
4523 Py_ssize_t len;
4524 Py_ssize_t i;
4525 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4526 if (tmp == NULL) goto failed;
4527 if (!PyList_Check(tmp)) {
4528 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4529 goto failed;
4530 }
4531 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004532 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004533 if (orelse == NULL) goto failed;
4534 for (i = 0; i < len; i++) {
4535 stmt_ty value;
4536 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4537 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004538 if (len != PyList_GET_SIZE(tmp)) {
4539 PyErr_SetString(PyExc_RuntimeError, "For field \"orelse\" changed size during iteration");
4540 goto failed;
4541 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004542 asdl_seq_SET(orelse, i, value);
4543 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004544 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004545 } else {
4546 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4547 return 1;
4548 }
4549 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4550 if (*out == NULL) goto failed;
4551 return 0;
4552 }
Yury Selivanov75445082015-05-11 22:57:16 -04004553 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4554 if (isinstance == -1) {
4555 return 1;
4556 }
4557 if (isinstance) {
4558 expr_ty target;
4559 expr_ty iter;
4560 asdl_seq* body;
4561 asdl_seq* orelse;
4562
4563 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4564 int res;
4565 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4566 if (tmp == NULL) goto failed;
4567 res = obj2ast_expr(tmp, &target, arena);
4568 if (res != 0) goto failed;
4569 Py_CLEAR(tmp);
4570 } else {
4571 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4572 return 1;
4573 }
4574 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4575 int res;
4576 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4577 if (tmp == NULL) goto failed;
4578 res = obj2ast_expr(tmp, &iter, arena);
4579 if (res != 0) goto failed;
4580 Py_CLEAR(tmp);
4581 } else {
4582 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4583 return 1;
4584 }
4585 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4586 int res;
4587 Py_ssize_t len;
4588 Py_ssize_t i;
4589 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4590 if (tmp == NULL) goto failed;
4591 if (!PyList_Check(tmp)) {
4592 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4593 goto failed;
4594 }
4595 len = PyList_GET_SIZE(tmp);
4596 body = _Py_asdl_seq_new(len, arena);
4597 if (body == NULL) goto failed;
4598 for (i = 0; i < len; i++) {
4599 stmt_ty value;
4600 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4601 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004602 if (len != PyList_GET_SIZE(tmp)) {
4603 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"body\" changed size during iteration");
4604 goto failed;
4605 }
Yury Selivanov75445082015-05-11 22:57:16 -04004606 asdl_seq_SET(body, i, value);
4607 }
4608 Py_CLEAR(tmp);
4609 } else {
4610 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4611 return 1;
4612 }
4613 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4614 int res;
4615 Py_ssize_t len;
4616 Py_ssize_t i;
4617 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4618 if (tmp == NULL) goto failed;
4619 if (!PyList_Check(tmp)) {
4620 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4621 goto failed;
4622 }
4623 len = PyList_GET_SIZE(tmp);
4624 orelse = _Py_asdl_seq_new(len, arena);
4625 if (orelse == NULL) goto failed;
4626 for (i = 0; i < len; i++) {
4627 stmt_ty value;
4628 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4629 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004630 if (len != PyList_GET_SIZE(tmp)) {
4631 PyErr_SetString(PyExc_RuntimeError, "AsyncFor field \"orelse\" changed size during iteration");
4632 goto failed;
4633 }
Yury Selivanov75445082015-05-11 22:57:16 -04004634 asdl_seq_SET(orelse, i, value);
4635 }
4636 Py_CLEAR(tmp);
4637 } else {
4638 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4639 return 1;
4640 }
4641 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4642 if (*out == NULL) goto failed;
4643 return 0;
4644 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004645 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4646 if (isinstance == -1) {
4647 return 1;
4648 }
4649 if (isinstance) {
4650 expr_ty test;
4651 asdl_seq* body;
4652 asdl_seq* orelse;
4653
4654 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4655 int res;
4656 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4657 if (tmp == NULL) goto failed;
4658 res = obj2ast_expr(tmp, &test, arena);
4659 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004660 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004661 } else {
4662 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4663 return 1;
4664 }
4665 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4666 int res;
4667 Py_ssize_t len;
4668 Py_ssize_t i;
4669 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4670 if (tmp == NULL) goto failed;
4671 if (!PyList_Check(tmp)) {
4672 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4673 goto failed;
4674 }
4675 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004676 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004677 if (body == NULL) goto failed;
4678 for (i = 0; i < len; i++) {
4679 stmt_ty value;
4680 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4681 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004682 if (len != PyList_GET_SIZE(tmp)) {
4683 PyErr_SetString(PyExc_RuntimeError, "While field \"body\" changed size during iteration");
4684 goto failed;
4685 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004686 asdl_seq_SET(body, i, value);
4687 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004688 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4691 return 1;
4692 }
4693 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4694 int res;
4695 Py_ssize_t len;
4696 Py_ssize_t i;
4697 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4698 if (tmp == NULL) goto failed;
4699 if (!PyList_Check(tmp)) {
4700 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4701 goto failed;
4702 }
4703 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004704 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004705 if (orelse == NULL) goto failed;
4706 for (i = 0; i < len; i++) {
4707 stmt_ty value;
4708 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4709 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004710 if (len != PyList_GET_SIZE(tmp)) {
4711 PyErr_SetString(PyExc_RuntimeError, "While field \"orelse\" changed size during iteration");
4712 goto failed;
4713 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004714 asdl_seq_SET(orelse, i, value);
4715 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004716 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004717 } else {
4718 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4719 return 1;
4720 }
4721 *out = While(test, body, orelse, lineno, col_offset, arena);
4722 if (*out == NULL) goto failed;
4723 return 0;
4724 }
4725 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4726 if (isinstance == -1) {
4727 return 1;
4728 }
4729 if (isinstance) {
4730 expr_ty test;
4731 asdl_seq* body;
4732 asdl_seq* orelse;
4733
4734 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4735 int res;
4736 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4737 if (tmp == NULL) goto failed;
4738 res = obj2ast_expr(tmp, &test, arena);
4739 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004740 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004741 } else {
4742 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4743 return 1;
4744 }
4745 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4746 int res;
4747 Py_ssize_t len;
4748 Py_ssize_t i;
4749 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4750 if (tmp == NULL) goto failed;
4751 if (!PyList_Check(tmp)) {
4752 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4753 goto failed;
4754 }
4755 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004756 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004757 if (body == NULL) goto failed;
4758 for (i = 0; i < len; i++) {
4759 stmt_ty value;
4760 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4761 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004762 if (len != PyList_GET_SIZE(tmp)) {
4763 PyErr_SetString(PyExc_RuntimeError, "If field \"body\" changed size during iteration");
4764 goto failed;
4765 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004766 asdl_seq_SET(body, i, value);
4767 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004768 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004769 } else {
4770 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4771 return 1;
4772 }
4773 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4774 int res;
4775 Py_ssize_t len;
4776 Py_ssize_t i;
4777 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4778 if (tmp == NULL) goto failed;
4779 if (!PyList_Check(tmp)) {
4780 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4781 goto failed;
4782 }
4783 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004784 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004785 if (orelse == NULL) goto failed;
4786 for (i = 0; i < len; i++) {
4787 stmt_ty value;
4788 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4789 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004790 if (len != PyList_GET_SIZE(tmp)) {
4791 PyErr_SetString(PyExc_RuntimeError, "If field \"orelse\" changed size during iteration");
4792 goto failed;
4793 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004794 asdl_seq_SET(orelse, i, value);
4795 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004796 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004797 } else {
4798 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4799 return 1;
4800 }
4801 *out = If(test, body, orelse, lineno, col_offset, arena);
4802 if (*out == NULL) goto failed;
4803 return 0;
4804 }
4805 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4806 if (isinstance == -1) {
4807 return 1;
4808 }
4809 if (isinstance) {
4810 asdl_seq* items;
4811 asdl_seq* body;
4812
4813 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4814 int res;
4815 Py_ssize_t len;
4816 Py_ssize_t i;
4817 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4818 if (tmp == NULL) goto failed;
4819 if (!PyList_Check(tmp)) {
4820 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4821 goto failed;
4822 }
4823 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004824 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004825 if (items == NULL) goto failed;
4826 for (i = 0; i < len; i++) {
4827 withitem_ty value;
4828 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4829 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004830 if (len != PyList_GET_SIZE(tmp)) {
4831 PyErr_SetString(PyExc_RuntimeError, "With field \"items\" changed size during iteration");
4832 goto failed;
4833 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004834 asdl_seq_SET(items, i, value);
4835 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004836 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004837 } else {
4838 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4839 return 1;
4840 }
4841 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4842 int res;
4843 Py_ssize_t len;
4844 Py_ssize_t i;
4845 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4846 if (tmp == NULL) goto failed;
4847 if (!PyList_Check(tmp)) {
4848 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4849 goto failed;
4850 }
4851 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004852 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004853 if (body == NULL) goto failed;
4854 for (i = 0; i < len; i++) {
4855 stmt_ty value;
4856 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4857 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004858 if (len != PyList_GET_SIZE(tmp)) {
4859 PyErr_SetString(PyExc_RuntimeError, "With field \"body\" changed size during iteration");
4860 goto failed;
4861 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004862 asdl_seq_SET(body, i, value);
4863 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004864 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004865 } else {
4866 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4867 return 1;
4868 }
4869 *out = With(items, body, lineno, col_offset, arena);
4870 if (*out == NULL) goto failed;
4871 return 0;
4872 }
Yury Selivanov75445082015-05-11 22:57:16 -04004873 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4874 if (isinstance == -1) {
4875 return 1;
4876 }
4877 if (isinstance) {
4878 asdl_seq* items;
4879 asdl_seq* body;
4880
4881 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4882 int res;
4883 Py_ssize_t len;
4884 Py_ssize_t i;
4885 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4886 if (tmp == NULL) goto failed;
4887 if (!PyList_Check(tmp)) {
4888 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4889 goto failed;
4890 }
4891 len = PyList_GET_SIZE(tmp);
4892 items = _Py_asdl_seq_new(len, arena);
4893 if (items == NULL) goto failed;
4894 for (i = 0; i < len; i++) {
4895 withitem_ty value;
4896 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4897 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004898 if (len != PyList_GET_SIZE(tmp)) {
4899 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"items\" changed size during iteration");
4900 goto failed;
4901 }
Yury Selivanov75445082015-05-11 22:57:16 -04004902 asdl_seq_SET(items, i, value);
4903 }
4904 Py_CLEAR(tmp);
4905 } else {
4906 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4907 return 1;
4908 }
4909 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4910 int res;
4911 Py_ssize_t len;
4912 Py_ssize_t i;
4913 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4914 if (tmp == NULL) goto failed;
4915 if (!PyList_Check(tmp)) {
4916 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4917 goto failed;
4918 }
4919 len = PyList_GET_SIZE(tmp);
4920 body = _Py_asdl_seq_new(len, arena);
4921 if (body == NULL) goto failed;
4922 for (i = 0; i < len; i++) {
4923 stmt_ty value;
4924 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4925 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03004926 if (len != PyList_GET_SIZE(tmp)) {
4927 PyErr_SetString(PyExc_RuntimeError, "AsyncWith field \"body\" changed size during iteration");
4928 goto failed;
4929 }
Yury Selivanov75445082015-05-11 22:57:16 -04004930 asdl_seq_SET(body, i, value);
4931 }
4932 Py_CLEAR(tmp);
4933 } else {
4934 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4935 return 1;
4936 }
4937 *out = AsyncWith(items, body, lineno, col_offset, arena);
4938 if (*out == NULL) goto failed;
4939 return 0;
4940 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004941 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4942 if (isinstance == -1) {
4943 return 1;
4944 }
4945 if (isinstance) {
4946 expr_ty exc;
4947 expr_ty cause;
4948
Victor Stinneree4b59c2013-07-27 00:01:35 +02004949 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004950 int res;
4951 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4952 if (tmp == NULL) goto failed;
4953 res = obj2ast_expr(tmp, &exc, arena);
4954 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004955 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004956 } else {
4957 exc = NULL;
4958 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004959 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004960 int res;
4961 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4962 if (tmp == NULL) goto failed;
4963 res = obj2ast_expr(tmp, &cause, arena);
4964 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004965 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004966 } else {
4967 cause = NULL;
4968 }
4969 *out = Raise(exc, cause, lineno, col_offset, arena);
4970 if (*out == NULL) goto failed;
4971 return 0;
4972 }
4973 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4974 if (isinstance == -1) {
4975 return 1;
4976 }
4977 if (isinstance) {
4978 asdl_seq* body;
4979 asdl_seq* handlers;
4980 asdl_seq* orelse;
4981 asdl_seq* finalbody;
4982
4983 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4984 int res;
4985 Py_ssize_t len;
4986 Py_ssize_t i;
4987 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4988 if (tmp == NULL) goto failed;
4989 if (!PyList_Check(tmp)) {
4990 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4991 goto failed;
4992 }
4993 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004994 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004995 if (body == NULL) goto failed;
4996 for (i = 0; i < len; i++) {
4997 stmt_ty value;
4998 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4999 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005000 if (len != PyList_GET_SIZE(tmp)) {
5001 PyErr_SetString(PyExc_RuntimeError, "Try field \"body\" changed size during iteration");
5002 goto failed;
5003 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005004 asdl_seq_SET(body, i, value);
5005 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005006 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005007 } else {
5008 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
5009 return 1;
5010 }
5011 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5012 int res;
5013 Py_ssize_t len;
5014 Py_ssize_t i;
5015 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5016 if (tmp == NULL) goto failed;
5017 if (!PyList_Check(tmp)) {
5018 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5019 goto failed;
5020 }
5021 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005022 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005023 if (handlers == NULL) goto failed;
5024 for (i = 0; i < len; i++) {
5025 excepthandler_ty value;
5026 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5027 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005028 if (len != PyList_GET_SIZE(tmp)) {
5029 PyErr_SetString(PyExc_RuntimeError, "Try field \"handlers\" changed size during iteration");
5030 goto failed;
5031 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005032 asdl_seq_SET(handlers, i, value);
5033 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005034 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005035 } else {
5036 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5037 return 1;
5038 }
5039 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5040 int res;
5041 Py_ssize_t len;
5042 Py_ssize_t i;
5043 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5044 if (tmp == NULL) goto failed;
5045 if (!PyList_Check(tmp)) {
5046 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5047 goto failed;
5048 }
5049 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005050 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005051 if (orelse == NULL) goto failed;
5052 for (i = 0; i < len; i++) {
5053 stmt_ty value;
5054 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5055 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005056 if (len != PyList_GET_SIZE(tmp)) {
5057 PyErr_SetString(PyExc_RuntimeError, "Try field \"orelse\" changed size during iteration");
5058 goto failed;
5059 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005060 asdl_seq_SET(orelse, i, value);
5061 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005062 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005063 } else {
5064 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5065 return 1;
5066 }
5067 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5068 int res;
5069 Py_ssize_t len;
5070 Py_ssize_t i;
5071 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5072 if (tmp == NULL) goto failed;
5073 if (!PyList_Check(tmp)) {
5074 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5075 goto failed;
5076 }
5077 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005078 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005079 if (finalbody == NULL) goto failed;
5080 for (i = 0; i < len; i++) {
5081 stmt_ty value;
5082 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5083 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005084 if (len != PyList_GET_SIZE(tmp)) {
5085 PyErr_SetString(PyExc_RuntimeError, "Try field \"finalbody\" changed size during iteration");
5086 goto failed;
5087 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005088 asdl_seq_SET(finalbody, i, value);
5089 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005090 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005091 } else {
5092 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5093 return 1;
5094 }
5095 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5096 arena);
5097 if (*out == NULL) goto failed;
5098 return 0;
5099 }
5100 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5101 if (isinstance == -1) {
5102 return 1;
5103 }
5104 if (isinstance) {
5105 expr_ty test;
5106 expr_ty msg;
5107
5108 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5109 int res;
5110 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5111 if (tmp == NULL) goto failed;
5112 res = obj2ast_expr(tmp, &test, arena);
5113 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005114 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005115 } else {
5116 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5117 return 1;
5118 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005119 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005120 int res;
5121 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5122 if (tmp == NULL) goto failed;
5123 res = obj2ast_expr(tmp, &msg, arena);
5124 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005125 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005126 } else {
5127 msg = NULL;
5128 }
5129 *out = Assert(test, msg, lineno, col_offset, arena);
5130 if (*out == NULL) goto failed;
5131 return 0;
5132 }
5133 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5134 if (isinstance == -1) {
5135 return 1;
5136 }
5137 if (isinstance) {
5138 asdl_seq* names;
5139
5140 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5141 int res;
5142 Py_ssize_t len;
5143 Py_ssize_t i;
5144 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5145 if (tmp == NULL) goto failed;
5146 if (!PyList_Check(tmp)) {
5147 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5148 goto failed;
5149 }
5150 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005151 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005152 if (names == NULL) goto failed;
5153 for (i = 0; i < len; i++) {
5154 alias_ty value;
5155 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5156 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005157 if (len != PyList_GET_SIZE(tmp)) {
5158 PyErr_SetString(PyExc_RuntimeError, "Import field \"names\" changed size during iteration");
5159 goto failed;
5160 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005161 asdl_seq_SET(names, i, value);
5162 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005163 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005164 } else {
5165 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5166 return 1;
5167 }
5168 *out = Import(names, lineno, col_offset, arena);
5169 if (*out == NULL) goto failed;
5170 return 0;
5171 }
5172 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5173 if (isinstance == -1) {
5174 return 1;
5175 }
5176 if (isinstance) {
5177 identifier module;
5178 asdl_seq* names;
5179 int level;
5180
Victor Stinneree4b59c2013-07-27 00:01:35 +02005181 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005182 int res;
5183 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5184 if (tmp == NULL) goto failed;
5185 res = obj2ast_identifier(tmp, &module, arena);
5186 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005187 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005188 } else {
5189 module = NULL;
5190 }
5191 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5192 int res;
5193 Py_ssize_t len;
5194 Py_ssize_t i;
5195 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5196 if (tmp == NULL) goto failed;
5197 if (!PyList_Check(tmp)) {
5198 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5199 goto failed;
5200 }
5201 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005202 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005203 if (names == NULL) goto failed;
5204 for (i = 0; i < len; i++) {
5205 alias_ty value;
5206 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5207 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005208 if (len != PyList_GET_SIZE(tmp)) {
5209 PyErr_SetString(PyExc_RuntimeError, "ImportFrom field \"names\" changed size during iteration");
5210 goto failed;
5211 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005212 asdl_seq_SET(names, i, value);
5213 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005214 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005215 } else {
5216 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5217 return 1;
5218 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005219 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005220 int res;
5221 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5222 if (tmp == NULL) goto failed;
5223 res = obj2ast_int(tmp, &level, arena);
5224 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005225 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005226 } else {
5227 level = 0;
5228 }
5229 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5230 if (*out == NULL) goto failed;
5231 return 0;
5232 }
5233 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5234 if (isinstance == -1) {
5235 return 1;
5236 }
5237 if (isinstance) {
5238 asdl_seq* names;
5239
5240 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5241 int res;
5242 Py_ssize_t len;
5243 Py_ssize_t i;
5244 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5245 if (tmp == NULL) goto failed;
5246 if (!PyList_Check(tmp)) {
5247 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5248 goto failed;
5249 }
5250 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005251 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005252 if (names == NULL) goto failed;
5253 for (i = 0; i < len; i++) {
5254 identifier value;
5255 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5256 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005257 if (len != PyList_GET_SIZE(tmp)) {
5258 PyErr_SetString(PyExc_RuntimeError, "Global field \"names\" changed size during iteration");
5259 goto failed;
5260 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005261 asdl_seq_SET(names, i, value);
5262 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005263 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005264 } else {
5265 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5266 return 1;
5267 }
5268 *out = Global(names, lineno, col_offset, arena);
5269 if (*out == NULL) goto failed;
5270 return 0;
5271 }
5272 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5273 if (isinstance == -1) {
5274 return 1;
5275 }
5276 if (isinstance) {
5277 asdl_seq* names;
5278
5279 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5280 int res;
5281 Py_ssize_t len;
5282 Py_ssize_t i;
5283 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5284 if (tmp == NULL) goto failed;
5285 if (!PyList_Check(tmp)) {
5286 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5287 goto failed;
5288 }
5289 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005290 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005291 if (names == NULL) goto failed;
5292 for (i = 0; i < len; i++) {
5293 identifier value;
5294 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5295 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005296 if (len != PyList_GET_SIZE(tmp)) {
5297 PyErr_SetString(PyExc_RuntimeError, "Nonlocal field \"names\" changed size during iteration");
5298 goto failed;
5299 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005300 asdl_seq_SET(names, i, value);
5301 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005302 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005303 } else {
5304 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5305 return 1;
5306 }
5307 *out = Nonlocal(names, lineno, col_offset, arena);
5308 if (*out == NULL) goto failed;
5309 return 0;
5310 }
5311 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5312 if (isinstance == -1) {
5313 return 1;
5314 }
5315 if (isinstance) {
5316 expr_ty value;
5317
5318 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5319 int res;
5320 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5321 if (tmp == NULL) goto failed;
5322 res = obj2ast_expr(tmp, &value, arena);
5323 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005324 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005325 } else {
5326 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5327 return 1;
5328 }
5329 *out = Expr(value, lineno, col_offset, arena);
5330 if (*out == NULL) goto failed;
5331 return 0;
5332 }
5333 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5334 if (isinstance == -1) {
5335 return 1;
5336 }
5337 if (isinstance) {
5338
5339 *out = Pass(lineno, col_offset, arena);
5340 if (*out == NULL) goto failed;
5341 return 0;
5342 }
5343 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5344 if (isinstance == -1) {
5345 return 1;
5346 }
5347 if (isinstance) {
5348
5349 *out = Break(lineno, col_offset, arena);
5350 if (*out == NULL) goto failed;
5351 return 0;
5352 }
5353 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5354 if (isinstance == -1) {
5355 return 1;
5356 }
5357 if (isinstance) {
5358
5359 *out = Continue(lineno, col_offset, arena);
5360 if (*out == NULL) goto failed;
5361 return 0;
5362 }
5363
5364 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5365 failed:
5366 Py_XDECREF(tmp);
5367 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005368}
5369
5370int
5371obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5372{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005373 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005374
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005375 PyObject *tmp = NULL;
5376 int lineno;
5377 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005378
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005379 if (obj == Py_None) {
5380 *out = NULL;
5381 return 0;
5382 }
5383 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5384 int res;
5385 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5386 if (tmp == NULL) goto failed;
5387 res = obj2ast_int(tmp, &lineno, arena);
5388 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005389 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005390 } else {
5391 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005392 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005393 }
5394 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5395 int res;
5396 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5397 if (tmp == NULL) goto failed;
5398 res = obj2ast_int(tmp, &col_offset, arena);
5399 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005400 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005401 } else {
5402 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5403 return 1;
5404 }
5405 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5406 if (isinstance == -1) {
5407 return 1;
5408 }
5409 if (isinstance) {
5410 boolop_ty op;
5411 asdl_seq* values;
5412
5413 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5414 int res;
5415 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5416 if (tmp == NULL) goto failed;
5417 res = obj2ast_boolop(tmp, &op, arena);
5418 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005419 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005420 } else {
5421 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5422 return 1;
5423 }
5424 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5425 int res;
5426 Py_ssize_t len;
5427 Py_ssize_t i;
5428 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5429 if (tmp == NULL) goto failed;
5430 if (!PyList_Check(tmp)) {
5431 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5432 goto failed;
5433 }
5434 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005435 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005436 if (values == NULL) goto failed;
5437 for (i = 0; i < len; i++) {
5438 expr_ty value;
5439 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5440 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005441 if (len != PyList_GET_SIZE(tmp)) {
5442 PyErr_SetString(PyExc_RuntimeError, "BoolOp field \"values\" changed size during iteration");
5443 goto failed;
5444 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005445 asdl_seq_SET(values, i, value);
5446 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005447 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005448 } else {
5449 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5450 return 1;
5451 }
5452 *out = BoolOp(op, values, lineno, col_offset, arena);
5453 if (*out == NULL) goto failed;
5454 return 0;
5455 }
5456 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5457 if (isinstance == -1) {
5458 return 1;
5459 }
5460 if (isinstance) {
5461 expr_ty left;
5462 operator_ty op;
5463 expr_ty right;
5464
5465 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5466 int res;
5467 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5468 if (tmp == NULL) goto failed;
5469 res = obj2ast_expr(tmp, &left, arena);
5470 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005471 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005472 } else {
5473 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5474 return 1;
5475 }
5476 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5477 int res;
5478 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5479 if (tmp == NULL) goto failed;
5480 res = obj2ast_operator(tmp, &op, arena);
5481 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005482 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005483 } else {
5484 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5485 return 1;
5486 }
5487 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5488 int res;
5489 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5490 if (tmp == NULL) goto failed;
5491 res = obj2ast_expr(tmp, &right, arena);
5492 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005493 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005494 } else {
5495 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5496 return 1;
5497 }
5498 *out = BinOp(left, op, right, lineno, col_offset, arena);
5499 if (*out == NULL) goto failed;
5500 return 0;
5501 }
5502 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5503 if (isinstance == -1) {
5504 return 1;
5505 }
5506 if (isinstance) {
5507 unaryop_ty op;
5508 expr_ty operand;
5509
5510 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5511 int res;
5512 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5513 if (tmp == NULL) goto failed;
5514 res = obj2ast_unaryop(tmp, &op, arena);
5515 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005516 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005517 } else {
5518 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5519 return 1;
5520 }
5521 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5522 int res;
5523 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5524 if (tmp == NULL) goto failed;
5525 res = obj2ast_expr(tmp, &operand, arena);
5526 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005527 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005528 } else {
5529 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5530 return 1;
5531 }
5532 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5533 if (*out == NULL) goto failed;
5534 return 0;
5535 }
5536 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5537 if (isinstance == -1) {
5538 return 1;
5539 }
5540 if (isinstance) {
5541 arguments_ty args;
5542 expr_ty body;
5543
5544 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5545 int res;
5546 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5547 if (tmp == NULL) goto failed;
5548 res = obj2ast_arguments(tmp, &args, arena);
5549 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005550 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005551 } else {
5552 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5553 return 1;
5554 }
5555 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5556 int res;
5557 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5558 if (tmp == NULL) goto failed;
5559 res = obj2ast_expr(tmp, &body, arena);
5560 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005561 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005562 } else {
5563 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5564 return 1;
5565 }
5566 *out = Lambda(args, body, lineno, col_offset, arena);
5567 if (*out == NULL) goto failed;
5568 return 0;
5569 }
5570 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5571 if (isinstance == -1) {
5572 return 1;
5573 }
5574 if (isinstance) {
5575 expr_ty test;
5576 expr_ty body;
5577 expr_ty orelse;
5578
5579 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5580 int res;
5581 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5582 if (tmp == NULL) goto failed;
5583 res = obj2ast_expr(tmp, &test, arena);
5584 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005586 } else {
5587 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5588 return 1;
5589 }
5590 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5591 int res;
5592 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5593 if (tmp == NULL) goto failed;
5594 res = obj2ast_expr(tmp, &body, arena);
5595 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005596 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005597 } else {
5598 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5599 return 1;
5600 }
5601 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5602 int res;
5603 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5604 if (tmp == NULL) goto failed;
5605 res = obj2ast_expr(tmp, &orelse, arena);
5606 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005607 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005608 } else {
5609 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5610 return 1;
5611 }
5612 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5613 if (*out == NULL) goto failed;
5614 return 0;
5615 }
5616 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5617 if (isinstance == -1) {
5618 return 1;
5619 }
5620 if (isinstance) {
5621 asdl_seq* keys;
5622 asdl_seq* values;
5623
5624 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5625 int res;
5626 Py_ssize_t len;
5627 Py_ssize_t i;
5628 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5629 if (tmp == NULL) goto failed;
5630 if (!PyList_Check(tmp)) {
5631 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5632 goto failed;
5633 }
5634 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005635 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005636 if (keys == NULL) goto failed;
5637 for (i = 0; i < len; i++) {
5638 expr_ty value;
5639 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5640 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005641 if (len != PyList_GET_SIZE(tmp)) {
5642 PyErr_SetString(PyExc_RuntimeError, "Dict field \"keys\" changed size during iteration");
5643 goto failed;
5644 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005645 asdl_seq_SET(keys, i, value);
5646 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005648 } else {
5649 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5650 return 1;
5651 }
5652 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5653 int res;
5654 Py_ssize_t len;
5655 Py_ssize_t i;
5656 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5657 if (tmp == NULL) goto failed;
5658 if (!PyList_Check(tmp)) {
5659 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5660 goto failed;
5661 }
5662 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005663 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005664 if (values == NULL) goto failed;
5665 for (i = 0; i < len; i++) {
5666 expr_ty value;
5667 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5668 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005669 if (len != PyList_GET_SIZE(tmp)) {
5670 PyErr_SetString(PyExc_RuntimeError, "Dict field \"values\" changed size during iteration");
5671 goto failed;
5672 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005673 asdl_seq_SET(values, i, value);
5674 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005675 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005676 } else {
5677 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5678 return 1;
5679 }
5680 *out = Dict(keys, values, lineno, col_offset, arena);
5681 if (*out == NULL) goto failed;
5682 return 0;
5683 }
5684 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5685 if (isinstance == -1) {
5686 return 1;
5687 }
5688 if (isinstance) {
5689 asdl_seq* elts;
5690
5691 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5692 int res;
5693 Py_ssize_t len;
5694 Py_ssize_t i;
5695 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5696 if (tmp == NULL) goto failed;
5697 if (!PyList_Check(tmp)) {
5698 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5699 goto failed;
5700 }
5701 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005702 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005703 if (elts == NULL) goto failed;
5704 for (i = 0; i < len; i++) {
5705 expr_ty value;
5706 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5707 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005708 if (len != PyList_GET_SIZE(tmp)) {
5709 PyErr_SetString(PyExc_RuntimeError, "Set field \"elts\" changed size during iteration");
5710 goto failed;
5711 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005712 asdl_seq_SET(elts, i, value);
5713 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005714 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005715 } else {
5716 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5717 return 1;
5718 }
5719 *out = Set(elts, lineno, col_offset, arena);
5720 if (*out == NULL) goto failed;
5721 return 0;
5722 }
5723 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5724 if (isinstance == -1) {
5725 return 1;
5726 }
5727 if (isinstance) {
5728 expr_ty elt;
5729 asdl_seq* generators;
5730
5731 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5732 int res;
5733 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5734 if (tmp == NULL) goto failed;
5735 res = obj2ast_expr(tmp, &elt, arena);
5736 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005737 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005738 } else {
5739 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5740 return 1;
5741 }
5742 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5743 int res;
5744 Py_ssize_t len;
5745 Py_ssize_t i;
5746 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5747 if (tmp == NULL) goto failed;
5748 if (!PyList_Check(tmp)) {
5749 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5750 goto failed;
5751 }
5752 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005753 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005754 if (generators == NULL) goto failed;
5755 for (i = 0; i < len; i++) {
5756 comprehension_ty value;
5757 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5758 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005759 if (len != PyList_GET_SIZE(tmp)) {
5760 PyErr_SetString(PyExc_RuntimeError, "ListComp field \"generators\" changed size during iteration");
5761 goto failed;
5762 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005763 asdl_seq_SET(generators, i, value);
5764 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005765 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005766 } else {
5767 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5768 return 1;
5769 }
5770 *out = ListComp(elt, generators, lineno, col_offset, arena);
5771 if (*out == NULL) goto failed;
5772 return 0;
5773 }
5774 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5775 if (isinstance == -1) {
5776 return 1;
5777 }
5778 if (isinstance) {
5779 expr_ty elt;
5780 asdl_seq* generators;
5781
5782 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5783 int res;
5784 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5785 if (tmp == NULL) goto failed;
5786 res = obj2ast_expr(tmp, &elt, arena);
5787 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005788 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005789 } else {
5790 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5791 return 1;
5792 }
5793 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5794 int res;
5795 Py_ssize_t len;
5796 Py_ssize_t i;
5797 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5798 if (tmp == NULL) goto failed;
5799 if (!PyList_Check(tmp)) {
5800 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5801 goto failed;
5802 }
5803 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005804 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005805 if (generators == NULL) goto failed;
5806 for (i = 0; i < len; i++) {
5807 comprehension_ty value;
5808 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5809 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005810 if (len != PyList_GET_SIZE(tmp)) {
5811 PyErr_SetString(PyExc_RuntimeError, "SetComp field \"generators\" changed size during iteration");
5812 goto failed;
5813 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005814 asdl_seq_SET(generators, i, value);
5815 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005816 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005817 } else {
5818 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5819 return 1;
5820 }
5821 *out = SetComp(elt, generators, lineno, col_offset, arena);
5822 if (*out == NULL) goto failed;
5823 return 0;
5824 }
5825 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5826 if (isinstance == -1) {
5827 return 1;
5828 }
5829 if (isinstance) {
5830 expr_ty key;
5831 expr_ty value;
5832 asdl_seq* generators;
5833
5834 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5835 int res;
5836 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5837 if (tmp == NULL) goto failed;
5838 res = obj2ast_expr(tmp, &key, arena);
5839 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 } else {
5842 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5843 return 1;
5844 }
5845 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5846 int res;
5847 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5848 if (tmp == NULL) goto failed;
5849 res = obj2ast_expr(tmp, &value, arena);
5850 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005851 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005852 } else {
5853 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5854 return 1;
5855 }
5856 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5857 int res;
5858 Py_ssize_t len;
5859 Py_ssize_t i;
5860 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5861 if (tmp == NULL) goto failed;
5862 if (!PyList_Check(tmp)) {
5863 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5864 goto failed;
5865 }
5866 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005867 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005868 if (generators == NULL) goto failed;
5869 for (i = 0; i < len; i++) {
5870 comprehension_ty value;
5871 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5872 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005873 if (len != PyList_GET_SIZE(tmp)) {
5874 PyErr_SetString(PyExc_RuntimeError, "DictComp field \"generators\" changed size during iteration");
5875 goto failed;
5876 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005877 asdl_seq_SET(generators, i, value);
5878 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005880 } else {
5881 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5882 return 1;
5883 }
5884 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5885 if (*out == NULL) goto failed;
5886 return 0;
5887 }
5888 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5889 if (isinstance == -1) {
5890 return 1;
5891 }
5892 if (isinstance) {
5893 expr_ty elt;
5894 asdl_seq* generators;
5895
5896 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5897 int res;
5898 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5899 if (tmp == NULL) goto failed;
5900 res = obj2ast_expr(tmp, &elt, arena);
5901 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005902 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005903 } else {
5904 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5905 return 1;
5906 }
5907 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5908 int res;
5909 Py_ssize_t len;
5910 Py_ssize_t i;
5911 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5912 if (tmp == NULL) goto failed;
5913 if (!PyList_Check(tmp)) {
5914 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5915 goto failed;
5916 }
5917 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005918 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005919 if (generators == NULL) goto failed;
5920 for (i = 0; i < len; i++) {
5921 comprehension_ty value;
5922 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5923 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03005924 if (len != PyList_GET_SIZE(tmp)) {
5925 PyErr_SetString(PyExc_RuntimeError, "GeneratorExp field \"generators\" changed size during iteration");
5926 goto failed;
5927 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005928 asdl_seq_SET(generators, i, value);
5929 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005930 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005931 } else {
5932 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5933 return 1;
5934 }
5935 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5936 if (*out == NULL) goto failed;
5937 return 0;
5938 }
Yury Selivanov75445082015-05-11 22:57:16 -04005939 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5940 if (isinstance == -1) {
5941 return 1;
5942 }
5943 if (isinstance) {
5944 expr_ty value;
5945
5946 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5947 int res;
5948 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5949 if (tmp == NULL) goto failed;
5950 res = obj2ast_expr(tmp, &value, arena);
5951 if (res != 0) goto failed;
5952 Py_CLEAR(tmp);
5953 } else {
5954 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5955 return 1;
5956 }
5957 *out = Await(value, lineno, col_offset, arena);
5958 if (*out == NULL) goto failed;
5959 return 0;
5960 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005961 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5962 if (isinstance == -1) {
5963 return 1;
5964 }
5965 if (isinstance) {
5966 expr_ty value;
5967
Victor Stinneree4b59c2013-07-27 00:01:35 +02005968 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005969 int res;
5970 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5971 if (tmp == NULL) goto failed;
5972 res = obj2ast_expr(tmp, &value, arena);
5973 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005975 } else {
5976 value = NULL;
5977 }
5978 *out = Yield(value, lineno, col_offset, arena);
5979 if (*out == NULL) goto failed;
5980 return 0;
5981 }
5982 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5983 if (isinstance == -1) {
5984 return 1;
5985 }
5986 if (isinstance) {
5987 expr_ty value;
5988
5989 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5990 int res;
5991 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5992 if (tmp == NULL) goto failed;
5993 res = obj2ast_expr(tmp, &value, arena);
5994 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005995 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005996 } else {
5997 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5998 return 1;
5999 }
6000 *out = YieldFrom(value, lineno, col_offset, arena);
6001 if (*out == NULL) goto failed;
6002 return 0;
6003 }
6004 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
6005 if (isinstance == -1) {
6006 return 1;
6007 }
6008 if (isinstance) {
6009 expr_ty left;
6010 asdl_int_seq* ops;
6011 asdl_seq* comparators;
6012
6013 if (_PyObject_HasAttrId(obj, &PyId_left)) {
6014 int res;
6015 tmp = _PyObject_GetAttrId(obj, &PyId_left);
6016 if (tmp == NULL) goto failed;
6017 res = obj2ast_expr(tmp, &left, arena);
6018 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006019 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006020 } else {
6021 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
6022 return 1;
6023 }
6024 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
6025 int res;
6026 Py_ssize_t len;
6027 Py_ssize_t i;
6028 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
6029 if (tmp == NULL) goto failed;
6030 if (!PyList_Check(tmp)) {
6031 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6032 goto failed;
6033 }
6034 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006035 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006036 if (ops == NULL) goto failed;
6037 for (i = 0; i < len; i++) {
6038 cmpop_ty value;
6039 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6040 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006041 if (len != PyList_GET_SIZE(tmp)) {
6042 PyErr_SetString(PyExc_RuntimeError, "Compare field \"ops\" changed size during iteration");
6043 goto failed;
6044 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006045 asdl_seq_SET(ops, i, value);
6046 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006048 } else {
6049 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
6050 return 1;
6051 }
6052 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6053 int res;
6054 Py_ssize_t len;
6055 Py_ssize_t i;
6056 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6057 if (tmp == NULL) goto failed;
6058 if (!PyList_Check(tmp)) {
6059 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6060 goto failed;
6061 }
6062 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006063 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006064 if (comparators == NULL) goto failed;
6065 for (i = 0; i < len; i++) {
6066 expr_ty value;
6067 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6068 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006069 if (len != PyList_GET_SIZE(tmp)) {
6070 PyErr_SetString(PyExc_RuntimeError, "Compare field \"comparators\" changed size during iteration");
6071 goto failed;
6072 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006073 asdl_seq_SET(comparators, i, value);
6074 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006075 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006076 } else {
6077 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
6078 return 1;
6079 }
6080 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6081 if (*out == NULL) goto failed;
6082 return 0;
6083 }
6084 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6085 if (isinstance == -1) {
6086 return 1;
6087 }
6088 if (isinstance) {
6089 expr_ty func;
6090 asdl_seq* args;
6091 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006092
6093 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6094 int res;
6095 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6096 if (tmp == NULL) goto failed;
6097 res = obj2ast_expr(tmp, &func, arena);
6098 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006099 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006100 } else {
6101 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6102 return 1;
6103 }
6104 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6105 int res;
6106 Py_ssize_t len;
6107 Py_ssize_t i;
6108 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6109 if (tmp == NULL) goto failed;
6110 if (!PyList_Check(tmp)) {
6111 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6112 goto failed;
6113 }
6114 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006115 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006116 if (args == NULL) goto failed;
6117 for (i = 0; i < len; i++) {
6118 expr_ty value;
6119 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6120 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006121 if (len != PyList_GET_SIZE(tmp)) {
6122 PyErr_SetString(PyExc_RuntimeError, "Call field \"args\" changed size during iteration");
6123 goto failed;
6124 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006125 asdl_seq_SET(args, i, value);
6126 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006127 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006128 } else {
6129 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6130 return 1;
6131 }
6132 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6133 int res;
6134 Py_ssize_t len;
6135 Py_ssize_t i;
6136 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6137 if (tmp == NULL) goto failed;
6138 if (!PyList_Check(tmp)) {
6139 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6140 goto failed;
6141 }
6142 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006143 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006144 if (keywords == NULL) goto failed;
6145 for (i = 0; i < len; i++) {
6146 keyword_ty value;
6147 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6148 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006149 if (len != PyList_GET_SIZE(tmp)) {
6150 PyErr_SetString(PyExc_RuntimeError, "Call field \"keywords\" changed size during iteration");
6151 goto failed;
6152 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006153 asdl_seq_SET(keywords, i, value);
6154 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006155 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006156 } else {
6157 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6158 return 1;
6159 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006160 *out = Call(func, args, keywords, lineno, 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*)Num_type);
6165 if (isinstance == -1) {
6166 return 1;
6167 }
6168 if (isinstance) {
6169 object n;
6170
6171 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6172 int res;
6173 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6174 if (tmp == NULL) goto failed;
6175 res = obj2ast_object(tmp, &n, arena);
6176 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006177 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006178 } else {
6179 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6180 return 1;
6181 }
6182 *out = Num(n, lineno, col_offset, arena);
6183 if (*out == NULL) goto failed;
6184 return 0;
6185 }
6186 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
6191 string s;
6192
6193 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6194 int res;
6195 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6196 if (tmp == NULL) goto failed;
6197 res = obj2ast_string(tmp, &s, arena);
6198 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006200 } else {
6201 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6202 return 1;
6203 }
6204 *out = Str(s, lineno, col_offset, arena);
6205 if (*out == NULL) goto failed;
6206 return 0;
6207 }
6208 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6209 if (isinstance == -1) {
6210 return 1;
6211 }
6212 if (isinstance) {
6213 bytes s;
6214
6215 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6216 int res;
6217 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6218 if (tmp == NULL) goto failed;
6219 res = obj2ast_bytes(tmp, &s, arena);
6220 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006221 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006222 } else {
6223 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6224 return 1;
6225 }
6226 *out = Bytes(s, lineno, col_offset, arena);
6227 if (*out == NULL) goto failed;
6228 return 0;
6229 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006230 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6231 if (isinstance == -1) {
6232 return 1;
6233 }
6234 if (isinstance) {
6235 singleton value;
6236
6237 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6238 int res;
6239 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6240 if (tmp == NULL) goto failed;
6241 res = obj2ast_singleton(tmp, &value, arena);
6242 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006243 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006244 } else {
6245 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6246 return 1;
6247 }
6248 *out = NameConstant(value, lineno, col_offset, arena);
6249 if (*out == NULL) goto failed;
6250 return 0;
6251 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006252 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6253 if (isinstance == -1) {
6254 return 1;
6255 }
6256 if (isinstance) {
6257
6258 *out = Ellipsis(lineno, col_offset, arena);
6259 if (*out == NULL) goto failed;
6260 return 0;
6261 }
6262 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6263 if (isinstance == -1) {
6264 return 1;
6265 }
6266 if (isinstance) {
6267 expr_ty value;
6268 identifier attr;
6269 expr_context_ty ctx;
6270
6271 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6272 int res;
6273 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6274 if (tmp == NULL) goto failed;
6275 res = obj2ast_expr(tmp, &value, arena);
6276 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006277 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006278 } else {
6279 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6280 return 1;
6281 }
6282 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6283 int res;
6284 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6285 if (tmp == NULL) goto failed;
6286 res = obj2ast_identifier(tmp, &attr, arena);
6287 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006288 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006289 } else {
6290 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6291 return 1;
6292 }
6293 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6294 int res;
6295 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6296 if (tmp == NULL) goto failed;
6297 res = obj2ast_expr_context(tmp, &ctx, arena);
6298 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006299 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006300 } else {
6301 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6302 return 1;
6303 }
6304 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6305 if (*out == NULL) goto failed;
6306 return 0;
6307 }
6308 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6309 if (isinstance == -1) {
6310 return 1;
6311 }
6312 if (isinstance) {
6313 expr_ty value;
6314 slice_ty slice;
6315 expr_context_ty ctx;
6316
6317 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6318 int res;
6319 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6320 if (tmp == NULL) goto failed;
6321 res = obj2ast_expr(tmp, &value, arena);
6322 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006323 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006324 } else {
6325 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6326 return 1;
6327 }
6328 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6329 int res;
6330 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6331 if (tmp == NULL) goto failed;
6332 res = obj2ast_slice(tmp, &slice, arena);
6333 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006334 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006335 } else {
6336 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6337 return 1;
6338 }
6339 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6340 int res;
6341 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6342 if (tmp == NULL) goto failed;
6343 res = obj2ast_expr_context(tmp, &ctx, arena);
6344 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006345 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006346 } else {
6347 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6348 return 1;
6349 }
6350 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6351 if (*out == NULL) goto failed;
6352 return 0;
6353 }
6354 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6355 if (isinstance == -1) {
6356 return 1;
6357 }
6358 if (isinstance) {
6359 expr_ty value;
6360 expr_context_ty ctx;
6361
6362 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6363 int res;
6364 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6365 if (tmp == NULL) goto failed;
6366 res = obj2ast_expr(tmp, &value, arena);
6367 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006368 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006369 } else {
6370 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6371 return 1;
6372 }
6373 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6374 int res;
6375 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6376 if (tmp == NULL) goto failed;
6377 res = obj2ast_expr_context(tmp, &ctx, arena);
6378 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006379 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006380 } else {
6381 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6382 return 1;
6383 }
6384 *out = Starred(value, ctx, lineno, col_offset, arena);
6385 if (*out == NULL) goto failed;
6386 return 0;
6387 }
6388 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6389 if (isinstance == -1) {
6390 return 1;
6391 }
6392 if (isinstance) {
6393 identifier id;
6394 expr_context_ty ctx;
6395
6396 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6397 int res;
6398 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6399 if (tmp == NULL) goto failed;
6400 res = obj2ast_identifier(tmp, &id, arena);
6401 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006402 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006403 } else {
6404 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6405 return 1;
6406 }
6407 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6408 int res;
6409 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6410 if (tmp == NULL) goto failed;
6411 res = obj2ast_expr_context(tmp, &ctx, arena);
6412 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006413 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006414 } else {
6415 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6416 return 1;
6417 }
6418 *out = Name(id, ctx, lineno, col_offset, arena);
6419 if (*out == NULL) goto failed;
6420 return 0;
6421 }
6422 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6423 if (isinstance == -1) {
6424 return 1;
6425 }
6426 if (isinstance) {
6427 asdl_seq* elts;
6428 expr_context_ty ctx;
6429
6430 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6431 int res;
6432 Py_ssize_t len;
6433 Py_ssize_t i;
6434 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6435 if (tmp == NULL) goto failed;
6436 if (!PyList_Check(tmp)) {
6437 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6438 goto failed;
6439 }
6440 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006441 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006442 if (elts == NULL) goto failed;
6443 for (i = 0; i < len; i++) {
6444 expr_ty value;
6445 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6446 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006447 if (len != PyList_GET_SIZE(tmp)) {
6448 PyErr_SetString(PyExc_RuntimeError, "List field \"elts\" changed size during iteration");
6449 goto failed;
6450 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006451 asdl_seq_SET(elts, i, value);
6452 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006453 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006454 } else {
6455 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6456 return 1;
6457 }
6458 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6459 int res;
6460 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6461 if (tmp == NULL) goto failed;
6462 res = obj2ast_expr_context(tmp, &ctx, arena);
6463 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006464 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006465 } else {
6466 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6467 return 1;
6468 }
6469 *out = List(elts, ctx, lineno, col_offset, arena);
6470 if (*out == NULL) goto failed;
6471 return 0;
6472 }
6473 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6474 if (isinstance == -1) {
6475 return 1;
6476 }
6477 if (isinstance) {
6478 asdl_seq* elts;
6479 expr_context_ty ctx;
6480
6481 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6482 int res;
6483 Py_ssize_t len;
6484 Py_ssize_t i;
6485 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6486 if (tmp == NULL) goto failed;
6487 if (!PyList_Check(tmp)) {
6488 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6489 goto failed;
6490 }
6491 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006492 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006493 if (elts == NULL) goto failed;
6494 for (i = 0; i < len; i++) {
6495 expr_ty value;
6496 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6497 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006498 if (len != PyList_GET_SIZE(tmp)) {
6499 PyErr_SetString(PyExc_RuntimeError, "Tuple field \"elts\" changed size during iteration");
6500 goto failed;
6501 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006502 asdl_seq_SET(elts, i, value);
6503 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006504 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006505 } else {
6506 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6507 return 1;
6508 }
6509 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6510 int res;
6511 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6512 if (tmp == NULL) goto failed;
6513 res = obj2ast_expr_context(tmp, &ctx, arena);
6514 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006515 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006516 } else {
6517 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6518 return 1;
6519 }
6520 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6521 if (*out == NULL) goto failed;
6522 return 0;
6523 }
6524
6525 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6526 failed:
6527 Py_XDECREF(tmp);
6528 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006529}
6530
6531int
6532obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6533{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006534 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006535
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006536 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6537 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006538 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006539 }
6540 if (isinstance) {
6541 *out = Load;
6542 return 0;
6543 }
6544 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6545 if (isinstance == -1) {
6546 return 1;
6547 }
6548 if (isinstance) {
6549 *out = Store;
6550 return 0;
6551 }
6552 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6553 if (isinstance == -1) {
6554 return 1;
6555 }
6556 if (isinstance) {
6557 *out = Del;
6558 return 0;
6559 }
6560 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6561 if (isinstance == -1) {
6562 return 1;
6563 }
6564 if (isinstance) {
6565 *out = AugLoad;
6566 return 0;
6567 }
6568 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6569 if (isinstance == -1) {
6570 return 1;
6571 }
6572 if (isinstance) {
6573 *out = AugStore;
6574 return 0;
6575 }
6576 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6577 if (isinstance == -1) {
6578 return 1;
6579 }
6580 if (isinstance) {
6581 *out = Param;
6582 return 0;
6583 }
6584
6585 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6586 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006587}
6588
6589int
6590obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006592 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006593
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006594 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006595
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006596 if (obj == Py_None) {
6597 *out = NULL;
6598 return 0;
6599 }
6600 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6601 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006602 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006603 }
6604 if (isinstance) {
6605 expr_ty lower;
6606 expr_ty upper;
6607 expr_ty step;
6608
Victor Stinneree4b59c2013-07-27 00:01:35 +02006609 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006610 int res;
6611 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6612 if (tmp == NULL) goto failed;
6613 res = obj2ast_expr(tmp, &lower, arena);
6614 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006615 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006616 } else {
6617 lower = NULL;
6618 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006619 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006620 int res;
6621 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6622 if (tmp == NULL) goto failed;
6623 res = obj2ast_expr(tmp, &upper, arena);
6624 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006625 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006626 } else {
6627 upper = NULL;
6628 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006629 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006630 int res;
6631 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6632 if (tmp == NULL) goto failed;
6633 res = obj2ast_expr(tmp, &step, arena);
6634 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006635 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006636 } else {
6637 step = NULL;
6638 }
6639 *out = Slice(lower, upper, step, arena);
6640 if (*out == NULL) goto failed;
6641 return 0;
6642 }
6643 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6644 if (isinstance == -1) {
6645 return 1;
6646 }
6647 if (isinstance) {
6648 asdl_seq* dims;
6649
6650 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6651 int res;
6652 Py_ssize_t len;
6653 Py_ssize_t i;
6654 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6655 if (tmp == NULL) goto failed;
6656 if (!PyList_Check(tmp)) {
6657 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6658 goto failed;
6659 }
6660 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006661 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 if (dims == NULL) goto failed;
6663 for (i = 0; i < len; i++) {
6664 slice_ty value;
6665 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6666 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03006667 if (len != PyList_GET_SIZE(tmp)) {
6668 PyErr_SetString(PyExc_RuntimeError, "ExtSlice field \"dims\" changed size during iteration");
6669 goto failed;
6670 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006671 asdl_seq_SET(dims, i, value);
6672 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006673 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006674 } else {
6675 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6676 return 1;
6677 }
6678 *out = ExtSlice(dims, arena);
6679 if (*out == NULL) goto failed;
6680 return 0;
6681 }
6682 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6683 if (isinstance == -1) {
6684 return 1;
6685 }
6686 if (isinstance) {
6687 expr_ty value;
6688
6689 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6690 int res;
6691 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6692 if (tmp == NULL) goto failed;
6693 res = obj2ast_expr(tmp, &value, arena);
6694 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006696 } else {
6697 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6698 return 1;
6699 }
6700 *out = Index(value, arena);
6701 if (*out == NULL) goto failed;
6702 return 0;
6703 }
6704
6705 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6706 failed:
6707 Py_XDECREF(tmp);
6708 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006709}
6710
6711int
6712obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6713{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006714 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006715
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006716 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6717 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006718 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006719 }
6720 if (isinstance) {
6721 *out = And;
6722 return 0;
6723 }
6724 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6725 if (isinstance == -1) {
6726 return 1;
6727 }
6728 if (isinstance) {
6729 *out = Or;
6730 return 0;
6731 }
6732
6733 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6734 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006735}
6736
6737int
6738obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6739{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006740 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006741
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006742 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6743 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006744 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006745 }
6746 if (isinstance) {
6747 *out = Add;
6748 return 0;
6749 }
6750 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6751 if (isinstance == -1) {
6752 return 1;
6753 }
6754 if (isinstance) {
6755 *out = Sub;
6756 return 0;
6757 }
6758 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6759 if (isinstance == -1) {
6760 return 1;
6761 }
6762 if (isinstance) {
6763 *out = Mult;
6764 return 0;
6765 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006766 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6767 if (isinstance == -1) {
6768 return 1;
6769 }
6770 if (isinstance) {
6771 *out = MatMult;
6772 return 0;
6773 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006774 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6775 if (isinstance == -1) {
6776 return 1;
6777 }
6778 if (isinstance) {
6779 *out = Div;
6780 return 0;
6781 }
6782 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6783 if (isinstance == -1) {
6784 return 1;
6785 }
6786 if (isinstance) {
6787 *out = Mod;
6788 return 0;
6789 }
6790 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6791 if (isinstance == -1) {
6792 return 1;
6793 }
6794 if (isinstance) {
6795 *out = Pow;
6796 return 0;
6797 }
6798 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6799 if (isinstance == -1) {
6800 return 1;
6801 }
6802 if (isinstance) {
6803 *out = LShift;
6804 return 0;
6805 }
6806 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6807 if (isinstance == -1) {
6808 return 1;
6809 }
6810 if (isinstance) {
6811 *out = RShift;
6812 return 0;
6813 }
6814 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6815 if (isinstance == -1) {
6816 return 1;
6817 }
6818 if (isinstance) {
6819 *out = BitOr;
6820 return 0;
6821 }
6822 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6823 if (isinstance == -1) {
6824 return 1;
6825 }
6826 if (isinstance) {
6827 *out = BitXor;
6828 return 0;
6829 }
6830 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6831 if (isinstance == -1) {
6832 return 1;
6833 }
6834 if (isinstance) {
6835 *out = BitAnd;
6836 return 0;
6837 }
6838 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6839 if (isinstance == -1) {
6840 return 1;
6841 }
6842 if (isinstance) {
6843 *out = FloorDiv;
6844 return 0;
6845 }
6846
6847 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6848 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006849}
6850
6851int
6852obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6853{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006854 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006855
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006856 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6857 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006858 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006859 }
6860 if (isinstance) {
6861 *out = Invert;
6862 return 0;
6863 }
6864 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6865 if (isinstance == -1) {
6866 return 1;
6867 }
6868 if (isinstance) {
6869 *out = Not;
6870 return 0;
6871 }
6872 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6873 if (isinstance == -1) {
6874 return 1;
6875 }
6876 if (isinstance) {
6877 *out = UAdd;
6878 return 0;
6879 }
6880 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6881 if (isinstance == -1) {
6882 return 1;
6883 }
6884 if (isinstance) {
6885 *out = USub;
6886 return 0;
6887 }
6888
6889 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6890 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006891}
6892
6893int
6894obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6895{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006896 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006897
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006898 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6899 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006900 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006901 }
6902 if (isinstance) {
6903 *out = Eq;
6904 return 0;
6905 }
6906 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6907 if (isinstance == -1) {
6908 return 1;
6909 }
6910 if (isinstance) {
6911 *out = NotEq;
6912 return 0;
6913 }
6914 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6915 if (isinstance == -1) {
6916 return 1;
6917 }
6918 if (isinstance) {
6919 *out = Lt;
6920 return 0;
6921 }
6922 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6923 if (isinstance == -1) {
6924 return 1;
6925 }
6926 if (isinstance) {
6927 *out = LtE;
6928 return 0;
6929 }
6930 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6931 if (isinstance == -1) {
6932 return 1;
6933 }
6934 if (isinstance) {
6935 *out = Gt;
6936 return 0;
6937 }
6938 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6939 if (isinstance == -1) {
6940 return 1;
6941 }
6942 if (isinstance) {
6943 *out = GtE;
6944 return 0;
6945 }
6946 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6947 if (isinstance == -1) {
6948 return 1;
6949 }
6950 if (isinstance) {
6951 *out = Is;
6952 return 0;
6953 }
6954 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6955 if (isinstance == -1) {
6956 return 1;
6957 }
6958 if (isinstance) {
6959 *out = IsNot;
6960 return 0;
6961 }
6962 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6963 if (isinstance == -1) {
6964 return 1;
6965 }
6966 if (isinstance) {
6967 *out = In;
6968 return 0;
6969 }
6970 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6971 if (isinstance == -1) {
6972 return 1;
6973 }
6974 if (isinstance) {
6975 *out = NotIn;
6976 return 0;
6977 }
6978
6979 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6980 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006981}
6982
6983int
6984obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6985{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006986 PyObject* tmp = NULL;
6987 expr_ty target;
6988 expr_ty iter;
6989 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006990
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006991 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6992 int res;
6993 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6994 if (tmp == NULL) goto failed;
6995 res = obj2ast_expr(tmp, &target, arena);
6996 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006997 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006998 } else {
6999 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007000 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007001 }
7002 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7003 int res;
7004 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7005 if (tmp == NULL) goto failed;
7006 res = obj2ast_expr(tmp, &iter, arena);
7007 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007008 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007009 } else {
7010 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7011 return 1;
7012 }
7013 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7014 int res;
7015 Py_ssize_t len;
7016 Py_ssize_t i;
7017 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7018 if (tmp == NULL) goto failed;
7019 if (!PyList_Check(tmp)) {
7020 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7021 goto failed;
7022 }
7023 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007024 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007025 if (ifs == NULL) goto failed;
7026 for (i = 0; i < len; i++) {
7027 expr_ty value;
7028 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7029 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007030 if (len != PyList_GET_SIZE(tmp)) {
7031 PyErr_SetString(PyExc_RuntimeError, "comprehension field \"ifs\" changed size during iteration");
7032 goto failed;
7033 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007034 asdl_seq_SET(ifs, i, value);
7035 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007036 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007037 } else {
7038 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7039 return 1;
7040 }
7041 *out = comprehension(target, iter, ifs, arena);
7042 return 0;
7043failed:
7044 Py_XDECREF(tmp);
7045 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007046}
7047
7048int
7049obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7050{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007051 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007052
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007053 PyObject *tmp = NULL;
7054 int lineno;
7055 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007056
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007057 if (obj == Py_None) {
7058 *out = NULL;
7059 return 0;
7060 }
7061 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7062 int res;
7063 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7064 if (tmp == NULL) goto failed;
7065 res = obj2ast_int(tmp, &lineno, arena);
7066 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007067 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007068 } else {
7069 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007070 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007071 }
7072 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7073 int res;
7074 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7075 if (tmp == NULL) goto failed;
7076 res = obj2ast_int(tmp, &col_offset, arena);
7077 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007078 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007079 } else {
7080 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7081 return 1;
7082 }
7083 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7084 if (isinstance == -1) {
7085 return 1;
7086 }
7087 if (isinstance) {
7088 expr_ty type;
7089 identifier name;
7090 asdl_seq* body;
7091
Victor Stinneree4b59c2013-07-27 00:01:35 +02007092 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007093 int res;
7094 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7095 if (tmp == NULL) goto failed;
7096 res = obj2ast_expr(tmp, &type, arena);
7097 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007098 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007099 } else {
7100 type = NULL;
7101 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007102 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007103 int res;
7104 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7105 if (tmp == NULL) goto failed;
7106 res = obj2ast_identifier(tmp, &name, arena);
7107 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007108 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007109 } else {
7110 name = NULL;
7111 }
7112 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7113 int res;
7114 Py_ssize_t len;
7115 Py_ssize_t i;
7116 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7117 if (tmp == NULL) goto failed;
7118 if (!PyList_Check(tmp)) {
7119 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7120 goto failed;
7121 }
7122 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007123 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007124 if (body == NULL) goto failed;
7125 for (i = 0; i < len; i++) {
7126 stmt_ty value;
7127 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7128 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007129 if (len != PyList_GET_SIZE(tmp)) {
7130 PyErr_SetString(PyExc_RuntimeError, "ExceptHandler field \"body\" changed size during iteration");
7131 goto failed;
7132 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007133 asdl_seq_SET(body, i, value);
7134 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007135 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007136 } else {
7137 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7138 return 1;
7139 }
7140 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7141 if (*out == NULL) goto failed;
7142 return 0;
7143 }
7144
7145 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7146 failed:
7147 Py_XDECREF(tmp);
7148 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007149}
7150
7151int
7152obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7153{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007154 PyObject* tmp = NULL;
7155 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007156 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007157 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007158 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007159 arg_ty kwarg;
7160 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007161
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007162 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7163 int res;
7164 Py_ssize_t len;
7165 Py_ssize_t i;
7166 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7167 if (tmp == NULL) goto failed;
7168 if (!PyList_Check(tmp)) {
7169 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7170 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007171 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007172 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007173 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007174 if (args == NULL) goto failed;
7175 for (i = 0; i < len; i++) {
7176 arg_ty value;
7177 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7178 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007179 if (len != PyList_GET_SIZE(tmp)) {
7180 PyErr_SetString(PyExc_RuntimeError, "arguments field \"args\" changed size during iteration");
7181 goto failed;
7182 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007183 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007184 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007185 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007186 } else {
7187 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007188 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007189 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007190 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007191 int res;
7192 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7193 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007194 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007195 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007196 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007197 } else {
7198 vararg = NULL;
7199 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007200 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7201 int res;
7202 Py_ssize_t len;
7203 Py_ssize_t i;
7204 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7205 if (tmp == NULL) goto failed;
7206 if (!PyList_Check(tmp)) {
7207 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7208 goto failed;
7209 }
7210 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007211 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007212 if (kwonlyargs == NULL) goto failed;
7213 for (i = 0; i < len; i++) {
7214 arg_ty value;
7215 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7216 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007217 if (len != PyList_GET_SIZE(tmp)) {
7218 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kwonlyargs\" changed size during iteration");
7219 goto failed;
7220 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007221 asdl_seq_SET(kwonlyargs, i, value);
7222 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007224 } else {
7225 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7226 return 1;
7227 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007228 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7229 int res;
7230 Py_ssize_t len;
7231 Py_ssize_t i;
7232 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7233 if (tmp == NULL) goto failed;
7234 if (!PyList_Check(tmp)) {
7235 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7236 goto failed;
7237 }
7238 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007239 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007240 if (kw_defaults == NULL) goto failed;
7241 for (i = 0; i < len; i++) {
7242 expr_ty value;
7243 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7244 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007245 if (len != PyList_GET_SIZE(tmp)) {
7246 PyErr_SetString(PyExc_RuntimeError, "arguments field \"kw_defaults\" changed size during iteration");
7247 goto failed;
7248 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007249 asdl_seq_SET(kw_defaults, i, value);
7250 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007251 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007252 } else {
7253 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7254 return 1;
7255 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007256 if (exists_not_none(obj, &PyId_kwarg)) {
7257 int res;
7258 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7259 if (tmp == NULL) goto failed;
7260 res = obj2ast_arg(tmp, &kwarg, arena);
7261 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007262 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007263 } else {
7264 kwarg = NULL;
7265 }
7266 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7267 int res;
7268 Py_ssize_t len;
7269 Py_ssize_t i;
7270 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7271 if (tmp == NULL) goto failed;
7272 if (!PyList_Check(tmp)) {
7273 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" 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 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007278 if (defaults == NULL) goto failed;
7279 for (i = 0; i < len; i++) {
7280 expr_ty value;
7281 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7282 if (res != 0) goto failed;
Serhiy Storchakacf380602016-10-07 21:51:28 +03007283 if (len != PyList_GET_SIZE(tmp)) {
7284 PyErr_SetString(PyExc_RuntimeError, "arguments field \"defaults\" changed size during iteration");
7285 goto failed;
7286 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007287 asdl_seq_SET(defaults, i, value);
7288 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007289 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007290 } else {
7291 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7292 return 1;
7293 }
7294 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7295 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007296 return 0;
7297failed:
7298 Py_XDECREF(tmp);
7299 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007300}
7301
7302int
7303obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7304{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007305 PyObject* tmp = NULL;
7306 identifier arg;
7307 expr_ty annotation;
Victor Stinnerf9827ea2015-11-06 17:01:48 +01007308 int lineno;
7309 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007310
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007311 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7312 int res;
7313 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7314 if (tmp == NULL) goto failed;
7315 res = obj2ast_identifier(tmp, &arg, arena);
7316 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007317 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007318 } else {
7319 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007320 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007321 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007322 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007323 int res;
7324 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7325 if (tmp == NULL) goto failed;
7326 res = obj2ast_expr(tmp, &annotation, arena);
7327 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007328 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007329 } else {
7330 annotation = NULL;
7331 }
Victor Stinnerf9827ea2015-11-06 17:01:48 +01007332 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7333 int res;
7334 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7335 if (tmp == NULL) goto failed;
7336 res = obj2ast_int(tmp, &lineno, arena);
7337 if (res != 0) goto failed;
7338 Py_CLEAR(tmp);
7339 } else {
7340 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7341 return 1;
7342 }
7343 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7344 int res;
7345 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7346 if (tmp == NULL) goto failed;
7347 res = obj2ast_int(tmp, &col_offset, arena);
7348 if (res != 0) goto failed;
7349 Py_CLEAR(tmp);
7350 } else {
7351 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7352 return 1;
7353 }
7354 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007355 return 0;
7356failed:
7357 Py_XDECREF(tmp);
7358 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007359}
7360
7361int
7362obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7363{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007364 PyObject* tmp = NULL;
7365 identifier arg;
7366 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007367
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007368 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007369 int res;
7370 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7371 if (tmp == NULL) goto failed;
7372 res = obj2ast_identifier(tmp, &arg, arena);
7373 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007374 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007375 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007376 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007377 }
7378 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7379 int res;
7380 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7381 if (tmp == NULL) goto failed;
7382 res = obj2ast_expr(tmp, &value, arena);
7383 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007384 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007385 } else {
7386 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7387 return 1;
7388 }
7389 *out = keyword(arg, value, arena);
7390 return 0;
7391failed:
7392 Py_XDECREF(tmp);
7393 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007394}
7395
7396int
7397obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7398{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007399 PyObject* tmp = NULL;
7400 identifier name;
7401 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007402
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007403 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7404 int res;
7405 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7406 if (tmp == NULL) goto failed;
7407 res = obj2ast_identifier(tmp, &name, arena);
7408 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007409 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007410 } else {
7411 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007412 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007413 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007414 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007415 int res;
7416 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7417 if (tmp == NULL) goto failed;
7418 res = obj2ast_identifier(tmp, &asname, arena);
7419 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007420 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007421 } else {
7422 asname = NULL;
7423 }
7424 *out = alias(name, asname, arena);
7425 return 0;
7426failed:
7427 Py_XDECREF(tmp);
7428 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007429}
7430
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007431int
7432obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7433{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007434 PyObject* tmp = NULL;
7435 expr_ty context_expr;
7436 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007437
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007438 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7439 int res;
7440 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7441 if (tmp == NULL) goto failed;
7442 res = obj2ast_expr(tmp, &context_expr, arena);
7443 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007444 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007445 } else {
7446 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007447 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007448 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007449 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007450 int res;
7451 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7452 if (tmp == NULL) goto failed;
7453 res = obj2ast_expr(tmp, &optional_vars, arena);
7454 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007455 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007456 } else {
7457 optional_vars = NULL;
7458 }
7459 *out = withitem(context_expr, optional_vars, arena);
7460 return 0;
7461failed:
7462 Py_XDECREF(tmp);
7463 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007464}
7465
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007466
Martin v. Löwis1a214512008-06-11 05:26:20 +00007467static struct PyModuleDef _astmodule = {
7468 PyModuleDef_HEAD_INIT, "_ast"
7469};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007470PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007471PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007472{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007473 PyObject *m, *d;
7474 if (!init_types()) return NULL;
7475 m = PyModule_Create(&_astmodule);
7476 if (!m) return NULL;
7477 d = PyModule_GetDict(m);
7478 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007479 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007480 return NULL;
7481 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7482 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7483 NULL;
7484 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7485 0) return NULL;
7486 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7487 return NULL;
7488 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7489 NULL;
7490 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7491 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7492 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007493 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7494 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007495 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7496 return NULL;
7497 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7498 NULL;
7499 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7500 NULL;
7501 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7502 NULL;
7503 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7504 return NULL;
7505 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007506 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7507 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007508 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7509 NULL;
7510 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7511 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007512 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7513 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007514 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7515 NULL;
7516 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7517 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7518 NULL;
7519 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7520 NULL;
7521 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7522 return NULL;
7523 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7524 NULL;
7525 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7526 return NULL;
7527 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7528 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7529 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7530 NULL;
7531 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7532 return NULL;
7533 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7534 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7535 NULL;
7536 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7537 NULL;
7538 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7539 NULL;
7540 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7541 NULL;
7542 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7543 NULL;
7544 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7545 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7546 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7547 return NULL;
7548 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7549 NULL;
7550 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7551 return NULL;
7552 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7553 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007554 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7555 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007556 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7557 NULL;
7558 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7559 return NULL;
7560 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7561 NULL;
7562 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7563 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7564 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
7565 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7566 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007567 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7568 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007569 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7570 return NULL;
7571 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7572 return NULL;
7573 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7574 return NULL;
7575 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7576 NULL;
7577 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7578 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7579 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7580 NULL;
7581 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7582 0) return NULL;
7583 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7584 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7585 NULL;
7586 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7587 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7588 NULL;
7589 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7590 return NULL;
7591 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7592 NULL;
7593 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7594 NULL;
7595 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7596 NULL;
7597 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7598 return NULL;
7599 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7600 NULL;
7601 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7602 NULL;
7603 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7604 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7605 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7606 return NULL;
7607 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7608 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7609 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007610 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7611 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007612 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7613 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7614 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7615 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7616 NULL;
7617 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7618 NULL;
7619 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7620 NULL;
7621 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7622 NULL;
7623 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7624 NULL;
7625 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7626 return NULL;
7627 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7628 NULL;
7629 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7630 NULL;
7631 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7632 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7633 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7634 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7635 NULL;
7636 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7637 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7638 NULL;
7639 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7640 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7641 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7642 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7643 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7644 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7645 NULL;
7646 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7647 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7648 NULL;
7649 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7650 < 0) return NULL;
7651 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7652 < 0) return NULL;
7653 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7654 < 0) return NULL;
7655 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7656 return NULL;
7657 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7658 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7659 NULL;
7660 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7661 NULL;
7662 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7663 return NULL;
7664 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007665}
7666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007667
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007668PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007669{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007670 if (!init_types())
7671 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007672 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007673}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007674
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007675/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7676mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007677{
7678 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007679 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007680 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007681 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007682
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007683 req_type[0] = (PyObject*)Module_type;
7684 req_type[1] = (PyObject*)Expression_type;
7685 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007686
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007687 assert(0 <= mode && mode <= 2);
7688
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007689 if (!init_types())
7690 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007691
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007692 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7693 if (isinstance == -1)
7694 return NULL;
7695 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007696 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7697 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007698 return NULL;
7699 }
7700 if (obj2ast_mod(ast, &res, arena) != 0)
7701 return NULL;
7702 else
7703 return res;
7704}
7705
7706int PyAST_Check(PyObject* obj)
7707{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007708 if (!init_types())
7709 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007710 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007711}
7712
Martin v. Löwis5b222132007-06-10 09:51:05 +00007713