blob: 8a2dc7cc54dc3e09ab608be38d038bcc47427007 [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
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002375arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002376{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002377 arg_ty p;
2378 if (!arg) {
2379 PyErr_SetString(PyExc_ValueError,
2380 "field arg is required for arg");
2381 return NULL;
2382 }
2383 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2384 if (!p)
2385 return NULL;
2386 p->arg = arg;
2387 p->annotation = annotation;
2388 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002389}
2390
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002392keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002394 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002395 if (!value) {
2396 PyErr_SetString(PyExc_ValueError,
2397 "field value is required for keyword");
2398 return NULL;
2399 }
2400 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2401 if (!p)
2402 return NULL;
2403 p->arg = arg;
2404 p->value = value;
2405 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406}
2407
2408alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002409alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002411 alias_ty p;
2412 if (!name) {
2413 PyErr_SetString(PyExc_ValueError,
2414 "field name is required for alias");
2415 return NULL;
2416 }
2417 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2418 if (!p)
2419 return NULL;
2420 p->name = name;
2421 p->asname = asname;
2422 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423}
2424
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002425withitem_ty
2426withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2427{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002428 withitem_ty p;
2429 if (!context_expr) {
2430 PyErr_SetString(PyExc_ValueError,
2431 "field context_expr is required for withitem");
2432 return NULL;
2433 }
2434 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2435 if (!p)
2436 return NULL;
2437 p->context_expr = context_expr;
2438 p->optional_vars = optional_vars;
2439 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002440}
2441
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002442
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002443PyObject*
2444ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002445{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002446 mod_ty o = (mod_ty)_o;
2447 PyObject *result = NULL, *value = NULL;
2448 if (!o) {
2449 Py_INCREF(Py_None);
2450 return Py_None;
2451 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002452
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002453 switch (o->kind) {
2454 case Module_kind:
2455 result = PyType_GenericNew(Module_type, NULL, NULL);
2456 if (!result) goto failed;
2457 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2458 if (!value) goto failed;
2459 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2460 goto failed;
2461 Py_DECREF(value);
2462 break;
2463 case Interactive_kind:
2464 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2465 if (!result) goto failed;
2466 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2467 if (!value) goto failed;
2468 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2469 goto failed;
2470 Py_DECREF(value);
2471 break;
2472 case Expression_kind:
2473 result = PyType_GenericNew(Expression_type, NULL, NULL);
2474 if (!result) goto failed;
2475 value = ast2obj_expr(o->v.Expression.body);
2476 if (!value) goto failed;
2477 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2478 goto failed;
2479 Py_DECREF(value);
2480 break;
2481 case Suite_kind:
2482 result = PyType_GenericNew(Suite_type, NULL, NULL);
2483 if (!result) goto failed;
2484 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2485 if (!value) goto failed;
2486 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2487 goto failed;
2488 Py_DECREF(value);
2489 break;
2490 }
2491 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002492failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002493 Py_XDECREF(value);
2494 Py_XDECREF(result);
2495 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496}
2497
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002498PyObject*
2499ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002501 stmt_ty o = (stmt_ty)_o;
2502 PyObject *result = NULL, *value = NULL;
2503 if (!o) {
2504 Py_INCREF(Py_None);
2505 return Py_None;
2506 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002507
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002508 switch (o->kind) {
2509 case FunctionDef_kind:
2510 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2511 if (!result) goto failed;
2512 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002513 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002514 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2515 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002516 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002517 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002518 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002519 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2520 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002521 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002522 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2523 if (!value) goto failed;
2524 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2525 goto failed;
2526 Py_DECREF(value);
2527 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2528 if (!value) goto failed;
2529 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2530 goto failed;
2531 Py_DECREF(value);
2532 value = ast2obj_expr(o->v.FunctionDef.returns);
2533 if (!value) goto failed;
2534 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2535 goto failed;
2536 Py_DECREF(value);
2537 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002538 case AsyncFunctionDef_kind:
2539 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2540 if (!result) goto failed;
2541 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2542 if (!value) goto failed;
2543 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2544 goto failed;
2545 Py_DECREF(value);
2546 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2547 if (!value) goto failed;
2548 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2549 goto failed;
2550 Py_DECREF(value);
2551 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2552 if (!value) goto failed;
2553 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2554 goto failed;
2555 Py_DECREF(value);
2556 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2557 ast2obj_expr);
2558 if (!value) goto failed;
2559 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2560 goto failed;
2561 Py_DECREF(value);
2562 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2563 if (!value) goto failed;
2564 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002568 case ClassDef_kind:
2569 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2570 if (!result) goto failed;
2571 value = ast2obj_identifier(o->v.ClassDef.name);
2572 if (!value) goto failed;
2573 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2574 goto failed;
2575 Py_DECREF(value);
2576 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2577 if (!value) goto failed;
2578 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2582 if (!value) goto failed;
2583 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2584 goto failed;
2585 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002586 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2587 if (!value) goto failed;
2588 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2589 goto failed;
2590 Py_DECREF(value);
2591 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2592 if (!value) goto failed;
2593 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2594 goto failed;
2595 Py_DECREF(value);
2596 break;
2597 case Return_kind:
2598 result = PyType_GenericNew(Return_type, NULL, NULL);
2599 if (!result) goto failed;
2600 value = ast2obj_expr(o->v.Return.value);
2601 if (!value) goto failed;
2602 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2603 goto failed;
2604 Py_DECREF(value);
2605 break;
2606 case Delete_kind:
2607 result = PyType_GenericNew(Delete_type, NULL, NULL);
2608 if (!result) goto failed;
2609 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2610 if (!value) goto failed;
2611 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 break;
2615 case Assign_kind:
2616 result = PyType_GenericNew(Assign_type, NULL, NULL);
2617 if (!result) goto failed;
2618 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2619 if (!value) goto failed;
2620 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2621 goto failed;
2622 Py_DECREF(value);
2623 value = ast2obj_expr(o->v.Assign.value);
2624 if (!value) goto failed;
2625 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 break;
2629 case AugAssign_kind:
2630 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2631 if (!result) goto failed;
2632 value = ast2obj_expr(o->v.AugAssign.target);
2633 if (!value) goto failed;
2634 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 value = ast2obj_operator(o->v.AugAssign.op);
2638 if (!value) goto failed;
2639 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2640 goto failed;
2641 Py_DECREF(value);
2642 value = ast2obj_expr(o->v.AugAssign.value);
2643 if (!value) goto failed;
2644 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2645 goto failed;
2646 Py_DECREF(value);
2647 break;
2648 case For_kind:
2649 result = PyType_GenericNew(For_type, NULL, NULL);
2650 if (!result) goto failed;
2651 value = ast2obj_expr(o->v.For.target);
2652 if (!value) goto failed;
2653 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2654 goto failed;
2655 Py_DECREF(value);
2656 value = ast2obj_expr(o->v.For.iter);
2657 if (!value) goto failed;
2658 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
2661 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2662 if (!value) goto failed;
2663 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2664 goto failed;
2665 Py_DECREF(value);
2666 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2667 if (!value) goto failed;
2668 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2669 goto failed;
2670 Py_DECREF(value);
2671 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002672 case AsyncFor_kind:
2673 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2674 if (!result) goto failed;
2675 value = ast2obj_expr(o->v.AsyncFor.target);
2676 if (!value) goto failed;
2677 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2678 goto failed;
2679 Py_DECREF(value);
2680 value = ast2obj_expr(o->v.AsyncFor.iter);
2681 if (!value) goto failed;
2682 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2691 if (!value) goto failed;
2692 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002696 case While_kind:
2697 result = PyType_GenericNew(While_type, NULL, NULL);
2698 if (!result) goto failed;
2699 value = ast2obj_expr(o->v.While.test);
2700 if (!value) goto failed;
2701 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 break;
2715 case If_kind:
2716 result = PyType_GenericNew(If_type, NULL, NULL);
2717 if (!result) goto failed;
2718 value = ast2obj_expr(o->v.If.test);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
2723 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2724 if (!value) goto failed;
2725 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 break;
2734 case With_kind:
2735 result = PyType_GenericNew(With_type, NULL, NULL);
2736 if (!result) goto failed;
2737 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2743 if (!value) goto failed;
2744 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2745 goto failed;
2746 Py_DECREF(value);
2747 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002748 case AsyncWith_kind:
2749 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2750 if (!result) goto failed;
2751 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2752 if (!value) goto failed;
2753 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2757 if (!value) goto failed;
2758 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
2761 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002762 case Raise_kind:
2763 result = PyType_GenericNew(Raise_type, NULL, NULL);
2764 if (!result) goto failed;
2765 value = ast2obj_expr(o->v.Raise.exc);
2766 if (!value) goto failed;
2767 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
2770 value = ast2obj_expr(o->v.Raise.cause);
2771 if (!value) goto failed;
2772 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 break;
2776 case Try_kind:
2777 result = PyType_GenericNew(Try_type, NULL, NULL);
2778 if (!result) goto failed;
2779 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2785 if (!value) goto failed;
2786 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
2789 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2790 if (!value) goto failed;
2791 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2792 goto failed;
2793 Py_DECREF(value);
2794 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2795 if (!value) goto failed;
2796 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2797 goto failed;
2798 Py_DECREF(value);
2799 break;
2800 case Assert_kind:
2801 result = PyType_GenericNew(Assert_type, NULL, NULL);
2802 if (!result) goto failed;
2803 value = ast2obj_expr(o->v.Assert.test);
2804 if (!value) goto failed;
2805 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2806 goto failed;
2807 Py_DECREF(value);
2808 value = ast2obj_expr(o->v.Assert.msg);
2809 if (!value) goto failed;
2810 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2811 goto failed;
2812 Py_DECREF(value);
2813 break;
2814 case Import_kind:
2815 result = PyType_GenericNew(Import_type, NULL, NULL);
2816 if (!result) goto failed;
2817 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2818 if (!value) goto failed;
2819 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2820 goto failed;
2821 Py_DECREF(value);
2822 break;
2823 case ImportFrom_kind:
2824 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2825 if (!result) goto failed;
2826 value = ast2obj_identifier(o->v.ImportFrom.module);
2827 if (!value) goto failed;
2828 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
2831 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2832 if (!value) goto failed;
2833 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2834 goto failed;
2835 Py_DECREF(value);
2836 value = ast2obj_int(o->v.ImportFrom.level);
2837 if (!value) goto failed;
2838 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2839 goto failed;
2840 Py_DECREF(value);
2841 break;
2842 case Global_kind:
2843 result = PyType_GenericNew(Global_type, NULL, NULL);
2844 if (!result) goto failed;
2845 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2846 if (!value) goto failed;
2847 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2848 goto failed;
2849 Py_DECREF(value);
2850 break;
2851 case Nonlocal_kind:
2852 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2853 if (!result) goto failed;
2854 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2855 if (!value) goto failed;
2856 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2857 goto failed;
2858 Py_DECREF(value);
2859 break;
2860 case Expr_kind:
2861 result = PyType_GenericNew(Expr_type, NULL, NULL);
2862 if (!result) goto failed;
2863 value = ast2obj_expr(o->v.Expr.value);
2864 if (!value) goto failed;
2865 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2866 goto failed;
2867 Py_DECREF(value);
2868 break;
2869 case Pass_kind:
2870 result = PyType_GenericNew(Pass_type, NULL, NULL);
2871 if (!result) goto failed;
2872 break;
2873 case Break_kind:
2874 result = PyType_GenericNew(Break_type, NULL, NULL);
2875 if (!result) goto failed;
2876 break;
2877 case Continue_kind:
2878 result = PyType_GenericNew(Continue_type, NULL, NULL);
2879 if (!result) goto failed;
2880 break;
2881 }
2882 value = ast2obj_int(o->lineno);
2883 if (!value) goto failed;
2884 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2885 goto failed;
2886 Py_DECREF(value);
2887 value = ast2obj_int(o->col_offset);
2888 if (!value) goto failed;
2889 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2890 goto failed;
2891 Py_DECREF(value);
2892 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002893failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002894 Py_XDECREF(value);
2895 Py_XDECREF(result);
2896 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897}
2898
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002899PyObject*
2900ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002902 expr_ty o = (expr_ty)_o;
2903 PyObject *result = NULL, *value = NULL;
2904 if (!o) {
2905 Py_INCREF(Py_None);
2906 return Py_None;
2907 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002908
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002909 switch (o->kind) {
2910 case BoolOp_kind:
2911 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2912 if (!result) goto failed;
2913 value = ast2obj_boolop(o->v.BoolOp.op);
2914 if (!value) goto failed;
2915 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2916 goto failed;
2917 Py_DECREF(value);
2918 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2919 if (!value) goto failed;
2920 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2921 goto failed;
2922 Py_DECREF(value);
2923 break;
2924 case BinOp_kind:
2925 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2926 if (!result) goto failed;
2927 value = ast2obj_expr(o->v.BinOp.left);
2928 if (!value) goto failed;
2929 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2930 goto failed;
2931 Py_DECREF(value);
2932 value = ast2obj_operator(o->v.BinOp.op);
2933 if (!value) goto failed;
2934 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2935 goto failed;
2936 Py_DECREF(value);
2937 value = ast2obj_expr(o->v.BinOp.right);
2938 if (!value) goto failed;
2939 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2940 goto failed;
2941 Py_DECREF(value);
2942 break;
2943 case UnaryOp_kind:
2944 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2945 if (!result) goto failed;
2946 value = ast2obj_unaryop(o->v.UnaryOp.op);
2947 if (!value) goto failed;
2948 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2949 goto failed;
2950 Py_DECREF(value);
2951 value = ast2obj_expr(o->v.UnaryOp.operand);
2952 if (!value) goto failed;
2953 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2954 goto failed;
2955 Py_DECREF(value);
2956 break;
2957 case Lambda_kind:
2958 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2959 if (!result) goto failed;
2960 value = ast2obj_arguments(o->v.Lambda.args);
2961 if (!value) goto failed;
2962 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
2965 value = ast2obj_expr(o->v.Lambda.body);
2966 if (!value) goto failed;
2967 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2968 goto failed;
2969 Py_DECREF(value);
2970 break;
2971 case IfExp_kind:
2972 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2973 if (!result) goto failed;
2974 value = ast2obj_expr(o->v.IfExp.test);
2975 if (!value) goto failed;
2976 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2977 goto failed;
2978 Py_DECREF(value);
2979 value = ast2obj_expr(o->v.IfExp.body);
2980 if (!value) goto failed;
2981 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2982 goto failed;
2983 Py_DECREF(value);
2984 value = ast2obj_expr(o->v.IfExp.orelse);
2985 if (!value) goto failed;
2986 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2987 goto failed;
2988 Py_DECREF(value);
2989 break;
2990 case Dict_kind:
2991 result = PyType_GenericNew(Dict_type, NULL, NULL);
2992 if (!result) goto failed;
2993 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2994 if (!value) goto failed;
2995 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2996 goto failed;
2997 Py_DECREF(value);
2998 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2999 if (!value) goto failed;
3000 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3001 goto failed;
3002 Py_DECREF(value);
3003 break;
3004 case Set_kind:
3005 result = PyType_GenericNew(Set_type, NULL, NULL);
3006 if (!result) goto failed;
3007 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3008 if (!value) goto failed;
3009 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3010 goto failed;
3011 Py_DECREF(value);
3012 break;
3013 case ListComp_kind:
3014 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3015 if (!result) goto failed;
3016 value = ast2obj_expr(o->v.ListComp.elt);
3017 if (!value) goto failed;
3018 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
3021 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3022 if (!value) goto failed;
3023 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3024 goto failed;
3025 Py_DECREF(value);
3026 break;
3027 case SetComp_kind:
3028 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3029 if (!result) goto failed;
3030 value = ast2obj_expr(o->v.SetComp.elt);
3031 if (!value) goto failed;
3032 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3033 goto failed;
3034 Py_DECREF(value);
3035 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3036 if (!value) goto failed;
3037 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3038 goto failed;
3039 Py_DECREF(value);
3040 break;
3041 case DictComp_kind:
3042 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3043 if (!result) goto failed;
3044 value = ast2obj_expr(o->v.DictComp.key);
3045 if (!value) goto failed;
3046 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3047 goto failed;
3048 Py_DECREF(value);
3049 value = ast2obj_expr(o->v.DictComp.value);
3050 if (!value) goto failed;
3051 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3052 goto failed;
3053 Py_DECREF(value);
3054 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3055 if (!value) goto failed;
3056 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3057 goto failed;
3058 Py_DECREF(value);
3059 break;
3060 case GeneratorExp_kind:
3061 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3062 if (!result) goto failed;
3063 value = ast2obj_expr(o->v.GeneratorExp.elt);
3064 if (!value) goto failed;
3065 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3066 goto failed;
3067 Py_DECREF(value);
3068 value = ast2obj_list(o->v.GeneratorExp.generators,
3069 ast2obj_comprehension);
3070 if (!value) goto failed;
3071 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3072 goto failed;
3073 Py_DECREF(value);
3074 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003075 case Await_kind:
3076 result = PyType_GenericNew(Await_type, NULL, NULL);
3077 if (!result) goto failed;
3078 value = ast2obj_expr(o->v.Await.value);
3079 if (!value) goto failed;
3080 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3081 goto failed;
3082 Py_DECREF(value);
3083 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003084 case Yield_kind:
3085 result = PyType_GenericNew(Yield_type, NULL, NULL);
3086 if (!result) goto failed;
3087 value = ast2obj_expr(o->v.Yield.value);
3088 if (!value) goto failed;
3089 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3090 goto failed;
3091 Py_DECREF(value);
3092 break;
3093 case YieldFrom_kind:
3094 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3095 if (!result) goto failed;
3096 value = ast2obj_expr(o->v.YieldFrom.value);
3097 if (!value) goto failed;
3098 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3099 goto failed;
3100 Py_DECREF(value);
3101 break;
3102 case Compare_kind:
3103 result = PyType_GenericNew(Compare_type, NULL, NULL);
3104 if (!result) goto failed;
3105 value = ast2obj_expr(o->v.Compare.left);
3106 if (!value) goto failed;
3107 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3108 goto failed;
3109 Py_DECREF(value);
3110 {
3111 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3112 value = PyList_New(n);
3113 if (!value) goto failed;
3114 for(i = 0; i < n; i++)
3115 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003117 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003118 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3119 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003120 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003121 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003122 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003123 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3124 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003125 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003126 break;
3127 case Call_kind:
3128 result = PyType_GenericNew(Call_type, NULL, NULL);
3129 if (!result) goto failed;
3130 value = ast2obj_expr(o->v.Call.func);
3131 if (!value) goto failed;
3132 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3133 goto failed;
3134 Py_DECREF(value);
3135 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3136 if (!value) goto failed;
3137 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3138 goto failed;
3139 Py_DECREF(value);
3140 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3141 if (!value) goto failed;
3142 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3143 goto failed;
3144 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003145 break;
3146 case Num_kind:
3147 result = PyType_GenericNew(Num_type, NULL, NULL);
3148 if (!result) goto failed;
3149 value = ast2obj_object(o->v.Num.n);
3150 if (!value) goto failed;
3151 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3152 goto failed;
3153 Py_DECREF(value);
3154 break;
3155 case Str_kind:
3156 result = PyType_GenericNew(Str_type, NULL, NULL);
3157 if (!result) goto failed;
3158 value = ast2obj_string(o->v.Str.s);
3159 if (!value) goto failed;
3160 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3161 goto failed;
3162 Py_DECREF(value);
3163 break;
3164 case Bytes_kind:
3165 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3166 if (!result) goto failed;
3167 value = ast2obj_bytes(o->v.Bytes.s);
3168 if (!value) goto failed;
3169 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3170 goto failed;
3171 Py_DECREF(value);
3172 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003173 case NameConstant_kind:
3174 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3175 if (!result) goto failed;
3176 value = ast2obj_singleton(o->v.NameConstant.value);
3177 if (!value) goto failed;
3178 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3179 goto failed;
3180 Py_DECREF(value);
3181 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003182 case Ellipsis_kind:
3183 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3184 if (!result) goto failed;
3185 break;
3186 case Attribute_kind:
3187 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3188 if (!result) goto failed;
3189 value = ast2obj_expr(o->v.Attribute.value);
3190 if (!value) goto failed;
3191 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3192 goto failed;
3193 Py_DECREF(value);
3194 value = ast2obj_identifier(o->v.Attribute.attr);
3195 if (!value) goto failed;
3196 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3197 goto failed;
3198 Py_DECREF(value);
3199 value = ast2obj_expr_context(o->v.Attribute.ctx);
3200 if (!value) goto failed;
3201 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3202 goto failed;
3203 Py_DECREF(value);
3204 break;
3205 case Subscript_kind:
3206 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3207 if (!result) goto failed;
3208 value = ast2obj_expr(o->v.Subscript.value);
3209 if (!value) goto failed;
3210 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3211 goto failed;
3212 Py_DECREF(value);
3213 value = ast2obj_slice(o->v.Subscript.slice);
3214 if (!value) goto failed;
3215 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3216 goto failed;
3217 Py_DECREF(value);
3218 value = ast2obj_expr_context(o->v.Subscript.ctx);
3219 if (!value) goto failed;
3220 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3221 goto failed;
3222 Py_DECREF(value);
3223 break;
3224 case Starred_kind:
3225 result = PyType_GenericNew(Starred_type, NULL, NULL);
3226 if (!result) goto failed;
3227 value = ast2obj_expr(o->v.Starred.value);
3228 if (!value) goto failed;
3229 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3230 goto failed;
3231 Py_DECREF(value);
3232 value = ast2obj_expr_context(o->v.Starred.ctx);
3233 if (!value) goto failed;
3234 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3235 goto failed;
3236 Py_DECREF(value);
3237 break;
3238 case Name_kind:
3239 result = PyType_GenericNew(Name_type, NULL, NULL);
3240 if (!result) goto failed;
3241 value = ast2obj_identifier(o->v.Name.id);
3242 if (!value) goto failed;
3243 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3244 goto failed;
3245 Py_DECREF(value);
3246 value = ast2obj_expr_context(o->v.Name.ctx);
3247 if (!value) goto failed;
3248 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3249 goto failed;
3250 Py_DECREF(value);
3251 break;
3252 case List_kind:
3253 result = PyType_GenericNew(List_type, NULL, NULL);
3254 if (!result) goto failed;
3255 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3256 if (!value) goto failed;
3257 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3258 goto failed;
3259 Py_DECREF(value);
3260 value = ast2obj_expr_context(o->v.List.ctx);
3261 if (!value) goto failed;
3262 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3263 goto failed;
3264 Py_DECREF(value);
3265 break;
3266 case Tuple_kind:
3267 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3268 if (!result) goto failed;
3269 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3270 if (!value) goto failed;
3271 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3272 goto failed;
3273 Py_DECREF(value);
3274 value = ast2obj_expr_context(o->v.Tuple.ctx);
3275 if (!value) goto failed;
3276 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3277 goto failed;
3278 Py_DECREF(value);
3279 break;
3280 }
3281 value = ast2obj_int(o->lineno);
3282 if (!value) goto failed;
3283 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3284 goto failed;
3285 Py_DECREF(value);
3286 value = ast2obj_int(o->col_offset);
3287 if (!value) goto failed;
3288 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3289 goto failed;
3290 Py_DECREF(value);
3291 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003292failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003293 Py_XDECREF(value);
3294 Py_XDECREF(result);
3295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296}
3297
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003298PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003300 switch(o) {
3301 case Load:
3302 Py_INCREF(Load_singleton);
3303 return Load_singleton;
3304 case Store:
3305 Py_INCREF(Store_singleton);
3306 return Store_singleton;
3307 case Del:
3308 Py_INCREF(Del_singleton);
3309 return Del_singleton;
3310 case AugLoad:
3311 Py_INCREF(AugLoad_singleton);
3312 return AugLoad_singleton;
3313 case AugStore:
3314 Py_INCREF(AugStore_singleton);
3315 return AugStore_singleton;
3316 case Param:
3317 Py_INCREF(Param_singleton);
3318 return Param_singleton;
3319 default:
3320 /* should never happen, but just in case ... */
3321 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3322 return NULL;
3323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003325PyObject*
3326ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003328 slice_ty o = (slice_ty)_o;
3329 PyObject *result = NULL, *value = NULL;
3330 if (!o) {
3331 Py_INCREF(Py_None);
3332 return Py_None;
3333 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003334
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003335 switch (o->kind) {
3336 case Slice_kind:
3337 result = PyType_GenericNew(Slice_type, NULL, NULL);
3338 if (!result) goto failed;
3339 value = ast2obj_expr(o->v.Slice.lower);
3340 if (!value) goto failed;
3341 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3342 goto failed;
3343 Py_DECREF(value);
3344 value = ast2obj_expr(o->v.Slice.upper);
3345 if (!value) goto failed;
3346 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3347 goto failed;
3348 Py_DECREF(value);
3349 value = ast2obj_expr(o->v.Slice.step);
3350 if (!value) goto failed;
3351 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3352 goto failed;
3353 Py_DECREF(value);
3354 break;
3355 case ExtSlice_kind:
3356 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3357 if (!result) goto failed;
3358 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3359 if (!value) goto failed;
3360 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3361 goto failed;
3362 Py_DECREF(value);
3363 break;
3364 case Index_kind:
3365 result = PyType_GenericNew(Index_type, NULL, NULL);
3366 if (!result) goto failed;
3367 value = ast2obj_expr(o->v.Index.value);
3368 if (!value) goto failed;
3369 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3370 goto failed;
3371 Py_DECREF(value);
3372 break;
3373 }
3374 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003375failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003376 Py_XDECREF(value);
3377 Py_XDECREF(result);
3378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379}
3380
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003381PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003383 switch(o) {
3384 case And:
3385 Py_INCREF(And_singleton);
3386 return And_singleton;
3387 case Or:
3388 Py_INCREF(Or_singleton);
3389 return Or_singleton;
3390 default:
3391 /* should never happen, but just in case ... */
3392 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3393 return NULL;
3394 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003395}
3396PyObject* ast2obj_operator(operator_ty o)
3397{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003398 switch(o) {
3399 case Add:
3400 Py_INCREF(Add_singleton);
3401 return Add_singleton;
3402 case Sub:
3403 Py_INCREF(Sub_singleton);
3404 return Sub_singleton;
3405 case Mult:
3406 Py_INCREF(Mult_singleton);
3407 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003408 case MatMult:
3409 Py_INCREF(MatMult_singleton);
3410 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003411 case Div:
3412 Py_INCREF(Div_singleton);
3413 return Div_singleton;
3414 case Mod:
3415 Py_INCREF(Mod_singleton);
3416 return Mod_singleton;
3417 case Pow:
3418 Py_INCREF(Pow_singleton);
3419 return Pow_singleton;
3420 case LShift:
3421 Py_INCREF(LShift_singleton);
3422 return LShift_singleton;
3423 case RShift:
3424 Py_INCREF(RShift_singleton);
3425 return RShift_singleton;
3426 case BitOr:
3427 Py_INCREF(BitOr_singleton);
3428 return BitOr_singleton;
3429 case BitXor:
3430 Py_INCREF(BitXor_singleton);
3431 return BitXor_singleton;
3432 case BitAnd:
3433 Py_INCREF(BitAnd_singleton);
3434 return BitAnd_singleton;
3435 case FloorDiv:
3436 Py_INCREF(FloorDiv_singleton);
3437 return FloorDiv_singleton;
3438 default:
3439 /* should never happen, but just in case ... */
3440 PyErr_Format(PyExc_SystemError, "unknown operator found");
3441 return NULL;
3442 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003443}
3444PyObject* ast2obj_unaryop(unaryop_ty o)
3445{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003446 switch(o) {
3447 case Invert:
3448 Py_INCREF(Invert_singleton);
3449 return Invert_singleton;
3450 case Not:
3451 Py_INCREF(Not_singleton);
3452 return Not_singleton;
3453 case UAdd:
3454 Py_INCREF(UAdd_singleton);
3455 return UAdd_singleton;
3456 case USub:
3457 Py_INCREF(USub_singleton);
3458 return USub_singleton;
3459 default:
3460 /* should never happen, but just in case ... */
3461 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3462 return NULL;
3463 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003464}
3465PyObject* ast2obj_cmpop(cmpop_ty o)
3466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003467 switch(o) {
3468 case Eq:
3469 Py_INCREF(Eq_singleton);
3470 return Eq_singleton;
3471 case NotEq:
3472 Py_INCREF(NotEq_singleton);
3473 return NotEq_singleton;
3474 case Lt:
3475 Py_INCREF(Lt_singleton);
3476 return Lt_singleton;
3477 case LtE:
3478 Py_INCREF(LtE_singleton);
3479 return LtE_singleton;
3480 case Gt:
3481 Py_INCREF(Gt_singleton);
3482 return Gt_singleton;
3483 case GtE:
3484 Py_INCREF(GtE_singleton);
3485 return GtE_singleton;
3486 case Is:
3487 Py_INCREF(Is_singleton);
3488 return Is_singleton;
3489 case IsNot:
3490 Py_INCREF(IsNot_singleton);
3491 return IsNot_singleton;
3492 case In:
3493 Py_INCREF(In_singleton);
3494 return In_singleton;
3495 case NotIn:
3496 Py_INCREF(NotIn_singleton);
3497 return NotIn_singleton;
3498 default:
3499 /* should never happen, but just in case ... */
3500 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3501 return NULL;
3502 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003503}
3504PyObject*
3505ast2obj_comprehension(void* _o)
3506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003507 comprehension_ty o = (comprehension_ty)_o;
3508 PyObject *result = NULL, *value = NULL;
3509 if (!o) {
3510 Py_INCREF(Py_None);
3511 return Py_None;
3512 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003513
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003514 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3515 if (!result) return NULL;
3516 value = ast2obj_expr(o->target);
3517 if (!value) goto failed;
3518 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3519 goto failed;
3520 Py_DECREF(value);
3521 value = ast2obj_expr(o->iter);
3522 if (!value) goto failed;
3523 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3524 goto failed;
3525 Py_DECREF(value);
3526 value = ast2obj_list(o->ifs, ast2obj_expr);
3527 if (!value) goto failed;
3528 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3529 goto failed;
3530 Py_DECREF(value);
3531 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003532failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003533 Py_XDECREF(value);
3534 Py_XDECREF(result);
3535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536}
3537
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003538PyObject*
3539ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003541 excepthandler_ty o = (excepthandler_ty)_o;
3542 PyObject *result = NULL, *value = NULL;
3543 if (!o) {
3544 Py_INCREF(Py_None);
3545 return Py_None;
3546 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003547
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003548 switch (o->kind) {
3549 case ExceptHandler_kind:
3550 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3551 if (!result) goto failed;
3552 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003553 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003554 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3555 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003556 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003557 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003558 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003559 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3560 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003561 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3563 if (!value) goto failed;
3564 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3565 goto failed;
3566 Py_DECREF(value);
3567 break;
3568 }
3569 value = ast2obj_int(o->lineno);
3570 if (!value) goto failed;
3571 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3572 goto failed;
3573 Py_DECREF(value);
3574 value = ast2obj_int(o->col_offset);
3575 if (!value) goto failed;
3576 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3577 goto failed;
3578 Py_DECREF(value);
3579 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003580failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003581 Py_XDECREF(value);
3582 Py_XDECREF(result);
3583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584}
3585
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003586PyObject*
3587ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003589 arguments_ty o = (arguments_ty)_o;
3590 PyObject *result = NULL, *value = NULL;
3591 if (!o) {
3592 Py_INCREF(Py_None);
3593 return Py_None;
3594 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003595
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003596 result = PyType_GenericNew(arguments_type, NULL, NULL);
3597 if (!result) return NULL;
3598 value = ast2obj_list(o->args, ast2obj_arg);
3599 if (!value) goto failed;
3600 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3601 goto failed;
3602 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003603 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003604 if (!value) goto failed;
3605 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3606 goto failed;
3607 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003608 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3609 if (!value) goto failed;
3610 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3611 goto failed;
3612 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003613 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003614 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003615 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003616 goto failed;
3617 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003618 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003619 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003620 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003621 goto failed;
3622 Py_DECREF(value);
3623 value = ast2obj_list(o->defaults, ast2obj_expr);
3624 if (!value) goto failed;
3625 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3626 goto failed;
3627 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003628 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003629failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003630 Py_XDECREF(value);
3631 Py_XDECREF(result);
3632 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633}
3634
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003635PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003636ast2obj_arg(void* _o)
3637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003638 arg_ty o = (arg_ty)_o;
3639 PyObject *result = NULL, *value = NULL;
3640 if (!o) {
3641 Py_INCREF(Py_None);
3642 return Py_None;
3643 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003644
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003645 result = PyType_GenericNew(arg_type, NULL, NULL);
3646 if (!result) return NULL;
3647 value = ast2obj_identifier(o->arg);
3648 if (!value) goto failed;
3649 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3650 goto failed;
3651 Py_DECREF(value);
3652 value = ast2obj_expr(o->annotation);
3653 if (!value) goto failed;
3654 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3655 goto failed;
3656 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003657 value = ast2obj_int(o->lineno);
3658 if (!value) goto failed;
3659 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3660 goto failed;
3661 Py_DECREF(value);
3662 value = ast2obj_int(o->col_offset);
3663 if (!value) goto failed;
3664 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3665 goto failed;
3666 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003667 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003668failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003669 Py_XDECREF(value);
3670 Py_XDECREF(result);
3671 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003672}
3673
3674PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003675ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003677 keyword_ty o = (keyword_ty)_o;
3678 PyObject *result = NULL, *value = NULL;
3679 if (!o) {
3680 Py_INCREF(Py_None);
3681 return Py_None;
3682 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003683
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003684 result = PyType_GenericNew(keyword_type, NULL, NULL);
3685 if (!result) return NULL;
3686 value = ast2obj_identifier(o->arg);
3687 if (!value) goto failed;
3688 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3689 goto failed;
3690 Py_DECREF(value);
3691 value = ast2obj_expr(o->value);
3692 if (!value) goto failed;
3693 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3694 goto failed;
3695 Py_DECREF(value);
3696 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003697failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003698 Py_XDECREF(value);
3699 Py_XDECREF(result);
3700 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701}
3702
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003703PyObject*
3704ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003706 alias_ty o = (alias_ty)_o;
3707 PyObject *result = NULL, *value = NULL;
3708 if (!o) {
3709 Py_INCREF(Py_None);
3710 return Py_None;
3711 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003712
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003713 result = PyType_GenericNew(alias_type, NULL, NULL);
3714 if (!result) return NULL;
3715 value = ast2obj_identifier(o->name);
3716 if (!value) goto failed;
3717 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3718 goto failed;
3719 Py_DECREF(value);
3720 value = ast2obj_identifier(o->asname);
3721 if (!value) goto failed;
3722 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3723 goto failed;
3724 Py_DECREF(value);
3725 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003726failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003727 Py_XDECREF(value);
3728 Py_XDECREF(result);
3729 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730}
3731
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003732PyObject*
3733ast2obj_withitem(void* _o)
3734{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003735 withitem_ty o = (withitem_ty)_o;
3736 PyObject *result = NULL, *value = NULL;
3737 if (!o) {
3738 Py_INCREF(Py_None);
3739 return Py_None;
3740 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003741
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003742 result = PyType_GenericNew(withitem_type, NULL, NULL);
3743 if (!result) return NULL;
3744 value = ast2obj_expr(o->context_expr);
3745 if (!value) goto failed;
3746 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3747 goto failed;
3748 Py_DECREF(value);
3749 value = ast2obj_expr(o->optional_vars);
3750 if (!value) goto failed;
3751 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3752 goto failed;
3753 Py_DECREF(value);
3754 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003755failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003756 Py_XDECREF(value);
3757 Py_XDECREF(result);
3758 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003759}
3760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003762int
3763obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3764{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003765 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003766
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003767 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003768
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003769 if (obj == Py_None) {
3770 *out = NULL;
3771 return 0;
3772 }
3773 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3774 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003775 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003776 }
3777 if (isinstance) {
3778 asdl_seq* body;
3779
3780 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3781 int res;
3782 Py_ssize_t len;
3783 Py_ssize_t i;
3784 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3785 if (tmp == NULL) goto failed;
3786 if (!PyList_Check(tmp)) {
3787 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3788 goto failed;
3789 }
3790 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003791 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003792 if (body == NULL) goto failed;
3793 for (i = 0; i < len; i++) {
3794 stmt_ty value;
3795 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3796 if (res != 0) goto failed;
3797 asdl_seq_SET(body, i, value);
3798 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003799 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003800 } else {
3801 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3802 return 1;
3803 }
3804 *out = Module(body, arena);
3805 if (*out == NULL) goto failed;
3806 return 0;
3807 }
3808 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3809 if (isinstance == -1) {
3810 return 1;
3811 }
3812 if (isinstance) {
3813 asdl_seq* body;
3814
3815 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3816 int res;
3817 Py_ssize_t len;
3818 Py_ssize_t i;
3819 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3820 if (tmp == NULL) goto failed;
3821 if (!PyList_Check(tmp)) {
3822 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3823 goto failed;
3824 }
3825 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003826 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003827 if (body == NULL) goto failed;
3828 for (i = 0; i < len; i++) {
3829 stmt_ty value;
3830 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3831 if (res != 0) goto failed;
3832 asdl_seq_SET(body, i, value);
3833 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003834 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003835 } else {
3836 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3837 return 1;
3838 }
3839 *out = Interactive(body, arena);
3840 if (*out == NULL) goto failed;
3841 return 0;
3842 }
3843 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3844 if (isinstance == -1) {
3845 return 1;
3846 }
3847 if (isinstance) {
3848 expr_ty body;
3849
3850 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3851 int res;
3852 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3853 if (tmp == NULL) goto failed;
3854 res = obj2ast_expr(tmp, &body, arena);
3855 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003856 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003857 } else {
3858 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3859 return 1;
3860 }
3861 *out = Expression(body, arena);
3862 if (*out == NULL) goto failed;
3863 return 0;
3864 }
3865 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3866 if (isinstance == -1) {
3867 return 1;
3868 }
3869 if (isinstance) {
3870 asdl_seq* body;
3871
3872 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3873 int res;
3874 Py_ssize_t len;
3875 Py_ssize_t i;
3876 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3877 if (tmp == NULL) goto failed;
3878 if (!PyList_Check(tmp)) {
3879 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3880 goto failed;
3881 }
3882 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003883 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003884 if (body == NULL) goto failed;
3885 for (i = 0; i < len; i++) {
3886 stmt_ty value;
3887 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3888 if (res != 0) goto failed;
3889 asdl_seq_SET(body, i, value);
3890 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003891 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003892 } else {
3893 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3894 return 1;
3895 }
3896 *out = Suite(body, arena);
3897 if (*out == NULL) goto failed;
3898 return 0;
3899 }
3900
3901 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3902 failed:
3903 Py_XDECREF(tmp);
3904 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003905}
3906
3907int
3908obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3909{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003910 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003911
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003912 PyObject *tmp = NULL;
3913 int lineno;
3914 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003915
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003916 if (obj == Py_None) {
3917 *out = NULL;
3918 return 0;
3919 }
3920 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3921 int res;
3922 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3923 if (tmp == NULL) goto failed;
3924 res = obj2ast_int(tmp, &lineno, arena);
3925 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003926 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003927 } else {
3928 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003929 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 }
3931 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3932 int res;
3933 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3934 if (tmp == NULL) goto failed;
3935 res = obj2ast_int(tmp, &col_offset, arena);
3936 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003937 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3940 return 1;
3941 }
3942 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3943 if (isinstance == -1) {
3944 return 1;
3945 }
3946 if (isinstance) {
3947 identifier name;
3948 arguments_ty args;
3949 asdl_seq* body;
3950 asdl_seq* decorator_list;
3951 expr_ty returns;
3952
3953 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3954 int res;
3955 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3956 if (tmp == NULL) goto failed;
3957 res = obj2ast_identifier(tmp, &name, arena);
3958 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003959 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003960 } else {
3961 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3962 return 1;
3963 }
3964 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3965 int res;
3966 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3967 if (tmp == NULL) goto failed;
3968 res = obj2ast_arguments(tmp, &args, arena);
3969 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003970 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003971 } else {
3972 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3973 return 1;
3974 }
3975 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3976 int res;
3977 Py_ssize_t len;
3978 Py_ssize_t i;
3979 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3980 if (tmp == NULL) goto failed;
3981 if (!PyList_Check(tmp)) {
3982 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3983 goto failed;
3984 }
3985 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003986 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003987 if (body == NULL) goto failed;
3988 for (i = 0; i < len; i++) {
3989 stmt_ty value;
3990 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3991 if (res != 0) goto failed;
3992 asdl_seq_SET(body, i, value);
3993 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003994 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003995 } else {
3996 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3997 return 1;
3998 }
3999 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4000 int res;
4001 Py_ssize_t len;
4002 Py_ssize_t i;
4003 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4004 if (tmp == NULL) goto failed;
4005 if (!PyList_Check(tmp)) {
4006 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4007 goto failed;
4008 }
4009 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004010 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 if (decorator_list == NULL) goto failed;
4012 for (i = 0; i < len; i++) {
4013 expr_ty value;
4014 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4015 if (res != 0) goto failed;
4016 asdl_seq_SET(decorator_list, i, value);
4017 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004018 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004019 } else {
4020 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4021 return 1;
4022 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004023 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004024 int res;
4025 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4026 if (tmp == NULL) goto failed;
4027 res = obj2ast_expr(tmp, &returns, arena);
4028 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004029 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004030 } else {
4031 returns = NULL;
4032 }
4033 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4034 col_offset, arena);
4035 if (*out == NULL) goto failed;
4036 return 0;
4037 }
Yury Selivanov75445082015-05-11 22:57:16 -04004038 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4039 if (isinstance == -1) {
4040 return 1;
4041 }
4042 if (isinstance) {
4043 identifier name;
4044 arguments_ty args;
4045 asdl_seq* body;
4046 asdl_seq* decorator_list;
4047 expr_ty returns;
4048
4049 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4050 int res;
4051 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4052 if (tmp == NULL) goto failed;
4053 res = obj2ast_identifier(tmp, &name, arena);
4054 if (res != 0) goto failed;
4055 Py_CLEAR(tmp);
4056 } else {
4057 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4058 return 1;
4059 }
4060 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4061 int res;
4062 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4063 if (tmp == NULL) goto failed;
4064 res = obj2ast_arguments(tmp, &args, arena);
4065 if (res != 0) goto failed;
4066 Py_CLEAR(tmp);
4067 } else {
4068 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4069 return 1;
4070 }
4071 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4072 int res;
4073 Py_ssize_t len;
4074 Py_ssize_t i;
4075 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4076 if (tmp == NULL) goto failed;
4077 if (!PyList_Check(tmp)) {
4078 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4079 goto failed;
4080 }
4081 len = PyList_GET_SIZE(tmp);
4082 body = _Py_asdl_seq_new(len, arena);
4083 if (body == NULL) goto failed;
4084 for (i = 0; i < len; i++) {
4085 stmt_ty value;
4086 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4087 if (res != 0) goto failed;
4088 asdl_seq_SET(body, i, value);
4089 }
4090 Py_CLEAR(tmp);
4091 } else {
4092 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4093 return 1;
4094 }
4095 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4096 int res;
4097 Py_ssize_t len;
4098 Py_ssize_t i;
4099 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4100 if (tmp == NULL) goto failed;
4101 if (!PyList_Check(tmp)) {
4102 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4103 goto failed;
4104 }
4105 len = PyList_GET_SIZE(tmp);
4106 decorator_list = _Py_asdl_seq_new(len, arena);
4107 if (decorator_list == NULL) goto failed;
4108 for (i = 0; i < len; i++) {
4109 expr_ty value;
4110 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4111 if (res != 0) goto failed;
4112 asdl_seq_SET(decorator_list, i, value);
4113 }
4114 Py_CLEAR(tmp);
4115 } else {
4116 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4117 return 1;
4118 }
4119 if (exists_not_none(obj, &PyId_returns)) {
4120 int res;
4121 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4122 if (tmp == NULL) goto failed;
4123 res = obj2ast_expr(tmp, &returns, arena);
4124 if (res != 0) goto failed;
4125 Py_CLEAR(tmp);
4126 } else {
4127 returns = NULL;
4128 }
4129 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4130 lineno, col_offset, arena);
4131 if (*out == NULL) goto failed;
4132 return 0;
4133 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004134 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4135 if (isinstance == -1) {
4136 return 1;
4137 }
4138 if (isinstance) {
4139 identifier name;
4140 asdl_seq* bases;
4141 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004142 asdl_seq* body;
4143 asdl_seq* decorator_list;
4144
4145 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4146 int res;
4147 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4148 if (tmp == NULL) goto failed;
4149 res = obj2ast_identifier(tmp, &name, arena);
4150 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004151 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004152 } else {
4153 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4154 return 1;
4155 }
4156 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4157 int res;
4158 Py_ssize_t len;
4159 Py_ssize_t i;
4160 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4161 if (tmp == NULL) goto failed;
4162 if (!PyList_Check(tmp)) {
4163 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4164 goto failed;
4165 }
4166 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004167 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004168 if (bases == NULL) goto failed;
4169 for (i = 0; i < len; i++) {
4170 expr_ty value;
4171 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4172 if (res != 0) goto failed;
4173 asdl_seq_SET(bases, i, value);
4174 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004175 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004176 } else {
4177 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4178 return 1;
4179 }
4180 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4181 int res;
4182 Py_ssize_t len;
4183 Py_ssize_t i;
4184 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4185 if (tmp == NULL) goto failed;
4186 if (!PyList_Check(tmp)) {
4187 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4188 goto failed;
4189 }
4190 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004191 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004192 if (keywords == NULL) goto failed;
4193 for (i = 0; i < len; i++) {
4194 keyword_ty value;
4195 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4196 if (res != 0) goto failed;
4197 asdl_seq_SET(keywords, i, value);
4198 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004200 } else {
4201 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4202 return 1;
4203 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004204 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4205 int res;
4206 Py_ssize_t len;
4207 Py_ssize_t i;
4208 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4209 if (tmp == NULL) goto failed;
4210 if (!PyList_Check(tmp)) {
4211 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4212 goto failed;
4213 }
4214 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004215 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004216 if (body == NULL) goto failed;
4217 for (i = 0; i < len; i++) {
4218 stmt_ty value;
4219 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4220 if (res != 0) goto failed;
4221 asdl_seq_SET(body, i, value);
4222 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004224 } else {
4225 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4226 return 1;
4227 }
4228 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4229 int res;
4230 Py_ssize_t len;
4231 Py_ssize_t i;
4232 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4233 if (tmp == NULL) goto failed;
4234 if (!PyList_Check(tmp)) {
4235 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4236 goto failed;
4237 }
4238 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004239 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004240 if (decorator_list == NULL) goto failed;
4241 for (i = 0; i < len; i++) {
4242 expr_ty value;
4243 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4244 if (res != 0) goto failed;
4245 asdl_seq_SET(decorator_list, i, value);
4246 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004247 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004248 } else {
4249 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4250 return 1;
4251 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004252 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4253 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004254 if (*out == NULL) goto failed;
4255 return 0;
4256 }
4257 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4258 if (isinstance == -1) {
4259 return 1;
4260 }
4261 if (isinstance) {
4262 expr_ty value;
4263
Victor Stinneree4b59c2013-07-27 00:01:35 +02004264 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004265 int res;
4266 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4267 if (tmp == NULL) goto failed;
4268 res = obj2ast_expr(tmp, &value, arena);
4269 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004270 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004271 } else {
4272 value = NULL;
4273 }
4274 *out = Return(value, lineno, col_offset, arena);
4275 if (*out == NULL) goto failed;
4276 return 0;
4277 }
4278 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4279 if (isinstance == -1) {
4280 return 1;
4281 }
4282 if (isinstance) {
4283 asdl_seq* targets;
4284
4285 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4286 int res;
4287 Py_ssize_t len;
4288 Py_ssize_t i;
4289 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4290 if (tmp == NULL) goto failed;
4291 if (!PyList_Check(tmp)) {
4292 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4293 goto failed;
4294 }
4295 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004296 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004297 if (targets == NULL) goto failed;
4298 for (i = 0; i < len; i++) {
4299 expr_ty value;
4300 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4301 if (res != 0) goto failed;
4302 asdl_seq_SET(targets, i, value);
4303 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004304 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004305 } else {
4306 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4307 return 1;
4308 }
4309 *out = Delete(targets, lineno, col_offset, arena);
4310 if (*out == NULL) goto failed;
4311 return 0;
4312 }
4313 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4314 if (isinstance == -1) {
4315 return 1;
4316 }
4317 if (isinstance) {
4318 asdl_seq* targets;
4319 expr_ty value;
4320
4321 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4322 int res;
4323 Py_ssize_t len;
4324 Py_ssize_t i;
4325 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4326 if (tmp == NULL) goto failed;
4327 if (!PyList_Check(tmp)) {
4328 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4329 goto failed;
4330 }
4331 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004332 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004333 if (targets == NULL) goto failed;
4334 for (i = 0; i < len; i++) {
4335 expr_ty value;
4336 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4337 if (res != 0) goto failed;
4338 asdl_seq_SET(targets, i, value);
4339 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004340 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004341 } else {
4342 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4343 return 1;
4344 }
4345 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4346 int res;
4347 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4348 if (tmp == NULL) goto failed;
4349 res = obj2ast_expr(tmp, &value, arena);
4350 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004351 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004352 } else {
4353 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4354 return 1;
4355 }
4356 *out = Assign(targets, value, lineno, col_offset, arena);
4357 if (*out == NULL) goto failed;
4358 return 0;
4359 }
4360 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4361 if (isinstance == -1) {
4362 return 1;
4363 }
4364 if (isinstance) {
4365 expr_ty target;
4366 operator_ty op;
4367 expr_ty value;
4368
4369 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4370 int res;
4371 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4372 if (tmp == NULL) goto failed;
4373 res = obj2ast_expr(tmp, &target, arena);
4374 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004375 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004376 } else {
4377 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4378 return 1;
4379 }
4380 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4381 int res;
4382 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4383 if (tmp == NULL) goto failed;
4384 res = obj2ast_operator(tmp, &op, arena);
4385 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004386 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004387 } else {
4388 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4389 return 1;
4390 }
4391 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4392 int res;
4393 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4394 if (tmp == NULL) goto failed;
4395 res = obj2ast_expr(tmp, &value, arena);
4396 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004397 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004398 } else {
4399 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4400 return 1;
4401 }
4402 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4403 if (*out == NULL) goto failed;
4404 return 0;
4405 }
4406 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4407 if (isinstance == -1) {
4408 return 1;
4409 }
4410 if (isinstance) {
4411 expr_ty target;
4412 expr_ty iter;
4413 asdl_seq* body;
4414 asdl_seq* orelse;
4415
4416 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4417 int res;
4418 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4419 if (tmp == NULL) goto failed;
4420 res = obj2ast_expr(tmp, &target, arena);
4421 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004422 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004423 } else {
4424 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4425 return 1;
4426 }
4427 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4428 int res;
4429 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4430 if (tmp == NULL) goto failed;
4431 res = obj2ast_expr(tmp, &iter, arena);
4432 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004433 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004434 } else {
4435 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4436 return 1;
4437 }
4438 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4439 int res;
4440 Py_ssize_t len;
4441 Py_ssize_t i;
4442 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4443 if (tmp == NULL) goto failed;
4444 if (!PyList_Check(tmp)) {
4445 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4446 goto failed;
4447 }
4448 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004449 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004450 if (body == NULL) goto failed;
4451 for (i = 0; i < len; i++) {
4452 stmt_ty value;
4453 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4454 if (res != 0) goto failed;
4455 asdl_seq_SET(body, i, value);
4456 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004457 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004458 } else {
4459 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4460 return 1;
4461 }
4462 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4463 int res;
4464 Py_ssize_t len;
4465 Py_ssize_t i;
4466 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4467 if (tmp == NULL) goto failed;
4468 if (!PyList_Check(tmp)) {
4469 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4470 goto failed;
4471 }
4472 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004473 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004474 if (orelse == NULL) goto failed;
4475 for (i = 0; i < len; i++) {
4476 stmt_ty value;
4477 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4478 if (res != 0) goto failed;
4479 asdl_seq_SET(orelse, i, value);
4480 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004481 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004482 } else {
4483 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4484 return 1;
4485 }
4486 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4487 if (*out == NULL) goto failed;
4488 return 0;
4489 }
Yury Selivanov75445082015-05-11 22:57:16 -04004490 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4491 if (isinstance == -1) {
4492 return 1;
4493 }
4494 if (isinstance) {
4495 expr_ty target;
4496 expr_ty iter;
4497 asdl_seq* body;
4498 asdl_seq* orelse;
4499
4500 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4501 int res;
4502 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4503 if (tmp == NULL) goto failed;
4504 res = obj2ast_expr(tmp, &target, arena);
4505 if (res != 0) goto failed;
4506 Py_CLEAR(tmp);
4507 } else {
4508 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4509 return 1;
4510 }
4511 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4512 int res;
4513 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4514 if (tmp == NULL) goto failed;
4515 res = obj2ast_expr(tmp, &iter, arena);
4516 if (res != 0) goto failed;
4517 Py_CLEAR(tmp);
4518 } else {
4519 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4520 return 1;
4521 }
4522 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4523 int res;
4524 Py_ssize_t len;
4525 Py_ssize_t i;
4526 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4527 if (tmp == NULL) goto failed;
4528 if (!PyList_Check(tmp)) {
4529 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4530 goto failed;
4531 }
4532 len = PyList_GET_SIZE(tmp);
4533 body = _Py_asdl_seq_new(len, arena);
4534 if (body == NULL) goto failed;
4535 for (i = 0; i < len; i++) {
4536 stmt_ty value;
4537 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4538 if (res != 0) goto failed;
4539 asdl_seq_SET(body, i, value);
4540 }
4541 Py_CLEAR(tmp);
4542 } else {
4543 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4544 return 1;
4545 }
4546 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4547 int res;
4548 Py_ssize_t len;
4549 Py_ssize_t i;
4550 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4551 if (tmp == NULL) goto failed;
4552 if (!PyList_Check(tmp)) {
4553 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4554 goto failed;
4555 }
4556 len = PyList_GET_SIZE(tmp);
4557 orelse = _Py_asdl_seq_new(len, arena);
4558 if (orelse == NULL) goto failed;
4559 for (i = 0; i < len; i++) {
4560 stmt_ty value;
4561 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4562 if (res != 0) goto failed;
4563 asdl_seq_SET(orelse, i, value);
4564 }
4565 Py_CLEAR(tmp);
4566 } else {
4567 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4568 return 1;
4569 }
4570 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4571 if (*out == NULL) goto failed;
4572 return 0;
4573 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004574 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4575 if (isinstance == -1) {
4576 return 1;
4577 }
4578 if (isinstance) {
4579 expr_ty test;
4580 asdl_seq* body;
4581 asdl_seq* orelse;
4582
4583 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4584 int res;
4585 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4586 if (tmp == NULL) goto failed;
4587 res = obj2ast_expr(tmp, &test, arena);
4588 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004590 } else {
4591 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4592 return 1;
4593 }
4594 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4595 int res;
4596 Py_ssize_t len;
4597 Py_ssize_t i;
4598 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4599 if (tmp == NULL) goto failed;
4600 if (!PyList_Check(tmp)) {
4601 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4602 goto failed;
4603 }
4604 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004605 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004606 if (body == NULL) goto failed;
4607 for (i = 0; i < len; i++) {
4608 stmt_ty value;
4609 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4610 if (res != 0) goto failed;
4611 asdl_seq_SET(body, i, value);
4612 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004613 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004614 } else {
4615 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4616 return 1;
4617 }
4618 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4619 int res;
4620 Py_ssize_t len;
4621 Py_ssize_t i;
4622 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4623 if (tmp == NULL) goto failed;
4624 if (!PyList_Check(tmp)) {
4625 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4626 goto failed;
4627 }
4628 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004629 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004630 if (orelse == NULL) goto failed;
4631 for (i = 0; i < len; i++) {
4632 stmt_ty value;
4633 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4634 if (res != 0) goto failed;
4635 asdl_seq_SET(orelse, i, value);
4636 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004637 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004638 } else {
4639 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4640 return 1;
4641 }
4642 *out = While(test, body, orelse, lineno, col_offset, arena);
4643 if (*out == NULL) goto failed;
4644 return 0;
4645 }
4646 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4647 if (isinstance == -1) {
4648 return 1;
4649 }
4650 if (isinstance) {
4651 expr_ty test;
4652 asdl_seq* body;
4653 asdl_seq* orelse;
4654
4655 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4656 int res;
4657 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4658 if (tmp == NULL) goto failed;
4659 res = obj2ast_expr(tmp, &test, arena);
4660 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004661 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004662 } else {
4663 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4664 return 1;
4665 }
4666 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4667 int res;
4668 Py_ssize_t len;
4669 Py_ssize_t i;
4670 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4671 if (tmp == NULL) goto failed;
4672 if (!PyList_Check(tmp)) {
4673 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4674 goto failed;
4675 }
4676 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004677 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004678 if (body == NULL) goto failed;
4679 for (i = 0; i < len; i++) {
4680 stmt_ty value;
4681 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4682 if (res != 0) goto failed;
4683 asdl_seq_SET(body, i, value);
4684 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004685 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004686 } else {
4687 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4688 return 1;
4689 }
4690 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4691 int res;
4692 Py_ssize_t len;
4693 Py_ssize_t i;
4694 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4695 if (tmp == NULL) goto failed;
4696 if (!PyList_Check(tmp)) {
4697 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4698 goto failed;
4699 }
4700 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004701 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004702 if (orelse == NULL) goto failed;
4703 for (i = 0; i < len; i++) {
4704 stmt_ty value;
4705 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4706 if (res != 0) goto failed;
4707 asdl_seq_SET(orelse, i, value);
4708 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004709 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4712 return 1;
4713 }
4714 *out = If(test, body, orelse, lineno, col_offset, arena);
4715 if (*out == NULL) goto failed;
4716 return 0;
4717 }
4718 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4719 if (isinstance == -1) {
4720 return 1;
4721 }
4722 if (isinstance) {
4723 asdl_seq* items;
4724 asdl_seq* body;
4725
4726 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4727 int res;
4728 Py_ssize_t len;
4729 Py_ssize_t i;
4730 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4731 if (tmp == NULL) goto failed;
4732 if (!PyList_Check(tmp)) {
4733 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4734 goto failed;
4735 }
4736 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004737 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004738 if (items == NULL) goto failed;
4739 for (i = 0; i < len; i++) {
4740 withitem_ty value;
4741 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4742 if (res != 0) goto failed;
4743 asdl_seq_SET(items, i, value);
4744 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004745 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4748 return 1;
4749 }
4750 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4758 goto failed;
4759 }
4760 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004761 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004762 if (body == NULL) goto failed;
4763 for (i = 0; i < len; i++) {
4764 stmt_ty value;
4765 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4766 if (res != 0) goto failed;
4767 asdl_seq_SET(body, i, value);
4768 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004770 } else {
4771 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4772 return 1;
4773 }
4774 *out = With(items, body, lineno, col_offset, arena);
4775 if (*out == NULL) goto failed;
4776 return 0;
4777 }
Yury Selivanov75445082015-05-11 22:57:16 -04004778 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4779 if (isinstance == -1) {
4780 return 1;
4781 }
4782 if (isinstance) {
4783 asdl_seq* items;
4784 asdl_seq* body;
4785
4786 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4787 int res;
4788 Py_ssize_t len;
4789 Py_ssize_t i;
4790 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4791 if (tmp == NULL) goto failed;
4792 if (!PyList_Check(tmp)) {
4793 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4794 goto failed;
4795 }
4796 len = PyList_GET_SIZE(tmp);
4797 items = _Py_asdl_seq_new(len, arena);
4798 if (items == NULL) goto failed;
4799 for (i = 0; i < len; i++) {
4800 withitem_ty value;
4801 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4802 if (res != 0) goto failed;
4803 asdl_seq_SET(items, i, value);
4804 }
4805 Py_CLEAR(tmp);
4806 } else {
4807 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4808 return 1;
4809 }
4810 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4811 int res;
4812 Py_ssize_t len;
4813 Py_ssize_t i;
4814 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4815 if (tmp == NULL) goto failed;
4816 if (!PyList_Check(tmp)) {
4817 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4818 goto failed;
4819 }
4820 len = PyList_GET_SIZE(tmp);
4821 body = _Py_asdl_seq_new(len, arena);
4822 if (body == NULL) goto failed;
4823 for (i = 0; i < len; i++) {
4824 stmt_ty value;
4825 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4826 if (res != 0) goto failed;
4827 asdl_seq_SET(body, i, value);
4828 }
4829 Py_CLEAR(tmp);
4830 } else {
4831 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4832 return 1;
4833 }
4834 *out = AsyncWith(items, body, lineno, col_offset, arena);
4835 if (*out == NULL) goto failed;
4836 return 0;
4837 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004838 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4839 if (isinstance == -1) {
4840 return 1;
4841 }
4842 if (isinstance) {
4843 expr_ty exc;
4844 expr_ty cause;
4845
Victor Stinneree4b59c2013-07-27 00:01:35 +02004846 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004847 int res;
4848 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4849 if (tmp == NULL) goto failed;
4850 res = obj2ast_expr(tmp, &exc, arena);
4851 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004852 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004853 } else {
4854 exc = NULL;
4855 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004856 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004857 int res;
4858 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4859 if (tmp == NULL) goto failed;
4860 res = obj2ast_expr(tmp, &cause, arena);
4861 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004862 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004863 } else {
4864 cause = NULL;
4865 }
4866 *out = Raise(exc, cause, lineno, col_offset, arena);
4867 if (*out == NULL) goto failed;
4868 return 0;
4869 }
4870 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4871 if (isinstance == -1) {
4872 return 1;
4873 }
4874 if (isinstance) {
4875 asdl_seq* body;
4876 asdl_seq* handlers;
4877 asdl_seq* orelse;
4878 asdl_seq* finalbody;
4879
4880 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4881 int res;
4882 Py_ssize_t len;
4883 Py_ssize_t i;
4884 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4885 if (tmp == NULL) goto failed;
4886 if (!PyList_Check(tmp)) {
4887 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4888 goto failed;
4889 }
4890 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004891 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004892 if (body == NULL) goto failed;
4893 for (i = 0; i < len; i++) {
4894 stmt_ty value;
4895 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4896 if (res != 0) goto failed;
4897 asdl_seq_SET(body, i, value);
4898 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004899 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004900 } else {
4901 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4902 return 1;
4903 }
4904 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4905 int res;
4906 Py_ssize_t len;
4907 Py_ssize_t i;
4908 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4909 if (tmp == NULL) goto failed;
4910 if (!PyList_Check(tmp)) {
4911 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4912 goto failed;
4913 }
4914 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004915 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004916 if (handlers == NULL) goto failed;
4917 for (i = 0; i < len; i++) {
4918 excepthandler_ty value;
4919 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4920 if (res != 0) goto failed;
4921 asdl_seq_SET(handlers, i, value);
4922 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004923 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004924 } else {
4925 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4926 return 1;
4927 }
4928 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4929 int res;
4930 Py_ssize_t len;
4931 Py_ssize_t i;
4932 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4933 if (tmp == NULL) goto failed;
4934 if (!PyList_Check(tmp)) {
4935 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4936 goto failed;
4937 }
4938 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004939 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004940 if (orelse == NULL) goto failed;
4941 for (i = 0; i < len; i++) {
4942 stmt_ty value;
4943 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4944 if (res != 0) goto failed;
4945 asdl_seq_SET(orelse, i, value);
4946 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004947 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004948 } else {
4949 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4950 return 1;
4951 }
4952 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4953 int res;
4954 Py_ssize_t len;
4955 Py_ssize_t i;
4956 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4957 if (tmp == NULL) goto failed;
4958 if (!PyList_Check(tmp)) {
4959 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4960 goto failed;
4961 }
4962 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004963 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004964 if (finalbody == NULL) goto failed;
4965 for (i = 0; i < len; i++) {
4966 stmt_ty value;
4967 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4968 if (res != 0) goto failed;
4969 asdl_seq_SET(finalbody, i, value);
4970 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004971 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004972 } else {
4973 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4974 return 1;
4975 }
4976 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4977 arena);
4978 if (*out == NULL) goto failed;
4979 return 0;
4980 }
4981 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4982 if (isinstance == -1) {
4983 return 1;
4984 }
4985 if (isinstance) {
4986 expr_ty test;
4987 expr_ty msg;
4988
4989 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4990 int res;
4991 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4992 if (tmp == NULL) goto failed;
4993 res = obj2ast_expr(tmp, &test, arena);
4994 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004995 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004996 } else {
4997 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4998 return 1;
4999 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005000 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005001 int res;
5002 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5003 if (tmp == NULL) goto failed;
5004 res = obj2ast_expr(tmp, &msg, arena);
5005 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005006 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005007 } else {
5008 msg = NULL;
5009 }
5010 *out = Assert(test, msg, lineno, col_offset, arena);
5011 if (*out == NULL) goto failed;
5012 return 0;
5013 }
5014 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5015 if (isinstance == -1) {
5016 return 1;
5017 }
5018 if (isinstance) {
5019 asdl_seq* names;
5020
5021 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5022 int res;
5023 Py_ssize_t len;
5024 Py_ssize_t i;
5025 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5026 if (tmp == NULL) goto failed;
5027 if (!PyList_Check(tmp)) {
5028 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5029 goto failed;
5030 }
5031 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005032 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005033 if (names == NULL) goto failed;
5034 for (i = 0; i < len; i++) {
5035 alias_ty value;
5036 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5037 if (res != 0) goto failed;
5038 asdl_seq_SET(names, i, value);
5039 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005041 } else {
5042 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5043 return 1;
5044 }
5045 *out = Import(names, lineno, col_offset, arena);
5046 if (*out == NULL) goto failed;
5047 return 0;
5048 }
5049 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5050 if (isinstance == -1) {
5051 return 1;
5052 }
5053 if (isinstance) {
5054 identifier module;
5055 asdl_seq* names;
5056 int level;
5057
Victor Stinneree4b59c2013-07-27 00:01:35 +02005058 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005059 int res;
5060 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5061 if (tmp == NULL) goto failed;
5062 res = obj2ast_identifier(tmp, &module, arena);
5063 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005064 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005065 } else {
5066 module = NULL;
5067 }
5068 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5069 int res;
5070 Py_ssize_t len;
5071 Py_ssize_t i;
5072 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5073 if (tmp == NULL) goto failed;
5074 if (!PyList_Check(tmp)) {
5075 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5076 goto failed;
5077 }
5078 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005079 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005080 if (names == NULL) goto failed;
5081 for (i = 0; i < len; i++) {
5082 alias_ty value;
5083 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5084 if (res != 0) goto failed;
5085 asdl_seq_SET(names, i, value);
5086 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005087 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005088 } else {
5089 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5090 return 1;
5091 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005092 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005093 int res;
5094 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5095 if (tmp == NULL) goto failed;
5096 res = obj2ast_int(tmp, &level, arena);
5097 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005098 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005099 } else {
5100 level = 0;
5101 }
5102 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5103 if (*out == NULL) goto failed;
5104 return 0;
5105 }
5106 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5107 if (isinstance == -1) {
5108 return 1;
5109 }
5110 if (isinstance) {
5111 asdl_seq* names;
5112
5113 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5114 int res;
5115 Py_ssize_t len;
5116 Py_ssize_t i;
5117 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5118 if (tmp == NULL) goto failed;
5119 if (!PyList_Check(tmp)) {
5120 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5121 goto failed;
5122 }
5123 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005124 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005125 if (names == NULL) goto failed;
5126 for (i = 0; i < len; i++) {
5127 identifier value;
5128 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5129 if (res != 0) goto failed;
5130 asdl_seq_SET(names, i, value);
5131 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005132 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005133 } else {
5134 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5135 return 1;
5136 }
5137 *out = Global(names, lineno, col_offset, arena);
5138 if (*out == NULL) goto failed;
5139 return 0;
5140 }
5141 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5142 if (isinstance == -1) {
5143 return 1;
5144 }
5145 if (isinstance) {
5146 asdl_seq* names;
5147
5148 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5149 int res;
5150 Py_ssize_t len;
5151 Py_ssize_t i;
5152 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5153 if (tmp == NULL) goto failed;
5154 if (!PyList_Check(tmp)) {
5155 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5156 goto failed;
5157 }
5158 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005159 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005160 if (names == NULL) goto failed;
5161 for (i = 0; i < len; i++) {
5162 identifier value;
5163 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5164 if (res != 0) goto failed;
5165 asdl_seq_SET(names, i, value);
5166 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005167 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005168 } else {
5169 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5170 return 1;
5171 }
5172 *out = Nonlocal(names, lineno, col_offset, arena);
5173 if (*out == NULL) goto failed;
5174 return 0;
5175 }
5176 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5177 if (isinstance == -1) {
5178 return 1;
5179 }
5180 if (isinstance) {
5181 expr_ty value;
5182
5183 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5184 int res;
5185 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5186 if (tmp == NULL) goto failed;
5187 res = obj2ast_expr(tmp, &value, arena);
5188 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005189 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005190 } else {
5191 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5192 return 1;
5193 }
5194 *out = Expr(value, lineno, col_offset, arena);
5195 if (*out == NULL) goto failed;
5196 return 0;
5197 }
5198 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5199 if (isinstance == -1) {
5200 return 1;
5201 }
5202 if (isinstance) {
5203
5204 *out = Pass(lineno, col_offset, arena);
5205 if (*out == NULL) goto failed;
5206 return 0;
5207 }
5208 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5209 if (isinstance == -1) {
5210 return 1;
5211 }
5212 if (isinstance) {
5213
5214 *out = Break(lineno, col_offset, arena);
5215 if (*out == NULL) goto failed;
5216 return 0;
5217 }
5218 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5219 if (isinstance == -1) {
5220 return 1;
5221 }
5222 if (isinstance) {
5223
5224 *out = Continue(lineno, col_offset, arena);
5225 if (*out == NULL) goto failed;
5226 return 0;
5227 }
5228
5229 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5230 failed:
5231 Py_XDECREF(tmp);
5232 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005233}
5234
5235int
5236obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5237{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005238 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005239
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005240 PyObject *tmp = NULL;
5241 int lineno;
5242 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005243
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005244 if (obj == Py_None) {
5245 *out = NULL;
5246 return 0;
5247 }
5248 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5249 int res;
5250 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5251 if (tmp == NULL) goto failed;
5252 res = obj2ast_int(tmp, &lineno, arena);
5253 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005254 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005255 } else {
5256 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005257 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005258 }
5259 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5260 int res;
5261 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5262 if (tmp == NULL) goto failed;
5263 res = obj2ast_int(tmp, &col_offset, arena);
5264 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005265 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005266 } else {
5267 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5268 return 1;
5269 }
5270 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5271 if (isinstance == -1) {
5272 return 1;
5273 }
5274 if (isinstance) {
5275 boolop_ty op;
5276 asdl_seq* values;
5277
5278 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5279 int res;
5280 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5281 if (tmp == NULL) goto failed;
5282 res = obj2ast_boolop(tmp, &op, arena);
5283 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005284 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005285 } else {
5286 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5287 return 1;
5288 }
5289 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5290 int res;
5291 Py_ssize_t len;
5292 Py_ssize_t i;
5293 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5294 if (tmp == NULL) goto failed;
5295 if (!PyList_Check(tmp)) {
5296 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5297 goto failed;
5298 }
5299 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005300 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005301 if (values == NULL) goto failed;
5302 for (i = 0; i < len; i++) {
5303 expr_ty value;
5304 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5305 if (res != 0) goto failed;
5306 asdl_seq_SET(values, i, value);
5307 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005308 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005309 } else {
5310 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5311 return 1;
5312 }
5313 *out = BoolOp(op, values, lineno, col_offset, arena);
5314 if (*out == NULL) goto failed;
5315 return 0;
5316 }
5317 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5318 if (isinstance == -1) {
5319 return 1;
5320 }
5321 if (isinstance) {
5322 expr_ty left;
5323 operator_ty op;
5324 expr_ty right;
5325
5326 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5327 int res;
5328 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5329 if (tmp == NULL) goto failed;
5330 res = obj2ast_expr(tmp, &left, arena);
5331 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005332 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005333 } else {
5334 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5335 return 1;
5336 }
5337 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5338 int res;
5339 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5340 if (tmp == NULL) goto failed;
5341 res = obj2ast_operator(tmp, &op, arena);
5342 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005344 } else {
5345 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5346 return 1;
5347 }
5348 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5349 int res;
5350 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5351 if (tmp == NULL) goto failed;
5352 res = obj2ast_expr(tmp, &right, arena);
5353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005355 } else {
5356 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5357 return 1;
5358 }
5359 *out = BinOp(left, op, right, lineno, col_offset, arena);
5360 if (*out == NULL) goto failed;
5361 return 0;
5362 }
5363 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5364 if (isinstance == -1) {
5365 return 1;
5366 }
5367 if (isinstance) {
5368 unaryop_ty op;
5369 expr_ty operand;
5370
5371 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5372 int res;
5373 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5374 if (tmp == NULL) goto failed;
5375 res = obj2ast_unaryop(tmp, &op, arena);
5376 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005377 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005378 } else {
5379 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5380 return 1;
5381 }
5382 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5383 int res;
5384 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5385 if (tmp == NULL) goto failed;
5386 res = obj2ast_expr(tmp, &operand, arena);
5387 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005388 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005389 } else {
5390 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5391 return 1;
5392 }
5393 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5394 if (*out == NULL) goto failed;
5395 return 0;
5396 }
5397 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5398 if (isinstance == -1) {
5399 return 1;
5400 }
5401 if (isinstance) {
5402 arguments_ty args;
5403 expr_ty body;
5404
5405 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5406 int res;
5407 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5408 if (tmp == NULL) goto failed;
5409 res = obj2ast_arguments(tmp, &args, arena);
5410 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005411 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005412 } else {
5413 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5414 return 1;
5415 }
5416 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5417 int res;
5418 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5419 if (tmp == NULL) goto failed;
5420 res = obj2ast_expr(tmp, &body, arena);
5421 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005422 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005423 } else {
5424 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5425 return 1;
5426 }
5427 *out = Lambda(args, body, lineno, col_offset, arena);
5428 if (*out == NULL) goto failed;
5429 return 0;
5430 }
5431 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5432 if (isinstance == -1) {
5433 return 1;
5434 }
5435 if (isinstance) {
5436 expr_ty test;
5437 expr_ty body;
5438 expr_ty orelse;
5439
5440 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5441 int res;
5442 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5443 if (tmp == NULL) goto failed;
5444 res = obj2ast_expr(tmp, &test, arena);
5445 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005446 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005447 } else {
5448 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5449 return 1;
5450 }
5451 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5452 int res;
5453 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5454 if (tmp == NULL) goto failed;
5455 res = obj2ast_expr(tmp, &body, arena);
5456 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005457 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005458 } else {
5459 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5460 return 1;
5461 }
5462 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5463 int res;
5464 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5465 if (tmp == NULL) goto failed;
5466 res = obj2ast_expr(tmp, &orelse, arena);
5467 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005468 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005469 } else {
5470 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5471 return 1;
5472 }
5473 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5474 if (*out == NULL) goto failed;
5475 return 0;
5476 }
5477 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5478 if (isinstance == -1) {
5479 return 1;
5480 }
5481 if (isinstance) {
5482 asdl_seq* keys;
5483 asdl_seq* values;
5484
5485 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5486 int res;
5487 Py_ssize_t len;
5488 Py_ssize_t i;
5489 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5490 if (tmp == NULL) goto failed;
5491 if (!PyList_Check(tmp)) {
5492 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5493 goto failed;
5494 }
5495 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005496 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005497 if (keys == NULL) goto failed;
5498 for (i = 0; i < len; i++) {
5499 expr_ty value;
5500 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5501 if (res != 0) goto failed;
5502 asdl_seq_SET(keys, i, value);
5503 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005504 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005505 } else {
5506 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5507 return 1;
5508 }
5509 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5510 int res;
5511 Py_ssize_t len;
5512 Py_ssize_t i;
5513 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5514 if (tmp == NULL) goto failed;
5515 if (!PyList_Check(tmp)) {
5516 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5517 goto failed;
5518 }
5519 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005520 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005521 if (values == NULL) goto failed;
5522 for (i = 0; i < len; i++) {
5523 expr_ty value;
5524 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5525 if (res != 0) goto failed;
5526 asdl_seq_SET(values, i, value);
5527 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005528 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005529 } else {
5530 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5531 return 1;
5532 }
5533 *out = Dict(keys, values, lineno, col_offset, arena);
5534 if (*out == NULL) goto failed;
5535 return 0;
5536 }
5537 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5538 if (isinstance == -1) {
5539 return 1;
5540 }
5541 if (isinstance) {
5542 asdl_seq* elts;
5543
5544 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5545 int res;
5546 Py_ssize_t len;
5547 Py_ssize_t i;
5548 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5549 if (tmp == NULL) goto failed;
5550 if (!PyList_Check(tmp)) {
5551 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5552 goto failed;
5553 }
5554 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005555 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005556 if (elts == NULL) goto failed;
5557 for (i = 0; i < len; i++) {
5558 expr_ty value;
5559 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5560 if (res != 0) goto failed;
5561 asdl_seq_SET(elts, i, value);
5562 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005563 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005564 } else {
5565 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5566 return 1;
5567 }
5568 *out = Set(elts, lineno, col_offset, arena);
5569 if (*out == NULL) goto failed;
5570 return 0;
5571 }
5572 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5573 if (isinstance == -1) {
5574 return 1;
5575 }
5576 if (isinstance) {
5577 expr_ty elt;
5578 asdl_seq* generators;
5579
5580 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5581 int res;
5582 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5583 if (tmp == NULL) goto failed;
5584 res = obj2ast_expr(tmp, &elt, arena);
5585 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005586 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005587 } else {
5588 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5589 return 1;
5590 }
5591 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5592 int res;
5593 Py_ssize_t len;
5594 Py_ssize_t i;
5595 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5596 if (tmp == NULL) goto failed;
5597 if (!PyList_Check(tmp)) {
5598 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5599 goto failed;
5600 }
5601 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005602 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005603 if (generators == NULL) goto failed;
5604 for (i = 0; i < len; i++) {
5605 comprehension_ty value;
5606 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5607 if (res != 0) goto failed;
5608 asdl_seq_SET(generators, i, value);
5609 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005610 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005611 } else {
5612 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5613 return 1;
5614 }
5615 *out = ListComp(elt, generators, lineno, col_offset, arena);
5616 if (*out == NULL) goto failed;
5617 return 0;
5618 }
5619 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5620 if (isinstance == -1) {
5621 return 1;
5622 }
5623 if (isinstance) {
5624 expr_ty elt;
5625 asdl_seq* generators;
5626
5627 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5628 int res;
5629 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5630 if (tmp == NULL) goto failed;
5631 res = obj2ast_expr(tmp, &elt, arena);
5632 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005633 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005634 } else {
5635 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5636 return 1;
5637 }
5638 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5639 int res;
5640 Py_ssize_t len;
5641 Py_ssize_t i;
5642 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5643 if (tmp == NULL) goto failed;
5644 if (!PyList_Check(tmp)) {
5645 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5646 goto failed;
5647 }
5648 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005649 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005650 if (generators == NULL) goto failed;
5651 for (i = 0; i < len; i++) {
5652 comprehension_ty value;
5653 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5654 if (res != 0) goto failed;
5655 asdl_seq_SET(generators, i, value);
5656 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005657 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005658 } else {
5659 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5660 return 1;
5661 }
5662 *out = SetComp(elt, generators, lineno, col_offset, arena);
5663 if (*out == NULL) goto failed;
5664 return 0;
5665 }
5666 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5667 if (isinstance == -1) {
5668 return 1;
5669 }
5670 if (isinstance) {
5671 expr_ty key;
5672 expr_ty value;
5673 asdl_seq* generators;
5674
5675 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5676 int res;
5677 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5678 if (tmp == NULL) goto failed;
5679 res = obj2ast_expr(tmp, &key, arena);
5680 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005681 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005682 } else {
5683 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5684 return 1;
5685 }
5686 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5687 int res;
5688 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5689 if (tmp == NULL) goto failed;
5690 res = obj2ast_expr(tmp, &value, arena);
5691 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005692 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005693 } else {
5694 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5695 return 1;
5696 }
5697 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5698 int res;
5699 Py_ssize_t len;
5700 Py_ssize_t i;
5701 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5702 if (tmp == NULL) goto failed;
5703 if (!PyList_Check(tmp)) {
5704 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5705 goto failed;
5706 }
5707 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005708 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005709 if (generators == NULL) goto failed;
5710 for (i = 0; i < len; i++) {
5711 comprehension_ty value;
5712 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5713 if (res != 0) goto failed;
5714 asdl_seq_SET(generators, i, value);
5715 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005716 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005717 } else {
5718 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5719 return 1;
5720 }
5721 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5722 if (*out == NULL) goto failed;
5723 return 0;
5724 }
5725 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5726 if (isinstance == -1) {
5727 return 1;
5728 }
5729 if (isinstance) {
5730 expr_ty elt;
5731 asdl_seq* generators;
5732
5733 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5734 int res;
5735 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5736 if (tmp == NULL) goto failed;
5737 res = obj2ast_expr(tmp, &elt, arena);
5738 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005739 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005740 } else {
5741 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5742 return 1;
5743 }
5744 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5745 int res;
5746 Py_ssize_t len;
5747 Py_ssize_t i;
5748 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5749 if (tmp == NULL) goto failed;
5750 if (!PyList_Check(tmp)) {
5751 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5752 goto failed;
5753 }
5754 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005755 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005756 if (generators == NULL) goto failed;
5757 for (i = 0; i < len; i++) {
5758 comprehension_ty value;
5759 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5760 if (res != 0) goto failed;
5761 asdl_seq_SET(generators, i, value);
5762 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005763 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005764 } else {
5765 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5766 return 1;
5767 }
5768 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5769 if (*out == NULL) goto failed;
5770 return 0;
5771 }
Yury Selivanov75445082015-05-11 22:57:16 -04005772 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5773 if (isinstance == -1) {
5774 return 1;
5775 }
5776 if (isinstance) {
5777 expr_ty value;
5778
5779 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5780 int res;
5781 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5782 if (tmp == NULL) goto failed;
5783 res = obj2ast_expr(tmp, &value, arena);
5784 if (res != 0) goto failed;
5785 Py_CLEAR(tmp);
5786 } else {
5787 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5788 return 1;
5789 }
5790 *out = Await(value, lineno, col_offset, arena);
5791 if (*out == NULL) goto failed;
5792 return 0;
5793 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005794 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5795 if (isinstance == -1) {
5796 return 1;
5797 }
5798 if (isinstance) {
5799 expr_ty value;
5800
Victor Stinneree4b59c2013-07-27 00:01:35 +02005801 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005802 int res;
5803 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5804 if (tmp == NULL) goto failed;
5805 res = obj2ast_expr(tmp, &value, arena);
5806 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005807 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005808 } else {
5809 value = NULL;
5810 }
5811 *out = Yield(value, lineno, col_offset, arena);
5812 if (*out == NULL) goto failed;
5813 return 0;
5814 }
5815 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5816 if (isinstance == -1) {
5817 return 1;
5818 }
5819 if (isinstance) {
5820 expr_ty value;
5821
5822 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5823 int res;
5824 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5825 if (tmp == NULL) goto failed;
5826 res = obj2ast_expr(tmp, &value, arena);
5827 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005828 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005829 } else {
5830 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5831 return 1;
5832 }
5833 *out = YieldFrom(value, lineno, col_offset, arena);
5834 if (*out == NULL) goto failed;
5835 return 0;
5836 }
5837 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5838 if (isinstance == -1) {
5839 return 1;
5840 }
5841 if (isinstance) {
5842 expr_ty left;
5843 asdl_int_seq* ops;
5844 asdl_seq* comparators;
5845
5846 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5847 int res;
5848 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5849 if (tmp == NULL) goto failed;
5850 res = obj2ast_expr(tmp, &left, arena);
5851 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005852 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005853 } else {
5854 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5855 return 1;
5856 }
5857 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5858 int res;
5859 Py_ssize_t len;
5860 Py_ssize_t i;
5861 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5862 if (tmp == NULL) goto failed;
5863 if (!PyList_Check(tmp)) {
5864 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5865 goto failed;
5866 }
5867 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005868 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005869 if (ops == NULL) goto failed;
5870 for (i = 0; i < len; i++) {
5871 cmpop_ty value;
5872 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5873 if (res != 0) goto failed;
5874 asdl_seq_SET(ops, i, value);
5875 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005876 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005877 } else {
5878 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5879 return 1;
5880 }
5881 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5882 int res;
5883 Py_ssize_t len;
5884 Py_ssize_t i;
5885 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5886 if (tmp == NULL) goto failed;
5887 if (!PyList_Check(tmp)) {
5888 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5889 goto failed;
5890 }
5891 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005892 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005893 if (comparators == NULL) goto failed;
5894 for (i = 0; i < len; i++) {
5895 expr_ty value;
5896 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5897 if (res != 0) goto failed;
5898 asdl_seq_SET(comparators, i, value);
5899 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005900 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005901 } else {
5902 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5903 return 1;
5904 }
5905 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5906 if (*out == NULL) goto failed;
5907 return 0;
5908 }
5909 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5910 if (isinstance == -1) {
5911 return 1;
5912 }
5913 if (isinstance) {
5914 expr_ty func;
5915 asdl_seq* args;
5916 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005917
5918 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5919 int res;
5920 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5921 if (tmp == NULL) goto failed;
5922 res = obj2ast_expr(tmp, &func, arena);
5923 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005924 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005925 } else {
5926 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5927 return 1;
5928 }
5929 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5930 int res;
5931 Py_ssize_t len;
5932 Py_ssize_t i;
5933 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5934 if (tmp == NULL) goto failed;
5935 if (!PyList_Check(tmp)) {
5936 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5937 goto failed;
5938 }
5939 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005940 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005941 if (args == NULL) goto failed;
5942 for (i = 0; i < len; i++) {
5943 expr_ty value;
5944 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5945 if (res != 0) goto failed;
5946 asdl_seq_SET(args, i, value);
5947 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005948 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005949 } else {
5950 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5951 return 1;
5952 }
5953 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5954 int res;
5955 Py_ssize_t len;
5956 Py_ssize_t i;
5957 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5958 if (tmp == NULL) goto failed;
5959 if (!PyList_Check(tmp)) {
5960 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5961 goto failed;
5962 }
5963 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005964 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005965 if (keywords == NULL) goto failed;
5966 for (i = 0; i < len; i++) {
5967 keyword_ty value;
5968 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5969 if (res != 0) goto failed;
5970 asdl_seq_SET(keywords, i, value);
5971 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005972 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005973 } else {
5974 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5975 return 1;
5976 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005977 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005978 if (*out == NULL) goto failed;
5979 return 0;
5980 }
5981 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5982 if (isinstance == -1) {
5983 return 1;
5984 }
5985 if (isinstance) {
5986 object n;
5987
5988 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5989 int res;
5990 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5991 if (tmp == NULL) goto failed;
5992 res = obj2ast_object(tmp, &n, arena);
5993 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005994 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005995 } else {
5996 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5997 return 1;
5998 }
5999 *out = Num(n, lineno, col_offset, arena);
6000 if (*out == NULL) goto failed;
6001 return 0;
6002 }
6003 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6004 if (isinstance == -1) {
6005 return 1;
6006 }
6007 if (isinstance) {
6008 string s;
6009
6010 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6011 int res;
6012 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6013 if (tmp == NULL) goto failed;
6014 res = obj2ast_string(tmp, &s, arena);
6015 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006016 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006017 } else {
6018 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6019 return 1;
6020 }
6021 *out = Str(s, lineno, col_offset, arena);
6022 if (*out == NULL) goto failed;
6023 return 0;
6024 }
6025 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6026 if (isinstance == -1) {
6027 return 1;
6028 }
6029 if (isinstance) {
6030 bytes s;
6031
6032 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6033 int res;
6034 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6035 if (tmp == NULL) goto failed;
6036 res = obj2ast_bytes(tmp, &s, arena);
6037 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006038 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006039 } else {
6040 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6041 return 1;
6042 }
6043 *out = Bytes(s, lineno, col_offset, arena);
6044 if (*out == NULL) goto failed;
6045 return 0;
6046 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006047 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6048 if (isinstance == -1) {
6049 return 1;
6050 }
6051 if (isinstance) {
6052 singleton value;
6053
6054 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6055 int res;
6056 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6057 if (tmp == NULL) goto failed;
6058 res = obj2ast_singleton(tmp, &value, arena);
6059 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006060 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006061 } else {
6062 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6063 return 1;
6064 }
6065 *out = NameConstant(value, lineno, col_offset, arena);
6066 if (*out == NULL) goto failed;
6067 return 0;
6068 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006069 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6070 if (isinstance == -1) {
6071 return 1;
6072 }
6073 if (isinstance) {
6074
6075 *out = Ellipsis(lineno, col_offset, arena);
6076 if (*out == NULL) goto failed;
6077 return 0;
6078 }
6079 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6080 if (isinstance == -1) {
6081 return 1;
6082 }
6083 if (isinstance) {
6084 expr_ty value;
6085 identifier attr;
6086 expr_context_ty ctx;
6087
6088 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6089 int res;
6090 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6091 if (tmp == NULL) goto failed;
6092 res = obj2ast_expr(tmp, &value, arena);
6093 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006094 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006095 } else {
6096 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6097 return 1;
6098 }
6099 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6100 int res;
6101 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6102 if (tmp == NULL) goto failed;
6103 res = obj2ast_identifier(tmp, &attr, arena);
6104 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006105 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006106 } else {
6107 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6108 return 1;
6109 }
6110 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6111 int res;
6112 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6113 if (tmp == NULL) goto failed;
6114 res = obj2ast_expr_context(tmp, &ctx, arena);
6115 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006116 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006117 } else {
6118 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6119 return 1;
6120 }
6121 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6122 if (*out == NULL) goto failed;
6123 return 0;
6124 }
6125 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6126 if (isinstance == -1) {
6127 return 1;
6128 }
6129 if (isinstance) {
6130 expr_ty value;
6131 slice_ty slice;
6132 expr_context_ty ctx;
6133
6134 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6135 int res;
6136 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6137 if (tmp == NULL) goto failed;
6138 res = obj2ast_expr(tmp, &value, arena);
6139 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006140 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006141 } else {
6142 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6143 return 1;
6144 }
6145 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6146 int res;
6147 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6148 if (tmp == NULL) goto failed;
6149 res = obj2ast_slice(tmp, &slice, arena);
6150 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006151 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006152 } else {
6153 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6154 return 1;
6155 }
6156 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6157 int res;
6158 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6159 if (tmp == NULL) goto failed;
6160 res = obj2ast_expr_context(tmp, &ctx, arena);
6161 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006162 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006163 } else {
6164 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6165 return 1;
6166 }
6167 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6168 if (*out == NULL) goto failed;
6169 return 0;
6170 }
6171 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6172 if (isinstance == -1) {
6173 return 1;
6174 }
6175 if (isinstance) {
6176 expr_ty value;
6177 expr_context_ty ctx;
6178
6179 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6180 int res;
6181 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6182 if (tmp == NULL) goto failed;
6183 res = obj2ast_expr(tmp, &value, arena);
6184 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006185 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006186 } else {
6187 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6188 return 1;
6189 }
6190 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6191 int res;
6192 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6193 if (tmp == NULL) goto failed;
6194 res = obj2ast_expr_context(tmp, &ctx, arena);
6195 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006196 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006197 } else {
6198 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6199 return 1;
6200 }
6201 *out = Starred(value, ctx, lineno, col_offset, arena);
6202 if (*out == NULL) goto failed;
6203 return 0;
6204 }
6205 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6206 if (isinstance == -1) {
6207 return 1;
6208 }
6209 if (isinstance) {
6210 identifier id;
6211 expr_context_ty ctx;
6212
6213 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6214 int res;
6215 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6216 if (tmp == NULL) goto failed;
6217 res = obj2ast_identifier(tmp, &id, arena);
6218 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006219 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006220 } else {
6221 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6222 return 1;
6223 }
6224 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6225 int res;
6226 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6227 if (tmp == NULL) goto failed;
6228 res = obj2ast_expr_context(tmp, &ctx, arena);
6229 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006230 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006231 } else {
6232 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6233 return 1;
6234 }
6235 *out = Name(id, ctx, lineno, col_offset, arena);
6236 if (*out == NULL) goto failed;
6237 return 0;
6238 }
6239 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6240 if (isinstance == -1) {
6241 return 1;
6242 }
6243 if (isinstance) {
6244 asdl_seq* elts;
6245 expr_context_ty ctx;
6246
6247 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6248 int res;
6249 Py_ssize_t len;
6250 Py_ssize_t i;
6251 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6252 if (tmp == NULL) goto failed;
6253 if (!PyList_Check(tmp)) {
6254 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6255 goto failed;
6256 }
6257 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006258 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006259 if (elts == NULL) goto failed;
6260 for (i = 0; i < len; i++) {
6261 expr_ty value;
6262 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6263 if (res != 0) goto failed;
6264 asdl_seq_SET(elts, i, value);
6265 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006266 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006267 } else {
6268 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6269 return 1;
6270 }
6271 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6272 int res;
6273 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6274 if (tmp == NULL) goto failed;
6275 res = obj2ast_expr_context(tmp, &ctx, 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 \"ctx\" missing from List");
6280 return 1;
6281 }
6282 *out = List(elts, ctx, lineno, col_offset, arena);
6283 if (*out == NULL) goto failed;
6284 return 0;
6285 }
6286 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6287 if (isinstance == -1) {
6288 return 1;
6289 }
6290 if (isinstance) {
6291 asdl_seq* elts;
6292 expr_context_ty ctx;
6293
6294 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6295 int res;
6296 Py_ssize_t len;
6297 Py_ssize_t i;
6298 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6299 if (tmp == NULL) goto failed;
6300 if (!PyList_Check(tmp)) {
6301 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6302 goto failed;
6303 }
6304 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006305 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006306 if (elts == NULL) goto failed;
6307 for (i = 0; i < len; i++) {
6308 expr_ty value;
6309 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6310 if (res != 0) goto failed;
6311 asdl_seq_SET(elts, i, value);
6312 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006313 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006314 } else {
6315 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6316 return 1;
6317 }
6318 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6319 int res;
6320 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6321 if (tmp == NULL) goto failed;
6322 res = obj2ast_expr_context(tmp, &ctx, arena);
6323 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006324 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006325 } else {
6326 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6327 return 1;
6328 }
6329 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6330 if (*out == NULL) goto failed;
6331 return 0;
6332 }
6333
6334 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6335 failed:
6336 Py_XDECREF(tmp);
6337 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006338}
6339
6340int
6341obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6342{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006343 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006344
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006345 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6346 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006347 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006348 }
6349 if (isinstance) {
6350 *out = Load;
6351 return 0;
6352 }
6353 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6354 if (isinstance == -1) {
6355 return 1;
6356 }
6357 if (isinstance) {
6358 *out = Store;
6359 return 0;
6360 }
6361 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6362 if (isinstance == -1) {
6363 return 1;
6364 }
6365 if (isinstance) {
6366 *out = Del;
6367 return 0;
6368 }
6369 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6370 if (isinstance == -1) {
6371 return 1;
6372 }
6373 if (isinstance) {
6374 *out = AugLoad;
6375 return 0;
6376 }
6377 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6378 if (isinstance == -1) {
6379 return 1;
6380 }
6381 if (isinstance) {
6382 *out = AugStore;
6383 return 0;
6384 }
6385 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6386 if (isinstance == -1) {
6387 return 1;
6388 }
6389 if (isinstance) {
6390 *out = Param;
6391 return 0;
6392 }
6393
6394 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6395 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006396}
6397
6398int
6399obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6400{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006401 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006402
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006403 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006404
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006405 if (obj == Py_None) {
6406 *out = NULL;
6407 return 0;
6408 }
6409 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6410 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006411 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006412 }
6413 if (isinstance) {
6414 expr_ty lower;
6415 expr_ty upper;
6416 expr_ty step;
6417
Victor Stinneree4b59c2013-07-27 00:01:35 +02006418 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006419 int res;
6420 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6421 if (tmp == NULL) goto failed;
6422 res = obj2ast_expr(tmp, &lower, arena);
6423 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006424 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006425 } else {
6426 lower = NULL;
6427 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006428 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006429 int res;
6430 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6431 if (tmp == NULL) goto failed;
6432 res = obj2ast_expr(tmp, &upper, arena);
6433 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006434 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006435 } else {
6436 upper = NULL;
6437 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006438 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006439 int res;
6440 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6441 if (tmp == NULL) goto failed;
6442 res = obj2ast_expr(tmp, &step, arena);
6443 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006444 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006445 } else {
6446 step = NULL;
6447 }
6448 *out = Slice(lower, upper, step, arena);
6449 if (*out == NULL) goto failed;
6450 return 0;
6451 }
6452 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6453 if (isinstance == -1) {
6454 return 1;
6455 }
6456 if (isinstance) {
6457 asdl_seq* dims;
6458
6459 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6460 int res;
6461 Py_ssize_t len;
6462 Py_ssize_t i;
6463 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6464 if (tmp == NULL) goto failed;
6465 if (!PyList_Check(tmp)) {
6466 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6467 goto failed;
6468 }
6469 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006470 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006471 if (dims == NULL) goto failed;
6472 for (i = 0; i < len; i++) {
6473 slice_ty value;
6474 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6475 if (res != 0) goto failed;
6476 asdl_seq_SET(dims, i, value);
6477 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006478 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006479 } else {
6480 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6481 return 1;
6482 }
6483 *out = ExtSlice(dims, arena);
6484 if (*out == NULL) goto failed;
6485 return 0;
6486 }
6487 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6488 if (isinstance == -1) {
6489 return 1;
6490 }
6491 if (isinstance) {
6492 expr_ty value;
6493
6494 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6495 int res;
6496 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6497 if (tmp == NULL) goto failed;
6498 res = obj2ast_expr(tmp, &value, arena);
6499 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006500 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006501 } else {
6502 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6503 return 1;
6504 }
6505 *out = Index(value, arena);
6506 if (*out == NULL) goto failed;
6507 return 0;
6508 }
6509
6510 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6511 failed:
6512 Py_XDECREF(tmp);
6513 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006514}
6515
6516int
6517obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6518{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006519 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006520
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006521 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6522 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006523 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006524 }
6525 if (isinstance) {
6526 *out = And;
6527 return 0;
6528 }
6529 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6530 if (isinstance == -1) {
6531 return 1;
6532 }
6533 if (isinstance) {
6534 *out = Or;
6535 return 0;
6536 }
6537
6538 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6539 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006540}
6541
6542int
6543obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6544{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006545 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006546
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006547 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6548 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006549 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 }
6551 if (isinstance) {
6552 *out = Add;
6553 return 0;
6554 }
6555 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6556 if (isinstance == -1) {
6557 return 1;
6558 }
6559 if (isinstance) {
6560 *out = Sub;
6561 return 0;
6562 }
6563 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6564 if (isinstance == -1) {
6565 return 1;
6566 }
6567 if (isinstance) {
6568 *out = Mult;
6569 return 0;
6570 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006571 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6572 if (isinstance == -1) {
6573 return 1;
6574 }
6575 if (isinstance) {
6576 *out = MatMult;
6577 return 0;
6578 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006579 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6580 if (isinstance == -1) {
6581 return 1;
6582 }
6583 if (isinstance) {
6584 *out = Div;
6585 return 0;
6586 }
6587 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6588 if (isinstance == -1) {
6589 return 1;
6590 }
6591 if (isinstance) {
6592 *out = Mod;
6593 return 0;
6594 }
6595 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6596 if (isinstance == -1) {
6597 return 1;
6598 }
6599 if (isinstance) {
6600 *out = Pow;
6601 return 0;
6602 }
6603 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6604 if (isinstance == -1) {
6605 return 1;
6606 }
6607 if (isinstance) {
6608 *out = LShift;
6609 return 0;
6610 }
6611 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6612 if (isinstance == -1) {
6613 return 1;
6614 }
6615 if (isinstance) {
6616 *out = RShift;
6617 return 0;
6618 }
6619 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6620 if (isinstance == -1) {
6621 return 1;
6622 }
6623 if (isinstance) {
6624 *out = BitOr;
6625 return 0;
6626 }
6627 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6628 if (isinstance == -1) {
6629 return 1;
6630 }
6631 if (isinstance) {
6632 *out = BitXor;
6633 return 0;
6634 }
6635 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6636 if (isinstance == -1) {
6637 return 1;
6638 }
6639 if (isinstance) {
6640 *out = BitAnd;
6641 return 0;
6642 }
6643 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6644 if (isinstance == -1) {
6645 return 1;
6646 }
6647 if (isinstance) {
6648 *out = FloorDiv;
6649 return 0;
6650 }
6651
6652 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6653 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006654}
6655
6656int
6657obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6658{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006659 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006660
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006661 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6662 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006663 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006664 }
6665 if (isinstance) {
6666 *out = Invert;
6667 return 0;
6668 }
6669 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6670 if (isinstance == -1) {
6671 return 1;
6672 }
6673 if (isinstance) {
6674 *out = Not;
6675 return 0;
6676 }
6677 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6678 if (isinstance == -1) {
6679 return 1;
6680 }
6681 if (isinstance) {
6682 *out = UAdd;
6683 return 0;
6684 }
6685 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6686 if (isinstance == -1) {
6687 return 1;
6688 }
6689 if (isinstance) {
6690 *out = USub;
6691 return 0;
6692 }
6693
6694 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6695 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006696}
6697
6698int
6699obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6700{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006701 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006702
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006703 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6704 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006705 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006706 }
6707 if (isinstance) {
6708 *out = Eq;
6709 return 0;
6710 }
6711 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6712 if (isinstance == -1) {
6713 return 1;
6714 }
6715 if (isinstance) {
6716 *out = NotEq;
6717 return 0;
6718 }
6719 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6720 if (isinstance == -1) {
6721 return 1;
6722 }
6723 if (isinstance) {
6724 *out = Lt;
6725 return 0;
6726 }
6727 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6728 if (isinstance == -1) {
6729 return 1;
6730 }
6731 if (isinstance) {
6732 *out = LtE;
6733 return 0;
6734 }
6735 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6736 if (isinstance == -1) {
6737 return 1;
6738 }
6739 if (isinstance) {
6740 *out = Gt;
6741 return 0;
6742 }
6743 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6744 if (isinstance == -1) {
6745 return 1;
6746 }
6747 if (isinstance) {
6748 *out = GtE;
6749 return 0;
6750 }
6751 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6752 if (isinstance == -1) {
6753 return 1;
6754 }
6755 if (isinstance) {
6756 *out = Is;
6757 return 0;
6758 }
6759 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6760 if (isinstance == -1) {
6761 return 1;
6762 }
6763 if (isinstance) {
6764 *out = IsNot;
6765 return 0;
6766 }
6767 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6768 if (isinstance == -1) {
6769 return 1;
6770 }
6771 if (isinstance) {
6772 *out = In;
6773 return 0;
6774 }
6775 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6776 if (isinstance == -1) {
6777 return 1;
6778 }
6779 if (isinstance) {
6780 *out = NotIn;
6781 return 0;
6782 }
6783
6784 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6785 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006786}
6787
6788int
6789obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6790{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006791 PyObject* tmp = NULL;
6792 expr_ty target;
6793 expr_ty iter;
6794 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006795
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006796 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6797 int res;
6798 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6799 if (tmp == NULL) goto failed;
6800 res = obj2ast_expr(tmp, &target, arena);
6801 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006802 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006803 } else {
6804 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006805 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006806 }
6807 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6808 int res;
6809 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6810 if (tmp == NULL) goto failed;
6811 res = obj2ast_expr(tmp, &iter, arena);
6812 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006813 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006814 } else {
6815 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6816 return 1;
6817 }
6818 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6819 int res;
6820 Py_ssize_t len;
6821 Py_ssize_t i;
6822 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6823 if (tmp == NULL) goto failed;
6824 if (!PyList_Check(tmp)) {
6825 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6826 goto failed;
6827 }
6828 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006829 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006830 if (ifs == NULL) goto failed;
6831 for (i = 0; i < len; i++) {
6832 expr_ty value;
6833 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6834 if (res != 0) goto failed;
6835 asdl_seq_SET(ifs, i, value);
6836 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006837 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006838 } else {
6839 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6840 return 1;
6841 }
6842 *out = comprehension(target, iter, ifs, arena);
6843 return 0;
6844failed:
6845 Py_XDECREF(tmp);
6846 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006847}
6848
6849int
6850obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6851{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006852 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006853
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006854 PyObject *tmp = NULL;
6855 int lineno;
6856 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006857
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006858 if (obj == Py_None) {
6859 *out = NULL;
6860 return 0;
6861 }
6862 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6863 int res;
6864 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6865 if (tmp == NULL) goto failed;
6866 res = obj2ast_int(tmp, &lineno, arena);
6867 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006868 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006869 } else {
6870 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006871 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006872 }
6873 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6874 int res;
6875 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6876 if (tmp == NULL) goto failed;
6877 res = obj2ast_int(tmp, &col_offset, arena);
6878 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006880 } else {
6881 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6882 return 1;
6883 }
6884 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6885 if (isinstance == -1) {
6886 return 1;
6887 }
6888 if (isinstance) {
6889 expr_ty type;
6890 identifier name;
6891 asdl_seq* body;
6892
Victor Stinneree4b59c2013-07-27 00:01:35 +02006893 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006894 int res;
6895 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6896 if (tmp == NULL) goto failed;
6897 res = obj2ast_expr(tmp, &type, arena);
6898 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006899 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006900 } else {
6901 type = NULL;
6902 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006903 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006904 int res;
6905 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6906 if (tmp == NULL) goto failed;
6907 res = obj2ast_identifier(tmp, &name, arena);
6908 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006909 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006910 } else {
6911 name = NULL;
6912 }
6913 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6914 int res;
6915 Py_ssize_t len;
6916 Py_ssize_t i;
6917 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6918 if (tmp == NULL) goto failed;
6919 if (!PyList_Check(tmp)) {
6920 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6921 goto failed;
6922 }
6923 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006924 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006925 if (body == NULL) goto failed;
6926 for (i = 0; i < len; i++) {
6927 stmt_ty value;
6928 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6929 if (res != 0) goto failed;
6930 asdl_seq_SET(body, i, value);
6931 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006932 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006933 } else {
6934 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6935 return 1;
6936 }
6937 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6938 if (*out == NULL) goto failed;
6939 return 0;
6940 }
6941
6942 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6943 failed:
6944 Py_XDECREF(tmp);
6945 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006946}
6947
6948int
6949obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6950{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006951 PyObject* tmp = NULL;
6952 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006953 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006954 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006955 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006956 arg_ty kwarg;
6957 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006958
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006959 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6960 int res;
6961 Py_ssize_t len;
6962 Py_ssize_t i;
6963 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6964 if (tmp == NULL) goto failed;
6965 if (!PyList_Check(tmp)) {
6966 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6967 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006968 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006969 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006970 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006971 if (args == NULL) goto failed;
6972 for (i = 0; i < len; i++) {
6973 arg_ty value;
6974 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6975 if (res != 0) goto failed;
6976 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006977 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006978 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006979 } else {
6980 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006981 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006982 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006983 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006984 int res;
6985 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6986 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006987 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006988 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006989 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006990 } else {
6991 vararg = NULL;
6992 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006993 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6994 int res;
6995 Py_ssize_t len;
6996 Py_ssize_t i;
6997 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
6998 if (tmp == NULL) goto failed;
6999 if (!PyList_Check(tmp)) {
7000 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7001 goto failed;
7002 }
7003 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007004 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007005 if (kwonlyargs == NULL) goto failed;
7006 for (i = 0; i < len; i++) {
7007 arg_ty value;
7008 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7009 if (res != 0) goto failed;
7010 asdl_seq_SET(kwonlyargs, i, value);
7011 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007012 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007013 } else {
7014 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7015 return 1;
7016 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007017 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7018 int res;
7019 Py_ssize_t len;
7020 Py_ssize_t i;
7021 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7022 if (tmp == NULL) goto failed;
7023 if (!PyList_Check(tmp)) {
7024 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7025 goto failed;
7026 }
7027 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007028 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007029 if (kw_defaults == NULL) goto failed;
7030 for (i = 0; i < len; i++) {
7031 expr_ty value;
7032 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7033 if (res != 0) goto failed;
7034 asdl_seq_SET(kw_defaults, 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 \"kw_defaults\" missing from arguments");
7039 return 1;
7040 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007041 if (exists_not_none(obj, &PyId_kwarg)) {
7042 int res;
7043 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7044 if (tmp == NULL) goto failed;
7045 res = obj2ast_arg(tmp, &kwarg, arena);
7046 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007047 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007048 } else {
7049 kwarg = NULL;
7050 }
7051 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7052 int res;
7053 Py_ssize_t len;
7054 Py_ssize_t i;
7055 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7056 if (tmp == NULL) goto failed;
7057 if (!PyList_Check(tmp)) {
7058 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7059 goto failed;
7060 }
7061 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007062 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007063 if (defaults == NULL) goto failed;
7064 for (i = 0; i < len; i++) {
7065 expr_ty value;
7066 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7067 if (res != 0) goto failed;
7068 asdl_seq_SET(defaults, i, value);
7069 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007070 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007071 } else {
7072 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7073 return 1;
7074 }
7075 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7076 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007077 return 0;
7078failed:
7079 Py_XDECREF(tmp);
7080 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007081}
7082
7083int
7084obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007086 PyObject* tmp = NULL;
7087 identifier arg;
7088 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007089
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007090 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7091 int res;
7092 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7093 if (tmp == NULL) goto failed;
7094 res = obj2ast_identifier(tmp, &arg, arena);
7095 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007096 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007097 } else {
7098 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007099 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007100 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007101 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007102 int res;
7103 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7104 if (tmp == NULL) goto failed;
7105 res = obj2ast_expr(tmp, &annotation, arena);
7106 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007107 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007108 } else {
7109 annotation = NULL;
7110 }
7111 *out = arg(arg, annotation, arena);
7112 return 0;
7113failed:
7114 Py_XDECREF(tmp);
7115 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007116}
7117
7118int
7119obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7120{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007121 PyObject* tmp = NULL;
7122 identifier arg;
7123 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007124
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007125 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007126 int res;
7127 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7128 if (tmp == NULL) goto failed;
7129 res = obj2ast_identifier(tmp, &arg, arena);
7130 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007131 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007132 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007133 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007134 }
7135 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7136 int res;
7137 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7138 if (tmp == NULL) goto failed;
7139 res = obj2ast_expr(tmp, &value, arena);
7140 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007141 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007142 } else {
7143 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7144 return 1;
7145 }
7146 *out = keyword(arg, value, arena);
7147 return 0;
7148failed:
7149 Py_XDECREF(tmp);
7150 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007151}
7152
7153int
7154obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7155{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007156 PyObject* tmp = NULL;
7157 identifier name;
7158 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007159
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007160 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7161 int res;
7162 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7163 if (tmp == NULL) goto failed;
7164 res = obj2ast_identifier(tmp, &name, arena);
7165 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007166 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007167 } else {
7168 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007169 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007170 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007171 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007172 int res;
7173 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7174 if (tmp == NULL) goto failed;
7175 res = obj2ast_identifier(tmp, &asname, arena);
7176 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007177 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007178 } else {
7179 asname = NULL;
7180 }
7181 *out = alias(name, asname, arena);
7182 return 0;
7183failed:
7184 Py_XDECREF(tmp);
7185 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007186}
7187
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007188int
7189obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7190{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007191 PyObject* tmp = NULL;
7192 expr_ty context_expr;
7193 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007194
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007195 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7196 int res;
7197 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7198 if (tmp == NULL) goto failed;
7199 res = obj2ast_expr(tmp, &context_expr, arena);
7200 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007201 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007202 } else {
7203 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007204 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007205 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007206 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007207 int res;
7208 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7209 if (tmp == NULL) goto failed;
7210 res = obj2ast_expr(tmp, &optional_vars, arena);
7211 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007212 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007213 } else {
7214 optional_vars = NULL;
7215 }
7216 *out = withitem(context_expr, optional_vars, arena);
7217 return 0;
7218failed:
7219 Py_XDECREF(tmp);
7220 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007221}
7222
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007223
Martin v. Löwis1a214512008-06-11 05:26:20 +00007224static struct PyModuleDef _astmodule = {
7225 PyModuleDef_HEAD_INIT, "_ast"
7226};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007227PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007228PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007229{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007230 PyObject *m, *d;
7231 if (!init_types()) return NULL;
7232 m = PyModule_Create(&_astmodule);
7233 if (!m) return NULL;
7234 d = PyModule_GetDict(m);
7235 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007236 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007237 return NULL;
7238 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7239 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7240 NULL;
7241 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7242 0) return NULL;
7243 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7244 return NULL;
7245 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7246 NULL;
7247 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7248 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7249 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007250 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7251 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007252 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7253 return NULL;
7254 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7255 NULL;
7256 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7257 NULL;
7258 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7259 NULL;
7260 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7261 return NULL;
7262 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007263 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7264 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007265 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7266 NULL;
7267 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7268 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007269 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7270 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007271 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7272 NULL;
7273 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7274 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7275 NULL;
7276 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7277 NULL;
7278 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7279 return NULL;
7280 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7281 NULL;
7282 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7283 return NULL;
7284 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7285 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7286 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7287 NULL;
7288 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7289 return NULL;
7290 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7291 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7292 NULL;
7293 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7294 NULL;
7295 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7296 NULL;
7297 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7298 NULL;
7299 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7300 NULL;
7301 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7302 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7303 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7304 return NULL;
7305 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7306 NULL;
7307 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7308 return NULL;
7309 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7310 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007311 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7312 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007313 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7314 NULL;
7315 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7316 return NULL;
7317 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7318 NULL;
7319 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7320 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7321 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
7322 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7323 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007324 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7325 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7327 return NULL;
7328 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7329 return NULL;
7330 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7331 return NULL;
7332 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7333 NULL;
7334 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7335 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7336 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7337 NULL;
7338 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7339 0) return NULL;
7340 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7341 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7342 NULL;
7343 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7344 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7345 NULL;
7346 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7347 return NULL;
7348 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7349 NULL;
7350 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7351 NULL;
7352 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7353 NULL;
7354 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7355 return NULL;
7356 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7357 NULL;
7358 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7359 NULL;
7360 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7361 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7362 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7363 return NULL;
7364 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7365 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7366 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007367 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7368 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007369 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7370 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7371 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7372 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7373 NULL;
7374 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7375 NULL;
7376 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7377 NULL;
7378 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7379 NULL;
7380 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7381 NULL;
7382 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7383 return NULL;
7384 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7385 NULL;
7386 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7387 NULL;
7388 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7389 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7390 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7391 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7392 NULL;
7393 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7394 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7395 NULL;
7396 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7397 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7398 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7399 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7400 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7401 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7402 NULL;
7403 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7404 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7405 NULL;
7406 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7407 < 0) return NULL;
7408 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7409 < 0) return NULL;
7410 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7411 < 0) return NULL;
7412 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7413 return NULL;
7414 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7415 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7416 NULL;
7417 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7418 NULL;
7419 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7420 return NULL;
7421 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007422}
7423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007424
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007425PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007426{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007427 if (!init_types())
7428 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007429 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007430}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007431
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007432/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7433mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007434{
7435 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007436 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007437 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007438 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007439
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007440 req_type[0] = (PyObject*)Module_type;
7441 req_type[1] = (PyObject*)Expression_type;
7442 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007443
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007444 assert(0 <= mode && mode <= 2);
7445
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007446 if (!init_types())
7447 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007448
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007449 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7450 if (isinstance == -1)
7451 return NULL;
7452 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007453 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7454 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007455 return NULL;
7456 }
7457 if (obj2ast_mod(ast, &res, arena) != 0)
7458 return NULL;
7459 else
7460 return res;
7461}
7462
7463int PyAST_Check(PyObject* obj)
7464{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007465 if (!init_types())
7466 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007467 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007468}
7469
Martin v. Löwis5b222132007-06-10 09:51:05 +00007470