blob: edfcbad1341ca50dbd65b3c878b8b17eb6f2cbb3 [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -07003#include <stddef.h>
4
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "Python.h"
6#include "Python-ast.h"
7
Neal Norwitz207c9f32008-03-31 04:42:11 +00008static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +00009static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000010static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000011static PyTypeObject *Module_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020012_Py_IDENTIFIER(body);
Neal Norwitz53d960c2006-02-28 22:47:29 +000013static char *Module_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020014 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000015};
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *Interactive_type;
17static char *Interactive_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020018 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000019};
Neal Norwitz53d960c2006-02-28 22:47:29 +000020static PyTypeObject *Expression_type;
21static char *Expression_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020022 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000023};
Neal Norwitz53d960c2006-02-28 22:47:29 +000024static PyTypeObject *Suite_type;
25static char *Suite_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020026 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000027};
Neal Norwitz53d960c2006-02-28 22:47:29 +000028static PyTypeObject *stmt_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020029_Py_IDENTIFIER(lineno);
30_Py_IDENTIFIER(col_offset);
Neal Norwitz53d960c2006-02-28 22:47:29 +000031static char *stmt_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +020032 "lineno",
33 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000034};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000035static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000036static PyTypeObject *FunctionDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020037_Py_IDENTIFIER(name);
38_Py_IDENTIFIER(args);
39_Py_IDENTIFIER(decorator_list);
40_Py_IDENTIFIER(returns);
Neal Norwitz53d960c2006-02-28 22:47:29 +000041static char *FunctionDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020042 "name",
43 "args",
44 "body",
45 "decorator_list",
46 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Yury Selivanov75445082015-05-11 22:57:16 -040048static PyTypeObject *AsyncFunctionDef_type;
49static char *AsyncFunctionDef_fields[]={
50 "name",
51 "args",
52 "body",
53 "decorator_list",
54 "returns",
55};
Neal Norwitz53d960c2006-02-28 22:47:29 +000056static PyTypeObject *ClassDef_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020057_Py_IDENTIFIER(bases);
58_Py_IDENTIFIER(keywords);
Neal Norwitz53d960c2006-02-28 22:47:29 +000059static char *ClassDef_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020060 "name",
61 "bases",
62 "keywords",
Victor Stinnerce72e1c2013-07-27 00:00:36 +020063 "body",
64 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000065};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Return_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020067_Py_IDENTIFIER(value);
Neal Norwitz53d960c2006-02-28 22:47:29 +000068static char *Return_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020069 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000070};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *Delete_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020072_Py_IDENTIFIER(targets);
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static char *Delete_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020074 "targets",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075};
Neal Norwitz53d960c2006-02-28 22:47:29 +000076static PyTypeObject *Assign_type;
77static char *Assign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020078 "targets",
79 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000080};
Neal Norwitz53d960c2006-02-28 22:47:29 +000081static PyTypeObject *AugAssign_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020082_Py_IDENTIFIER(target);
83_Py_IDENTIFIER(op);
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static char *AugAssign_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020085 "target",
86 "op",
87 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088};
Neal Norwitz53d960c2006-02-28 22:47:29 +000089static PyTypeObject *For_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020090_Py_IDENTIFIER(iter);
91_Py_IDENTIFIER(orelse);
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static char *For_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +020093 "target",
94 "iter",
95 "body",
96 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000097};
Yury Selivanov75445082015-05-11 22:57:16 -040098static PyTypeObject *AsyncFor_type;
99static char *AsyncFor_fields[]={
100 "target",
101 "iter",
102 "body",
103 "orelse",
104};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000105static PyTypeObject *While_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200106_Py_IDENTIFIER(test);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static char *While_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200108 "test",
109 "body",
110 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000111};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000112static PyTypeObject *If_type;
113static char *If_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200114 "test",
115 "body",
116 "orelse",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *With_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200119_Py_IDENTIFIER(items);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000120static char *With_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200121 "items",
122 "body",
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123};
Yury Selivanov75445082015-05-11 22:57:16 -0400124static PyTypeObject *AsyncWith_type;
125static char *AsyncWith_fields[]={
126 "items",
127 "body",
128};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000129static PyTypeObject *Raise_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200130_Py_IDENTIFIER(exc);
131_Py_IDENTIFIER(cause);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000132static char *Raise_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200133 "exc",
134 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500136static PyTypeObject *Try_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200137_Py_IDENTIFIER(handlers);
138_Py_IDENTIFIER(finalbody);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500139static char *Try_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200140 "body",
141 "handlers",
142 "orelse",
143 "finalbody",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *Assert_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200146_Py_IDENTIFIER(msg);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000147static char *Assert_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200148 "test",
149 "msg",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000150};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000151static PyTypeObject *Import_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200152_Py_IDENTIFIER(names);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000153static char *Import_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200154 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000155};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000156static PyTypeObject *ImportFrom_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200157_Py_IDENTIFIER(module);
158_Py_IDENTIFIER(level);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static char *ImportFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200160 "module",
161 "names",
162 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *Global_type;
165static char *Global_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200166 "names",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000168static PyTypeObject *Nonlocal_type;
169static char *Nonlocal_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200170 "names",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000171};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000172static PyTypeObject *Expr_type;
173static char *Expr_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200174 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000175};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000176static PyTypeObject *Pass_type;
177static PyTypeObject *Break_type;
178static PyTypeObject *Continue_type;
179static PyTypeObject *expr_type;
180static char *expr_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200181 "lineno",
182 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000183};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000184static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000185static PyTypeObject *BoolOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200186_Py_IDENTIFIER(values);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000187static char *BoolOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200188 "op",
189 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000190};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000191static PyTypeObject *BinOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200192_Py_IDENTIFIER(left);
193_Py_IDENTIFIER(right);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000194static char *BinOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200195 "left",
196 "op",
197 "right",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000199static PyTypeObject *UnaryOp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200200_Py_IDENTIFIER(operand);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static char *UnaryOp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200202 "op",
203 "operand",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Lambda_type;
206static char *Lambda_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200207 "args",
208 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000209};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000210static PyTypeObject *IfExp_type;
211static char *IfExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200212 "test",
213 "body",
214 "orelse",
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000215};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000216static PyTypeObject *Dict_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200217_Py_IDENTIFIER(keys);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000218static char *Dict_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200219 "keys",
220 "values",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000222static PyTypeObject *Set_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200223_Py_IDENTIFIER(elts);
Guido van Rossum86e58e22006-08-28 15:27:34 +0000224static char *Set_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200225 "elts",
Guido van Rossum86e58e22006-08-28 15:27:34 +0000226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *ListComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200228_Py_IDENTIFIER(elt);
229_Py_IDENTIFIER(generators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000230static char *ListComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200231 "elt",
232 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000234static PyTypeObject *SetComp_type;
235static char *SetComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200236 "elt",
237 "generators",
Nick Coghlan650f0d02007-04-15 12:05:43 +0000238};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000239static PyTypeObject *DictComp_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200240_Py_IDENTIFIER(key);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000241static char *DictComp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200242 "key",
243 "value",
244 "generators",
Guido van Rossum992d4a32007-07-11 13:09:30 +0000245};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000246static PyTypeObject *GeneratorExp_type;
247static char *GeneratorExp_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200248 "elt",
249 "generators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250};
Yury Selivanov75445082015-05-11 22:57:16 -0400251static PyTypeObject *Await_type;
252static char *Await_fields[]={
253 "value",
254};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000255static PyTypeObject *Yield_type;
256static char *Yield_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200257 "value",
Benjamin Peterson527c6222012-01-14 08:58:23 -0500258};
259static PyTypeObject *YieldFrom_type;
260static char *YieldFrom_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200261 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000262};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000263static PyTypeObject *Compare_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200264_Py_IDENTIFIER(ops);
265_Py_IDENTIFIER(comparators);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000266static char *Compare_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200267 "left",
268 "ops",
269 "comparators",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000270};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000271static PyTypeObject *Call_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200272_Py_IDENTIFIER(func);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static char *Call_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200274 "func",
275 "args",
276 "keywords",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000277};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *Num_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200279_Py_IDENTIFIER(n);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000280static char *Num_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200281 "n",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000282};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000283static PyTypeObject *Str_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200284_Py_IDENTIFIER(s);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static char *Str_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200286 "s",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000288static PyTypeObject *Bytes_type;
289static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200290 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000291};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500292static PyTypeObject *NameConstant_type;
293static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200294 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500295};
Georg Brandl52318d62006-09-06 07:06:08 +0000296static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000297static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200298_Py_IDENTIFIER(attr);
299_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200301 "value",
302 "attr",
303 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000304};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000305static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200306_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000307static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200308 "value",
309 "slice",
310 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000311};
Guido van Rossum0368b722007-05-11 16:50:42 +0000312static PyTypeObject *Starred_type;
313static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200314 "value",
315 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000316};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000317static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200318_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000319static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200320 "id",
321 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000322};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000323static PyTypeObject *List_type;
324static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200325 "elts",
326 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000327};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000328static PyTypeObject *Tuple_type;
329static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200330 "elts",
331 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
335*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
336static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000337static PyTypeObject *Load_type;
338static PyTypeObject *Store_type;
339static PyTypeObject *Del_type;
340static PyTypeObject *AugLoad_type;
341static PyTypeObject *AugStore_type;
342static PyTypeObject *Param_type;
343static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000345static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200346_Py_IDENTIFIER(lower);
347_Py_IDENTIFIER(upper);
348_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200350 "lower",
351 "upper",
352 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000354static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200355_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200357 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000359static PyTypeObject *Index_type;
360static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200361 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000363static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000364static PyObject *And_singleton, *Or_singleton;
365static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static PyTypeObject *And_type;
367static PyTypeObject *Or_type;
368static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400370*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
371*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
372*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000373static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000374static PyTypeObject *Add_type;
375static PyTypeObject *Sub_type;
376static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400377static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000378static PyTypeObject *Div_type;
379static PyTypeObject *Mod_type;
380static PyTypeObject *Pow_type;
381static PyTypeObject *LShift_type;
382static PyTypeObject *RShift_type;
383static PyTypeObject *BitOr_type;
384static PyTypeObject *BitXor_type;
385static PyTypeObject *BitAnd_type;
386static PyTypeObject *FloorDiv_type;
387static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000388static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
389*USub_singleton;
390static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000391static PyTypeObject *Invert_type;
392static PyTypeObject *Not_type;
393static PyTypeObject *UAdd_type;
394static PyTypeObject *USub_type;
395static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000396static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
397*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
398*NotIn_singleton;
399static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000400static PyTypeObject *Eq_type;
401static PyTypeObject *NotEq_type;
402static PyTypeObject *Lt_type;
403static PyTypeObject *LtE_type;
404static PyTypeObject *Gt_type;
405static PyTypeObject *GtE_type;
406static PyTypeObject *Is_type;
407static PyTypeObject *IsNot_type;
408static PyTypeObject *In_type;
409static PyTypeObject *NotIn_type;
410static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000411static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200412_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000413static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200414 "target",
415 "iter",
416 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000417};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000418static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000419static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200420 "lineno",
421 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000422};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000423static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000424static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200425_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000426static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200427 "type",
428 "name",
429 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000430};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000431static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000432static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200433_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200434_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200435_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700436_Py_IDENTIFIER(kwarg);
437_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000438static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200439 "args",
440 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200441 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200442 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200443 "kwarg",
444 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000445};
Neal Norwitzc1505362006-12-28 06:47:50 +0000446static PyTypeObject *arg_type;
447static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700448static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200449 "lineno",
450 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700451};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200452_Py_IDENTIFIER(arg);
453_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000454static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200455 "arg",
456 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000457};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000458static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000459static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000460static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200461 "arg",
462 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000463};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000464static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000465static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200466_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000467static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200468 "name",
469 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000470};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500471static PyTypeObject *withitem_type;
472static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200473_Py_IDENTIFIER(context_expr);
474_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500475static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200476 "context_expr",
477 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500478};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000479
480
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700481typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100482 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700483 PyObject *dict;
484} AST_object;
485
Benjamin Peterson1767e022012-03-14 21:50:29 -0500486static void
487ast_dealloc(AST_object *self)
488{
489 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200490 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500491}
492
Neal Norwitz207c9f32008-03-31 04:42:11 +0000493static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700494ast_traverse(AST_object *self, visitproc visit, void *arg)
495{
496 Py_VISIT(self->dict);
497 return 0;
498}
499
500static void
501ast_clear(AST_object *self)
502{
503 Py_CLEAR(self->dict);
504}
505
506static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000507ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
508{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200509 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000510 Py_ssize_t i, numfields = 0;
511 int res = -1;
512 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200513 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000514 if (!fields)
515 PyErr_Clear();
516 if (fields) {
517 numfields = PySequence_Size(fields);
518 if (numfields == -1)
519 goto cleanup;
520 }
521 res = 0; /* if no error occurs, this stays 0 to the end */
522 if (PyTuple_GET_SIZE(args) > 0) {
523 if (numfields != PyTuple_GET_SIZE(args)) {
524 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000525 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000526 Py_TYPE(self)->tp_name,
527 numfields == 0 ? "" : "either 0 or ",
528 numfields, numfields == 1 ? "" : "s");
529 res = -1;
530 goto cleanup;
531 }
532 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
533 /* cannot be reached when fields is NULL */
534 PyObject *name = PySequence_GetItem(fields, i);
535 if (!name) {
536 res = -1;
537 goto cleanup;
538 }
539 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
540 Py_DECREF(name);
541 if (res < 0)
542 goto cleanup;
543 }
544 }
545 if (kw) {
546 i = 0; /* needed by PyDict_Next */
547 while (PyDict_Next(kw, &i, &key, &value)) {
548 res = PyObject_SetAttr(self, key, value);
549 if (res < 0)
550 goto cleanup;
551 }
552 }
553 cleanup:
554 Py_XDECREF(fields);
555 return res;
556}
557
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000558/* Pickling support */
559static PyObject *
560ast_type_reduce(PyObject *self, PyObject *unused)
561{
562 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200563 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200564 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000565 if (dict == NULL) {
566 if (PyErr_ExceptionMatches(PyExc_AttributeError))
567 PyErr_Clear();
568 else
569 return NULL;
570 }
571 if (dict) {
572 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
573 Py_DECREF(dict);
574 return res;
575 }
576 return Py_BuildValue("O()", Py_TYPE(self));
577}
578
579static PyMethodDef ast_type_methods[] = {
580 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
581 {NULL}
582};
583
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700584static PyGetSetDef ast_type_getsets[] = {
585 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
586 {NULL}
587};
588
Neal Norwitz207c9f32008-03-31 04:42:11 +0000589static PyTypeObject AST_type = {
590 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000591 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700592 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000593 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500594 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000595 0, /* tp_print */
596 0, /* tp_getattr */
597 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000598 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000599 0, /* tp_repr */
600 0, /* tp_as_number */
601 0, /* tp_as_sequence */
602 0, /* tp_as_mapping */
603 0, /* tp_hash */
604 0, /* tp_call */
605 0, /* tp_str */
606 PyObject_GenericGetAttr, /* tp_getattro */
607 PyObject_GenericSetAttr, /* tp_setattro */
608 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000610 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700611 (traverseproc)ast_traverse, /* tp_traverse */
612 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000613 0, /* tp_richcompare */
614 0, /* tp_weaklistoffset */
615 0, /* tp_iter */
616 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000617 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000618 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700619 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000620 0, /* tp_base */
621 0, /* tp_dict */
622 0, /* tp_descr_get */
623 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700624 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000625 (initproc)ast_type_init, /* tp_init */
626 PyType_GenericAlloc, /* tp_alloc */
627 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700628 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000629};
630
631
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000632static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
633{
634 PyObject *fnames, *result;
635 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000636 fnames = PyTuple_New(num_fields);
637 if (!fnames) return NULL;
638 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000639 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000640 if (!field) {
641 Py_DECREF(fnames);
642 return NULL;
643 }
644 PyTuple_SET_ITEM(fnames, i, field);
645 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000646 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 Py_DECREF(fnames);
649 return (PyTypeObject*)result;
650}
651
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
653{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200655 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000656 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000657 if (!l)
658 return 0;
659 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000660 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!s) {
662 Py_DECREF(l);
663 return 0;
664 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000665 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200667 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668 Py_DECREF(l);
669 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670}
671
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000672/* Conversion AST -> Python */
673
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
675{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700676 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000677 PyObject *result = PyList_New(n);
678 PyObject *value;
679 if (!result)
680 return NULL;
681 for (i = 0; i < n; i++) {
682 value = func(asdl_seq_GET(seq, i));
683 if (!value) {
684 Py_DECREF(result);
685 return NULL;
686 }
687 PyList_SET_ITEM(result, i, value);
688 }
689 return result;
690}
691
692static PyObject* ast2obj_object(void *o)
693{
694 if (!o)
695 o = Py_None;
696 Py_INCREF((PyObject*)o);
697 return (PyObject*)o;
698}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500699#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700#define ast2obj_identifier ast2obj_object
701#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500702#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000704static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705{
Christian Heimes217cfd12007-12-02 14:31:20 +0000706 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707}
708
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000709/* Conversion Python -> AST */
710
Benjamin Peterson442f2092012-12-06 17:41:04 -0500711static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
712{
713 if (obj != Py_None && obj != Py_True && obj != Py_False) {
714 PyErr_SetString(PyExc_ValueError,
715 "AST singleton must be True, False, or None");
716 return 1;
717 }
718 *out = obj;
719 return 0;
720}
721
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000722static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
723{
724 if (obj == Py_None)
725 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200726 if (obj) {
727 if (PyArena_AddPyObject(arena, obj) < 0) {
728 *out = NULL;
729 return -1;
730 }
731 Py_INCREF(obj);
732 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000733 *out = obj;
734 return 0;
735}
736
Benjamin Peterson180e6352011-07-22 11:09:07 -0500737static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500738{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500739 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
740 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500741 return 1;
742 }
743 return obj2ast_object(obj, out, arena);
744}
745
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500746static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
747{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400748 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500749 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
750 return 1;
751 }
752 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500753}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000754
Benjamin Petersone2498412011-08-09 16:08:39 -0500755static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
756{
757 if (!PyBytes_CheckExact(obj)) {
758 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
759 return 1;
760 }
761 return obj2ast_object(obj, out, arena);
762}
763
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000764static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
765{
766 int i;
767 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100768 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000769 return 1;
770 }
771
772 i = (int)PyLong_AsLong(obj);
773 if (i == -1 && PyErr_Occurred())
774 return 1;
775 *out = i;
776 return 0;
777}
778
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000779static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000780{
781 PyObject *empty_tuple, *d;
782 if (PyType_Ready(&AST_type) < 0)
783 return -1;
784 d = AST_type.tp_dict;
785 empty_tuple = PyTuple_New(0);
786 if (!empty_tuple ||
787 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
788 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
789 Py_XDECREF(empty_tuple);
790 return -1;
791 }
792 Py_DECREF(empty_tuple);
793 return 0;
794}
795
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700796static int exists_not_none(PyObject *obj, _Py_Identifier *id)
797{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700798 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700799 PyObject *attr = _PyObject_GetAttrId(obj, id);
800 if (!attr) {
801 PyErr_Clear();
802 return 0;
803 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700804 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700805 Py_DECREF(attr);
806 return !isnone;
807}
808
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000809
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810static int init_types(void)
811{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200812 static int initialized;
813 if (initialized) return 1;
814 if (add_ast_fields() < 0) return 0;
815 mod_type = make_type("mod", &AST_type, NULL, 0);
816 if (!mod_type) return 0;
817 if (!add_attributes(mod_type, NULL, 0)) return 0;
818 Module_type = make_type("Module", mod_type, Module_fields, 1);
819 if (!Module_type) return 0;
820 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
821 1);
822 if (!Interactive_type) return 0;
823 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
824 if (!Expression_type) return 0;
825 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
826 if (!Suite_type) return 0;
827 stmt_type = make_type("stmt", &AST_type, NULL, 0);
828 if (!stmt_type) return 0;
829 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
830 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
831 5);
832 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400833 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
834 AsyncFunctionDef_fields, 5);
835 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400836 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200837 if (!ClassDef_type) return 0;
838 Return_type = make_type("Return", stmt_type, Return_fields, 1);
839 if (!Return_type) return 0;
840 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
841 if (!Delete_type) return 0;
842 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
843 if (!Assign_type) return 0;
844 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
845 if (!AugAssign_type) return 0;
846 For_type = make_type("For", stmt_type, For_fields, 4);
847 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400848 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
849 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200850 While_type = make_type("While", stmt_type, While_fields, 3);
851 if (!While_type) return 0;
852 If_type = make_type("If", stmt_type, If_fields, 3);
853 if (!If_type) return 0;
854 With_type = make_type("With", stmt_type, With_fields, 2);
855 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400856 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
857 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200858 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
859 if (!Raise_type) return 0;
860 Try_type = make_type("Try", stmt_type, Try_fields, 4);
861 if (!Try_type) return 0;
862 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
863 if (!Assert_type) return 0;
864 Import_type = make_type("Import", stmt_type, Import_fields, 1);
865 if (!Import_type) return 0;
866 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
867 if (!ImportFrom_type) return 0;
868 Global_type = make_type("Global", stmt_type, Global_fields, 1);
869 if (!Global_type) return 0;
870 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
871 if (!Nonlocal_type) return 0;
872 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
873 if (!Expr_type) return 0;
874 Pass_type = make_type("Pass", stmt_type, NULL, 0);
875 if (!Pass_type) return 0;
876 Break_type = make_type("Break", stmt_type, NULL, 0);
877 if (!Break_type) return 0;
878 Continue_type = make_type("Continue", stmt_type, NULL, 0);
879 if (!Continue_type) return 0;
880 expr_type = make_type("expr", &AST_type, NULL, 0);
881 if (!expr_type) return 0;
882 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
883 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
884 if (!BoolOp_type) return 0;
885 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
886 if (!BinOp_type) return 0;
887 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
888 if (!UnaryOp_type) return 0;
889 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
890 if (!Lambda_type) return 0;
891 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
892 if (!IfExp_type) return 0;
893 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
894 if (!Dict_type) return 0;
895 Set_type = make_type("Set", expr_type, Set_fields, 1);
896 if (!Set_type) return 0;
897 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
898 if (!ListComp_type) return 0;
899 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
900 if (!SetComp_type) return 0;
901 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
902 if (!DictComp_type) return 0;
903 GeneratorExp_type = make_type("GeneratorExp", expr_type,
904 GeneratorExp_fields, 2);
905 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400906 Await_type = make_type("Await", expr_type, Await_fields, 1);
907 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200908 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
909 if (!Yield_type) return 0;
910 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
911 if (!YieldFrom_type) return 0;
912 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
913 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400914 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200915 if (!Call_type) return 0;
916 Num_type = make_type("Num", expr_type, Num_fields, 1);
917 if (!Num_type) return 0;
918 Str_type = make_type("Str", expr_type, Str_fields, 1);
919 if (!Str_type) return 0;
920 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
921 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200922 NameConstant_type = make_type("NameConstant", expr_type,
923 NameConstant_fields, 1);
924 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200925 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
926 if (!Ellipsis_type) return 0;
927 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
928 if (!Attribute_type) return 0;
929 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
930 if (!Subscript_type) return 0;
931 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
932 if (!Starred_type) return 0;
933 Name_type = make_type("Name", expr_type, Name_fields, 2);
934 if (!Name_type) return 0;
935 List_type = make_type("List", expr_type, List_fields, 2);
936 if (!List_type) return 0;
937 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
938 if (!Tuple_type) return 0;
939 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
940 if (!expr_context_type) return 0;
941 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
942 Load_type = make_type("Load", expr_context_type, NULL, 0);
943 if (!Load_type) return 0;
944 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
945 if (!Load_singleton) return 0;
946 Store_type = make_type("Store", expr_context_type, NULL, 0);
947 if (!Store_type) return 0;
948 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
949 if (!Store_singleton) return 0;
950 Del_type = make_type("Del", expr_context_type, NULL, 0);
951 if (!Del_type) return 0;
952 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
953 if (!Del_singleton) return 0;
954 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
955 if (!AugLoad_type) return 0;
956 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
957 if (!AugLoad_singleton) return 0;
958 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
959 if (!AugStore_type) return 0;
960 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
961 if (!AugStore_singleton) return 0;
962 Param_type = make_type("Param", expr_context_type, NULL, 0);
963 if (!Param_type) return 0;
964 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
965 if (!Param_singleton) return 0;
966 slice_type = make_type("slice", &AST_type, NULL, 0);
967 if (!slice_type) return 0;
968 if (!add_attributes(slice_type, NULL, 0)) return 0;
969 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
970 if (!Slice_type) return 0;
971 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
972 if (!ExtSlice_type) return 0;
973 Index_type = make_type("Index", slice_type, Index_fields, 1);
974 if (!Index_type) return 0;
975 boolop_type = make_type("boolop", &AST_type, NULL, 0);
976 if (!boolop_type) return 0;
977 if (!add_attributes(boolop_type, NULL, 0)) return 0;
978 And_type = make_type("And", boolop_type, NULL, 0);
979 if (!And_type) return 0;
980 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
981 if (!And_singleton) return 0;
982 Or_type = make_type("Or", boolop_type, NULL, 0);
983 if (!Or_type) return 0;
984 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
985 if (!Or_singleton) return 0;
986 operator_type = make_type("operator", &AST_type, NULL, 0);
987 if (!operator_type) return 0;
988 if (!add_attributes(operator_type, NULL, 0)) return 0;
989 Add_type = make_type("Add", operator_type, NULL, 0);
990 if (!Add_type) return 0;
991 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
992 if (!Add_singleton) return 0;
993 Sub_type = make_type("Sub", operator_type, NULL, 0);
994 if (!Sub_type) return 0;
995 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
996 if (!Sub_singleton) return 0;
997 Mult_type = make_type("Mult", operator_type, NULL, 0);
998 if (!Mult_type) return 0;
999 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1000 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001001 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1002 if (!MatMult_type) return 0;
1003 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1004 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001005 Div_type = make_type("Div", operator_type, NULL, 0);
1006 if (!Div_type) return 0;
1007 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1008 if (!Div_singleton) return 0;
1009 Mod_type = make_type("Mod", operator_type, NULL, 0);
1010 if (!Mod_type) return 0;
1011 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1012 if (!Mod_singleton) return 0;
1013 Pow_type = make_type("Pow", operator_type, NULL, 0);
1014 if (!Pow_type) return 0;
1015 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1016 if (!Pow_singleton) return 0;
1017 LShift_type = make_type("LShift", operator_type, NULL, 0);
1018 if (!LShift_type) return 0;
1019 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1020 if (!LShift_singleton) return 0;
1021 RShift_type = make_type("RShift", operator_type, NULL, 0);
1022 if (!RShift_type) return 0;
1023 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1024 if (!RShift_singleton) return 0;
1025 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1026 if (!BitOr_type) return 0;
1027 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1028 if (!BitOr_singleton) return 0;
1029 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1030 if (!BitXor_type) return 0;
1031 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1032 if (!BitXor_singleton) return 0;
1033 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1034 if (!BitAnd_type) return 0;
1035 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1036 if (!BitAnd_singleton) return 0;
1037 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1038 if (!FloorDiv_type) return 0;
1039 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1040 if (!FloorDiv_singleton) return 0;
1041 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1042 if (!unaryop_type) return 0;
1043 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1044 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1045 if (!Invert_type) return 0;
1046 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1047 if (!Invert_singleton) return 0;
1048 Not_type = make_type("Not", unaryop_type, NULL, 0);
1049 if (!Not_type) return 0;
1050 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1051 if (!Not_singleton) return 0;
1052 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1053 if (!UAdd_type) return 0;
1054 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1055 if (!UAdd_singleton) return 0;
1056 USub_type = make_type("USub", unaryop_type, NULL, 0);
1057 if (!USub_type) return 0;
1058 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1059 if (!USub_singleton) return 0;
1060 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1061 if (!cmpop_type) return 0;
1062 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1063 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1064 if (!Eq_type) return 0;
1065 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1066 if (!Eq_singleton) return 0;
1067 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1068 if (!NotEq_type) return 0;
1069 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1070 if (!NotEq_singleton) return 0;
1071 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1072 if (!Lt_type) return 0;
1073 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1074 if (!Lt_singleton) return 0;
1075 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1076 if (!LtE_type) return 0;
1077 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1078 if (!LtE_singleton) return 0;
1079 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1080 if (!Gt_type) return 0;
1081 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1082 if (!Gt_singleton) return 0;
1083 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1084 if (!GtE_type) return 0;
1085 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1086 if (!GtE_singleton) return 0;
1087 Is_type = make_type("Is", cmpop_type, NULL, 0);
1088 if (!Is_type) return 0;
1089 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1090 if (!Is_singleton) return 0;
1091 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1092 if (!IsNot_type) return 0;
1093 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1094 if (!IsNot_singleton) return 0;
1095 In_type = make_type("In", cmpop_type, NULL, 0);
1096 if (!In_type) return 0;
1097 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1098 if (!In_singleton) return 0;
1099 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1100 if (!NotIn_type) return 0;
1101 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1102 if (!NotIn_singleton) return 0;
1103 comprehension_type = make_type("comprehension", &AST_type,
1104 comprehension_fields, 3);
1105 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001106 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001107 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1108 if (!excepthandler_type) return 0;
1109 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1110 return 0;
1111 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1112 ExceptHandler_fields, 3);
1113 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001114 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001115 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001116 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001117 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1118 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001119 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001120 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1121 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001122 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001123 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1124 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001125 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001126 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1127 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001128 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001129 initialized = 1;
1130 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001131}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001132
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001133static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1134static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1135static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1136static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1137 arena);
1138static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1139static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1140static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1141static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1142static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1143static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1144 arena);
1145static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1146 arena);
1147static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1148static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1149static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1150static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001151static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001154Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001156 mod_ty p;
1157 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1158 if (!p)
1159 return NULL;
1160 p->kind = Module_kind;
1161 p->v.Module.body = body;
1162 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163}
1164
1165mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001166Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001168 mod_ty p;
1169 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1170 if (!p)
1171 return NULL;
1172 p->kind = Interactive_kind;
1173 p->v.Interactive.body = body;
1174 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175}
1176
1177mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001178Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001180 mod_ty p;
1181 if (!body) {
1182 PyErr_SetString(PyExc_ValueError,
1183 "field body is required for Expression");
1184 return NULL;
1185 }
1186 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1187 if (!p)
1188 return NULL;
1189 p->kind = Expression_kind;
1190 p->v.Expression.body = body;
1191 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192}
1193
1194mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001197 mod_ty p;
1198 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1199 if (!p)
1200 return NULL;
1201 p->kind = Suite_kind;
1202 p->v.Suite.body = body;
1203 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204}
1205
1206stmt_ty
1207FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001208 decorator_list, expr_ty returns, int lineno, int col_offset,
1209 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001211 stmt_ty p;
1212 if (!name) {
1213 PyErr_SetString(PyExc_ValueError,
1214 "field name is required for FunctionDef");
1215 return NULL;
1216 }
1217 if (!args) {
1218 PyErr_SetString(PyExc_ValueError,
1219 "field args is required for FunctionDef");
1220 return NULL;
1221 }
1222 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1223 if (!p)
1224 return NULL;
1225 p->kind = FunctionDef_kind;
1226 p->v.FunctionDef.name = name;
1227 p->v.FunctionDef.args = args;
1228 p->v.FunctionDef.body = body;
1229 p->v.FunctionDef.decorator_list = decorator_list;
1230 p->v.FunctionDef.returns = returns;
1231 p->lineno = lineno;
1232 p->col_offset = col_offset;
1233 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234}
1235
1236stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001237AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1238 * decorator_list, expr_ty returns, int lineno, int col_offset,
1239 PyArena *arena)
1240{
1241 stmt_ty p;
1242 if (!name) {
1243 PyErr_SetString(PyExc_ValueError,
1244 "field name is required for AsyncFunctionDef");
1245 return NULL;
1246 }
1247 if (!args) {
1248 PyErr_SetString(PyExc_ValueError,
1249 "field args is required for AsyncFunctionDef");
1250 return NULL;
1251 }
1252 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1253 if (!p)
1254 return NULL;
1255 p->kind = AsyncFunctionDef_kind;
1256 p->v.AsyncFunctionDef.name = name;
1257 p->v.AsyncFunctionDef.args = args;
1258 p->v.AsyncFunctionDef.body = body;
1259 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1260 p->v.AsyncFunctionDef.returns = returns;
1261 p->lineno = lineno;
1262 p->col_offset = col_offset;
1263 return p;
1264}
1265
1266stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001267ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1268 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1269 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001271 stmt_ty p;
1272 if (!name) {
1273 PyErr_SetString(PyExc_ValueError,
1274 "field name is required for ClassDef");
1275 return NULL;
1276 }
1277 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1278 if (!p)
1279 return NULL;
1280 p->kind = ClassDef_kind;
1281 p->v.ClassDef.name = name;
1282 p->v.ClassDef.bases = bases;
1283 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001284 p->v.ClassDef.body = body;
1285 p->v.ClassDef.decorator_list = decorator_list;
1286 p->lineno = lineno;
1287 p->col_offset = col_offset;
1288 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001292Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001294 stmt_ty p;
1295 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1296 if (!p)
1297 return NULL;
1298 p->kind = Return_kind;
1299 p->v.Return.value = value;
1300 p->lineno = lineno;
1301 p->col_offset = col_offset;
1302 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303}
1304
1305stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001306Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001308 stmt_ty p;
1309 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1310 if (!p)
1311 return NULL;
1312 p->kind = Delete_kind;
1313 p->v.Delete.targets = targets;
1314 p->lineno = lineno;
1315 p->col_offset = col_offset;
1316 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
1319stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001320Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1321 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001323 stmt_ty p;
1324 if (!value) {
1325 PyErr_SetString(PyExc_ValueError,
1326 "field value is required for Assign");
1327 return NULL;
1328 }
1329 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1330 if (!p)
1331 return NULL;
1332 p->kind = Assign_kind;
1333 p->v.Assign.targets = targets;
1334 p->v.Assign.value = value;
1335 p->lineno = lineno;
1336 p->col_offset = col_offset;
1337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338}
1339
1340stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1342 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001344 stmt_ty p;
1345 if (!target) {
1346 PyErr_SetString(PyExc_ValueError,
1347 "field target is required for AugAssign");
1348 return NULL;
1349 }
1350 if (!op) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field op is required for AugAssign");
1353 return NULL;
1354 }
1355 if (!value) {
1356 PyErr_SetString(PyExc_ValueError,
1357 "field value is required for AugAssign");
1358 return NULL;
1359 }
1360 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1361 if (!p)
1362 return NULL;
1363 p->kind = AugAssign_kind;
1364 p->v.AugAssign.target = target;
1365 p->v.AugAssign.op = op;
1366 p->v.AugAssign.value = value;
1367 p->lineno = lineno;
1368 p->col_offset = col_offset;
1369 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370}
1371
1372stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001374 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001376 stmt_ty p;
1377 if (!target) {
1378 PyErr_SetString(PyExc_ValueError,
1379 "field target is required for For");
1380 return NULL;
1381 }
1382 if (!iter) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "field iter is required for For");
1385 return NULL;
1386 }
1387 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1388 if (!p)
1389 return NULL;
1390 p->kind = For_kind;
1391 p->v.For.target = target;
1392 p->v.For.iter = iter;
1393 p->v.For.body = body;
1394 p->v.For.orelse = orelse;
1395 p->lineno = lineno;
1396 p->col_offset = col_offset;
1397 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398}
1399
1400stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001401AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1402 lineno, int col_offset, PyArena *arena)
1403{
1404 stmt_ty p;
1405 if (!target) {
1406 PyErr_SetString(PyExc_ValueError,
1407 "field target is required for AsyncFor");
1408 return NULL;
1409 }
1410 if (!iter) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field iter is required for AsyncFor");
1413 return NULL;
1414 }
1415 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1416 if (!p)
1417 return NULL;
1418 p->kind = AsyncFor_kind;
1419 p->v.AsyncFor.target = target;
1420 p->v.AsyncFor.iter = iter;
1421 p->v.AsyncFor.body = body;
1422 p->v.AsyncFor.orelse = orelse;
1423 p->lineno = lineno;
1424 p->col_offset = col_offset;
1425 return p;
1426}
1427
1428stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001429While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1430 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001432 stmt_ty p;
1433 if (!test) {
1434 PyErr_SetString(PyExc_ValueError,
1435 "field test is required for While");
1436 return NULL;
1437 }
1438 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1439 if (!p)
1440 return NULL;
1441 p->kind = While_kind;
1442 p->v.While.test = test;
1443 p->v.While.body = body;
1444 p->v.While.orelse = orelse;
1445 p->lineno = lineno;
1446 p->col_offset = col_offset;
1447 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448}
1449
1450stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001451If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1452 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001454 stmt_ty p;
1455 if (!test) {
1456 PyErr_SetString(PyExc_ValueError,
1457 "field test is required for If");
1458 return NULL;
1459 }
1460 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1461 if (!p)
1462 return NULL;
1463 p->kind = If_kind;
1464 p->v.If.test = test;
1465 p->v.If.body = body;
1466 p->v.If.orelse = orelse;
1467 p->lineno = lineno;
1468 p->col_offset = col_offset;
1469 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470}
1471
1472stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001473With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1474 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001475{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001476 stmt_ty p;
1477 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1478 if (!p)
1479 return NULL;
1480 p->kind = With_kind;
1481 p->v.With.items = items;
1482 p->v.With.body = body;
1483 p->lineno = lineno;
1484 p->col_offset = col_offset;
1485 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001486}
1487
1488stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001489AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1490 PyArena *arena)
1491{
1492 stmt_ty p;
1493 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1494 if (!p)
1495 return NULL;
1496 p->kind = AsyncWith_kind;
1497 p->v.AsyncWith.items = items;
1498 p->v.AsyncWith.body = body;
1499 p->lineno = lineno;
1500 p->col_offset = col_offset;
1501 return p;
1502}
1503
1504stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001505Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001507 stmt_ty p;
1508 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1509 if (!p)
1510 return NULL;
1511 p->kind = Raise_kind;
1512 p->v.Raise.exc = exc;
1513 p->v.Raise.cause = cause;
1514 p->lineno = lineno;
1515 p->col_offset = col_offset;
1516 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517}
1518
1519stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001520Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1521 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001523 stmt_ty p;
1524 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1525 if (!p)
1526 return NULL;
1527 p->kind = Try_kind;
1528 p->v.Try.body = body;
1529 p->v.Try.handlers = handlers;
1530 p->v.Try.orelse = orelse;
1531 p->v.Try.finalbody = finalbody;
1532 p->lineno = lineno;
1533 p->col_offset = col_offset;
1534 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
1537stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001538Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001540 stmt_ty p;
1541 if (!test) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "field test is required for Assert");
1544 return NULL;
1545 }
1546 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1547 if (!p)
1548 return NULL;
1549 p->kind = Assert_kind;
1550 p->v.Assert.test = test;
1551 p->v.Assert.msg = msg;
1552 p->lineno = lineno;
1553 p->col_offset = col_offset;
1554 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001560 stmt_ty p;
1561 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1562 if (!p)
1563 return NULL;
1564 p->kind = Import_kind;
1565 p->v.Import.names = names;
1566 p->lineno = lineno;
1567 p->col_offset = col_offset;
1568 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
1571stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001572ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1573 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001575 stmt_ty p;
1576 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1577 if (!p)
1578 return NULL;
1579 p->kind = ImportFrom_kind;
1580 p->v.ImportFrom.module = module;
1581 p->v.ImportFrom.names = names;
1582 p->v.ImportFrom.level = level;
1583 p->lineno = lineno;
1584 p->col_offset = col_offset;
1585 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
1588stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001591 stmt_ty p;
1592 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1593 if (!p)
1594 return NULL;
1595 p->kind = Global_kind;
1596 p->v.Global.names = names;
1597 p->lineno = lineno;
1598 p->col_offset = col_offset;
1599 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001603Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1604{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001605 stmt_ty p;
1606 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1607 if (!p)
1608 return NULL;
1609 p->kind = Nonlocal_kind;
1610 p->v.Nonlocal.names = names;
1611 p->lineno = lineno;
1612 p->col_offset = col_offset;
1613 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001614}
1615
1616stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001617Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001619 stmt_ty p;
1620 if (!value) {
1621 PyErr_SetString(PyExc_ValueError,
1622 "field value is required for Expr");
1623 return NULL;
1624 }
1625 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1626 if (!p)
1627 return NULL;
1628 p->kind = Expr_kind;
1629 p->v.Expr.value = value;
1630 p->lineno = lineno;
1631 p->col_offset = col_offset;
1632 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633}
1634
1635stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001636Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001638 stmt_ty p;
1639 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1640 if (!p)
1641 return NULL;
1642 p->kind = Pass_kind;
1643 p->lineno = lineno;
1644 p->col_offset = col_offset;
1645 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001651 stmt_ty p;
1652 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1653 if (!p)
1654 return NULL;
1655 p->kind = Break_kind;
1656 p->lineno = lineno;
1657 p->col_offset = col_offset;
1658 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
1661stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001662Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001664 stmt_ty p;
1665 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1666 if (!p)
1667 return NULL;
1668 p->kind = Continue_kind;
1669 p->lineno = lineno;
1670 p->col_offset = col_offset;
1671 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672}
1673
1674expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001675BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1676 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001678 expr_ty p;
1679 if (!op) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field op is required for BoolOp");
1682 return NULL;
1683 }
1684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1685 if (!p)
1686 return NULL;
1687 p->kind = BoolOp_kind;
1688 p->v.BoolOp.op = op;
1689 p->v.BoolOp.values = values;
1690 p->lineno = lineno;
1691 p->col_offset = col_offset;
1692 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693}
1694
1695expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1697 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001699 expr_ty p;
1700 if (!left) {
1701 PyErr_SetString(PyExc_ValueError,
1702 "field left is required for BinOp");
1703 return NULL;
1704 }
1705 if (!op) {
1706 PyErr_SetString(PyExc_ValueError,
1707 "field op is required for BinOp");
1708 return NULL;
1709 }
1710 if (!right) {
1711 PyErr_SetString(PyExc_ValueError,
1712 "field right is required for BinOp");
1713 return NULL;
1714 }
1715 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1716 if (!p)
1717 return NULL;
1718 p->kind = BinOp_kind;
1719 p->v.BinOp.left = left;
1720 p->v.BinOp.op = op;
1721 p->v.BinOp.right = right;
1722 p->lineno = lineno;
1723 p->col_offset = col_offset;
1724 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
1727expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1729 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001731 expr_ty p;
1732 if (!op) {
1733 PyErr_SetString(PyExc_ValueError,
1734 "field op is required for UnaryOp");
1735 return NULL;
1736 }
1737 if (!operand) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "field operand is required for UnaryOp");
1740 return NULL;
1741 }
1742 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1743 if (!p)
1744 return NULL;
1745 p->kind = UnaryOp_kind;
1746 p->v.UnaryOp.op = op;
1747 p->v.UnaryOp.operand = operand;
1748 p->lineno = lineno;
1749 p->col_offset = col_offset;
1750 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751}
1752
1753expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001754Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1755 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001757 expr_ty p;
1758 if (!args) {
1759 PyErr_SetString(PyExc_ValueError,
1760 "field args is required for Lambda");
1761 return NULL;
1762 }
1763 if (!body) {
1764 PyErr_SetString(PyExc_ValueError,
1765 "field body is required for Lambda");
1766 return NULL;
1767 }
1768 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1769 if (!p)
1770 return NULL;
1771 p->kind = Lambda_kind;
1772 p->v.Lambda.args = args;
1773 p->v.Lambda.body = body;
1774 p->lineno = lineno;
1775 p->col_offset = col_offset;
1776 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777}
1778
1779expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001780IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1781 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001782{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001783 expr_ty p;
1784 if (!test) {
1785 PyErr_SetString(PyExc_ValueError,
1786 "field test is required for IfExp");
1787 return NULL;
1788 }
1789 if (!body) {
1790 PyErr_SetString(PyExc_ValueError,
1791 "field body is required for IfExp");
1792 return NULL;
1793 }
1794 if (!orelse) {
1795 PyErr_SetString(PyExc_ValueError,
1796 "field orelse is required for IfExp");
1797 return NULL;
1798 }
1799 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1800 if (!p)
1801 return NULL;
1802 p->kind = IfExp_kind;
1803 p->v.IfExp.test = test;
1804 p->v.IfExp.body = body;
1805 p->v.IfExp.orelse = orelse;
1806 p->lineno = lineno;
1807 p->col_offset = col_offset;
1808 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001809}
1810
1811expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001812Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1813 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001815 expr_ty p;
1816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1817 if (!p)
1818 return NULL;
1819 p->kind = Dict_kind;
1820 p->v.Dict.keys = keys;
1821 p->v.Dict.values = values;
1822 p->lineno = lineno;
1823 p->col_offset = col_offset;
1824 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825}
1826
1827expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001828Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1829{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001830 expr_ty p;
1831 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1832 if (!p)
1833 return NULL;
1834 p->kind = Set_kind;
1835 p->v.Set.elts = elts;
1836 p->lineno = lineno;
1837 p->col_offset = col_offset;
1838 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001839}
1840
1841expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001842ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1843 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001845 expr_ty p;
1846 if (!elt) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field elt is required for ListComp");
1849 return NULL;
1850 }
1851 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1852 if (!p)
1853 return NULL;
1854 p->kind = ListComp_kind;
1855 p->v.ListComp.elt = elt;
1856 p->v.ListComp.generators = generators;
1857 p->lineno = lineno;
1858 p->col_offset = col_offset;
1859 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860}
1861
1862expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001863SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1864 *arena)
1865{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001866 expr_ty p;
1867 if (!elt) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field elt is required for SetComp");
1870 return NULL;
1871 }
1872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1873 if (!p)
1874 return NULL;
1875 p->kind = SetComp_kind;
1876 p->v.SetComp.elt = elt;
1877 p->v.SetComp.generators = generators;
1878 p->lineno = lineno;
1879 p->col_offset = col_offset;
1880 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001881}
1882
1883expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001884DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1885 col_offset, PyArena *arena)
1886{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001887 expr_ty p;
1888 if (!key) {
1889 PyErr_SetString(PyExc_ValueError,
1890 "field key is required for DictComp");
1891 return NULL;
1892 }
1893 if (!value) {
1894 PyErr_SetString(PyExc_ValueError,
1895 "field value is required for DictComp");
1896 return NULL;
1897 }
1898 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1899 if (!p)
1900 return NULL;
1901 p->kind = DictComp_kind;
1902 p->v.DictComp.key = key;
1903 p->v.DictComp.value = value;
1904 p->v.DictComp.generators = generators;
1905 p->lineno = lineno;
1906 p->col_offset = col_offset;
1907 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908}
1909
1910expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001911GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1912 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001914 expr_ty p;
1915 if (!elt) {
1916 PyErr_SetString(PyExc_ValueError,
1917 "field elt is required for GeneratorExp");
1918 return NULL;
1919 }
1920 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1921 if (!p)
1922 return NULL;
1923 p->kind = GeneratorExp_kind;
1924 p->v.GeneratorExp.elt = elt;
1925 p->v.GeneratorExp.generators = generators;
1926 p->lineno = lineno;
1927 p->col_offset = col_offset;
1928 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
1931expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001932Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
1933{
1934 expr_ty p;
1935 if (!value) {
1936 PyErr_SetString(PyExc_ValueError,
1937 "field value is required for Await");
1938 return NULL;
1939 }
1940 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1941 if (!p)
1942 return NULL;
1943 p->kind = Await_kind;
1944 p->v.Await.value = value;
1945 p->lineno = lineno;
1946 p->col_offset = col_offset;
1947 return p;
1948}
1949
1950expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001951Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001953 expr_ty p;
1954 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1955 if (!p)
1956 return NULL;
1957 p->kind = Yield_kind;
1958 p->v.Yield.value = value;
1959 p->lineno = lineno;
1960 p->col_offset = col_offset;
1961 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962}
1963
1964expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001965YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1966{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001967 expr_ty p;
1968 if (!value) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field value is required for YieldFrom");
1971 return NULL;
1972 }
1973 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1974 if (!p)
1975 return NULL;
1976 p->kind = YieldFrom_kind;
1977 p->v.YieldFrom.value = value;
1978 p->lineno = lineno;
1979 p->col_offset = col_offset;
1980 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001981}
1982
1983expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001984Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1985 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001987 expr_ty p;
1988 if (!left) {
1989 PyErr_SetString(PyExc_ValueError,
1990 "field left is required for Compare");
1991 return NULL;
1992 }
1993 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1994 if (!p)
1995 return NULL;
1996 p->kind = Compare_kind;
1997 p->v.Compare.left = left;
1998 p->v.Compare.ops = ops;
1999 p->v.Compare.comparators = comparators;
2000 p->lineno = lineno;
2001 p->col_offset = col_offset;
2002 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003}
2004
2005expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2007 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002009 expr_ty p;
2010 if (!func) {
2011 PyErr_SetString(PyExc_ValueError,
2012 "field func is required for Call");
2013 return NULL;
2014 }
2015 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2016 if (!p)
2017 return NULL;
2018 p->kind = Call_kind;
2019 p->v.Call.func = func;
2020 p->v.Call.args = args;
2021 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002022 p->lineno = lineno;
2023 p->col_offset = col_offset;
2024 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
2027expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002028Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002030 expr_ty p;
2031 if (!n) {
2032 PyErr_SetString(PyExc_ValueError,
2033 "field n is required for Num");
2034 return NULL;
2035 }
2036 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2037 if (!p)
2038 return NULL;
2039 p->kind = Num_kind;
2040 p->v.Num.n = n;
2041 p->lineno = lineno;
2042 p->col_offset = col_offset;
2043 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044}
2045
2046expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002047Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002049 expr_ty p;
2050 if (!s) {
2051 PyErr_SetString(PyExc_ValueError,
2052 "field s is required for Str");
2053 return NULL;
2054 }
2055 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2056 if (!p)
2057 return NULL;
2058 p->kind = Str_kind;
2059 p->v.Str.s = s;
2060 p->lineno = lineno;
2061 p->col_offset = col_offset;
2062 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063}
2064
2065expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002066Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002067{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002068 expr_ty p;
2069 if (!s) {
2070 PyErr_SetString(PyExc_ValueError,
2071 "field s is required for Bytes");
2072 return NULL;
2073 }
2074 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2075 if (!p)
2076 return NULL;
2077 p->kind = Bytes_kind;
2078 p->v.Bytes.s = s;
2079 p->lineno = lineno;
2080 p->col_offset = col_offset;
2081 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002082}
2083
2084expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002085NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2086{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002087 expr_ty p;
2088 if (!value) {
2089 PyErr_SetString(PyExc_ValueError,
2090 "field value is required for NameConstant");
2091 return NULL;
2092 }
2093 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2094 if (!p)
2095 return NULL;
2096 p->kind = NameConstant_kind;
2097 p->v.NameConstant.value = value;
2098 p->lineno = lineno;
2099 p->col_offset = col_offset;
2100 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002101}
2102
2103expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002104Ellipsis(int lineno, int col_offset, PyArena *arena)
2105{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002106 expr_ty p;
2107 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2108 if (!p)
2109 return NULL;
2110 p->kind = Ellipsis_kind;
2111 p->lineno = lineno;
2112 p->col_offset = col_offset;
2113 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002114}
2115
2116expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002117Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2118 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002120 expr_ty p;
2121 if (!value) {
2122 PyErr_SetString(PyExc_ValueError,
2123 "field value is required for Attribute");
2124 return NULL;
2125 }
2126 if (!attr) {
2127 PyErr_SetString(PyExc_ValueError,
2128 "field attr is required for Attribute");
2129 return NULL;
2130 }
2131 if (!ctx) {
2132 PyErr_SetString(PyExc_ValueError,
2133 "field ctx is required for Attribute");
2134 return NULL;
2135 }
2136 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2137 if (!p)
2138 return NULL;
2139 p->kind = Attribute_kind;
2140 p->v.Attribute.value = value;
2141 p->v.Attribute.attr = attr;
2142 p->v.Attribute.ctx = ctx;
2143 p->lineno = lineno;
2144 p->col_offset = col_offset;
2145 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146}
2147
2148expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002149Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2150 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002152 expr_ty p;
2153 if (!value) {
2154 PyErr_SetString(PyExc_ValueError,
2155 "field value is required for Subscript");
2156 return NULL;
2157 }
2158 if (!slice) {
2159 PyErr_SetString(PyExc_ValueError,
2160 "field slice is required for Subscript");
2161 return NULL;
2162 }
2163 if (!ctx) {
2164 PyErr_SetString(PyExc_ValueError,
2165 "field ctx is required for Subscript");
2166 return NULL;
2167 }
2168 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2169 if (!p)
2170 return NULL;
2171 p->kind = Subscript_kind;
2172 p->v.Subscript.value = value;
2173 p->v.Subscript.slice = slice;
2174 p->v.Subscript.ctx = ctx;
2175 p->lineno = lineno;
2176 p->col_offset = col_offset;
2177 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178}
2179
2180expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002181Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2182 *arena)
2183{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002184 expr_ty p;
2185 if (!value) {
2186 PyErr_SetString(PyExc_ValueError,
2187 "field value is required for Starred");
2188 return NULL;
2189 }
2190 if (!ctx) {
2191 PyErr_SetString(PyExc_ValueError,
2192 "field ctx is required for Starred");
2193 return NULL;
2194 }
2195 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2196 if (!p)
2197 return NULL;
2198 p->kind = Starred_kind;
2199 p->v.Starred.value = value;
2200 p->v.Starred.ctx = ctx;
2201 p->lineno = lineno;
2202 p->col_offset = col_offset;
2203 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002204}
2205
2206expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002207Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2208 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002210 expr_ty p;
2211 if (!id) {
2212 PyErr_SetString(PyExc_ValueError,
2213 "field id is required for Name");
2214 return NULL;
2215 }
2216 if (!ctx) {
2217 PyErr_SetString(PyExc_ValueError,
2218 "field ctx is required for Name");
2219 return NULL;
2220 }
2221 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2222 if (!p)
2223 return NULL;
2224 p->kind = Name_kind;
2225 p->v.Name.id = id;
2226 p->v.Name.ctx = ctx;
2227 p->lineno = lineno;
2228 p->col_offset = col_offset;
2229 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
2232expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002233List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2234 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002236 expr_ty p;
2237 if (!ctx) {
2238 PyErr_SetString(PyExc_ValueError,
2239 "field ctx is required for List");
2240 return NULL;
2241 }
2242 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2243 if (!p)
2244 return NULL;
2245 p->kind = List_kind;
2246 p->v.List.elts = elts;
2247 p->v.List.ctx = ctx;
2248 p->lineno = lineno;
2249 p->col_offset = col_offset;
2250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
2253expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002254Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2255 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002257 expr_ty p;
2258 if (!ctx) {
2259 PyErr_SetString(PyExc_ValueError,
2260 "field ctx is required for Tuple");
2261 return NULL;
2262 }
2263 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2264 if (!p)
2265 return NULL;
2266 p->kind = Tuple_kind;
2267 p->v.Tuple.elts = elts;
2268 p->v.Tuple.ctx = ctx;
2269 p->lineno = lineno;
2270 p->col_offset = col_offset;
2271 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272}
2273
2274slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002275Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002277 slice_ty p;
2278 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2279 if (!p)
2280 return NULL;
2281 p->kind = Slice_kind;
2282 p->v.Slice.lower = lower;
2283 p->v.Slice.upper = upper;
2284 p->v.Slice.step = step;
2285 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286}
2287
2288slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002289ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002291 slice_ty p;
2292 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2293 if (!p)
2294 return NULL;
2295 p->kind = ExtSlice_kind;
2296 p->v.ExtSlice.dims = dims;
2297 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298}
2299
2300slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002301Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002303 slice_ty p;
2304 if (!value) {
2305 PyErr_SetString(PyExc_ValueError,
2306 "field value is required for Index");
2307 return NULL;
2308 }
2309 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2310 if (!p)
2311 return NULL;
2312 p->kind = Index_kind;
2313 p->v.Index.value = value;
2314 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315}
2316
2317comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002318comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002320 comprehension_ty p;
2321 if (!target) {
2322 PyErr_SetString(PyExc_ValueError,
2323 "field target is required for comprehension");
2324 return NULL;
2325 }
2326 if (!iter) {
2327 PyErr_SetString(PyExc_ValueError,
2328 "field iter is required for comprehension");
2329 return NULL;
2330 }
2331 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2332 if (!p)
2333 return NULL;
2334 p->target = target;
2335 p->iter = iter;
2336 p->ifs = ifs;
2337 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338}
2339
2340excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002341ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002344 excepthandler_ty p;
2345 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2346 if (!p)
2347 return NULL;
2348 p->kind = ExceptHandler_kind;
2349 p->v.ExceptHandler.type = type;
2350 p->v.ExceptHandler.name = name;
2351 p->v.ExceptHandler.body = body;
2352 p->lineno = lineno;
2353 p->col_offset = col_offset;
2354 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355}
2356
2357arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002358arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2359 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002361 arguments_ty p;
2362 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2363 if (!p)
2364 return NULL;
2365 p->args = args;
2366 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002367 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002368 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002369 p->kwarg = kwarg;
2370 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002371 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372}
2373
Neal Norwitzc1505362006-12-28 06:47:50 +00002374arg_ty
Victor Stinnerf9827ea2015-11-06 17:01:48 +01002375arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2376 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002377{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002378 arg_ty p;
2379 if (!arg) {
2380 PyErr_SetString(PyExc_ValueError,
2381 "field arg is required for arg");
2382 return NULL;
2383 }
2384 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2385 if (!p)
2386 return NULL;
2387 p->arg = arg;
2388 p->annotation = annotation;
Victor Stinnerf9827ea2015-11-06 17:01:48 +01002389 p->lineno = lineno;
2390 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002391 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002392}
2393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002395keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002397 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002398 if (!value) {
2399 PyErr_SetString(PyExc_ValueError,
2400 "field value is required for keyword");
2401 return NULL;
2402 }
2403 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2404 if (!p)
2405 return NULL;
2406 p->arg = arg;
2407 p->value = value;
2408 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409}
2410
2411alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002412alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002414 alias_ty p;
2415 if (!name) {
2416 PyErr_SetString(PyExc_ValueError,
2417 "field name is required for alias");
2418 return NULL;
2419 }
2420 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2421 if (!p)
2422 return NULL;
2423 p->name = name;
2424 p->asname = asname;
2425 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426}
2427
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002428withitem_ty
2429withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2430{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002431 withitem_ty p;
2432 if (!context_expr) {
2433 PyErr_SetString(PyExc_ValueError,
2434 "field context_expr is required for withitem");
2435 return NULL;
2436 }
2437 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2438 if (!p)
2439 return NULL;
2440 p->context_expr = context_expr;
2441 p->optional_vars = optional_vars;
2442 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002443}
2444
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002445
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002446PyObject*
2447ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002449 mod_ty o = (mod_ty)_o;
2450 PyObject *result = NULL, *value = NULL;
2451 if (!o) {
2452 Py_INCREF(Py_None);
2453 return Py_None;
2454 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002455
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002456 switch (o->kind) {
2457 case Module_kind:
2458 result = PyType_GenericNew(Module_type, NULL, NULL);
2459 if (!result) goto failed;
2460 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2461 if (!value) goto failed;
2462 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
2465 break;
2466 case Interactive_kind:
2467 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2468 if (!result) goto failed;
2469 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2470 if (!value) goto failed;
2471 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
2474 break;
2475 case Expression_kind:
2476 result = PyType_GenericNew(Expression_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_expr(o->v.Expression.body);
2479 if (!value) goto failed;
2480 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 break;
2484 case Suite_kind:
2485 result = PyType_GenericNew(Suite_type, NULL, NULL);
2486 if (!result) goto failed;
2487 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2488 if (!value) goto failed;
2489 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 break;
2493 }
2494 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002495failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002496 Py_XDECREF(value);
2497 Py_XDECREF(result);
2498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499}
2500
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002501PyObject*
2502ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002504 stmt_ty o = (stmt_ty)_o;
2505 PyObject *result = NULL, *value = NULL;
2506 if (!o) {
2507 Py_INCREF(Py_None);
2508 return Py_None;
2509 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002510
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002511 switch (o->kind) {
2512 case FunctionDef_kind:
2513 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2514 if (!result) goto failed;
2515 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002516 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002517 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2518 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002519 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002520 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002521 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002522 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2523 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002524 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002525 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2526 if (!value) goto failed;
2527 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2531 if (!value) goto failed;
2532 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 value = ast2obj_expr(o->v.FunctionDef.returns);
2536 if (!value) goto failed;
2537 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2538 goto failed;
2539 Py_DECREF(value);
2540 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002541 case AsyncFunctionDef_kind:
2542 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2543 if (!result) goto failed;
2544 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2545 if (!value) goto failed;
2546 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2550 if (!value) goto failed;
2551 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2555 if (!value) goto failed;
2556 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2557 goto failed;
2558 Py_DECREF(value);
2559 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2560 ast2obj_expr);
2561 if (!value) goto failed;
2562 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2563 goto failed;
2564 Py_DECREF(value);
2565 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2566 if (!value) goto failed;
2567 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2568 goto failed;
2569 Py_DECREF(value);
2570 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002571 case ClassDef_kind:
2572 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2573 if (!result) goto failed;
2574 value = ast2obj_identifier(o->v.ClassDef.name);
2575 if (!value) goto failed;
2576 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2577 goto failed;
2578 Py_DECREF(value);
2579 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2580 if (!value) goto failed;
2581 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2582 goto failed;
2583 Py_DECREF(value);
2584 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2585 if (!value) goto failed;
2586 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002589 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2590 if (!value) goto failed;
2591 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 break;
2600 case Return_kind:
2601 result = PyType_GenericNew(Return_type, NULL, NULL);
2602 if (!result) goto failed;
2603 value = ast2obj_expr(o->v.Return.value);
2604 if (!value) goto failed;
2605 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 break;
2609 case Delete_kind:
2610 result = PyType_GenericNew(Delete_type, NULL, NULL);
2611 if (!result) goto failed;
2612 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2613 if (!value) goto failed;
2614 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
2617 break;
2618 case Assign_kind:
2619 result = PyType_GenericNew(Assign_type, NULL, NULL);
2620 if (!result) goto failed;
2621 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2622 if (!value) goto failed;
2623 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
2626 value = ast2obj_expr(o->v.Assign.value);
2627 if (!value) goto failed;
2628 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
2631 break;
2632 case AugAssign_kind:
2633 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2634 if (!result) goto failed;
2635 value = ast2obj_expr(o->v.AugAssign.target);
2636 if (!value) goto failed;
2637 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2638 goto failed;
2639 Py_DECREF(value);
2640 value = ast2obj_operator(o->v.AugAssign.op);
2641 if (!value) goto failed;
2642 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
2645 value = ast2obj_expr(o->v.AugAssign.value);
2646 if (!value) goto failed;
2647 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2648 goto failed;
2649 Py_DECREF(value);
2650 break;
2651 case For_kind:
2652 result = PyType_GenericNew(For_type, NULL, NULL);
2653 if (!result) goto failed;
2654 value = ast2obj_expr(o->v.For.target);
2655 if (!value) goto failed;
2656 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2657 goto failed;
2658 Py_DECREF(value);
2659 value = ast2obj_expr(o->v.For.iter);
2660 if (!value) goto failed;
2661 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2665 if (!value) goto failed;
2666 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2670 if (!value) goto failed;
2671 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2672 goto failed;
2673 Py_DECREF(value);
2674 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002675 case AsyncFor_kind:
2676 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2677 if (!result) goto failed;
2678 value = ast2obj_expr(o->v.AsyncFor.target);
2679 if (!value) goto failed;
2680 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_expr(o->v.AsyncFor.iter);
2684 if (!value) goto failed;
2685 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
2688 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2694 if (!value) goto failed;
2695 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2696 goto failed;
2697 Py_DECREF(value);
2698 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002699 case While_kind:
2700 result = PyType_GenericNew(While_type, NULL, NULL);
2701 if (!result) goto failed;
2702 value = ast2obj_expr(o->v.While.test);
2703 if (!value) goto failed;
2704 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
2707 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2708 if (!value) goto failed;
2709 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2710 goto failed;
2711 Py_DECREF(value);
2712 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2713 if (!value) goto failed;
2714 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 break;
2718 case If_kind:
2719 result = PyType_GenericNew(If_type, NULL, NULL);
2720 if (!result) goto failed;
2721 value = ast2obj_expr(o->v.If.test);
2722 if (!value) goto failed;
2723 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2727 if (!value) goto failed;
2728 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
2731 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2732 if (!value) goto failed;
2733 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 break;
2737 case With_kind:
2738 result = PyType_GenericNew(With_type, NULL, NULL);
2739 if (!result) goto failed;
2740 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2741 if (!value) goto failed;
2742 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2743 goto failed;
2744 Py_DECREF(value);
2745 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2746 if (!value) goto failed;
2747 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
2750 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002751 case AsyncWith_kind:
2752 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2753 if (!result) goto failed;
2754 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2755 if (!value) goto failed;
2756 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
2759 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2760 if (!value) goto failed;
2761 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002765 case Raise_kind:
2766 result = PyType_GenericNew(Raise_type, NULL, NULL);
2767 if (!result) goto failed;
2768 value = ast2obj_expr(o->v.Raise.exc);
2769 if (!value) goto failed;
2770 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 value = ast2obj_expr(o->v.Raise.cause);
2774 if (!value) goto failed;
2775 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 break;
2779 case Try_kind:
2780 result = PyType_GenericNew(Try_type, NULL, NULL);
2781 if (!result) goto failed;
2782 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2783 if (!value) goto failed;
2784 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2785 goto failed;
2786 Py_DECREF(value);
2787 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2788 if (!value) goto failed;
2789 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2793 if (!value) goto failed;
2794 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
2797 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2798 if (!value) goto failed;
2799 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
2802 break;
2803 case Assert_kind:
2804 result = PyType_GenericNew(Assert_type, NULL, NULL);
2805 if (!result) goto failed;
2806 value = ast2obj_expr(o->v.Assert.test);
2807 if (!value) goto failed;
2808 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2809 goto failed;
2810 Py_DECREF(value);
2811 value = ast2obj_expr(o->v.Assert.msg);
2812 if (!value) goto failed;
2813 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2814 goto failed;
2815 Py_DECREF(value);
2816 break;
2817 case Import_kind:
2818 result = PyType_GenericNew(Import_type, NULL, NULL);
2819 if (!result) goto failed;
2820 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2821 if (!value) goto failed;
2822 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
2825 break;
2826 case ImportFrom_kind:
2827 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2828 if (!result) goto failed;
2829 value = ast2obj_identifier(o->v.ImportFrom.module);
2830 if (!value) goto failed;
2831 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2835 if (!value) goto failed;
2836 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2837 goto failed;
2838 Py_DECREF(value);
2839 value = ast2obj_int(o->v.ImportFrom.level);
2840 if (!value) goto failed;
2841 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2842 goto failed;
2843 Py_DECREF(value);
2844 break;
2845 case Global_kind:
2846 result = PyType_GenericNew(Global_type, NULL, NULL);
2847 if (!result) goto failed;
2848 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2849 if (!value) goto failed;
2850 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 break;
2854 case Nonlocal_kind:
2855 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2856 if (!result) goto failed;
2857 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2858 if (!value) goto failed;
2859 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 break;
2863 case Expr_kind:
2864 result = PyType_GenericNew(Expr_type, NULL, NULL);
2865 if (!result) goto failed;
2866 value = ast2obj_expr(o->v.Expr.value);
2867 if (!value) goto failed;
2868 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 break;
2872 case Pass_kind:
2873 result = PyType_GenericNew(Pass_type, NULL, NULL);
2874 if (!result) goto failed;
2875 break;
2876 case Break_kind:
2877 result = PyType_GenericNew(Break_type, NULL, NULL);
2878 if (!result) goto failed;
2879 break;
2880 case Continue_kind:
2881 result = PyType_GenericNew(Continue_type, NULL, NULL);
2882 if (!result) goto failed;
2883 break;
2884 }
2885 value = ast2obj_int(o->lineno);
2886 if (!value) goto failed;
2887 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_int(o->col_offset);
2891 if (!value) goto failed;
2892 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2893 goto failed;
2894 Py_DECREF(value);
2895 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002896failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002897 Py_XDECREF(value);
2898 Py_XDECREF(result);
2899 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
2901
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002902PyObject*
2903ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002905 expr_ty o = (expr_ty)_o;
2906 PyObject *result = NULL, *value = NULL;
2907 if (!o) {
2908 Py_INCREF(Py_None);
2909 return Py_None;
2910 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002911
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002912 switch (o->kind) {
2913 case BoolOp_kind:
2914 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2915 if (!result) goto failed;
2916 value = ast2obj_boolop(o->v.BoolOp.op);
2917 if (!value) goto failed;
2918 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
2921 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2922 if (!value) goto failed;
2923 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
2926 break;
2927 case BinOp_kind:
2928 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2929 if (!result) goto failed;
2930 value = ast2obj_expr(o->v.BinOp.left);
2931 if (!value) goto failed;
2932 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2933 goto failed;
2934 Py_DECREF(value);
2935 value = ast2obj_operator(o->v.BinOp.op);
2936 if (!value) goto failed;
2937 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2938 goto failed;
2939 Py_DECREF(value);
2940 value = ast2obj_expr(o->v.BinOp.right);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 break;
2946 case UnaryOp_kind:
2947 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2948 if (!result) goto failed;
2949 value = ast2obj_unaryop(o->v.UnaryOp.op);
2950 if (!value) goto failed;
2951 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2952 goto failed;
2953 Py_DECREF(value);
2954 value = ast2obj_expr(o->v.UnaryOp.operand);
2955 if (!value) goto failed;
2956 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
2957 goto failed;
2958 Py_DECREF(value);
2959 break;
2960 case Lambda_kind:
2961 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2962 if (!result) goto failed;
2963 value = ast2obj_arguments(o->v.Lambda.args);
2964 if (!value) goto failed;
2965 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2966 goto failed;
2967 Py_DECREF(value);
2968 value = ast2obj_expr(o->v.Lambda.body);
2969 if (!value) goto failed;
2970 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 break;
2974 case IfExp_kind:
2975 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2976 if (!result) goto failed;
2977 value = ast2obj_expr(o->v.IfExp.test);
2978 if (!value) goto failed;
2979 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2980 goto failed;
2981 Py_DECREF(value);
2982 value = ast2obj_expr(o->v.IfExp.body);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 value = ast2obj_expr(o->v.IfExp.orelse);
2988 if (!value) goto failed;
2989 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
2992 break;
2993 case Dict_kind:
2994 result = PyType_GenericNew(Dict_type, NULL, NULL);
2995 if (!result) goto failed;
2996 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2997 if (!value) goto failed;
2998 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
3001 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3002 if (!value) goto failed;
3003 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3004 goto failed;
3005 Py_DECREF(value);
3006 break;
3007 case Set_kind:
3008 result = PyType_GenericNew(Set_type, NULL, NULL);
3009 if (!result) goto failed;
3010 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3011 if (!value) goto failed;
3012 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3013 goto failed;
3014 Py_DECREF(value);
3015 break;
3016 case ListComp_kind:
3017 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3018 if (!result) goto failed;
3019 value = ast2obj_expr(o->v.ListComp.elt);
3020 if (!value) goto failed;
3021 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3022 goto failed;
3023 Py_DECREF(value);
3024 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3025 if (!value) goto failed;
3026 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3027 goto failed;
3028 Py_DECREF(value);
3029 break;
3030 case SetComp_kind:
3031 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3032 if (!result) goto failed;
3033 value = ast2obj_expr(o->v.SetComp.elt);
3034 if (!value) goto failed;
3035 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3036 goto failed;
3037 Py_DECREF(value);
3038 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3039 if (!value) goto failed;
3040 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3041 goto failed;
3042 Py_DECREF(value);
3043 break;
3044 case DictComp_kind:
3045 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3046 if (!result) goto failed;
3047 value = ast2obj_expr(o->v.DictComp.key);
3048 if (!value) goto failed;
3049 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3050 goto failed;
3051 Py_DECREF(value);
3052 value = ast2obj_expr(o->v.DictComp.value);
3053 if (!value) goto failed;
3054 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3055 goto failed;
3056 Py_DECREF(value);
3057 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3058 if (!value) goto failed;
3059 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3060 goto failed;
3061 Py_DECREF(value);
3062 break;
3063 case GeneratorExp_kind:
3064 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3065 if (!result) goto failed;
3066 value = ast2obj_expr(o->v.GeneratorExp.elt);
3067 if (!value) goto failed;
3068 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3069 goto failed;
3070 Py_DECREF(value);
3071 value = ast2obj_list(o->v.GeneratorExp.generators,
3072 ast2obj_comprehension);
3073 if (!value) goto failed;
3074 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3075 goto failed;
3076 Py_DECREF(value);
3077 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003078 case Await_kind:
3079 result = PyType_GenericNew(Await_type, NULL, NULL);
3080 if (!result) goto failed;
3081 value = ast2obj_expr(o->v.Await.value);
3082 if (!value) goto failed;
3083 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3084 goto failed;
3085 Py_DECREF(value);
3086 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003087 case Yield_kind:
3088 result = PyType_GenericNew(Yield_type, NULL, NULL);
3089 if (!result) goto failed;
3090 value = ast2obj_expr(o->v.Yield.value);
3091 if (!value) goto failed;
3092 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3093 goto failed;
3094 Py_DECREF(value);
3095 break;
3096 case YieldFrom_kind:
3097 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3098 if (!result) goto failed;
3099 value = ast2obj_expr(o->v.YieldFrom.value);
3100 if (!value) goto failed;
3101 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3102 goto failed;
3103 Py_DECREF(value);
3104 break;
3105 case Compare_kind:
3106 result = PyType_GenericNew(Compare_type, NULL, NULL);
3107 if (!result) goto failed;
3108 value = ast2obj_expr(o->v.Compare.left);
3109 if (!value) goto failed;
3110 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3111 goto failed;
3112 Py_DECREF(value);
3113 {
3114 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3115 value = PyList_New(n);
3116 if (!value) goto failed;
3117 for(i = 0; i < n; i++)
3118 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003120 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003121 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3122 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003123 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003124 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003126 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3127 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003128 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003129 break;
3130 case Call_kind:
3131 result = PyType_GenericNew(Call_type, NULL, NULL);
3132 if (!result) goto failed;
3133 value = ast2obj_expr(o->v.Call.func);
3134 if (!value) goto failed;
3135 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3136 goto failed;
3137 Py_DECREF(value);
3138 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3139 if (!value) goto failed;
3140 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3141 goto failed;
3142 Py_DECREF(value);
3143 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3144 if (!value) goto failed;
3145 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003148 break;
3149 case Num_kind:
3150 result = PyType_GenericNew(Num_type, NULL, NULL);
3151 if (!result) goto failed;
3152 value = ast2obj_object(o->v.Num.n);
3153 if (!value) goto failed;
3154 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3155 goto failed;
3156 Py_DECREF(value);
3157 break;
3158 case Str_kind:
3159 result = PyType_GenericNew(Str_type, NULL, NULL);
3160 if (!result) goto failed;
3161 value = ast2obj_string(o->v.Str.s);
3162 if (!value) goto failed;
3163 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 break;
3167 case Bytes_kind:
3168 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3169 if (!result) goto failed;
3170 value = ast2obj_bytes(o->v.Bytes.s);
3171 if (!value) goto failed;
3172 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003176 case NameConstant_kind:
3177 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3178 if (!result) goto failed;
3179 value = ast2obj_singleton(o->v.NameConstant.value);
3180 if (!value) goto failed;
3181 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3182 goto failed;
3183 Py_DECREF(value);
3184 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003185 case Ellipsis_kind:
3186 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3187 if (!result) goto failed;
3188 break;
3189 case Attribute_kind:
3190 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3191 if (!result) goto failed;
3192 value = ast2obj_expr(o->v.Attribute.value);
3193 if (!value) goto failed;
3194 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3195 goto failed;
3196 Py_DECREF(value);
3197 value = ast2obj_identifier(o->v.Attribute.attr);
3198 if (!value) goto failed;
3199 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3200 goto failed;
3201 Py_DECREF(value);
3202 value = ast2obj_expr_context(o->v.Attribute.ctx);
3203 if (!value) goto failed;
3204 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3205 goto failed;
3206 Py_DECREF(value);
3207 break;
3208 case Subscript_kind:
3209 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3210 if (!result) goto failed;
3211 value = ast2obj_expr(o->v.Subscript.value);
3212 if (!value) goto failed;
3213 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 value = ast2obj_slice(o->v.Subscript.slice);
3217 if (!value) goto failed;
3218 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3219 goto failed;
3220 Py_DECREF(value);
3221 value = ast2obj_expr_context(o->v.Subscript.ctx);
3222 if (!value) goto failed;
3223 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3224 goto failed;
3225 Py_DECREF(value);
3226 break;
3227 case Starred_kind:
3228 result = PyType_GenericNew(Starred_type, NULL, NULL);
3229 if (!result) goto failed;
3230 value = ast2obj_expr(o->v.Starred.value);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 value = ast2obj_expr_context(o->v.Starred.ctx);
3236 if (!value) goto failed;
3237 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
3240 break;
3241 case Name_kind:
3242 result = PyType_GenericNew(Name_type, NULL, NULL);
3243 if (!result) goto failed;
3244 value = ast2obj_identifier(o->v.Name.id);
3245 if (!value) goto failed;
3246 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3247 goto failed;
3248 Py_DECREF(value);
3249 value = ast2obj_expr_context(o->v.Name.ctx);
3250 if (!value) goto failed;
3251 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3252 goto failed;
3253 Py_DECREF(value);
3254 break;
3255 case List_kind:
3256 result = PyType_GenericNew(List_type, NULL, NULL);
3257 if (!result) goto failed;
3258 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3259 if (!value) goto failed;
3260 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3261 goto failed;
3262 Py_DECREF(value);
3263 value = ast2obj_expr_context(o->v.List.ctx);
3264 if (!value) goto failed;
3265 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3266 goto failed;
3267 Py_DECREF(value);
3268 break;
3269 case Tuple_kind:
3270 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3271 if (!result) goto failed;
3272 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3273 if (!value) goto failed;
3274 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
3277 value = ast2obj_expr_context(o->v.Tuple.ctx);
3278 if (!value) goto failed;
3279 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
3282 break;
3283 }
3284 value = ast2obj_int(o->lineno);
3285 if (!value) goto failed;
3286 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3287 goto failed;
3288 Py_DECREF(value);
3289 value = ast2obj_int(o->col_offset);
3290 if (!value) goto failed;
3291 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3292 goto failed;
3293 Py_DECREF(value);
3294 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003295failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003296 Py_XDECREF(value);
3297 Py_XDECREF(result);
3298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
3300
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003301PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003303 switch(o) {
3304 case Load:
3305 Py_INCREF(Load_singleton);
3306 return Load_singleton;
3307 case Store:
3308 Py_INCREF(Store_singleton);
3309 return Store_singleton;
3310 case Del:
3311 Py_INCREF(Del_singleton);
3312 return Del_singleton;
3313 case AugLoad:
3314 Py_INCREF(AugLoad_singleton);
3315 return AugLoad_singleton;
3316 case AugStore:
3317 Py_INCREF(AugStore_singleton);
3318 return AugStore_singleton;
3319 case Param:
3320 Py_INCREF(Param_singleton);
3321 return Param_singleton;
3322 default:
3323 /* should never happen, but just in case ... */
3324 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3325 return NULL;
3326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003328PyObject*
3329ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003331 slice_ty o = (slice_ty)_o;
3332 PyObject *result = NULL, *value = NULL;
3333 if (!o) {
3334 Py_INCREF(Py_None);
3335 return Py_None;
3336 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003337
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003338 switch (o->kind) {
3339 case Slice_kind:
3340 result = PyType_GenericNew(Slice_type, NULL, NULL);
3341 if (!result) goto failed;
3342 value = ast2obj_expr(o->v.Slice.lower);
3343 if (!value) goto failed;
3344 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3345 goto failed;
3346 Py_DECREF(value);
3347 value = ast2obj_expr(o->v.Slice.upper);
3348 if (!value) goto failed;
3349 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3350 goto failed;
3351 Py_DECREF(value);
3352 value = ast2obj_expr(o->v.Slice.step);
3353 if (!value) goto failed;
3354 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3355 goto failed;
3356 Py_DECREF(value);
3357 break;
3358 case ExtSlice_kind:
3359 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3360 if (!result) goto failed;
3361 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3362 if (!value) goto failed;
3363 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3364 goto failed;
3365 Py_DECREF(value);
3366 break;
3367 case Index_kind:
3368 result = PyType_GenericNew(Index_type, NULL, NULL);
3369 if (!result) goto failed;
3370 value = ast2obj_expr(o->v.Index.value);
3371 if (!value) goto failed;
3372 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3373 goto failed;
3374 Py_DECREF(value);
3375 break;
3376 }
3377 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003378failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003379 Py_XDECREF(value);
3380 Py_XDECREF(result);
3381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}
3383
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003384PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003386 switch(o) {
3387 case And:
3388 Py_INCREF(And_singleton);
3389 return And_singleton;
3390 case Or:
3391 Py_INCREF(Or_singleton);
3392 return Or_singleton;
3393 default:
3394 /* should never happen, but just in case ... */
3395 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3396 return NULL;
3397 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003398}
3399PyObject* ast2obj_operator(operator_ty o)
3400{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003401 switch(o) {
3402 case Add:
3403 Py_INCREF(Add_singleton);
3404 return Add_singleton;
3405 case Sub:
3406 Py_INCREF(Sub_singleton);
3407 return Sub_singleton;
3408 case Mult:
3409 Py_INCREF(Mult_singleton);
3410 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003411 case MatMult:
3412 Py_INCREF(MatMult_singleton);
3413 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003414 case Div:
3415 Py_INCREF(Div_singleton);
3416 return Div_singleton;
3417 case Mod:
3418 Py_INCREF(Mod_singleton);
3419 return Mod_singleton;
3420 case Pow:
3421 Py_INCREF(Pow_singleton);
3422 return Pow_singleton;
3423 case LShift:
3424 Py_INCREF(LShift_singleton);
3425 return LShift_singleton;
3426 case RShift:
3427 Py_INCREF(RShift_singleton);
3428 return RShift_singleton;
3429 case BitOr:
3430 Py_INCREF(BitOr_singleton);
3431 return BitOr_singleton;
3432 case BitXor:
3433 Py_INCREF(BitXor_singleton);
3434 return BitXor_singleton;
3435 case BitAnd:
3436 Py_INCREF(BitAnd_singleton);
3437 return BitAnd_singleton;
3438 case FloorDiv:
3439 Py_INCREF(FloorDiv_singleton);
3440 return FloorDiv_singleton;
3441 default:
3442 /* should never happen, but just in case ... */
3443 PyErr_Format(PyExc_SystemError, "unknown operator found");
3444 return NULL;
3445 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003446}
3447PyObject* ast2obj_unaryop(unaryop_ty o)
3448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003449 switch(o) {
3450 case Invert:
3451 Py_INCREF(Invert_singleton);
3452 return Invert_singleton;
3453 case Not:
3454 Py_INCREF(Not_singleton);
3455 return Not_singleton;
3456 case UAdd:
3457 Py_INCREF(UAdd_singleton);
3458 return UAdd_singleton;
3459 case USub:
3460 Py_INCREF(USub_singleton);
3461 return USub_singleton;
3462 default:
3463 /* should never happen, but just in case ... */
3464 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3465 return NULL;
3466 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003467}
3468PyObject* ast2obj_cmpop(cmpop_ty o)
3469{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003470 switch(o) {
3471 case Eq:
3472 Py_INCREF(Eq_singleton);
3473 return Eq_singleton;
3474 case NotEq:
3475 Py_INCREF(NotEq_singleton);
3476 return NotEq_singleton;
3477 case Lt:
3478 Py_INCREF(Lt_singleton);
3479 return Lt_singleton;
3480 case LtE:
3481 Py_INCREF(LtE_singleton);
3482 return LtE_singleton;
3483 case Gt:
3484 Py_INCREF(Gt_singleton);
3485 return Gt_singleton;
3486 case GtE:
3487 Py_INCREF(GtE_singleton);
3488 return GtE_singleton;
3489 case Is:
3490 Py_INCREF(Is_singleton);
3491 return Is_singleton;
3492 case IsNot:
3493 Py_INCREF(IsNot_singleton);
3494 return IsNot_singleton;
3495 case In:
3496 Py_INCREF(In_singleton);
3497 return In_singleton;
3498 case NotIn:
3499 Py_INCREF(NotIn_singleton);
3500 return NotIn_singleton;
3501 default:
3502 /* should never happen, but just in case ... */
3503 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3504 return NULL;
3505 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003506}
3507PyObject*
3508ast2obj_comprehension(void* _o)
3509{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003510 comprehension_ty o = (comprehension_ty)_o;
3511 PyObject *result = NULL, *value = NULL;
3512 if (!o) {
3513 Py_INCREF(Py_None);
3514 return Py_None;
3515 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003516
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003517 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3518 if (!result) return NULL;
3519 value = ast2obj_expr(o->target);
3520 if (!value) goto failed;
3521 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3522 goto failed;
3523 Py_DECREF(value);
3524 value = ast2obj_expr(o->iter);
3525 if (!value) goto failed;
3526 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3527 goto failed;
3528 Py_DECREF(value);
3529 value = ast2obj_list(o->ifs, ast2obj_expr);
3530 if (!value) goto failed;
3531 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3532 goto failed;
3533 Py_DECREF(value);
3534 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003535failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003536 Py_XDECREF(value);
3537 Py_XDECREF(result);
3538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539}
3540
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003541PyObject*
3542ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003544 excepthandler_ty o = (excepthandler_ty)_o;
3545 PyObject *result = NULL, *value = NULL;
3546 if (!o) {
3547 Py_INCREF(Py_None);
3548 return Py_None;
3549 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003550
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003551 switch (o->kind) {
3552 case ExceptHandler_kind:
3553 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3554 if (!result) goto failed;
3555 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003556 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003557 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3558 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003559 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003560 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003561 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003562 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3563 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003564 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003565 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3566 if (!value) goto failed;
3567 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3568 goto failed;
3569 Py_DECREF(value);
3570 break;
3571 }
3572 value = ast2obj_int(o->lineno);
3573 if (!value) goto failed;
3574 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3575 goto failed;
3576 Py_DECREF(value);
3577 value = ast2obj_int(o->col_offset);
3578 if (!value) goto failed;
3579 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3580 goto failed;
3581 Py_DECREF(value);
3582 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003583failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003584 Py_XDECREF(value);
3585 Py_XDECREF(result);
3586 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587}
3588
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003589PyObject*
3590ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003592 arguments_ty o = (arguments_ty)_o;
3593 PyObject *result = NULL, *value = NULL;
3594 if (!o) {
3595 Py_INCREF(Py_None);
3596 return Py_None;
3597 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003598
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003599 result = PyType_GenericNew(arguments_type, NULL, NULL);
3600 if (!result) return NULL;
3601 value = ast2obj_list(o->args, ast2obj_arg);
3602 if (!value) goto failed;
3603 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3604 goto failed;
3605 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003606 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003607 if (!value) goto failed;
3608 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3609 goto failed;
3610 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003611 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3612 if (!value) goto failed;
3613 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3614 goto failed;
3615 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003616 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003617 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003618 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003619 goto failed;
3620 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003621 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003622 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003623 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003624 goto failed;
3625 Py_DECREF(value);
3626 value = ast2obj_list(o->defaults, ast2obj_expr);
3627 if (!value) goto failed;
3628 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3629 goto failed;
3630 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003631 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003632failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003633 Py_XDECREF(value);
3634 Py_XDECREF(result);
3635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636}
3637
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003638PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003639ast2obj_arg(void* _o)
3640{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003641 arg_ty o = (arg_ty)_o;
3642 PyObject *result = NULL, *value = NULL;
3643 if (!o) {
3644 Py_INCREF(Py_None);
3645 return Py_None;
3646 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003647
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003648 result = PyType_GenericNew(arg_type, NULL, NULL);
3649 if (!result) return NULL;
3650 value = ast2obj_identifier(o->arg);
3651 if (!value) goto failed;
3652 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3653 goto failed;
3654 Py_DECREF(value);
3655 value = ast2obj_expr(o->annotation);
3656 if (!value) goto failed;
3657 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3658 goto failed;
3659 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003660 value = ast2obj_int(o->lineno);
3661 if (!value) goto failed;
3662 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3663 goto failed;
3664 Py_DECREF(value);
3665 value = ast2obj_int(o->col_offset);
3666 if (!value) goto failed;
3667 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3668 goto failed;
3669 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003670 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003671failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003672 Py_XDECREF(value);
3673 Py_XDECREF(result);
3674 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003675}
3676
3677PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003678ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003680 keyword_ty o = (keyword_ty)_o;
3681 PyObject *result = NULL, *value = NULL;
3682 if (!o) {
3683 Py_INCREF(Py_None);
3684 return Py_None;
3685 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003686
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003687 result = PyType_GenericNew(keyword_type, NULL, NULL);
3688 if (!result) return NULL;
3689 value = ast2obj_identifier(o->arg);
3690 if (!value) goto failed;
3691 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3692 goto failed;
3693 Py_DECREF(value);
3694 value = ast2obj_expr(o->value);
3695 if (!value) goto failed;
3696 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3697 goto failed;
3698 Py_DECREF(value);
3699 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003700failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003701 Py_XDECREF(value);
3702 Py_XDECREF(result);
3703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704}
3705
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003706PyObject*
3707ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003709 alias_ty o = (alias_ty)_o;
3710 PyObject *result = NULL, *value = NULL;
3711 if (!o) {
3712 Py_INCREF(Py_None);
3713 return Py_None;
3714 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003715
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003716 result = PyType_GenericNew(alias_type, NULL, NULL);
3717 if (!result) return NULL;
3718 value = ast2obj_identifier(o->name);
3719 if (!value) goto failed;
3720 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3721 goto failed;
3722 Py_DECREF(value);
3723 value = ast2obj_identifier(o->asname);
3724 if (!value) goto failed;
3725 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3726 goto failed;
3727 Py_DECREF(value);
3728 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003729failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003730 Py_XDECREF(value);
3731 Py_XDECREF(result);
3732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733}
3734
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003735PyObject*
3736ast2obj_withitem(void* _o)
3737{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003738 withitem_ty o = (withitem_ty)_o;
3739 PyObject *result = NULL, *value = NULL;
3740 if (!o) {
3741 Py_INCREF(Py_None);
3742 return Py_None;
3743 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003744
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003745 result = PyType_GenericNew(withitem_type, NULL, NULL);
3746 if (!result) return NULL;
3747 value = ast2obj_expr(o->context_expr);
3748 if (!value) goto failed;
3749 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3750 goto failed;
3751 Py_DECREF(value);
3752 value = ast2obj_expr(o->optional_vars);
3753 if (!value) goto failed;
3754 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3755 goto failed;
3756 Py_DECREF(value);
3757 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003758failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003759 Py_XDECREF(value);
3760 Py_XDECREF(result);
3761 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003762}
3763
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003765int
3766obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3767{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003768 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003769
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003770 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003771
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003772 if (obj == Py_None) {
3773 *out = NULL;
3774 return 0;
3775 }
3776 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3777 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003778 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003779 }
3780 if (isinstance) {
3781 asdl_seq* body;
3782
3783 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3784 int res;
3785 Py_ssize_t len;
3786 Py_ssize_t i;
3787 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3788 if (tmp == NULL) goto failed;
3789 if (!PyList_Check(tmp)) {
3790 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3791 goto failed;
3792 }
3793 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003794 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003795 if (body == NULL) goto failed;
3796 for (i = 0; i < len; i++) {
3797 stmt_ty value;
3798 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3799 if (res != 0) goto failed;
3800 asdl_seq_SET(body, i, value);
3801 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003802 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003803 } else {
3804 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3805 return 1;
3806 }
3807 *out = Module(body, arena);
3808 if (*out == NULL) goto failed;
3809 return 0;
3810 }
3811 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3812 if (isinstance == -1) {
3813 return 1;
3814 }
3815 if (isinstance) {
3816 asdl_seq* body;
3817
3818 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3819 int res;
3820 Py_ssize_t len;
3821 Py_ssize_t i;
3822 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3823 if (tmp == NULL) goto failed;
3824 if (!PyList_Check(tmp)) {
3825 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3826 goto failed;
3827 }
3828 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003829 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003830 if (body == NULL) goto failed;
3831 for (i = 0; i < len; i++) {
3832 stmt_ty value;
3833 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3834 if (res != 0) goto failed;
3835 asdl_seq_SET(body, i, value);
3836 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003837 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003838 } else {
3839 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3840 return 1;
3841 }
3842 *out = Interactive(body, arena);
3843 if (*out == NULL) goto failed;
3844 return 0;
3845 }
3846 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3847 if (isinstance == -1) {
3848 return 1;
3849 }
3850 if (isinstance) {
3851 expr_ty body;
3852
3853 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3854 int res;
3855 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3856 if (tmp == NULL) goto failed;
3857 res = obj2ast_expr(tmp, &body, arena);
3858 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003859 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003860 } else {
3861 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3862 return 1;
3863 }
3864 *out = Expression(body, arena);
3865 if (*out == NULL) goto failed;
3866 return 0;
3867 }
3868 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3869 if (isinstance == -1) {
3870 return 1;
3871 }
3872 if (isinstance) {
3873 asdl_seq* body;
3874
3875 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3876 int res;
3877 Py_ssize_t len;
3878 Py_ssize_t i;
3879 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3880 if (tmp == NULL) goto failed;
3881 if (!PyList_Check(tmp)) {
3882 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3883 goto failed;
3884 }
3885 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003886 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003887 if (body == NULL) goto failed;
3888 for (i = 0; i < len; i++) {
3889 stmt_ty value;
3890 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3891 if (res != 0) goto failed;
3892 asdl_seq_SET(body, i, value);
3893 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003894 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003895 } else {
3896 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3897 return 1;
3898 }
3899 *out = Suite(body, arena);
3900 if (*out == NULL) goto failed;
3901 return 0;
3902 }
3903
3904 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3905 failed:
3906 Py_XDECREF(tmp);
3907 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003908}
3909
3910int
3911obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3912{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003913 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003914
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003915 PyObject *tmp = NULL;
3916 int lineno;
3917 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003918
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003919 if (obj == Py_None) {
3920 *out = NULL;
3921 return 0;
3922 }
3923 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
3924 int res;
3925 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
3926 if (tmp == NULL) goto failed;
3927 res = obj2ast_int(tmp, &lineno, arena);
3928 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003929 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003930 } else {
3931 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003932 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003933 }
3934 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
3935 int res;
3936 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
3937 if (tmp == NULL) goto failed;
3938 res = obj2ast_int(tmp, &col_offset, arena);
3939 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003940 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 } else {
3942 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3943 return 1;
3944 }
3945 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3946 if (isinstance == -1) {
3947 return 1;
3948 }
3949 if (isinstance) {
3950 identifier name;
3951 arguments_ty args;
3952 asdl_seq* body;
3953 asdl_seq* decorator_list;
3954 expr_ty returns;
3955
3956 if (_PyObject_HasAttrId(obj, &PyId_name)) {
3957 int res;
3958 tmp = _PyObject_GetAttrId(obj, &PyId_name);
3959 if (tmp == NULL) goto failed;
3960 res = obj2ast_identifier(tmp, &name, arena);
3961 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003962 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003963 } else {
3964 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3965 return 1;
3966 }
3967 if (_PyObject_HasAttrId(obj, &PyId_args)) {
3968 int res;
3969 tmp = _PyObject_GetAttrId(obj, &PyId_args);
3970 if (tmp == NULL) goto failed;
3971 res = obj2ast_arguments(tmp, &args, arena);
3972 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003973 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003974 } else {
3975 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3976 return 1;
3977 }
3978 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3979 int res;
3980 Py_ssize_t len;
3981 Py_ssize_t i;
3982 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3983 if (tmp == NULL) goto failed;
3984 if (!PyList_Check(tmp)) {
3985 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3986 goto failed;
3987 }
3988 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003989 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003990 if (body == NULL) goto failed;
3991 for (i = 0; i < len; i++) {
3992 stmt_ty value;
3993 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3994 if (res != 0) goto failed;
3995 asdl_seq_SET(body, i, value);
3996 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003997 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003998 } else {
3999 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4000 return 1;
4001 }
4002 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4003 int res;
4004 Py_ssize_t len;
4005 Py_ssize_t i;
4006 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4007 if (tmp == NULL) goto failed;
4008 if (!PyList_Check(tmp)) {
4009 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4010 goto failed;
4011 }
4012 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004013 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004014 if (decorator_list == NULL) goto failed;
4015 for (i = 0; i < len; i++) {
4016 expr_ty value;
4017 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4018 if (res != 0) goto failed;
4019 asdl_seq_SET(decorator_list, i, value);
4020 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004021 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004022 } else {
4023 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4024 return 1;
4025 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004026 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004027 int res;
4028 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4029 if (tmp == NULL) goto failed;
4030 res = obj2ast_expr(tmp, &returns, arena);
4031 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004032 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004033 } else {
4034 returns = NULL;
4035 }
4036 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4037 col_offset, arena);
4038 if (*out == NULL) goto failed;
4039 return 0;
4040 }
Yury Selivanov75445082015-05-11 22:57:16 -04004041 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4042 if (isinstance == -1) {
4043 return 1;
4044 }
4045 if (isinstance) {
4046 identifier name;
4047 arguments_ty args;
4048 asdl_seq* body;
4049 asdl_seq* decorator_list;
4050 expr_ty returns;
4051
4052 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4053 int res;
4054 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4055 if (tmp == NULL) goto failed;
4056 res = obj2ast_identifier(tmp, &name, arena);
4057 if (res != 0) goto failed;
4058 Py_CLEAR(tmp);
4059 } else {
4060 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4061 return 1;
4062 }
4063 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4064 int res;
4065 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4066 if (tmp == NULL) goto failed;
4067 res = obj2ast_arguments(tmp, &args, arena);
4068 if (res != 0) goto failed;
4069 Py_CLEAR(tmp);
4070 } else {
4071 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4072 return 1;
4073 }
4074 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4075 int res;
4076 Py_ssize_t len;
4077 Py_ssize_t i;
4078 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4079 if (tmp == NULL) goto failed;
4080 if (!PyList_Check(tmp)) {
4081 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4082 goto failed;
4083 }
4084 len = PyList_GET_SIZE(tmp);
4085 body = _Py_asdl_seq_new(len, arena);
4086 if (body == NULL) goto failed;
4087 for (i = 0; i < len; i++) {
4088 stmt_ty value;
4089 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4090 if (res != 0) goto failed;
4091 asdl_seq_SET(body, i, value);
4092 }
4093 Py_CLEAR(tmp);
4094 } else {
4095 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4096 return 1;
4097 }
4098 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4099 int res;
4100 Py_ssize_t len;
4101 Py_ssize_t i;
4102 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4103 if (tmp == NULL) goto failed;
4104 if (!PyList_Check(tmp)) {
4105 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4106 goto failed;
4107 }
4108 len = PyList_GET_SIZE(tmp);
4109 decorator_list = _Py_asdl_seq_new(len, arena);
4110 if (decorator_list == NULL) goto failed;
4111 for (i = 0; i < len; i++) {
4112 expr_ty value;
4113 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4114 if (res != 0) goto failed;
4115 asdl_seq_SET(decorator_list, i, value);
4116 }
4117 Py_CLEAR(tmp);
4118 } else {
4119 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4120 return 1;
4121 }
4122 if (exists_not_none(obj, &PyId_returns)) {
4123 int res;
4124 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4125 if (tmp == NULL) goto failed;
4126 res = obj2ast_expr(tmp, &returns, arena);
4127 if (res != 0) goto failed;
4128 Py_CLEAR(tmp);
4129 } else {
4130 returns = NULL;
4131 }
4132 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4133 lineno, col_offset, arena);
4134 if (*out == NULL) goto failed;
4135 return 0;
4136 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004137 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4138 if (isinstance == -1) {
4139 return 1;
4140 }
4141 if (isinstance) {
4142 identifier name;
4143 asdl_seq* bases;
4144 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004145 asdl_seq* body;
4146 asdl_seq* decorator_list;
4147
4148 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4149 int res;
4150 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4151 if (tmp == NULL) goto failed;
4152 res = obj2ast_identifier(tmp, &name, arena);
4153 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004154 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004155 } else {
4156 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4157 return 1;
4158 }
4159 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4160 int res;
4161 Py_ssize_t len;
4162 Py_ssize_t i;
4163 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4164 if (tmp == NULL) goto failed;
4165 if (!PyList_Check(tmp)) {
4166 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4167 goto failed;
4168 }
4169 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004170 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004171 if (bases == NULL) goto failed;
4172 for (i = 0; i < len; i++) {
4173 expr_ty value;
4174 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4175 if (res != 0) goto failed;
4176 asdl_seq_SET(bases, i, value);
4177 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004178 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004179 } else {
4180 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4181 return 1;
4182 }
4183 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4184 int res;
4185 Py_ssize_t len;
4186 Py_ssize_t i;
4187 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4188 if (tmp == NULL) goto failed;
4189 if (!PyList_Check(tmp)) {
4190 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4191 goto failed;
4192 }
4193 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004194 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004195 if (keywords == NULL) goto failed;
4196 for (i = 0; i < len; i++) {
4197 keyword_ty value;
4198 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4199 if (res != 0) goto failed;
4200 asdl_seq_SET(keywords, i, value);
4201 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004202 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004203 } else {
4204 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4205 return 1;
4206 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004207 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4208 int res;
4209 Py_ssize_t len;
4210 Py_ssize_t i;
4211 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4212 if (tmp == NULL) goto failed;
4213 if (!PyList_Check(tmp)) {
4214 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4215 goto failed;
4216 }
4217 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004218 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004219 if (body == NULL) goto failed;
4220 for (i = 0; i < len; i++) {
4221 stmt_ty value;
4222 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4223 if (res != 0) goto failed;
4224 asdl_seq_SET(body, i, value);
4225 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004226 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004227 } else {
4228 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4229 return 1;
4230 }
4231 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4232 int res;
4233 Py_ssize_t len;
4234 Py_ssize_t i;
4235 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4236 if (tmp == NULL) goto failed;
4237 if (!PyList_Check(tmp)) {
4238 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4239 goto failed;
4240 }
4241 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004242 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004243 if (decorator_list == NULL) goto failed;
4244 for (i = 0; i < len; i++) {
4245 expr_ty value;
4246 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4247 if (res != 0) goto failed;
4248 asdl_seq_SET(decorator_list, i, value);
4249 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004250 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004251 } else {
4252 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4253 return 1;
4254 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004255 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4256 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004257 if (*out == NULL) goto failed;
4258 return 0;
4259 }
4260 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4261 if (isinstance == -1) {
4262 return 1;
4263 }
4264 if (isinstance) {
4265 expr_ty value;
4266
Victor Stinneree4b59c2013-07-27 00:01:35 +02004267 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004268 int res;
4269 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4270 if (tmp == NULL) goto failed;
4271 res = obj2ast_expr(tmp, &value, arena);
4272 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004273 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004274 } else {
4275 value = NULL;
4276 }
4277 *out = Return(value, lineno, col_offset, arena);
4278 if (*out == NULL) goto failed;
4279 return 0;
4280 }
4281 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4282 if (isinstance == -1) {
4283 return 1;
4284 }
4285 if (isinstance) {
4286 asdl_seq* targets;
4287
4288 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4289 int res;
4290 Py_ssize_t len;
4291 Py_ssize_t i;
4292 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4293 if (tmp == NULL) goto failed;
4294 if (!PyList_Check(tmp)) {
4295 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4296 goto failed;
4297 }
4298 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004299 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004300 if (targets == NULL) goto failed;
4301 for (i = 0; i < len; i++) {
4302 expr_ty value;
4303 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4304 if (res != 0) goto failed;
4305 asdl_seq_SET(targets, i, value);
4306 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004307 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004308 } else {
4309 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4310 return 1;
4311 }
4312 *out = Delete(targets, lineno, col_offset, arena);
4313 if (*out == NULL) goto failed;
4314 return 0;
4315 }
4316 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4317 if (isinstance == -1) {
4318 return 1;
4319 }
4320 if (isinstance) {
4321 asdl_seq* targets;
4322 expr_ty value;
4323
4324 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4325 int res;
4326 Py_ssize_t len;
4327 Py_ssize_t i;
4328 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4329 if (tmp == NULL) goto failed;
4330 if (!PyList_Check(tmp)) {
4331 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4332 goto failed;
4333 }
4334 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004335 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004336 if (targets == NULL) goto failed;
4337 for (i = 0; i < len; i++) {
4338 expr_ty value;
4339 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4340 if (res != 0) goto failed;
4341 asdl_seq_SET(targets, i, value);
4342 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004344 } else {
4345 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4346 return 1;
4347 }
4348 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4349 int res;
4350 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4351 if (tmp == NULL) goto failed;
4352 res = obj2ast_expr(tmp, &value, arena);
4353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004355 } else {
4356 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4357 return 1;
4358 }
4359 *out = Assign(targets, value, lineno, col_offset, arena);
4360 if (*out == NULL) goto failed;
4361 return 0;
4362 }
4363 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4364 if (isinstance == -1) {
4365 return 1;
4366 }
4367 if (isinstance) {
4368 expr_ty target;
4369 operator_ty op;
4370 expr_ty value;
4371
4372 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4373 int res;
4374 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4375 if (tmp == NULL) goto failed;
4376 res = obj2ast_expr(tmp, &target, arena);
4377 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004378 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004379 } else {
4380 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4381 return 1;
4382 }
4383 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4384 int res;
4385 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4386 if (tmp == NULL) goto failed;
4387 res = obj2ast_operator(tmp, &op, arena);
4388 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004389 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004390 } else {
4391 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4392 return 1;
4393 }
4394 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4395 int res;
4396 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4397 if (tmp == NULL) goto failed;
4398 res = obj2ast_expr(tmp, &value, arena);
4399 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004400 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004401 } else {
4402 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4403 return 1;
4404 }
4405 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4406 if (*out == NULL) goto failed;
4407 return 0;
4408 }
4409 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4410 if (isinstance == -1) {
4411 return 1;
4412 }
4413 if (isinstance) {
4414 expr_ty target;
4415 expr_ty iter;
4416 asdl_seq* body;
4417 asdl_seq* orelse;
4418
4419 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4420 int res;
4421 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4422 if (tmp == NULL) goto failed;
4423 res = obj2ast_expr(tmp, &target, arena);
4424 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004425 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004426 } else {
4427 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4428 return 1;
4429 }
4430 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4431 int res;
4432 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4433 if (tmp == NULL) goto failed;
4434 res = obj2ast_expr(tmp, &iter, arena);
4435 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004436 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004437 } else {
4438 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4439 return 1;
4440 }
4441 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4442 int res;
4443 Py_ssize_t len;
4444 Py_ssize_t i;
4445 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4446 if (tmp == NULL) goto failed;
4447 if (!PyList_Check(tmp)) {
4448 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4449 goto failed;
4450 }
4451 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004452 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004453 if (body == NULL) goto failed;
4454 for (i = 0; i < len; i++) {
4455 stmt_ty value;
4456 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4457 if (res != 0) goto failed;
4458 asdl_seq_SET(body, i, value);
4459 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004460 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004461 } else {
4462 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4463 return 1;
4464 }
4465 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4466 int res;
4467 Py_ssize_t len;
4468 Py_ssize_t i;
4469 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4470 if (tmp == NULL) goto failed;
4471 if (!PyList_Check(tmp)) {
4472 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4473 goto failed;
4474 }
4475 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004476 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004477 if (orelse == NULL) goto failed;
4478 for (i = 0; i < len; i++) {
4479 stmt_ty value;
4480 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4481 if (res != 0) goto failed;
4482 asdl_seq_SET(orelse, i, value);
4483 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004484 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004485 } else {
4486 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4487 return 1;
4488 }
4489 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4490 if (*out == NULL) goto failed;
4491 return 0;
4492 }
Yury Selivanov75445082015-05-11 22:57:16 -04004493 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4494 if (isinstance == -1) {
4495 return 1;
4496 }
4497 if (isinstance) {
4498 expr_ty target;
4499 expr_ty iter;
4500 asdl_seq* body;
4501 asdl_seq* orelse;
4502
4503 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4504 int res;
4505 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4506 if (tmp == NULL) goto failed;
4507 res = obj2ast_expr(tmp, &target, arena);
4508 if (res != 0) goto failed;
4509 Py_CLEAR(tmp);
4510 } else {
4511 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4512 return 1;
4513 }
4514 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4515 int res;
4516 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4517 if (tmp == NULL) goto failed;
4518 res = obj2ast_expr(tmp, &iter, arena);
4519 if (res != 0) goto failed;
4520 Py_CLEAR(tmp);
4521 } else {
4522 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4523 return 1;
4524 }
4525 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4526 int res;
4527 Py_ssize_t len;
4528 Py_ssize_t i;
4529 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4530 if (tmp == NULL) goto failed;
4531 if (!PyList_Check(tmp)) {
4532 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4533 goto failed;
4534 }
4535 len = PyList_GET_SIZE(tmp);
4536 body = _Py_asdl_seq_new(len, arena);
4537 if (body == NULL) goto failed;
4538 for (i = 0; i < len; i++) {
4539 stmt_ty value;
4540 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4541 if (res != 0) goto failed;
4542 asdl_seq_SET(body, i, value);
4543 }
4544 Py_CLEAR(tmp);
4545 } else {
4546 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4547 return 1;
4548 }
4549 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4550 int res;
4551 Py_ssize_t len;
4552 Py_ssize_t i;
4553 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4554 if (tmp == NULL) goto failed;
4555 if (!PyList_Check(tmp)) {
4556 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4557 goto failed;
4558 }
4559 len = PyList_GET_SIZE(tmp);
4560 orelse = _Py_asdl_seq_new(len, arena);
4561 if (orelse == NULL) goto failed;
4562 for (i = 0; i < len; i++) {
4563 stmt_ty value;
4564 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4565 if (res != 0) goto failed;
4566 asdl_seq_SET(orelse, i, value);
4567 }
4568 Py_CLEAR(tmp);
4569 } else {
4570 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4571 return 1;
4572 }
4573 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4574 if (*out == NULL) goto failed;
4575 return 0;
4576 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004577 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4578 if (isinstance == -1) {
4579 return 1;
4580 }
4581 if (isinstance) {
4582 expr_ty test;
4583 asdl_seq* body;
4584 asdl_seq* orelse;
4585
4586 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4587 int res;
4588 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4589 if (tmp == NULL) goto failed;
4590 res = obj2ast_expr(tmp, &test, arena);
4591 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004592 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004593 } else {
4594 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4595 return 1;
4596 }
4597 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4598 int res;
4599 Py_ssize_t len;
4600 Py_ssize_t i;
4601 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4602 if (tmp == NULL) goto failed;
4603 if (!PyList_Check(tmp)) {
4604 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4605 goto failed;
4606 }
4607 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004608 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004609 if (body == NULL) goto failed;
4610 for (i = 0; i < len; i++) {
4611 stmt_ty value;
4612 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4613 if (res != 0) goto failed;
4614 asdl_seq_SET(body, i, value);
4615 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004616 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004617 } else {
4618 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4619 return 1;
4620 }
4621 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4622 int res;
4623 Py_ssize_t len;
4624 Py_ssize_t i;
4625 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4626 if (tmp == NULL) goto failed;
4627 if (!PyList_Check(tmp)) {
4628 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4629 goto failed;
4630 }
4631 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004632 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004633 if (orelse == NULL) goto failed;
4634 for (i = 0; i < len; i++) {
4635 stmt_ty value;
4636 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4637 if (res != 0) goto failed;
4638 asdl_seq_SET(orelse, i, value);
4639 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004640 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004641 } else {
4642 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4643 return 1;
4644 }
4645 *out = While(test, body, orelse, lineno, col_offset, arena);
4646 if (*out == NULL) goto failed;
4647 return 0;
4648 }
4649 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4650 if (isinstance == -1) {
4651 return 1;
4652 }
4653 if (isinstance) {
4654 expr_ty test;
4655 asdl_seq* body;
4656 asdl_seq* orelse;
4657
4658 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4659 int res;
4660 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4661 if (tmp == NULL) goto failed;
4662 res = obj2ast_expr(tmp, &test, arena);
4663 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004664 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004665 } else {
4666 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4667 return 1;
4668 }
4669 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4670 int res;
4671 Py_ssize_t len;
4672 Py_ssize_t i;
4673 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4674 if (tmp == NULL) goto failed;
4675 if (!PyList_Check(tmp)) {
4676 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4677 goto failed;
4678 }
4679 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004680 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004681 if (body == NULL) goto failed;
4682 for (i = 0; i < len; i++) {
4683 stmt_ty value;
4684 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4685 if (res != 0) goto failed;
4686 asdl_seq_SET(body, i, value);
4687 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004688 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4691 return 1;
4692 }
4693 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4694 int res;
4695 Py_ssize_t len;
4696 Py_ssize_t i;
4697 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4698 if (tmp == NULL) goto failed;
4699 if (!PyList_Check(tmp)) {
4700 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4701 goto failed;
4702 }
4703 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004704 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004705 if (orelse == NULL) goto failed;
4706 for (i = 0; i < len; i++) {
4707 stmt_ty value;
4708 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4709 if (res != 0) goto failed;
4710 asdl_seq_SET(orelse, i, value);
4711 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004712 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004713 } else {
4714 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4715 return 1;
4716 }
4717 *out = If(test, body, orelse, lineno, col_offset, arena);
4718 if (*out == NULL) goto failed;
4719 return 0;
4720 }
4721 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4722 if (isinstance == -1) {
4723 return 1;
4724 }
4725 if (isinstance) {
4726 asdl_seq* items;
4727 asdl_seq* body;
4728
4729 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4730 int res;
4731 Py_ssize_t len;
4732 Py_ssize_t i;
4733 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4734 if (tmp == NULL) goto failed;
4735 if (!PyList_Check(tmp)) {
4736 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4737 goto failed;
4738 }
4739 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004740 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004741 if (items == NULL) goto failed;
4742 for (i = 0; i < len; i++) {
4743 withitem_ty value;
4744 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4745 if (res != 0) goto failed;
4746 asdl_seq_SET(items, i, value);
4747 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004748 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004749 } else {
4750 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4751 return 1;
4752 }
4753 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4754 int res;
4755 Py_ssize_t len;
4756 Py_ssize_t i;
4757 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4758 if (tmp == NULL) goto failed;
4759 if (!PyList_Check(tmp)) {
4760 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4761 goto failed;
4762 }
4763 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004764 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004765 if (body == NULL) goto failed;
4766 for (i = 0; i < len; i++) {
4767 stmt_ty value;
4768 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4769 if (res != 0) goto failed;
4770 asdl_seq_SET(body, i, value);
4771 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004772 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004773 } else {
4774 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4775 return 1;
4776 }
4777 *out = With(items, body, lineno, col_offset, arena);
4778 if (*out == NULL) goto failed;
4779 return 0;
4780 }
Yury Selivanov75445082015-05-11 22:57:16 -04004781 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4782 if (isinstance == -1) {
4783 return 1;
4784 }
4785 if (isinstance) {
4786 asdl_seq* items;
4787 asdl_seq* body;
4788
4789 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4790 int res;
4791 Py_ssize_t len;
4792 Py_ssize_t i;
4793 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4794 if (tmp == NULL) goto failed;
4795 if (!PyList_Check(tmp)) {
4796 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4797 goto failed;
4798 }
4799 len = PyList_GET_SIZE(tmp);
4800 items = _Py_asdl_seq_new(len, arena);
4801 if (items == NULL) goto failed;
4802 for (i = 0; i < len; i++) {
4803 withitem_ty value;
4804 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4805 if (res != 0) goto failed;
4806 asdl_seq_SET(items, i, value);
4807 }
4808 Py_CLEAR(tmp);
4809 } else {
4810 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4811 return 1;
4812 }
4813 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4814 int res;
4815 Py_ssize_t len;
4816 Py_ssize_t i;
4817 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4818 if (tmp == NULL) goto failed;
4819 if (!PyList_Check(tmp)) {
4820 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4821 goto failed;
4822 }
4823 len = PyList_GET_SIZE(tmp);
4824 body = _Py_asdl_seq_new(len, arena);
4825 if (body == NULL) goto failed;
4826 for (i = 0; i < len; i++) {
4827 stmt_ty value;
4828 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4829 if (res != 0) goto failed;
4830 asdl_seq_SET(body, i, value);
4831 }
4832 Py_CLEAR(tmp);
4833 } else {
4834 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4835 return 1;
4836 }
4837 *out = AsyncWith(items, body, lineno, col_offset, arena);
4838 if (*out == NULL) goto failed;
4839 return 0;
4840 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004841 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4842 if (isinstance == -1) {
4843 return 1;
4844 }
4845 if (isinstance) {
4846 expr_ty exc;
4847 expr_ty cause;
4848
Victor Stinneree4b59c2013-07-27 00:01:35 +02004849 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004850 int res;
4851 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4852 if (tmp == NULL) goto failed;
4853 res = obj2ast_expr(tmp, &exc, arena);
4854 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004855 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004856 } else {
4857 exc = NULL;
4858 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004859 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004860 int res;
4861 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4862 if (tmp == NULL) goto failed;
4863 res = obj2ast_expr(tmp, &cause, arena);
4864 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004865 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004866 } else {
4867 cause = NULL;
4868 }
4869 *out = Raise(exc, cause, lineno, col_offset, arena);
4870 if (*out == NULL) goto failed;
4871 return 0;
4872 }
4873 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4874 if (isinstance == -1) {
4875 return 1;
4876 }
4877 if (isinstance) {
4878 asdl_seq* body;
4879 asdl_seq* handlers;
4880 asdl_seq* orelse;
4881 asdl_seq* finalbody;
4882
4883 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4884 int res;
4885 Py_ssize_t len;
4886 Py_ssize_t i;
4887 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4888 if (tmp == NULL) goto failed;
4889 if (!PyList_Check(tmp)) {
4890 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4891 goto failed;
4892 }
4893 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004894 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004895 if (body == NULL) goto failed;
4896 for (i = 0; i < len; i++) {
4897 stmt_ty value;
4898 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4899 if (res != 0) goto failed;
4900 asdl_seq_SET(body, i, value);
4901 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004902 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004903 } else {
4904 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4905 return 1;
4906 }
4907 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4908 int res;
4909 Py_ssize_t len;
4910 Py_ssize_t i;
4911 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4912 if (tmp == NULL) goto failed;
4913 if (!PyList_Check(tmp)) {
4914 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4915 goto failed;
4916 }
4917 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004918 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004919 if (handlers == NULL) goto failed;
4920 for (i = 0; i < len; i++) {
4921 excepthandler_ty value;
4922 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4923 if (res != 0) goto failed;
4924 asdl_seq_SET(handlers, i, value);
4925 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004926 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004927 } else {
4928 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
4929 return 1;
4930 }
4931 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4932 int res;
4933 Py_ssize_t len;
4934 Py_ssize_t i;
4935 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4936 if (tmp == NULL) goto failed;
4937 if (!PyList_Check(tmp)) {
4938 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4939 goto failed;
4940 }
4941 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004942 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004943 if (orelse == NULL) goto failed;
4944 for (i = 0; i < len; i++) {
4945 stmt_ty value;
4946 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4947 if (res != 0) goto failed;
4948 asdl_seq_SET(orelse, i, value);
4949 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004950 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004951 } else {
4952 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
4953 return 1;
4954 }
4955 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
4956 int res;
4957 Py_ssize_t len;
4958 Py_ssize_t i;
4959 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
4960 if (tmp == NULL) goto failed;
4961 if (!PyList_Check(tmp)) {
4962 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4963 goto failed;
4964 }
4965 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004966 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004967 if (finalbody == NULL) goto failed;
4968 for (i = 0; i < len; i++) {
4969 stmt_ty value;
4970 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4971 if (res != 0) goto failed;
4972 asdl_seq_SET(finalbody, i, value);
4973 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004975 } else {
4976 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
4977 return 1;
4978 }
4979 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
4980 arena);
4981 if (*out == NULL) goto failed;
4982 return 0;
4983 }
4984 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4985 if (isinstance == -1) {
4986 return 1;
4987 }
4988 if (isinstance) {
4989 expr_ty test;
4990 expr_ty msg;
4991
4992 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4993 int res;
4994 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4995 if (tmp == NULL) goto failed;
4996 res = obj2ast_expr(tmp, &test, arena);
4997 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004998 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004999 } else {
5000 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5001 return 1;
5002 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005003 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005004 int res;
5005 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5006 if (tmp == NULL) goto failed;
5007 res = obj2ast_expr(tmp, &msg, arena);
5008 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005009 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005010 } else {
5011 msg = NULL;
5012 }
5013 *out = Assert(test, msg, lineno, col_offset, arena);
5014 if (*out == NULL) goto failed;
5015 return 0;
5016 }
5017 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5018 if (isinstance == -1) {
5019 return 1;
5020 }
5021 if (isinstance) {
5022 asdl_seq* names;
5023
5024 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5025 int res;
5026 Py_ssize_t len;
5027 Py_ssize_t i;
5028 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5029 if (tmp == NULL) goto failed;
5030 if (!PyList_Check(tmp)) {
5031 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5032 goto failed;
5033 }
5034 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005035 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005036 if (names == NULL) goto failed;
5037 for (i = 0; i < len; i++) {
5038 alias_ty value;
5039 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5040 if (res != 0) goto failed;
5041 asdl_seq_SET(names, i, value);
5042 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005043 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005044 } else {
5045 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5046 return 1;
5047 }
5048 *out = Import(names, lineno, col_offset, arena);
5049 if (*out == NULL) goto failed;
5050 return 0;
5051 }
5052 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5053 if (isinstance == -1) {
5054 return 1;
5055 }
5056 if (isinstance) {
5057 identifier module;
5058 asdl_seq* names;
5059 int level;
5060
Victor Stinneree4b59c2013-07-27 00:01:35 +02005061 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005062 int res;
5063 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5064 if (tmp == NULL) goto failed;
5065 res = obj2ast_identifier(tmp, &module, arena);
5066 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005067 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005068 } else {
5069 module = NULL;
5070 }
5071 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5072 int res;
5073 Py_ssize_t len;
5074 Py_ssize_t i;
5075 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5076 if (tmp == NULL) goto failed;
5077 if (!PyList_Check(tmp)) {
5078 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5079 goto failed;
5080 }
5081 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005082 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005083 if (names == NULL) goto failed;
5084 for (i = 0; i < len; i++) {
5085 alias_ty value;
5086 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5087 if (res != 0) goto failed;
5088 asdl_seq_SET(names, i, value);
5089 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005090 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005091 } else {
5092 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5093 return 1;
5094 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005095 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005096 int res;
5097 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5098 if (tmp == NULL) goto failed;
5099 res = obj2ast_int(tmp, &level, arena);
5100 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005101 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005102 } else {
5103 level = 0;
5104 }
5105 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5106 if (*out == NULL) goto failed;
5107 return 0;
5108 }
5109 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5110 if (isinstance == -1) {
5111 return 1;
5112 }
5113 if (isinstance) {
5114 asdl_seq* names;
5115
5116 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5117 int res;
5118 Py_ssize_t len;
5119 Py_ssize_t i;
5120 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5121 if (tmp == NULL) goto failed;
5122 if (!PyList_Check(tmp)) {
5123 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5124 goto failed;
5125 }
5126 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005127 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005128 if (names == NULL) goto failed;
5129 for (i = 0; i < len; i++) {
5130 identifier value;
5131 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5132 if (res != 0) goto failed;
5133 asdl_seq_SET(names, i, value);
5134 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005135 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005136 } else {
5137 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5138 return 1;
5139 }
5140 *out = Global(names, lineno, col_offset, arena);
5141 if (*out == NULL) goto failed;
5142 return 0;
5143 }
5144 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5145 if (isinstance == -1) {
5146 return 1;
5147 }
5148 if (isinstance) {
5149 asdl_seq* names;
5150
5151 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5152 int res;
5153 Py_ssize_t len;
5154 Py_ssize_t i;
5155 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5156 if (tmp == NULL) goto failed;
5157 if (!PyList_Check(tmp)) {
5158 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5159 goto failed;
5160 }
5161 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005162 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005163 if (names == NULL) goto failed;
5164 for (i = 0; i < len; i++) {
5165 identifier value;
5166 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5167 if (res != 0) goto failed;
5168 asdl_seq_SET(names, i, value);
5169 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005170 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005171 } else {
5172 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5173 return 1;
5174 }
5175 *out = Nonlocal(names, lineno, col_offset, arena);
5176 if (*out == NULL) goto failed;
5177 return 0;
5178 }
5179 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5180 if (isinstance == -1) {
5181 return 1;
5182 }
5183 if (isinstance) {
5184 expr_ty value;
5185
5186 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5187 int res;
5188 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5189 if (tmp == NULL) goto failed;
5190 res = obj2ast_expr(tmp, &value, arena);
5191 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005192 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005193 } else {
5194 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5195 return 1;
5196 }
5197 *out = Expr(value, lineno, col_offset, arena);
5198 if (*out == NULL) goto failed;
5199 return 0;
5200 }
5201 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5202 if (isinstance == -1) {
5203 return 1;
5204 }
5205 if (isinstance) {
5206
5207 *out = Pass(lineno, col_offset, arena);
5208 if (*out == NULL) goto failed;
5209 return 0;
5210 }
5211 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5212 if (isinstance == -1) {
5213 return 1;
5214 }
5215 if (isinstance) {
5216
5217 *out = Break(lineno, col_offset, arena);
5218 if (*out == NULL) goto failed;
5219 return 0;
5220 }
5221 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5222 if (isinstance == -1) {
5223 return 1;
5224 }
5225 if (isinstance) {
5226
5227 *out = Continue(lineno, col_offset, arena);
5228 if (*out == NULL) goto failed;
5229 return 0;
5230 }
5231
5232 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5233 failed:
5234 Py_XDECREF(tmp);
5235 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005236}
5237
5238int
5239obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5240{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005241 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005242
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005243 PyObject *tmp = NULL;
5244 int lineno;
5245 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005246
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005247 if (obj == Py_None) {
5248 *out = NULL;
5249 return 0;
5250 }
5251 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5252 int res;
5253 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5254 if (tmp == NULL) goto failed;
5255 res = obj2ast_int(tmp, &lineno, arena);
5256 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005257 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005258 } else {
5259 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005260 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005261 }
5262 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5263 int res;
5264 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5265 if (tmp == NULL) goto failed;
5266 res = obj2ast_int(tmp, &col_offset, arena);
5267 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005268 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005269 } else {
5270 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5271 return 1;
5272 }
5273 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5274 if (isinstance == -1) {
5275 return 1;
5276 }
5277 if (isinstance) {
5278 boolop_ty op;
5279 asdl_seq* values;
5280
5281 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5282 int res;
5283 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5284 if (tmp == NULL) goto failed;
5285 res = obj2ast_boolop(tmp, &op, arena);
5286 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005287 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005288 } else {
5289 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5290 return 1;
5291 }
5292 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5293 int res;
5294 Py_ssize_t len;
5295 Py_ssize_t i;
5296 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5297 if (tmp == NULL) goto failed;
5298 if (!PyList_Check(tmp)) {
5299 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5300 goto failed;
5301 }
5302 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005303 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005304 if (values == NULL) goto failed;
5305 for (i = 0; i < len; i++) {
5306 expr_ty value;
5307 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5308 if (res != 0) goto failed;
5309 asdl_seq_SET(values, i, value);
5310 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005311 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005312 } else {
5313 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5314 return 1;
5315 }
5316 *out = BoolOp(op, values, lineno, col_offset, arena);
5317 if (*out == NULL) goto failed;
5318 return 0;
5319 }
5320 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5321 if (isinstance == -1) {
5322 return 1;
5323 }
5324 if (isinstance) {
5325 expr_ty left;
5326 operator_ty op;
5327 expr_ty right;
5328
5329 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5330 int res;
5331 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5332 if (tmp == NULL) goto failed;
5333 res = obj2ast_expr(tmp, &left, arena);
5334 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005335 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005336 } else {
5337 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5338 return 1;
5339 }
5340 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5341 int res;
5342 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5343 if (tmp == NULL) goto failed;
5344 res = obj2ast_operator(tmp, &op, arena);
5345 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005346 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005347 } else {
5348 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5349 return 1;
5350 }
5351 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5352 int res;
5353 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5354 if (tmp == NULL) goto failed;
5355 res = obj2ast_expr(tmp, &right, arena);
5356 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005357 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005358 } else {
5359 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5360 return 1;
5361 }
5362 *out = BinOp(left, op, right, lineno, col_offset, arena);
5363 if (*out == NULL) goto failed;
5364 return 0;
5365 }
5366 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5367 if (isinstance == -1) {
5368 return 1;
5369 }
5370 if (isinstance) {
5371 unaryop_ty op;
5372 expr_ty operand;
5373
5374 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5375 int res;
5376 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5377 if (tmp == NULL) goto failed;
5378 res = obj2ast_unaryop(tmp, &op, arena);
5379 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005380 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005381 } else {
5382 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5383 return 1;
5384 }
5385 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5386 int res;
5387 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5388 if (tmp == NULL) goto failed;
5389 res = obj2ast_expr(tmp, &operand, arena);
5390 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005391 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005392 } else {
5393 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5394 return 1;
5395 }
5396 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5397 if (*out == NULL) goto failed;
5398 return 0;
5399 }
5400 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5401 if (isinstance == -1) {
5402 return 1;
5403 }
5404 if (isinstance) {
5405 arguments_ty args;
5406 expr_ty body;
5407
5408 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5409 int res;
5410 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5411 if (tmp == NULL) goto failed;
5412 res = obj2ast_arguments(tmp, &args, arena);
5413 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005414 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005415 } else {
5416 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5417 return 1;
5418 }
5419 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5420 int res;
5421 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5422 if (tmp == NULL) goto failed;
5423 res = obj2ast_expr(tmp, &body, arena);
5424 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005425 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005426 } else {
5427 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5428 return 1;
5429 }
5430 *out = Lambda(args, body, lineno, col_offset, arena);
5431 if (*out == NULL) goto failed;
5432 return 0;
5433 }
5434 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5435 if (isinstance == -1) {
5436 return 1;
5437 }
5438 if (isinstance) {
5439 expr_ty test;
5440 expr_ty body;
5441 expr_ty orelse;
5442
5443 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5444 int res;
5445 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5446 if (tmp == NULL) goto failed;
5447 res = obj2ast_expr(tmp, &test, arena);
5448 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005449 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005450 } else {
5451 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5452 return 1;
5453 }
5454 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5455 int res;
5456 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5457 if (tmp == NULL) goto failed;
5458 res = obj2ast_expr(tmp, &body, arena);
5459 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005460 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005461 } else {
5462 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5463 return 1;
5464 }
5465 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5466 int res;
5467 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5468 if (tmp == NULL) goto failed;
5469 res = obj2ast_expr(tmp, &orelse, arena);
5470 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005471 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005472 } else {
5473 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5474 return 1;
5475 }
5476 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5477 if (*out == NULL) goto failed;
5478 return 0;
5479 }
5480 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5481 if (isinstance == -1) {
5482 return 1;
5483 }
5484 if (isinstance) {
5485 asdl_seq* keys;
5486 asdl_seq* values;
5487
5488 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5489 int res;
5490 Py_ssize_t len;
5491 Py_ssize_t i;
5492 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5493 if (tmp == NULL) goto failed;
5494 if (!PyList_Check(tmp)) {
5495 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5496 goto failed;
5497 }
5498 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005499 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005500 if (keys == NULL) goto failed;
5501 for (i = 0; i < len; i++) {
5502 expr_ty value;
5503 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5504 if (res != 0) goto failed;
5505 asdl_seq_SET(keys, i, value);
5506 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005507 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005508 } else {
5509 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5510 return 1;
5511 }
5512 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5513 int res;
5514 Py_ssize_t len;
5515 Py_ssize_t i;
5516 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5517 if (tmp == NULL) goto failed;
5518 if (!PyList_Check(tmp)) {
5519 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5520 goto failed;
5521 }
5522 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005523 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005524 if (values == NULL) goto failed;
5525 for (i = 0; i < len; i++) {
5526 expr_ty value;
5527 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5528 if (res != 0) goto failed;
5529 asdl_seq_SET(values, i, value);
5530 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005531 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005532 } else {
5533 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5534 return 1;
5535 }
5536 *out = Dict(keys, values, lineno, col_offset, arena);
5537 if (*out == NULL) goto failed;
5538 return 0;
5539 }
5540 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5541 if (isinstance == -1) {
5542 return 1;
5543 }
5544 if (isinstance) {
5545 asdl_seq* elts;
5546
5547 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5548 int res;
5549 Py_ssize_t len;
5550 Py_ssize_t i;
5551 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5552 if (tmp == NULL) goto failed;
5553 if (!PyList_Check(tmp)) {
5554 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5555 goto failed;
5556 }
5557 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005558 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005559 if (elts == NULL) goto failed;
5560 for (i = 0; i < len; i++) {
5561 expr_ty value;
5562 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5563 if (res != 0) goto failed;
5564 asdl_seq_SET(elts, i, value);
5565 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005566 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005567 } else {
5568 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5569 return 1;
5570 }
5571 *out = Set(elts, lineno, col_offset, arena);
5572 if (*out == NULL) goto failed;
5573 return 0;
5574 }
5575 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5576 if (isinstance == -1) {
5577 return 1;
5578 }
5579 if (isinstance) {
5580 expr_ty elt;
5581 asdl_seq* generators;
5582
5583 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5584 int res;
5585 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5586 if (tmp == NULL) goto failed;
5587 res = obj2ast_expr(tmp, &elt, arena);
5588 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005590 } else {
5591 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5592 return 1;
5593 }
5594 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5595 int res;
5596 Py_ssize_t len;
5597 Py_ssize_t i;
5598 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5599 if (tmp == NULL) goto failed;
5600 if (!PyList_Check(tmp)) {
5601 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5602 goto failed;
5603 }
5604 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005605 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005606 if (generators == NULL) goto failed;
5607 for (i = 0; i < len; i++) {
5608 comprehension_ty value;
5609 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5610 if (res != 0) goto failed;
5611 asdl_seq_SET(generators, i, value);
5612 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005613 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005614 } else {
5615 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5616 return 1;
5617 }
5618 *out = ListComp(elt, generators, lineno, col_offset, arena);
5619 if (*out == NULL) goto failed;
5620 return 0;
5621 }
5622 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5623 if (isinstance == -1) {
5624 return 1;
5625 }
5626 if (isinstance) {
5627 expr_ty elt;
5628 asdl_seq* generators;
5629
5630 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5631 int res;
5632 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5633 if (tmp == NULL) goto failed;
5634 res = obj2ast_expr(tmp, &elt, arena);
5635 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005636 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005637 } else {
5638 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5639 return 1;
5640 }
5641 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5642 int res;
5643 Py_ssize_t len;
5644 Py_ssize_t i;
5645 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5646 if (tmp == NULL) goto failed;
5647 if (!PyList_Check(tmp)) {
5648 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5649 goto failed;
5650 }
5651 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005652 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005653 if (generators == NULL) goto failed;
5654 for (i = 0; i < len; i++) {
5655 comprehension_ty value;
5656 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5657 if (res != 0) goto failed;
5658 asdl_seq_SET(generators, i, value);
5659 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005660 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005661 } else {
5662 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5663 return 1;
5664 }
5665 *out = SetComp(elt, generators, lineno, col_offset, arena);
5666 if (*out == NULL) goto failed;
5667 return 0;
5668 }
5669 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5670 if (isinstance == -1) {
5671 return 1;
5672 }
5673 if (isinstance) {
5674 expr_ty key;
5675 expr_ty value;
5676 asdl_seq* generators;
5677
5678 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5679 int res;
5680 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5681 if (tmp == NULL) goto failed;
5682 res = obj2ast_expr(tmp, &key, arena);
5683 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005684 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005685 } else {
5686 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5687 return 1;
5688 }
5689 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5690 int res;
5691 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5692 if (tmp == NULL) goto failed;
5693 res = obj2ast_expr(tmp, &value, arena);
5694 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005696 } else {
5697 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5698 return 1;
5699 }
5700 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5701 int res;
5702 Py_ssize_t len;
5703 Py_ssize_t i;
5704 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5705 if (tmp == NULL) goto failed;
5706 if (!PyList_Check(tmp)) {
5707 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5708 goto failed;
5709 }
5710 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005711 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005712 if (generators == NULL) goto failed;
5713 for (i = 0; i < len; i++) {
5714 comprehension_ty value;
5715 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5716 if (res != 0) goto failed;
5717 asdl_seq_SET(generators, i, value);
5718 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005719 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005720 } else {
5721 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5722 return 1;
5723 }
5724 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5725 if (*out == NULL) goto failed;
5726 return 0;
5727 }
5728 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5729 if (isinstance == -1) {
5730 return 1;
5731 }
5732 if (isinstance) {
5733 expr_ty elt;
5734 asdl_seq* generators;
5735
5736 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5737 int res;
5738 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5739 if (tmp == NULL) goto failed;
5740 res = obj2ast_expr(tmp, &elt, arena);
5741 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005742 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005743 } else {
5744 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5745 return 1;
5746 }
5747 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5748 int res;
5749 Py_ssize_t len;
5750 Py_ssize_t i;
5751 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5752 if (tmp == NULL) goto failed;
5753 if (!PyList_Check(tmp)) {
5754 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5755 goto failed;
5756 }
5757 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005758 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005759 if (generators == NULL) goto failed;
5760 for (i = 0; i < len; i++) {
5761 comprehension_ty value;
5762 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5763 if (res != 0) goto failed;
5764 asdl_seq_SET(generators, i, value);
5765 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005767 } else {
5768 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5769 return 1;
5770 }
5771 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5772 if (*out == NULL) goto failed;
5773 return 0;
5774 }
Yury Selivanov75445082015-05-11 22:57:16 -04005775 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5776 if (isinstance == -1) {
5777 return 1;
5778 }
5779 if (isinstance) {
5780 expr_ty value;
5781
5782 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5783 int res;
5784 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5785 if (tmp == NULL) goto failed;
5786 res = obj2ast_expr(tmp, &value, arena);
5787 if (res != 0) goto failed;
5788 Py_CLEAR(tmp);
5789 } else {
5790 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5791 return 1;
5792 }
5793 *out = Await(value, lineno, col_offset, arena);
5794 if (*out == NULL) goto failed;
5795 return 0;
5796 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005797 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5798 if (isinstance == -1) {
5799 return 1;
5800 }
5801 if (isinstance) {
5802 expr_ty value;
5803
Victor Stinneree4b59c2013-07-27 00:01:35 +02005804 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005805 int res;
5806 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5807 if (tmp == NULL) goto failed;
5808 res = obj2ast_expr(tmp, &value, arena);
5809 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005810 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005811 } else {
5812 value = NULL;
5813 }
5814 *out = Yield(value, lineno, col_offset, arena);
5815 if (*out == NULL) goto failed;
5816 return 0;
5817 }
5818 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5819 if (isinstance == -1) {
5820 return 1;
5821 }
5822 if (isinstance) {
5823 expr_ty value;
5824
5825 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5826 int res;
5827 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5828 if (tmp == NULL) goto failed;
5829 res = obj2ast_expr(tmp, &value, arena);
5830 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005831 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005832 } else {
5833 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5834 return 1;
5835 }
5836 *out = YieldFrom(value, lineno, col_offset, arena);
5837 if (*out == NULL) goto failed;
5838 return 0;
5839 }
5840 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5841 if (isinstance == -1) {
5842 return 1;
5843 }
5844 if (isinstance) {
5845 expr_ty left;
5846 asdl_int_seq* ops;
5847 asdl_seq* comparators;
5848
5849 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5850 int res;
5851 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5852 if (tmp == NULL) goto failed;
5853 res = obj2ast_expr(tmp, &left, arena);
5854 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005855 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005856 } else {
5857 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5858 return 1;
5859 }
5860 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5861 int res;
5862 Py_ssize_t len;
5863 Py_ssize_t i;
5864 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5865 if (tmp == NULL) goto failed;
5866 if (!PyList_Check(tmp)) {
5867 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5868 goto failed;
5869 }
5870 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005871 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005872 if (ops == NULL) goto failed;
5873 for (i = 0; i < len; i++) {
5874 cmpop_ty value;
5875 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5876 if (res != 0) goto failed;
5877 asdl_seq_SET(ops, i, value);
5878 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005879 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005880 } else {
5881 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5882 return 1;
5883 }
5884 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5885 int res;
5886 Py_ssize_t len;
5887 Py_ssize_t i;
5888 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5889 if (tmp == NULL) goto failed;
5890 if (!PyList_Check(tmp)) {
5891 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5892 goto failed;
5893 }
5894 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005895 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005896 if (comparators == NULL) goto failed;
5897 for (i = 0; i < len; i++) {
5898 expr_ty value;
5899 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5900 if (res != 0) goto failed;
5901 asdl_seq_SET(comparators, i, value);
5902 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005903 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005904 } else {
5905 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5906 return 1;
5907 }
5908 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5909 if (*out == NULL) goto failed;
5910 return 0;
5911 }
5912 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5913 if (isinstance == -1) {
5914 return 1;
5915 }
5916 if (isinstance) {
5917 expr_ty func;
5918 asdl_seq* args;
5919 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005920
5921 if (_PyObject_HasAttrId(obj, &PyId_func)) {
5922 int res;
5923 tmp = _PyObject_GetAttrId(obj, &PyId_func);
5924 if (tmp == NULL) goto failed;
5925 res = obj2ast_expr(tmp, &func, arena);
5926 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005927 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005928 } else {
5929 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5930 return 1;
5931 }
5932 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5933 int res;
5934 Py_ssize_t len;
5935 Py_ssize_t i;
5936 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5937 if (tmp == NULL) goto failed;
5938 if (!PyList_Check(tmp)) {
5939 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5940 goto failed;
5941 }
5942 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005943 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005944 if (args == NULL) goto failed;
5945 for (i = 0; i < len; i++) {
5946 expr_ty value;
5947 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5948 if (res != 0) goto failed;
5949 asdl_seq_SET(args, i, value);
5950 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005951 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005952 } else {
5953 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5954 return 1;
5955 }
5956 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
5957 int res;
5958 Py_ssize_t len;
5959 Py_ssize_t i;
5960 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
5961 if (tmp == NULL) goto failed;
5962 if (!PyList_Check(tmp)) {
5963 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5964 goto failed;
5965 }
5966 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005967 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005968 if (keywords == NULL) goto failed;
5969 for (i = 0; i < len; i++) {
5970 keyword_ty value;
5971 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5972 if (res != 0) goto failed;
5973 asdl_seq_SET(keywords, i, value);
5974 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005975 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005976 } else {
5977 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5978 return 1;
5979 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005980 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005981 if (*out == NULL) goto failed;
5982 return 0;
5983 }
5984 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5985 if (isinstance == -1) {
5986 return 1;
5987 }
5988 if (isinstance) {
5989 object n;
5990
5991 if (_PyObject_HasAttrId(obj, &PyId_n)) {
5992 int res;
5993 tmp = _PyObject_GetAttrId(obj, &PyId_n);
5994 if (tmp == NULL) goto failed;
5995 res = obj2ast_object(tmp, &n, arena);
5996 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005997 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005998 } else {
5999 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6000 return 1;
6001 }
6002 *out = Num(n, lineno, col_offset, arena);
6003 if (*out == NULL) goto failed;
6004 return 0;
6005 }
6006 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6007 if (isinstance == -1) {
6008 return 1;
6009 }
6010 if (isinstance) {
6011 string s;
6012
6013 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6014 int res;
6015 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6016 if (tmp == NULL) goto failed;
6017 res = obj2ast_string(tmp, &s, arena);
6018 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006019 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006020 } else {
6021 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6022 return 1;
6023 }
6024 *out = Str(s, lineno, col_offset, arena);
6025 if (*out == NULL) goto failed;
6026 return 0;
6027 }
6028 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6029 if (isinstance == -1) {
6030 return 1;
6031 }
6032 if (isinstance) {
6033 bytes s;
6034
6035 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6036 int res;
6037 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6038 if (tmp == NULL) goto failed;
6039 res = obj2ast_bytes(tmp, &s, arena);
6040 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006041 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006042 } else {
6043 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6044 return 1;
6045 }
6046 *out = Bytes(s, lineno, col_offset, arena);
6047 if (*out == NULL) goto failed;
6048 return 0;
6049 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006050 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6051 if (isinstance == -1) {
6052 return 1;
6053 }
6054 if (isinstance) {
6055 singleton value;
6056
6057 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6058 int res;
6059 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6060 if (tmp == NULL) goto failed;
6061 res = obj2ast_singleton(tmp, &value, arena);
6062 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006063 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006064 } else {
6065 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6066 return 1;
6067 }
6068 *out = NameConstant(value, lineno, col_offset, arena);
6069 if (*out == NULL) goto failed;
6070 return 0;
6071 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006072 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6073 if (isinstance == -1) {
6074 return 1;
6075 }
6076 if (isinstance) {
6077
6078 *out = Ellipsis(lineno, col_offset, arena);
6079 if (*out == NULL) goto failed;
6080 return 0;
6081 }
6082 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6083 if (isinstance == -1) {
6084 return 1;
6085 }
6086 if (isinstance) {
6087 expr_ty value;
6088 identifier attr;
6089 expr_context_ty ctx;
6090
6091 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6092 int res;
6093 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6094 if (tmp == NULL) goto failed;
6095 res = obj2ast_expr(tmp, &value, arena);
6096 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006097 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006098 } else {
6099 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6100 return 1;
6101 }
6102 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6103 int res;
6104 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6105 if (tmp == NULL) goto failed;
6106 res = obj2ast_identifier(tmp, &attr, arena);
6107 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006108 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006109 } else {
6110 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6111 return 1;
6112 }
6113 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6114 int res;
6115 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6116 if (tmp == NULL) goto failed;
6117 res = obj2ast_expr_context(tmp, &ctx, arena);
6118 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006119 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006120 } else {
6121 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6122 return 1;
6123 }
6124 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6125 if (*out == NULL) goto failed;
6126 return 0;
6127 }
6128 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6129 if (isinstance == -1) {
6130 return 1;
6131 }
6132 if (isinstance) {
6133 expr_ty value;
6134 slice_ty slice;
6135 expr_context_ty ctx;
6136
6137 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6138 int res;
6139 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6140 if (tmp == NULL) goto failed;
6141 res = obj2ast_expr(tmp, &value, arena);
6142 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006143 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006144 } else {
6145 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6146 return 1;
6147 }
6148 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6149 int res;
6150 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6151 if (tmp == NULL) goto failed;
6152 res = obj2ast_slice(tmp, &slice, arena);
6153 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006154 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006155 } else {
6156 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6157 return 1;
6158 }
6159 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6160 int res;
6161 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6162 if (tmp == NULL) goto failed;
6163 res = obj2ast_expr_context(tmp, &ctx, arena);
6164 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006165 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006166 } else {
6167 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6168 return 1;
6169 }
6170 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6171 if (*out == NULL) goto failed;
6172 return 0;
6173 }
6174 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6175 if (isinstance == -1) {
6176 return 1;
6177 }
6178 if (isinstance) {
6179 expr_ty value;
6180 expr_context_ty ctx;
6181
6182 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6183 int res;
6184 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6185 if (tmp == NULL) goto failed;
6186 res = obj2ast_expr(tmp, &value, arena);
6187 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006188 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006189 } else {
6190 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6191 return 1;
6192 }
6193 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6194 int res;
6195 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6196 if (tmp == NULL) goto failed;
6197 res = obj2ast_expr_context(tmp, &ctx, arena);
6198 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006200 } else {
6201 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6202 return 1;
6203 }
6204 *out = Starred(value, ctx, lineno, col_offset, arena);
6205 if (*out == NULL) goto failed;
6206 return 0;
6207 }
6208 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6209 if (isinstance == -1) {
6210 return 1;
6211 }
6212 if (isinstance) {
6213 identifier id;
6214 expr_context_ty ctx;
6215
6216 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6217 int res;
6218 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6219 if (tmp == NULL) goto failed;
6220 res = obj2ast_identifier(tmp, &id, arena);
6221 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006222 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006223 } else {
6224 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6225 return 1;
6226 }
6227 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6228 int res;
6229 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6230 if (tmp == NULL) goto failed;
6231 res = obj2ast_expr_context(tmp, &ctx, arena);
6232 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006233 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006234 } else {
6235 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6236 return 1;
6237 }
6238 *out = Name(id, ctx, lineno, col_offset, arena);
6239 if (*out == NULL) goto failed;
6240 return 0;
6241 }
6242 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6243 if (isinstance == -1) {
6244 return 1;
6245 }
6246 if (isinstance) {
6247 asdl_seq* elts;
6248 expr_context_ty ctx;
6249
6250 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6251 int res;
6252 Py_ssize_t len;
6253 Py_ssize_t i;
6254 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6255 if (tmp == NULL) goto failed;
6256 if (!PyList_Check(tmp)) {
6257 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6258 goto failed;
6259 }
6260 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006261 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006262 if (elts == NULL) goto failed;
6263 for (i = 0; i < len; i++) {
6264 expr_ty value;
6265 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6266 if (res != 0) goto failed;
6267 asdl_seq_SET(elts, i, value);
6268 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006269 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006270 } else {
6271 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6272 return 1;
6273 }
6274 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6275 int res;
6276 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6277 if (tmp == NULL) goto failed;
6278 res = obj2ast_expr_context(tmp, &ctx, arena);
6279 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006280 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006281 } else {
6282 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6283 return 1;
6284 }
6285 *out = List(elts, ctx, lineno, col_offset, arena);
6286 if (*out == NULL) goto failed;
6287 return 0;
6288 }
6289 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6290 if (isinstance == -1) {
6291 return 1;
6292 }
6293 if (isinstance) {
6294 asdl_seq* elts;
6295 expr_context_ty ctx;
6296
6297 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6298 int res;
6299 Py_ssize_t len;
6300 Py_ssize_t i;
6301 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6302 if (tmp == NULL) goto failed;
6303 if (!PyList_Check(tmp)) {
6304 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6305 goto failed;
6306 }
6307 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006308 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006309 if (elts == NULL) goto failed;
6310 for (i = 0; i < len; i++) {
6311 expr_ty value;
6312 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6313 if (res != 0) goto failed;
6314 asdl_seq_SET(elts, i, value);
6315 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006316 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006317 } else {
6318 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6319 return 1;
6320 }
6321 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6322 int res;
6323 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6324 if (tmp == NULL) goto failed;
6325 res = obj2ast_expr_context(tmp, &ctx, arena);
6326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006327 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006328 } else {
6329 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6330 return 1;
6331 }
6332 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6333 if (*out == NULL) goto failed;
6334 return 0;
6335 }
6336
6337 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6338 failed:
6339 Py_XDECREF(tmp);
6340 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006341}
6342
6343int
6344obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6345{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006346 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006347
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006348 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6349 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006350 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006351 }
6352 if (isinstance) {
6353 *out = Load;
6354 return 0;
6355 }
6356 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6357 if (isinstance == -1) {
6358 return 1;
6359 }
6360 if (isinstance) {
6361 *out = Store;
6362 return 0;
6363 }
6364 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6365 if (isinstance == -1) {
6366 return 1;
6367 }
6368 if (isinstance) {
6369 *out = Del;
6370 return 0;
6371 }
6372 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6373 if (isinstance == -1) {
6374 return 1;
6375 }
6376 if (isinstance) {
6377 *out = AugLoad;
6378 return 0;
6379 }
6380 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6381 if (isinstance == -1) {
6382 return 1;
6383 }
6384 if (isinstance) {
6385 *out = AugStore;
6386 return 0;
6387 }
6388 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6389 if (isinstance == -1) {
6390 return 1;
6391 }
6392 if (isinstance) {
6393 *out = Param;
6394 return 0;
6395 }
6396
6397 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6398 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006399}
6400
6401int
6402obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6403{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006404 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006405
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006406 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006407
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006408 if (obj == Py_None) {
6409 *out = NULL;
6410 return 0;
6411 }
6412 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6413 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006414 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006415 }
6416 if (isinstance) {
6417 expr_ty lower;
6418 expr_ty upper;
6419 expr_ty step;
6420
Victor Stinneree4b59c2013-07-27 00:01:35 +02006421 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006422 int res;
6423 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6424 if (tmp == NULL) goto failed;
6425 res = obj2ast_expr(tmp, &lower, arena);
6426 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006427 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006428 } else {
6429 lower = NULL;
6430 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006431 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006432 int res;
6433 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6434 if (tmp == NULL) goto failed;
6435 res = obj2ast_expr(tmp, &upper, arena);
6436 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006437 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006438 } else {
6439 upper = NULL;
6440 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006441 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006442 int res;
6443 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6444 if (tmp == NULL) goto failed;
6445 res = obj2ast_expr(tmp, &step, arena);
6446 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006447 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006448 } else {
6449 step = NULL;
6450 }
6451 *out = Slice(lower, upper, step, arena);
6452 if (*out == NULL) goto failed;
6453 return 0;
6454 }
6455 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6456 if (isinstance == -1) {
6457 return 1;
6458 }
6459 if (isinstance) {
6460 asdl_seq* dims;
6461
6462 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6463 int res;
6464 Py_ssize_t len;
6465 Py_ssize_t i;
6466 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6467 if (tmp == NULL) goto failed;
6468 if (!PyList_Check(tmp)) {
6469 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6470 goto failed;
6471 }
6472 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006473 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006474 if (dims == NULL) goto failed;
6475 for (i = 0; i < len; i++) {
6476 slice_ty value;
6477 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6478 if (res != 0) goto failed;
6479 asdl_seq_SET(dims, i, value);
6480 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006481 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006482 } else {
6483 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6484 return 1;
6485 }
6486 *out = ExtSlice(dims, arena);
6487 if (*out == NULL) goto failed;
6488 return 0;
6489 }
6490 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6491 if (isinstance == -1) {
6492 return 1;
6493 }
6494 if (isinstance) {
6495 expr_ty value;
6496
6497 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6498 int res;
6499 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6500 if (tmp == NULL) goto failed;
6501 res = obj2ast_expr(tmp, &value, arena);
6502 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006503 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 } else {
6505 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6506 return 1;
6507 }
6508 *out = Index(value, arena);
6509 if (*out == NULL) goto failed;
6510 return 0;
6511 }
6512
6513 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6514 failed:
6515 Py_XDECREF(tmp);
6516 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006517}
6518
6519int
6520obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6521{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006522 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006523
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006524 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6525 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006526 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006527 }
6528 if (isinstance) {
6529 *out = And;
6530 return 0;
6531 }
6532 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6533 if (isinstance == -1) {
6534 return 1;
6535 }
6536 if (isinstance) {
6537 *out = Or;
6538 return 0;
6539 }
6540
6541 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6542 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006543}
6544
6545int
6546obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6547{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006548 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006549
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006550 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6551 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006552 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006553 }
6554 if (isinstance) {
6555 *out = Add;
6556 return 0;
6557 }
6558 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6559 if (isinstance == -1) {
6560 return 1;
6561 }
6562 if (isinstance) {
6563 *out = Sub;
6564 return 0;
6565 }
6566 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6567 if (isinstance == -1) {
6568 return 1;
6569 }
6570 if (isinstance) {
6571 *out = Mult;
6572 return 0;
6573 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006574 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6575 if (isinstance == -1) {
6576 return 1;
6577 }
6578 if (isinstance) {
6579 *out = MatMult;
6580 return 0;
6581 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006582 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6583 if (isinstance == -1) {
6584 return 1;
6585 }
6586 if (isinstance) {
6587 *out = Div;
6588 return 0;
6589 }
6590 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6591 if (isinstance == -1) {
6592 return 1;
6593 }
6594 if (isinstance) {
6595 *out = Mod;
6596 return 0;
6597 }
6598 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6599 if (isinstance == -1) {
6600 return 1;
6601 }
6602 if (isinstance) {
6603 *out = Pow;
6604 return 0;
6605 }
6606 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6607 if (isinstance == -1) {
6608 return 1;
6609 }
6610 if (isinstance) {
6611 *out = LShift;
6612 return 0;
6613 }
6614 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6615 if (isinstance == -1) {
6616 return 1;
6617 }
6618 if (isinstance) {
6619 *out = RShift;
6620 return 0;
6621 }
6622 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6623 if (isinstance == -1) {
6624 return 1;
6625 }
6626 if (isinstance) {
6627 *out = BitOr;
6628 return 0;
6629 }
6630 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6631 if (isinstance == -1) {
6632 return 1;
6633 }
6634 if (isinstance) {
6635 *out = BitXor;
6636 return 0;
6637 }
6638 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6639 if (isinstance == -1) {
6640 return 1;
6641 }
6642 if (isinstance) {
6643 *out = BitAnd;
6644 return 0;
6645 }
6646 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6647 if (isinstance == -1) {
6648 return 1;
6649 }
6650 if (isinstance) {
6651 *out = FloorDiv;
6652 return 0;
6653 }
6654
6655 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6656 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006657}
6658
6659int
6660obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6661{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006663
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006664 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6665 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006666 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006667 }
6668 if (isinstance) {
6669 *out = Invert;
6670 return 0;
6671 }
6672 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6673 if (isinstance == -1) {
6674 return 1;
6675 }
6676 if (isinstance) {
6677 *out = Not;
6678 return 0;
6679 }
6680 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6681 if (isinstance == -1) {
6682 return 1;
6683 }
6684 if (isinstance) {
6685 *out = UAdd;
6686 return 0;
6687 }
6688 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6689 if (isinstance == -1) {
6690 return 1;
6691 }
6692 if (isinstance) {
6693 *out = USub;
6694 return 0;
6695 }
6696
6697 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6698 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006699}
6700
6701int
6702obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6703{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006704 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006705
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006706 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6707 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006708 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006709 }
6710 if (isinstance) {
6711 *out = Eq;
6712 return 0;
6713 }
6714 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6715 if (isinstance == -1) {
6716 return 1;
6717 }
6718 if (isinstance) {
6719 *out = NotEq;
6720 return 0;
6721 }
6722 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6723 if (isinstance == -1) {
6724 return 1;
6725 }
6726 if (isinstance) {
6727 *out = Lt;
6728 return 0;
6729 }
6730 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6731 if (isinstance == -1) {
6732 return 1;
6733 }
6734 if (isinstance) {
6735 *out = LtE;
6736 return 0;
6737 }
6738 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6739 if (isinstance == -1) {
6740 return 1;
6741 }
6742 if (isinstance) {
6743 *out = Gt;
6744 return 0;
6745 }
6746 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6747 if (isinstance == -1) {
6748 return 1;
6749 }
6750 if (isinstance) {
6751 *out = GtE;
6752 return 0;
6753 }
6754 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6755 if (isinstance == -1) {
6756 return 1;
6757 }
6758 if (isinstance) {
6759 *out = Is;
6760 return 0;
6761 }
6762 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6763 if (isinstance == -1) {
6764 return 1;
6765 }
6766 if (isinstance) {
6767 *out = IsNot;
6768 return 0;
6769 }
6770 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6771 if (isinstance == -1) {
6772 return 1;
6773 }
6774 if (isinstance) {
6775 *out = In;
6776 return 0;
6777 }
6778 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6779 if (isinstance == -1) {
6780 return 1;
6781 }
6782 if (isinstance) {
6783 *out = NotIn;
6784 return 0;
6785 }
6786
6787 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6788 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006789}
6790
6791int
6792obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6793{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006794 PyObject* tmp = NULL;
6795 expr_ty target;
6796 expr_ty iter;
6797 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006798
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006799 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6800 int res;
6801 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6802 if (tmp == NULL) goto failed;
6803 res = obj2ast_expr(tmp, &target, arena);
6804 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006805 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006806 } else {
6807 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006808 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006809 }
6810 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6811 int res;
6812 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6813 if (tmp == NULL) goto failed;
6814 res = obj2ast_expr(tmp, &iter, arena);
6815 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006816 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006817 } else {
6818 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6819 return 1;
6820 }
6821 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6822 int res;
6823 Py_ssize_t len;
6824 Py_ssize_t i;
6825 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6826 if (tmp == NULL) goto failed;
6827 if (!PyList_Check(tmp)) {
6828 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6829 goto failed;
6830 }
6831 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006832 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006833 if (ifs == NULL) goto failed;
6834 for (i = 0; i < len; i++) {
6835 expr_ty value;
6836 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6837 if (res != 0) goto failed;
6838 asdl_seq_SET(ifs, i, value);
6839 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006840 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006841 } else {
6842 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6843 return 1;
6844 }
6845 *out = comprehension(target, iter, ifs, arena);
6846 return 0;
6847failed:
6848 Py_XDECREF(tmp);
6849 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006850}
6851
6852int
6853obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6854{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006855 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00006856
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006857 PyObject *tmp = NULL;
6858 int lineno;
6859 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006860
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006861 if (obj == Py_None) {
6862 *out = NULL;
6863 return 0;
6864 }
6865 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
6866 int res;
6867 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
6868 if (tmp == NULL) goto failed;
6869 res = obj2ast_int(tmp, &lineno, arena);
6870 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006871 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006872 } else {
6873 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006874 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006875 }
6876 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
6877 int res;
6878 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
6879 if (tmp == NULL) goto failed;
6880 res = obj2ast_int(tmp, &col_offset, arena);
6881 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006882 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006883 } else {
6884 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6885 return 1;
6886 }
6887 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6888 if (isinstance == -1) {
6889 return 1;
6890 }
6891 if (isinstance) {
6892 expr_ty type;
6893 identifier name;
6894 asdl_seq* body;
6895
Victor Stinneree4b59c2013-07-27 00:01:35 +02006896 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006897 int res;
6898 tmp = _PyObject_GetAttrId(obj, &PyId_type);
6899 if (tmp == NULL) goto failed;
6900 res = obj2ast_expr(tmp, &type, arena);
6901 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006902 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006903 } else {
6904 type = NULL;
6905 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006906 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006907 int res;
6908 tmp = _PyObject_GetAttrId(obj, &PyId_name);
6909 if (tmp == NULL) goto failed;
6910 res = obj2ast_identifier(tmp, &name, arena);
6911 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006912 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006913 } else {
6914 name = NULL;
6915 }
6916 if (_PyObject_HasAttrId(obj, &PyId_body)) {
6917 int res;
6918 Py_ssize_t len;
6919 Py_ssize_t i;
6920 tmp = _PyObject_GetAttrId(obj, &PyId_body);
6921 if (tmp == NULL) goto failed;
6922 if (!PyList_Check(tmp)) {
6923 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6924 goto failed;
6925 }
6926 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006927 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006928 if (body == NULL) goto failed;
6929 for (i = 0; i < len; i++) {
6930 stmt_ty value;
6931 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6932 if (res != 0) goto failed;
6933 asdl_seq_SET(body, i, value);
6934 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006935 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006936 } else {
6937 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6938 return 1;
6939 }
6940 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
6941 if (*out == NULL) goto failed;
6942 return 0;
6943 }
6944
6945 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6946 failed:
6947 Py_XDECREF(tmp);
6948 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006949}
6950
6951int
6952obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6953{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006954 PyObject* tmp = NULL;
6955 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006956 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006957 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006958 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006959 arg_ty kwarg;
6960 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006961
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006962 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6963 int res;
6964 Py_ssize_t len;
6965 Py_ssize_t i;
6966 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6967 if (tmp == NULL) goto failed;
6968 if (!PyList_Check(tmp)) {
6969 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6970 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006971 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006972 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006973 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006974 if (args == NULL) goto failed;
6975 for (i = 0; i < len; i++) {
6976 arg_ty value;
6977 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6978 if (res != 0) goto failed;
6979 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006980 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006981 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006982 } else {
6983 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006984 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006985 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006986 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006987 int res;
6988 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
6989 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02006990 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006991 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006992 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006993 } else {
6994 vararg = NULL;
6995 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006996 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
6997 int res;
6998 Py_ssize_t len;
6999 Py_ssize_t i;
7000 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7001 if (tmp == NULL) goto failed;
7002 if (!PyList_Check(tmp)) {
7003 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7004 goto failed;
7005 }
7006 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007007 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007008 if (kwonlyargs == NULL) goto failed;
7009 for (i = 0; i < len; i++) {
7010 arg_ty value;
7011 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7012 if (res != 0) goto failed;
7013 asdl_seq_SET(kwonlyargs, i, value);
7014 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007015 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007016 } else {
7017 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7018 return 1;
7019 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007020 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7021 int res;
7022 Py_ssize_t len;
7023 Py_ssize_t i;
7024 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7025 if (tmp == NULL) goto failed;
7026 if (!PyList_Check(tmp)) {
7027 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7028 goto failed;
7029 }
7030 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007031 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007032 if (kw_defaults == NULL) goto failed;
7033 for (i = 0; i < len; i++) {
7034 expr_ty value;
7035 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7036 if (res != 0) goto failed;
7037 asdl_seq_SET(kw_defaults, i, value);
7038 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007039 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007040 } else {
7041 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7042 return 1;
7043 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007044 if (exists_not_none(obj, &PyId_kwarg)) {
7045 int res;
7046 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7047 if (tmp == NULL) goto failed;
7048 res = obj2ast_arg(tmp, &kwarg, arena);
7049 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007050 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007051 } else {
7052 kwarg = NULL;
7053 }
7054 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7055 int res;
7056 Py_ssize_t len;
7057 Py_ssize_t i;
7058 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7059 if (tmp == NULL) goto failed;
7060 if (!PyList_Check(tmp)) {
7061 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7062 goto failed;
7063 }
7064 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007065 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007066 if (defaults == NULL) goto failed;
7067 for (i = 0; i < len; i++) {
7068 expr_ty value;
7069 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7070 if (res != 0) goto failed;
7071 asdl_seq_SET(defaults, i, value);
7072 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007073 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007074 } else {
7075 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7076 return 1;
7077 }
7078 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7079 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007080 return 0;
7081failed:
7082 Py_XDECREF(tmp);
7083 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007084}
7085
7086int
7087obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7088{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007089 PyObject* tmp = NULL;
7090 identifier arg;
7091 expr_ty annotation;
Victor Stinnerf9827ea2015-11-06 17:01:48 +01007092 int lineno;
7093 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007094
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007095 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7096 int res;
7097 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7098 if (tmp == NULL) goto failed;
7099 res = obj2ast_identifier(tmp, &arg, arena);
7100 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007101 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007102 } else {
7103 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007104 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007105 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007106 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007107 int res;
7108 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7109 if (tmp == NULL) goto failed;
7110 res = obj2ast_expr(tmp, &annotation, arena);
7111 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007112 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007113 } else {
7114 annotation = NULL;
7115 }
Victor Stinnerf9827ea2015-11-06 17:01:48 +01007116 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7117 int res;
7118 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7119 if (tmp == NULL) goto failed;
7120 res = obj2ast_int(tmp, &lineno, arena);
7121 if (res != 0) goto failed;
7122 Py_CLEAR(tmp);
7123 } else {
7124 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7125 return 1;
7126 }
7127 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7128 int res;
7129 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7130 if (tmp == NULL) goto failed;
7131 res = obj2ast_int(tmp, &col_offset, arena);
7132 if (res != 0) goto failed;
7133 Py_CLEAR(tmp);
7134 } else {
7135 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7136 return 1;
7137 }
7138 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007139 return 0;
7140failed:
7141 Py_XDECREF(tmp);
7142 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007143}
7144
7145int
7146obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7147{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007148 PyObject* tmp = NULL;
7149 identifier arg;
7150 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007151
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007152 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007153 int res;
7154 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7155 if (tmp == NULL) goto failed;
7156 res = obj2ast_identifier(tmp, &arg, arena);
7157 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007158 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007159 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007160 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007161 }
7162 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7163 int res;
7164 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7165 if (tmp == NULL) goto failed;
7166 res = obj2ast_expr(tmp, &value, arena);
7167 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007168 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007169 } else {
7170 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7171 return 1;
7172 }
7173 *out = keyword(arg, value, arena);
7174 return 0;
7175failed:
7176 Py_XDECREF(tmp);
7177 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007178}
7179
7180int
7181obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7182{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007183 PyObject* tmp = NULL;
7184 identifier name;
7185 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007186
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007187 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7188 int res;
7189 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7190 if (tmp == NULL) goto failed;
7191 res = obj2ast_identifier(tmp, &name, arena);
7192 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007193 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007194 } else {
7195 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007196 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007197 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007198 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007199 int res;
7200 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7201 if (tmp == NULL) goto failed;
7202 res = obj2ast_identifier(tmp, &asname, arena);
7203 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007204 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007205 } else {
7206 asname = NULL;
7207 }
7208 *out = alias(name, asname, arena);
7209 return 0;
7210failed:
7211 Py_XDECREF(tmp);
7212 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007213}
7214
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007215int
7216obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7217{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007218 PyObject* tmp = NULL;
7219 expr_ty context_expr;
7220 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007221
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007222 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7223 int res;
7224 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7225 if (tmp == NULL) goto failed;
7226 res = obj2ast_expr(tmp, &context_expr, arena);
7227 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007228 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007229 } else {
7230 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007231 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007232 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007233 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007234 int res;
7235 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7236 if (tmp == NULL) goto failed;
7237 res = obj2ast_expr(tmp, &optional_vars, arena);
7238 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007239 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007240 } else {
7241 optional_vars = NULL;
7242 }
7243 *out = withitem(context_expr, optional_vars, arena);
7244 return 0;
7245failed:
7246 Py_XDECREF(tmp);
7247 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007248}
7249
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007250
Martin v. Löwis1a214512008-06-11 05:26:20 +00007251static struct PyModuleDef _astmodule = {
7252 PyModuleDef_HEAD_INIT, "_ast"
7253};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007254PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007255PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007256{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007257 PyObject *m, *d;
7258 if (!init_types()) return NULL;
7259 m = PyModule_Create(&_astmodule);
7260 if (!m) return NULL;
7261 d = PyModule_GetDict(m);
7262 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007263 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007264 return NULL;
7265 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7266 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7267 NULL;
7268 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7269 0) return NULL;
7270 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7271 return NULL;
7272 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7273 NULL;
7274 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7275 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7276 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007277 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7278 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007279 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7280 return NULL;
7281 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7282 NULL;
7283 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7284 NULL;
7285 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7286 NULL;
7287 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7288 return NULL;
7289 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007290 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7291 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007292 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7293 NULL;
7294 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7295 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007296 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7297 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007298 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7299 NULL;
7300 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7301 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7302 NULL;
7303 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7304 NULL;
7305 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7306 return NULL;
7307 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7308 NULL;
7309 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7310 return NULL;
7311 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7312 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7313 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7314 NULL;
7315 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7316 return NULL;
7317 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7318 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7319 NULL;
7320 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7321 NULL;
7322 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7323 NULL;
7324 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7325 NULL;
7326 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7327 NULL;
7328 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7329 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7330 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7331 return NULL;
7332 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7333 NULL;
7334 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7335 return NULL;
7336 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7337 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007338 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7339 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007340 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7341 NULL;
7342 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7343 return NULL;
7344 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7345 NULL;
7346 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7347 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7348 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
7349 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7350 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007351 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7352 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007353 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7354 return NULL;
7355 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7356 return NULL;
7357 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7358 return NULL;
7359 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7360 NULL;
7361 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7362 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7363 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7364 NULL;
7365 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7366 0) return NULL;
7367 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7368 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7369 NULL;
7370 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7371 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7372 NULL;
7373 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7374 return NULL;
7375 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7376 NULL;
7377 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7378 NULL;
7379 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7380 NULL;
7381 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7382 return NULL;
7383 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7384 NULL;
7385 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7386 NULL;
7387 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7388 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7389 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7390 return NULL;
7391 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7392 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7393 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007394 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7395 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007396 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7397 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7398 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7399 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7400 NULL;
7401 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7402 NULL;
7403 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7404 NULL;
7405 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7406 NULL;
7407 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7408 NULL;
7409 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7410 return NULL;
7411 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7412 NULL;
7413 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7414 NULL;
7415 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7416 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7417 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7418 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7419 NULL;
7420 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7421 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7422 NULL;
7423 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7424 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7425 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7426 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7427 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7428 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7429 NULL;
7430 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7431 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7432 NULL;
7433 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7434 < 0) return NULL;
7435 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7436 < 0) return NULL;
7437 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7438 < 0) return NULL;
7439 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7440 return NULL;
7441 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7442 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7443 NULL;
7444 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7445 NULL;
7446 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7447 return NULL;
7448 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007449}
7450
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007451
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007452PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007453{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007454 if (!init_types())
7455 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007456 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007457}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007458
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007459/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7460mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007461{
7462 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007463 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007464 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007465 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007466
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007467 req_type[0] = (PyObject*)Module_type;
7468 req_type[1] = (PyObject*)Expression_type;
7469 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007470
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007471 assert(0 <= mode && mode <= 2);
7472
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007473 if (!init_types())
7474 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007475
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007476 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7477 if (isinstance == -1)
7478 return NULL;
7479 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007480 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7481 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007482 return NULL;
7483 }
7484 if (obj2ast_mod(ast, &res, arena) != 0)
7485 return NULL;
7486 else
7487 return res;
7488}
7489
7490int PyAST_Check(PyObject* obj)
7491{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007492 if (!init_types())
7493 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007494 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007495}
7496
Martin v. Löwis5b222132007-06-10 09:51:05 +00007497